]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/arbc.c
avcodec: Constify AVCodecs
[ffmpeg] / libavcodec / arbc.c
index 4558304f1203b2c6968326e7a4b7ad36e3f4bffd..c4fc30e047a7ea1f23f34e8b9741d54c3c42cba3 100644 (file)
@@ -38,12 +38,16 @@ typedef struct ARBCContext {
     AVFrame *prev_frame;
 } ARBCContext;
 
-static void fill_tile4(AVCodecContext *avctx, uint8_t *color, AVFrame *frame)
+static int 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;
+    int pixels_overwritten = 0;
+
+    if ((avctx->width / 4 + 1) * (avctx->height / 4 + 1) < nb_tiles)
+        return 0;
 
     for (int i = 0; i < nb_tiles; i++) {
         int y = bytestream2_get_byte(gb);
@@ -59,18 +63,18 @@ 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);
+                    pixels_overwritten ++;
                 }
                 mask = mask << 1;
             }
         }
     }
+    return pixels_overwritten;
 }
 
-static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
-                       uint8_t *color, AVFrame *frame)
+static int fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
+                       int color, AVFrame *frame)
 {
     ARBCContext *s = avctx->priv_data;
     GetByteContext *gb = &s->gb;
@@ -78,6 +82,10 @@ static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
     const int step_w = tile_width / 4;
     int nb_tiles = bytestream2_get_le16(gb);
     int h = avctx->height - 1;
+    int pixels_overwritten = 0;
+
+    if ((avctx->width / tile_width + 1) * (avctx->height / tile_height + 1) < nb_tiles)
+        return 0;
 
     for (int i = 0; i < nb_tiles; i++) {
         int y = bytestream2_get_byte(gb);
@@ -86,6 +94,9 @@ static void fill_tileX(AVCodecContext *avctx, int tile_width, int tile_height,
         int start_y = y * tile_height, start_x = x * tile_width;
         int end_y = start_y + tile_height, end_x = start_x + tile_width;
 
+        if (start_x >= avctx->width || start_y >= avctx->height)
+            continue;
+
         for (int j = start_y; j < end_y; j += step_h) {
             for (int k = start_x; k < end_x; k += step_w) {
                 if (mask & 0x8000U) {
@@ -93,16 +104,16 @@ 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);
                         }
                     }
+                    pixels_overwritten += FFMIN(step_h, avctx->height - j) * FFMIN(step_w, avctx->width - k);
                 }
                 mask = mask << 1;
             }
         }
     }
+    return pixels_overwritten;
 }
 
 static int decode_frame(AVCodecContext *avctx, void *data,
@@ -110,11 +121,21 @@ static int decode_frame(AVCodecContext *avctx, void *data,
 {
     ARBCContext *s = avctx->priv_data;
     AVFrame *frame = data;
-    int ret, nb_segments, keyframe = 1;
+    int ret, nb_segments;
+    int prev_pixels = avctx->width * avctx->height;
 
     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,45 +145,39 @@ 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);
 
         if (resolution_flag & 0x10)
-            fill_tileX(avctx, 1024, 1024, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 1024, 1024, fill, frame);
         if (resolution_flag & 0x08)
-            fill_tileX(avctx, 256, 256, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 256, 256, fill, frame);
         if (resolution_flag & 0x04)
-            fill_tileX(avctx, 64, 64, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 64, 64, fill, frame);
         if (resolution_flag & 0x02)
-            fill_tileX(avctx, 16, 16, fill, frame);
+            prev_pixels -= fill_tileX(avctx, 16, 16, fill, frame);
         if (resolution_flag & 0x01)
-            fill_tile4(avctx, fill, frame);
+            prev_pixels -= fill_tile4(avctx, fill, frame);
     }
 
     av_frame_unref(s->prev_frame);
     if ((ret = av_frame_ref(s->prev_frame, frame)) < 0)
         return ret;
 
-    frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
-    frame->key_frame = keyframe;
+    frame->pict_type = prev_pixels <= 0 ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
+    frame->key_frame = prev_pixels <= 0;
     *got_frame = 1;
 
     return avpkt->size;
@@ -197,7 +212,7 @@ static av_cold int decode_close(AVCodecContext *avctx)
     return 0;
 }
 
-AVCodec ff_arbc_decoder = {
+const AVCodec ff_arbc_decoder = {
     .name           = "arbc",
     .long_name      = NULL_IF_CONFIG_SMALL("Gryphon's Anim Compressor"),
     .type           = AVMEDIA_TYPE_VIDEO,