[FFmpeg-devel] [PATCH 01/14] avcodec/mjpegenc: Use custom close function directly

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Fri Dec 31 12:03:27 EET 2021


Andreas Rheinhardt:
> Currently, ff_mpv_encode_end() is the close function of
> the two MJPEG-based encoders; it calls ff_mjpeg_encode_close()
> for them which adds a check to the generic code.
> This commit reverses the order of this relationship:
> The MJPEG encoders directly use a custom close function
> which in turn calls ff_mpv_encode_end(). This avoids the branch
> in ff_mpv_encode_end() and makes the generic code more generic.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> ---
>  libavcodec/mjpegenc.c      | 9 ++++++---
>  libavcodec/mjpegenc.h      | 1 -
>  libavcodec/mpegvideo_enc.c | 3 ---
>  3 files changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
> index 0ade66bc5f..5bd25b4f3b 100644
> --- a/libavcodec/mjpegenc.c
> +++ b/libavcodec/mjpegenc.c
> @@ -320,12 +320,15 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
>      return 0;
>  }
>  
> -av_cold void ff_mjpeg_encode_close(MpegEncContext *s)
> +static av_cold int mjpeg_encode_close(AVCodecContext *avctx)
>  {
> +    MpegEncContext *const s = avctx->priv_data;
>      if (s->mjpeg_ctx) {
>          av_freep(&s->mjpeg_ctx->huff_buffer);
>          av_freep(&s->mjpeg_ctx);
>      }
> +    ff_mpv_encode_end(avctx);
> +    return 0;
>  }
>  
>  /**
> @@ -618,7 +621,7 @@ const AVCodec ff_mjpeg_encoder = {
>      .priv_data_size = sizeof(MpegEncContext),
>      .init           = ff_mpv_encode_init,
>      .encode2        = ff_mpv_encode_picture,
> -    .close          = ff_mpv_encode_end,
> +    .close          = mjpeg_encode_close,
>      .capabilities   = AV_CODEC_CAP_SLICE_THREADS | AV_CODEC_CAP_FRAME_THREADS,
>      .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
>      .pix_fmts       = (const enum AVPixelFormat[]) {
> @@ -647,7 +650,7 @@ const AVCodec ff_amv_encoder = {
>      .priv_data_size = sizeof(MpegEncContext),
>      .init           = ff_mpv_encode_init,
>      .encode2        = amv_encode_picture,
> -    .close          = ff_mpv_encode_end,
> +    .close          = mjpeg_encode_close,
>      .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
>      .pix_fmts       = (const enum AVPixelFormat[]) {
>          AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_NONE
> diff --git a/libavcodec/mjpegenc.h b/libavcodec/mjpegenc.h
> index 2e92511276..bc9b017e7a 100644
> --- a/libavcodec/mjpegenc.h
> +++ b/libavcodec/mjpegenc.h
> @@ -105,7 +105,6 @@ static inline void put_marker(PutBitContext *p, enum JpegMarker code)
>  }
>  
>  int  ff_mjpeg_encode_init(MpegEncContext *s);
> -void ff_mjpeg_encode_close(MpegEncContext *s);
>  void ff_mjpeg_encode_mb(MpegEncContext *s, int16_t block[12][64]);
>  int  ff_mjpeg_encode_stuffing(MpegEncContext *s);
>  
> diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
> index 128d1a327c..d2520368e1 100644
> --- a/libavcodec/mpegvideo_enc.c
> +++ b/libavcodec/mpegvideo_enc.c
> @@ -970,9 +970,6 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx)
>      ff_rate_control_uninit(s);
>  
>      ff_mpv_common_end(s);
> -    if ((CONFIG_MJPEG_ENCODER || CONFIG_AMV_ENCODER) &&
> -        s->out_format == FMT_MJPEG)
> -        ff_mjpeg_encode_close(s);
>  
>      for (i = 0; i < FF_ARRAY_ELEMS(s->tmp_frames); i++)
>          av_frame_free(&s->tmp_frames[i]);
> 

Will apply this patchset tonight unless there are objections.

- Andreas


More information about the ffmpeg-devel mailing list