[FFmpeg-devel] [PATCH 5/5] avcodec/mjpegbdec: Don't use GetBit-API for byte-aligned reads

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Thu Apr 14 18:57:40 EEST 2022


Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavcodec/mjpegbdec.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c
index 46b9eb377e..50fff2562b 100644
--- a/libavcodec/mjpegbdec.c
+++ b/libavcodec/mjpegbdec.c
@@ -27,12 +27,15 @@
 #include <inttypes.h>
 
 #include "avcodec.h"
+#include "bytestream.h"
 #include "codec_internal.h"
 #include "mjpeg.h"
 #include "mjpegdec.h"
 
-static uint32_t read_offs(AVCodecContext *avctx, GetBitContext *gb, uint32_t size, const char *err_msg){
-    uint32_t offs= get_bits_long(gb, 32);
+static uint32_t read_offs(AVCodecContext *avctx, GetByteContext *gb,
+                          uint32_t size, const char *err_msg)
+{
+    uint32_t offs = bytestream2_get_be32u(gb);
     if(offs >= size){
         av_log(avctx, AV_LOG_WARNING, err_msg, offs, size);
         return 0;
@@ -47,7 +50,7 @@ static int mjpegb_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
     int buf_size = avpkt->size;
     MJpegDecodeContext *s = avctx->priv_data;
     const uint8_t *buf_end, *buf_ptr;
-    GetBitContext hgb; /* for the header */
+    GetByteContext hgb; /* for the header */
     uint32_t dqt_offs, dht_offs, sof_offs, sos_offs, second_field_offs;
     uint32_t field_size, sod_offs;
     int ret;
@@ -64,21 +67,22 @@ read_header:
     s->restart_count = 0;
     s->mjpb_skiptosod = 0;
 
-    if (buf_end - buf_ptr >= 1 << 28)
+    if (buf_end - buf_ptr >= 1 << 28 ||
+        buf_end - buf_ptr < 40 /* header size */)
         return AVERROR_INVALIDDATA;
 
-    init_get_bits(&hgb, buf_ptr, /*buf_size*/(buf_end - buf_ptr)*8);
+    bytestream2_init(&hgb, buf_ptr, buf_end - buf_ptr);
 
-    skip_bits(&hgb, 32); /* reserved zeros */
+    bytestream2_skipu(&hgb, 4); /* reserved zeros */
 
-    if (get_bits_long(&hgb, 32) != MKBETAG('m','j','p','g')) {
+    if (bytestream2_get_be32u(&hgb) != MKBETAG('m','j','p','g')) {
         av_log(avctx, AV_LOG_WARNING, "not mjpeg-b (bad fourcc)\n");
         return AVERROR_INVALIDDATA;
     }
 
-    field_size = get_bits_long(&hgb, 32); /* field size */
+    field_size = bytestream2_get_be32u(&hgb); /* field size */
     av_log(avctx, AV_LOG_DEBUG, "field size: 0x%"PRIx32"\n", field_size);
-    skip_bits(&hgb, 32); /* padded field size */
+    bytestream2_skipu(&hgb, 4); /* padded field size */
     second_field_offs = read_offs(avctx, &hgb, buf_end - buf_ptr, "second_field_offs is %d and size is %d\n");
     av_log(avctx, AV_LOG_DEBUG, "second field offs: 0x%"PRIx32"\n",
            second_field_offs);
-- 
2.32.0



More information about the ffmpeg-devel mailing list