Skip to content

Commit

Permalink
Add virtual function to ParamDefinition that reads the parts of param…
Browse files Browse the repository at this point in the history
… blocks that are common to all.

PiperOrigin-RevId: 628137604
  • Loading branch information
Googler authored and jwcullen committed Apr 25, 2024
1 parent 3f61caa commit 1305d1d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions iamf/obu/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ cc_library(
":leb128",
"//iamf/common:macros",
"//iamf/common:obu_util",
"//iamf/common:read_bit_buffer",
"//iamf/common:write_bit_buffer",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
Expand Down
30 changes: 30 additions & 0 deletions iamf/obu/param_definitions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "absl/strings/str_cat.h"
#include "iamf/common/macros.h"
#include "iamf/common/obu_util.h"
#include "iamf/common/read_bit_buffer.h"
#include "iamf/common/write_bit_buffer.h"
#include "iamf/obu/demixing_info_param_data.h"
#include "iamf/obu/leb128.h"
Expand Down Expand Up @@ -156,6 +157,35 @@ absl::Status ParamDefinition::ValidateAndWrite(WriteBitBuffer& wb) const {
return absl::OkStatus();
}

absl::Status ParamDefinition::ReadAndValidate(ReadBitBuffer& rb) {
// Read the fields that are always present in `param_definition`.
RETURN_IF_NOT_OK(rb.ReadULeb128(parameter_id_));
RETURN_IF_NOT_OK(rb.ReadULeb128(parameter_rate_));
RETURN_IF_NOT_OK(rb.ReadUnsignedLiteral(1, param_definition_mode_));
RETURN_IF_NOT_OK(rb.ReadUnsignedLiteral(7, reserved_));
if (param_definition_mode_ != 0) {
return absl::OkStatus();
}

// Read the fields dependent on `param_definition_mode == 0`.
RETURN_IF_NOT_OK(rb.ReadULeb128(duration_));
RETURN_IF_NOT_OK(rb.ReadULeb128(constant_subblock_duration_));
if (constant_subblock_duration_ != 0) {
return absl::OkStatus();
}

// Loop to read the `subblock_durations` array if it should be included.
RETURN_IF_NOT_OK(rb.ReadULeb128(num_subblocks_));
for (int i = 0; i < num_subblocks_; i++) {
DecodedUleb128 subblock_duration;
RETURN_IF_NOT_OK(rb.ReadULeb128(subblock_duration));
subblock_durations_.push_back(subblock_duration);
}

RETURN_IF_NOT_OK(Validate());
return absl::OkStatus();
}

void ParamDefinition::Print() const {
LOG(INFO) << " parameter_id= " << parameter_id_;
LOG(INFO) << " parameter_rate= " << parameter_rate_;
Expand Down
11 changes: 11 additions & 0 deletions iamf/obu/param_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <vector>

#include "absl/status/status.h"
#include "iamf/common/read_bit_buffer.h"
#include "iamf/common/write_bit_buffer.h"
#include "iamf/obu/demixing_info_param_data.h"
#include "iamf/obu/leb128.h"
Expand Down Expand Up @@ -101,6 +102,16 @@ class ParamDefinition {
*/
virtual absl::Status ValidateAndWrite(WriteBitBuffer& wb) const;

/*!\brief Reads and validates the parameter definition.
*
* This function defines the validating and reading of the common parts,
* and the sub-classes's overridden ones shall define their specific parts.
*
* \param rb Buffer to read from.
* \return `absl::OkStatus()` if successful. A specific status on failure.
*/
virtual absl::Status ReadAndValidate(ReadBitBuffer& rb);

/*!\brief Gets the `ParameterDefinitionType`.
*
* \return Type of this parameter definition.
Expand Down
1 change: 1 addition & 0 deletions iamf/obu/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ cc_test(
srcs = ["param_definitions_test.cc"],
deps = [
"//iamf/cli:leb_generator",
"//iamf/common:read_bit_buffer",
"//iamf/common:write_bit_buffer",
"//iamf/common/tests:test_utils",
"//iamf/obu:demixing_info_param_data",
Expand Down
55 changes: 55 additions & 0 deletions iamf/obu/tests/param_definitions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "absl/status/status.h"
#include "gtest/gtest.h"
#include "iamf/cli/leb_generator.h"
#include "iamf/common/read_bit_buffer.h"
#include "iamf/common/tests/test_utils.h"
#include "iamf/common/write_bit_buffer.h"
#include "iamf/obu/demixing_info_param_data.h"
Expand Down Expand Up @@ -573,5 +574,59 @@ TEST_F(ExtendedParamDefinitionTest, WriteFailsIfSizeIsInconsistent) {
TestWrite({});
}

TEST(ReadParamDefinitionTest, Mode1) {
ParamDefinition param_definition;
std::vector<uint8_t> source = {
// Parameter ID.
0x00,
// Parameter Rate.
1,
// Param Definition Mode (upper bit), next 7 bits reserved.
0x80};
ReadBitBuffer buffer(1024, &source);
EXPECT_TRUE(param_definition.ReadAndValidate(buffer).ok());
}

TEST(ReadParamDefinitionTest, Mode0NonZeroSubblockDuration) {
ParamDefinition param_definition;
std::vector<uint8_t> source = {
// Parameter ID.
0x00,
// Parameter Rate.
1,
// Param Definition Mode (upper bit), next 7 bits reserved.
0x00,
// `duration`.
0xc0, 0x00,
// `constant_subblock_duration`.
0xc0, 0x00};
ReadBitBuffer buffer(1024, &source);
EXPECT_TRUE(param_definition.ReadAndValidate(buffer).ok());
}

TEST(ReadParamDefinitionTest, Mode0SubblockArray) {
ParamDefinition param_definition;
std::vector<uint8_t> source = {
// Parameter ID.
0x00,
// Parameter Rate.
1,
// Param Definition Mode (upper bit), next 7 bits reserved.
0x00,
// `duration` (64).
0xc0, 0x00,
// `constant_subblock_duration`.
0x00,
// `num_subblocks`
0x02,
// `subblock_durations`
// `subblock_duration`
40,
// `subblock_duration`
24};
ReadBitBuffer buffer(1024, &source);
EXPECT_TRUE(param_definition.ReadAndValidate(buffer).ok());
}

} // namespace
} // namespace iamf_tools

0 comments on commit 1305d1d

Please sign in to comment.