Skip to content

Commit

Permalink
Add AudioFrameObu::CreateFromBuffer functionality.
Browse files Browse the repository at this point in the history
This function will call a private explicit constructor which uses defaults to create an instance of the class. It will then call ValidateAndReadPayload, which will read from the provided buffer and fill in the appropriate fields within the AudioFrameObu instance. This call must succeed - if it does not, we will return an error status which will signal to the caller that we were not able to read the OBU properly.

This function will be called in ProcessTemporalUnitObus.

PiperOrigin-RevId: 631820105
  • Loading branch information
Googler authored and jwcullen committed May 9, 2024
1 parent ff96df6 commit b152572
Show file tree
Hide file tree
Showing 5 changed files with 44 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 @@ -53,6 +53,7 @@ cc_library(
"//iamf/common:write_bit_buffer",
"@com_google_absl//absl/log",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
],
)

Expand Down
8 changes: 8 additions & 0 deletions iamf/obu/audio_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ AudioFrameObu::AudioFrameObu(const ObuHeader& header,
audio_frame_(audio_frame),
audio_substream_id_(substream_id) {}

absl::StatusOr<AudioFrameObu> AudioFrameObu::CreateFromBuffer(
const ObuHeader& header, const int64_t obu_payload_serialized_size,
ReadBitBuffer& rb) {
AudioFrameObu audio_frame_obu(header);
RETURN_IF_NOT_OK(audio_frame_obu.ValidateAndReadPayload(rb));
return audio_frame_obu;
}

absl::Status AudioFrameObu::ValidateAndWritePayload(WriteBitBuffer& wb) const {
if (header_.obu_type == kObuIaAudioFrame) {
// The ID is explicitly in the bitstream when `kObuIaAudioFrame`. Otherwise
Expand Down
22 changes: 22 additions & 0 deletions iamf/obu/audio_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vector>

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "iamf/common/read_bit_buffer.h"
#include "iamf/common/write_bit_buffer.h"
#include "iamf/obu/leb128.h"
Expand All @@ -37,6 +38,22 @@ namespace iamf_tools {
*/
class AudioFrameObu : public ObuBase {
public:
/*!\brief Creates an `AudioFrameObu` from a `ReadBitBuffer`.
*
* This function is designed to be used from the perspective of the decoder.
* It will call `ValidateAndReadPayload` in order to read from the buffer;
* therefore it can fail.
*
* \param header `ObuHeader` of the OBU.
* \param obu_payload_serialized_size Size of the obu payload in bytes.
* \param rb `ReadBitBuffer` where the `AudioFrameObu` data is stored.
* Data read from the buffer is consumed.
* \return a `AudioFrameObu` on success. A specific status on failure.
*/
static absl::StatusOr<AudioFrameObu> CreateFromBuffer(
const ObuHeader& header, int64_t obu_payload_serialized_size,
ReadBitBuffer& rb);

/*\!brief Constructor.
*
* \param header `ObuHeader` of the OBU.
Expand Down Expand Up @@ -68,6 +85,11 @@ class AudioFrameObu : public ObuBase {
// This field is not serialized when in the range [0, 17].
const DecodedUleb128 audio_substream_id_;

// Used only by the factory create function.
explicit AudioFrameObu(const ObuHeader& header)
: ObuBase(header, kObuIaAudioElement),
audio_frame_({}),
audio_substream_id_(DecodedUleb128()) {}
/*\!brief Writes the OBU payload to the buffer.
*
* \param wb Buffer to write to.
Expand Down
1 change: 1 addition & 0 deletions iamf/obu/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ cc_test(
deps = [
":obu_test_base",
"//iamf/cli:leb_generator",
"//iamf/common:read_bit_buffer",
"//iamf/common:write_bit_buffer",
"//iamf/obu:audio_frame",
"//iamf/obu:leb128",
Expand Down
12 changes: 12 additions & 0 deletions iamf/obu/tests/audio_frame_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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/write_bit_buffer.h"
#include "iamf/obu/leb128.h"
#include "iamf/obu/obu_header.h"
Expand Down Expand Up @@ -285,5 +286,16 @@ TEST_F(AudioFrameObuTest, ValidateAndWriteObuFailsWithIllegalRedundantCopy) {
EXPECT_FALSE(obu_->ValidateAndWriteObu(unused_wb).ok());
}

// --- Begin CreateFromBuffer tests ---
// TODO(b/329700769): Update test once ValidateAndReadPayload is implemented.
TEST(CreateFromBuffer, IsNotSupported) {
std::vector<uint8_t> source;
ReadBitBuffer buffer(1024, &source);
ObuHeader header;
int64_t obu_payload_size = 16;
EXPECT_FALSE(
AudioFrameObu::CreateFromBuffer(header, obu_payload_size, buffer).ok());
}

} // namespace
} // namespace iamf_tools

0 comments on commit b152572

Please sign in to comment.