]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/anm.c
avcodec/zmbvenc: Simplify setting keyframe flag
[ffmpeg] / libavcodec / anm.c
index cd1fcc5998e5def96be6b0b113d457b3563be09c..494903235d158cc383fae6df402b97d0a1e5e617 100644 (file)
 typedef struct AnmContext {
     AVFrame *frame;
     int palette[AVPALETTE_COUNT];
-    GetByteContext gb;
-    int x;  ///< x coordinate position
 } AnmContext;
 
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     AnmContext *s = avctx->priv_data;
+    GetByteContext gb;
     int i;
 
+    if (avctx->extradata_size < 16 * 8 + 4 * 256)
+        return AVERROR_INVALIDDATA;
+
     avctx->pix_fmt = AV_PIX_FMT_PAL8;
 
     s->frame = av_frame_alloc();
     if (!s->frame)
         return AVERROR(ENOMEM);
 
-    bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
-    if (bytestream2_get_bytes_left(&s->gb) < 16 * 8 + 4 * 256) {
-        av_frame_free(&s->frame);
-        return AVERROR_INVALIDDATA;
-    }
-
-    bytestream2_skipu(&s->gb, 16 * 8);
+    bytestream2_init(&gb, avctx->extradata, avctx->extradata_size);
+    bytestream2_skipu(&gb, 16 * 8);
     for (i = 0; i < 256; i++)
-        s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&s->gb);
+        s->palette[i] = (0xFFU << 24) | bytestream2_get_le32u(&gb);
 
     return 0;
 }
@@ -117,7 +114,8 @@ static int decode_frame(AVCodecContext *avctx,
     AnmContext *s = avctx->priv_data;
     const int buf_size = avpkt->size;
     uint8_t *dst, *dst_end;
-    int count, ret;
+    GetByteContext gb;
+    int count, ret, x = 0;
 
     if (buf_size < 7)
         return AVERROR_INVALIDDATA;
@@ -127,37 +125,36 @@ static int decode_frame(AVCodecContext *avctx,
     dst     = s->frame->data[0];
     dst_end = s->frame->data[0] + s->frame->linesize[0]*avctx->height;
 
-    bytestream2_init(&s->gb, avpkt->data, buf_size);
+    bytestream2_init(&gb, avpkt->data, buf_size);
 
-    if (bytestream2_get_byte(&s->gb) != 0x42) {
+    if (bytestream2_get_byte(&gb) != 0x42) {
         avpriv_request_sample(avctx, "Unknown record type");
         return AVERROR_INVALIDDATA;
     }
-    if (bytestream2_get_byte(&s->gb)) {
+    if (bytestream2_get_byte(&gb)) {
         avpriv_request_sample(avctx, "Padding bytes");
         return AVERROR_PATCHWELCOME;
     }
-    bytestream2_skip(&s->gb, 2);
+    bytestream2_skip(&gb, 2);
 
-    s->x = 0;
     do {
         /* if statements are ordered by probability */
 #define OP(gb, pixel, count) \
-    op(&dst, dst_end, (gb), (pixel), (count), &s->x, avctx->width, s->frame->linesize[0])
+    op(&dst, dst_end, (gb), (pixel), (count), &x, avctx->width, s->frame->linesize[0])
 
-        int type = bytestream2_get_byte(&s->gb);
+        int type = bytestream2_get_byte(&gb);
         count = type & 0x7F;
         type >>= 7;
         if (count) {
-            if (OP(type ? NULL : &s->gb, -1, count)) break;
+            if (OP(type ? NULL : &gb, -1, count)) break;
         } else if (!type) {
             int pixel;
-            count = bytestream2_get_byte(&s->gb);  /* count==0 gives nop */
-            pixel = bytestream2_get_byte(&s->gb);
+            count = bytestream2_get_byte(&gb);  /* count==0 gives nop */
+            pixel = bytestream2_get_byte(&gb);
             if (OP(NULL, pixel, count)) break;
         } else {
             int pixel;
-            type = bytestream2_get_le16(&s->gb);
+            type = bytestream2_get_le16(&gb);
             count = type & 0x3FFF;
             type >>= 14;
             if (!count) {
@@ -169,11 +166,11 @@ static int decode_frame(AVCodecContext *avctx,
                 }
                 continue;
             }
-            pixel = type == 3 ? bytestream2_get_byte(&s->gb) : -1;
+            pixel = type == 3 ? bytestream2_get_byte(&gb) : -1;
             if (type == 1) count += 0x4000;
-            if (OP(type == 2 ? &s->gb : NULL, pixel, count)) break;
+            if (OP(type == 2 ? &gb : NULL, pixel, count)) break;
         }
-    } while (bytestream2_get_bytes_left(&s->gb) > 0);
+    } while (bytestream2_get_bytes_left(&gb) > 0);
 
     memcpy(s->frame->data[1], s->palette, AVPALETTE_SIZE);
 
@@ -192,7 +189,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
     return 0;
 }
 
-AVCodec ff_anm_decoder = {
+const AVCodec ff_anm_decoder = {
     .name           = "anm",
     .long_name      = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
     .type           = AVMEDIA_TYPE_VIDEO,