scrap: use the same bitrate ratio control as vpx

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2022-06-06 14:53:29 +08:00
parent 4bb09865cf
commit 42546a7468
4 changed files with 21 additions and 45 deletions

2
Cargo.lock generated
View File

@ -2236,7 +2236,7 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hwcodec" name = "hwcodec"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/21pages/hwcodec#1cfe82f08f254e88f18cea1ad7c332f1c28dc1b9" source = "git+https://github.com/21pages/hwcodec#888af61b3e960d30ef1f2e49eb0bcf9f6f7a14ee"
dependencies = [ dependencies = [
"bindgen", "bindgen",
"cc", "cc",

View File

@ -12,7 +12,7 @@ use crate::vpxcodec::*;
use hbb_common::{ use hbb_common::{
anyhow::anyhow, anyhow::anyhow,
log, log,
message_proto::{video_frame, ImageQuality, Message, VP9s, VideoCodecState}, message_proto::{video_frame, Message, VP9s, VideoCodecState},
ResultType, ResultType,
}; };
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
@ -31,7 +31,7 @@ pub struct HwEncoderConfig {
pub codec_name: String, pub codec_name: String,
pub width: usize, pub width: usize,
pub height: usize, pub height: usize,
pub quallity: ImageQuality, pub bitrate_ratio: i32,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View File

@ -5,7 +5,7 @@ use crate::{
use hbb_common::{ use hbb_common::{
anyhow::{anyhow, Context}, anyhow::{anyhow, Context},
lazy_static, log, lazy_static, log,
message_proto::{H264s, H265s, ImageQuality, Message, VideoFrame, H264, H265}, message_proto::{H264s, H265s, Message, VideoFrame, H264, H265},
ResultType, ResultType,
}; };
use hwcodec::{ use hwcodec::{
@ -47,7 +47,7 @@ impl EncoderApi for HwEncoder {
match cfg { match cfg {
EncoderCfg::HW(config) => { EncoderCfg::HW(config) => {
let (bitrate, timebase, gop, quality, rc) = let (bitrate, timebase, gop, quality, rc) =
HwEncoder::convert_quality(&config.codec_name, config.quallity); HwEncoder::convert_quality(&config.codec_name, config.bitrate_ratio);
let ctx = EncodeContext { let ctx = EncodeContext {
name: config.codec_name.clone(), name: config.codec_name.clone(),
width: config.width as _, width: config.width as _,
@ -192,36 +192,26 @@ impl HwEncoder {
} }
} }
fn convert_quality(name: &str, q: ImageQuality) -> (i32, [i32; 2], i32, Quality, RateContorl) { fn convert_quality(
name: &str,
bitrate_ratio: i32,
) -> (i32, [i32; 2], i32, Quality, RateContorl) {
// TODO // TODO
let bitrate = if name.contains("qsv") { let mut bitrate = if name.contains("qsv") {
1_000_000 1_000_000
} else { } else {
2_000_000 2_000_000
}; };
match q { if bitrate_ratio > 0 && bitrate_ratio <= 200 {
ImageQuality::Low => ( bitrate = bitrate * bitrate_ratio / 100;
bitrate / 2, };
(
bitrate,
DEFAULT_TIME_BASE, DEFAULT_TIME_BASE,
DEFAULT_GOP, DEFAULT_GOP,
DEFAULT_HW_QUALITY, DEFAULT_HW_QUALITY,
DEFAULT_RC, DEFAULT_RC,
), )
ImageQuality::Best => (
bitrate * 2,
DEFAULT_TIME_BASE,
DEFAULT_GOP,
DEFAULT_HW_QUALITY,
DEFAULT_RC,
),
ImageQuality::NotSet | ImageQuality::Balanced => (
0,
DEFAULT_TIME_BASE,
DEFAULT_GOP,
DEFAULT_HW_QUALITY,
DEFAULT_RC,
),
}
} }
} }

View File

@ -186,7 +186,7 @@ fn run(sp: GenericService) -> ResultType<()> {
codec_name, codec_name,
width, width,
height, height,
quallity: convert_quality_back(q), bitrate_ratio: q >> 8,
}), }),
None => EncoderCfg::VPX(VpxEncoderConfig { None => EncoderCfg::VPX(VpxEncoderConfig {
width: width as _, width: width as _,
@ -516,20 +516,6 @@ fn convert_quality(q: i32) -> i32 {
} }
} }
fn convert_quality_back(q: i32) -> ImageQuality {
let q = q >> 8;
if q == 100 * 2 / 3 {
ImageQuality::Balanced
} else if q == 100 / 2 {
ImageQuality::Low
} else if q == 100 {
ImageQuality::Best
} else {
log::error!("Error convert quality:{}", q);
ImageQuality::Balanced
}
}
pub fn update_image_quality(id: i32, q: Option<i32>) { pub fn update_image_quality(id: i32, q: Option<i32>) {
match q { match q {
Some(q) => { Some(q) => {