Skip to content

Commit

Permalink
fix mediacodec bad encoding quality (rustdesk#8159)
Browse files Browse the repository at this point in the history
Signed-off-by: 21pages <[email protected]>
  • Loading branch information
21pages authored May 27, 2024
1 parent 9ce62dc commit c7308db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 21 additions & 14 deletions libs/scrap/src/common/hwcodec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ use hbb_common::{
serde_json, ResultType,
};
use hwcodec::{
common::DataFormat,
common::{
DataFormat,
Quality::{self, *},
RateControl::{self, *},
},
ffmpeg::AVPixelFormat,
ffmpeg_ram::{
decode::{DecodeContext, DecodeFrame, Decoder},
encode::{EncodeContext, EncodeFrame, Encoder},
CodecInfo,
Quality::{self, *},
RateControl::{self, *},
},
};

const DEFAULT_PIXFMT: AVPixelFormat = AVPixelFormat::AV_PIX_FMT_NV12;
pub const DEFAULT_TIME_BASE: [i32; 2] = [1, 30];
const DEFAULT_GOP: i32 = i32::MAX;
const DEFAULT_HW_QUALITY: Quality = Quality_Default;
#[cfg(target_os = "android")]
const DEFAULT_RC: RateControl = RC_VBR; // android cbr poor quality
#[cfg(not(target_os = "android"))]
const DEFAULT_RC: RateControl = RC_CBR;

#[derive(Debug, Clone)]
pub struct HwRamEncoderConfig {
Expand All @@ -59,6 +57,7 @@ impl EncoderApi for HwRamEncoder {
{
match cfg {
EncoderCfg::HWRAM(config) => {
let rc = Self::rate_control(&config);
let b = Self::convert_quality(&config.name, config.quality);
let base_bitrate = base_bitrate(config.width as _, config.height as _);
let mut bitrate = base_bitrate * b / 100;
Expand All @@ -78,7 +77,8 @@ impl EncoderApi for HwRamEncoder {
timebase: DEFAULT_TIME_BASE,
gop,
quality: DEFAULT_HW_QUALITY,
rc: DEFAULT_RC,
rc,
q: -1,
thread_count: codec_thread_num(16) as _, // ffmpeg's thread_count is used for cpu
};
let format = match Encoder::format_from_name(config.name.clone()) {
Expand Down Expand Up @@ -175,6 +175,7 @@ impl EncoderApi for HwRamEncoder {
self.encoder.set_bitrate(bitrate as _).ok();
self.bitrate = bitrate;
}
self.config.quality = quality;
Ok(())
}

Expand Down Expand Up @@ -232,6 +233,14 @@ impl HwRamEncoder {
}
}

fn rate_control(config: &HwRamEncoderConfig) -> RateControl {
#[cfg(target_os = "android")]
if config.name.contains("mediacodec") {
return RC_VBR;
}
RC_CBR
}

pub fn convert_quality(name: &str, quality: crate::codec::Quality) -> u32 {
use crate::codec::Quality;
let quality = match quality {
Expand All @@ -241,11 +250,8 @@ impl HwRamEncoder {
Quality::Custom(b) => b,
};
let factor = if name.contains("mediacodec") {
if name.contains("h264") {
6
} else {
3
}
// https://stackoverflow.com/questions/26110337/what-are-valid-bit-rates-to-set-for-mediacodec?rq=3
5
} else {
1
};
Expand Down Expand Up @@ -510,7 +516,8 @@ pub fn check_available_hwcodec() {
timebase: DEFAULT_TIME_BASE,
gop: DEFAULT_GOP,
quality: DEFAULT_HW_QUALITY,
rc: DEFAULT_RC,
rc: RC_CBR,
q: -1,
thread_count: 4,
};
#[cfg(feature = "vram")]
Expand Down

0 comments on commit c7308db

Please sign in to comment.