Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(536)

Side by Side Diff: webrtc/modules/audio_coding/neteq/payload_splitter.cc

Issue 2342443005: Moved Opus-specific payload splitting into AudioDecoderOpus. (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_coding/neteq/payload_splitter.h" 11 #include "webrtc/modules/audio_coding/neteq/payload_splitter.h"
12 12
13 #include <assert.h> 13 #include <assert.h>
14 14
15 #include "webrtc/base/checks.h" 15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/logging.h" 16 #include "webrtc/base/logging.h"
17 #include "webrtc/modules/audio_coding/neteq/decoder_database.h" 17 #include "webrtc/modules/audio_coding/neteq/decoder_database.h"
18 18
19 namespace webrtc { 19 namespace webrtc {
20 20
21 // The method loops through a list of packets {A, B, C, ...}. Each packet is 21 // The method loops through a list of packets {A, B, C, ...}. Each packet is
22 // split into its corresponding RED payloads, {A1, A2, ...}, which is 22 // split into its corresponding RED payloads, {A1, A2, ...}, which is
23 // temporarily held in the list |new_packets|. 23 // temporarily held in the list |new_packets|.
24 // When the first packet in |packet_list| has been processed, the orignal packet 24 // When the first packet in |packet_list| has been processed, the orignal packet
25 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes: 25 // is replaced by the new ones in |new_packets|, so that |packet_list| becomes:
26 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all 26 // {A1, A2, ..., B, C, ...}. The method then continues with B, and C, until all
27 // the original packets have been replaced by their split payloads. 27 // the original packets have been replaced by their split payloads.
28 int PayloadSplitter::SplitRed(PacketList* packet_list) { 28 int PayloadSplitter::SplitRed(PacketList* packet_list) {
29 // This could be as high as 256 (to match the number of priority levels) but
30 // very high values here probably indicate some sort of problem. Having a max
31 // value here makes the implementation easier, since we don't need to deal
32 // with the (very unexpected) case of more blocks than there are priority
33 // levels.
34 const size_t kMaxRedBlocks = 32;
29 int ret = kOK; 35 int ret = kOK;
30 PacketList::iterator it = packet_list->begin(); 36 PacketList::iterator it = packet_list->begin();
31 while (it != packet_list->end()) { 37 while (it != packet_list->end()) {
32 const Packet* red_packet = (*it); 38 const Packet* red_packet = (*it);
33 assert(!red_packet->payload.empty()); 39 assert(!red_packet->payload.empty());
34 const uint8_t* payload_ptr = red_packet->payload.data(); 40 const uint8_t* payload_ptr = red_packet->payload.data();
35 41
36 // Read RED headers (according to RFC 2198): 42 // Read RED headers (according to RFC 2198):
37 // 43 //
38 // 0 1 2 3 44 // 0 1 2 3
39 // 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 45 // 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
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 46 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 // |F| block PT | timestamp offset | block length | 47 // |F| block PT | timestamp offset | block length |
42 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 48 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
43 // Last RED header: 49 // Last RED header:
44 // 0 1 2 3 4 5 6 7 50 // 0 1 2 3 4 5 6 7
45 // +-+-+-+-+-+-+-+-+ 51 // +-+-+-+-+-+-+-+-+
46 // |0| Block PT | 52 // |0| Block PT |
47 // +-+-+-+-+-+-+-+-+ 53 // +-+-+-+-+-+-+-+-+
48 54
49 struct RedHeader { 55 struct RedHeader {
50 uint8_t payload_type; 56 uint8_t payload_type;
51 uint32_t timestamp; 57 uint32_t timestamp;
52 size_t payload_length; 58 size_t payload_length;
53 bool primary;
54 }; 59 };
55 60
56 std::vector<RedHeader> new_headers; 61 std::vector<RedHeader> new_headers;
57 bool last_block = false; 62 bool last_block = false;
58 size_t sum_length = 0; 63 size_t sum_length = 0;
59 while (!last_block) { 64 while (!last_block) {
60 RedHeader new_header; 65 RedHeader new_header;
61 // Check the F bit. If F == 0, this was the last block. 66 // Check the F bit. If F == 0, this was the last block.
62 last_block = ((*payload_ptr & 0x80) == 0); 67 last_block = ((*payload_ptr & 0x80) == 0);
63 // Bits 1 through 7 are payload type. 68 // Bits 1 through 7 are payload type.
64 new_header.payload_type = payload_ptr[0] & 0x7F; 69 new_header.payload_type = payload_ptr[0] & 0x7F;
65 if (last_block) { 70 if (last_block) {
66 // No more header data to read. 71 // No more header data to read.
67 ++sum_length; // Account for RED header size of 1 byte. 72 ++sum_length; // Account for RED header size of 1 byte.
68 new_header.timestamp = red_packet->header.timestamp; 73 new_header.timestamp = red_packet->header.timestamp;
69 new_header.payload_length = red_packet->payload.size() - sum_length; 74 new_header.payload_length = red_packet->payload.size() - sum_length;
70 new_header.primary = true; // Last block is always primary.
71 payload_ptr += 1; // Advance to first payload byte. 75 payload_ptr += 1; // Advance to first payload byte.
72 } else { 76 } else {
73 // Bits 8 through 21 are timestamp offset. 77 // Bits 8 through 21 are timestamp offset.
74 int timestamp_offset = (payload_ptr[1] << 6) + 78 int timestamp_offset = (payload_ptr[1] << 6) +
75 ((payload_ptr[2] & 0xFC) >> 2); 79 ((payload_ptr[2] & 0xFC) >> 2);
76 new_header.timestamp = red_packet->header.timestamp - timestamp_offset; 80 new_header.timestamp = red_packet->header.timestamp - timestamp_offset;
77 // Bits 22 through 31 are payload length. 81 // Bits 22 through 31 are payload length.
78 new_header.payload_length = 82 new_header.payload_length =
79 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3]; 83 ((payload_ptr[2] & 0x03) << 8) + payload_ptr[3];
80 new_header.primary = false;
81 payload_ptr += 4; // Advance to next RED header. 84 payload_ptr += 4; // Advance to next RED header.
82 } 85 }
83 sum_length += new_header.payload_length; 86 sum_length += new_header.payload_length;
84 sum_length += 4; // Account for RED header size of 4 bytes. 87 sum_length += 4; // Account for RED header size of 4 bytes.
85 // Store in new list of packets. 88 // Store in new list of packets.
86 new_headers.push_back(new_header); 89 new_headers.push_back(new_header);
87 } 90 }
88 91
89 // Populate the new packets with payload data. 92 if (new_headers.size() <= kMaxRedBlocks) {
90 // |payload_ptr| now points at the first payload byte. 93 // Populate the new packets with payload data.
91 PacketList new_packets; // An empty list to store the split packets in. 94 // |payload_ptr| now points at the first payload byte.
92 for (const auto& new_header : new_headers) { 95 PacketList new_packets; // An empty list to store the split packets in.
93 size_t payload_length = new_header.payload_length; 96 for (size_t i = 0; i != new_headers.size(); ++i) {
94 if (payload_ptr + payload_length > 97 const auto& new_header = new_headers[i];
95 red_packet->payload.data() + red_packet->payload.size()) { 98 size_t payload_length = new_header.payload_length;
96 // The block lengths in the RED headers do not match the overall packet 99 if (payload_ptr + payload_length >
97 // length. Something is corrupt. Discard this and the remaining 100 red_packet->payload.data() + red_packet->payload.size()) {
98 // payloads from this packet. 101 // The block lengths in the RED headers do not match the overall
99 LOG(LS_WARNING) << "SplitRed length mismatch"; 102 // packet length. Something is corrupt. Discard this and the remaining
100 ret = kRedLengthMismatch; 103 // payloads from this packet.
101 break; 104 LOG(LS_WARNING) << "SplitRed length mismatch";
105 ret = kRedLengthMismatch;
106 break;
107 }
108
109 Packet* new_packet = new Packet;
110 new_packet->header = red_packet->header;
111 new_packet->header.timestamp = new_header.timestamp;
112 new_packet->header.payloadType = new_header.payload_type;
113 new_packet->priority.red_level =
114 static_cast<uint8_t>((new_headers.size() - 1) - i);
115 new_packet->payload.SetData(payload_ptr, payload_length);
116 new_packets.push_front(new_packet);
117 payload_ptr += payload_length;
102 } 118 }
103 Packet* new_packet = new Packet; 119 // Insert new packets into original list, before the element pointed to by
104 new_packet->header = red_packet->header; 120 // iterator |it|.
105 new_packet->header.timestamp = new_header.timestamp; 121 packet_list->splice(it, new_packets, new_packets.begin(),
106 new_packet->header.payloadType = new_header.payload_type; 122 new_packets.end());
107 new_packet->primary = new_header.primary; 123 } else {
108 new_packet->payload.SetData(payload_ptr, payload_length); 124 LOG(LS_WARNING) << "SplitRed too many blocks: " << new_headers.size();
109 new_packets.push_front(new_packet); 125 ret = kRedLengthMismatch;
110 payload_ptr += payload_length;
111 } 126 }
112 // Insert new packets into original list, before the element pointed to by
113 // iterator |it|.
114 packet_list->splice(it, new_packets, new_packets.begin(),
115 new_packets.end());
116 // Delete old packet payload. 127 // Delete old packet payload.
117 delete (*it); 128 delete (*it);
118 // Remove |it| from the packet list. This operation effectively moves the 129 // Remove |it| from the packet list. This operation effectively moves the
119 // iterator |it| to the next packet in the list. Thus, we do not have to 130 // iterator |it| to the next packet in the list. Thus, we do not have to
120 // increment it manually. 131 // increment it manually.
121 it = packet_list->erase(it); 132 it = packet_list->erase(it);
122 } 133 }
123 return ret; 134 return ret;
124 } 135 }
125 136
126 int PayloadSplitter::SplitFec(PacketList* packet_list,
127 DecoderDatabase* decoder_database) {
128 PacketList::iterator it = packet_list->begin();
129 // Iterate through all packets in |packet_list|.
130 while (it != packet_list->end()) {
131 Packet* packet = (*it); // Just to make the notation more intuitive.
132 // Get codec type for this payload.
133 uint8_t payload_type = packet->header.payloadType;
134 const DecoderDatabase::DecoderInfo* info =
135 decoder_database->GetDecoderInfo(payload_type);
136 if (!info) {
137 LOG(LS_WARNING) << "SplitFec unknown payload type";
138 return kUnknownPayloadType;
139 }
140
141 // Not an FEC packet.
142 AudioDecoder* decoder = decoder_database->GetDecoder(payload_type);
143 // decoder should not return NULL, except for comfort noise payloads which
144 // are handled separately.
145 assert(decoder != NULL || decoder_database->IsComfortNoise(payload_type));
146 if (!decoder ||
147 !decoder->PacketHasFec(packet->payload.data(),
148 packet->payload.size())) {
149 ++it;
150 continue;
151 }
152
153 switch (info->codec_type) {
154 case NetEqDecoder::kDecoderOpus:
155 case NetEqDecoder::kDecoderOpus_2ch: {
156 // The main payload of this packet should be decoded as a primary
157 // payload, even if it comes as a secondary payload in a RED packet.
158 packet->primary = true;
159
160 Packet* new_packet = new Packet;
161 new_packet->header = packet->header;
162 int duration = decoder->PacketDurationRedundant(packet->payload.data(),
163 packet->payload.size());
164 new_packet->header.timestamp -= duration;
165 new_packet->payload.SetData(packet->payload);
166 new_packet->primary = false;
167 // Waiting time should not be set here.
168 RTC_DCHECK(!packet->waiting_time);
169
170 packet_list->insert(it, new_packet);
171 break;
172 }
173 default: {
174 LOG(LS_WARNING) << "SplitFec wrong payload type";
175 return kFecSplitError;
176 }
177 }
178
179 ++it;
180 }
181 return kOK;
182 }
183
184 int PayloadSplitter::CheckRedPayloads(PacketList* packet_list, 137 int PayloadSplitter::CheckRedPayloads(PacketList* packet_list,
185 const DecoderDatabase& decoder_database) { 138 const DecoderDatabase& decoder_database) {
186 PacketList::iterator it = packet_list->begin(); 139 PacketList::iterator it = packet_list->begin();
187 int main_payload_type = -1; 140 int main_payload_type = -1;
188 int num_deleted_packets = 0; 141 int num_deleted_packets = 0;
189 while (it != packet_list->end()) { 142 while (it != packet_list->end()) {
190 uint8_t this_payload_type = (*it)->header.payloadType; 143 uint8_t this_payload_type = (*it)->header.payloadType;
191 if (!decoder_database.IsDtmf(this_payload_type) && 144 if (!decoder_database.IsDtmf(this_payload_type) &&
192 !decoder_database.IsComfortNoise(this_payload_type)) { 145 !decoder_database.IsComfortNoise(this_payload_type)) {
193 if (main_payload_type == -1) { 146 if (main_payload_type == -1) {
(...skipping 12 matching lines...) Expand all
206 continue; 159 continue;
207 } 160 }
208 } 161 }
209 } 162 }
210 ++it; 163 ++it;
211 } 164 }
212 return num_deleted_packets; 165 return num_deleted_packets;
213 } 166 }
214 167
215 } // namespace webrtc 168 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698