Skip to content

Commit

Permalink
Update config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
osagie98 committed Jul 9, 2024
1 parent 2d324c9 commit 68a425d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 36 deletions.
56 changes: 24 additions & 32 deletions starboard/shared/libiamf/iamf_audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,30 @@ bool IamfAudioDecoder::DecodeInternal(
scoped_refptr<DecodedAudio> 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<void*>(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";
Expand Down Expand Up @@ -176,55 +178,57 @@ 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)
<< "IAMF_decoder_output_layout_set_sound_system() fails with error "
<< 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);
Expand All @@ -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());
Expand All @@ -244,7 +246,6 @@ bool IamfAudioDecoder::InitializeCodec() {
<< error;
return false;
}
SB_LOG(INFO) << "7";
}

error = IAMF_decoder_peak_limiter_enable(decoder_, 0);
Expand All @@ -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) {
Expand All @@ -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;
}

Expand All @@ -294,15 +291,14 @@ scoped_refptr<IamfAudioDecoder::DecodedAudio> IamfAudioDecoder::Read(
result = decoded_audios_.front();
decoded_audios_.pop();
}
*samples_per_second = 48000;
*samples_per_second = kOutputSamplesPerSecond;
return result;
}

void IamfAudioDecoder::Reset() {
SB_DCHECK(BelongsToCurrentThread());

if (is_valid()) {
// If fail to reset opus decoder, re-create it.
TeardownCodec();
InitializeCodec();
}
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion starboard/shared/libiamf/iamf_audio_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class IamfAudioDecoder
void TeardownCodec();
void DecodePendingBuffers();
bool DecodeInternal(const scoped_refptr<InputBuffer>& 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;

Expand Down
54 changes: 51 additions & 3 deletions starboard/shared/libiamf/iamf_config_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "starboard/shared/libiamf/iamf_config_reader.h"

#include <string>

#include "starboard/common/string.h"

namespace starboard {
Expand Down Expand Up @@ -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<char>(buf[bytes_read]));
bytes_read++;
}

return bytes_read;
}
} // namespace

bool IamfConfigReader::Read(scoped_refptr<InputBuffer> 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);
Expand All @@ -72,10 +89,11 @@ bool IamfConfigReader::Read(scoped_refptr<InputBuffer> 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;
Expand All @@ -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<int>(obu_type)) {
case kObuTypeCodecConfig:
SB_LOG(INFO) << "Reading codec config OBU";
Expand All @@ -106,15 +128,41 @@ 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_);
if (bytes_read < 0) {
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<int>(obu_type);
completed_parsing = true;
Expand Down

0 comments on commit 68a425d

Please sign in to comment.