Skip to content

Commit

Permalink
mediacodec: Do not prefer hardware if profile != 0
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 682026600
  • Loading branch information
vigneshvg authored and copybara-github committed Oct 3, 2024
1 parent 9669802 commit 0009ebf
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
76 changes: 41 additions & 35 deletions src/codecs/android_mediacodec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,24 @@ enum CodecInitializer {
ByMimeType(String),
}

#[cfg(android_soong)]
fn prefer_hw(config: &DecoderConfig) -> bool {
let prefer_hw = rustutils::system_properties::read_bool(
"media.stagefright.thumbnail.prefer_hw_codecs",
false,
)
.unwrap_or(false);
// We will return true when all of the below conditions are true:
// 1) prefer_hw is true.
// 2) category is not Alpha. We do not prefer hardware for decoding the alpha plane since
// they generally tend to be monochrome images and using hardware for that is
// unreliable.
// 3) profile is 0. As of Sep 2024, there are no AV1 hardware decoders that support
// anything other than profile 0.
prefer_hw && config.category != Category::Alpha && config.codec_config.profile() == 0;
}

fn get_codec_initializers(config: &DecoderConfig) -> Vec<CodecInitializer> {
let dav1d = String::from("c2.android.av1-dav1d.decoder");
let gav1 = String::from("c2.android.av1.decoder");
// As of Sep 2024, c2.android.av1.decoder is the only known decoder to support 12-bit AV1. So
// prefer that for 12 bit images.
let prefer_gav1 = config.depth == 12;
let mime_type = MediaCodec::AV1_MIME;
#[cfg(android_soong)]
{
// Use a specific decoder if it is requested.
Expand All @@ -232,42 +243,37 @@ fn get_codec_initializers(config: &DecoderConfig) -> Vec<CodecInitializer> {
return vec![CodecInitializer::ByName(decoder)];
}
}
// If hardware decoders are allowed, then search by mime type first and then try the
// software decoders.
let prefer_hw = rustutils::system_properties::read_bool(
"media.stagefright.thumbnail.prefer_hw_codecs",
false,
)
.unwrap_or(false);
if prefer_hw {
if prefer_gav1 {
return vec![
CodecInitializer::ByName(gav1),
CodecInitializer::ByMimeType(mime_type.to_string()),
CodecInitializer::ByName(dav1d),
];
} else {
return vec![
CodecInitializer::ByMimeType(mime_type.to_string()),
CodecInitializer::ByName(dav1d),
CodecInitializer::ByName(gav1),
];
}
}
}
// Default list of initializers.
if prefer_gav1 {
vec![
let dav1d = String::from("c2.android.av1-dav1d.decoder");
let gav1 = String::from("c2.android.av1.decoder");
// As of Sep 2024, c2.android.av1.decoder is the only known decoder to support 12-bit AV1. So
// prefer that for 12 bit images.
let prefer_gav1 = config.depth == 12;
let mime_type = MediaCodec::AV1_MIME;
let prefer_hw = false;
#[cfg(android_soong)]
let prefer_hw = prefer_hw(config);
match (prefer_hw, prefer_gav1) {
(true, true) => vec![
CodecInitializer::ByName(gav1),
CodecInitializer::ByMimeType(mime_type.to_string()),
CodecInitializer::ByName(dav1d),
],
(true, false) => vec![
CodecInitializer::ByMimeType(mime_type.to_string()),
CodecInitializer::ByName(dav1d),
CodecInitializer::ByName(gav1),
],
(false, true) => vec![
CodecInitializer::ByName(gav1),
CodecInitializer::ByName(dav1d),
CodecInitializer::ByMimeType(mime_type.to_string()),
]
} else {
vec![
],
(false, false) => vec![
CodecInitializer::ByName(dav1d),
CodecInitializer::ByName(gav1),
CodecInitializer::ByMimeType(mime_type.to_string()),
]
],
}
}

Expand Down
1 change: 1 addition & 0 deletions src/codecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct DecoderConfig {
pub max_threads: u32,
pub max_input_size: usize,
pub codec_config: CodecConfiguration,
pub category: Category,
}

pub trait Decoder {
Expand Down
1 change: 1 addition & 0 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,7 @@ impl Decoder {
max_threads: self.settings.max_threads,
max_input_size: tile.max_sample_size(),
codec_config: tile.codec_config.clone(),
category,
};
codec.initialize(&config)?;
self.codecs.push(codec);
Expand Down
6 changes: 6 additions & 0 deletions src/parser/mp4box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ impl CodecConfiguration {
Self::Av1(config) => &config.raw_data,
}
}

pub fn profile(&self) -> u8 {
match self {
Self::Av1(config) => config.seq_profile,
}
}
}

#[derive(Clone, Debug, Default)]
Expand Down

0 comments on commit 0009ebf

Please sign in to comment.