• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 3-Clause Clear
5  * License and the Alliance for Open Media Patent License 1.0. If the BSD
6  * 3-Clause Clear License was not distributed with this source code in the
7  * LICENSE file, you can obtain it at
8  * www.aomedia.org/license/software-license/bsd-3-c-c. If the Alliance for
9  * Open Media Patent License 1.0 was not distributed with this source code
10  * in the PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11  */
12 
13 #include "iamf/cli/recon_gain_generator.h"
14 
15 #include <cstdint>
16 #include <vector>
17 
18 #include "absl/status/status_matchers.h"
19 #include "absl/types/span.h"
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "iamf/cli/channel_label.h"
23 #include "iamf/cli/demixing_module.h"
24 #include "iamf/cli/proto/user_metadata.pb.h"
25 #include "iamf/cli/tests/cli_test_utils.h"
26 #include "iamf/obu/types.h"
27 
28 namespace iamf_tools {
29 namespace {
30 
31 using ::absl_testing::IsOk;
32 
33 using enum ChannelLabel::Label;
34 
35 constexpr InternalSampleType kArbitrarySample = 1.0;
36 
TestComputeReconGainForOneChannelLrs7(absl::Span<const int32_t> original_channel_int,absl::Span<const int32_t> mixed_channel_int,absl::Span<const int32_t> demixed_channel_int,const double expected_recon_gain)37 void TestComputeReconGainForOneChannelLrs7(
38     absl::Span<const int32_t> original_channel_int,
39     absl::Span<const int32_t> mixed_channel_int,
40     absl::Span<const int32_t> demixed_channel_int,
41     const double expected_recon_gain) {
42   const LabelSamplesMap label_to_samples{
43       {kDemixedLrs7, Int32ToInternalSampleType(original_channel_int)},
44       {kLs5, Int32ToInternalSampleType(mixed_channel_int)}};
45   const LabelSamplesMap label_to_decoded_samples{
46       {kDemixedLrs7, Int32ToInternalSampleType(demixed_channel_int)}};
47 
48   double recon_gain;
49   EXPECT_THAT(ReconGainGenerator::ComputeReconGain(
50                   kDemixedLrs7, label_to_samples, label_to_decoded_samples,
51                   /*additional_logging=*/true, recon_gain),
52               IsOk());
53   EXPECT_NEAR(recon_gain, expected_recon_gain, 0.0001);
54 }
55 
TEST(ComputeReconGain,LessThanFirstThreshold)56 TEST(ComputeReconGain, LessThanFirstThreshold) {
57   // 10 * log_10(Ok / 32767^2) ~= -80.30 dB. Since this is < -80 dB the
58   // recon gain must be set to 0.0.
59   TestComputeReconGainForOneChannelLrs7({10}, {10}, {10}, 0.0);
60 }
61 
TEST(ComputeReconGain,GreaterThanSecondThreshold)62 TEST(ComputeReconGain, GreaterThanSecondThreshold) {
63   // 10 * log_10(Ok/Mk) ~= -4.77 dB. Since this is >= -6 dB the recon gain
64   // must be set to 1.0.
65   TestComputeReconGainForOneChannelLrs7({20 << 16}, {60 << 16}, {60 << 16},
66                                         1.0);
67 }
68 
TEST(ComputeReconGain,LessThanSecondThreshold)69 TEST(ComputeReconGain, LessThanSecondThreshold) {
70   // 10 * log_10(Ok/Mk) ~= -6.99 dB. Since this is < -6 dB the recon gain is
71   // set to the value which makes Ok = (Recon_Gain(k,1))^2 * Dk.
72   TestComputeReconGainForOneChannelLrs7({12 << 16}, {60 << 16}, {60 << 16},
73                                         0.4472);
74 }
75 
TEST(ComputeReconGain,SucceedsForTwoLayerStereo)76 TEST(ComputeReconGain, SucceedsForTwoLayerStereo) {
77   const std::vector<InternalSampleType> kOriginalChannel{kArbitrarySample};
78   const std::vector<InternalSampleType> kMixedChannel{kArbitrarySample};
79   const std::vector<InternalSampleType> kDemixedChannel{kArbitrarySample};
80   const LabelSamplesMap label_to_samples{{kDemixedR2, kOriginalChannel},
81                                          {kMono, kMixedChannel}};
82   const LabelSamplesMap label_to_decoded_samples{{kDemixedR2, kDemixedChannel}};
83 
84   double recon_gain;
85   EXPECT_THAT(ReconGainGenerator::ComputeReconGain(
86                   kDemixedR2, label_to_samples, label_to_decoded_samples,
87                   /*additional_logging=*/true, recon_gain),
88               IsOk());
89 }
90 
TEST(ComputeReconGain,InvalidWhenRelevantMixedSampleCannotBeFound)91 TEST(ComputeReconGain, InvalidWhenRelevantMixedSampleCannotBeFound) {
92   const std::vector<InternalSampleType> kOriginalChannel{kArbitrarySample};
93   const std::vector<InternalSampleType> kDemixedChannel{kArbitrarySample};
94   const LabelSamplesMap label_to_samples{{kDemixedR2, kOriginalChannel}};
95   const LabelSamplesMap label_to_decoded_samples{{kDemixedR2, kDemixedChannel}};
96 
97   double recon_gain;
98   EXPECT_FALSE(ReconGainGenerator::ComputeReconGain(
99                    kDemixedR2, label_to_samples, label_to_decoded_samples,
100                    /*additional_logging=*/true, recon_gain)
101                    .ok());
102 }
103 
104 }  // namespace
105 }  // namespace iamf_tools
106