blob: 56039a57ec6d280aac562398d5d8326580ef0cf7 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
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#include "webrtc/modules/audio_coding/neteq4/payload_splitter.h"
12
13#include <assert.h>
14
15#include "webrtc/modules/audio_coding/neteq4/decoder_database.h"
16
17namespace webrtc {
18
19// The method loops through a list of packets {A, B, C, ...}. Each packet is
20// split into its corresponding RED payloads, {A1, A2, ...}, which is
21// temporarily held in the list |new_packets|.
22// When the first packet in |packet_list| has been processed, the orignal packet
23// is replaced by the new ones in |new_packets|, so that |packet_list| becomes:
24// {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all
25// the original packets have been replaced by their split payloads.
26int PayloadSplitter::SplitRed(PacketList* packet_list) {
27 int ret = kOK;
28 PacketList::iterator it = packet_list->begin();
29 while (it != packet_list->end()) {
30 PacketList new_packets; // An empty list to store the split packets in.
31 Packet* red_packet = (*it);
32 assert(red_packet->payload);
33 uint8_t* payload_ptr = red_packet->payload;
34
35 // Read RED headers (according to RFC 2198):
36 //
37 // 0 1 2 3
38 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
39 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 // |F| block PT | timestamp offset | block length |
41 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 // Last RED header:
43 // 0 1 2 3 4 5 6 7
44 // +-+-+-+-+-+-+-+-+
45 // |0| Block PT |
46 // +-+-+-+-+-+-+-+-+
47
48 bool last_block = false;
49 int sum_length = 0;
50 while (!last_block) {
51 Packet* new_packet = new Packet;
52 new_packet->header = red_packet->header;
53 // Check the F bit. If F == 0, this was the last block.
54 last_block = ((*payload_ptr & 0x80) == 0);
55 // Bits 1 through 7 are payload type.
56 new_packet->header.payloadType = payload_ptr[0] & 0x7F;
57 if (last_block) {
58 // No more header data to read.
59 ++sum_length; // Account for RED header size of 1 byte.
60 new_packet->payload_length = red_packet->payload_length - sum_length;
61 new_packet->primary = true; // Last block is always primary.
62 payload_ptr += 1; // Advance to first payload byte.
63 } else {
64 // Bits 8 through 21 are timestamp offset.
65 int timestamp_offset = (payload_ptr[1] << 6) +
66 ((payload_ptr[2] & 0xFC) >> 2);
67 new_packet->header.timestamp = red_packet->header.timestamp -
68 timestamp_offset;
69 // Bits 22 through 31 are payload length.
70 new_packet->payload_length = ((payload_ptr[2] & 0x03) << 8) +
71 payload_ptr[3];
72 new_packet->primary = false;
73 payload_ptr += 4; // Advance to next RED header.
74 }
75 sum_length += new_packet->payload_length;
76 sum_length += 4; // Account for RED header size of 4 bytes.
77 // Store in new list of packets.
78 new_packets.push_back(new_packet);
79 }
80
81 // Populate the new packets with payload data.
82 // |payload_ptr| now points at the first payload byte.
83 PacketList::iterator new_it;
84 for (new_it = new_packets.begin(); new_it != new_packets.end(); ++new_it) {
85 int payload_length = (*new_it)->payload_length;
86 if (payload_ptr + payload_length >
87 red_packet->payload + red_packet->payload_length) {
88 // The block lengths in the RED headers do not match the overall packet
89 // length. Something is corrupt. Discard this and the remaining
90 // payloads from this packet.
91 while (new_it != new_packets.end()) {
92 // Payload should not have been allocated yet.
93 assert(!(*new_it)->payload);
94 delete (*new_it);
95 new_it = new_packets.erase(new_it);
96 }
97 ret = kRedLengthMismatch;
98 break;
99 }
100 (*new_it)->payload = new uint8_t[payload_length];
101 memcpy((*new_it)->payload, payload_ptr, payload_length);
102 payload_ptr += payload_length;
103 }
104 // Reverse the order of the new packets, so that the primary payload is
105 // always first.
106 new_packets.reverse();
107 // Insert new packets into original list, before the element pointed to by
108 // iterator |it|.
109 packet_list->splice(it, new_packets, new_packets.begin(),
110 new_packets.end());
111 // Delete old packet payload.
112 delete [] (*it)->payload;
113 delete (*it);
114 // Remove |it| from the packet list. This operation effectively moves the
115 // iterator |it| to the next packet in the list. Thus, we do not have to
116 // increment it manually.
117 it = packet_list->erase(it);
118 }
119 return ret;
120}
121
122int PayloadSplitter::CheckRedPayloads(PacketList* packet_list,
123 const DecoderDatabase& decoder_database) {
124 PacketList::iterator it = packet_list->begin();
125 int main_payload_type = -1;
126 int num_deleted_packets = 0;
127 while (it != packet_list->end()) {
128 uint8_t this_payload_type = (*it)->header.payloadType;
129 if (!decoder_database.IsDtmf(this_payload_type) &&
130 !decoder_database.IsComfortNoise(this_payload_type)) {
131 if (main_payload_type == -1) {
132 // This is the first packet in the list which is non-DTMF non-CNG.
133 main_payload_type = this_payload_type;
134 } else {
135 if (this_payload_type != main_payload_type) {
136 // We do not allow redundant payloads of a different type.
137 // Discard this payload.
138 delete [] (*it)->payload;
139 delete (*it);
140 // Remove |it| from the packet list. This operation effectively
141 // moves the iterator |it| to the next packet in the list. Thus, we
142 // do not have to increment it manually.
143 it = packet_list->erase(it);
144 ++num_deleted_packets;
145 continue;
146 }
147 }
148 }
149 ++it;
150 }
151 return num_deleted_packets;
152}
153
154int PayloadSplitter::SplitAudio(PacketList* packet_list,
155 const DecoderDatabase& decoder_database) {
156 PacketList::iterator it = packet_list->begin();
157 // Iterate through all packets in |packet_list|.
158 while (it != packet_list->end()) {
159 Packet* packet = (*it); // Just to make the notation more intuitive.
160 // Get codec type for this payload.
161 const DecoderDatabase::DecoderInfo* info =
162 decoder_database.GetDecoderInfo(packet->header.payloadType);
163 if (!info) {
164 return kUnknownPayloadType;
165 }
turaj@webrtc.org7b75ac62013-09-26 00:27:56 +0000166 // No splitting for a sync-packet.
167 if (packet->sync_packet) {
168 ++it;
169 continue;
170 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171 PacketList new_packets;
172 switch (info->codec_type) {
173 case kDecoderPCMu:
174 case kDecoderPCMa: {
175 // 8 bytes per ms; 8 timestamps per ms.
176 SplitBySamples(packet, 8, 8, &new_packets);
177 break;
178 }
179 case kDecoderPCMu_2ch:
180 case kDecoderPCMa_2ch: {
181 // 2 * 8 bytes per ms; 8 timestamps per ms.
182 SplitBySamples(packet, 2 * 8, 8, &new_packets);
183 break;
184 }
185 case kDecoderG722: {
186 // 8 bytes per ms; 16 timestamps per ms.
187 SplitBySamples(packet, 8, 16, &new_packets);
188 break;
189 }
190 case kDecoderPCM16B: {
191 // 16 bytes per ms; 8 timestamps per ms.
192 SplitBySamples(packet, 16, 8, &new_packets);
193 break;
194 }
195 case kDecoderPCM16Bwb: {
196 // 32 bytes per ms; 16 timestamps per ms.
197 SplitBySamples(packet, 32, 16, &new_packets);
198 break;
199 }
200 case kDecoderPCM16Bswb32kHz: {
201 // 64 bytes per ms; 32 timestamps per ms.
202 SplitBySamples(packet, 64, 32, &new_packets);
203 break;
204 }
205 case kDecoderPCM16Bswb48kHz: {
206 // 96 bytes per ms; 48 timestamps per ms.
207 SplitBySamples(packet, 96, 48, &new_packets);
208 break;
209 }
210 case kDecoderPCM16B_2ch: {
211 // 2 * 16 bytes per ms; 8 timestamps per ms.
212 SplitBySamples(packet, 2 * 16, 8, &new_packets);
213 break;
214 }
215 case kDecoderPCM16Bwb_2ch: {
216 // 2 * 32 bytes per ms; 16 timestamps per ms.
217 SplitBySamples(packet, 2 * 32, 16, &new_packets);
218 break;
219 }
220 case kDecoderPCM16Bswb32kHz_2ch: {
221 // 2 * 64 bytes per ms; 32 timestamps per ms.
222 SplitBySamples(packet, 2 * 64, 32, &new_packets);
223 break;
224 }
225 case kDecoderPCM16Bswb48kHz_2ch: {
226 // 2 * 96 bytes per ms; 48 timestamps per ms.
227 SplitBySamples(packet, 2 * 96, 48, &new_packets);
228 break;
229 }
230 case kDecoderPCM16B_5ch: {
231 // 5 * 16 bytes per ms; 8 timestamps per ms.
232 SplitBySamples(packet, 5 * 16, 8, &new_packets);
233 break;
234 }
235 case kDecoderILBC: {
236 int bytes_per_frame;
237 int timestamps_per_frame;
238 if (packet->payload_length >= 950) {
239 return kTooLargePayload;
240 } else if (packet->payload_length % 38 == 0) {
241 // 20 ms frames.
242 bytes_per_frame = 38;
243 timestamps_per_frame = 160;
244 } else if (packet->payload_length % 50 == 0) {
245 // 30 ms frames.
246 bytes_per_frame = 50;
247 timestamps_per_frame = 240;
248 } else {
249 return kFrameSplitError;
250 }
251 int ret = SplitByFrames(packet, bytes_per_frame, timestamps_per_frame,
252 &new_packets);
253 if (ret < 0) {
254 return ret;
255 } else if (ret == kNoSplit) {
256 // Do not split at all. Simply advance to the next packet in the list.
257 ++it;
258 // We do not have any new packets to insert, and should not delete the
259 // old one. Skip the code after the switch case, and jump straight to
260 // the next packet in the while loop.
261 continue;
262 }
263 break;
264 }
265 default: {
266 // Do not split at all. Simply advance to the next packet in the list.
267 ++it;
268 // We do not have any new packets to insert, and should not delete the
269 // old one. Skip the code after the switch case, and jump straight to
270 // the next packet in the while loop.
271 continue;
272 }
273 }
274 // Insert new packets into original list, before the element pointed to by
275 // iterator |it|.
276 packet_list->splice(it, new_packets, new_packets.begin(),
277 new_packets.end());
278 // Delete old packet payload.
279 delete [] (*it)->payload;
280 delete (*it);
281 // Remove |it| from the packet list. This operation effectively moves the
282 // iterator |it| to the next packet in the list. Thus, we do not have to
283 // increment it manually.
284 it = packet_list->erase(it);
285 }
286 return 0;
287}
288
289void PayloadSplitter::SplitBySamples(const Packet* packet,
290 int bytes_per_ms,
291 int timestamps_per_ms,
292 PacketList* new_packets) {
293 assert(packet);
294 assert(new_packets);
295
296 int split_size_bytes = packet->payload_length;
297
298 // Find a "chunk size" >= 20 ms and < 40 ms.
299 int min_chunk_size = bytes_per_ms * 20;
300 // Reduce the split size by half as long as |split_size_bytes| is at least
301 // twice the minimum chunk size (so that the resulting size is at least as
302 // large as the minimum chunk size).
303 while (split_size_bytes >= 2 * min_chunk_size) {
304 split_size_bytes >>= 1;
305 }
306 int timestamps_per_chunk =
307 split_size_bytes * timestamps_per_ms / bytes_per_ms;
308 uint32_t timestamp = packet->header.timestamp;
309
310 uint8_t* payload_ptr = packet->payload;
311 int len = packet->payload_length;
312 while (len >= (2 * split_size_bytes)) {
313 Packet* new_packet = new Packet;
314 new_packet->payload_length = split_size_bytes;
315 new_packet->header = packet->header;
316 new_packet->header.timestamp = timestamp;
317 timestamp += timestamps_per_chunk;
318 new_packet->primary = packet->primary;
319 new_packet->payload = new uint8_t[split_size_bytes];
320 memcpy(new_packet->payload, payload_ptr, split_size_bytes);
321 payload_ptr += split_size_bytes;
322 new_packets->push_back(new_packet);
323 len -= split_size_bytes;
324 }
325
326 if (len > 0) {
327 Packet* new_packet = new Packet;
328 new_packet->payload_length = len;
329 new_packet->header = packet->header;
330 new_packet->header.timestamp = timestamp;
331 new_packet->primary = packet->primary;
332 new_packet->payload = new uint8_t[len];
333 memcpy(new_packet->payload, payload_ptr, len);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334 new_packets->push_back(new_packet);
335 }
336}
337
338int PayloadSplitter::SplitByFrames(const Packet* packet,
339 int bytes_per_frame,
340 int timestamps_per_frame,
341 PacketList* new_packets) {
342 if (packet->payload_length % bytes_per_frame != 0) {
343 return kFrameSplitError;
344 }
345
346 int num_frames = packet->payload_length / bytes_per_frame;
347 if (num_frames == 1) {
348 // Special case. Do not split the payload.
349 return kNoSplit;
350 }
351
352 uint32_t timestamp = packet->header.timestamp;
353 uint8_t* payload_ptr = packet->payload;
354 int len = packet->payload_length;
355 while (len > 0) {
356 assert(len >= bytes_per_frame);
357 Packet* new_packet = new Packet;
358 new_packet->payload_length = bytes_per_frame;
359 new_packet->header = packet->header;
360 new_packet->header.timestamp = timestamp;
361 timestamp += timestamps_per_frame;
362 new_packet->primary = packet->primary;
363 new_packet->payload = new uint8_t[bytes_per_frame];
364 memcpy(new_packet->payload, payload_ptr, bytes_per_frame);
365 payload_ptr += bytes_per_frame;
366 new_packets->push_back(new_packet);
367 len -= bytes_per_frame;
368 }
369 return kOK;
370}
371
372} // namespace webrtc