blob: e8ee40c919d5b44a800237cd1b27b783df8f3b50 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.orgdf697752012-02-08 10:22:21 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/*
12 * Implementation of the actual packet buffer data structure.
13 */
14
15#include "packet_buffer.h"
16
17#include <string.h> /* to define NULL */
18
19#include "signal_processing_library.h"
20
21#include "neteq_error_codes.h"
22
23#ifdef NETEQ_DELAY_LOGGING
24/* special code for offline delay logging */
25#include "delay_logging.h"
26#include <stdio.h>
27
28extern FILE *delay_fid2; /* file pointer to delay log file */
29extern WebRtc_UWord32 tot_received_packets;
30#endif /* NETEQ_DELAY_LOGGING */
31
32
33int WebRtcNetEQ_PacketBufferInit(PacketBuf_t *bufferInst, int maxNoOfPackets,
34 WebRtc_Word16 *pw16_memory, int memorySize)
35{
36 int i;
37 int pos = 0;
38
39 /* Sanity check */
40 if ((memorySize < PBUFFER_MIN_MEMORY_SIZE) || (pw16_memory == NULL)
41 || (maxNoOfPackets < 2) || (maxNoOfPackets > 600))
42 {
43 /* Invalid parameters */
44 return (PBUFFER_INIT_ERROR);
45 }
46
47 /* Clear the buffer instance */
48 WebRtcSpl_MemSetW16((WebRtc_Word16*) bufferInst, 0,
49 sizeof(PacketBuf_t) / sizeof(WebRtc_Word16));
50
51 /* Clear the buffer memory */
52 WebRtcSpl_MemSetW16((WebRtc_Word16*) pw16_memory, 0, memorySize);
53
54 /* Set maximum number of packets */
55 bufferInst->maxInsertPositions = maxNoOfPackets;
56
57 /* Initialize array pointers */
58 /* After each pointer has been set, the index pos is advanced to point immediately
59 * after the the recently allocated vector. Note that one step for the pos index
60 * corresponds to a WebRtc_Word16.
61 */
62
63 bufferInst->timeStamp = (WebRtc_UWord32*) &pw16_memory[pos];
64 pos += maxNoOfPackets << 1; /* advance maxNoOfPackets * WebRtc_UWord32 */
65
66 bufferInst->payloadLocation = (WebRtc_Word16**) &pw16_memory[pos];
67 pos += maxNoOfPackets * (sizeof(WebRtc_Word16*) / sizeof(WebRtc_Word16)); /* advance */
68
69 bufferInst->seqNumber = (WebRtc_UWord16*) &pw16_memory[pos];
70 pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_UWord16 */
71
72 bufferInst->payloadType = &pw16_memory[pos];
73 pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_Word16 */
74
75 bufferInst->payloadLengthBytes = &pw16_memory[pos];
76 pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_Word16 */
77
78 bufferInst->rcuPlCntr = &pw16_memory[pos];
79 pos += maxNoOfPackets; /* advance maxNoOfPackets * WebRtc_Word16 */
80
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +000081 bufferInst->waitingTime = (int*) (&pw16_memory[pos]);
82 /* Advance maxNoOfPackets * sizeof(waitingTime element). */
83 pos += maxNoOfPackets *
84 sizeof(*bufferInst->waitingTime) / sizeof(*pw16_memory);
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086 /* The payload memory starts after the slot arrays */
87 bufferInst->startPayloadMemory = &pw16_memory[pos];
88 bufferInst->currentMemoryPos = bufferInst->startPayloadMemory;
89 bufferInst->memorySizeW16 = (memorySize - pos); /* Remaining memory */
90
91 /* Initialize each payload slot as empty with infinite delay */
92 for (i = 0; i < bufferInst->maxInsertPositions; i++)
93 {
94 bufferInst->payloadType[i] = -1;
95 }
96
97 /* Reset buffer parameters */
98 bufferInst->numPacketsInBuffer = 0;
99 bufferInst->packSizeSamples = 0;
100 bufferInst->insertPosition = 0;
101
102 /* Reset buffer statistics */
103 bufferInst->discardedPackets = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
105 return (0);
106}
107
108
109int WebRtcNetEQ_PacketBufferFlush(PacketBuf_t *bufferInst)
110{
111 int i;
112
113 /* Sanity check */
114 if (bufferInst->startPayloadMemory == NULL)
115 {
116 /* Packet buffer has not been initialized */
117 /* Don't do the flushing operation, since we do not
118 know the state of the struct variables */
119 return (0);
120 }
121
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 /* Set all payload lengths to zero */
123 WebRtcSpl_MemSetW16(bufferInst->payloadLengthBytes, 0, bufferInst->maxInsertPositions);
124
125 /* Reset buffer variables */
126 bufferInst->numPacketsInBuffer = 0;
127 bufferInst->currentMemoryPos = bufferInst->startPayloadMemory;
128 bufferInst->insertPosition = 0;
129
130 /* Clear all slots, starting with the last one */
131 for (i = (bufferInst->maxInsertPositions - 1); i >= 0; i--)
132 {
133 bufferInst->payloadType[i] = -1;
134 bufferInst->timeStamp[i] = 0;
135 bufferInst->seqNumber[i] = 0;
136 }
137
138 return (0);
139}
140
141
142int WebRtcNetEQ_PacketBufferInsert(PacketBuf_t *bufferInst, const RTPPacket_t *RTPpacket,
143 WebRtc_Word16 *flushed)
144{
145 int nextPos;
146 int i;
147
148#ifdef NETEQ_DELAY_LOGGING
149 /* special code for offline delay logging */
150 int temp_var;
151#endif /* NETEQ_DELAY_LOGGING */
152
153 /* Initialize to "no flush" */
154 *flushed = 0;
155
156 /* Sanity check */
157 if (bufferInst->startPayloadMemory == NULL)
158 {
159 /* packet buffer has not been initialized */
160 return (-1);
161 }
162
163 /* Sanity check for payload length
164 (payloadLen in bytes and memory size in WebRtc_Word16) */
165 if ((RTPpacket->payloadLen > (bufferInst->memorySizeW16 << 1)) || (RTPpacket->payloadLen
166 <= 0))
167 {
168 /* faulty or too long payload length */
169 return (-1);
170 }
171
172 /* Find a position in the buffer for this packet */
173 if (bufferInst->numPacketsInBuffer != 0)
174 {
175 /* Get the next slot */
176 bufferInst->insertPosition++;
177 if (bufferInst->insertPosition >= bufferInst->maxInsertPositions)
178 {
179 /* "Wrap around" and start from the beginning */
180 bufferInst->insertPosition = 0;
181 }
182
183 /* Check if there is enough space for the new packet */
184 if (bufferInst->currentMemoryPos + ((RTPpacket->payloadLen + 1) >> 1)
185 >= &bufferInst->startPayloadMemory[bufferInst->memorySizeW16])
186 {
187 WebRtc_Word16 *tempMemAddress;
188
189 /*
190 * Payload does not fit at the end of the memory, put it in the beginning
191 * instead
192 */
193 bufferInst->currentMemoryPos = bufferInst->startPayloadMemory;
194
195 /*
196 * Now, we must search for the next non-empty payload,
197 * finding the one with the lowest start address for the payload
198 */
199 tempMemAddress = &bufferInst->startPayloadMemory[bufferInst->memorySizeW16];
200 nextPos = -1;
201
202 /* Loop through all slots again */
203 for (i = 0; i < bufferInst->maxInsertPositions; i++)
204 {
205 /* Look for the non-empty slot with the lowest
206 payload location address */
207 if (bufferInst->payloadLengthBytes[i] != 0 && bufferInst->payloadLocation[i]
208 < tempMemAddress)
209 {
210 tempMemAddress = bufferInst->payloadLocation[i];
211 nextPos = i;
212 }
213 }
214
215 /* Check that we did find a previous payload */
216 if (nextPos == -1)
217 {
218 /* The buffer is corrupt => flush and return error */
219 WebRtcNetEQ_PacketBufferFlush(bufferInst);
220 *flushed = 1;
221 return (-1);
222 }
223 }
224 else
225 {
226 /* Payload fits at the end of memory. */
227
228 /* Find the next non-empty slot. */
229 nextPos = bufferInst->insertPosition + 1;
230
231 /* Increase nextPos until a non-empty slot is found or end of array is encountered*/
232 while ((bufferInst->payloadLengthBytes[nextPos] == 0) && (nextPos
233 < bufferInst->maxInsertPositions))
234 {
235 nextPos++;
236 }
237
238 if (nextPos == bufferInst->maxInsertPositions)
239 {
240 /*
241 * Reached the end of the array, so there must be a packet in the first
242 * position instead
243 */
244 nextPos = 0;
245
246 /* Increase nextPos until a non-empty slot is found */
247 while (bufferInst->payloadLengthBytes[nextPos] == 0)
248 {
249 nextPos++;
250 }
251 }
252 } /* end if-else */
253
254 /*
255 * Check if the new payload will extend into a payload later in memory.
256 * If so, the buffer is full.
257 */
258 if ((bufferInst->currentMemoryPos <= bufferInst->payloadLocation[nextPos])
259 && ((&bufferInst->currentMemoryPos[(RTPpacket->payloadLen + 1) >> 1])
260 > bufferInst->payloadLocation[nextPos]))
261 {
262 /* Buffer is full, so the buffer must be flushed */
263 WebRtcNetEQ_PacketBufferFlush(bufferInst);
264 *flushed = 1;
265 }
266
267 if (bufferInst->payloadLengthBytes[bufferInst->insertPosition] != 0)
268 {
269 /* All positions are already taken and entire buffer should be flushed */
270 WebRtcNetEQ_PacketBufferFlush(bufferInst);
271 *flushed = 1;
272 }
273
274 }
275 else
276 {
277 /* Buffer is empty, just insert the packet at the beginning */
278 bufferInst->currentMemoryPos = bufferInst->startPayloadMemory;
279 bufferInst->insertPosition = 0;
280 }
281
282 /* Insert packet in the found position */
283 if (RTPpacket->starts_byte1 == 0)
284 {
285 /* Payload is 16-bit aligned => just copy it */
286
287 WEBRTC_SPL_MEMCPY_W16(bufferInst->currentMemoryPos,
288 RTPpacket->payload, (RTPpacket->payloadLen + 1) >> 1);
289 }
290 else
291 {
292 /* Payload is not 16-bit aligned => align it during copy operation */
293 for (i = 0; i < RTPpacket->payloadLen; i++)
294 {
295 /* copy the (i+1)-th byte to the i-th byte */
296
297 WEBRTC_SPL_SET_BYTE(bufferInst->currentMemoryPos,
298 (WEBRTC_SPL_GET_BYTE(RTPpacket->payload, (i + 1))), i);
299 }
300 }
301
302 /* Copy the packet information */
303 bufferInst->payloadLocation[bufferInst->insertPosition] = bufferInst->currentMemoryPos;
304 bufferInst->payloadLengthBytes[bufferInst->insertPosition] = RTPpacket->payloadLen;
305 bufferInst->payloadType[bufferInst->insertPosition] = RTPpacket->payloadType;
306 bufferInst->seqNumber[bufferInst->insertPosition] = RTPpacket->seqNumber;
307 bufferInst->timeStamp[bufferInst->insertPosition] = RTPpacket->timeStamp;
308 bufferInst->rcuPlCntr[bufferInst->insertPosition] = RTPpacket->rcuPlCntr;
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000309 bufferInst->rcuPlCntr[bufferInst->insertPosition] = 0;
310 bufferInst->waitingTime[bufferInst->insertPosition] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000311 /* Update buffer parameters */
312 bufferInst->numPacketsInBuffer++;
313 bufferInst->currentMemoryPos += (RTPpacket->payloadLen + 1) >> 1;
314
315#ifdef NETEQ_DELAY_LOGGING
316 /* special code for offline delay logging */
317 if (*flushed)
318 {
319 temp_var = NETEQ_DELAY_LOGGING_SIGNAL_FLUSH;
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000320 if (fwrite(&temp_var, sizeof(int), 1, delay_fid2) != 1) {
321 return -1;
322 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 }
324 temp_var = NETEQ_DELAY_LOGGING_SIGNAL_RECIN;
leozwang@webrtc.org354b0ed2012-06-01 17:46:21 +0000325 if ((fwrite(&temp_var, sizeof(int),
326 1, delay_fid2) != 1) ||
327 (fwrite(&RTPpacket->timeStamp, sizeof(WebRtc_UWord32),
328 1, delay_fid2) != 1) ||
329 (fwrite(&RTPpacket->seqNumber, sizeof(WebRtc_UWord16),
330 1, delay_fid2) != 1) ||
331 (fwrite(&RTPpacket->payloadType, sizeof(int),
332 1, delay_fid2) != 1) ||
333 (fwrite(&RTPpacket->payloadLen, sizeof(WebRtc_Word16),
334 1, delay_fid2) != 1)) {
335 return -1;
336 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000337 tot_received_packets++;
338#endif /* NETEQ_DELAY_LOGGING */
339
340 return (0);
341}
342
343
344int WebRtcNetEQ_PacketBufferExtract(PacketBuf_t *bufferInst, RTPPacket_t *RTPpacket,
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000345 int bufferPosition, int *waitingTime)
niklase@google.com470e71d2011-07-07 08:21:25 +0000346{
347
348 /* Sanity check */
349 if (bufferInst->startPayloadMemory == NULL)
350 {
351 /* packet buffer has not been initialized */
352 return (PBUFFER_NOT_INITIALIZED);
353 }
354
355 if (bufferPosition < 0 || bufferPosition >= bufferInst->maxInsertPositions)
356 {
357 /* buffer position is outside valid range */
358 return (NETEQ_OTHER_ERROR);
359 }
360
361 /* Check that there is a valid payload in the specified position */
362 if (bufferInst->payloadLengthBytes[bufferPosition] <= 0)
363 {
364 /* The position does not contain a valid payload */
365 RTPpacket->payloadLen = 0; /* Set zero length */
366 return (PBUFFER_NONEXISTING_PACKET); /* Return error */
367 }
368
369 /* Payload exists => extract payload data */
370
371 /* Copy the actual data payload to RTP packet struct */
372
373 WEBRTC_SPL_MEMCPY_W16((WebRtc_Word16*) RTPpacket->payload,
374 bufferInst->payloadLocation[bufferPosition],
375 (bufferInst->payloadLengthBytes[bufferPosition] + 1) >> 1); /*length in WebRtc_Word16*/
376
377 /* Copy payload parameters */
378 RTPpacket->payloadLen = bufferInst->payloadLengthBytes[bufferPosition];
379 RTPpacket->payloadType = bufferInst->payloadType[bufferPosition];
380 RTPpacket->seqNumber = bufferInst->seqNumber[bufferPosition];
381 RTPpacket->timeStamp = bufferInst->timeStamp[bufferPosition];
382 RTPpacket->rcuPlCntr = bufferInst->rcuPlCntr[bufferPosition];
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000383 *waitingTime = bufferInst->waitingTime[bufferPosition];
niklase@google.com470e71d2011-07-07 08:21:25 +0000384 RTPpacket->starts_byte1 = 0; /* payload is 16-bit aligned */
385
386 /* Clear the position in the packet buffer */
387 bufferInst->payloadType[bufferPosition] = -1;
388 bufferInst->payloadLengthBytes[bufferPosition] = 0;
389 bufferInst->seqNumber[bufferPosition] = 0;
390 bufferInst->timeStamp[bufferPosition] = 0;
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000391 bufferInst->waitingTime[bufferPosition] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000392 bufferInst->payloadLocation[bufferPosition] = bufferInst->startPayloadMemory;
393
394 /* Reduce packet counter with one */
395 bufferInst->numPacketsInBuffer--;
396
397 return (0);
398}
399
kma@webrtc.org89a10002012-01-30 15:37:33 +0000400int WebRtcNetEQ_PacketBufferFindLowestTimestamp(PacketBuf_t* buffer_inst,
401 uint32_t current_time_stamp,
402 uint32_t* time_stamp,
403 int* buffer_position,
404 int erase_old_packets,
405 int16_t* payload_type) {
406 int32_t time_stamp_diff = WEBRTC_SPL_WORD32_MAX; /* Smallest diff found. */
407 int32_t new_diff;
408 int i;
409 int16_t rcu_payload_cntr;
niklase@google.com470e71d2011-07-07 08:21:25 +0000410
kma@webrtc.org89a10002012-01-30 15:37:33 +0000411 if (buffer_inst->startPayloadMemory == NULL) {
412 /* Packet buffer has not been initialized. */
413 return PBUFFER_NOT_INITIALIZED;
414 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000415
kma@webrtc.org89a10002012-01-30 15:37:33 +0000416 /* Initialize all return values. */
417 *time_stamp = 0;
418 *payload_type = -1; /* Indicates that no packet was found. */
419 *buffer_position = -1; /* Indicates that no packet was found. */
420 rcu_payload_cntr = WEBRTC_SPL_WORD16_MAX; /* Indicates no packet found. */
niklase@google.com470e71d2011-07-07 08:21:25 +0000421
kma@webrtc.org89a10002012-01-30 15:37:33 +0000422 /* Check if buffer is empty. */
423 if (buffer_inst->numPacketsInBuffer <= 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000424 return 0;
kma@webrtc.org89a10002012-01-30 15:37:33 +0000425 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000426
kma@webrtc.org89a10002012-01-30 15:37:33 +0000427 /* Loop through all slots in buffer. */
428 if (erase_old_packets) { /* If old payloads should be discarded. */
429 for (i = 0; i < buffer_inst->maxInsertPositions; ++i) {
430 /* Calculate difference between this slot and current_time_stamp. */
431 new_diff = (int32_t)(buffer_inst->timeStamp[i] - current_time_stamp);
432
433 /* Check if payload should be discarded. */
434 if ((new_diff < 0) /* Payload is too old */
435 && (new_diff > -30000) /* Account for TS wrap-around. */
436 && (buffer_inst->payloadLengthBytes[i] > 0)) { /* Payload exists. */
437 /* Throw away old packet. */
438
439 /* Clear the position in the buffer. */
440 buffer_inst->payloadType[i] = -1;
441 buffer_inst->payloadLengthBytes[i] = 0;
442
443 /* Reduce packet counter by one. */
444 buffer_inst->numPacketsInBuffer--;
445 /* Increase discard counter for in-call statistics. */
446 buffer_inst->discardedPackets++;
447 } else if (((new_diff < time_stamp_diff)
448 || ((new_diff == time_stamp_diff)
449 && (buffer_inst->rcuPlCntr[i] < rcu_payload_cntr)))
450 && (buffer_inst->payloadLengthBytes[i] > 0)) {
451 /* New diff is smaller than previous diffs or we have a candidate with a
452 * time stamp as previous candidate but better RCU-counter;
453 * and the payload exists.
454 */
455 /* Save this position as the best candidate. */
456 *buffer_position = i;
457 time_stamp_diff = new_diff;
458 *payload_type = buffer_inst->payloadType[i];
459 rcu_payload_cntr = buffer_inst->rcuPlCntr[i];
460 }
461 }
462 } else {
463 for (i = 0; i < buffer_inst->maxInsertPositions; ++i) {
464 /* Calculate difference between this slot and current_time_stamp. */
465 new_diff = (int32_t)(buffer_inst->timeStamp[i] - current_time_stamp);
466
467 /* Check if this is the oldest packet. */
468 if (((new_diff < time_stamp_diff)
469 || ((new_diff == time_stamp_diff)
470 && (buffer_inst->rcuPlCntr[i] < rcu_payload_cntr)))
471 && (buffer_inst->payloadLengthBytes[i] > 0)) {
472 /* New diff is smaller than previous diffs or we have a candidate with a
473 * time_stamp as previous candidate but better RCU-counter;
474 * and the payload exists.
475 */
476 /* Save this position as the best candidate. */
477 *buffer_position = i;
478 time_stamp_diff = new_diff;
479 *payload_type = buffer_inst->payloadType[i];
480 rcu_payload_cntr = buffer_inst->rcuPlCntr[i];
481 }
482 }
483 }
484
485 /* Check that we did find a real position. */
486 if (*buffer_position >= 0) {
487 /* Get the time_stamp for the best position. */
488 *time_stamp = buffer_inst->timeStamp[*buffer_position];
489 }
490
491 return 0;
492}
niklase@google.com470e71d2011-07-07 08:21:25 +0000493
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000494int WebRtcNetEQ_PacketBufferGetPacketSize(const PacketBuf_t* buffer_inst,
495 int buffer_pos,
496 const CodecDbInst_t* codec_database,
497 int codec_pos, int last_duration) {
498 if (codec_database->funcDurationEst[codec_pos] == NULL) {
499 return last_duration;
500 }
501 return (*codec_database->funcDurationEst[codec_pos])(
502 codec_database->codec_state[codec_pos],
503 (const uint8_t *)buffer_inst->payloadLocation[buffer_pos],
504 buffer_inst->payloadLengthBytes[buffer_pos]);
505}
niklase@google.com470e71d2011-07-07 08:21:25 +0000506
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000507WebRtc_Word32 WebRtcNetEQ_PacketBufferGetSize(const PacketBuf_t* buffer_inst,
508 const CodecDbInst_t*
509 codec_database) {
510 int i, count;
511 int last_duration;
512 int last_codec_pos;
513 int last_payload_type;
514 WebRtc_Word32 size_samples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000515
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000516 count = 0;
517 last_duration = buffer_inst->packSizeSamples;
518 last_codec_pos = -1;
519 last_payload_type = -1;
520 size_samples = 0;
521
522 /* Loop through all slots in the buffer */
523 for (i = 0; i < buffer_inst->maxInsertPositions; i++) {
524 /* Only count the packets with non-zero size */
525 if (buffer_inst->payloadLengthBytes[i] != 0) {
526 int payload_type;
527 int codec_pos;
528 /* Figure out the codec database entry for this payload_type. */
529 payload_type = buffer_inst->payloadType[i];
530 /* Remember the last one, to avoid the database search. */
531 if(payload_type == last_payload_type) {
532 codec_pos = last_codec_pos;
533 }
534 else {
535 codec_pos = WebRtcNetEQ_DbGetCodec(codec_database,
536 payload_type);
537 if (codec_pos >= 0) {
538 codec_pos = codec_database->position[codec_pos];
niklase@google.com470e71d2011-07-07 08:21:25 +0000539 }
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000540 }
541 last_codec_pos = codec_pos;
542 last_payload_type = payload_type;
543 if (codec_pos >= 0) {
544 /*
545 * Right now WebRtcNetEQ_PacketBufferGetPacketSize either always
546 * returns last_duration or always computes the real duration without
547 * looking at last_duration. If an implementation really wanted to use
548 * last_duration to compute a changing duration, we would have to
549 * iterate through the packets in chronological order by timestamp.
550 */
551 last_duration = WebRtcNetEQ_PacketBufferGetPacketSize(
552 buffer_inst, i, codec_database, codec_pos,
553 last_duration);
554 }
555 /* Add in the size of this packet. */
556 size_samples += last_duration;
557 count++;
niklase@google.com470e71d2011-07-07 08:21:25 +0000558 }
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000559 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000560
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000561 /* Sanity check; size cannot be negative */
562 if (size_samples < 0) {
563 size_samples = 0;
564 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000565
tina.legrand@webrtc.org5ac387c2012-11-19 08:02:55 +0000566 return size_samples;
niklase@google.com470e71d2011-07-07 08:21:25 +0000567}
568
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000569void WebRtcNetEQ_IncrementWaitingTimes(PacketBuf_t *buffer_inst) {
570 int i;
571 /* Loop through all slots in the buffer. */
572 for (i = 0; i < buffer_inst->maxInsertPositions; ++i) {
573 /* Only increment waiting time for the packets with non-zero size. */
574 if (buffer_inst->payloadLengthBytes[i] != 0) {
575 buffer_inst->waitingTime[i]++;
576 }
577 }
578}
niklase@google.com470e71d2011-07-07 08:21:25 +0000579
580int WebRtcNetEQ_GetDefaultCodecSettings(const enum WebRtcNetEQDecoder *codecID,
581 int noOfCodecs, int *maxBytes, int *maxSlots)
582{
583 int i;
584 int ok = 0;
585 WebRtc_Word16 w16_tmp;
586 WebRtc_Word16 codecBytes;
587 WebRtc_Word16 codecBuffers;
588
589 /* Initialize return variables to zero */
590 *maxBytes = 0;
591 *maxSlots = 0;
592
593 /* Loop through all codecs supplied to function */
594 for (i = 0; i < noOfCodecs; i++)
595 {
596 /* Find current codec and set parameters accordingly */
597
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000598 if ((codecID[i] == kDecoderPCMu) || (codecID[i] == kDecoderPCMu_2ch))
niklase@google.com470e71d2011-07-07 08:21:25 +0000599 {
600 codecBytes = 1680; /* Up to 210ms @ 64kbps */
601 codecBuffers = 30; /* Down to 5ms frames */
602 }
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000603 else if ((codecID[i] == kDecoderPCMa) ||
604 (codecID[i] == kDecoderPCMa_2ch))
niklase@google.com470e71d2011-07-07 08:21:25 +0000605 {
606 codecBytes = 1680; /* Up to 210ms @ 64kbps */
607 codecBuffers = 30; /* Down to 5ms frames */
608 }
609 else if (codecID[i] == kDecoderILBC)
610 {
611 codecBytes = 380; /* 200ms @ 15.2kbps (20ms frames) */
612 codecBuffers = 10;
613 }
614 else if (codecID[i] == kDecoderISAC)
615 {
616 codecBytes = 960; /* 240ms @ 32kbps (60ms frames) */
617 codecBuffers = 8;
618 }
619 else if (codecID[i] == kDecoderISACswb)
620 {
621 codecBytes = 1560; /* 240ms @ 52kbps (30ms frames) */
622 codecBuffers = 8;
623 }
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000624 else if ((codecID[i] == kDecoderOpus) ||
625 (codecID[i] == kDecoderOpus_2ch))
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000626 {
627 codecBytes = 15300; /* 240ms @ 510kbps (60ms frames) */
628 codecBuffers = 30; /* Replicating the value for PCMu/a */
629 }
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000630 else if ((codecID[i] == kDecoderPCM16B) ||
631 (codecID[i] == kDecoderPCM16B_2ch))
niklase@google.com470e71d2011-07-07 08:21:25 +0000632 {
633 codecBytes = 3360; /* 210ms */
634 codecBuffers = 15;
635 }
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000636 else if ((codecID[i] == kDecoderPCM16Bwb) ||
637 (codecID[i] == kDecoderPCM16Bwb_2ch))
niklase@google.com470e71d2011-07-07 08:21:25 +0000638 {
639 codecBytes = 6720; /* 210ms */
640 codecBuffers = 15;
641 }
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000642 else if ((codecID[i] == kDecoderPCM16Bswb32kHz) ||
643 (codecID[i] == kDecoderPCM16Bswb32kHz_2ch))
niklase@google.com470e71d2011-07-07 08:21:25 +0000644 {
645 codecBytes = 13440; /* 210ms */
646 codecBuffers = 15;
647 }
648 else if (codecID[i] == kDecoderPCM16Bswb48kHz)
649 {
650 codecBytes = 20160; /* 210ms */
651 codecBuffers = 15;
652 }
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000653 else if ((codecID[i] == kDecoderG722) ||
654 (codecID[i] == kDecoderG722_2ch))
niklase@google.com470e71d2011-07-07 08:21:25 +0000655 {
656 codecBytes = 1680; /* 210ms @ 64kbps */
657 codecBuffers = 15;
658 }
659 else if (codecID[i] == kDecoderRED)
660 {
661 codecBytes = 0; /* Should not be max... */
662 codecBuffers = 0;
663 }
664 else if (codecID[i] == kDecoderAVT)
665 {
666 codecBytes = 0; /* Should not be max... */
667 codecBuffers = 0;
668 }
669 else if (codecID[i] == kDecoderCNG)
670 {
671 codecBytes = 0; /* Should not be max... */
672 codecBuffers = 0;
673 }
674 else if (codecID[i] == kDecoderG729)
675 {
676 codecBytes = 210; /* 210ms @ 8kbps */
677 codecBuffers = 20; /* max 200ms supported for 10ms frames */
678 }
679 else if (codecID[i] == kDecoderG729_1)
680 {
681 codecBytes = 840; /* 210ms @ 32kbps */
682 codecBuffers = 10; /* max 200ms supported for 20ms frames */
683 }
684 else if (codecID[i] == kDecoderG726_16)
685 {
686 codecBytes = 400; /* 200ms @ 16kbps */
687 codecBuffers = 10;
688 }
689 else if (codecID[i] == kDecoderG726_24)
690 {
691 codecBytes = 600; /* 200ms @ 24kbps */
692 codecBuffers = 10;
693 }
694 else if (codecID[i] == kDecoderG726_32)
695 {
696 codecBytes = 800; /* 200ms @ 32kbps */
697 codecBuffers = 10;
698 }
699 else if (codecID[i] == kDecoderG726_40)
700 {
701 codecBytes = 1000; /* 200ms @ 40kbps */
702 codecBuffers = 10;
703 }
704 else if (codecID[i] == kDecoderG722_1_16)
705 {
706 codecBytes = 420; /* 210ms @ 16kbps */
707 codecBuffers = 10;
708 }
709 else if (codecID[i] == kDecoderG722_1_24)
710 {
711 codecBytes = 630; /* 210ms @ 24kbps */
712 codecBuffers = 10;
713 }
714 else if (codecID[i] == kDecoderG722_1_32)
715 {
716 codecBytes = 840; /* 210ms @ 32kbps */
717 codecBuffers = 10;
718 }
719 else if (codecID[i] == kDecoderG722_1C_24)
720 {
721 codecBytes = 630; /* 210ms @ 24kbps */
722 codecBuffers = 10;
723 }
724 else if (codecID[i] == kDecoderG722_1C_32)
725 {
726 codecBytes = 840; /* 210ms @ 32kbps */
727 codecBuffers = 10;
728 }
729 else if (codecID[i] == kDecoderG722_1C_48)
730 {
731 codecBytes = 1260; /* 210ms @ 48kbps */
732 codecBuffers = 10;
733 }
734 else if (codecID[i] == kDecoderSPEEX_8)
735 {
736 codecBytes = 1250; /* 210ms @ 50kbps */
737 codecBuffers = 10;
738 }
739 else if (codecID[i] == kDecoderSPEEX_16)
740 {
741 codecBytes = 1250; /* 210ms @ 50kbps */
742 codecBuffers = 10;
743 }
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000744 else if ((codecID[i] == kDecoderCELT_32) ||
745 (codecID[i] == kDecoderCELT_32_2ch))
tina.legrand@webrtc.orgdf697752012-02-08 10:22:21 +0000746 {
747 codecBytes = 1250; /* 210ms @ 50kbps */
748 codecBuffers = 10;
749 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000750 else if (codecID[i] == kDecoderGSMFR)
751 {
752 codecBytes = 340; /* 200ms */
753 codecBuffers = 10;
754 }
755 else if (codecID[i] == kDecoderAMR)
756 {
757 codecBytes = 384; /* 240ms @ 12.2kbps+headers (60ms frames) */
758 codecBuffers = 10;
759 }
760 else if (codecID[i] == kDecoderAMRWB)
761 {
762 codecBytes = 744;
763 codecBuffers = 10;
764 }
765 else if (codecID[i] == kDecoderArbitrary)
766 {
767 codecBytes = 6720; /* Assume worst case uncompressed WB 210ms */
768 codecBuffers = 15;
769 }
770 else
771 {
772 /*Unknow codec */
773 codecBytes = 0;
774 codecBuffers = 0;
775 ok = CODEC_DB_UNKNOWN_CODEC;
776 }
777
778 /* Update max variables */
779 *maxBytes = WEBRTC_SPL_MAX((*maxBytes), codecBytes);
780 *maxSlots = WEBRTC_SPL_MAX((*maxSlots), codecBuffers);
781
782 } /* end of for loop */
783
784 /*
785 * Add size needed by the additional pointers for each slot inside struct,
786 * as indicated on each line below.
787 */
788 w16_tmp = (sizeof(WebRtc_UWord32) /* timeStamp */
789 + sizeof(WebRtc_Word16*) /* payloadLocation */
790 + sizeof(WebRtc_UWord16) /* seqNumber */
henrik.lundin@webrtc.orgdbba1f92011-12-20 15:45:05 +0000791 + sizeof(WebRtc_Word16) /* payloadType */
792 + sizeof(WebRtc_Word16) /* payloadLengthBytes */
793 + sizeof(WebRtc_Word16) /* rcuPlCntr */
794 + sizeof(int)); /* waitingTime */
niklase@google.com470e71d2011-07-07 08:21:25 +0000795 /* Add the extra size per slot to the memory count */
796 *maxBytes += w16_tmp * (*maxSlots);
797
798 return ok;
799}