[FFmpeg-cvslog] avcodec/mjpegenc: Avoid allocation of MJpegContext

Andreas Rheinhardt git at videolan.org
Tue Jan 4 18:20:17 EET 2022


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Tue Dec 21 22:39:30 2021 +0100| [26e117a14dca123e00237e471abf9aa3e5eea812] | committer: Andreas Rheinhardt

avcodec/mjpegenc: Avoid allocation of MJpegContext

This is possible by allocating it together with the MpegEncContext.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=26e117a14dca123e00237e471abf9aa3e5eea812
---

 libavcodec/mjpegenc.c        | 31 +++++++++++++++++--------------
 libavcodec/mjpegenc_common.c |  2 +-
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/libavcodec/mjpegenc.c b/libavcodec/mjpegenc.c
index 05b6130de4..c4c45c885b 100644
--- a/libavcodec/mjpegenc.c
+++ b/libavcodec/mjpegenc.c
@@ -41,6 +41,15 @@
 #include "mjpegenc.h"
 #include "profiles.h"
 
+/* The following is the private context of MJPEG/AMV decoder.
+ * Note that when using slice threading only the main thread's
+ * MpegEncContext is followed by a MjpegContext; the other threads
+ * can access this shared context via MpegEncContext.mjpeg. */
+typedef struct MJPEGEncContext {
+    MpegEncContext mpeg;
+    MJpegContext   mjpeg;
+} MJPEGEncContext;
+
 static av_cold void init_uni_ac_vlc(const uint8_t huff_size_ac[256],
                                     uint8_t *uni_ac_vlc_len)
 {
@@ -260,9 +269,11 @@ static int alloc_huffman(MpegEncContext *s)
 
 av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
 {
-    MJpegContext *m;
+    MJpegContext *const m = &((MJPEGEncContext*)s)->mjpeg;
     int ret;
 
+    s->mjpeg_ctx = m;
+
     av_assert0(s->slice_context_count == 1);
 
     /* The following check is automatically true for AMV,
@@ -276,10 +287,6 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
         return AVERROR(EINVAL);
     }
 
-    m = av_mallocz(sizeof(MJpegContext));
-    if (!m)
-        return AVERROR(ENOMEM);
-
     s->min_qcoeff=-1023;
     s->max_qcoeff= 1023;
 
@@ -312,7 +319,6 @@ av_cold int ff_mjpeg_encode_init(MpegEncContext *s)
 
     // Buffers start out empty.
     m->huff_ncode = 0;
-    s->mjpeg_ctx = m;
 
     if(s->huffman == HUFFMAN_TABLE_OPTIMAL)
         return alloc_huffman(s);
@@ -322,11 +328,8 @@ av_cold int ff_mjpeg_encode_init(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);
-    }
+    MJPEGEncContext *const mjpeg = avctx->priv_data;
+    av_freep(&mjpeg->mjpeg.huff_buffer);
     ff_mpv_encode_end(avctx);
     return 0;
 }
@@ -377,7 +380,7 @@ static void ff_mjpeg_encode_coef(MJpegContext *s, uint8_t table_id, int val, int
 /**
  * Add the block's data into the JPEG buffer.
  *
- * @param s The MJpegEncContext that contains the JPEG buffer.
+ * @param s The MpegEncContext that contains the JPEG buffer.
  * @param block The block.
  * @param n The block's index or number.
  */
@@ -618,7 +621,7 @@ const AVCodec ff_mjpeg_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_MJPEG,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MJPEGEncContext),
     .init           = ff_mpv_encode_init,
     .encode2        = ff_mpv_encode_picture,
     .close          = mjpeg_encode_close,
@@ -647,7 +650,7 @@ const AVCodec ff_amv_encoder = {
     .long_name      = NULL_IF_CONFIG_SMALL("AMV Video"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_AMV,
-    .priv_data_size = sizeof(MpegEncContext),
+    .priv_data_size = sizeof(MJPEGEncContext),
     .init           = ff_mpv_encode_init,
     .encode2        = amv_encode_picture,
     .close          = mjpeg_encode_close,
diff --git a/libavcodec/mjpegenc_common.c b/libavcodec/mjpegenc_common.c
index 368e87128c..ba5c5320d7 100644
--- a/libavcodec/mjpegenc_common.c
+++ b/libavcodec/mjpegenc_common.c
@@ -323,7 +323,7 @@ void ff_mjpeg_encode_picture_header(AVCodecContext *avctx, PutBitContext *pb,
 end:
     if (!lossless) {
         MpegEncContext *s = avctx->priv_data;
-        av_assert0(avctx->codec->priv_data_size == sizeof(MpegEncContext));
+        av_assert0(avctx->codec->priv_data_size > sizeof(MpegEncContext));
 
         s->esc_pos = put_bytes_count(pb, 0);
         for(i=1; i<s->slice_context_count; i++)



More information about the ffmpeg-cvslog mailing list