History

avcodec/mpegvideo: Move ratecontrol-adjacent fields to MPVMainEncContext

Andreas Rheinhardt committed 1 year ago

Commit d1fd8661 parent b7106985 files · +44 −39Browse files

Unified · Split

5 files
  1. libavcodec/mpegvideo.h +0 −6
  2. libavcodec/mpegvideo_enc.c +21 −19
  3. libavcodec/mpegvideoenc.h +11 −4
  4. libavcodec/ratecontrol.c +10 −8
  5. libavcodec/snowenc.c +2 −2
libavcodec/mpegvideo.h+0−6
@@ -101,7 +101,6 @@ typedef struct MpegEncContext { · 100 unmodified lines
101101 int h263_flv; ///< use flv H.263 header
102102
103103 enum AVCodecID codec_id; /* see AV_CODEC_ID_xxx */
104− int fixed_qscale; ///< fixed qscale if non zero
105104 int encoding; ///< true if we are encoding (vs decoding)
106105 int max_b_frames; ///< max number of B-frames for encoding
107106 int luma_elim_threshold;
@@ -478,10 +477,6 @@ typedef struct MpegEncContext { · 370 unmodified lines
478477 me_cmp_func sse_cmp[2];
479478 int (*sum_abs_dctelem)(const int16_t *block);
480479
481− float border_masking;
482− int lmin, lmax;
483− int vbv_ignore_qmax;
484−
485480 /// Bitfield containing information which frames to reconstruct.
486481 int frame_reconstruction_bitfield;
487482
@@ -493,7 +488,6 @@ typedef struct MpegEncContext { · 5 unmodified lines
493488
494489 int error_rate;
495490
496− int scenechange_threshold;
497491 int noise_reduction;
498492
499493 int intra_penalty;
libavcodec/mpegvideo_enc.c+21−19
@@ -195,8 +195,10 @@ void ff_convert_matrix(MpegEncContext *s, int (*qmat)[64], · 194 unmodified lines
195195 }
196196 }
197197
198−static inline void update_qscale(MpegEncContext *s)
198+static inline void update_qscale(MPVMainEncContext *const m)
199199 {
200+ MpegEncContext *const s = &m->s;
201+
200202 if (s->q_scale_type == 1 && 0) {
201203 int i;
202204 int bestdiff=INT_MAX;
@@ -205,7 +207,7 @@ static inline void update_qscale(MpegEncContext *s) · 2 unmodified lines
205207 for (i = 0 ; i<FF_ARRAY_ELEMS(ff_mpeg2_non_linear_qscale); i++) {
206208 int diff = FFABS((ff_mpeg2_non_linear_qscale[i]<<(FF_LAMBDA_SHIFT + 6)) - (int)s->lambda * 139);
207209 if (ff_mpeg2_non_linear_qscale[i] < s->avctx->qmin ||
208− (ff_mpeg2_non_linear_qscale[i] > s->avctx->qmax && !s->vbv_ignore_qmax))
210+ (ff_mpeg2_non_linear_qscale[i] > s->avctx->qmax && !m->vbv_ignore_qmax))
209211 continue;
210212 if (diff < bestdiff) {
211213 bestdiff = diff;
@@ -216,7 +218,7 @@ static inline void update_qscale(MpegEncContext *s) · 4 unmodified lines
216218 } else {
217219 s->qscale = (s->lambda * 139 + FF_LAMBDA_SCALE * 64) >>
218220 (FF_LAMBDA_SHIFT + 7);
219− s->qscale = av_clip(s->qscale, s->avctx->qmin, s->vbv_ignore_qmax ? 31 : s->avctx->qmax);
221+ s->qscale = av_clip(s->qscale, s->avctx->qmin, m->vbv_ignore_qmax ? 31 : s->avctx->qmax);
220222 }
221223
222224 s->lambda2 = (s->lambda * s->lambda + FF_LAMBDA_SCALE / 2) >>
@@ -507,16 +509,16 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 284 unmodified lines
507509 }
508510
509511 /* Fixed QSCALE */
510− s->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
512+ m->fixed_qscale = !!(avctx->flags & AV_CODEC_FLAG_QSCALE);
511513
512514 s->adaptive_quant = (avctx->lumi_masking ||
513515 avctx->dark_masking ||
514516 avctx->temporal_cplx_masking ||
515517 avctx->spatial_cplx_masking ||
516518 avctx->p_masking ||
517− s->border_masking ||
519+ m->border_masking ||
518520 (s->mpv_flags & FF_MPV_FLAG_QP_RD)) &&
519− !s->fixed_qscale;
521+ !m->fixed_qscale;
520522
521523 s->loop_filter = !!(avctx->flags & AV_CODEC_FLAG_LOOP_FILTER);
522524
@@ -580,7 +582,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 57 unmodified lines
580582 return AVERROR(EINVAL);
581583 }
582584
583− if (!s->fixed_qscale &&
585+ if (!m->fixed_qscale &&
584586 avctx->bit_rate * av_q2d(avctx->time_base) >
585587 avctx->bit_rate_tolerance) {
586588 double nbt = avctx->bit_rate * av_q2d(avctx->time_base) * 5;
@@ -675,7 +677,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 88 unmodified lines
675677 return AVERROR(EINVAL);
676678 }
677679
678− if (s->scenechange_threshold < 1000000000 &&
680+ if (m->scenechange_threshold < 1000000000 &&
679681 (avctx->flags & AV_CODEC_FLAG_CLOSED_GOP)) {
680682 av_log(avctx, AV_LOG_ERROR,
681683 "closed gop with scene change detection are not supported yet, "
@@ -890,9 +892,9 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 208 unmodified lines
890892 s->frame_reconstruction_bitfield = 0;
891893 }
892894
893− if (s->lmin > s->lmax) {
894− av_log(avctx, AV_LOG_WARNING, "Clipping lmin value to %d\n", s->lmax);
895− s->lmin = s->lmax;
895+ if (m->lmin > m->lmax) {
896+ av_log(avctx, AV_LOG_WARNING, "Clipping lmin value to %d\n", m->lmax);
897+ m->lmin = m->lmax;
896898 }
897899
898900 /* init */
@@ -1876,7 +1878,7 @@ int ff_mpv_encode_picture(AVCodecContext *avctx, AVPacket *pkt, · 977 unmodified lines
18761878
18771879 ff_mpv_unref_picture(&s->cur_pic);
18781880
1879− s->vbv_ignore_qmax = 0;
1881+ m->vbv_ignore_qmax = 0;
18801882
18811883 m->picture_in_gop_number++;
18821884
@@ -1935,7 +1937,7 @@ vbv_retry: · 52 unmodified lines
19351937 int min_step = hq ? 1 : (1<<(FF_LAMBDA_SHIFT + 7))/139;
19361938
19371939 if (put_bits_count(&s->pb) > max_size &&
1938− s->lambda < s->lmax) {
1940+ s->lambda < m->lmax) {
19391941 m->next_lambda = FFMAX(s->lambda + min_step, s->lambda *
19401942 (s->qscale + 1) / s->qscale);
19411943 if (s->adaptive_quant) {
@@ -1955,7 +1957,7 @@ vbv_retry: · 13 unmodified lines
19551957 s->time_base = s->last_time_base;
19561958 s->last_non_b_time = s->time - s->pp_time;
19571959 }
1958− s->vbv_ignore_qmax = 1;
1960+ m->vbv_ignore_qmax = 1;
19591961 av_log(avctx, AV_LOG_VERBOSE, "reencoding frame due to VBV\n");
19601962 goto vbv_retry;
19611963 }
@@ -3616,7 +3618,7 @@ static int estimate_qp(MPVMainEncContext *const m, int dry_run) · 1654 unmodified lines
36163618 if (m->next_lambda){
36173619 s->cur_pic.ptr->f->quality = m->next_lambda;
36183620 if(!dry_run) m->next_lambda= 0;
3619− } else if (!s->fixed_qscale) {
3621+ } else if (!m->fixed_qscale) {
36203622 int quality = ff_rate_estimate_qscale(m, dry_run);
36213623 s->cur_pic.ptr->f->quality = quality;
36223624 if (s->cur_pic.ptr->f->quality < 0)
@@ -3643,7 +3645,7 @@ static int estimate_qp(MPVMainEncContext *const m, int dry_run) · 20 unmodified lines
36433645 //FIXME broken
36443646 }else
36453647 s->lambda = s->cur_pic.ptr->f->quality;
3646− update_qscale(s);
3648+ update_qscale(m);
36473649 return 0;
36483650 }
36493651
@@ -3700,7 +3702,7 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt) · 50 unmodified lines
37003702 s->lambda= s->last_lambda_for[s->pict_type];
37013703 else
37023704 s->lambda= s->last_lambda_for[s->last_non_b_pict_type];
3703− update_qscale(s);
3705+ update_qscale(m);
37043706 }
37053707
37063708 ff_me_init_pic(s);
@@ -3742,7 +3744,7 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt) · 35 unmodified lines
37423744 for(i=0; i<s->mb_stride*s->mb_height; i++)
37433745 s->mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
37443746
3745− if(!s->fixed_qscale){
3747+ if (!m->fixed_qscale) {
37463748 /* finding spatial complexity for I-frame rate control */
37473749 s->avctx->execute(s->avctx, mb_var_thread, &s->thread_context[0], NULL, context_count, sizeof(void*));
37483750 }
@@ -3754,7 +3756,7 @@ static int encode_picture(MPVMainEncContext *const m, const AVPacket *pkt) · 5 unmodified lines
37543756 s->mb_var_sum = s->me. mb_var_sum_temp;
37553757 emms_c();
37563758
3757− if (s->me.scene_change_score > s->scenechange_threshold &&
3759+ if (s->me.scene_change_score > m->scenechange_threshold &&
37583760 s->pict_type == AV_PICTURE_TYPE_P) {
37593761 s->pict_type= AV_PICTURE_TYPE_I;
37603762 for(i=0; i<s->mb_stride*s->mb_height; i++)
libavcodec/mpegvideoenc.h+11−4
@@ -61,6 +61,12 @@ typedef struct MPVMainEncContext { · 60 unmodified lines
6161 int b_sensitivity;
6262 int brd_scale;
6363
64+ int scenechange_threshold;
65+
66+ float border_masking;
67+ int lmin, lmax;
68+ int vbv_ignore_qmax;
69+
6470 /* frame skip options */
6571 int frame_skip_threshold;
6672 int frame_skip_factor;
@@ -73,6 +79,7 @@ typedef struct MPVMainEncContext { · 6 unmodified lines
7379 int frame_bits; ///< bits used for the current frame
7480 int stuffing_bits; ///< bits used for stuffing
7581 int next_lambda; ///< next lambda used for retrying to encode a frame
82+ int fixed_qscale; ///< fixed qscale if non zero
7683 RateControlContext rc_context; ///< contains stuff only accessed in ratecontrol.c
7784 } MPVMainEncContext;
7885
@@ -154,14 +161,14 @@ FF_MPV_OPT_CMP_FUNC, \ · 75 unmodified lines
154161 FF_RC_OFFSET(rc_eq), AV_OPT_TYPE_STRING, .flags = FF_MPV_OPT_FLAGS }, \
155162 {"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}, \
156163 {"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}, \
157−{"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}, \
158−{"lmin", "minimum Lagrange factor (VBR)", FF_MPV_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
159−{"lmax", "maximum Lagrange factor (VBR)", FF_MPV_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
164+{"border_mask", "increase the quantizer for macroblocks close to borders", FF_MPV_MAIN_OFFSET(border_masking), AV_OPT_TYPE_FLOAT, {.dbl = 0 }, -FLT_MAX, FLT_MAX, FF_MPV_OPT_FLAGS}, \
165+{"lmin", "minimum Lagrange factor (VBR)", FF_MPV_MAIN_OFFSET(lmin), AV_OPT_TYPE_INT, {.i64 = 2*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
166+{"lmax", "maximum Lagrange factor (VBR)", FF_MPV_MAIN_OFFSET(lmax), AV_OPT_TYPE_INT, {.i64 = 31*FF_QP2LAMBDA }, 0, INT_MAX, FF_MPV_OPT_FLAGS }, \
160167 {"skip_threshold", "Frame skip threshold", FF_MPV_MAIN_OFFSET(frame_skip_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
161168 {"skip_factor", "Frame skip factor", FF_MPV_MAIN_OFFSET(frame_skip_factor), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
162169 {"skip_exp", "Frame skip exponent", FF_MPV_MAIN_OFFSET(frame_skip_exp), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
163170 {"skip_cmp", "Frame skip compare function", FF_MPV_MAIN_OFFSET(frame_skip_cmp), AV_OPT_TYPE_INT, {.i64 = FF_CMP_DCTMAX }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS, .unit = "cmp_func" }, \
164−{"sc_threshold", "Scene change threshold", FF_MPV_OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
171+{"sc_threshold", "Scene change threshold", FF_MPV_MAIN_OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
165172 {"noise_reduction", "Noise reduction", FF_MPV_OFFSET(noise_reduction), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
166173 {"ps", "RTP payload size in bytes", FF_MPV_OFFSET(rtp_payload_size), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, FF_MPV_OPT_FLAGS }, \
167174
libavcodec/ratecontrol.c+10−8
@@ -142,10 +142,11 @@ static double get_diff_limited_q(MPVMainEncContext *m, const RateControlEntry *r · 141 unmodified lines
142142 /**
143143 * Get the qmin & qmax for pict_type.
144144 */
145−static void get_qminmax(int *qmin_ret, int *qmax_ret, MpegEncContext *s, int pict_type)
145+static void get_qminmax(int *qmin_ret, int *qmax_ret, MPVMainEncContext *const m, int pict_type)
146146 {
147− int qmin = s->lmin;
148− int qmax = s->lmax;
147+ MpegEncContext *const s = &m->s;
148+ int qmin = m->lmin;
149+ int qmax = m->lmax;
149150
150151 av_assert0(qmin <= qmax);
151152
@@ -182,7 +183,7 @@ static double modify_qscale(MPVMainEncContext *const m, const RateControlEntry * · 30 unmodified lines
182183 const int pict_type = rce->new_pict_type;
183184 int qmin, qmax;
184185
185− get_qminmax(&qmin, &qmax, s, pict_type);
186+ get_qminmax(&qmin, &qmax, m, pict_type);
186187
187188 /* modulation */
188189 if (rcc->qmod_freq &&
@@ -784,15 +785,16 @@ static void update_predictor(Predictor *p, double q, double var, double size) · 595 unmodified lines
784785 }
785786
786787 static void adaptive_quantization(RateControlContext *const rcc,
787− MpegEncContext *const s, double q)
788+ MPVMainEncContext *const m, double q)
788789 {
790+ MpegEncContext *const s = &m->s;
789791 int i;
790792 const float lumi_masking = s->avctx->lumi_masking / (128.0 * 128.0);
791793 const float dark_masking = s->avctx->dark_masking / (128.0 * 128.0);
792794 const float temp_cplx_masking = s->avctx->temporal_cplx_masking;
793795 const float spatial_cplx_masking = s->avctx->spatial_cplx_masking;
794796 const float p_masking = s->avctx->p_masking;
795− const float border_masking = s->border_masking;
797+ const float border_masking = m->border_masking;
796798 float bits_sum = 0.0;
797799 float cplx_sum = 0.0;
798800 float *cplx_tab = rcc->cplx_tab;
@@ -932,7 +934,7 @@ float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run) · 133 unmodified lines
932934 const int pict_type = s->pict_type;
933935 emms_c();
934936
935− get_qminmax(&qmin, &qmax, s, pict_type);
937+ get_qminmax(&qmin, &qmax, m, pict_type);
936938
937939 fps = get_fps(s->avctx);
938940 /* update predictors */
@@ -1068,7 +1070,7 @@ float ff_rate_estimate_qscale(MPVMainEncContext *const m, int dry_run) · 129 unmodified lines
10681070 q = qmax;
10691071
10701072 if (s->adaptive_quant)
1071− adaptive_quantization(rcc, s, q);
1073+ adaptive_quantization(rcc, m, q);
10721074 else
10731075 q = (int)(q + 0.5);
10741076
libavcodec/snowenc.c+2−2
@@ -228,8 +228,8 @@ static av_cold int encode_init(AVCodecContext *avctx) · 227 unmodified lines
228228
229229 mpv->avctx = avctx;
230230 mpv->bit_rate= avctx->bit_rate;
231− mpv->lmin = avctx->mb_lmin;
232− mpv->lmax = avctx->mb_lmax;
231+ enc->m.lmin = avctx->mb_lmin;
232+ enc->m.lmax = avctx->mb_lmax;
233233 mpv->mb_num = (avctx->width * avctx->height + 255) / 256; // For ratecontrol
234234
235235 mpv->me.temp =

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