mirror of
https://github.com/pion/mediadevices.git
synced 2025-11-01 20:32:38 +08:00
Move from CRF to ABR+VBV
This commit is contained in:
@@ -37,8 +37,10 @@ Encoder *enc_new(x264_param_t param, int* rc) {
|
||||
// Intra refres:
|
||||
e->param.i_keyint_max = param.i_keyint_max;
|
||||
// Rate control:
|
||||
e->param.rc.i_rc_method = X264_RC_CRF;
|
||||
e->param.rc.f_rf_constant = param.rc.f_rf_constant;
|
||||
e->param.rc.i_rc_method = X264_RC_ABR;
|
||||
e->param.rc.i_bitrate = param.rc.i_bitrate;
|
||||
e->param.rc.i_vbv_max_bitrate = param.rc.i_vbv_max_bitrate;
|
||||
e->param.rc.i_vbv_buffer_size = param.rc.i_vbv_buffer_size;
|
||||
// For streaming:
|
||||
e->param.b_repeat_headers = 1;
|
||||
e->param.b_annexb = 1;
|
||||
|
||||
@@ -2,8 +2,4 @@ package x264
|
||||
|
||||
// Params stores libx264 specific encoding parameters.
|
||||
type Params struct {
|
||||
// Quality of the encoding [0-9].
|
||||
// Larger value results higher quality and higher CPU usage.
|
||||
// It depends on the selected codec.
|
||||
Quality int
|
||||
}
|
||||
|
||||
@@ -21,14 +21,6 @@ import (
|
||||
"github.com/pion/webrtc/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
// maxRF is a limit for x264 compression level
|
||||
// TODO: Probably remove this hardcoded value.
|
||||
// I only saw that 51 was also hardcoded in their source.
|
||||
maxRF = 51
|
||||
maxQuality = 10
|
||||
)
|
||||
|
||||
type encoder struct {
|
||||
engine *C.Encoder
|
||||
buff []byte
|
||||
@@ -81,33 +73,22 @@ func newEncoder(r video.Reader, p prop.Media) (io.ReadCloser, error) {
|
||||
p.KeyFrameInterval = 60
|
||||
}
|
||||
|
||||
quality := 5
|
||||
switch cp := p.CodecParams.(type) {
|
||||
switch p.CodecParams.(type) {
|
||||
case nil:
|
||||
case Params:
|
||||
quality = cp.Quality
|
||||
default:
|
||||
return nil, errors.New("unsupported CodecParams type")
|
||||
}
|
||||
|
||||
var rf C.float
|
||||
// first reverse quality value since rf is the inverse of Quality,
|
||||
// and add 1 to map [1,maxQuality] to [maxQuality-1,0]
|
||||
rf = C.float(maxQuality - quality)
|
||||
// Then, map to x264 RF range, [0,maxQuality-1] to [1,maxRF].
|
||||
// [1,maxRF] because 0 is lossless and constrained baseline doesn't support
|
||||
// lossless.
|
||||
rf *= (maxRF - 1)
|
||||
rf /= (maxQuality - 1)
|
||||
rf++
|
||||
|
||||
param := C.x264_param_t{
|
||||
i_csp: C.X264_CSP_I420,
|
||||
i_width: C.int(p.Width),
|
||||
i_height: C.int(p.Height),
|
||||
i_keyint_max: C.int(p.KeyFrameInterval),
|
||||
}
|
||||
param.rc.f_rf_constant = rf
|
||||
param.rc.i_bitrate = C.int(p.BitRate)
|
||||
param.rc.i_vbv_max_bitrate = param.rc.i_bitrate
|
||||
param.rc.i_vbv_buffer_size = param.rc.i_vbv_max_bitrate * 2
|
||||
|
||||
var rc C.int
|
||||
engine := C.enc_new(param, &rc)
|
||||
|
||||
Reference in New Issue
Block a user