]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/arbc.c
avcodec/arbc: Skip unchanged frames
[ffmpeg] / libavcodec / arbc.c
index 4558304f1203b2c6968326e7a4b7ad36e3f4bffd..08d3a0ae6b8266d35d2b9990b8dec2f30d37de19 100644 (file)
@@ -38,13 +38,16 @@ typedef struct ARBCContext {
     AVFrame *prev_frame;
 } ARBCContext;
 
-static void fill_tile4(AVCodecContext *avctx, uint8_t *color, AVFrame *frame)
+static void fill_tile4(AVCodecContext *avctx, int color, AVFrame *frame)
 {
     ARBCContext *s = avctx->priv_data;
     GetByteContext *gb = &s->gb;
     int nb_tiles = bytestream2_get_le16(gb);
     int h = avctx->height - 1;
 
+    if ((avctx->width / 4 + 1) * (avctx->height / 4 + 1) < nb_tiles)
+        return;
+
     for (int i = 0; i < nb_tiles; i++) {
         int y = bytestream2_get_byte(gb);
         int x = bytestream2_get_byte(gb);
@@ -59,9 +62,7 @@ static void fill_tile4(AVCodecContext *avctx, uint8_t *color, AVFrame *frame)
                         mask = mask << 1;
                         continue;
                     }
-                    frame->data[0][frame->linesize[0] * (h - j) + 3 * k + 0] = color[0];
-                    frame->data[0][frame->linesize[0] * (h - j) + 3 * k + 1] = color[1];
-                    frame->data[0][frame->linesize[0] * (h - j) + 3 * k + 2] = color[2];
+                    AV_WB24(&frame->data[0][frame->linesize[0] * (h - j) + 3 * k], color);
                 }
                 mask = mask << 1;
             }
@@ -70,7 +71,7 @@ static void fill_tile4(AVCodecContext *avctx, uint8_t *color, AVFrame *frame)
 }
 
 static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
-                       uint8_t *color, AVFrame *frame)
+                       int color, AVFrame *frame)
 {
     ARBCContext *s = avctx->priv_data;
     GetByteContext *gb = &s->gb;
@@ -79,6 +80,9 @@ static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
     int nb_tiles = bytestream2_get_le16(gb);
     int h = avctx->height - 1;
 
+    if ((avctx->width / tile_width + 1) * (avctx->height / tile_height + 1) < nb_tiles)
+        return;
+
     for (int i = 0; i < nb_tiles; i++) {
         int y = bytestream2_get_byte(gb);
         int x = bytestream2_get_byte(gb);
@@ -93,9 +97,7 @@ static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
                         for (int n = 0; n < step_w; n++) {
                             if (j + m >= avctx->height || k + n >= avctx->width)
                                 continue;
-                            frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n) + 0] = color[0];
-                            frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n) + 1] = color[1];
-                            frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n) + 2] = color[2];
+                            AV_WB24(&frame->data[0][frame->linesize[0] * (h - (j + m)) + 3 * (k + n)], color);
                         }
                     }
                 }
@@ -115,6 +117,15 @@ static int decode_frame(AVCodecContext *avctx, void *data,
     if (avpkt->size < 10)
         return AVERROR_INVALIDDATA;
 
+    bytestream2_init(&s->gb, avpkt->data, avpkt->size);
+    bytestream2_skip(&s->gb, 8);
+    nb_segments = bytestream2_get_le16(&s->gb);
+    if (nb_segments == 0)
+        return avpkt->size;
+
+    if (7 * nb_segments > bytestream2_get_bytes_left(&s->gb))
+        return AVERROR_INVALIDDATA;
+
     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
 
@@ -124,24 +135,18 @@ static int decode_frame(AVCodecContext *avctx, void *data,
             return ret;
     }
 
-    bytestream2_init(&s->gb, avpkt->data, avpkt->size);
-    bytestream2_skip(&s->gb, 8);
-    nb_segments = bytestream2_get_le16(&s->gb);
-    if (nb_segments == 0)
-        keyframe = 0;
-
     for (int i = 0; i < nb_segments; i++) {
         int resolution_flag;
-        uint8_t fill[3];
+        int fill;
 
         if (bytestream2_get_bytes_left(&s->gb) <= 0)
             return AVERROR_INVALIDDATA;
 
-        fill[0] = bytestream2_get_byte(&s->gb);
+        fill = bytestream2_get_byte(&s->gb) << 16;
         bytestream2_skip(&s->gb, 1);
-        fill[1] = bytestream2_get_byte(&s->gb);
+        fill |= bytestream2_get_byte(&s->gb) << 8;
         bytestream2_skip(&s->gb, 1);
-        fill[2] = bytestream2_get_byte(&s->gb);
+        fill |= bytestream2_get_byte(&s->gb) << 0;
         bytestream2_skip(&s->gb, 1);
         resolution_flag = bytestream2_get_byte(&s->gb);