blob: 62ed5dae789ecaea525ca4f5cfb1abd0f9df5f6c [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 }
166 PacketList new_packets;
167 switch (info->codec_type) {
168 case kDecoderPCMu:
169 case kDecoderPCMa: {
170 // 8 bytes per ms; 8 timestamps per ms.
171 SplitBySamples(packet, 8, 8, &new_packets);
172 break;
173 }
174 case kDecoderPCMu_2ch:
175 case kDecoderPCMa_2ch: {
176 // 2 * 8 bytes per ms; 8 timestamps per ms.
177 SplitBySamples(packet, 2 * 8, 8, &new_packets);
178 break;
179 }
180 case kDecoderG722: {
181 // 8 bytes per ms; 16 timestamps per ms.
182 SplitBySamples(packet, 8, 16, &new_packets);
183 break;
184 }
185 case kDecoderPCM16B: {
186 // 16 bytes per ms; 8 timestamps per ms.
187 SplitBySamples(packet, 16, 8, &new_packets);
188 break;
189 }
190 case kDecoderPCM16Bwb: {
191 // 32 bytes per ms; 16 timestamps per ms.
192 SplitBySamples(packet, 32, 16, &new_packets);
193 break;
194 }
195 case kDecoderPCM16Bswb32kHz: {
196 // 64 bytes per ms; 32 timestamps per ms.
197 SplitBySamples(packet, 64, 32, &new_packets);
198 break;
199 }
200 case kDecoderPCM16Bswb48kHz: {
201 // 96 bytes per ms; 48 timestamps per ms.
202 SplitBySamples(packet, 96, 48, &new_packets);
203 break;
204 }
205 case kDecoderPCM16B_2ch: {
206 // 2 * 16 bytes per ms; 8 timestamps per ms.
207 SplitBySamples(packet, 2 * 16, 8, &new_packets);
208 break;
209 }
210 case kDecoderPCM16Bwb_2ch: {
211 // 2 * 32 bytes per ms; 16 timestamps per ms.
212 SplitBySamples(packet, 2 * 32, 16, &new_packets);
213 break;
214 }
215 case kDecoderPCM16Bswb32kHz_2ch: {
216 // 2 * 64 bytes per ms; 32 timestamps per ms.
217 SplitBySamples(packet, 2 * 64, 32, &new_packets);
218 break;
219 }
220 case kDecoderPCM16Bswb48kHz_2ch: {
221 // 2 * 96 bytes per ms; 48 timestamps per ms.
222 SplitBySamples(packet, 2 * 96, 48, &new_packets);
223 break;
224 }
225 case kDecoderPCM16B_5ch: {
226 // 5 * 16 bytes per ms; 8 timestamps per ms.
227 SplitBySamples(packet, 5 * 16, 8, &new_packets);
228 break;
229 }
230 case kDecoderILBC: {
231 int bytes_per_frame;
232 int timestamps_per_frame;
233 if (packet->payload_length >= 950) {
234 return kTooLargePayload;
235 } else if (packet->payload_length % 38 == 0) {
236 // 20 ms frames.
237 bytes_per_frame = 38;
238 timestamps_per_frame = 160;
239 } else if (packet->payload_length % 50 == 0) {
240 // 30 ms frames.
241 bytes_per_frame = 50;
242 timestamps_per_frame = 240;
243 } else {
244 return kFrameSplitError;
245 }
246 int ret = SplitByFrames(packet, bytes_per_frame, timestamps_per_frame,
247 &new_packets);
248 if (ret < 0) {
249 return ret;
250 } else if (ret == kNoSplit) {
251 // Do not split at all. Simply advance to the next packet in the list.
252 ++it;
253 // We do not have any new packets to insert, and should not delete the
254 // old one. Skip the code after the switch case, and jump straight to
255 // the next packet in the while loop.
256 continue;
257 }
258 break;
259 }
260 default: {
261 // Do not split at all. Simply advance to the next packet in the list.
262 ++it;
263 // We do not have any new packets to insert, and should not delete the
264 // old one. Skip the code after the switch case, and jump straight to
265 // the next packet in the while loop.
266 continue;
267 }
268 }
269 // Insert new packets into original list, before the element pointed to by
270 // iterator |it|.
271 packet_list->splice(it, new_packets, new_packets.begin(),
272 new_packets.end());
273 // Delete old packet payload.
274 delete [] (*it)->payload;
275 delete (*it);
276 // Remove |it| from the packet list. This operation effectively moves the
277 // iterator |it| to the next packet in the list. Thus, we do not have to
278 // increment it manually.
279 it = packet_list->erase(it);
280 }
281 return 0;
282}
283
284void PayloadSplitter::SplitBySamples(const Packet* packet,
285 int bytes_per_ms,
286 int timestamps_per_ms,
287 PacketList* new_packets) {
288 assert(packet);
289 assert(new_packets);
290
291 int split_size_bytes = packet->payload_length;
292
293 // Find a "chunk size" >= 20 ms and < 40 ms.
294 int min_chunk_size = bytes_per_ms * 20;
295 // Reduce the split size by half as long as |split_size_bytes| is at least
296 // twice the minimum chunk size (so that the resulting size is at least as
297 // large as the minimum chunk size).
298 while (split_size_bytes >= 2 * min_chunk_size) {
299 split_size_bytes >>= 1;
300 }
301 int timestamps_per_chunk =
302 split_size_bytes * timestamps_per_ms / bytes_per_ms;
303 uint32_t timestamp = packet->header.timestamp;
304
305 uint8_t* payload_ptr = packet->payload;
306 int len = packet->payload_length;
307 while (len >= (2 * split_size_bytes)) {
308 Packet* new_packet = new Packet;
309 new_packet->payload_length = split_size_bytes;
310 new_packet->header = packet->header;
311 new_packet->header.timestamp = timestamp;
312 timestamp += timestamps_per_chunk;
313 new_packet->primary = packet->primary;
314 new_packet->payload = new uint8_t[split_size_bytes];
315 memcpy(new_packet->payload, payload_ptr, split_size_bytes);
316 payload_ptr += split_size_bytes;
317 new_packets->push_back(new_packet);
318 len -= split_size_bytes;
319 }
320
321 if (len > 0) {
322 Packet* new_packet = new Packet;
323 new_packet->payload_length = len;
324 new_packet->header = packet->header;
325 new_packet->header.timestamp = timestamp;
326 new_packet->primary = packet->primary;
327 new_packet->payload = new uint8_t[len];
328 memcpy(new_packet->payload, payload_ptr, len);
329 payload_ptr += len;
330 new_packets->push_back(new_packet);
331 }
332}
333
334int PayloadSplitter::SplitByFrames(const Packet* packet,
335 int bytes_per_frame,
336 int timestamps_per_frame,
337 PacketList* new_packets) {
338 if (packet->payload_length % bytes_per_frame != 0) {
339 return kFrameSplitError;
340 }
341
342 int num_frames = packet->payload_length / bytes_per_frame;
343 if (num_frames == 1) {
344 // Special case. Do not split the payload.
345 return kNoSplit;
346 }
347
348 uint32_t timestamp = packet->header.timestamp;
349 uint8_t* payload_ptr = packet->payload;
350 int len = packet->payload_length;
351 while (len > 0) {
352 assert(len >= bytes_per_frame);
353 Packet* new_packet = new Packet;
354 new_packet->payload_length = bytes_per_frame;
355 new_packet->header = packet->header;
356 new_packet->header.timestamp = timestamp;
357 timestamp += timestamps_per_frame;
358 new_packet->primary = packet->primary;
359 new_packet->payload = new uint8_t[bytes_per_frame];
360 memcpy(new_packet->payload, payload_ptr, bytes_per_frame);
361 payload_ptr += bytes_per_frame;
362 new_packets->push_back(new_packet);
363 len -= bytes_per_frame;
364 }
365 return kOK;
366}
367
368} // namespace webrtc