[FFmpeg-devel] libavc/libx264: add support to propagate SSE values through encoder stats

Carotti, Elias eliascrt at amazon.it
Wed Oct 11 13:54:21 EEST 2023


Hi Anton, 

On Tue, 2023-10-10 at 12:54 +0200, Anton Khirnov wrote:
> 
> Quoting Carotti, Elias via ffmpeg-devel (2023-10-02 19:35:09)
> > diff --git a/libavcodec/libx264.c b/libavcodec/libx264.c
> > index 77a9f173b4..4c643c9066 100644
> > --- a/libavcodec/libx264.c
> > +++ b/libavcodec/libx264.c
> > @@ -129,6 +129,8 @@ typedef struct X264Context {
> >      int roi_warned;
> > 
> >      int mb_info;
> > +
> > +    int64_t sse[3];
> 
> The values don't need to be preserved across frames, so might as well
> put this on stack in the block calling
> ff_side_data_set_encoder_stats().

Agreed.

> 
> >  } X264Context;
> > 
> >  static void X264_log(void *p, int level, const char *fmt, va_list
> > args)
> > @@ -726,7 +728,40 @@ FF_ENABLE_DEPRECATION_WARNINGS
> > 
> >      pkt->flags |= AV_PKT_FLAG_KEY*pic_out.b_keyframe;
> >      if (ret) {
> > -        ff_side_data_set_encoder_stats(pkt, (pic_out.i_qpplus1 -
> > 1) * FF_QP2LAMBDA, NULL, 0, pict_type);
> > +        const AVPixFmtDescriptor *pix_desc =
> > av_pix_fmt_desc_get(csp_to_pixfmt(pic_out.img.i_csp));
> > +        int error_count = 0;
> > +        int64_t *errors = NULL;
> > +
> > +        if (ctx->flags & AV_CODEC_FLAG_PSNR) {
> > +            double scale[3] = { 1,
> > +                (1 << pix_desc->log2_chroma_h) * (double)(1 <<
> > pix_desc->log2_chroma_w),
> > +                (1 << pix_desc->log2_chroma_h) * (double)(1 <<
> > pix_desc->log2_chroma_w),
> 
> Any particular reason the cast is on the second value? It looks
> strange.
> 

Just my habit. Fixed.

> > +            };
> > +            double mse;
> > +            int i;
> > +
> > +            error_count = pix_desc->nb_components;
> > +
> > +            av_log(ctx, AV_LOG_DEBUG, "PSNR values from libx264:
> > %.3f %.3f %.3f.\n",
> > +                   pic_out.prop.f_psnr[0], pic_out.prop.f_psnr[1],
> > pic_out.prop.f_psnr[2]);
> > +
> > +            for (i = 0; i < pix_desc->nb_components; ++i) {
> 
> for (int i ....

Agreed.

I also found the - (minus) sign in the mse formula was wrong and I
removed it.
Numbers seem to be coherent with those from libx264.
Please find attached a new patch rebased against the latest master with
the above fixes.

There is an increasing error (over increasing PSNRs and resolutions)
when reconstructing the PSNR from the SSE as computed above due to the
approximations and the roundings back and forth, however it seems to
yield similar values as those computed by libx264.

Best





NICE SRL, viale Monte Grappa 3/5, 20124 Milano, Italia, Registro delle Imprese di Milano Monza Brianza Lodi REA n. 2096882, Capitale Sociale: 10.329,14 EUR i.v., Cod. Fisc. e P.IVA 01133050052, Societa con Socio Unico


-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-avcodec-libx264-Add-the-SSE-computation-for-libx264.patch
Type: text/x-patch
Size: 2413 bytes
Desc: 0001-avcodec-libx264-Add-the-SSE-computation-for-libx264.patch
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20231011/c3f598b7/attachment.bin>


More information about the ffmpeg-devel mailing list