Home | History | Annotate | Download | only in neteq
      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 #include "webrtc/modules/audio_coding/neteq/audio_decoder_impl.h"
     12 
     13 #include <assert.h>
     14 #include <stdlib.h>
     15 
     16 #include <string>
     17 
     18 #include "gtest/gtest.h"
     19 #include "webrtc/common_audio/resampler/include/resampler.h"
     20 #ifdef WEBRTC_CODEC_CELT
     21 #include "webrtc/modules/audio_coding/codecs/celt/include/celt_interface.h"
     22 #endif
     23 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h"
     24 #include "webrtc/modules/audio_coding/codecs/g722/include/g722_interface.h"
     25 #include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h"
     26 #include "webrtc/modules/audio_coding/codecs/isac/fix/interface/isacfix.h"
     27 #include "webrtc/modules/audio_coding/codecs/isac/main/interface/isac.h"
     28 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
     29 #include "webrtc/modules/audio_coding/codecs/pcm16b/include/pcm16b.h"
     30 #include "webrtc/system_wrappers/interface/data_log.h"
     31 #include "webrtc/test/testsupport/fileutils.h"
     32 
     33 namespace webrtc {
     34 
     35 class AudioDecoderTest : public ::testing::Test {
     36  protected:
     37   AudioDecoderTest()
     38     : input_fp_(NULL),
     39       input_(NULL),
     40       encoded_(NULL),
     41       decoded_(NULL),
     42       frame_size_(0),
     43       data_length_(0),
     44       encoded_bytes_(0),
     45       channels_(1),
     46       decoder_(NULL) {
     47     input_file_ = webrtc::test::ProjectRootPath() +
     48         "resources/audio_coding/testfile32kHz.pcm";
     49   }
     50 
     51   virtual ~AudioDecoderTest() {}
     52 
     53   virtual void SetUp() {
     54     // Create arrays.
     55     ASSERT_GT(data_length_, 0u) << "The test must set data_length_ > 0";
     56     input_ = new int16_t[data_length_];
     57     encoded_ = new uint8_t[data_length_ * 2];
     58     decoded_ = new int16_t[data_length_ * channels_];
     59     // Open input file.
     60     input_fp_ = fopen(input_file_.c_str(), "rb");
     61     ASSERT_TRUE(input_fp_ != NULL) << "Failed to open file " << input_file_;
     62     // Read data to |input_|.
     63     ASSERT_EQ(data_length_,
     64               fread(input_, sizeof(int16_t), data_length_, input_fp_)) <<
     65                   "Could not read enough data from file";
     66     // Logging to view input and output in Matlab.
     67     // Use 'gyp -Denable_data_logging=1' to enable logging.
     68     DataLog::CreateLog();
     69     DataLog::AddTable("CodecTest");
     70     DataLog::AddColumn("CodecTest", "input", 1);
     71     DataLog::AddColumn("CodecTest", "output", 1);
     72   }
     73 
     74   virtual void TearDown() {
     75     delete decoder_;
     76     decoder_ = NULL;
     77     // Close input file.
     78     fclose(input_fp_);
     79     // Delete arrays.
     80     delete [] input_;
     81     input_ = NULL;
     82     delete [] encoded_;
     83     encoded_ = NULL;
     84     delete [] decoded_;
     85     decoded_ = NULL;
     86     // Close log.
     87     DataLog::ReturnLog();
     88   }
     89 
     90   virtual void InitEncoder() { }
     91 
     92   // This method must be implemented for all tests derived from this class.
     93   virtual int EncodeFrame(const int16_t* input, size_t input_len,
     94                           uint8_t* output) = 0;
     95 
     96   // Encodes and decodes audio. The absolute difference between the input and
     97   // output is compared vs |tolerance|, and the mean-squared error is compared
     98   // with |mse|. The encoded stream should contain |expected_bytes|. For stereo
     99   // audio, the absolute difference between the two channels is compared vs
    100   // |channel_diff_tolerance|.
    101   void EncodeDecodeTest(size_t expected_bytes, int tolerance, double mse,
    102                         int delay = 0, int channel_diff_tolerance = 0) {
    103     ASSERT_GE(tolerance, 0) << "Test must define a tolerance >= 0";
    104     ASSERT_GE(channel_diff_tolerance, 0) <<
    105         "Test must define a channel_diff_tolerance >= 0";
    106     size_t processed_samples = 0u;
    107     encoded_bytes_ = 0u;
    108     InitEncoder();
    109     EXPECT_EQ(0, decoder_->Init());
    110     while (processed_samples + frame_size_ <= data_length_) {
    111       size_t enc_len = EncodeFrame(&input_[processed_samples], frame_size_,
    112                                    &encoded_[encoded_bytes_]);
    113       AudioDecoder::SpeechType speech_type;
    114       size_t dec_len = decoder_->Decode(&encoded_[encoded_bytes_], enc_len,
    115                                         &decoded_[processed_samples *
    116                                                   channels_],
    117                                         &speech_type);
    118       EXPECT_EQ(frame_size_ * channels_, dec_len);
    119       encoded_bytes_ += enc_len;
    120       processed_samples += frame_size_;
    121     }
    122     // For some codecs it doesn't make sense to check expected number of bytes,
    123     // since the number can vary for different platforms. Opus and iSAC are
    124     // such codecs. In this case expected_bytes is set to 0.
    125     if (expected_bytes) {
    126       EXPECT_EQ(expected_bytes, encoded_bytes_);
    127     }
    128     CompareInputOutput(processed_samples, tolerance, delay);
    129     if (channels_ == 2)
    130       CompareTwoChannels(processed_samples, channel_diff_tolerance);
    131     EXPECT_LE(MseInputOutput(processed_samples, delay), mse);
    132   }
    133 
    134   // The absolute difference between the input and output (the first channel) is
    135   // compared vs |tolerance|. The parameter |delay| is used to correct for codec
    136   // delays.
    137   virtual void CompareInputOutput(size_t num_samples, int tolerance,
    138                                   int delay) const {
    139     assert(num_samples <= data_length_);
    140     for (unsigned int n = 0; n < num_samples - delay; ++n) {
    141       ASSERT_NEAR(input_[n], decoded_[channels_ * n + delay], tolerance) <<
    142           "Exit test on first diff; n = " << n;
    143       DataLog::InsertCell("CodecTest", "input", input_[n]);
    144       DataLog::InsertCell("CodecTest", "output", decoded_[channels_ * n]);
    145       DataLog::NextRow("CodecTest");
    146     }
    147   }
    148 
    149   // The absolute difference between the two channels in a stereo is compared vs
    150   // |tolerance|.
    151   virtual void CompareTwoChannels(size_t samples_per_channel,
    152                                   int tolerance) const {
    153     assert(samples_per_channel <= data_length_);
    154     for (unsigned int n = 0; n < samples_per_channel; ++n)
    155       ASSERT_NEAR(decoded_[channels_ * n], decoded_[channels_ * n + 1],
    156                   tolerance) << "Stereo samples differ.";
    157   }
    158 
    159   // Calculates mean-squared error between input and output (the first channel).
    160   // The parameter |delay| is used to correct for codec delays.
    161   virtual double MseInputOutput(size_t num_samples, int delay) const {
    162     assert(num_samples <= data_length_);
    163     if (num_samples == 0) return 0.0;
    164     double squared_sum = 0.0;
    165     for (unsigned int n = 0; n < num_samples - delay; ++n) {
    166       squared_sum += (input_[n] - decoded_[channels_ * n + delay]) *
    167           (input_[n] - decoded_[channels_ * n + delay]);
    168     }
    169     return squared_sum / (num_samples - delay);
    170   }
    171 
    172   // Encodes a payload and decodes it twice with decoder re-init before each
    173   // decode. Verifies that the decoded result is the same.
    174   void ReInitTest() {
    175     uint8_t* encoded = encoded_;
    176     uint8_t* encoded_copy = encoded_ + 2 * frame_size_;
    177     int16_t* output1 = decoded_;
    178     int16_t* output2 = decoded_ + frame_size_;
    179     InitEncoder();
    180     size_t enc_len = EncodeFrame(input_, frame_size_, encoded);
    181     size_t dec_len;
    182     // Copy payload since iSAC fix destroys it during decode.
    183     // Issue: http://code.google.com/p/webrtc/issues/detail?id=845.
    184     // TODO(hlundin): Remove if the iSAC bug gets fixed.
    185     memcpy(encoded_copy, encoded, enc_len);
    186     AudioDecoder::SpeechType speech_type1, speech_type2;
    187     EXPECT_EQ(0, decoder_->Init());
    188     dec_len = decoder_->Decode(encoded, enc_len, output1, &speech_type1);
    189     EXPECT_EQ(frame_size_ * channels_, dec_len);
    190     // Re-init decoder and decode again.
    191     EXPECT_EQ(0, decoder_->Init());
    192     dec_len = decoder_->Decode(encoded_copy, enc_len, output2, &speech_type2);
    193     EXPECT_EQ(frame_size_ * channels_, dec_len);
    194     for (unsigned int n = 0; n < frame_size_; ++n) {
    195       ASSERT_EQ(output1[n], output2[n]) << "Exit test on first diff; n = " << n;
    196     }
    197     EXPECT_EQ(speech_type1, speech_type2);
    198   }
    199 
    200   // Call DecodePlc and verify that the correct number of samples is produced.
    201   void DecodePlcTest() {
    202     InitEncoder();
    203     size_t enc_len = EncodeFrame(input_, frame_size_, encoded_);
    204     AudioDecoder::SpeechType speech_type;
    205     EXPECT_EQ(0, decoder_->Init());
    206     size_t dec_len =
    207         decoder_->Decode(encoded_, enc_len, decoded_, &speech_type);
    208     EXPECT_EQ(frame_size_ * channels_, dec_len);
    209     // Call DecodePlc and verify that we get one frame of data.
    210     // (Overwrite the output from the above Decode call, but that does not
    211     // matter.)
    212     dec_len = decoder_->DecodePlc(1, decoded_);
    213     EXPECT_EQ(frame_size_ * channels_, dec_len);
    214   }
    215 
    216   std::string input_file_;
    217   FILE* input_fp_;
    218   int16_t* input_;
    219   uint8_t* encoded_;
    220   int16_t* decoded_;
    221   size_t frame_size_;
    222   size_t data_length_;
    223   size_t encoded_bytes_;
    224   size_t channels_;
    225   AudioDecoder* decoder_;
    226 };
    227 
    228 class AudioDecoderPcmUTest : public AudioDecoderTest {
    229  protected:
    230   AudioDecoderPcmUTest() : AudioDecoderTest() {
    231     frame_size_ = 160;
    232     data_length_ = 10 * frame_size_;
    233     decoder_ = new AudioDecoderPcmU;
    234     assert(decoder_);
    235   }
    236 
    237   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    238                           uint8_t* output) {
    239     int enc_len_bytes =
    240         WebRtcG711_EncodeU(NULL, const_cast<int16_t*>(input),
    241                            static_cast<int>(input_len_samples),
    242                            reinterpret_cast<int16_t*>(output));
    243     EXPECT_EQ(input_len_samples, static_cast<size_t>(enc_len_bytes));
    244     return enc_len_bytes;
    245   }
    246 };
    247 
    248 class AudioDecoderPcmATest : public AudioDecoderTest {
    249  protected:
    250   AudioDecoderPcmATest() : AudioDecoderTest() {
    251     frame_size_ = 160;
    252     data_length_ = 10 * frame_size_;
    253     decoder_ = new AudioDecoderPcmA;
    254     assert(decoder_);
    255   }
    256 
    257   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    258                           uint8_t* output) {
    259     int enc_len_bytes =
    260         WebRtcG711_EncodeA(NULL, const_cast<int16_t*>(input),
    261                            static_cast<int>(input_len_samples),
    262                            reinterpret_cast<int16_t*>(output));
    263     EXPECT_EQ(input_len_samples, static_cast<size_t>(enc_len_bytes));
    264     return enc_len_bytes;
    265   }
    266 };
    267 
    268 class AudioDecoderPcm16BTest : public AudioDecoderTest {
    269  protected:
    270   AudioDecoderPcm16BTest() : AudioDecoderTest() {
    271     frame_size_ = 160;
    272     data_length_ = 10 * frame_size_;
    273     decoder_ = new AudioDecoderPcm16B(kDecoderPCM16B);
    274     assert(decoder_);
    275   }
    276 
    277   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    278                           uint8_t* output) {
    279     int enc_len_bytes = WebRtcPcm16b_EncodeW16(
    280         const_cast<int16_t*>(input), static_cast<int>(input_len_samples),
    281         reinterpret_cast<int16_t*>(output));
    282     EXPECT_EQ(2 * input_len_samples, static_cast<size_t>(enc_len_bytes));
    283     return enc_len_bytes;
    284   }
    285 };
    286 
    287 class AudioDecoderIlbcTest : public AudioDecoderTest {
    288  protected:
    289   AudioDecoderIlbcTest() : AudioDecoderTest() {
    290     frame_size_ = 240;
    291     data_length_ = 10 * frame_size_;
    292     decoder_ = new AudioDecoderIlbc;
    293     assert(decoder_);
    294     WebRtcIlbcfix_EncoderCreate(&encoder_);
    295   }
    296 
    297   ~AudioDecoderIlbcTest() {
    298     WebRtcIlbcfix_EncoderFree(encoder_);
    299   }
    300 
    301   virtual void InitEncoder() {
    302     ASSERT_EQ(0, WebRtcIlbcfix_EncoderInit(encoder_, 30));  // 30 ms.
    303   }
    304 
    305   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    306                           uint8_t* output) {
    307     int enc_len_bytes =
    308         WebRtcIlbcfix_Encode(encoder_, input,
    309                              static_cast<int>(input_len_samples),
    310                              reinterpret_cast<int16_t*>(output));
    311     EXPECT_EQ(50, enc_len_bytes);
    312     return enc_len_bytes;
    313   }
    314 
    315   // Overload the default test since iLBC's function WebRtcIlbcfix_NetEqPlc does
    316   // not return any data. It simply resets a few states and returns 0.
    317   void DecodePlcTest() {
    318     InitEncoder();
    319     size_t enc_len = EncodeFrame(input_, frame_size_, encoded_);
    320     AudioDecoder::SpeechType speech_type;
    321     EXPECT_EQ(0, decoder_->Init());
    322     size_t dec_len =
    323         decoder_->Decode(encoded_, enc_len, decoded_, &speech_type);
    324     EXPECT_EQ(frame_size_, dec_len);
    325     // Simply call DecodePlc and verify that we get 0 as return value.
    326     EXPECT_EQ(0, decoder_->DecodePlc(1, decoded_));
    327   }
    328 
    329   iLBC_encinst_t* encoder_;
    330 };
    331 
    332 class AudioDecoderIsacFloatTest : public AudioDecoderTest {
    333  protected:
    334   AudioDecoderIsacFloatTest() : AudioDecoderTest() {
    335     input_size_ = 160;
    336     frame_size_ = 480;
    337     data_length_ = 10 * frame_size_;
    338     decoder_ = new AudioDecoderIsac;
    339     assert(decoder_);
    340     WebRtcIsac_Create(&encoder_);
    341     WebRtcIsac_SetEncSampRate(encoder_, 16000);
    342   }
    343 
    344   ~AudioDecoderIsacFloatTest() {
    345     WebRtcIsac_Free(encoder_);
    346   }
    347 
    348   virtual void InitEncoder() {
    349     ASSERT_EQ(0, WebRtcIsac_EncoderInit(encoder_, 1));  // Fixed mode.
    350     ASSERT_EQ(0, WebRtcIsac_Control(encoder_, 32000, 30));  // 32 kbps, 30 ms.
    351   }
    352 
    353   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    354                           uint8_t* output) {
    355     // Insert 3 * 10 ms. Expect non-zero output on third call.
    356     EXPECT_EQ(0, WebRtcIsac_Encode(encoder_, input,
    357                                    reinterpret_cast<int16_t*>(output)));
    358     input += input_size_;
    359     EXPECT_EQ(0, WebRtcIsac_Encode(encoder_, input,
    360                                    reinterpret_cast<int16_t*>(output)));
    361     input += input_size_;
    362     int enc_len_bytes =
    363         WebRtcIsac_Encode(encoder_, input, reinterpret_cast<int16_t*>(output));
    364     EXPECT_GT(enc_len_bytes, 0);
    365     return enc_len_bytes;
    366   }
    367 
    368   ISACStruct* encoder_;
    369   int input_size_;
    370 };
    371 
    372 class AudioDecoderIsacSwbTest : public AudioDecoderTest {
    373  protected:
    374   AudioDecoderIsacSwbTest() : AudioDecoderTest() {
    375     input_size_ = 320;
    376     frame_size_ = 960;
    377     data_length_ = 10 * frame_size_;
    378     decoder_ = new AudioDecoderIsacSwb;
    379     assert(decoder_);
    380     WebRtcIsac_Create(&encoder_);
    381     WebRtcIsac_SetEncSampRate(encoder_, 32000);
    382   }
    383 
    384   ~AudioDecoderIsacSwbTest() {
    385     WebRtcIsac_Free(encoder_);
    386   }
    387 
    388   virtual void InitEncoder() {
    389     ASSERT_EQ(0, WebRtcIsac_EncoderInit(encoder_, 1));  // Fixed mode.
    390     ASSERT_EQ(0, WebRtcIsac_Control(encoder_, 32000, 30));  // 32 kbps, 30 ms.
    391   }
    392 
    393   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    394                           uint8_t* output) {
    395     // Insert 3 * 10 ms. Expect non-zero output on third call.
    396     EXPECT_EQ(0, WebRtcIsac_Encode(encoder_, input,
    397                                    reinterpret_cast<int16_t*>(output)));
    398     input += input_size_;
    399     EXPECT_EQ(0, WebRtcIsac_Encode(encoder_, input,
    400                                    reinterpret_cast<int16_t*>(output)));
    401     input += input_size_;
    402     int enc_len_bytes =
    403         WebRtcIsac_Encode(encoder_, input, reinterpret_cast<int16_t*>(output));
    404     EXPECT_GT(enc_len_bytes, 0);
    405     return enc_len_bytes;
    406   }
    407 
    408   ISACStruct* encoder_;
    409   int input_size_;
    410 };
    411 
    412 // This test is identical to AudioDecoderIsacSwbTest, except that it creates
    413 // an AudioDecoderIsacFb decoder object.
    414 class AudioDecoderIsacFbTest : public AudioDecoderIsacSwbTest {
    415  protected:
    416   AudioDecoderIsacFbTest() : AudioDecoderIsacSwbTest() {
    417     // Delete the |decoder_| that was created by AudioDecoderIsacSwbTest and
    418     // create an AudioDecoderIsacFb object instead.
    419     delete decoder_;
    420     decoder_ = new AudioDecoderIsacFb;
    421     assert(decoder_);
    422   }
    423 };
    424 
    425 class AudioDecoderIsacFixTest : public AudioDecoderTest {
    426  protected:
    427   AudioDecoderIsacFixTest() : AudioDecoderTest() {
    428     input_size_ = 160;
    429     frame_size_ = 480;
    430     data_length_ = 10 * frame_size_;
    431     decoder_ = new AudioDecoderIsacFix;
    432     assert(decoder_);
    433     WebRtcIsacfix_Create(&encoder_);
    434   }
    435 
    436   ~AudioDecoderIsacFixTest() {
    437     WebRtcIsacfix_Free(encoder_);
    438   }
    439 
    440   virtual void InitEncoder() {
    441     ASSERT_EQ(0, WebRtcIsacfix_EncoderInit(encoder_, 1));  // Fixed mode.
    442     ASSERT_EQ(0,
    443               WebRtcIsacfix_Control(encoder_, 32000, 30));  // 32 kbps, 30 ms.
    444   }
    445 
    446   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    447                           uint8_t* output) {
    448     // Insert 3 * 10 ms. Expect non-zero output on third call.
    449     EXPECT_EQ(0, WebRtcIsacfix_Encode(encoder_, input,
    450                                       reinterpret_cast<int16_t*>(output)));
    451     input += input_size_;
    452     EXPECT_EQ(0, WebRtcIsacfix_Encode(encoder_, input,
    453                                       reinterpret_cast<int16_t*>(output)));
    454     input += input_size_;
    455     int enc_len_bytes = WebRtcIsacfix_Encode(
    456         encoder_, input, reinterpret_cast<int16_t*>(output));
    457     EXPECT_GT(enc_len_bytes, 0);
    458     return enc_len_bytes;
    459   }
    460 
    461   ISACFIX_MainStruct* encoder_;
    462   int input_size_;
    463 };
    464 
    465 class AudioDecoderG722Test : public AudioDecoderTest {
    466  protected:
    467   AudioDecoderG722Test() : AudioDecoderTest() {
    468     frame_size_ = 160;
    469     data_length_ = 10 * frame_size_;
    470     decoder_ = new AudioDecoderG722;
    471     assert(decoder_);
    472     WebRtcG722_CreateEncoder(&encoder_);
    473   }
    474 
    475   ~AudioDecoderG722Test() {
    476     WebRtcG722_FreeEncoder(encoder_);
    477   }
    478 
    479   virtual void InitEncoder() {
    480     ASSERT_EQ(0, WebRtcG722_EncoderInit(encoder_));
    481   }
    482 
    483   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    484                           uint8_t* output) {
    485     int enc_len_bytes =
    486         WebRtcG722_Encode(encoder_, const_cast<int16_t*>(input),
    487                           static_cast<int>(input_len_samples),
    488                           reinterpret_cast<int16_t*>(output));
    489     EXPECT_EQ(80, enc_len_bytes);
    490     return enc_len_bytes;
    491   }
    492 
    493   G722EncInst* encoder_;
    494 };
    495 
    496 class AudioDecoderG722StereoTest : public AudioDecoderG722Test {
    497  protected:
    498   AudioDecoderG722StereoTest() : AudioDecoderG722Test() {
    499     channels_ = 2;
    500     // Delete the |decoder_| that was created by AudioDecoderG722Test and
    501     // create an AudioDecoderG722Stereo object instead.
    502     delete decoder_;
    503     decoder_ = new AudioDecoderG722Stereo;
    504     assert(decoder_);
    505   }
    506 
    507   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    508                           uint8_t* output) {
    509     uint8_t* temp_output = new uint8_t[data_length_ * 2];
    510     // Encode a mono payload using the base test class.
    511     int mono_enc_len_bytes =
    512         AudioDecoderG722Test::EncodeFrame(input, input_len_samples,
    513                                           temp_output);
    514     // The bit-stream consists of 4-bit samples:
    515     // +--------+--------+--------+
    516     // | s0  s1 | s2  s3 | s4  s5 |
    517     // +--------+--------+--------+
    518     //
    519     // Duplicate them to the |output| such that the stereo stream becomes:
    520     // +--------+--------+--------+
    521     // | s0  s0 | s1  s1 | s2  s2 |
    522     // +--------+--------+--------+
    523     EXPECT_LE(mono_enc_len_bytes * 2, static_cast<int>(data_length_ * 2));
    524     uint8_t* output_ptr = output;
    525     for (int i = 0; i < mono_enc_len_bytes; ++i) {
    526       *output_ptr = (temp_output[i] & 0xF0) + (temp_output[i] >> 4);
    527       ++output_ptr;
    528       *output_ptr = (temp_output[i] << 4) + (temp_output[i] & 0x0F);
    529       ++output_ptr;
    530     }
    531     delete [] temp_output;
    532     return mono_enc_len_bytes * 2;
    533   }
    534 };
    535 
    536 #ifdef WEBRTC_CODEC_CELT
    537 class AudioDecoderCeltTest : public AudioDecoderTest {
    538  protected:
    539   static const int kEncodingRateBitsPerSecond = 64000;
    540   AudioDecoderCeltTest() : AudioDecoderTest(), encoder_(NULL) {
    541     frame_size_ = 640;
    542     data_length_ = 10 * frame_size_;
    543     decoder_ = AudioDecoder::CreateAudioDecoder(kDecoderCELT_32);
    544     assert(decoder_);
    545     WebRtcCelt_CreateEnc(&encoder_, static_cast<int>(channels_));
    546   }
    547 
    548   ~AudioDecoderCeltTest() {
    549     WebRtcCelt_FreeEnc(encoder_);
    550   }
    551 
    552   virtual void InitEncoder() {
    553     assert(encoder_);
    554     ASSERT_EQ(0, WebRtcCelt_EncoderInit(
    555         encoder_, static_cast<int>(channels_), kEncodingRateBitsPerSecond));
    556   }
    557 
    558   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    559                           uint8_t* output) {
    560     assert(encoder_);
    561     return WebRtcCelt_Encode(encoder_, input, output);
    562   }
    563 
    564   CELT_encinst_t* encoder_;
    565 };
    566 
    567 class AudioDecoderCeltStereoTest : public AudioDecoderTest {
    568  protected:
    569   static const int kEncodingRateBitsPerSecond = 64000;
    570   AudioDecoderCeltStereoTest() : AudioDecoderTest(), encoder_(NULL) {
    571     channels_ = 2;
    572     frame_size_ = 640;
    573     data_length_ = 10 * frame_size_;
    574     decoder_ = AudioDecoder::CreateAudioDecoder(kDecoderCELT_32_2ch);
    575     assert(decoder_);
    576     stereo_input_ = new int16_t[frame_size_ * channels_];
    577     WebRtcCelt_CreateEnc(&encoder_, static_cast<int>(channels_));
    578   }
    579 
    580   ~AudioDecoderCeltStereoTest() {
    581     delete [] stereo_input_;
    582     WebRtcCelt_FreeEnc(encoder_);
    583   }
    584 
    585   virtual void InitEncoder() {
    586     assert(encoder_);
    587     ASSERT_EQ(0, WebRtcCelt_EncoderInit(
    588         encoder_, static_cast<int>(channels_), kEncodingRateBitsPerSecond));
    589   }
    590 
    591   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    592                           uint8_t* output) {
    593     assert(encoder_);
    594     assert(stereo_input_);
    595     for (size_t n = 0; n < frame_size_; ++n) {
    596       stereo_input_[n * 2] = stereo_input_[n * 2 + 1] = input[n];
    597     }
    598     return WebRtcCelt_Encode(encoder_, stereo_input_, output);
    599   }
    600 
    601   int16_t* stereo_input_;
    602   CELT_encinst_t* encoder_;
    603 };
    604 
    605 #endif
    606 
    607 class AudioDecoderOpusTest : public AudioDecoderTest {
    608  protected:
    609   AudioDecoderOpusTest() : AudioDecoderTest() {
    610     frame_size_ = 320;
    611     data_length_ = 10 * frame_size_;
    612     decoder_ = new AudioDecoderOpus(kDecoderOpus);
    613     assert(decoder_);
    614     WebRtcOpus_EncoderCreate(&encoder_, 1);
    615   }
    616 
    617   ~AudioDecoderOpusTest() {
    618     WebRtcOpus_EncoderFree(encoder_);
    619   }
    620 
    621   virtual void InitEncoder() {}
    622 
    623   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    624                           uint8_t* output) {
    625     // Upsample from 32 to 48 kHz.
    626     Resampler rs;
    627     rs.Reset(32000, 48000, kResamplerSynchronous);
    628     const int max_resamp_len_samples = static_cast<int>(input_len_samples) *
    629         3 / 2;
    630     int16_t* resamp_input = new int16_t[max_resamp_len_samples];
    631     int resamp_len_samples;
    632     EXPECT_EQ(0, rs.Push(input, static_cast<int>(input_len_samples),
    633                          resamp_input, max_resamp_len_samples,
    634                          resamp_len_samples));
    635     EXPECT_EQ(max_resamp_len_samples, resamp_len_samples);
    636     int enc_len_bytes =
    637         WebRtcOpus_Encode(encoder_, resamp_input, resamp_len_samples,
    638                           static_cast<int>(data_length_), output);
    639     EXPECT_GT(enc_len_bytes, 0);
    640     delete [] resamp_input;
    641     return enc_len_bytes;
    642   }
    643 
    644   OpusEncInst* encoder_;
    645 };
    646 
    647 class AudioDecoderOpusStereoTest : public AudioDecoderTest {
    648  protected:
    649   AudioDecoderOpusStereoTest() : AudioDecoderTest() {
    650     channels_ = 2;
    651     frame_size_ = 320;
    652     data_length_ = 10 * frame_size_;
    653     decoder_ = new AudioDecoderOpus(kDecoderOpus_2ch);
    654     assert(decoder_);
    655     WebRtcOpus_EncoderCreate(&encoder_, 2);
    656   }
    657 
    658   ~AudioDecoderOpusStereoTest() {
    659     WebRtcOpus_EncoderFree(encoder_);
    660   }
    661 
    662   virtual void InitEncoder() {}
    663 
    664   virtual int EncodeFrame(const int16_t* input, size_t input_len_samples,
    665                           uint8_t* output) {
    666     // Create stereo by duplicating each sample in |input|.
    667     const int input_stereo_samples = static_cast<int>(input_len_samples) * 2;
    668     int16_t* input_stereo = new int16_t[input_stereo_samples];
    669     for (size_t i = 0; i < input_len_samples; i++)
    670       input_stereo[i * 2] = input_stereo[i * 2 + 1] = input[i];
    671     // Upsample from 32 to 48 kHz.
    672     Resampler rs;
    673     rs.Reset(32000, 48000, kResamplerSynchronousStereo);
    674     const int max_resamp_len_samples = input_stereo_samples * 3 / 2;
    675     int16_t* resamp_input = new int16_t[max_resamp_len_samples];
    676     int resamp_len_samples;
    677     EXPECT_EQ(0, rs.Push(input_stereo, input_stereo_samples, resamp_input,
    678                          max_resamp_len_samples, resamp_len_samples));
    679     EXPECT_EQ(max_resamp_len_samples, resamp_len_samples);
    680     int enc_len_bytes =
    681         WebRtcOpus_Encode(encoder_, resamp_input, resamp_len_samples / 2,
    682                           static_cast<int16_t>(data_length_), output);
    683     EXPECT_GT(enc_len_bytes, 0);
    684     delete [] resamp_input;
    685     delete [] input_stereo;
    686     return enc_len_bytes;
    687   }
    688 
    689   OpusEncInst* encoder_;
    690 };
    691 
    692 TEST_F(AudioDecoderPcmUTest, EncodeDecode) {
    693   int tolerance = 251;
    694   double mse = 1734.0;
    695   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCMu));
    696   EncodeDecodeTest(data_length_, tolerance, mse);
    697   ReInitTest();
    698   EXPECT_FALSE(decoder_->HasDecodePlc());
    699 }
    700 
    701 TEST_F(AudioDecoderPcmATest, EncodeDecode) {
    702   int tolerance = 308;
    703   double mse = 1931.0;
    704   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCMa));
    705   EncodeDecodeTest(data_length_, tolerance, mse);
    706   ReInitTest();
    707   EXPECT_FALSE(decoder_->HasDecodePlc());
    708 }
    709 
    710 TEST_F(AudioDecoderPcm16BTest, EncodeDecode) {
    711   int tolerance = 0;
    712   double mse = 0.0;
    713   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16B));
    714   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bwb));
    715   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bswb32kHz));
    716   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bswb48kHz));
    717   EncodeDecodeTest(2 * data_length_, tolerance, mse);
    718   ReInitTest();
    719   EXPECT_FALSE(decoder_->HasDecodePlc());
    720 }
    721 
    722 TEST_F(AudioDecoderIlbcTest, EncodeDecode) {
    723   int tolerance = 6808;
    724   double mse = 2.13e6;
    725   int delay = 80;  // Delay from input to output.
    726   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderILBC));
    727   EncodeDecodeTest(500, tolerance, mse, delay);
    728   ReInitTest();
    729   EXPECT_TRUE(decoder_->HasDecodePlc());
    730   DecodePlcTest();
    731 }
    732 
    733 TEST_F(AudioDecoderIsacFloatTest, EncodeDecode) {
    734   int tolerance = 3399;
    735   double mse = 434951.0;
    736   int delay = 48;  // Delay from input to output.
    737   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISAC));
    738   EncodeDecodeTest(0, tolerance, mse, delay);
    739   ReInitTest();
    740   EXPECT_TRUE(decoder_->HasDecodePlc());
    741   DecodePlcTest();
    742 }
    743 
    744 TEST_F(AudioDecoderIsacSwbTest, EncodeDecode) {
    745   int tolerance = 19757;
    746   double mse = 8.18e6;
    747   int delay = 160;  // Delay from input to output.
    748   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISACswb));
    749   EncodeDecodeTest(0, tolerance, mse, delay);
    750   ReInitTest();
    751   EXPECT_TRUE(decoder_->HasDecodePlc());
    752   DecodePlcTest();
    753 }
    754 
    755 TEST_F(AudioDecoderIsacFbTest, EncodeDecode) {
    756   int tolerance = 19757;
    757   double mse = 8.18e6;
    758   int delay = 160;  // Delay from input to output.
    759   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISACswb));
    760   EncodeDecodeTest(0, tolerance, mse, delay);
    761   ReInitTest();
    762   EXPECT_TRUE(decoder_->HasDecodePlc());
    763   DecodePlcTest();
    764 }
    765 
    766 TEST_F(AudioDecoderIsacFixTest, DISABLED_EncodeDecode) {
    767   int tolerance = 11034;
    768   double mse = 3.46e6;
    769   int delay = 54;  // Delay from input to output.
    770   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISAC));
    771   EncodeDecodeTest(735, tolerance, mse, delay);
    772   ReInitTest();
    773   EXPECT_FALSE(decoder_->HasDecodePlc());
    774 }
    775 
    776 TEST_F(AudioDecoderG722Test, EncodeDecode) {
    777   int tolerance = 6176;
    778   double mse = 238630.0;
    779   int delay = 22;  // Delay from input to output.
    780   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderG722));
    781   EncodeDecodeTest(data_length_ / 2, tolerance, mse, delay);
    782   ReInitTest();
    783   EXPECT_FALSE(decoder_->HasDecodePlc());
    784 }
    785 
    786 TEST_F(AudioDecoderG722StereoTest, CreateAndDestroy) {
    787   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderG722_2ch));
    788 }
    789 
    790 TEST_F(AudioDecoderG722StereoTest, EncodeDecode) {
    791   int tolerance = 6176;
    792   int channel_diff_tolerance = 0;
    793   double mse = 238630.0;
    794   int delay = 22;  // Delay from input to output.
    795   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderG722_2ch));
    796   EncodeDecodeTest(data_length_, tolerance, mse, delay, channel_diff_tolerance);
    797   ReInitTest();
    798   EXPECT_FALSE(decoder_->HasDecodePlc());
    799 }
    800 
    801 TEST_F(AudioDecoderOpusTest, EncodeDecode) {
    802   int tolerance = 6176;
    803   double mse = 238630.0;
    804   int delay = 22;  // Delay from input to output.
    805   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderOpus));
    806   EncodeDecodeTest(0, tolerance, mse, delay);
    807   ReInitTest();
    808   EXPECT_FALSE(decoder_->HasDecodePlc());
    809 }
    810 
    811 TEST_F(AudioDecoderOpusStereoTest, EncodeDecode) {
    812   int tolerance = 6176;
    813   int channel_diff_tolerance = 0;
    814   double mse = 238630.0;
    815   int delay = 22;  // Delay from input to output.
    816   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderOpus_2ch));
    817   EncodeDecodeTest(0, tolerance, mse, delay, channel_diff_tolerance);
    818   ReInitTest();
    819   EXPECT_FALSE(decoder_->HasDecodePlc());
    820 }
    821 
    822 #ifdef WEBRTC_CODEC_CELT
    823 // In the two following CELT tests, the low amplitude of the test signal allow
    824 // us to have such low error thresholds, i.e. |tolerance|, |mse|. Furthermore,
    825 // in general, stereo signals with identical channels do not result in identical
    826 // encoded channels.
    827 TEST_F(AudioDecoderCeltTest, EncodeDecode) {
    828   int tolerance = 20;
    829   double mse = 17.0;
    830   int delay = 80;  // Delay from input to output in samples.
    831   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCELT_32));
    832   EncodeDecodeTest(1600, tolerance, mse, delay);
    833   ReInitTest();
    834   EXPECT_TRUE(decoder_->HasDecodePlc());
    835   DecodePlcTest();
    836 }
    837 
    838 TEST_F(AudioDecoderCeltStereoTest, EncodeDecode) {
    839   int tolerance = 20;
    840   // If both channels are identical, CELT not necessarily decodes identical
    841   // channels. However, for this input this is the case.
    842   int channel_diff_tolerance = 0;
    843   double mse = 20.0;
    844   // Delay from input to output in samples, accounting for stereo.
    845   int delay = 160;
    846   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCELT_32_2ch));
    847   EncodeDecodeTest(1600, tolerance, mse, delay, channel_diff_tolerance);
    848   ReInitTest();
    849   EXPECT_TRUE(decoder_->HasDecodePlc());
    850   DecodePlcTest();
    851 }
    852 #endif
    853 
    854 TEST(AudioDecoder, CodecSampleRateHz) {
    855   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCMu));
    856   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCMa));
    857   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCMu_2ch));
    858   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCMa_2ch));
    859   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderILBC));
    860   EXPECT_EQ(16000, AudioDecoder::CodecSampleRateHz(kDecoderISAC));
    861   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderISACswb));
    862   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderISACfb));
    863   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16B));
    864   EXPECT_EQ(16000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16Bwb));
    865   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16Bswb32kHz));
    866   EXPECT_EQ(48000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16Bswb48kHz));
    867   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16B_2ch));
    868   EXPECT_EQ(16000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16Bwb_2ch));
    869   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16Bswb32kHz_2ch));
    870   EXPECT_EQ(48000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16Bswb48kHz_2ch));
    871   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderPCM16B_5ch));
    872   EXPECT_EQ(16000, AudioDecoder::CodecSampleRateHz(kDecoderG722));
    873   EXPECT_EQ(16000, AudioDecoder::CodecSampleRateHz(kDecoderG722_2ch));
    874   EXPECT_EQ(-1, AudioDecoder::CodecSampleRateHz(kDecoderRED));
    875   EXPECT_EQ(-1, AudioDecoder::CodecSampleRateHz(kDecoderAVT));
    876   EXPECT_EQ(8000, AudioDecoder::CodecSampleRateHz(kDecoderCNGnb));
    877   EXPECT_EQ(16000, AudioDecoder::CodecSampleRateHz(kDecoderCNGwb));
    878   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderCNGswb32kHz));
    879   // TODO(tlegrand): Change 32000 to 48000 below once ACM has 48 kHz support.
    880   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderCNGswb48kHz));
    881   EXPECT_EQ(-1, AudioDecoder::CodecSampleRateHz(kDecoderArbitrary));
    882   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderOpus));
    883   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderOpus_2ch));
    884 #ifdef WEBRTC_CODEC_CELT
    885   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderCELT_32));
    886   EXPECT_EQ(32000, AudioDecoder::CodecSampleRateHz(kDecoderCELT_32_2ch));
    887 #else
    888   EXPECT_EQ(-1, AudioDecoder::CodecSampleRateHz(kDecoderCELT_32));
    889   EXPECT_EQ(-1, AudioDecoder::CodecSampleRateHz(kDecoderCELT_32_2ch));
    890 #endif
    891 }
    892 
    893 TEST(AudioDecoder, CodecSupported) {
    894   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCMu));
    895   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCMa));
    896   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCMu_2ch));
    897   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCMa_2ch));
    898   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderILBC));
    899   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISAC));
    900   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISACswb));
    901   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderISACfb));
    902   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16B));
    903   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bwb));
    904   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bswb32kHz));
    905   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bswb48kHz));
    906   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16B_2ch));
    907   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bwb_2ch));
    908   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bswb32kHz_2ch));
    909   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16Bswb48kHz_2ch));
    910   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderPCM16B_5ch));
    911   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderG722));
    912   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderG722_2ch));
    913   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderRED));
    914   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderAVT));
    915   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCNGnb));
    916   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCNGwb));
    917   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCNGswb32kHz));
    918   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCNGswb48kHz));
    919   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderArbitrary));
    920   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderOpus));
    921   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderOpus_2ch));
    922 #ifdef WEBRTC_CODEC_CELT
    923   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCELT_32));
    924   EXPECT_TRUE(AudioDecoder::CodecSupported(kDecoderCELT_32_2ch));
    925 #else
    926   EXPECT_FALSE(AudioDecoder::CodecSupported(kDecoderCELT_32));
    927   EXPECT_FALSE(AudioDecoder::CodecSupported(kDecoderCELT_32_2ch));
    928 #endif
    929 }
    930 
    931 }  // namespace webrtc
    932