From 68a425d4a94f6e1cdae667a114c8823703eef119 Mon Sep 17 00:00:00 2001 From: Austin Osagie Date: Tue, 9 Jul 2024 14:43:08 -0700 Subject: [PATCH] Update config parsing --- .../shared/libiamf/iamf_audio_decoder.cc | 56 ++++++++----------- starboard/shared/libiamf/iamf_audio_decoder.h | 4 +- .../shared/libiamf/iamf_config_reader.cc | 54 +++++++++++++++++- 3 files changed, 78 insertions(+), 36 deletions(-) diff --git a/starboard/shared/libiamf/iamf_audio_decoder.cc b/starboard/shared/libiamf/iamf_audio_decoder.cc index 8b537f9d51cc..aa80f3d02e9d 100644 --- a/starboard/shared/libiamf/iamf_audio_decoder.cc +++ b/starboard/shared/libiamf/iamf_audio_decoder.cc @@ -124,28 +124,30 @@ bool IamfAudioDecoder::DecodeInternal( scoped_refptr decoded_audio = new DecodedAudio( audio_stream_info_.number_of_channels, GetSampleType(), kSbMediaAudioFrameStorageTypeInterleaved, input_buffer->timestamp(), - audio_stream_info_.number_of_channels * frames_per_au_ * + audio_stream_info_.number_of_channels * kMaxOpusFramesPerAU * starboard::media::GetBytesPerSample(GetSampleType())); - uint32_t rsize = 0; - SB_LOG(INFO) << "Start decode"; + SB_LOG(INFO) << "Start decode with AU size " << reader_.data().size(); int ret = IAMF_decoder_decode(decoder_, reader_.data().data(), - reader_.data().size(), &rsize, + reader_.data().size(), nullptr, reinterpret_cast(decoded_audio->data())); if (ret < 1) { SB_LOG(INFO) << "IAMF_decoder_decode() error " << std::hex << ret; error_cb_(kSbPlayerErrorDecode, "Failed to decode sample"); return false; + } else { + SB_LOG(INFO) << "Decoded " << ret << " samples"; + SB_DCHECK(ret <= kMaxOpusFramesPerAU); } frames_per_au_ = ret; decoded_audio->ShrinkTo(audio_stream_info_.number_of_channels * frames_per_au_ * starboard::media::GetBytesPerSample(GetSampleType())); - const auto& sample_info = input_buffer->audio_sample_info(); - decoded_audio->AdjustForDiscardedDurations( - audio_stream_info_.samples_per_second, - sample_info.discarded_duration_from_front, - sample_info.discarded_duration_from_back); + // const auto& sample_info = input_buffer->audio_sample_info(); + // decoded_audio->AdjustForDiscardedDurations( + // audio_stream_info_.samples_per_second, + // sample_info.discarded_duration_from_front, + // sample_info.discarded_duration_from_back); decoded_audios_.push(decoded_audio); output_cb_(); SB_LOG(INFO) << "Returning decoded audio"; @@ -176,47 +178,48 @@ bool IamfAudioDecoder::InitializeCodec() { SB_LOG(ERROR) << "Can't create decoder with " << channels << " channels"; return false; } - SB_LOG(INFO) << "1"; decoder_ = IAMF_decoder_open(); if (!decoder_) { SB_LOG(ERROR) << "Error creating libiamf decoder"; return false; } - SB_LOG(INFO) << "2"; - int error = IAMF_decoder_set_bit_depth(decoder_, 32); + int error = IAMF_decoder_set_bit_depth(decoder_, kOutputBitDepth); if (error != IAMF_OK) { SB_LOG(ERROR) << "IAMF_decoder_set_bit_depth() fails with error " << error; return false; } - SB_LOG(INFO) << "3"; - error = IAMF_decoder_set_sampling_rate(decoder_, 48000); + error = IAMF_decoder_set_sampling_rate(decoder_, kOutputSamplesPerSecond); if (error != IAMF_OK) { SB_LOG(ERROR) << "IAMF_decoder_set_sampling_rate() fails with error " << error; return false; } - SB_LOG(INFO) << "4"; IAMF_SoundSystem sound_system = SOUND_SYSTEM_INVALID; switch (channels) { case 1: sound_system = SOUND_SYSTEM_MONO; + SB_LOG(INFO) << "Configuring IamfAudioDecoder for mono output"; break; case 2: sound_system = SOUND_SYSTEM_A; + SB_LOG(INFO) << "Configuring IamfAudioDecoder for stereo output"; break; case 6: sound_system = SOUND_SYSTEM_B; + SB_LOG(INFO) << "Configuring IamfAudioDecoder for 5.1 output"; break; case 8: sound_system = SOUND_SYSTEM_C; + SB_LOG(INFO) << "Configuring IamfAudioDecoder for 7.1 output"; break; default: SB_NOTREACHED(); } + error = IAMF_decoder_output_layout_set_sound_system(decoder_, sound_system); if (error != IAMF_OK) { SB_LOG(ERROR) @@ -224,7 +227,8 @@ bool IamfAudioDecoder::InitializeCodec() { << error; return false; } - SB_LOG(INFO) << "5"; + SB_LOG(INFO) << "num channels: " + << IAMF_layout_sound_system_channels_count(sound_system); // TODO: Accurately set pts upon resume, if needed. error = IAMF_decoder_set_pts(decoder_, 0, 90000); @@ -233,8 +237,6 @@ bool IamfAudioDecoder::InitializeCodec() { return false; } - SB_LOG(INFO) << "6"; - if (reader_.has_mix_presentation_id()) { error = IAMF_decoder_set_mix_presentation_id(decoder_, reader_.mix_presentation_id()); @@ -244,7 +246,6 @@ bool IamfAudioDecoder::InitializeCodec() { << error; return false; } - SB_LOG(INFO) << "7"; } error = IAMF_decoder_peak_limiter_enable(decoder_, 0); @@ -253,7 +254,6 @@ bool IamfAudioDecoder::InitializeCodec() { << error; return false; } - SB_LOG(INFO) << "8"; error = IAMF_decoder_set_normalization_loudness(decoder_, .0f); if (error != IAMF_OK) { @@ -262,17 +262,14 @@ bool IamfAudioDecoder::InitializeCodec() { << error; return false; } - SB_LOG(INFO) << "9"; - uint32_t rsize = 0; error = IAMF_decoder_configure(decoder_, reader_.config_obus().data(), - reader_.config_size(), &rsize); + reader_.config_size(), nullptr); if (error != IAMF_OK) { - SB_LOG(ERROR) << "IAMF_decoder_configure() fails with error " << error - << ", rsize " << rsize; + SB_LOG(ERROR) << "IAMF_decoder_configure() fails with error " << error; return false; } - SB_LOG(INFO) << "rsize is " << rsize; + return true; } @@ -294,7 +291,7 @@ scoped_refptr IamfAudioDecoder::Read( result = decoded_audios_.front(); decoded_audios_.pop(); } - *samples_per_second = 48000; + *samples_per_second = kOutputSamplesPerSecond; return result; } @@ -302,7 +299,6 @@ void IamfAudioDecoder::Reset() { SB_DCHECK(BelongsToCurrentThread()); if (is_valid()) { - // If fail to reset opus decoder, re-create it. TeardownCodec(); InitializeCodec(); } @@ -320,11 +316,7 @@ void IamfAudioDecoder::Reset() { SbMediaAudioSampleType IamfAudioDecoder::GetSampleType() const { SB_DCHECK(BelongsToCurrentThread()); -#if SB_API_VERSION <= 15 && SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES) - return kSbMediaAudioSampleTypeInt16; -#else // SB_API_VERSION <= 15 && SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES) return kSbMediaAudioSampleTypeFloat32; -#endif // SB_API_VERSION <= 15 && SB_HAS_QUIRK(SUPPORT_INT16_AUDIO_SAMPLES) } } // namespace libiamf diff --git a/starboard/shared/libiamf/iamf_audio_decoder.h b/starboard/shared/libiamf/iamf_audio_decoder.h index 47279a0cf986..3dd29023e65b 100644 --- a/starboard/shared/libiamf/iamf_audio_decoder.h +++ b/starboard/shared/libiamf/iamf_audio_decoder.h @@ -59,7 +59,9 @@ class IamfAudioDecoder void TeardownCodec(); void DecodePendingBuffers(); bool DecodeInternal(const scoped_refptr& input_buffer); - static const int kMaxOpusFramesPerAU = 9600; + static const int kMaxOpusFramesPerAU = 960; + static const int kOutputSamplesPerSecond = 48000; + static const int kOutputBitDepth = 32; SbMediaAudioSampleType GetSampleType() const; diff --git a/starboard/shared/libiamf/iamf_config_reader.cc b/starboard/shared/libiamf/iamf_config_reader.cc index bc797937d0d3..1391ae7132ff 100644 --- a/starboard/shared/libiamf/iamf_config_reader.cc +++ b/starboard/shared/libiamf/iamf_config_reader.cc @@ -14,6 +14,8 @@ #include "starboard/shared/libiamf/iamf_config_reader.h" +#include + #include "starboard/common/string.h" namespace starboard { @@ -50,11 +52,26 @@ int ReadLeb128Value(const uint8_t* buf, uint32_t* value) { } return i + 1; } + +int ReadString(const uint8_t* buf, std::string& value) { + SB_DCHECK(buf); + value = ""; + value.reserve(128); + + int bytes_read = 0; + while (bytes_read < 128 && buf[bytes_read] != '\0') { + value.push_back(static_cast(buf[bytes_read])); + bytes_read++; + } + + return bytes_read; +} } // namespace bool IamfConfigReader::Read(scoped_refptr input_buffer) { buffer_head_ = 0; has_mix_presentation_id_ = false; + config_size_ = 0; SB_LOG(INFO) << "Input buffer size is " << input_buffer->size(); const uint8_t* buf = input_buffer->data(); SB_DCHECK(buf); @@ -72,10 +89,11 @@ bool IamfConfigReader::Read(scoped_refptr input_buffer) { SB_LOG(INFO) << "End OBU loop"; data_size_ = input_buffer->size() - config_size_; + SB_LOG(INFO) << "Input size: " << input_buffer->size() + << ", Data size: " << input_buffer->size() - config_size_ + << ", config size: " << config_size_; config_obus_.assign(buf, buf + config_size_); data_.assign(buf + config_size_, buf + input_buffer->size()); - SB_LOG(INFO) << ::starboard::HexEncode(config_obus_.data(), - config_obus_.size()); SB_LOG(INFO) << "Return read"; return true; @@ -96,6 +114,10 @@ bool IamfConfigReader::ReadOBU(const uint8_t* buf, bool& completed_parsing) { int next_obu_pos = buffer_head_ + obu_size; int bytes_read = 0; + uint32_t count_label = 0; + std::string str; + uint32_t num_sub_mixes = 0; + switch (static_cast(obu_type)) { case kObuTypeCodecConfig: SB_LOG(INFO) << "Reading codec config OBU"; @@ -106,7 +128,7 @@ bool IamfConfigReader::ReadOBU(const uint8_t* buf, bool& completed_parsing) { case kObuTypeSequenceHeader: SB_LOG(INFO) << "Reading sequence header OBU"; break; - case kObuTypeMixPresentation: + case kObuTypeMixPresentation: { SB_LOG(INFO) << "Reading mix presentation OBU"; has_mix_presentation_id_ = true; bytes_read = ReadLeb128Value(&buf[buffer_head_], &mix_presentation_id_); @@ -114,7 +136,33 @@ bool IamfConfigReader::ReadOBU(const uint8_t* buf, bool& completed_parsing) { return false; } buffer_head_ += bytes_read; + + // count_label + bytes_read = ReadLeb128Value(&buf[buffer_head_], &count_label); + if (bytes_read < 0) { + return false; + } + buffer_head_ += bytes_read; + + // language_label + for (int i = 0; i < count_label; ++i) { + buffer_head_ += ReadString(&buf[buffer_head_], str); + } + + // MixPresentationAnnotations + for (int i = 0; i < count_label; ++i) { + buffer_head_ += ReadString(&buf[buffer_head_], str); + } + + // num_sub_mixes + bytes_read = ReadLeb128Value(&buf[buffer_head_], &num_sub_mixes); + if (bytes_read < 0) { + return false; + } + buffer_head_ += bytes_read; + break; + } default: SB_LOG(INFO) << "OBU type " << static_cast(obu_type); completed_parsing = true;