Skip to content

Commit

Permalink
Fills out LpcmDecoder::Initialize and tests it
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 632556010
  • Loading branch information
trevorknight authored and jwcullen committed May 14, 2024
1 parent af7c4ba commit 65507c2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
4 changes: 3 additions & 1 deletion iamf/cli/codec/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ cc_library(
hdrs = ["lpcm_decoder.h"],
deps = [
":decoder_base",
"//iamf/common:macros",
"//iamf/obu:codec_config",
"//iamf/obu/decoder_config:lpcm_decoder_config",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
],
)

Expand Down
9 changes: 6 additions & 3 deletions iamf/cli/codec/lpcm_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "absl/status/status.h"
#include "iamf/cli/codec/decoder_base.h"
#include "iamf/common/macros.h"
#include "iamf/obu/codec_config.h"
#include "iamf/obu/decoder_config/lpcm_decoder_config.h"

Expand All @@ -27,11 +28,13 @@ LpcmDecoder::LpcmDecoder(const CodecConfigObu& codec_config_obu,
: DecoderBase(num_channels,
static_cast<int>(codec_config_obu.GetNumSamplesPerFrame())),
decoder_config_(std::get<LpcmDecoderConfig>(
codec_config_obu.GetCodecConfig().decoder_config)) {}
codec_config_obu.GetCodecConfig().decoder_config)),
audio_roll_distance_(
codec_config_obu.GetCodecConfig().audio_roll_distance) {}

absl::Status LpcmDecoder::Initialize() {
return absl::UnimplementedError(
"LPCMDecoder::Initialize() is not implemented.");
RETURN_IF_NOT_OK(decoder_config_.Validate(audio_roll_distance_));
return absl::OkStatus();
}

absl::Status LpcmDecoder::DecodeAudioFrame(
Expand Down
6 changes: 5 additions & 1 deletion iamf/cli/codec/lpcm_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef CLI_CODEC_LPCM_DECODER_H_
#define CLI_CODEC_LPCM_DECODER_H_

#include <cstddef>
#include <cstdint>
#include <vector>

Expand Down Expand Up @@ -40,14 +41,17 @@ class LpcmDecoder : public DecoderBase {

~LpcmDecoder() override = default;

private:
absl::Status Initialize() override;

absl::Status DecodeAudioFrame(
const std::vector<uint8_t>& encoded_frame,
std::vector<std::vector<int32_t>>& decoded_frames) override;

private:
const LpcmDecoderConfig decoder_config_;
// We don't need the audio_roll_distance_ for decoding, but needed to validate
// the LpcmDecoderConfig.
int16_t audio_roll_distance_;
};

} // namespace iamf_tools
Expand Down
35 changes: 27 additions & 8 deletions iamf/cli/codec/tests/lpcm_decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
namespace iamf_tools {
namespace {

CodecConfigObu CreateCodecConfigObu(uint32_t num_samples_per_frame) {
LpcmDecoderConfig lpcm_decoder_config;
lpcm_decoder_config.sample_size_ = 16;
lpcm_decoder_config.sample_rate_ = 48000;

CodecConfigObu CreateCodecConfigObu(LpcmDecoderConfig lpcm_decoder_config,
uint32_t num_samples_per_frame = 1024) {
const CodecConfig codec_config = {
.codec_id = CodecConfig::kCodecIdLpcm,
.num_samples_per_frame = num_samples_per_frame,
Expand All @@ -26,13 +23,35 @@ CodecConfigObu CreateCodecConfigObu(uint32_t num_samples_per_frame) {
};

TEST(LpcmDecoderTest, Construct) {
uint32_t num_samples_per_frame = 1024;
CodecConfigObu codec_config_obu = CreateCodecConfigObu(num_samples_per_frame);
LpcmDecoderConfig lpcm_decoder_config;
lpcm_decoder_config.sample_rate_ = 48000;
lpcm_decoder_config.sample_size_ = 16;
lpcm_decoder_config.sample_format_flags_bitmask_ =
LpcmDecoderConfig::LpcmFormatFlagsBitmask::kLpcmLittleEndian;
CodecConfigObu codec_config_obu = CreateCodecConfigObu(lpcm_decoder_config);
ASSERT_TRUE(codec_config_obu.Initialize().ok());
int number_of_channels = 11; // Arbitrary.

int number_of_channels = 11;
LpcmDecoder lpcm_decoder(codec_config_obu, number_of_channels);
}

TEST(LpcmDecoderTest, Initialize_InvalidConfigFails) {
LpcmDecoderConfig lpcm_decoder_config;
// The sample rate and bit depth are validated with CodecConfigObu::Initialize
// so if we want to test the validation in LpcmDecoderConfig::Initialize we
// will give an invalid sample_format_flags_bitmask_.
lpcm_decoder_config.sample_rate_ = 48000;
lpcm_decoder_config.sample_size_ = 16;
lpcm_decoder_config.sample_format_flags_bitmask_ =
LpcmDecoderConfig::LpcmFormatFlagsBitmask::kLpcmBeginReserved;
CodecConfigObu codec_config_obu = CreateCodecConfigObu(lpcm_decoder_config);
ASSERT_TRUE(codec_config_obu.Initialize().ok());
int number_of_channels = 11; // Arbitrary.

LpcmDecoder lpcm_decoder(codec_config_obu, number_of_channels);
auto status = lpcm_decoder.Initialize();

EXPECT_FALSE(status.ok());
}

} // namespace
Expand Down

0 comments on commit 65507c2

Please sign in to comment.