History

avcodec/mpegvideo: Move ratecontrol-only options to RateControlContext

Andreas Rheinhardt committed 1 year ago

Commit 6844e961 parent 1ec1f1e5 files · +32 −30Browse files

Unified · Split

5 files
  1. libavcodec/mpegvideo.h +0 −11
  2. libavcodec/mpegvideoenc.h +7 −6
  3. libavcodec/ratecontrol.c +13 −12
  4. libavcodec/ratecontrol.h +11 −0
  5. libavcodec/snowenc.c +1 −1
libavcodec/mpegvideo.h+0−11
@@ -507,21 +507,10 @@ typedef struct MpegEncContext { · 506 unmodified lines
507 me_cmp_func sse_cmp[2];507 me_cmp_func sse_cmp[2];
508 int (*sum_abs_dctelem)(const int16_t *block);508 int (*sum_abs_dctelem)(const int16_t *block);
509 509
510 /**
511 * ratecontrol qmin qmax limiting method
512 * 0-> clipping, 1-> use a nice continuous function to limit qscale within qmin/qmax.
513 */
514 float rc_qsquish;
515 float rc_qmod_amp;
516 int rc_qmod_freq;
517 float rc_initial_cplx;
518 float rc_buffer_aggressivity;
519 float border_masking;510 float border_masking;
520 int lmin, lmax;511 int lmin, lmax;
521 int vbv_ignore_qmax;512 int vbv_ignore_qmax;
522 513
523 char *rc_eq;
524
525 /* temp buffers for rate control */514 /* temp buffers for rate control */
526 float *cplx_tab, *bits_tab;515 float *cplx_tab, *bits_tab;
527 516
libavcodec/mpegvideoenc.h+7−6
@@ -81,6 +81,7 @@ · 80 unmodified lines
81{ "msad", "Sum of absolute differences, median predicted", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_MEDIAN_SAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }81{ "msad", "Sum of absolute differences, median predicted", 0, AV_OPT_TYPE_CONST, {.i64 = FF_CMP_MEDIAN_SAD }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }
82 82
83#define FF_MPV_OFFSET(x) offsetof(MpegEncContext, x)83#define FF_MPV_OFFSET(x) offsetof(MpegEncContext, x)
84#define FF_RC_OFFSET(x) offsetof(MpegEncContext, rc_context.x)
84#define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)85#define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
85#define FF_MPV_COMMON_OPTS \86#define FF_MPV_COMMON_OPTS \
86FF_MPV_OPT_CMP_FUNC, \87FF_MPV_OPT_CMP_FUNC, \
@@ -99,16 +100,16 @@ FF_MPV_OPT_CMP_FUNC, \ · 12 unmodified lines
99{ "error_rate", "Simulate errors in the bitstream to test error concealment.", \100{ "error_rate", "Simulate errors in the bitstream to test error concealment.", \
100 FF_MPV_OFFSET(error_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\101 FF_MPV_OFFSET(error_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\
101{"qsquish", "how to keep quantizer between qmin and qmax (0 = clip, 1 = use differentiable function)", \102{"qsquish", "how to keep quantizer between qmin and qmax (0 = clip, 1 = use differentiable function)", \
102 FF_MPV_OFFSET(rc_qsquish), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0, 99, FF_MPV_OPT_FLAGS}, \103 FF_RC_OFFSET(qsquish), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0, 99, FF_MPV_OPT_FLAGS}, \
103{"rc_qmod_amp", "experimental quantizer modulation", FF_MPV_OFFSET(rc_qmod_amp), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \104{"rc_qmod_amp", "experimental quantizer modulation", FF_RC_OFFSET(qmod_amp), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
104{"rc_qmod_freq", "experimental quantizer modulation", FF_MPV_OFFSET(rc_qmod_freq), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS}, \105{"rc_qmod_freq", "experimental quantizer modulation", FF_RC_OFFSET(qmod_freq), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS}, \
105{"rc_eq", "Set rate control equation. When computing the expression, besides the standard functions " \106{"rc_eq", "Set rate control equation. When computing the expression, besides the standard functions " \
106 "defined in the section 'Expression Evaluation', the following functions are available: " \107 "defined in the section 'Expression Evaluation', the following functions are available: " \
107 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv " \108 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv " \
108 "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.", \109 "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.", \
109 FF_MPV_OFFSET(rc_eq), AV_OPT_TYPE_STRING, .flags = FF_MPV_OPT_FLAGS }, \110 FF_RC_OFFSET(rc_eq), AV_OPT_TYPE_STRING, .flags = FF_MPV_OPT_FLAGS }, \
110{"rc_init_cplx", "initial complexity for 1-pass encoding", FF_MPV_OFFSET(rc_initial_cplx), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \111{"rc_init_cplx", "initial complexity for 1-pass encoding", FF_RC_OFFSET(initial_cplx), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
111{"rc_buf_aggressivity", "currently useless", FF_MPV_OFFSET(rc_buffer_aggressivity), AV_OPT_TYPE_FLOAT, {.dbl = 1.0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \112{"rc_buf_aggressivity", "currently useless", FF_RC_OFFSET(buffer_aggressivity), AV_OPT_TYPE_FLOAT, {.dbl = 1.0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
112{"border_mask", "increase the quantizer for macroblocks close to borders", FF_MPV_OFFSET(border_masking), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \113{"border_mask", "increase the quantizer for macroblocks close to borders", FF_MPV_OFFSET(border_masking), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
113{"lmin", "minimum Lagrange factor (VBR)", FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \114{"lmin", "minimum Lagrange factor (VBR)", FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
114{"lmax", "maximum Lagrange factor (VBR)", FF_MPV_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \115{"lmax", "maximum Lagrange factor (VBR)", FF_MPV_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
libavcodec/ratecontrol.c+13−12
@@ -182,10 +182,10 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 181 unmodified lines
182 get_qminmax(&qmin, &qmax, s, pict_type);182 get_qminmax(&qmin, &qmax, s, pict_type);
183 183
184 /* modulation */184 /* modulation */
185 if (s->rc_qmod_freq &&185 if (rcc->qmod_freq &&
186 frame_num % s->rc_qmod_freq == 0 &&186 frame_num % rcc->qmod_freq == 0 &&
187 pict_type == AV_PICTURE_TYPE_P)187 pict_type == AV_PICTURE_TYPE_P)
188 q *= s->rc_qmod_amp;188 q *= rcc->qmod_amp;
189 189
190 /* buffer overflow/underflow protection */190 /* buffer overflow/underflow protection */
191 if (buffer_size) {191 if (buffer_size) {
@@ -198,7 +198,7 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 6 unmodified lines
198 d = 1.0;198 d = 1.0;
199 else if (d < 0.0001)199 else if (d < 0.0001)
200 d = 0.0001;200 d = 0.0001;
201 q *= pow(d, 1.0 / s->rc_buffer_aggressivity);201 q *= pow(d, 1.0 / rcc->buffer_aggressivity);
202 202
203 q_limit = bits2qp(rce,203 q_limit = bits2qp(rce,
204 FFMAX((min_rate - buffer_size + rcc->buffer_index) *204 FFMAX((min_rate - buffer_size + rcc->buffer_index) *
@@ -218,7 +218,7 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 13 unmodified lines
218 d = 1.0;218 d = 1.0;
219 else if (d < 0.0001)219 else if (d < 0.0001)
220 d = 0.0001;220 d = 0.0001;
221 q /= pow(d, 1.0 / s->rc_buffer_aggressivity);221 q /= pow(d, 1.0 / rcc->buffer_aggressivity);
222 222
223 q_limit = bits2qp(rce,223 q_limit = bits2qp(rce,
224 FFMAX(rcc->buffer_index *224 FFMAX(rcc->buffer_index *
@@ -234,8 +234,8 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 9 unmodified lines
234 }234 }
235 ff_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",235 ff_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
236 q, max_rate, min_rate, buffer_size, rcc->buffer_index,236 q, max_rate, min_rate, buffer_size, rcc->buffer_index,
237 s->rc_buffer_aggressivity);237 rcc->buffer_aggressivity);
238 if (s->rc_qsquish == 0.0 || qmin == qmax) {238 if (rcc->qsquish == 0.0 || qmin == qmax) {
239 if (q < qmin)239 if (q < qmin)
240 q = qmin;240 q = qmin;
241 else if (q > qmax)241 else if (q > qmax)
@@ -295,7 +295,7 @@ static double get_qscale(MpegEncContext *s, RateControlEntry *rce, · 53 unmodified lines
295 295
296 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);296 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
297 if (isnan(bits)) {297 if (isnan(bits)) {
298 av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", s->rc_eq);298 av_log(s->avctx, AV_LOG_ERROR, "Error evaluating rc_eq \"%s\"\n", rcc->rc_eq);
299 return -1;299 return -1;
300 }300 }
301 301
@@ -541,11 +541,11 @@ av_cold int ff_rate_control_init(MpegEncContext *s) · 239 unmodified lines
541 }541 }
542 542
543 res = av_expr_parse(&rcc->rc_eq_eval,543 res = av_expr_parse(&rcc->rc_eq_eval,
544 s->rc_eq ? s->rc_eq : "tex^qComp",544 rcc->rc_eq ? rcc->rc_eq : "tex^qComp",
545 const_names, func1_names, func1,545 const_names, func1_names, func1,
546 NULL, NULL, 0, s->avctx);546 NULL, NULL, 0, s->avctx);
547 if (res < 0) {547 if (res < 0) {
548 av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", s->rc_eq);548 av_log(s->avctx, AV_LOG_ERROR, "Error parsing rc_eq \"%s\"\n", rcc->rc_eq);
549 return res;549 return res;
550 }550 }
551 551
@@ -649,9 +649,9 @@ av_cold int ff_rate_control_init(MpegEncContext *s) · 97 unmodified lines
649 return -1;649 return -1;
650 }650 }
651 /* init stuff with the user specified complexity */651 /* init stuff with the user specified complexity */
652 if (s->rc_initial_cplx) {652 if (rcc->initial_cplx) {
653 for (i = 0; i < 60 * 30; i++) {653 for (i = 0; i < 60 * 30; i++) {
654 double bits = s->rc_initial_cplx * (i / 10000.0 + 1.0) * s->mb_num;654 double bits = rcc->initial_cplx * (i / 10000.0 + 1.0) * s->mb_num;
655 RateControlEntry rce;655 RateControlEntry rce;
656 656
657 if (i % ((s->gop_size + 3) / 4) == 0)657 if (i % ((s->gop_size + 3) / 4) == 0)
@@ -701,6 +701,7 @@ av_cold void ff_rate_control_uninit(RateControlContext *rcc) · 43 unmodified lines
701{701{
702 emms_c();702 emms_c();
703 703
704 // rc_eq is always managed via an AVOption and therefore not freed here.
704 av_expr_free(rcc->rc_eq_eval);705 av_expr_free(rcc->rc_eq_eval);
705 rcc->rc_eq_eval = NULL;706 rcc->rc_eq_eval = NULL;
706 av_freep(&rcc->entry);707 av_freep(&rcc->entry);
libavcodec/ratecontrol.h+11−0
@@ -77,6 +77,17 @@ typedef struct RateControlContext{ · 76 unmodified lines
77 int frame_count[5];77 int frame_count[5];
78 int last_non_b_pict_type;78 int last_non_b_pict_type;
79 79
80 /**
81 * ratecontrol qmin qmax limiting method
82 * 0-> clipping, 1-> use a nice continuous function to limit qscale within qmin/qmax.
83 */
84 float qsquish;
85 float qmod_amp;
86 int qmod_freq;
87 float initial_cplx;
88 float buffer_aggressivity;
89
90 char *rc_eq;
80 struct AVExpr *rc_eq_eval;91 struct AVExpr *rc_eq_eval;
81}RateControlContext;92}RateControlContext;
82 93
libavcodec/snowenc.c+1−1
@@ -2118,7 +2118,7 @@ static const AVOption options[] = { · 2117 unmodified lines
2118 "defined in the section 'Expression Evaluation', the following functions are available: "2118 "defined in the section 'Expression Evaluation', the following functions are available: "
2119 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv "2119 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv "
2120 "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.",2120 "fCode iCount mcVar var isI isP isB avgQP qComp avgIITex avgPITex avgPPTex avgBPTex avgTex.",
2121 OFFSET(m.rc_eq), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },2121 OFFSET(m.rc_context.rc_eq), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
2122 { NULL },2122 { NULL },
2123};2123};
2124 2124

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t