blob: 4553b52c62dc5664cf16f93f000c8473e445b906 [file] [log] [blame]
turaj@webrtc.org7959e162013-09-12 18:30:26 +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/*
12 * This file generates databases with information about all supported audio
13 * codecs.
14 */
15
16// TODO(tlegrand): Change constant input pointers in all functions to constant
17// references, where appropriate.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/acm2/acm_codec_database.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000019
20#include <assert.h>
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000023
kwiberg65cb70d2017-03-03 06:16:28 -080024#if ((defined WEBRTC_CODEC_ISAC) && (defined WEBRTC_CODEC_ISACFX))
25#error iSAC and iSACFX codecs cannot be enabled at the same time
26#endif
27
turaj@webrtc.org7959e162013-09-12 18:30:26 +000028namespace webrtc {
29
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000030namespace acm2 {
31
kwibergec249d42015-09-24 04:32:03 -070032namespace {
33
kwibergec249d42015-09-24 04:32:03 -070034// Checks if the bitrate is valid for iSAC.
35bool IsISACRateValid(int rate) {
36 return (rate == -1) || ((rate <= 56000) && (rate >= 10000));
37}
38
39// Checks if the bitrate is valid for iLBC.
40bool IsILBCRateValid(int rate, int frame_size_samples) {
41 if (((frame_size_samples == 240) || (frame_size_samples == 480)) &&
42 (rate == 13300)) {
43 return true;
44 } else if (((frame_size_samples == 160) || (frame_size_samples == 320)) &&
45 (rate == 15200)) {
46 return true;
47 } else {
48 return false;
49 }
50}
51
52// Checks if the bitrate is valid for Opus.
53bool IsOpusRateValid(int rate) {
54 return (rate >= 6000) && (rate <= 510000);
55}
56
57} // namespace
58
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +000059// Not yet used payload-types.
60// 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68,
61// 67, 66, 65
turaj@webrtc.org7959e162013-09-12 18:30:26 +000062
63const CodecInst ACMCodecDB::database_[] = {
64#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwiberg65cb70d2017-03-03 06:16:28 -080065 {103, "ISAC", 16000, 480, 1, 32000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000066# if (defined(WEBRTC_CODEC_ISAC))
kwiberg65cb70d2017-03-03 06:16:28 -080067 {104, "ISAC", 32000, 960, 1, 56000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000068# endif
69#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +000070 // Mono
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +000071 {107, "L16", 8000, 80, 1, 128000},
72 {108, "L16", 16000, 160, 1, 256000},
73 {109, "L16", 32000, 320, 1, 512000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000074 // Stereo
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +000075 {111, "L16", 8000, 80, 2, 128000},
76 {112, "L16", 16000, 160, 2, 256000},
77 {113, "L16", 32000, 320, 2, 512000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000078 // G.711, PCM mu-law and A-law.
79 // Mono
80 {0, "PCMU", 8000, 160, 1, 64000},
81 {8, "PCMA", 8000, 160, 1, 64000},
82 // Stereo
83 {110, "PCMU", 8000, 160, 2, 64000},
84 {118, "PCMA", 8000, 160, 2, 64000},
85#ifdef WEBRTC_CODEC_ILBC
86 {102, "ILBC", 8000, 240, 1, 13300},
87#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +000088 // Mono
89 {9, "G722", 16000, 320, 1, 64000},
90 // Stereo
91 {119, "G722", 16000, 320, 2, 64000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000092#ifdef WEBRTC_CODEC_OPUS
93 // Opus internally supports 48, 24, 16, 12, 8 kHz.
94 // Mono and stereo.
95 {120, "opus", 48000, 960, 2, 64000},
96#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +000097 // Comfort noise for four different sampling frequencies.
98 {13, "CN", 8000, 240, 1, 0},
99 {98, "CN", 16000, 480, 1, 0},
100 {99, "CN", 32000, 960, 1, 0},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000101#ifdef ENABLE_48000_HZ
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000102 {100, "CN", 48000, 1440, 1, 0},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000103#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000104 {106, "telephone-event", 8000, 240, 1, 0},
solenberg2779bab2016-11-17 04:45:19 -0800105 {114, "telephone-event", 16000, 240, 1, 0},
106 {115, "telephone-event", 32000, 240, 1, 0},
107 {116, "telephone-event", 48000, 240, 1, 0},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000108#ifdef WEBRTC_CODEC_RED
109 {127, "red", 8000, 0, 1, 0},
110#endif
111 // To prevent compile errors due to trailing commas.
pkasting25702cb2016-01-08 13:50:27 -0800112 {-1, "Null", -1, -1, 0, -1}
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000113};
114
115// Create database with all codec settings at compile time.
116// Each entry needs the following parameters in the given order:
117// Number of allowed packet sizes, a vector with the allowed packet sizes,
118// Basic block samples, max number of channels that are supported.
119const ACMCodecDB::CodecSettings ACMCodecDB::codec_settings_[] = {
120#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwiberg65cb70d2017-03-03 06:16:28 -0800121 {2, {480, 960}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000122# if (defined(WEBRTC_CODEC_ISAC))
kwiberg65cb70d2017-03-03 06:16:28 -0800123 {1, {960}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000124# endif
125#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000126 // Mono
kwibergec249d42015-09-24 04:32:03 -0700127 {4, {80, 160, 240, 320}, 0, 2},
128 {4, {160, 320, 480, 640}, 0, 2},
129 {2, {320, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000130 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700131 {4, {80, 160, 240, 320}, 0, 2},
132 {4, {160, 320, 480, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000133 {2, {320, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000134 // G.711, PCM mu-law and A-law.
135 // Mono
kwibergec249d42015-09-24 04:32:03 -0700136 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
137 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000138 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700139 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
140 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000141#ifdef WEBRTC_CODEC_ILBC
kwibergec249d42015-09-24 04:32:03 -0700142 {4, {160, 240, 320, 480}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000143#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000144 // Mono
kwibergec249d42015-09-24 04:32:03 -0700145 {6, {160, 320, 480, 640, 800, 960}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000146 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700147 {6, {160, 320, 480, 640, 800, 960}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000148#ifdef WEBRTC_CODEC_OPUS
149 // Opus supports frames shorter than 10ms,
150 // but it doesn't help us to use them.
151 // Mono and stereo.
minyue2e03c662017-02-01 17:31:11 -0800152#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
153 {5, {480, 960, 1920, 2880, 5760}, 0, 2},
154#else
kwibergec249d42015-09-24 04:32:03 -0700155 {4, {480, 960, 1920, 2880}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000156#endif
minyue2e03c662017-02-01 17:31:11 -0800157#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000158 // Comfort noise for three different sampling frequencies.
kwibergec249d42015-09-24 04:32:03 -0700159 {1, {240}, 240, 1},
160 {1, {480}, 480, 1},
161 {1, {960}, 960, 1},
solenberg2779bab2016-11-17 04:45:19 -0800162// TODO(solenberg): What is this flag? It is never set in the build files.
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000163#ifdef ENABLE_48000_HZ
kwibergec249d42015-09-24 04:32:03 -0700164 {1, {1440}, 1440, 1},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000165#endif
kwibergec249d42015-09-24 04:32:03 -0700166 {1, {240}, 240, 1},
solenberg2779bab2016-11-17 04:45:19 -0800167 {1, {240}, 240, 1},
168 {1, {240}, 240, 1},
169 {1, {240}, 240, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000170#ifdef WEBRTC_CODEC_RED
kwibergec249d42015-09-24 04:32:03 -0700171 {1, {0}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000172#endif
173 // To prevent compile errors due to trailing commas.
pkasting25702cb2016-01-08 13:50:27 -0800174 {-1, {-1}, -1, 0}
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000175};
176
177// Create a database of all NetEQ decoders at compile time.
178const NetEqDecoder ACMCodecDB::neteq_decoders_[] = {
179#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwibergee1879c2015-10-29 06:20:28 -0700180 NetEqDecoder::kDecoderISAC,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000181# if (defined(WEBRTC_CODEC_ISAC))
kwibergee1879c2015-10-29 06:20:28 -0700182 NetEqDecoder::kDecoderISACswb,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000183# endif
184#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000185 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700186 NetEqDecoder::kDecoderPCM16B, NetEqDecoder::kDecoderPCM16Bwb,
187 NetEqDecoder::kDecoderPCM16Bswb32kHz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000188 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700189 NetEqDecoder::kDecoderPCM16B_2ch, NetEqDecoder::kDecoderPCM16Bwb_2ch,
190 NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000191 // G.711, PCM mu-las and A-law.
192 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700193 NetEqDecoder::kDecoderPCMu, NetEqDecoder::kDecoderPCMa,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000194 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700195 NetEqDecoder::kDecoderPCMu_2ch, NetEqDecoder::kDecoderPCMa_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000196#ifdef WEBRTC_CODEC_ILBC
kwibergee1879c2015-10-29 06:20:28 -0700197 NetEqDecoder::kDecoderILBC,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000198#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000199 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700200 NetEqDecoder::kDecoderG722,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000201 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700202 NetEqDecoder::kDecoderG722_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000203#ifdef WEBRTC_CODEC_OPUS
204 // Mono and stereo.
kwibergee1879c2015-10-29 06:20:28 -0700205 NetEqDecoder::kDecoderOpus,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000206#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000207 // Comfort noise for three different sampling frequencies.
kwibergee1879c2015-10-29 06:20:28 -0700208 NetEqDecoder::kDecoderCNGnb, NetEqDecoder::kDecoderCNGwb,
209 NetEqDecoder::kDecoderCNGswb32kHz,
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000210#ifdef ENABLE_48000_HZ
kwibergee1879c2015-10-29 06:20:28 -0700211 NetEqDecoder::kDecoderCNGswb48kHz,
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000212#endif
kwibergee1879c2015-10-29 06:20:28 -0700213 NetEqDecoder::kDecoderAVT,
solenberg2779bab2016-11-17 04:45:19 -0800214 NetEqDecoder::kDecoderAVT16kHz,
215 NetEqDecoder::kDecoderAVT32kHz,
216 NetEqDecoder::kDecoderAVT48kHz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000217#ifdef WEBRTC_CODEC_RED
kwibergee1879c2015-10-29 06:20:28 -0700218 NetEqDecoder::kDecoderRED,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000219#endif
220};
221
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000222// Enumerator for error codes when asking for codec database id.
223enum {
224 kInvalidCodec = -10,
225 kInvalidPayloadtype = -30,
226 kInvalidPacketSize = -40,
227 kInvalidRate = -50
228};
229
230// Gets the codec id number from the database. If there is some mismatch in
231// the codec settings, the function will return an error code.
232// NOTE! The first mismatch found will generate the return value.
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200233int ACMCodecDB::CodecNumber(const CodecInst& codec_inst) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000234 // Look for a matching codec in the database.
235 int codec_id = CodecId(codec_inst);
236
237 // Checks if we found a matching codec.
238 if (codec_id == -1) {
239 return kInvalidCodec;
240 }
241
242 // Checks the validity of payload type
kwiberg93a2feb2015-11-05 07:39:37 -0800243 if (!RentACodec::IsPayloadTypeValid(codec_inst.pltype)) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000244 return kInvalidPayloadtype;
245 }
246
247 // Comfort Noise is special case, packet-size & rate is not checked.
248 if (STR_CASE_CMP(database_[codec_id].plname, "CN") == 0) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000249 return codec_id;
250 }
251
252 // RED is special case, packet-size & rate is not checked.
253 if (STR_CASE_CMP(database_[codec_id].plname, "red") == 0) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000254 return codec_id;
255 }
256
257 // Checks the validity of packet size.
258 if (codec_settings_[codec_id].num_packet_sizes > 0) {
259 bool packet_size_ok = false;
260 int i;
261 int packet_size_samples;
262 for (i = 0; i < codec_settings_[codec_id].num_packet_sizes; i++) {
263 packet_size_samples =
264 codec_settings_[codec_id].packet_sizes_samples[i];
265 if (codec_inst.pacsize == packet_size_samples) {
266 packet_size_ok = true;
267 break;
268 }
269 }
270
271 if (!packet_size_ok) {
272 return kInvalidPacketSize;
273 }
274 }
275
276 if (codec_inst.pacsize < 1) {
277 return kInvalidPacketSize;
278 }
279
280 // Check the validity of rate. Codecs with multiple rates have their own
281 // function for this.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000282 if (STR_CASE_CMP("isac", codec_inst.plname) == 0) {
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200283 return IsISACRateValid(codec_inst.rate) ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000284 } else if (STR_CASE_CMP("ilbc", codec_inst.plname) == 0) {
285 return IsILBCRateValid(codec_inst.rate, codec_inst.pacsize)
286 ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000287 } else if (STR_CASE_CMP("opus", codec_inst.plname) == 0) {
288 return IsOpusRateValid(codec_inst.rate)
289 ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000290 }
291
kwiberg4b938e52015-11-03 12:38:27 -0800292 return database_[codec_id].rate == codec_inst.rate ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000293}
294
295// Looks for a matching payload name, frequency, and channels in the
296// codec list. Need to check all three since some codecs have several codec
297// entries with different frequencies and/or channels.
298// Does not check other codec settings, such as payload type and packet size.
299// Returns the id of the codec, or -1 if no match is found.
300int ACMCodecDB::CodecId(const CodecInst& codec_inst) {
301 return (CodecId(codec_inst.plname, codec_inst.plfreq,
302 codec_inst.channels));
303}
304
Peter Kasting69558702016-01-12 16:26:35 -0800305int ACMCodecDB::CodecId(const char* payload_name,
306 int frequency,
307 size_t channels) {
kwibergfce4a942015-10-27 11:40:24 -0700308 for (const CodecInst& ci : RentACodec::Database()) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000309 bool name_match = false;
310 bool frequency_match = false;
311 bool channels_match = false;
312
313 // Payload name, sampling frequency and number of channels need to match.
314 // NOTE! If |frequency| is -1, the frequency is not applicable, and is
315 // always treated as true, like for RED.
kwibergfce4a942015-10-27 11:40:24 -0700316 name_match = (STR_CASE_CMP(ci.plname, payload_name) == 0);
317 frequency_match = (frequency == ci.plfreq) || (frequency == -1);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000318 // The number of channels must match for all codecs but Opus.
319 if (STR_CASE_CMP(payload_name, "opus") != 0) {
kwibergfce4a942015-10-27 11:40:24 -0700320 channels_match = (channels == ci.channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000321 } else {
322 // For opus we just check that number of channels is valid.
323 channels_match = (channels == 1 || channels == 2);
324 }
325
326 if (name_match && frequency_match && channels_match) {
327 // We have found a matching codec in the list.
kwibergfce4a942015-10-27 11:40:24 -0700328 return &ci - RentACodec::Database().data();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000329 }
330 }
331
332 // We didn't find a matching codec.
333 return -1;
334}
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200335// Gets codec id number from database for the receiver.
336int ACMCodecDB::ReceiverCodecNumber(const CodecInst& codec_inst) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000337 // Look for a matching codec in the database.
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200338 return CodecId(codec_inst);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000339}
340
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000341} // namespace acm2
342
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000343} // namespace webrtc