[FFmpeg-cvslog] avcodec/mjpegdec: Only use receive_frame for SMVJPEG

Andreas Rheinhardt git at videolan.org
Sat Dec 10 22:23:53 EET 2022


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Wed Dec  7 17:36:22 2022 +0100| [05086a6c936c360a43db38b11b5074fb0cb65211] | committer: Andreas Rheinhardt

avcodec/mjpegdec: Only use receive_frame for SMVJPEG

Only one codec using mjpegdec.c actually creates multiple
frames from a single packet, namely SMVJPEG. The other can
use the ordinary decode callback just fine. This e.g. has
the advantage of confining the special SP5X/AMV code to sp5xdec.c.

This reverts most of commit e9a2a8777317d91af658f774c68442ac4aa726ec;
of course it is not a simple revert: Way too much has changed;
furthermore, outright reverting the sp5xdec.c changes would readd
a stack packet to sp5x_decode_frame() which is not desired.
In order to avoid this without modifying the given AVPacket,
a variant of ff_mjpeg_decode_frame() with explicit buf and size
parameters has been added.

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

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

 libavcodec/jpeglsdec.c |  5 ++--
 libavcodec/mjpegdec.c  | 80 ++++++++++++++++++++++----------------------------
 libavcodec/mjpegdec.h  | 11 +++----
 libavcodec/sp5xdec.c   | 32 ++++++++++----------
 4 files changed, 59 insertions(+), 69 deletions(-)

diff --git a/libavcodec/jpeglsdec.c b/libavcodec/jpeglsdec.c
index 2e6d018ea6..ec163b8964 100644
--- a/libavcodec/jpeglsdec.c
+++ b/libavcodec/jpeglsdec.c
@@ -558,8 +558,7 @@ const FFCodec ff_jpegls_decoder = {
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
-    FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+    FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame),
     .p.capabilities = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
-                      FF_CODEC_CAP_SETS_PKT_DTS,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
diff --git a/libavcodec/mjpegdec.c b/libavcodec/mjpegdec.c
index 63151a68c9..908025b189 100644
--- a/libavcodec/mjpegdec.c
+++ b/libavcodec/mjpegdec.c
@@ -131,8 +131,6 @@ av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
         s->picture_ptr = s->picture;
     }
 
-    s->pkt = avctx->internal->in_pkt;
-
     s->avctx = avctx;
     ff_blockdsp_init(&s->bdsp);
     ff_hpeldsp_init(&s->hdsp, avctx->flags);
@@ -2366,31 +2364,9 @@ static void smv_process_frame(AVCodecContext *avctx, AVFrame *frame)
         av_frame_unref(s->smv_frame);
 }
 
-static int mjpeg_get_packet(AVCodecContext *avctx)
-{
-    MJpegDecodeContext *s = avctx->priv_data;
-    int ret;
-
-    av_packet_unref(s->pkt);
-    ret = ff_decode_get_packet(avctx, s->pkt);
-    if (ret < 0)
-        return ret;
-
-#if CONFIG_SP5X_DECODER || CONFIG_AMV_DECODER
-    if (avctx->codec_id == AV_CODEC_ID_SP5X ||
-        avctx->codec_id == AV_CODEC_ID_AMV) {
-        ret = ff_sp5x_process_packet(avctx, s->pkt);
-        if (ret < 0)
-            return ret;
-    }
-#endif
-
-    s->buf_size = s->pkt->size;
-
-    return 0;
-}
-
-int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx, AVFrame *frame,
+                                   int *got_frame, const AVPacket *avpkt,
+                                   const uint8_t *buf, const int buf_size)
 {
     MJpegDecodeContext *s = avctx->priv_data;
     const uint8_t *buf_end, *buf_ptr;
@@ -2405,6 +2381,8 @@ int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 
     s->force_pal8 = 0;
 
+    s->buf_size = buf_size;
+
     av_dict_free(&s->exif_metadata);
     av_freep(&s->stereo3d);
     s->adobe_transform = -1;
@@ -2412,12 +2390,9 @@ int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
     if (s->iccnum != 0)
         reset_icc_profile(s);
 
-    ret = mjpeg_get_packet(avctx);
-    if (ret < 0)
-        return ret;
 redo_for_pal8:
-    buf_ptr = s->pkt->data;
-    buf_end = s->pkt->data + s->pkt->size;
+    buf_ptr = buf;
+    buf_end = buf + buf_size;
     while (buf_ptr < buf_end) {
         /* find start next marker */
         start_code = ff_mjpeg_find_marker(s, &buf_ptr, buf_end,
@@ -2429,7 +2404,7 @@ redo_for_pal8:
         } else if (unescaped_buf_size > INT_MAX / 8) {
             av_log(avctx, AV_LOG_ERROR,
                    "MJPEG packet 0x%x too big (%d/%d), corrupt data?\n",
-                   start_code, unescaped_buf_size, s->pkt->size);
+                   start_code, unescaped_buf_size, buf_size);
             return AVERROR_INVALIDDATA;
         }
         av_log(avctx, AV_LOG_DEBUG, "marker=%x avail_size_in_buf=%"PTRDIFF_SPECIFIER"\n",
@@ -2568,7 +2543,6 @@ eoi_parser:
             }
             if (avctx->skip_frame == AVDISCARD_ALL) {
                 s->got_picture = 0;
-                ret = AVERROR(EAGAIN);
                 goto the_end_no_picture;
             }
             if (s->avctx->hwaccel) {
@@ -2580,10 +2554,9 @@ eoi_parser:
             }
             if ((ret = av_frame_ref(frame, s->picture_ptr)) < 0)
                 return ret;
+            *got_frame = 1;
             s->got_picture = 0;
 
-            frame->pkt_dts = s->pkt->dts;
-
             if (!s->lossless && avctx->debug & FF_DEBUG_QP) {
                 int qp = FFMAX3(s->qscale[0],
                                 s->qscale[1],
@@ -2909,15 +2882,20 @@ the_end:
         frame->crop_top = frame->height - avctx->height;
     }
 
-    ret = 0;
-
 the_end_no_picture:
     av_log(avctx, AV_LOG_DEBUG, "decode frame unused %"PTRDIFF_SPECIFIER" bytes\n",
            buf_end - buf_ptr);
+    return buf_ptr - buf;
+}
 
-    return ret;
+int ff_mjpeg_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame,
+                          AVPacket *avpkt)
+{
+    return ff_mjpeg_decode_frame_from_buf(avctx, frame, got_frame,
+                                          avpkt, avpkt->data, avpkt->size);
 }
 
+
 /* mxpeg may call the following function (with a blank MJpegDecodeContext)
  * even without having called ff_mjpeg_decode_init(). */
 av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
@@ -2993,7 +2971,7 @@ const FFCodec ff_mjpeg_decoder = {
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
-    FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+    FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame),
     .flush          = decode_flush,
     .p.capabilities = AV_CODEC_CAP_DR1,
     .p.max_lowres   = 3,
@@ -3001,7 +2979,6 @@ const FFCodec ff_mjpeg_decoder = {
     .p.profiles     = NULL_IF_CONFIG_SMALL(ff_mjpeg_profiles),
     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
                       FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
-                      FF_CODEC_CAP_SETS_PKT_DTS |
                       FF_CODEC_CAP_ICC_PROFILES,
     .hw_configs     = (const AVCodecHWConfigInternal *const []) {
 #if CONFIG_MJPEG_NVDEC_HWACCEL
@@ -3023,12 +3000,11 @@ const FFCodec ff_thp_decoder = {
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
-    FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+    FF_CODEC_DECODE_CB(ff_mjpeg_decode_frame),
     .flush          = decode_flush,
     .p.capabilities = AV_CODEC_CAP_DR1,
     .p.max_lowres   = 3,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
-                      FF_CODEC_CAP_SETS_PKT_DTS,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif
 
@@ -3036,6 +3012,9 @@ const FFCodec ff_thp_decoder = {
 static int smvjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
 {
     MJpegDecodeContext *s = avctx->priv_data;
+    AVPacket *const pkt = avctx->internal->in_pkt;
+    int64_t pkt_dts;
+    int got_frame = 0;
     int ret;
 
     if (s->smv_next_frame > 0) {
@@ -3048,10 +3027,21 @@ static int smvjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame)
         return 0;
     }
 
-    ret = ff_mjpeg_receive_frame(avctx, frame);
+    ret = ff_decode_get_packet(avctx, pkt);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_mjpeg_decode_frame(avctx, frame, &got_frame, pkt);
+    pkt_dts = pkt->dts;
+    av_packet_unref(pkt);
     if (ret < 0)
         return ret;
 
+    if (!got_frame)
+        return AVERROR(EAGAIN);
+
+    frame->pkt_dts = pkt_dts;
+
     av_assert0(frame->buf[0]);
     av_frame_unref(s->smv_frame);
     ret = av_frame_ref(s->smv_frame, frame);
diff --git a/libavcodec/mjpegdec.h b/libavcodec/mjpegdec.h
index 2cb218902c..13c524d597 100644
--- a/libavcodec/mjpegdec.h
+++ b/libavcodec/mjpegdec.h
@@ -57,8 +57,6 @@ typedef struct MJpegDecodeContext {
     GetBitContext gb;
     int buf_size;
 
-    AVPacket *pkt;
-
     int start_code; /* current start code */
     int buffer_size;
     uint8_t *buffer;
@@ -173,7 +171,12 @@ int ff_mjpeg_build_vlc(VLC *vlc, const uint8_t *bits_table,
                        const uint8_t *val_table, int is_ac, void *logctx);
 int ff_mjpeg_decode_init(AVCodecContext *avctx);
 int ff_mjpeg_decode_end(AVCodecContext *avctx);
-int ff_mjpeg_receive_frame(AVCodecContext *avctx, AVFrame *frame);
+int ff_mjpeg_decode_frame(AVCodecContext *avctx,
+                          AVFrame *frame, int *got_frame,
+                          AVPacket *avpkt);
+int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx,
+                                   AVFrame *frame, int *got_frame,
+                                   const AVPacket *avpkt, const uint8_t *buf, int buf_size);
 int ff_mjpeg_decode_dqt(MJpegDecodeContext *s);
 int ff_mjpeg_decode_dht(MJpegDecodeContext *s);
 int ff_mjpeg_decode_sof(MJpegDecodeContext *s);
@@ -184,6 +187,4 @@ int ff_mjpeg_find_marker(MJpegDecodeContext *s,
                          const uint8_t **buf_ptr, const uint8_t *buf_end,
                          const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size);
 
-int ff_sp5x_process_packet(AVCodecContext *avctx, AVPacket *avpkt);
-
 #endif /* AVCODEC_MJPEGDEC_H */
diff --git a/libavcodec/sp5xdec.c b/libavcodec/sp5xdec.c
index 394448c5a9..dfed725500 100644
--- a/libavcodec/sp5xdec.c
+++ b/libavcodec/sp5xdec.c
@@ -32,21 +32,23 @@
 #include "mjpegdec.h"
 #include "sp5x.h"
 
-int ff_sp5x_process_packet(AVCodecContext *avctx, AVPacket *avpkt)
+
+static int sp5x_decode_frame(AVCodecContext *avctx,
+                             AVFrame *frame, int *got_frame,
+                             AVPacket *avpkt)
 {
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
-    AVBufferRef *buf_recoded;
     uint8_t *recoded;
     int i = 0, j = 0;
+    int ret;
 
     if (!avctx->width || !avctx->height)
         return -1;
 
-    buf_recoded = av_buffer_allocz(buf_size + 1024);
-    if (!buf_recoded)
+    recoded = av_mallocz(buf_size + 1024);
+    if (!recoded)
         return -1;
-    recoded = buf_recoded->data;
 
     /* SOI */
     recoded[j++] = 0xFF;
@@ -83,12 +85,12 @@ int ff_sp5x_process_packet(AVCodecContext *avctx, AVPacket *avpkt)
     recoded[j++] = 0xFF;
     recoded[j++] = 0xD9;
 
-    av_buffer_unref(&avpkt->buf);
-    avpkt->buf  = buf_recoded;
-    avpkt->data = recoded;
-    avpkt->size = j;
+    ret = ff_mjpeg_decode_frame_from_buf(avctx, frame, got_frame,
+                                         avpkt, recoded, j);
+
+    av_free(recoded);
 
-    return 0;
+    return ret < 0 ? ret : avpkt->size;
 }
 
 #if CONFIG_SP5X_DECODER
@@ -100,11 +102,10 @@ const FFCodec ff_sp5x_decoder = {
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
-    FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+    FF_CODEC_DECODE_CB(sp5x_decode_frame),
     .p.capabilities = AV_CODEC_CAP_DR1,
     .p.max_lowres   = 3,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
-                      FF_CODEC_CAP_SETS_PKT_DTS,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif
 #if CONFIG_AMV_DECODER
@@ -116,10 +117,9 @@ const FFCodec ff_amv_decoder = {
     .priv_data_size = sizeof(MJpegDecodeContext),
     .init           = ff_mjpeg_decode_init,
     .close          = ff_mjpeg_decode_end,
-    FF_CODEC_RECEIVE_FRAME_CB(ff_mjpeg_receive_frame),
+    FF_CODEC_DECODE_CB(sp5x_decode_frame),
     .p.max_lowres   = 3,
     .p.capabilities = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP |
-                      FF_CODEC_CAP_SETS_PKT_DTS,
+    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif



More information about the ffmpeg-cvslog mailing list