Skip to content

Commit

Permalink
Create an abstract LoudnessCalculatorFactory.
Browse files Browse the repository at this point in the history
  - Implement a simple version which just copies user loudness.
  - In the future this will be used to configure a module which actually measures loudness for a Mix Presentation OBU.

PiperOrigin-RevId: 628083616
  • Loading branch information
jwcullen committed Apr 25, 2024
1 parent cb4cff4 commit 3f61caa
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 1 deletion.
10 changes: 10 additions & 0 deletions iamf/cli/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ cc_library(
],
)

cc_library(
name = "loudness_calculator_factory",
srcs = ["loudness_calculator_factory.cc"],
hdrs = ["loudness_calculator_factory.h"],
deps = [
":loudness_calculator",
"//iamf/obu:mix_presentation",
],
)

cc_library(
name = "mix_presentation_finalizer",
srcs = ["mix_presentation_finalizer.cc"],
Expand Down
3 changes: 2 additions & 1 deletion iamf/cli/loudness_calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class LoudnessCalculatorUserProvidedLoudness : public LoudnessCalculatorBase {
*
* \param user_provided_loudness User provided loudness to echo back.
*/
LoudnessCalculatorUserProvidedLoudness(LoudnessInfo user_provided_loudness)
LoudnessCalculatorUserProvidedLoudness(
const LoudnessInfo& user_provided_loudness)
: user_provided_loudness_(user_provided_loudness) {}

/*\!brief Destructor. */
Expand Down
31 changes: 31 additions & 0 deletions iamf/cli/loudness_calculator_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/
#include "iamf/cli/loudness_calculator_factory.h"

#include <cstdint>
#include <memory>

#include "iamf/cli/loudness_calculator.h"
#include "iamf/obu/mix_presentation.h"

namespace iamf_tools {

LoudnessCalculatorFactoryBase::~LoudnessCalculatorFactoryBase() {}

std::unique_ptr<LoudnessCalculatorBase>
LoudnessCalculatorFactoryUserProvidedLoudness::CreateLoudnessCalculator(
const MixPresentationLayout& layout, int32_t, int32_t) const {
return std::make_unique<LoudnessCalculatorUserProvidedLoudness>(
layout.loudness);
}

} // namespace iamf_tools
72 changes: 72 additions & 0 deletions iamf/cli/loudness_calculator_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/
#ifndef CLI_LOUDNESS_CALCULATOR_FACTORY_H_
#define CLI_LOUDNESS_CALCULATOR_FACTORY_H_

#include <cstdint>
#include <memory>

#include "iamf/cli/loudness_calculator.h"
#include "iamf/obu/mix_presentation.h"

namespace iamf_tools {

/*\!brief Abstract class to create loudness calculators.
*
* This class will be used when calculating the loudness of a mix presentation
* layout. The mix presentation finalizer will take in a factory (or factories)
* and use them to create a loudness calculator for each stream. By taking in a
* factory the finalizer can be agnostic to the type of loudness calculator
* being used which may depend on implementation details, or it may depend on
* the specific layouts.
*/
class LoudnessCalculatorFactoryBase {
public:
/*\!brief Creates a loudness calculator.
*
* \param layout Layout to measure loudness on.
* \param rendered_sample_rate Sample rate of the rendered audio.
* \param rendered_bit_depth Bit-depth of the rendered audio.
* \return Unique pointer to a loudness calculator.
*/
virtual std::unique_ptr<LoudnessCalculatorBase> CreateLoudnessCalculator(
const MixPresentationLayout& layout, int32_t rendered_sample_rate,
int32_t rendered_bit_depth) const = 0;

/*\!brief Destructor. */
virtual ~LoudnessCalculatorFactoryBase() = 0;
};

// TODO(b/302273947): Use this class to measure loudness when finalizing mix
// presentations.
/*\!brief Factory which always provides a fallback loudness calculator. */
class LoudnessCalculatorFactoryUserProvidedLoudness
: public LoudnessCalculatorFactoryBase {
public:
/*\!brief Creates a fallback loudness calculator.
*
* \param layout Layout to use when echoing loudness back.
* \param rendered_sample_rate Sample rate of the rendered audio to ignore.
* \param rendered_bit_depth Bit-depth of the rendered audio to ignore.
* \return Unique pointer to a loudness calculator.
*/
std::unique_ptr<LoudnessCalculatorBase> CreateLoudnessCalculator(
const MixPresentationLayout& layout, int32_t /*rendered_sample_rate*/,
int32_t /*rendered_bit_depth*/) const override;

/*\!brief Destructor. */
~LoudnessCalculatorFactoryUserProvidedLoudness() override = default;
};

} // namespace iamf_tools

#endif // CLI_LOUDNESS_CALCULATOR_H_
2 changes: 2 additions & 0 deletions iamf/cli/mix_presentation_finalizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ MeasureLoudnessOrFallbackToUserLoudnessMixPresentationFinalizer::Finalize(
<< "provided values.";

// TODO(b/332567539): Use `RendererFactory` to render certain layouts.
// TODO(b/302273947): Once layouts are rendered and mixed then use a
// `LoudnessCalculatorFactory` to measure loudness.
int metadata_index = 0;
for (auto& mix_presentation_obu : mix_presentation_obus) {
for (int sub_mix_index = 0;
Expand Down
14 changes: 14 additions & 0 deletions iamf/cli/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ cc_test(
],
)

cc_test(
name = "loudness_calculator_factory_test",
srcs = ["loudness_calculator_factory_test.cc"],
deps = [
"//iamf/cli:loudness_calculator_factory",
"//iamf/cli/proto:obu_header_cc_proto",
"//iamf/cli/proto:parameter_data_cc_proto",
"//iamf/cli/proto:temporal_delimiter_cc_proto",
"//iamf/cli/proto:user_metadata_cc_proto",
"//iamf/obu:mix_presentation",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "loudness_calculator_test",
srcs = ["loudness_calculator_test.cc"],
Expand Down
39 changes: 39 additions & 0 deletions iamf/cli/tests/loudness_calculator_factory_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, Alliance for Open Media. All rights reserved
*
* This source code is subject to the terms of the BSD 3-Clause Clear License
* and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
* License was not distributed with this source code in the LICENSE file, you
* can obtain it at www.aomedia.org/license/software-license/bsd-3-c-c. If the
* Alliance for Open Media Patent License 1.0 was not distributed with this
* source code in the PATENTS file, you can obtain it at
* www.aomedia.org/license/patent.
*/
#include "iamf/cli/loudness_calculator_factory.h"

#include <cstdint>

#include "gtest/gtest.h"
#include "iamf/cli/proto/obu_header.pb.h"
#include "iamf/cli/proto/parameter_data.pb.h"
#include "iamf/cli/proto/temporal_delimiter.pb.h"
#include "iamf/cli/proto/user_metadata.pb.h"
#include "iamf/obu/mix_presentation.h"

namespace iamf_tools {
namespace {

constexpr int32_t kUnusedRenderedSampleRate = 0;
constexpr int32_t kUnusedRenderedBitDepth = 0;

TEST(CreatePrimaryLoudnessCalculator, NeverReturnsNull) {
const LoudnessCalculatorFactoryUserProvidedLoudness factory;
const MixPresentationLayout layout = {};

EXPECT_NE(factory.CreateLoudnessCalculator(layout, kUnusedRenderedSampleRate,
kUnusedRenderedBitDepth),
nullptr);
}

} // namespace
} // namespace iamf_tools

0 comments on commit 3f61caa

Please sign in to comment.