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
507507 me_cmp_func sse_cmp[2];
508508 int (*sum_abs_dctelem)(const int16_t *block);
509509
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;
519510 float border_masking;
520511 int lmin, lmax;
521512 int vbv_ignore_qmax;
522513
523− char *rc_eq;
524−
525514 /* temp buffers for rate control */
526515 float *cplx_tab, *bits_tab;
527516
libavcodec/mpegvideoenc.h+7−6
@@ -81,6 +81,7 @@ · 80 unmodified lines
8181 { "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" }
8282
8383 #define FF_MPV_OFFSET(x) offsetof(MpegEncContext, x)
84+#define FF_RC_OFFSET(x) offsetof(MpegEncContext, rc_context.x)
8485 #define FF_MPV_OPT_FLAGS (AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
8586 #define FF_MPV_COMMON_OPTS \
8687 FF_MPV_OPT_CMP_FUNC, \
@@ -99,16 +100,16 @@ FF_MPV_OPT_CMP_FUNC, \ · 12 unmodified lines
99100 { "error_rate", "Simulate errors in the bitstream to test error concealment.", \
100101 FF_MPV_OFFSET(error_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FF_MPV_OPT_FLAGS },\
101102 {"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−{"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_freq", "experimental quantizer modulation", FF_MPV_OFFSET(rc_qmod_freq), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS}, \
103+ FF_RC_OFFSET(qsquish), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, 0, 99, 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}, \
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}, \
105106 {"rc_eq", "Set rate control equation. When computing the expression, besides the standard functions " \
106107 "defined in the section 'Expression Evaluation', the following functions are available: " \
107108 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv " \
108109 "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−{"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_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}, \
110+ FF_RC_OFFSET(rc_eq), AV_OPT_TYPE_STRING, .flags = 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}, \
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}, \
112113 {"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}, \
113114 {"lmin", "minimum Lagrange factor (VBR)", FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
114115 {"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
182182 get_qminmax(&qmin, &qmax, s, pict_type);
183183
184184 /* modulation */
185− if (s->rc_qmod_freq &&
186− frame_num % s->rc_qmod_freq == 0 &&
185+ if (rcc->qmod_freq &&
186+ frame_num % rcc->qmod_freq == 0 &&
187187 pict_type == AV_PICTURE_TYPE_P)
188− q *= s->rc_qmod_amp;
188+ q *= rcc->qmod_amp;
189189
190190 /* buffer overflow/underflow protection */
191191 if (buffer_size) {
@@ -198,7 +198,7 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 6 unmodified lines
198198 d = 1.0;
199199 else if (d < 0.0001)
200200 d = 0.0001;
201− q *= pow(d, 1.0 / s->rc_buffer_aggressivity);
201+ q *= pow(d, 1.0 / rcc->buffer_aggressivity);
202202
203203 q_limit = bits2qp(rce,
204204 FFMAX((min_rate - buffer_size + rcc->buffer_index) *
@@ -218,7 +218,7 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 13 unmodified lines
218218 d = 1.0;
219219 else if (d < 0.0001)
220220 d = 0.0001;
221− q /= pow(d, 1.0 / s->rc_buffer_aggressivity);
221+ q /= pow(d, 1.0 / rcc->buffer_aggressivity);
222222
223223 q_limit = bits2qp(rce,
224224 FFMAX(rcc->buffer_index *
@@ -234,8 +234,8 @@ static double modify_qscale(MpegEncContext *s, const RateControlEntry *rce, · 9 unmodified lines
234234 }
235235 ff_dlog(s, "q:%f max:%f min:%f size:%f index:%f agr:%f\n",
236236 q, max_rate, min_rate, buffer_size, rcc->buffer_index,
237− s->rc_buffer_aggressivity);
238− if (s->rc_qsquish == 0.0 || qmin == qmax) {
237+ rcc->buffer_aggressivity);
238+ if (rcc->qsquish == 0.0 || qmin == qmax) {
239239 if (q < qmin)
240240 q = qmin;
241241 else if (q > qmax)
@@ -295,7 +295,7 @@ static double get_qscale(MpegEncContext *s, RateControlEntry *rce, · 53 unmodified lines
295295
296296 bits = av_expr_eval(rcc->rc_eq_eval, const_values, rce);
297297 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);
299299 return -1;
300300 }
301301
@@ -541,11 +541,11 @@ av_cold int ff_rate_control_init(MpegEncContext *s) · 239 unmodified lines
541541 }
542542
543543 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",
545545 const_names, func1_names, func1,
546546 NULL, NULL, 0, s->avctx);
547547 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);
549549 return res;
550550 }
551551
@@ -649,9 +649,9 @@ av_cold int ff_rate_control_init(MpegEncContext *s) · 97 unmodified lines
649649 return -1;
650650 }
651651 /* init stuff with the user specified complexity */
652− if (s->rc_initial_cplx) {
652+ if (rcc->initial_cplx) {
653653 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;
655655 RateControlEntry rce;
656656
657657 if (i % ((s->gop_size + 3) / 4) == 0)
@@ -701,6 +701,7 @@ av_cold void ff_rate_control_uninit(RateControlContext *rcc) · 43 unmodified lines
701701 {
702702 emms_c();
703703
704+ // rc_eq is always managed via an AVOption and therefore not freed here.
704705 av_expr_free(rcc->rc_eq_eval);
705706 rcc->rc_eq_eval = NULL;
706707 av_freep(&rcc->entry);
libavcodec/ratecontrol.h+11−0
@@ -77,6 +77,17 @@ typedef struct RateControlContext{ · 76 unmodified lines
7777 int frame_count[5];
7878 int last_non_b_pict_type;
7979
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;
8091 struct AVExpr *rc_eq_eval;
8192 }RateControlContext;
8293
libavcodec/snowenc.c+1−1
@@ -2118,7 +2118,7 @@ static const AVOption options[] = { · 2117 unmodified lines
21182118 "defined in the section 'Expression Evaluation', the following functions are available: "
21192119 "bits2qp(bits), qp2bits(qp). Also the following constants are available: iTex pTex tex mv "
21202120 "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 },
21222122 { NULL },
21232123 };
21242124

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