]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/eatgv.c
lavc: add a null bitstream filter
[ffmpeg] / libavcodec / eatgv.c
index fabc5167bb07b2e5e153fab2352016e41c8f55f7..549b5b6d3ca418bfbdc6959ccffac7194c0921e9 100644 (file)
  * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_TGV
  */
 
-#include "avcodec.h"
-#define BITSTREAM_READER_LE
-#include "get_bits.h"
-#include "internal.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/mem.h"
 
+#define BITSTREAM_READER_LE
+#include "avcodec.h"
+#include "bitstream.h"
+#include "internal.h"
+
 #define EA_PREAMBLE_SIZE    8
 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
 
 typedef struct TgvContext {
     AVCodecContext *avctx;
-    AVFrame last_frame;
+    AVFrame *last_frame;
     uint8_t *frame_buffer;
     int width,height;
     uint32_t palette[AVPALETTE_COUNT];
@@ -55,8 +56,13 @@ static av_cold int tgv_decode_init(AVCodecContext *avctx)
 {
     TgvContext *s = avctx->priv_data;
     s->avctx         = avctx;
-    avctx->time_base = (AVRational){1, 15};
+    avctx->framerate = (AVRational){ 15, 1 };
     avctx->pix_fmt   = AV_PIX_FMT_PAL8;
+
+    s->last_frame = av_frame_alloc();
+    if (!s->last_frame)
+        return AVERROR(ENOMEM);
+
     return 0;
 }
 
@@ -147,7 +153,7 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
     int num_blocks_packed;
     int vector_bits;
     int i,j,x,y;
-    GetBitContext gb;
+    BitstreamContext bc;
     int mvbits;
     const uint8_t *blocks_raw;
 
@@ -160,7 +166,7 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
     vector_bits       = AV_RL16(&buf[6]);
     buf += 12;
 
-    if (vector_bits > MIN_CACHE_BITS || !vector_bits) {
+    if (vector_bits > 32 || !vector_bits) {
         av_log(s->avctx, AV_LOG_ERROR,
                "Invalid value for motion vector bits: %d\n", vector_bits);
         return AVERROR_INVALIDDATA;
@@ -168,12 +174,18 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
 
     /* allocate codebook buffers as necessary */
     if (num_mvs > s->num_mvs) {
-        s->mv_codebook = av_realloc(s->mv_codebook, num_mvs*2*sizeof(int));
+        int err = av_reallocp(&s->mv_codebook, num_mvs * 2 * sizeof(int));
+        if (err < 0)
+            return err;
         s->num_mvs = num_mvs;
     }
 
     if (num_blocks_packed > s->num_blocks_packed) {
-        s->block_codebook = av_realloc(s->block_codebook, num_blocks_packed*16);
+        int err;
+        if ((err = av_reallocp(&s->block_codebook, num_blocks_packed * 16)) < 0) {
+            s->num_blocks_packed = 0;
+            return err;
+        }
         s->num_blocks_packed = num_blocks_packed;
     }
 
@@ -183,10 +195,10 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
     if (buf + (mvbits >> 3) + 16 * num_blocks_raw + 8 * num_blocks_packed > buf_end)
         return AVERROR_INVALIDDATA;
 
-    init_get_bits(&gb, buf, mvbits);
+    bitstream_init(&bc, buf, mvbits);
     for (i = 0; i < num_mvs; i++) {
-        s->mv_codebook[i][0] = get_sbits(&gb, 10);
-        s->mv_codebook[i][1] = get_sbits(&gb, 10);
+        s->mv_codebook[i][0] = bitstream_read_signed(&bc, 10);
+        s->mv_codebook[i][1] = bitstream_read_signed(&bc, 10);
     }
     buf += mvbits >> 3;
 
@@ -195,25 +207,25 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
     buf       += num_blocks_raw * 16;
 
     /* read compressed blocks */
-    init_get_bits(&gb, buf, (buf_end - buf) << 3);
+    bitstream_init(&bc, buf, (buf_end - buf) << 3);
     for (i = 0; i < num_blocks_packed; i++) {
         int tmp[4];
         for (j = 0; j < 4; j++)
-            tmp[j] = get_bits(&gb, 8);
+            tmp[j] = bitstream_read(&bc, 8);
         for (j = 0; j < 16; j++)
-            s->block_codebook[i][15-j] = tmp[get_bits(&gb, 2)];
+            s->block_codebook[i][15-j] = tmp[bitstream_read(&bc, 2)];
     }
 
-    if (get_bits_left(&gb) < vector_bits *
+    if (bitstream_bits_left(&bc) < vector_bits *
         (s->avctx->height / 4) * (s->avctx->width / 4))
         return AVERROR_INVALIDDATA;
 
     /* read vectors and build frame */
     for (y = 0; y < s->avctx->height / 4; y++)
         for (x = 0; x < s->avctx->width / 4; x++) {
-            unsigned int vector = get_bits(&gb, vector_bits);
+            unsigned int vector = bitstream_read(&bc, vector_bits);
             const uint8_t *src;
-            int src_stride;
+            ptrdiff_t src_stride;
 
             if (vector < num_mvs) {
                 int mx = x * 4 + s->mv_codebook[vector][0];
@@ -223,8 +235,8 @@ static int tgv_decode_inter(TgvContext *s, AVFrame *frame,
                     my < 0 || my + 4 > s->avctx->height)
                     continue;
 
-                src = s->last_frame.data[0] + mx + my * s->last_frame.linesize[0];
-                src_stride = s->last_frame.linesize[0];
+                src = s->last_frame->data[0] + mx + my * s->last_frame->linesize[0];
+                src_stride = s->last_frame->linesize[0];
             } else {
                 int offset = vector - num_mvs;
                 if (offset < num_blocks_raw)
@@ -269,9 +281,10 @@ static int tgv_decode_frame(AVCodecContext *avctx,
         s->width  = AV_RL16(&buf[0]);
         s->height = AV_RL16(&buf[2]);
         if (s->avctx->width != s->width || s->avctx->height != s->height) {
-            avcodec_set_dimensions(s->avctx, s->width, s->height);
             av_freep(&s->frame_buffer);
-            av_frame_unref(&s->last_frame);
+            av_frame_unref(s->last_frame);
+            if ((ret = ff_set_dimensions(s->avctx, s->width, s->height)) < 0)
+                return ret;
         }
 
         pal_count = AV_RL16(&buf[6]);
@@ -282,9 +295,6 @@ static int tgv_decode_frame(AVCodecContext *avctx,
         }
     }
 
-    if ((ret = av_image_check_size(s->width, s->height, 0, avctx)) < 0)
-        return ret;
-
     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
 
@@ -308,7 +318,7 @@ static int tgv_decode_frame(AVCodecContext *avctx,
                    s->frame_buffer + y * s->width,
                    s->width);
     } else {
-        if (!s->last_frame.data[0]) {
+        if (!s->last_frame->data[0]) {
             av_log(avctx, AV_LOG_WARNING, "inter frame without corresponding intra frame\n");
             return buf_size;
         }
@@ -320,8 +330,8 @@ static int tgv_decode_frame(AVCodecContext *avctx,
         }
     }
 
-    av_frame_unref(&s->last_frame);
-    if ((ret = av_frame_ref(&s->last_frame, frame)) < 0)
+    av_frame_unref(s->last_frame);
+    if ((ret = av_frame_ref(s->last_frame, frame)) < 0)
         return ret;
 
     *got_frame = 1;
@@ -332,7 +342,7 @@ static int tgv_decode_frame(AVCodecContext *avctx,
 static av_cold int tgv_decode_end(AVCodecContext *avctx)
 {
     TgvContext *s = avctx->priv_data;
-    av_frame_unref(&s->last_frame);
+    av_frame_free(&s->last_frame);
     av_freep(&s->frame_buffer);
     av_free(s->mv_codebook);
     av_free(s->block_codebook);
@@ -348,5 +358,5 @@ AVCodec ff_eatgv_decoder = {
     .init           = tgv_decode_init,
     .close          = tgv_decode_end,
     .decode         = tgv_decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
+    .capabilities   = AV_CODEC_CAP_DR1,
 };