avcodec/mpegpicture: Move encoding_error and mb_var_sum to MpegEncCtx

These fields are only ever set by the encoder for the current picture
and for no other picture. So only one set of these values needs to
exist, so move them to MpegEncContext.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-08-09 14:14:38 +02:00
parent d576b37fa7
commit 73ea6db0ba
6 changed files with 28 additions and 37 deletions

View File

@ -376,16 +376,11 @@ int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
}
dst->field_picture = src->field_picture;
dst->mb_var_sum = src->mb_var_sum;
dst->mc_mb_var_sum = src->mc_mb_var_sum;
dst->b_frame_score = src->b_frame_score;
dst->needs_realloc = src->needs_realloc;
dst->reference = src->reference;
dst->shared = src->shared;
memcpy(dst->encoding_error, src->encoding_error,
sizeof(dst->encoding_error));
return 0;
fail:
ff_mpeg_unref_picture(avctx, dst);

View File

@ -71,16 +71,11 @@ typedef struct Picture {
int field_picture; ///< whether or not the picture was encoded in separate fields
int64_t mb_var_sum; ///< sum of MB variance for current frame
int64_t mc_mb_var_sum; ///< motion compensated MB variance for current frame
int b_frame_score;
int needs_realloc; ///< Picture needs to be reallocated (eg due to a frame size change)
int reference;
int shared;
uint64_t encoding_error[MPEGVIDEO_MAX_PLANES];
} Picture;
/**

View File

@ -236,10 +236,13 @@ typedef struct MpegEncContext {
uint8_t (*p_field_select_table[2]); ///< Only the first element is allocated
uint8_t (*b_field_select_table[2][2]); ///< Only the first element is allocated
/* The following three arrays are encoder-only */
/* The following fields are encoder-only */
uint16_t *mb_var; ///< Table for MB variances
uint16_t *mc_mb_var; ///< Table for motion compensated MB variances
uint8_t *mb_mean; ///< Table for MB luminance
int64_t mb_var_sum; ///< sum of MB variance for current frame
int64_t mc_mb_var_sum; ///< motion compensated MB variance for current frame
uint64_t encoding_error[MPEGVIDEO_MAX_PLANES];
int motion_est; ///< ME algorithm
int me_penalty_compensation;

View File

@ -1783,11 +1783,10 @@ vbv_retry:
ff_write_pass1_stats(s);
for (i = 0; i < 4; i++) {
s->current_picture_ptr->encoding_error[i] = s->current_picture.encoding_error[i];
avctx->error[i] += s->current_picture_ptr->encoding_error[i];
avctx->error[i] += s->encoding_error[i];
}
ff_side_data_set_encoder_stats(pkt, s->current_picture.f->quality,
s->current_picture_ptr->encoding_error,
s->encoding_error,
(avctx->flags&AV_CODEC_FLAG_PSNR) ? MPEGVIDEO_MAX_PLANES : 0,
s->pict_type);
@ -2792,7 +2791,7 @@ static int encode_thread(AVCodecContext *c, void *arg){
/* note: quant matrix value (8) is implied here */
s->last_dc[i] = 128 << s->intra_dc_precision;
s->current_picture.encoding_error[i] = 0;
s->encoding_error[i] = 0;
}
if(s->codec_id==AV_CODEC_ID_AMV){
s->last_dc[0] = 128*8/13;
@ -3370,13 +3369,13 @@ static int encode_thread(AVCodecContext *c, void *arg){
if(s->mb_x*16 + 16 > s->width ) w= s->width - s->mb_x*16;
if(s->mb_y*16 + 16 > s->height) h= s->height- s->mb_y*16;
s->current_picture.encoding_error[0] += sse(
s->encoding_error[0] += sse(
s, s->new_picture->data[0] + s->mb_x*16 + s->mb_y*s->linesize*16,
s->dest[0], w, h, s->linesize);
s->current_picture.encoding_error[1] += sse(
s->encoding_error[1] += sse(
s, s->new_picture->data[1] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
s->dest[1], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
s->current_picture.encoding_error[2] += sse(
s->encoding_error[2] += sse(
s, s->new_picture->data[2] + s->mb_x*8 + s->mb_y*s->uvlinesize*chr_h,
s->dest[2], w>>1, h>>s->chroma_y_shift, s->uvlinesize);
}
@ -3416,9 +3415,9 @@ static void merge_context_after_encode(MpegEncContext *dst, MpegEncContext *src)
MERGE(i_count);
MERGE(skip_count);
MERGE(misc_bits);
MERGE(current_picture.encoding_error[0]);
MERGE(current_picture.encoding_error[1]);
MERGE(current_picture.encoding_error[2]);
MERGE(encoding_error[0]);
MERGE(encoding_error[1]);
MERGE(encoding_error[2]);
if (dst->noise_reduction){
for(i=0; i<64; i++){
@ -3570,8 +3569,8 @@ static int encode_picture(MpegEncContext *s, int picture_number)
for(i=1; i<context_count; i++){
merge_context_after_me(s, s->thread_context[i]);
}
s->current_picture.mc_mb_var_sum= s->current_picture_ptr->mc_mb_var_sum= s->me.mc_mb_var_sum_temp;
s->current_picture. mb_var_sum= s->current_picture_ptr-> mb_var_sum= s->me. mb_var_sum_temp;
s->mc_mb_var_sum = s->me.mc_mb_var_sum_temp;
s->mb_var_sum = s->me. mb_var_sum_temp;
emms_c();
if (s->me.scene_change_score > s->scenechange_threshold &&
@ -3582,7 +3581,7 @@ static int encode_picture(MpegEncContext *s, int picture_number)
if(s->msmpeg4_version >= 3)
s->no_rounding=1;
ff_dlog(s, "Scene change detected, encoding as I Frame %"PRId64" %"PRId64"\n",
s->current_picture.mb_var_sum, s->current_picture.mc_mb_var_sum);
s->mb_var_sum, s->mc_mb_var_sum);
}
if(!s->umvplus){

View File

@ -49,8 +49,8 @@ void ff_write_pass1_stats(MpegEncContext *s)
s->misc_bits,
s->f_code,
s->b_code,
s->current_picture.mc_mb_var_sum,
s->current_picture.mb_var_sum,
s->mc_mb_var_sum,
s->mb_var_sum,
s->i_count, s->skip_count,
s->header_bits);
}
@ -880,7 +880,6 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
double rate_factor;
int64_t var;
const int pict_type = s->pict_type;
Picture * const pic = &s->current_picture;
emms_c();
get_qminmax(&qmin, &qmax, s, pict_type);
@ -929,7 +928,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
if (br_compensation <= 0.0)
br_compensation = 0.001;
var = pict_type == AV_PICTURE_TYPE_I ? pic->mb_var_sum : pic->mc_mb_var_sum;
var = pict_type == AV_PICTURE_TYPE_I ? s->mb_var_sum : s->mc_mb_var_sum;
short_term_q = 0; /* avoid warning */
if (s->avctx->flags & AV_CODEC_FLAG_PASS2) {
@ -942,8 +941,8 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
} else {
rce->pict_type =
rce->new_pict_type = pict_type;
rce->mc_mb_var_sum = pic->mc_mb_var_sum;
rce->mb_var_sum = pic->mb_var_sum;
rce->mc_mb_var_sum = s->mc_mb_var_sum;
rce->mb_var_sum = s->mb_var_sum;
rce->qscale = FF_QP2LAMBDA * 2;
rce->f_code = s->f_code;
rce->b_code = s->b_code;
@ -1003,7 +1002,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
qmin, q, qmax, picture_number,
wanted_bits / 1000, s->total_bits / 1000,
br_compensation, short_term_q, s->frame_bits,
pic->mb_var_sum, pic->mc_mb_var_sum,
s->mb_var_sum, s->mc_mb_var_sum,
s->bit_rate / 1000, (int)fps);
}
@ -1019,8 +1018,8 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run)
if (!dry_run) {
rcc->last_qscale = q;
rcc->last_mc_mb_var_sum = pic->mc_mb_var_sum;
rcc->last_mb_var_sum = pic->mb_var_sum;
rcc->last_mc_mb_var_sum = s->mc_mb_var_sum;
rcc->last_mb_var_sum = s->mb_var_sum;
}
return q;
}

View File

@ -1527,11 +1527,11 @@ static int ratecontrol_1pass(SnowContext *s, AVFrame *pict)
coef_sum = (uint64_t)coef_sum * coef_sum >> 16;
if(pict->pict_type == AV_PICTURE_TYPE_I){
s->m.current_picture.mb_var_sum= coef_sum;
s->m.current_picture.mc_mb_var_sum= 0;
s->m.mb_var_sum = coef_sum;
s->m.mc_mb_var_sum = 0;
}else{
s->m.current_picture.mc_mb_var_sum= coef_sum;
s->m.current_picture.mb_var_sum= 0;
s->m.mc_mb_var_sum = coef_sum;
s->m.mb_var_sum = 0;
}
pict->quality= ff_rate_estimate_qscale(&s->m, 1);