Home | History | Annotate | Download | only in acm2
      1 /*
      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.
     18 #include "webrtc/modules/audio_coding/main/acm2/acm_codec_database.h"
     19 
     20 #include <assert.h>
     21 
     22 #include "webrtc/modules/audio_coding/main/acm2/acm_common_defs.h"
     23 #include "webrtc/modules/audio_coding/neteq/interface/audio_decoder.h"
     24 #include "webrtc/system_wrappers/interface/trace.h"
     25 
     26 // Includes needed to create the codecs.
     27 // G711, PCM mu-law and A-law
     28 #include "webrtc/modules/audio_coding/main/acm2/acm_pcma.h"
     29 #include "webrtc/modules/audio_coding/main/acm2/acm_pcmu.h"
     30 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h"
     31 // CNG
     32 #include "webrtc/modules/audio_coding/codecs/cng/include/webrtc_cng.h"
     33 #include "webrtc/modules/audio_coding/main/acm2/acm_cng.h"
     34 #ifdef WEBRTC_CODEC_ISAC
     35 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
     36 #endif
     37 #ifdef WEBRTC_CODEC_ISACFX
     38 #include "webrtc/modules/audio_coding/codecs/isac/fix/interface/isacfix.h"
     39 #endif
     40 #if (defined WEBRTC_CODEC_ISACFX) || (defined WEBRTC_CODEC_ISAC)
     41 #include "webrtc/modules/audio_coding/main/acm2/acm_isac.h"
     42 #include "webrtc/modules/audio_coding/main/acm2/acm_isac_macros.h"
     43 #endif
     44 #ifdef WEBRTC_CODEC_PCM16
     45 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
     46 #include "webrtc/modules/audio_coding/main/acm2/acm_pcm16b.h"
     47 #endif
     48 #ifdef WEBRTC_CODEC_ILBC
     49 #include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h"
     50 #include "webrtc/modules/audio_coding/main/acm2/acm_ilbc.h"
     51 #endif
     52 #ifdef WEBRTC_CODEC_AMR
     53 #include "webrtc/modules/audio_coding/codecs/amr/include/amr_interface.h"
     54 #include "webrtc/modules/audio_coding/main/acm2/acm_amr.h"
     55 #endif
     56 #ifdef WEBRTC_CODEC_AMRWB
     57 #include "webrtc/modules/audio_coding/codecs/amrwb/include/amrwb_interface.h"
     58 #include "webrtc/modules/audio_coding/main/acm2/acm_amrwb.h"
     59 #endif
     60 #ifdef WEBRTC_CODEC_CELT
     61 #include "webrtc/modules/audio_coding/codecs/celt/include/celt_interface.h"
     62 #include "webrtc/modules/audio_coding/main/acm2/acm_celt.h"
     63 #endif
     64 #ifdef WEBRTC_CODEC_G722
     65 #include "webrtc/modules/audio_coding/codecs/g722/include/g722_interface.h"
     66 #include "webrtc/modules/audio_coding/main/acm2/acm_g722.h"
     67 #endif
     68 #ifdef WEBRTC_CODEC_G722_1
     69 #include "webrtc/modules/audio_coding/codecs/g7221/include/g7221_interface.h"
     70 #include "webrtc/modules/audio_coding/main/acm2/acm_g7221.h"
     71 #endif
     72 #ifdef WEBRTC_CODEC_G722_1C
     73 #include "webrtc/modules/audio_coding/codecs/g7221c/include/g7221c_interface.h"
     74 #include "webrtc/modules/audio_coding/main/acm2/acm_g7221c.h"
     75 #endif
     76 #ifdef WEBRTC_CODEC_G729
     77 #include "webrtc/modules/audio_coding/codecs/g729/include/g729_interface.h"
     78 #include "webrtc/modules/audio_coding/main/acm2/acm_g729.h"
     79 #endif
     80 #ifdef WEBRTC_CODEC_G729_1
     81 #include "webrtc/modules/audio_coding/codecs/g7291/include/g7291_interface.h"
     82 #include "webrtc/modules/audio_coding/main/acm2/acm_g7291.h"
     83 #endif
     84 #ifdef WEBRTC_CODEC_GSMFR
     85 #include "webrtc/modules/audio_coding/codecs/gsmfr/include/gsmfr_interface.h"
     86 #include "webrtc/modules/audio_coding/main/acm2/acm_gsmfr.h"
     87 #endif
     88 #ifdef WEBRTC_CODEC_OPUS
     89 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
     90 #include "webrtc/modules/audio_coding/main/acm2/acm_opus.h"
     91 #endif
     92 #ifdef WEBRTC_CODEC_SPEEX
     93 #include "webrtc/modules/audio_coding/codecs/speex/include/speex_interface.h"
     94 #include "webrtc/modules/audio_coding/main/acm2/acm_speex.h"
     95 #endif
     96 #ifdef WEBRTC_CODEC_AVT
     97 #include "webrtc/modules/audio_coding/main/acm2/acm_dtmf_playout.h"
     98 #endif
     99 #ifdef WEBRTC_CODEC_RED
    100 #include "webrtc/modules/audio_coding/main/acm2/acm_red.h"
    101 #endif
    102 
    103 namespace webrtc {
    104 
    105 namespace acm2 {
    106 
    107 // Not yet used payload-types.
    108 // 83,  82,  81, 80, 79,  78,  77,  76,  75,  74,  73,  72,  71,  70,  69, 68,
    109 // 67, 66, 65
    110 
    111 const CodecInst ACMCodecDB::database_[] = {
    112 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
    113   {103, "ISAC", 16000, kIsacPacSize480, 1, kIsacWbDefaultRate},
    114 # if (defined(WEBRTC_CODEC_ISAC))
    115   {104, "ISAC", 32000, kIsacPacSize960, 1, kIsacSwbDefaultRate},
    116   {105, "ISAC", 48000, kIsacPacSize1440, 1, kIsacSwbDefaultRate},
    117 # endif
    118 #endif
    119 #ifdef WEBRTC_CODEC_PCM16
    120   // Mono
    121   {107, "L16", 8000, 80, 1, 128000},
    122   {108, "L16", 16000, 160, 1, 256000},
    123   {109, "L16", 32000, 320, 1, 512000},
    124   // Stereo
    125   {111, "L16", 8000, 80, 2, 128000},
    126   {112, "L16", 16000, 160, 2, 256000},
    127   {113, "L16", 32000, 320, 2, 512000},
    128 #endif
    129   // G.711, PCM mu-law and A-law.
    130   // Mono
    131   {0, "PCMU", 8000, 160, 1, 64000},
    132   {8, "PCMA", 8000, 160, 1, 64000},
    133   // Stereo
    134   {110, "PCMU", 8000, 160, 2, 64000},
    135   {118, "PCMA", 8000, 160, 2, 64000},
    136 #ifdef WEBRTC_CODEC_ILBC
    137   {102, "ILBC", 8000, 240, 1, 13300},
    138 #endif
    139 #ifdef WEBRTC_CODEC_AMR
    140   {114, "AMR", 8000, 160, 1, 12200},
    141 #endif
    142 #ifdef WEBRTC_CODEC_AMRWB
    143   {115, "AMR-WB", 16000, 320, 1, 20000},
    144 #endif
    145 #ifdef WEBRTC_CODEC_CELT
    146   // Mono
    147   {116, "CELT", 32000, 640, 1, 64000},
    148   // Stereo
    149   {117, "CELT", 32000, 640, 2, 64000},
    150 #endif
    151 #ifdef WEBRTC_CODEC_G722
    152   // Mono
    153   {9, "G722", 16000, 320, 1, 64000},
    154   // Stereo
    155   {119, "G722", 16000, 320, 2, 64000},
    156 #endif
    157 #ifdef WEBRTC_CODEC_G722_1
    158   {92, "G7221", 16000, 320, 1, 32000},
    159   {91, "G7221", 16000, 320, 1, 24000},
    160   {90, "G7221", 16000, 320, 1, 16000},
    161 #endif
    162 #ifdef WEBRTC_CODEC_G722_1C
    163   {89, "G7221", 32000, 640, 1, 48000},
    164   {88, "G7221", 32000, 640, 1, 32000},
    165   {87, "G7221", 32000, 640, 1, 24000},
    166 #endif
    167 #ifdef WEBRTC_CODEC_G729
    168   {18, "G729", 8000, 240, 1, 8000},
    169 #endif
    170 #ifdef WEBRTC_CODEC_G729_1
    171   {86, "G7291", 16000, 320, 1, 32000},
    172 #endif
    173 #ifdef WEBRTC_CODEC_GSMFR
    174   {3, "GSM", 8000, 160, 1, 13200},
    175 #endif
    176 #ifdef WEBRTC_CODEC_OPUS
    177   // Opus internally supports 48, 24, 16, 12, 8 kHz.
    178   // Mono and stereo.
    179   {120, "opus", 48000, 960, 2, 64000},
    180 #endif
    181 #ifdef WEBRTC_CODEC_SPEEX
    182   {85, "speex", 8000, 160, 1, 11000},
    183   {84, "speex", 16000, 320, 1, 22000},
    184 #endif
    185   // Comfort noise for four different sampling frequencies.
    186   {13, "CN", 8000, 240, 1, 0},
    187   {98, "CN", 16000, 480, 1, 0},
    188   {99, "CN", 32000, 960, 1, 0},
    189 #ifdef ENABLE_48000_HZ
    190   {100, "CN", 48000, 1440, 1, 0},
    191 #endif
    192 #ifdef WEBRTC_CODEC_AVT
    193   {106, "telephone-event", 8000, 240, 1, 0},
    194 #endif
    195 #ifdef WEBRTC_CODEC_RED
    196   {127, "red", 8000, 0, 1, 0},
    197 #endif
    198   // To prevent compile errors due to trailing commas.
    199   {-1, "Null", -1, -1, -1, -1}
    200 };
    201 
    202 // Create database with all codec settings at compile time.
    203 // Each entry needs the following parameters in the given order:
    204 // Number of allowed packet sizes, a vector with the allowed packet sizes,
    205 // Basic block samples, max number of channels that are supported.
    206 const ACMCodecDB::CodecSettings ACMCodecDB::codec_settings_[] = {
    207 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
    208     {2, {kIsacPacSize480, kIsacPacSize960}, 0, 1, true},
    209 # if (defined(WEBRTC_CODEC_ISAC))
    210     {1, {kIsacPacSize960}, 0, 1, false},
    211     {1, {kIsacPacSize1440}, 0, 1, true},
    212 # endif
    213 #endif
    214 #ifdef WEBRTC_CODEC_PCM16
    215     // Mono
    216     {4, {80, 160, 240, 320}, 0, 2, false},
    217     {4, {160, 320, 480, 640}, 0, 2, false},
    218     {2, {320, 640}, 0, 2, false},
    219     // Stereo
    220     {4, {80, 160, 240, 320}, 0, 2, false},
    221     {4, {160, 320, 480, 640}, 0, 2, false},
    222     {2, {320, 640}, 0, 2},
    223 #endif
    224     // G.711, PCM mu-law and A-law.
    225     // Mono
    226     {6, {80, 160, 240, 320, 400, 480}, 0, 2, false},
    227     {6, {80, 160, 240, 320, 400, 480}, 0, 2, false},
    228     // Stereo
    229     {6, {80, 160, 240, 320, 400, 480}, 0, 2, false},
    230     {6, {80, 160, 240, 320, 400, 480}, 0, 2, false},
    231 #ifdef WEBRTC_CODEC_ILBC
    232     {4, {160, 240, 320, 480}, 0, 1, false},
    233 #endif
    234 #ifdef WEBRTC_CODEC_AMR
    235     {3, {160, 320, 480}, 0, 1, true},
    236 #endif
    237 #ifdef WEBRTC_CODEC_AMRWB
    238     {3, {320, 640, 960}, 0, 1, true},
    239 #endif
    240 #ifdef WEBRTC_CODEC_CELT
    241     // Mono
    242     {1, {640}, 0, 2, false},
    243     // Stereo
    244     {1, {640}, 0, 2, false},
    245 #endif
    246 #ifdef WEBRTC_CODEC_G722
    247     // Mono
    248     {6, {160, 320, 480, 640, 800, 960}, 0, 2, false},
    249     // Stereo
    250     {6, {160, 320, 480, 640, 800, 960}, 0, 2, false},
    251 #endif
    252 #ifdef WEBRTC_CODEC_G722_1
    253     {1, {320}, 320, 1, false},
    254     {1, {320}, 320, 1, false},
    255     {1, {320}, 320, 1, false},
    256 #endif
    257 #ifdef WEBRTC_CODEC_G722_1C
    258     {1, {640}, 640, 1, false},
    259     {1, {640}, 640, 1, false},
    260     {1, {640}, 640, 1, false},
    261 #endif
    262 #ifdef WEBRTC_CODEC_G729
    263     {6, {80, 160, 240, 320, 400, 480}, 0, 1, false},
    264 #endif
    265 #ifdef WEBRTC_CODEC_G729_1
    266     {3, {320, 640, 960}, 0, 1, false},
    267 #endif
    268 #ifdef WEBRTC_CODEC_GSMFR
    269     {3, {160, 320, 480}, 160, 1, false},
    270 #endif
    271 #ifdef WEBRTC_CODEC_OPUS
    272     // Opus supports frames shorter than 10ms,
    273     // but it doesn't help us to use them.
    274     // Mono and stereo.
    275     {4, {480, 960, 1920, 2880}, 0, 2, false},
    276 #endif
    277 #ifdef WEBRTC_CODEC_SPEEX
    278     {3, {160, 320, 480}, 0, 1, false},
    279     {3, {320, 640, 960}, 0, 1, false},
    280 #endif
    281     // Comfort noise for three different sampling frequencies.
    282     {1, {240}, 240, 1, false},
    283     {1, {480}, 480, 1, false},
    284     {1, {960}, 960, 1, false},
    285 #ifdef ENABLE_48000_HZ
    286     {1, {1440}, 1440, 1, false},
    287 #endif
    288 #ifdef WEBRTC_CODEC_AVT
    289     {1, {240}, 240, 1, false},
    290 #endif
    291 #ifdef WEBRTC_CODEC_RED
    292     {1, {0}, 0, 1, false},
    293 #endif
    294     // To prevent compile errors due to trailing commas.
    295     {-1, {-1}, -1, -1, false}
    296 };
    297 
    298 // Create a database of all NetEQ decoders at compile time.
    299 const NetEqDecoder ACMCodecDB::neteq_decoders_[] = {
    300 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
    301     kDecoderISAC,
    302 # if (defined(WEBRTC_CODEC_ISAC))
    303     kDecoderISACswb,
    304     kDecoderISACfb,
    305 # endif
    306 #endif
    307 #ifdef WEBRTC_CODEC_PCM16
    308     // Mono
    309     kDecoderPCM16B,
    310     kDecoderPCM16Bwb,
    311     kDecoderPCM16Bswb32kHz,
    312     // Stereo
    313     kDecoderPCM16B_2ch,
    314     kDecoderPCM16Bwb_2ch,
    315     kDecoderPCM16Bswb32kHz_2ch,
    316 #endif
    317     // G.711, PCM mu-las and A-law.
    318     // Mono
    319     kDecoderPCMu,
    320     kDecoderPCMa,
    321     // Stereo
    322     kDecoderPCMu_2ch,
    323     kDecoderPCMa_2ch,
    324 #ifdef WEBRTC_CODEC_ILBC
    325     kDecoderILBC,
    326 #endif
    327 #ifdef WEBRTC_CODEC_AMR
    328     kDecoderAMR,
    329 #endif
    330 #ifdef WEBRTC_CODEC_AMRWB
    331     kDecoderAMRWB,
    332 #endif
    333 #ifdef WEBRTC_CODEC_CELT
    334     // Mono
    335     kDecoderCELT_32,
    336     // Stereo
    337     kDecoderCELT_32_2ch,
    338 #endif
    339 #ifdef WEBRTC_CODEC_G722
    340     // Mono
    341     kDecoderG722,
    342     // Stereo
    343     kDecoderG722_2ch,
    344 #endif
    345 #ifdef WEBRTC_CODEC_G722_1
    346     kDecoderG722_1_32,
    347     kDecoderG722_1_24,
    348     kDecoderG722_1_16,
    349 #endif
    350 #ifdef WEBRTC_CODEC_G722_1C
    351     kDecoderG722_1C_48,
    352     kDecoderG722_1C_32,
    353     kDecoderG722_1C_24,
    354 #endif
    355 #ifdef WEBRTC_CODEC_G729
    356     kDecoderG729,
    357 #endif
    358 #ifdef WEBRTC_CODEC_G729_1
    359     kDecoderG729_1,
    360 #endif
    361 #ifdef WEBRTC_CODEC_GSMFR
    362     kDecoderGSMFR,
    363 #endif
    364 #ifdef WEBRTC_CODEC_OPUS
    365     // Mono and stereo.
    366     kDecoderOpus,
    367 #endif
    368 #ifdef WEBRTC_CODEC_SPEEX
    369     kDecoderSPEEX_8,
    370     kDecoderSPEEX_16,
    371 #endif
    372     // Comfort noise for three different sampling frequencies.
    373     kDecoderCNGnb,
    374     kDecoderCNGwb,
    375     kDecoderCNGswb32kHz
    376 #ifdef ENABLE_48000_HZ
    377     , kDecoderCNGswb48kHz
    378 #endif
    379 #ifdef WEBRTC_CODEC_AVT
    380     , kDecoderAVT
    381 #endif
    382 #ifdef WEBRTC_CODEC_RED
    383     , kDecoderRED
    384 #endif
    385 };
    386 
    387 // Get codec information from database.
    388 // TODO(tlegrand): replace memcpy with a pointer to the data base memory.
    389 int ACMCodecDB::Codec(int codec_id, CodecInst* codec_inst) {
    390   // Error check to see that codec_id is not out of bounds.
    391   if ((codec_id < 0) || (codec_id >= kNumCodecs)) {
    392     return -1;
    393   }
    394 
    395   // Copy database information for the codec to the output.
    396   memcpy(codec_inst, &database_[codec_id], sizeof(CodecInst));
    397 
    398   return 0;
    399 }
    400 
    401 // Enumerator for error codes when asking for codec database id.
    402 enum {
    403   kInvalidCodec = -10,
    404   kInvalidPayloadtype = -30,
    405   kInvalidPacketSize = -40,
    406   kInvalidRate = -50
    407 };
    408 
    409 // Gets the codec id number from the database. If there is some mismatch in
    410 // the codec settings, the function will return an error code.
    411 // NOTE! The first mismatch found will generate the return value.
    412 int ACMCodecDB::CodecNumber(const CodecInst& codec_inst, int* mirror_id) {
    413   // Look for a matching codec in the database.
    414   int codec_id = CodecId(codec_inst);
    415 
    416   // Checks if we found a matching codec.
    417   if (codec_id == -1) {
    418     return kInvalidCodec;
    419   }
    420 
    421   // Checks the validity of payload type
    422   if (!ValidPayloadType(codec_inst.pltype)) {
    423     return kInvalidPayloadtype;
    424   }
    425 
    426   // Comfort Noise is special case, packet-size & rate is not checked.
    427   if (STR_CASE_CMP(database_[codec_id].plname, "CN") == 0) {
    428     *mirror_id = codec_id;
    429     return codec_id;
    430   }
    431 
    432   // RED is special case, packet-size & rate is not checked.
    433   if (STR_CASE_CMP(database_[codec_id].plname, "red") == 0) {
    434     *mirror_id = codec_id;
    435     return codec_id;
    436   }
    437 
    438   // Checks the validity of packet size.
    439   if (codec_settings_[codec_id].num_packet_sizes > 0) {
    440     bool packet_size_ok = false;
    441     int i;
    442     int packet_size_samples;
    443     for (i = 0; i < codec_settings_[codec_id].num_packet_sizes; i++) {
    444       packet_size_samples =
    445           codec_settings_[codec_id].packet_sizes_samples[i];
    446       if (codec_inst.pacsize == packet_size_samples) {
    447         packet_size_ok = true;
    448         break;
    449       }
    450     }
    451 
    452     if (!packet_size_ok) {
    453       return kInvalidPacketSize;
    454     }
    455   }
    456 
    457   if (codec_inst.pacsize < 1) {
    458     return kInvalidPacketSize;
    459   }
    460 
    461   // Check the validity of rate. Codecs with multiple rates have their own
    462   // function for this.
    463   *mirror_id = codec_id;
    464   if (STR_CASE_CMP("isac", codec_inst.plname) == 0) {
    465     if (IsISACRateValid(codec_inst.rate)) {
    466       // Set mirrorID to iSAC WB which is only created once to be used both for
    467       // iSAC WB and SWB, because they need to share struct.
    468       *mirror_id = kISAC;
    469       return  codec_id;
    470     } else {
    471       return kInvalidRate;
    472     }
    473   } else if (STR_CASE_CMP("ilbc", codec_inst.plname) == 0) {
    474     return IsILBCRateValid(codec_inst.rate, codec_inst.pacsize)
    475         ? codec_id : kInvalidRate;
    476   } else if (STR_CASE_CMP("amr", codec_inst.plname) == 0) {
    477     return IsAMRRateValid(codec_inst.rate)
    478         ? codec_id : kInvalidRate;
    479   } else if (STR_CASE_CMP("amr-wb", codec_inst.plname) == 0) {
    480     return IsAMRwbRateValid(codec_inst.rate)
    481         ? codec_id : kInvalidRate;
    482   } else if (STR_CASE_CMP("g7291", codec_inst.plname) == 0) {
    483     return IsG7291RateValid(codec_inst.rate)
    484         ? codec_id : kInvalidRate;
    485   } else if (STR_CASE_CMP("opus", codec_inst.plname) == 0) {
    486     return IsOpusRateValid(codec_inst.rate)
    487         ? codec_id : kInvalidRate;
    488   } else if (STR_CASE_CMP("speex", codec_inst.plname) == 0) {
    489     return IsSpeexRateValid(codec_inst.rate)
    490         ? codec_id : kInvalidRate;
    491   } else if (STR_CASE_CMP("celt", codec_inst.plname) == 0) {
    492     return IsCeltRateValid(codec_inst.rate)
    493         ? codec_id : kInvalidRate;
    494   }
    495 
    496   return IsRateValid(codec_id, codec_inst.rate) ?
    497       codec_id : kInvalidRate;
    498 }
    499 
    500 // Looks for a matching payload name, frequency, and channels in the
    501 // codec list. Need to check all three since some codecs have several codec
    502 // entries with different frequencies and/or channels.
    503 // Does not check other codec settings, such as payload type and packet size.
    504 // Returns the id of the codec, or -1 if no match is found.
    505 int ACMCodecDB::CodecId(const CodecInst& codec_inst) {
    506   return (CodecId(codec_inst.plname, codec_inst.plfreq,
    507                   codec_inst.channels));
    508 }
    509 
    510 int ACMCodecDB::CodecId(const char* payload_name, int frequency, int channels) {
    511   for (int id = 0; id < kNumCodecs; id++) {
    512     bool name_match = false;
    513     bool frequency_match = false;
    514     bool channels_match = false;
    515 
    516     // Payload name, sampling frequency and number of channels need to match.
    517     // NOTE! If |frequency| is -1, the frequency is not applicable, and is
    518     // always treated as true, like for RED.
    519     name_match = (STR_CASE_CMP(database_[id].plname, payload_name) == 0);
    520     frequency_match = (frequency == database_[id].plfreq) || (frequency == -1);
    521     // The number of channels must match for all codecs but Opus.
    522     if (STR_CASE_CMP(payload_name, "opus") != 0) {
    523       channels_match = (channels == database_[id].channels);
    524     } else {
    525       // For opus we just check that number of channels is valid.
    526       channels_match = (channels == 1 || channels == 2);
    527     }
    528 
    529     if (name_match && frequency_match && channels_match) {
    530       // We have found a matching codec in the list.
    531       return id;
    532     }
    533   }
    534 
    535   // We didn't find a matching codec.
    536   return -1;
    537 }
    538 // Gets codec id number, and mirror id, from database for the receiver.
    539 int ACMCodecDB::ReceiverCodecNumber(const CodecInst& codec_inst,
    540                                     int* mirror_id) {
    541   // Look for a matching codec in the database.
    542   int codec_id = CodecId(codec_inst);
    543 
    544   // Set |mirror_id| to |codec_id|, except for iSAC. In case of iSAC we always
    545   // set |mirror_id| to iSAC WB (kISAC) which is only created once to be used
    546   // both for iSAC WB and SWB, because they need to share struct.
    547   if (STR_CASE_CMP(codec_inst.plname, "ISAC") != 0) {
    548     *mirror_id = codec_id;
    549   } else {
    550     *mirror_id = kISAC;
    551   }
    552 
    553   return codec_id;
    554 }
    555 
    556 // Returns the codec sampling frequency for codec with id = "codec_id" in
    557 // database.
    558 int ACMCodecDB::CodecFreq(int codec_id) {
    559   // Error check to see that codec_id is not out of bounds.
    560   if (codec_id < 0 || codec_id >= kNumCodecs) {
    561     return -1;
    562   }
    563 
    564   return database_[codec_id].plfreq;
    565 }
    566 
    567 // Returns the codec's basic coding block size in samples.
    568 int ACMCodecDB::BasicCodingBlock(int codec_id) {
    569   // Error check to see that codec_id is not out of bounds.
    570   if (codec_id < 0 || codec_id >= kNumCodecs) {
    571     return -1;
    572   }
    573 
    574   return codec_settings_[codec_id].basic_block_samples;
    575 }
    576 
    577 // Returns the NetEQ decoder database.
    578 const NetEqDecoder* ACMCodecDB::NetEQDecoders() {
    579   return neteq_decoders_;
    580 }
    581 
    582 // Gets mirror id. The Id is used for codecs sharing struct for settings that
    583 // need different payload types.
    584 int ACMCodecDB::MirrorID(int codec_id) {
    585   if (STR_CASE_CMP(database_[codec_id].plname, "isac") == 0) {
    586     return kISAC;
    587   } else {
    588     return codec_id;
    589   }
    590 }
    591 
    592 // Creates memory/instance for storing codec state.
    593 ACMGenericCodec* ACMCodecDB::CreateCodecInstance(const CodecInst& codec_inst) {
    594   // All we have support for right now.
    595   if (!STR_CASE_CMP(codec_inst.plname, "ISAC")) {
    596 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
    597     return new ACMISAC(kISAC);
    598 #endif
    599   } else if (!STR_CASE_CMP(codec_inst.plname, "PCMU")) {
    600     if (codec_inst.channels == 1) {
    601       return new ACMPCMU(kPCMU);
    602     } else {
    603       return new ACMPCMU(kPCMU_2ch);
    604     }
    605   } else if (!STR_CASE_CMP(codec_inst.plname, "PCMA")) {
    606     if (codec_inst.channels == 1) {
    607       return new ACMPCMA(kPCMA);
    608     } else {
    609       return new ACMPCMA(kPCMA_2ch);
    610     }
    611   } else if (!STR_CASE_CMP(codec_inst.plname, "ILBC")) {
    612 #ifdef WEBRTC_CODEC_ILBC
    613     return new ACMILBC(kILBC);
    614 #endif
    615   } else if (!STR_CASE_CMP(codec_inst.plname, "AMR")) {
    616 #ifdef WEBRTC_CODEC_AMR
    617     return new ACMAMR(kGSMAMR);
    618 #endif
    619   } else if (!STR_CASE_CMP(codec_inst.plname, "AMR-WB")) {
    620 #ifdef WEBRTC_CODEC_AMRWB
    621     return new ACMAMRwb(kGSMAMRWB);
    622 #endif
    623   } else if (!STR_CASE_CMP(codec_inst.plname, "CELT")) {
    624 #ifdef WEBRTC_CODEC_CELT
    625     if (codec_inst.channels == 1) {
    626       return new ACMCELT(kCELT32);
    627     } else {
    628       return new ACMCELT(kCELT32_2ch);
    629     }
    630 #endif
    631   } else if (!STR_CASE_CMP(codec_inst.plname, "G722")) {
    632 #ifdef WEBRTC_CODEC_G722
    633     if (codec_inst.channels == 1) {
    634       return new ACMG722(kG722);
    635     } else {
    636       return new ACMG722(kG722_2ch);
    637     }
    638 #endif
    639   } else if (!STR_CASE_CMP(codec_inst.plname, "G7221")) {
    640     switch (codec_inst.plfreq) {
    641       case 16000: {
    642 #ifdef WEBRTC_CODEC_G722_1
    643         int codec_id;
    644         switch (codec_inst->rate) {
    645           case 16000 : {
    646             codec_id = kG722_1_16;
    647             break;
    648           }
    649           case 24000 : {
    650             codec_id = kG722_1_24;
    651             break;
    652           }
    653           case 32000 : {
    654             codec_id = kG722_1_32;
    655             break;
    656           }
    657           default: {
    658             return NULL;
    659           }
    660           return new ACMG722_1(codec_id);
    661         }
    662 #endif
    663       }
    664       case 32000: {
    665 #ifdef WEBRTC_CODEC_G722_1C
    666         int codec_id;
    667         switch (codec_inst->rate) {
    668           case 24000 : {
    669             codec_id = kG722_1C_24;
    670             break;
    671           }
    672           case 32000 : {
    673             codec_id = kG722_1C_32;
    674             break;
    675           }
    676           case 48000 : {
    677             codec_id = kG722_1C_48;
    678             break;
    679           }
    680           default: {
    681             return NULL;
    682           }
    683           return new ACMG722_1C(codec_id);
    684         }
    685 #endif
    686       }
    687     }
    688   } else if (!STR_CASE_CMP(codec_inst.plname, "CN")) {
    689     // For CN we need to check sampling frequency to know what codec to create.
    690     int codec_id;
    691     switch (codec_inst.plfreq) {
    692       case 8000: {
    693         codec_id = kCNNB;
    694         break;
    695       }
    696       case 16000: {
    697         codec_id = kCNWB;
    698         break;
    699       }
    700       case 32000: {
    701         codec_id = kCNSWB;
    702         break;
    703       }
    704 #ifdef ENABLE_48000_HZ
    705       case 48000: {
    706         codec_id = kCNFB;
    707         break;
    708       }
    709 #endif
    710       default: {
    711         return NULL;
    712       }
    713     }
    714     return new ACMCNG(codec_id);
    715   } else if (!STR_CASE_CMP(codec_inst.plname, "G729")) {
    716 #ifdef WEBRTC_CODEC_G729
    717     return new ACMG729(kG729);
    718 #endif
    719   } else if (!STR_CASE_CMP(codec_inst.plname, "G7291")) {
    720 #ifdef WEBRTC_CODEC_G729_1
    721     return new ACMG729_1(kG729_1);
    722 #endif
    723   } else if (!STR_CASE_CMP(codec_inst.plname, "opus")) {
    724 #ifdef WEBRTC_CODEC_OPUS
    725     return new ACMOpus(kOpus);
    726 #endif
    727   } else if (!STR_CASE_CMP(codec_inst.plname, "speex")) {
    728 #ifdef WEBRTC_CODEC_SPEEX
    729     int codec_id;
    730     switch (codec_inst->plfreq) {
    731       case 8000: {
    732         codec_id = kSPEEX8;
    733         break;
    734       }
    735       case 16000: {
    736         codec_id = kSPEEX16;
    737         break;
    738       }
    739       default: {
    740         return NULL;
    741       }
    742     }
    743     return new ACMSPEEX(codec_id);
    744 #endif
    745   } else if (!STR_CASE_CMP(codec_inst.plname, "CN")) {
    746     // For CN we need to check sampling frequency to know what codec to create.
    747     int codec_id;
    748     switch (codec_inst.plfreq) {
    749       case 8000: {
    750         codec_id = kCNNB;
    751         break;
    752       }
    753       case 16000: {
    754         codec_id = kCNWB;
    755         break;
    756       }
    757       case 32000: {
    758         codec_id = kCNSWB;
    759         break;
    760       }
    761 #ifdef ENABLE_48000_HZ
    762       case 48000: {
    763         codec_id = kCNFB;
    764         break;
    765       }
    766 #endif
    767       default: {
    768         return NULL;
    769       }
    770     }
    771     return new ACMCNG(codec_id);
    772   } else if (!STR_CASE_CMP(codec_inst.plname, "L16")) {
    773 #ifdef WEBRTC_CODEC_PCM16
    774     // For L16 we need to check sampling frequency to know what codec to create.
    775     int codec_id;
    776     if (codec_inst.channels == 1) {
    777       switch (codec_inst.plfreq) {
    778         case 8000: {
    779           codec_id = kPCM16B;
    780           break;
    781         }
    782         case 16000: {
    783           codec_id = kPCM16Bwb;
    784           break;
    785         }
    786         case 32000: {
    787           codec_id = kPCM16Bswb32kHz;
    788           break;
    789         }
    790         default: {
    791           return NULL;
    792         }
    793       }
    794     } else {
    795       switch (codec_inst.plfreq) {
    796         case 8000: {
    797           codec_id = kPCM16B_2ch;
    798           break;
    799         }
    800         case 16000: {
    801           codec_id = kPCM16Bwb_2ch;
    802           break;
    803         }
    804         case 32000: {
    805           codec_id = kPCM16Bswb32kHz_2ch;
    806           break;
    807         }
    808         default: {
    809           return NULL;
    810         }
    811       }
    812     }
    813     return new ACMPCM16B(codec_id);
    814 #endif
    815   } else if (!STR_CASE_CMP(codec_inst.plname, "telephone-event")) {
    816 #ifdef WEBRTC_CODEC_AVT
    817     return new ACMDTMFPlayout(kAVT);
    818 #endif
    819   } else if (!STR_CASE_CMP(codec_inst.plname, "red")) {
    820 #ifdef WEBRTC_CODEC_RED
    821     return new ACMRED(kRED);
    822 #endif
    823   }
    824   return NULL;
    825 }
    826 
    827 // Checks if the bitrate is valid for the codec.
    828 bool ACMCodecDB::IsRateValid(int codec_id, int rate) {
    829   if (database_[codec_id].rate == rate) {
    830     return true;
    831   } else {
    832     return false;
    833   }
    834 }
    835 
    836 // Checks if the bitrate is valid for iSAC.
    837 bool ACMCodecDB::IsISACRateValid(int rate) {
    838   if ((rate == -1) || ((rate <= 56000) && (rate >= 10000))) {
    839     return true;
    840   } else {
    841     return false;
    842   }
    843 }
    844 
    845 // Checks if the bitrate is valid for iLBC.
    846 bool ACMCodecDB::IsILBCRateValid(int rate, int frame_size_samples) {
    847   if (((frame_size_samples == 240) || (frame_size_samples == 480)) &&
    848       (rate == 13300)) {
    849     return true;
    850   } else if (((frame_size_samples == 160) || (frame_size_samples == 320)) &&
    851       (rate == 15200)) {
    852     return true;
    853   } else {
    854     return false;
    855   }
    856 }
    857 
    858 // Check if the bitrate is valid for the GSM-AMR.
    859 bool ACMCodecDB::IsAMRRateValid(int rate) {
    860   switch (rate) {
    861     case 4750:
    862     case 5150:
    863     case 5900:
    864     case 6700:
    865     case 7400:
    866     case 7950:
    867     case 10200:
    868     case 12200: {
    869       return true;
    870     }
    871     default: {
    872       return false;
    873     }
    874   }
    875 }
    876 
    877 // Check if the bitrate is valid for GSM-AMR-WB.
    878 bool ACMCodecDB::IsAMRwbRateValid(int rate) {
    879   switch (rate) {
    880     case 7000:
    881     case 9000:
    882     case 12000:
    883     case 14000:
    884     case 16000:
    885     case 18000:
    886     case 20000:
    887     case 23000:
    888     case 24000: {
    889       return true;
    890     }
    891     default: {
    892       return false;
    893     }
    894   }
    895 }
    896 
    897 // Check if the bitrate is valid for G.729.1.
    898 bool ACMCodecDB::IsG7291RateValid(int rate) {
    899   switch (rate) {
    900     case 8000:
    901     case 12000:
    902     case 14000:
    903     case 16000:
    904     case 18000:
    905     case 20000:
    906     case 22000:
    907     case 24000:
    908     case 26000:
    909     case 28000:
    910     case 30000:
    911     case 32000: {
    912       return true;
    913     }
    914     default: {
    915       return false;
    916     }
    917   }
    918 }
    919 
    920 // Checks if the bitrate is valid for Speex.
    921 bool ACMCodecDB::IsSpeexRateValid(int rate) {
    922   if (rate > 2000) {
    923     return true;
    924   } else {
    925     return false;
    926   }
    927 }
    928 
    929 // Checks if the bitrate is valid for Opus.
    930 bool ACMCodecDB::IsOpusRateValid(int rate) {
    931   if ((rate < 6000) || (rate > 510000)) {
    932     return false;
    933   }
    934   return true;
    935 }
    936 
    937 // Checks if the bitrate is valid for Celt.
    938 bool ACMCodecDB::IsCeltRateValid(int rate) {
    939   if ((rate >= 48000) && (rate <= 128000)) {
    940     return true;
    941   } else {
    942     return false;
    943   }
    944 }
    945 
    946 // Checks if the payload type is in the valid range.
    947 bool ACMCodecDB::ValidPayloadType(int payload_type) {
    948   if ((payload_type < 0) || (payload_type > 127)) {
    949     return false;
    950   }
    951   return true;
    952 }
    953 
    954 bool ACMCodecDB::OwnsDecoder(int codec_id) {
    955   assert(codec_id >= 0 && codec_id < ACMCodecDB::kNumCodecs);
    956   return ACMCodecDB::codec_settings_[codec_id].owns_decoder;
    957 }
    958 
    959 }  // namespace acm2
    960 
    961 }  // namespace webrtc
    962