Revert "use fullrange by default for yuv420p if supported (#6655)" (#6656)

This reverts commit 80afa98d66.
This commit is contained in:
RustDesk
2023-12-11 23:46:32 +09:00
committed by GitHub
parent 80afa98d66
commit ebb14af488
15 changed files with 141 additions and 388 deletions

View File

@@ -6,10 +6,10 @@
include!(concat!(env!("OUT_DIR"), "/aom_ffi.rs"));
use crate::codec::{base_bitrate, codec_thread_num, ExtraEncoderCfg, Quality};
use crate::codec::{base_bitrate, codec_thread_num, Quality};
use crate::{codec::EncoderApi, EncodeFrame, STRIDE_ALIGN};
use crate::{common::GoogleImage, generate_call_macro, generate_call_ptr_macro, Error, Result};
use crate::{ColorRange, EncodeYuvFormat, Pixfmt};
use crate::{EncodeYuvFormat, Pixfmt};
use hbb_common::{
anyhow::{anyhow, Context},
bytes::Bytes,
@@ -53,7 +53,7 @@ pub struct AomEncoder {
ctx: aom_codec_ctx_t,
width: usize,
height: usize,
extra: ExtraEncoderCfg,
i444: bool,
yuvfmt: EncodeYuvFormat,
}
@@ -98,7 +98,7 @@ mod webrtc {
pub fn enc_cfg(
i: *const aom_codec_iface,
cfg: AomEncoderConfig,
extra: &ExtraEncoderCfg,
i444: bool,
) -> ResultType<aom_codec_enc_cfg> {
let mut c = unsafe { std::mem::MaybeUninit::zeroed().assume_init() };
call_aom!(aom_codec_enc_config_default(i, &mut c, kUsageProfile));
@@ -144,20 +144,12 @@ mod webrtc {
c.g_lag_in_frames = kLagInFrames; // No look ahead when lag equals 0.
// https://aomedia.googlesource.com/aom/+/refs/tags/v3.6.0/av1/common/enums.h#82
c.g_profile = if extra.pixfmt == Pixfmt::YUV444P {
1
} else {
0
};
c.g_profile = if i444 { 1 } else { 0 };
Ok(c)
}
pub fn set_controls(
ctx: *mut aom_codec_ctx_t,
cfg: &aom_codec_enc_cfg,
extra: ExtraEncoderCfg,
) -> ResultType<()> {
pub fn set_controls(ctx: *mut aom_codec_ctx_t, cfg: &aom_codec_enc_cfg) -> ResultType<()> {
use aom_tune_content::*;
use aome_enc_control_id::*;
macro_rules! call_ctl {
@@ -219,45 +211,20 @@ mod webrtc {
call_ctl!(ctx, AV1E_SET_ENABLE_SMOOTH_INTERINTRA, 0);
call_ctl!(ctx, AV1E_SET_ENABLE_TX64, 0);
call_ctl!(ctx, AV1E_SET_MAX_REFERENCE_FRAMES, 3);
// https://github.com/chromium/chromium/blob/327564a4861822c816d35395dfb54d7e5039e6ea/media/video/av1_video_encoder.cc#L662
call_ctl!(
ctx,
AV1E_SET_COLOR_RANGE,
if extra.range == ColorRange::Full {
aom_color_range::AOM_CR_FULL_RANGE
} else {
aom_color_range::AOM_CR_STUDIO_RANGE
}
);
call_ctl!(
ctx,
AV1E_SET_COLOR_PRIMARIES,
aom_color_primaries::AOM_CICP_CP_BT_601
);
call_ctl!(
ctx,
AV1E_SET_TRANSFER_CHARACTERISTICS,
aom_transfer_characteristics::AOM_CICP_TC_BT_601
);
call_ctl!(
ctx,
AV1E_SET_MATRIX_COEFFICIENTS,
aom_matrix_coefficients::AOM_CICP_MC_BT_601
);
Ok(())
}
}
impl EncoderApi for AomEncoder {
fn new(cfg: crate::codec::EncoderCfg, extra: ExtraEncoderCfg) -> ResultType<Self>
fn new(cfg: crate::codec::EncoderCfg, i444: bool) -> ResultType<Self>
where
Self: Sized,
{
match cfg {
crate::codec::EncoderCfg::AOM(config) => {
let i = call_aom_ptr!(aom_codec_av1_cx());
let c = webrtc::enc_cfg(i, config, &extra)?;
let c = webrtc::enc_cfg(i, config, i444)?;
let mut ctx = Default::default();
// Flag options: AOM_CODEC_USE_PSNR and AOM_CODEC_USE_HIGHBITDEPTH
@@ -269,13 +236,13 @@ impl EncoderApi for AomEncoder {
flags,
AOM_ENCODER_ABI_VERSION as _
));
webrtc::set_controls(&mut ctx, &c, extra)?;
webrtc::set_controls(&mut ctx, &c)?;
Ok(Self {
ctx,
width: config.width as _,
height: config.height as _,
extra,
yuvfmt: Self::get_yuvfmt(config.width, config.height, &extra),
i444,
yuvfmt: Self::get_yuvfmt(config.width, config.height, i444),
})
}
_ => Err(anyhow!("encoder type mismatch")),
@@ -324,13 +291,16 @@ impl EncoderApi for AomEncoder {
impl AomEncoder {
pub fn encode(&mut self, pts: i64, data: &[u8], stride_align: usize) -> Result<EncodeFrames> {
let (fmt, bpp) = match self.extra.pixfmt {
Pixfmt::YUV444P => (aom_img_fmt::AOM_IMG_FMT_I444, 24),
_ => (aom_img_fmt::AOM_IMG_FMT_I420, 12),
};
let bpp = if self.i444 { 24 } else { 12 };
if data.len() < self.width * self.height * bpp / 8 {
return Err(Error::FailedCall("len not enough".to_string()));
}
let fmt = if self.i444 {
aom_img_fmt::AOM_IMG_FMT_I444
} else {
aom_img_fmt::AOM_IMG_FMT_I420
};
let mut image = Default::default();
call_aom_ptr!(aom_img_wrap(
&mut image,
@@ -405,11 +375,12 @@ impl AomEncoder {
(q_min, q_max)
}
fn get_yuvfmt(width: u32, height: u32, extra: &ExtraEncoderCfg) -> EncodeYuvFormat {
fn get_yuvfmt(width: u32, height: u32, i444: bool) -> EncodeYuvFormat {
let mut img = Default::default();
let fmt = match extra.pixfmt {
Pixfmt::YUV444P => aom_img_fmt::AOM_IMG_FMT_I444,
_ => aom_img_fmt::AOM_IMG_FMT_I420,
let fmt = if i444 {
aom_img_fmt::AOM_IMG_FMT_I444
} else {
aom_img_fmt::AOM_IMG_FMT_I420
};
unsafe {
aom_img_wrap(
@@ -421,9 +392,9 @@ impl AomEncoder {
0x1 as _,
);
}
let pixfmt = if i444 { Pixfmt::I444 } else { Pixfmt::I420 };
EncodeYuvFormat {
pixfmt: extra.pixfmt,
range: extra.range,
pixfmt,
w: img.w as _,
h: img.h as _,
stride: img.stride.map(|s| s as usize).to_vec(),
@@ -599,22 +570,8 @@ impl GoogleImage for Image {
fn chroma(&self) -> Chroma {
match self.inner().fmt {
aom_img_fmt::AOM_IMG_FMT_I444 => Chroma::C444,
_ => Chroma::C420,
}
}
fn pixfmt(&self) -> Pixfmt {
match self.inner().fmt {
aom_img_fmt::AOM_IMG_FMT_I444 => Pixfmt::YUV444P,
_ => Pixfmt::YUV420P,
}
}
fn range(&self) -> ColorRange {
match self.inner().range {
aom_color_range::AOM_CR_STUDIO_RANGE => ColorRange::Studio,
aom_color_range::AOM_CR_FULL_RANGE => ColorRange::Full,
aom_img_fmt::AOM_IMG_FMT_I444 => Chroma::I444,
_ => Chroma::I420,
}
}
}