blob: a322c952101e5f5fd9df4c94408bd1c55f9c14d3 [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)) &&
Yves Gerey665174f2018-06-19 15:03:05 +020045 (rate == 15200)) {
kwibergec249d42015-09-24 04:32:03 -070046 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))
Yves Gerey665174f2018-06-19 15:03:05 +020065 {103, "ISAC", 16000, 480, 1, 32000},
66#if (defined(WEBRTC_CODEC_ISAC))
67 {104, "ISAC", 32000, 960, 1, 56000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000068#endif
Yves Gerey665174f2018-06-19 15:03:05 +020069#endif
70 // Mono
71 {107, "L16", 8000, 80, 1, 128000},
72 {108, "L16", 16000, 160, 1, 256000},
73 {109, "L16", 32000, 320, 1, 512000},
74 // Stereo
75 {111, "L16", 8000, 80, 2, 128000},
76 {112, "L16", 16000, 160, 2, 256000},
77 {113, "L16", 32000, 320, 2, 512000},
78 // 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},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000085#ifdef WEBRTC_CODEC_ILBC
Yves Gerey665174f2018-06-19 15:03:05 +020086 {102, "ILBC", 8000, 240, 1, 13300},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000087#endif
Yves Gerey665174f2018-06-19 15:03:05 +020088 // 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
Yves Gerey665174f2018-06-19 15:03:05 +020093 // Opus internally supports 48, 24, 16, 12, 8 kHz.
94 // Mono and stereo.
95 {120, "opus", 48000, 960, 2, 64000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000096#endif
Yves Gerey665174f2018-06-19 15:03:05 +020097 // 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
Yves Gerey665174f2018-06-19 15:03:05 +0200102 {100, "CN", 48000, 1440, 1, 0},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000103#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200104 {106, "telephone-event", 8000, 240, 1, 0},
105 {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
Yves Gerey665174f2018-06-19 15:03:05 +0200109 {127, "red", 8000, 0, 1, 0},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000110#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200111 // To prevent compile errors due to trailing commas.
112 {-1, "Null", -1, -1, 0, -1}};
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000113
114// Create database with all codec settings at compile time.
115// Each entry needs the following parameters in the given order:
116// Number of allowed packet sizes, a vector with the allowed packet sizes,
117// Basic block samples, max number of channels that are supported.
118const ACMCodecDB::CodecSettings ACMCodecDB::codec_settings_[] = {
119#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwiberg65cb70d2017-03-03 06:16:28 -0800120 {2, {480, 960}, 0, 1},
Yves Gerey665174f2018-06-19 15:03:05 +0200121#if (defined(WEBRTC_CODEC_ISAC))
kwiberg65cb70d2017-03-03 06:16:28 -0800122 {1, {960}, 0, 1},
Yves Gerey665174f2018-06-19 15:03:05 +0200123#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000124#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000125 // Mono
kwibergec249d42015-09-24 04:32:03 -0700126 {4, {80, 160, 240, 320}, 0, 2},
127 {4, {160, 320, 480, 640}, 0, 2},
128 {2, {320, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000129 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700130 {4, {80, 160, 240, 320}, 0, 2},
131 {4, {160, 320, 480, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132 {2, {320, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000133 // G.711, PCM mu-law and A-law.
134 // Mono
kwibergec249d42015-09-24 04:32:03 -0700135 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
136 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000137 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700138 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
139 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000140#ifdef WEBRTC_CODEC_ILBC
kwibergec249d42015-09-24 04:32:03 -0700141 {4, {160, 240, 320, 480}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000142#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000143 // Mono
kwibergec249d42015-09-24 04:32:03 -0700144 {6, {160, 320, 480, 640, 800, 960}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000145 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700146 {6, {160, 320, 480, 640, 800, 960}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000147#ifdef WEBRTC_CODEC_OPUS
Yves Gerey665174f2018-06-19 15:03:05 +0200148// Opus supports frames shorter than 10ms,
149// but it doesn't help us to use them.
150// Mono and stereo.
minyue2e03c662017-02-01 17:31:11 -0800151#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
152 {5, {480, 960, 1920, 2880, 5760}, 0, 2},
153#else
kwibergec249d42015-09-24 04:32:03 -0700154 {4, {480, 960, 1920, 2880}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000155#endif
minyue2e03c662017-02-01 17:31:11 -0800156#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000157 // Comfort noise for three different sampling frequencies.
kwibergec249d42015-09-24 04:32:03 -0700158 {1, {240}, 240, 1},
159 {1, {480}, 480, 1},
160 {1, {960}, 960, 1},
solenberg2779bab2016-11-17 04:45:19 -0800161// TODO(solenberg): What is this flag? It is never set in the build files.
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000162#ifdef ENABLE_48000_HZ
kwibergec249d42015-09-24 04:32:03 -0700163 {1, {1440}, 1440, 1},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000164#endif
kwibergec249d42015-09-24 04:32:03 -0700165 {1, {240}, 240, 1},
solenberg2779bab2016-11-17 04:45:19 -0800166 {1, {240}, 240, 1},
167 {1, {240}, 240, 1},
168 {1, {240}, 240, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000169#ifdef WEBRTC_CODEC_RED
kwibergec249d42015-09-24 04:32:03 -0700170 {1, {0}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000171#endif
172 // To prevent compile errors due to trailing commas.
Yves Gerey665174f2018-06-19 15:03:05 +0200173 {-1, {-1}, -1, 0}};
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000174
175// Create a database of all NetEQ decoders at compile time.
176const NetEqDecoder ACMCodecDB::neteq_decoders_[] = {
177#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwibergee1879c2015-10-29 06:20:28 -0700178 NetEqDecoder::kDecoderISAC,
Yves Gerey665174f2018-06-19 15:03:05 +0200179#if (defined(WEBRTC_CODEC_ISAC))
kwibergee1879c2015-10-29 06:20:28 -0700180 NetEqDecoder::kDecoderISACswb,
Yves Gerey665174f2018-06-19 15:03:05 +0200181#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000182#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000183 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700184 NetEqDecoder::kDecoderPCM16B, NetEqDecoder::kDecoderPCM16Bwb,
185 NetEqDecoder::kDecoderPCM16Bswb32kHz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000186 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700187 NetEqDecoder::kDecoderPCM16B_2ch, NetEqDecoder::kDecoderPCM16Bwb_2ch,
188 NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000189 // G.711, PCM mu-las and A-law.
190 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700191 NetEqDecoder::kDecoderPCMu, NetEqDecoder::kDecoderPCMa,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000192 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700193 NetEqDecoder::kDecoderPCMu_2ch, NetEqDecoder::kDecoderPCMa_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000194#ifdef WEBRTC_CODEC_ILBC
kwibergee1879c2015-10-29 06:20:28 -0700195 NetEqDecoder::kDecoderILBC,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000196#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000197 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700198 NetEqDecoder::kDecoderG722,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000199 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700200 NetEqDecoder::kDecoderG722_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000201#ifdef WEBRTC_CODEC_OPUS
202 // Mono and stereo.
kwibergee1879c2015-10-29 06:20:28 -0700203 NetEqDecoder::kDecoderOpus,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000204#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000205 // Comfort noise for three different sampling frequencies.
kwibergee1879c2015-10-29 06:20:28 -0700206 NetEqDecoder::kDecoderCNGnb, NetEqDecoder::kDecoderCNGwb,
207 NetEqDecoder::kDecoderCNGswb32kHz,
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000208#ifdef ENABLE_48000_HZ
kwibergee1879c2015-10-29 06:20:28 -0700209 NetEqDecoder::kDecoderCNGswb48kHz,
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000210#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200211 NetEqDecoder::kDecoderAVT, NetEqDecoder::kDecoderAVT16kHz,
212 NetEqDecoder::kDecoderAVT32kHz, NetEqDecoder::kDecoderAVT48kHz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000213#ifdef WEBRTC_CODEC_RED
kwibergee1879c2015-10-29 06:20:28 -0700214 NetEqDecoder::kDecoderRED,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000215#endif
216};
217
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000218// Enumerator for error codes when asking for codec database id.
219enum {
220 kInvalidCodec = -10,
221 kInvalidPayloadtype = -30,
222 kInvalidPacketSize = -40,
223 kInvalidRate = -50
224};
225
226// Gets the codec id number from the database. If there is some mismatch in
227// the codec settings, the function will return an error code.
228// NOTE! The first mismatch found will generate the return value.
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200229int ACMCodecDB::CodecNumber(const CodecInst& codec_inst) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000230 // Look for a matching codec in the database.
231 int codec_id = CodecId(codec_inst);
232
233 // Checks if we found a matching codec.
234 if (codec_id == -1) {
235 return kInvalidCodec;
236 }
237
238 // Checks the validity of payload type
kwiberg93a2feb2015-11-05 07:39:37 -0800239 if (!RentACodec::IsPayloadTypeValid(codec_inst.pltype)) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000240 return kInvalidPayloadtype;
241 }
242
243 // Comfort Noise is special case, packet-size & rate is not checked.
244 if (STR_CASE_CMP(database_[codec_id].plname, "CN") == 0) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000245 return codec_id;
246 }
247
248 // RED is special case, packet-size & rate is not checked.
249 if (STR_CASE_CMP(database_[codec_id].plname, "red") == 0) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000250 return codec_id;
251 }
252
253 // Checks the validity of packet size.
254 if (codec_settings_[codec_id].num_packet_sizes > 0) {
255 bool packet_size_ok = false;
256 int i;
257 int packet_size_samples;
258 for (i = 0; i < codec_settings_[codec_id].num_packet_sizes; i++) {
Yves Gerey665174f2018-06-19 15:03:05 +0200259 packet_size_samples = codec_settings_[codec_id].packet_sizes_samples[i];
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000260 if (codec_inst.pacsize == packet_size_samples) {
261 packet_size_ok = true;
262 break;
263 }
264 }
265
266 if (!packet_size_ok) {
267 return kInvalidPacketSize;
268 }
269 }
270
271 if (codec_inst.pacsize < 1) {
272 return kInvalidPacketSize;
273 }
274
275 // Check the validity of rate. Codecs with multiple rates have their own
276 // function for this.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000277 if (STR_CASE_CMP("isac", codec_inst.plname) == 0) {
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200278 return IsISACRateValid(codec_inst.rate) ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000279 } else if (STR_CASE_CMP("ilbc", codec_inst.plname) == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200280 return IsILBCRateValid(codec_inst.rate, codec_inst.pacsize) ? codec_id
281 : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000282 } else if (STR_CASE_CMP("opus", codec_inst.plname) == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200283 return IsOpusRateValid(codec_inst.rate) ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000284 }
285
kwiberg4b938e52015-11-03 12:38:27 -0800286 return database_[codec_id].rate == codec_inst.rate ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000287}
288
289// Looks for a matching payload name, frequency, and channels in the
290// codec list. Need to check all three since some codecs have several codec
291// entries with different frequencies and/or channels.
292// Does not check other codec settings, such as payload type and packet size.
293// Returns the id of the codec, or -1 if no match is found.
294int ACMCodecDB::CodecId(const CodecInst& codec_inst) {
Yves Gerey665174f2018-06-19 15:03:05 +0200295 return (CodecId(codec_inst.plname, codec_inst.plfreq, codec_inst.channels));
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000296}
297
Peter Kasting69558702016-01-12 16:26:35 -0800298int ACMCodecDB::CodecId(const char* payload_name,
299 int frequency,
300 size_t channels) {
kwibergfce4a942015-10-27 11:40:24 -0700301 for (const CodecInst& ci : RentACodec::Database()) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000302 bool name_match = false;
303 bool frequency_match = false;
304 bool channels_match = false;
305
306 // Payload name, sampling frequency and number of channels need to match.
307 // NOTE! If |frequency| is -1, the frequency is not applicable, and is
308 // always treated as true, like for RED.
kwibergfce4a942015-10-27 11:40:24 -0700309 name_match = (STR_CASE_CMP(ci.plname, payload_name) == 0);
310 frequency_match = (frequency == ci.plfreq) || (frequency == -1);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000311 // The number of channels must match for all codecs but Opus.
312 if (STR_CASE_CMP(payload_name, "opus") != 0) {
kwibergfce4a942015-10-27 11:40:24 -0700313 channels_match = (channels == ci.channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000314 } else {
315 // For opus we just check that number of channels is valid.
316 channels_match = (channels == 1 || channels == 2);
317 }
318
319 if (name_match && frequency_match && channels_match) {
320 // We have found a matching codec in the list.
kwibergfce4a942015-10-27 11:40:24 -0700321 return &ci - RentACodec::Database().data();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000322 }
323 }
324
325 // We didn't find a matching codec.
326 return -1;
327}
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200328// Gets codec id number from database for the receiver.
329int ACMCodecDB::ReceiverCodecNumber(const CodecInst& codec_inst) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000330 // Look for a matching codec in the database.
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200331 return CodecId(codec_inst);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000332}
333
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000334} // namespace acm2
335
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000336} // namespace webrtc