Skip to content

Commit

Permalink
mediacodec: Disallow hardware for alpha decoding
Browse files Browse the repository at this point in the history
Alpha planes are generally monochrome and hardware is not reliable
(and a bit unnecessary) for those images.

Note that the color planes will still be decoded by hardware if it
is allowed.

PiperOrigin-RevId: 682061628
  • Loading branch information
vigneshvg authored and copybara-github committed Oct 3, 2024
1 parent fdd8257 commit b221bc8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/codecs/android_mediacodec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,19 @@ enum CodecInitializer {
ByMimeType(String),
}

fn prefer_hw() -> bool {
#[cfg(not(android_soong))]
return false;

#[cfg(android_soong)]
{
let prefer_hw = rustutils::system_properties::read_bool(
"media.stagefright.thumbnail.prefer_hw_codecs",
false,
)
.unwrap_or(false);
if !prefer_hw {
return false;
}
return true;
}
#[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.
prefer_hw && config.category != Category::Alpha
}

fn get_codec_initializers(config: &DecoderConfig) -> Vec<CodecInitializer> {
Expand All @@ -251,7 +248,10 @@ fn get_codec_initializers(config: &DecoderConfig) -> Vec<CodecInitializer> {
// prefer that for 12 bit images.
let prefer_gav1 = config.depth == 12;
let mime_type = MediaCodec::AV1_MIME;
match (prefer_hw(), prefer_gav1) {
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()),
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

0 comments on commit b221bc8

Please sign in to comment.