blob: e109cc7e028f610a39a8fdc72a3a98be7fb551be [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
Niels Möller2edab4c2018-10-22 09:48:08 +020020#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000022
kwiberg65cb70d2017-03-03 06:16:28 -080023#if ((defined WEBRTC_CODEC_ISAC) && (defined WEBRTC_CODEC_ISACFX))
24#error iSAC and iSACFX codecs cannot be enabled at the same time
25#endif
26
turaj@webrtc.org7959e162013-09-12 18:30:26 +000027namespace webrtc {
28
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000029namespace acm2 {
30
kwibergec249d42015-09-24 04:32:03 -070031namespace {
32
kwibergec249d42015-09-24 04:32:03 -070033// Checks if the bitrate is valid for iSAC.
34bool IsISACRateValid(int rate) {
35 return (rate == -1) || ((rate <= 56000) && (rate >= 10000));
36}
37
38// Checks if the bitrate is valid for iLBC.
39bool IsILBCRateValid(int rate, int frame_size_samples) {
40 if (((frame_size_samples == 240) || (frame_size_samples == 480)) &&
41 (rate == 13300)) {
42 return true;
43 } else if (((frame_size_samples == 160) || (frame_size_samples == 320)) &&
Yves Gerey665174f2018-06-19 15:03:05 +020044 (rate == 15200)) {
kwibergec249d42015-09-24 04:32:03 -070045 return true;
46 } else {
47 return false;
48 }
49}
50
51// Checks if the bitrate is valid for Opus.
52bool IsOpusRateValid(int rate) {
53 return (rate >= 6000) && (rate <= 510000);
54}
55
56} // namespace
57
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +000058// Not yet used payload-types.
59// 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68,
60// 67, 66, 65
turaj@webrtc.org7959e162013-09-12 18:30:26 +000061
62const CodecInst ACMCodecDB::database_[] = {
63#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
Yves Gerey665174f2018-06-19 15:03:05 +020064 {103, "ISAC", 16000, 480, 1, 32000},
65#if (defined(WEBRTC_CODEC_ISAC))
66 {104, "ISAC", 32000, 960, 1, 56000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000067#endif
Yves Gerey665174f2018-06-19 15:03:05 +020068#endif
69 // Mono
70 {107, "L16", 8000, 80, 1, 128000},
71 {108, "L16", 16000, 160, 1, 256000},
72 {109, "L16", 32000, 320, 1, 512000},
73 // Stereo
74 {111, "L16", 8000, 80, 2, 128000},
75 {112, "L16", 16000, 160, 2, 256000},
76 {113, "L16", 32000, 320, 2, 512000},
77 // G.711, PCM mu-law and A-law.
78 // Mono
79 {0, "PCMU", 8000, 160, 1, 64000},
80 {8, "PCMA", 8000, 160, 1, 64000},
81 // Stereo
82 {110, "PCMU", 8000, 160, 2, 64000},
83 {118, "PCMA", 8000, 160, 2, 64000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000084#ifdef WEBRTC_CODEC_ILBC
Yves Gerey665174f2018-06-19 15:03:05 +020085 {102, "ILBC", 8000, 240, 1, 13300},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000086#endif
Yves Gerey665174f2018-06-19 15:03:05 +020087 // Mono
88 {9, "G722", 16000, 320, 1, 64000},
89 // Stereo
90 {119, "G722", 16000, 320, 2, 64000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000091#ifdef WEBRTC_CODEC_OPUS
Yves Gerey665174f2018-06-19 15:03:05 +020092 // Opus internally supports 48, 24, 16, 12, 8 kHz.
93 // Mono and stereo.
94 {120, "opus", 48000, 960, 2, 64000},
turaj@webrtc.org7959e162013-09-12 18:30:26 +000095#endif
Yves Gerey665174f2018-06-19 15:03:05 +020096 // Comfort noise for four different sampling frequencies.
97 {13, "CN", 8000, 240, 1, 0},
98 {98, "CN", 16000, 480, 1, 0},
99 {99, "CN", 32000, 960, 1, 0},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000100#ifdef ENABLE_48000_HZ
Yves Gerey665174f2018-06-19 15:03:05 +0200101 {100, "CN", 48000, 1440, 1, 0},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000102#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200103 {106, "telephone-event", 8000, 240, 1, 0},
104 {114, "telephone-event", 16000, 240, 1, 0},
105 {115, "telephone-event", 32000, 240, 1, 0},
106 {116, "telephone-event", 48000, 240, 1, 0},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000107#ifdef WEBRTC_CODEC_RED
Yves Gerey665174f2018-06-19 15:03:05 +0200108 {127, "red", 8000, 0, 1, 0},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000109#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200110 // To prevent compile errors due to trailing commas.
111 {-1, "Null", -1, -1, 0, -1}};
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000112
113// Create database with all codec settings at compile time.
114// Each entry needs the following parameters in the given order:
115// Number of allowed packet sizes, a vector with the allowed packet sizes,
116// Basic block samples, max number of channels that are supported.
117const ACMCodecDB::CodecSettings ACMCodecDB::codec_settings_[] = {
118#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwiberg65cb70d2017-03-03 06:16:28 -0800119 {2, {480, 960}, 0, 1},
Yves Gerey665174f2018-06-19 15:03:05 +0200120#if (defined(WEBRTC_CODEC_ISAC))
kwiberg65cb70d2017-03-03 06:16:28 -0800121 {1, {960}, 0, 1},
Yves Gerey665174f2018-06-19 15:03:05 +0200122#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000123#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000124 // Mono
kwibergec249d42015-09-24 04:32:03 -0700125 {4, {80, 160, 240, 320}, 0, 2},
126 {4, {160, 320, 480, 640}, 0, 2},
127 {2, {320, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000128 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700129 {4, {80, 160, 240, 320}, 0, 2},
130 {4, {160, 320, 480, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000131 {2, {320, 640}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132 // G.711, PCM mu-law and A-law.
133 // Mono
kwibergec249d42015-09-24 04:32:03 -0700134 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
135 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000136 // Stereo
kwibergec249d42015-09-24 04:32:03 -0700137 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
138 {6, {80, 160, 240, 320, 400, 480}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000139#ifdef WEBRTC_CODEC_ILBC
kwibergec249d42015-09-24 04:32:03 -0700140 {4, {160, 240, 320, 480}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000141#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000142 // Mono
kwibergec249d42015-09-24 04:32:03 -0700143 {6, {160, 320, 480, 640, 800, 960}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000144 // Stereo
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#ifdef WEBRTC_CODEC_OPUS
Yves Gerey665174f2018-06-19 15:03:05 +0200147// Opus supports frames shorter than 10ms,
148// but it doesn't help us to use them.
149// Mono and stereo.
minyue2e03c662017-02-01 17:31:11 -0800150#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
151 {5, {480, 960, 1920, 2880, 5760}, 0, 2},
152#else
kwibergec249d42015-09-24 04:32:03 -0700153 {4, {480, 960, 1920, 2880}, 0, 2},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000154#endif
minyue2e03c662017-02-01 17:31:11 -0800155#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000156 // Comfort noise for three different sampling frequencies.
kwibergec249d42015-09-24 04:32:03 -0700157 {1, {240}, 240, 1},
158 {1, {480}, 480, 1},
159 {1, {960}, 960, 1},
solenberg2779bab2016-11-17 04:45:19 -0800160// TODO(solenberg): What is this flag? It is never set in the build files.
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000161#ifdef ENABLE_48000_HZ
kwibergec249d42015-09-24 04:32:03 -0700162 {1, {1440}, 1440, 1},
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000163#endif
kwibergec249d42015-09-24 04:32:03 -0700164 {1, {240}, 240, 1},
solenberg2779bab2016-11-17 04:45:19 -0800165 {1, {240}, 240, 1},
166 {1, {240}, 240, 1},
167 {1, {240}, 240, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000168#ifdef WEBRTC_CODEC_RED
kwibergec249d42015-09-24 04:32:03 -0700169 {1, {0}, 0, 1},
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000170#endif
171 // To prevent compile errors due to trailing commas.
Yves Gerey665174f2018-06-19 15:03:05 +0200172 {-1, {-1}, -1, 0}};
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000173
174// Create a database of all NetEQ decoders at compile time.
175const NetEqDecoder ACMCodecDB::neteq_decoders_[] = {
176#if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
kwibergee1879c2015-10-29 06:20:28 -0700177 NetEqDecoder::kDecoderISAC,
Yves Gerey665174f2018-06-19 15:03:05 +0200178#if (defined(WEBRTC_CODEC_ISAC))
kwibergee1879c2015-10-29 06:20:28 -0700179 NetEqDecoder::kDecoderISACswb,
Yves Gerey665174f2018-06-19 15:03:05 +0200180#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000181#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000182 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700183 NetEqDecoder::kDecoderPCM16B, NetEqDecoder::kDecoderPCM16Bwb,
184 NetEqDecoder::kDecoderPCM16Bswb32kHz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000185 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700186 NetEqDecoder::kDecoderPCM16B_2ch, NetEqDecoder::kDecoderPCM16Bwb_2ch,
187 NetEqDecoder::kDecoderPCM16Bswb32kHz_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000188 // G.711, PCM mu-las and A-law.
189 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700190 NetEqDecoder::kDecoderPCMu, NetEqDecoder::kDecoderPCMa,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000191 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700192 NetEqDecoder::kDecoderPCMu_2ch, NetEqDecoder::kDecoderPCMa_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000193#ifdef WEBRTC_CODEC_ILBC
kwibergee1879c2015-10-29 06:20:28 -0700194 NetEqDecoder::kDecoderILBC,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000195#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000196 // Mono
kwibergee1879c2015-10-29 06:20:28 -0700197 NetEqDecoder::kDecoderG722,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000198 // Stereo
kwibergee1879c2015-10-29 06:20:28 -0700199 NetEqDecoder::kDecoderG722_2ch,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000200#ifdef WEBRTC_CODEC_OPUS
201 // Mono and stereo.
kwibergee1879c2015-10-29 06:20:28 -0700202 NetEqDecoder::kDecoderOpus,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000203#endif
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000204 // Comfort noise for three different sampling frequencies.
kwibergee1879c2015-10-29 06:20:28 -0700205 NetEqDecoder::kDecoderCNGnb, NetEqDecoder::kDecoderCNGwb,
206 NetEqDecoder::kDecoderCNGswb32kHz,
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000207#ifdef ENABLE_48000_HZ
kwibergee1879c2015-10-29 06:20:28 -0700208 NetEqDecoder::kDecoderCNGswb48kHz,
turaj@webrtc.org532f3dc2013-09-19 00:12:23 +0000209#endif
Yves Gerey665174f2018-06-19 15:03:05 +0200210 NetEqDecoder::kDecoderAVT, NetEqDecoder::kDecoderAVT16kHz,
211 NetEqDecoder::kDecoderAVT32kHz, NetEqDecoder::kDecoderAVT48kHz,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000212#ifdef WEBRTC_CODEC_RED
kwibergee1879c2015-10-29 06:20:28 -0700213 NetEqDecoder::kDecoderRED,
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000214#endif
215};
216
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000217// Enumerator for error codes when asking for codec database id.
218enum {
219 kInvalidCodec = -10,
220 kInvalidPayloadtype = -30,
221 kInvalidPacketSize = -40,
222 kInvalidRate = -50
223};
224
225// Gets the codec id number from the database. If there is some mismatch in
226// the codec settings, the function will return an error code.
227// NOTE! The first mismatch found will generate the return value.
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200228int ACMCodecDB::CodecNumber(const CodecInst& codec_inst) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000229 // Look for a matching codec in the database.
230 int codec_id = CodecId(codec_inst);
231
232 // Checks if we found a matching codec.
233 if (codec_id == -1) {
234 return kInvalidCodec;
235 }
236
237 // Checks the validity of payload type
kwiberg93a2feb2015-11-05 07:39:37 -0800238 if (!RentACodec::IsPayloadTypeValid(codec_inst.pltype)) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000239 return kInvalidPayloadtype;
240 }
241
242 // Comfort Noise is special case, packet-size & rate is not checked.
Niels Möller2edab4c2018-10-22 09:48:08 +0200243 if (absl::EqualsIgnoreCase(database_[codec_id].plname, "CN")) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000244 return codec_id;
245 }
246
247 // RED is special case, packet-size & rate is not checked.
Niels Möller2edab4c2018-10-22 09:48:08 +0200248 if (absl::EqualsIgnoreCase(database_[codec_id].plname, "red")) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000249 return codec_id;
250 }
251
252 // Checks the validity of packet size.
253 if (codec_settings_[codec_id].num_packet_sizes > 0) {
254 bool packet_size_ok = false;
255 int i;
256 int packet_size_samples;
257 for (i = 0; i < codec_settings_[codec_id].num_packet_sizes; i++) {
Yves Gerey665174f2018-06-19 15:03:05 +0200258 packet_size_samples = codec_settings_[codec_id].packet_sizes_samples[i];
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000259 if (codec_inst.pacsize == packet_size_samples) {
260 packet_size_ok = true;
261 break;
262 }
263 }
264
265 if (!packet_size_ok) {
266 return kInvalidPacketSize;
267 }
268 }
269
270 if (codec_inst.pacsize < 1) {
271 return kInvalidPacketSize;
272 }
273
274 // Check the validity of rate. Codecs with multiple rates have their own
275 // function for this.
Niels Möller2edab4c2018-10-22 09:48:08 +0200276 if (absl::EqualsIgnoreCase("isac", codec_inst.plname)) {
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200277 return IsISACRateValid(codec_inst.rate) ? codec_id : kInvalidRate;
Niels Möller2edab4c2018-10-22 09:48:08 +0200278 } else if (absl::EqualsIgnoreCase("ilbc", codec_inst.plname)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200279 return IsILBCRateValid(codec_inst.rate, codec_inst.pacsize) ? codec_id
280 : kInvalidRate;
Niels Möller2edab4c2018-10-22 09:48:08 +0200281 } else if (absl::EqualsIgnoreCase("opus", codec_inst.plname)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200282 return IsOpusRateValid(codec_inst.rate) ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000283 }
284
kwiberg4b938e52015-11-03 12:38:27 -0800285 return database_[codec_id].rate == codec_inst.rate ? codec_id : kInvalidRate;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000286}
287
288// Looks for a matching payload name, frequency, and channels in the
289// codec list. Need to check all three since some codecs have several codec
290// entries with different frequencies and/or channels.
291// Does not check other codec settings, such as payload type and packet size.
292// Returns the id of the codec, or -1 if no match is found.
293int ACMCodecDB::CodecId(const CodecInst& codec_inst) {
Yves Gerey665174f2018-06-19 15:03:05 +0200294 return (CodecId(codec_inst.plname, codec_inst.plfreq, codec_inst.channels));
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000295}
296
Peter Kasting69558702016-01-12 16:26:35 -0800297int ACMCodecDB::CodecId(const char* payload_name,
298 int frequency,
299 size_t channels) {
kwibergfce4a942015-10-27 11:40:24 -0700300 for (const CodecInst& ci : RentACodec::Database()) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000301 bool name_match = false;
302 bool frequency_match = false;
303 bool channels_match = false;
304
305 // Payload name, sampling frequency and number of channels need to match.
306 // NOTE! If |frequency| is -1, the frequency is not applicable, and is
307 // always treated as true, like for RED.
Niels Möller2edab4c2018-10-22 09:48:08 +0200308 name_match = absl::EqualsIgnoreCase(ci.plname, payload_name);
kwibergfce4a942015-10-27 11:40:24 -0700309 frequency_match = (frequency == ci.plfreq) || (frequency == -1);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000310 // The number of channels must match for all codecs but Opus.
Niels Möller2edab4c2018-10-22 09:48:08 +0200311 if (!absl::EqualsIgnoreCase(payload_name, "opus")) {
kwibergfce4a942015-10-27 11:40:24 -0700312 channels_match = (channels == ci.channels);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000313 } else {
314 // For opus we just check that number of channels is valid.
315 channels_match = (channels == 1 || channels == 2);
316 }
317
318 if (name_match && frequency_match && channels_match) {
319 // We have found a matching codec in the list.
kwibergfce4a942015-10-27 11:40:24 -0700320 return &ci - RentACodec::Database().data();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000321 }
322 }
323
324 // We didn't find a matching codec.
325 return -1;
326}
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200327// Gets codec id number from database for the receiver.
328int ACMCodecDB::ReceiverCodecNumber(const CodecInst& codec_inst) {
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000329 // Look for a matching codec in the database.
Henrik Lundin93ef1d82015-04-13 09:31:16 +0200330 return CodecId(codec_inst);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000331}
332
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000333} // namespace acm2
334
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000335} // namespace webrtc