Skip to content

Commit

Permalink
Unify style of Opus and AAC error code conversion utilities.
Browse files Browse the repository at this point in the history
  - Prefer `absl::string_view`.
  - Prefer returning `absl::Status` because the `StatusCode` was always being wrapped into one anyway.

PiperOrigin-RevId: 643439926
  • Loading branch information
jwcullen committed Jun 14, 2024
1 parent c0acc6c commit 1623380
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 42 deletions.
2 changes: 2 additions & 0 deletions iamf/cli/codec/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ cc_library(
hdrs = ["opus_utils.h"],
deps = [
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@libopus",
],
)
19 changes: 5 additions & 14 deletions iamf/cli/codec/opus_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ absl::Status OpusDecoder::Initialize() {
int opus_error_code;
decoder_ = opus_decoder_create(static_cast<opus_int32>(output_sample_rate_),
num_channels_, &opus_error_code);

const auto status_code = OpusErrorCodeToAbslStatusCode(opus_error_code);
if (status_code != absl::StatusCode::kOk) {
decoder_ = nullptr;
return absl::Status(
status_code,
absl::StrCat("Failed to initialize Opus decoder: opus_error_code= ",
opus_error_code));
}
RETURN_IF_NOT_OK(OpusErrorCodeToAbslStatus(
opus_error_code, "Failed to initialize Opus decoder."));

return absl::OkStatus();
}
Expand All @@ -107,11 +100,9 @@ absl::Status OpusDecoder::DecodeAudioFrame(
/*frame_size=*/num_samples_per_channel_,
/*decode_fec=*/0);
if (num_output_samples < 0) {
// When `num_output_samples` is negative, it is an Opus error code.
return absl::Status(
OpusErrorCodeToAbslStatusCode(num_output_samples),
absl::StrCat("Failed to initialize Opus decoder: num_output_samples= ",
num_output_samples));
// When `num_output_samples` is negative, it is a non-OK Opus error code.
return OpusErrorCodeToAbslStatus(num_output_samples,
"Failed to decode Opus frame.");
}
output_pcm_float.resize(num_output_samples * num_channels_);
LOG_FIRST_N(INFO, 3) << "Opus decoded " << num_output_samples
Expand Down
24 changes: 7 additions & 17 deletions iamf/cli/codec/opus_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,15 @@ absl::Status OpusEncoder::InitializeEncoder() {
application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
break;
default:
LOG(ERROR) << "Unrecognized application: "
<< encoder_metadata_.application();
return absl::UnknownError("");
return absl::InvalidArgumentError(absl::StrCat(
"Unrecognized application.", encoder_metadata_.application()));
}

int opus_error_code;
encoder_ = opus_encoder_create(input_sample_rate_, num_channels_, application,
&opus_error_code);

const auto status_code = OpusErrorCodeToAbslStatusCode(opus_error_code);
if (status_code != absl::StatusCode::kOk) {
return absl::Status(
status_code,
absl::StrCat("Failed to initialize Opus encoder: opus_error_code= ",
opus_error_code));
}
RETURN_IF_NOT_OK(OpusErrorCodeToAbslStatus(
opus_error_code, "Failed to initialize Opus encoder."));

// `OPUS_SET_BITRATE` treats this as the bit-rate for the entire substream.
// Configure `libopus` so coupled substreams and mono substreams have the same
Expand Down Expand Up @@ -193,12 +186,9 @@ absl::Status OpusEncoder::EncodeAudioFrame(
}

if (*encoded_length_bytes < 0) {
// When `encoded_length_bytes` is negative, it is an Opus error code.
return absl::Status(
OpusErrorCodeToAbslStatusCode(*encoded_length_bytes),
absl::StrCat(
"Failed to initialize Opus decoder: encoded_length_bytes= ",
*encoded_length_bytes));
// When `encoded_length_bytes` is negative, it is a non-OK Opus error code.
return OpusErrorCodeToAbslStatus(*encoded_length_bytes,
"Failed to encode samples.");
}

// Shrink output vector to actual size.
Expand Down
32 changes: 23 additions & 9 deletions iamf/cli/codec/opus_utils.cc
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
#include "iamf/cli/codec/opus_utils.h"

#include "absl/status/status.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "include/opus_defines.h"

namespace iamf_tools {

absl::StatusCode OpusErrorCodeToAbslStatusCode(int opus_error_code) {
absl::Status OpusErrorCodeToAbslStatus(int opus_error_code,
absl::string_view error_message) {
absl::StatusCode status_code;
switch (opus_error_code) {
case OPUS_OK:
return absl::StatusCode::kOk;
return absl::OkStatus();
case OPUS_BAD_ARG:
return absl::StatusCode::kInvalidArgument;
status_code = absl::StatusCode::kInvalidArgument;
break;
case OPUS_BUFFER_TOO_SMALL:
case OPUS_INVALID_STATE:
return absl::StatusCode::kFailedPrecondition;
status_code = absl::StatusCode::kFailedPrecondition;
break;
case OPUS_INTERNAL_ERROR:
return absl::StatusCode::kInternal;
status_code = absl::StatusCode::kInternal;
break;
case OPUS_INVALID_PACKET:
return absl::StatusCode::kDataLoss;
status_code = absl::StatusCode::kDataLoss;
break;
case OPUS_UNIMPLEMENTED:
return absl::StatusCode::kUnimplemented;
status_code = absl::StatusCode::kUnimplemented;
break;
case OPUS_ALLOC_FAIL:
return absl::StatusCode::kResourceExhausted;
status_code = absl::StatusCode::kResourceExhausted;
break;
default:
return absl::StatusCode::kUnknown;
status_code = absl::StatusCode::kUnknown;
break;
}
return absl::Status(
status_code,
absl::StrCat(error_message, " opus_error_code= ", opus_error_code));
}

} // namespace iamf_tools
11 changes: 9 additions & 2 deletions iamf/cli/codec/opus_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
#define CLI_CODEC_OPUS_UTILS_H_

#include "absl/status/status.h"
#include "absl/strings/string_view.h"

namespace iamf_tools {

/*!\brief Translates from Opus error codes to Abseil standard error codes.*/
absl::StatusCode OpusErrorCodeToAbslStatusCode(int opus_error_code);
/*!\brief Converts a `libopus` error code to an `absl::Status`.
*
* \param opus_error_code Error code from `libopus`.
* \param message Message to include in the returned `absl::Status`.
* \return `absl::Status` corresponding to input arguments.
*/
absl::Status OpusErrorCodeToAbslStatus(int opus_error_code,
absl::string_view message);

} // namespace iamf_tools

Expand Down

0 comments on commit 1623380

Please sign in to comment.