neteq_isac_quality_test.cc revision dce40cf804019a9898b6ab8d8262466b697c56e0
16568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org/*
26568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
36568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *
46568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *  Use of this source code is governed by a BSD-style license
56568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *  that can be found in the LICENSE file in the root of the source
66568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *  tree. An additional intellectual property rights grant can be found
76568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *  in the file PATENTS.  All contributing project authors may
86568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
96568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org */
106568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
116568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org#include "webrtc/modules/audio_coding/codecs/isac/fix/interface/isacfix.h"
126568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org#include "webrtc/modules/audio_coding/neteq/tools/neteq_quality_test.h"
136568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
146568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgusing google::RegisterFlagValidator;
156568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgusing google::ParseCommandLineFlags;
166568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgusing std::string;
176568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgusing testing::InitGoogleTest;
186568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
196568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgnamespace webrtc {
206568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgnamespace test {
2183b5c053b9687813c5fc9c08b3beee4d464f7950Henrik Lundinnamespace {
226568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgstatic const int kIsacBlockDurationMs = 30;
236568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgstatic const int kIsacInputSamplingKhz = 16;
246568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgstatic const int kIsacOutputSamplingKhz = 16;
256568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
2683b5c053b9687813c5fc9c08b3beee4d464f7950Henrik Lundin// Define switch for bit rate.
276568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgstatic bool ValidateBitRate(const char* flagname, int32_t value) {
286568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  if (value >= 10 && value <= 32)
296568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org    return true;
306568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  printf("Invalid bit rate, should be between 10 and 32 kbps.");
316568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  return false;
326568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}
336568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
346568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgDEFINE_int32(bit_rate_kbps, 32, "Target bit rate (kbps).");
356568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
366568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgstatic const bool bit_rate_dummy =
376568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org    RegisterFlagValidator(&FLAGS_bit_rate_kbps, &ValidateBitRate);
386568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
3983b5c053b9687813c5fc9c08b3beee4d464f7950Henrik Lundin}  // namespace
4083b5c053b9687813c5fc9c08b3beee4d464f7950Henrik Lundin
416568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgclass NetEqIsacQualityTest : public NetEqQualityTest {
426568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org protected:
436568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  NetEqIsacQualityTest();
4414665ff7d4024d07e58622f498b23fd980001871kjellander@webrtc.org  void SetUp() override;
4514665ff7d4024d07e58622f498b23fd980001871kjellander@webrtc.org  void TearDown() override;
46dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting  virtual int EncodeBlock(int16_t* in_data, size_t block_size_samples,
47dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting                          uint8_t* payload, size_t max_bytes);
486568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org private:
496568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  ISACFIX_MainStruct* isac_encoder_;
506568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  int bit_rate_kbps_;
516568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org};
526568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
536568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgNetEqIsacQualityTest::NetEqIsacQualityTest()
5483b5c053b9687813c5fc9c08b3beee4d464f7950Henrik Lundin    : NetEqQualityTest(kIsacBlockDurationMs,
5583b5c053b9687813c5fc9c08b3beee4d464f7950Henrik Lundin                       kIsacInputSamplingKhz,
566568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org                       kIsacOutputSamplingKhz,
57f761d10393ae47283c6170387fcb8cce4aadbd59Minyue Li                       kDecoderISAC),
586568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org      isac_encoder_(NULL),
596568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org      bit_rate_kbps_(FLAGS_bit_rate_kbps) {
606568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}
616568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
626568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgvoid NetEqIsacQualityTest::SetUp() {
63f761d10393ae47283c6170387fcb8cce4aadbd59Minyue Li  ASSERT_EQ(1, channels_) << "iSAC supports only mono audio.";
646568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  // Create encoder memory.
656568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  WebRtcIsacfix_Create(&isac_encoder_);
666568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  ASSERT_TRUE(isac_encoder_ != NULL);
676568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  EXPECT_EQ(0, WebRtcIsacfix_EncoderInit(isac_encoder_, 1));
686568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  // Set bitrate and block length.
696568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  EXPECT_EQ(0, WebRtcIsacfix_Control(isac_encoder_, bit_rate_kbps_ * 1000,
706568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org                                     kIsacBlockDurationMs));
716568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  NetEqQualityTest::SetUp();
726568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}
736568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
746568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgvoid NetEqIsacQualityTest::TearDown() {
756568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  // Free memory.
766568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  EXPECT_EQ(0, WebRtcIsacfix_Free(isac_encoder_));
776568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  NetEqQualityTest::TearDown();
786568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}
796568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
806568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgint NetEqIsacQualityTest::EncodeBlock(int16_t* in_data,
81dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting                                      size_t block_size_samples,
82dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting                                      uint8_t* payload, size_t max_bytes) {
836568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  // ISAC takes 10 ms for every call.
846568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  const int subblocks = kIsacBlockDurationMs / 10;
856568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  const int subblock_length = 10 * kIsacInputSamplingKhz;
866568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  int value = 0;
876568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
886568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  int pointer = 0;
896568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  for (int idx = 0; idx < subblocks; idx++, pointer += subblock_length) {
906568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org    // The Isac encoder does not perform encoding (and returns 0) until it
916568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org    // receives a sequence of sub-blocks that amount to the frame duration.
926568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org    EXPECT_EQ(0, value);
937ee24a79065a655dcc62a27fd22e0cc77fee6d68kwiberg@webrtc.org    value = WebRtcIsacfix_Encode(isac_encoder_, &in_data[pointer], payload);
946568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  }
956568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  EXPECT_GT(value, 0);
966568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org  return value;
976568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}
986568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
996568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.orgTEST_F(NetEqIsacQualityTest, Test) {
100e5ff00a1c65750e7d30a25c4f1da91f0c035d07fHenrik Lundin  Simulate();
1016568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}
1026568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org
1036568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}  // namespace test
1046568e97d108dffbf10af04a764287f3d1589691fminyue@webrtc.org}  // namespace webrtc
105