]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/truemotion2.c
Move check_marker() from get_bits to mpeg4videodec
[ffmpeg] / libavcodec / truemotion2.c
index 5d9408d1667163c266eea7779a7746f042999cd7..172644007c5011522db2fe1891dd92bb7799cc63 100644 (file)
  * Duck TrueMotion2 decoder.
  */
 
+#include <inttypes.h>
+
 #include "avcodec.h"
+#include "bswapdsp.h"
 #include "bytestream.h"
 #include "get_bits.h"
-#include "dsputil.h"
 #include "internal.h"
 
 #define TM2_ESCAPE 0x80000000
@@ -58,10 +60,10 @@ enum TM2_BLOCKS {
 
 typedef struct TM2Context {
     AVCodecContext *avctx;
-    AVFrame pic;
+    AVFrame *pic;
 
     GetBitContext gb;
-    DSPContext dsp;
+    BswapDSPContext bdsp;
 
     /* TM2 streams */
     int *tokens[TM2_NUM_STREAMS];
@@ -169,6 +171,10 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
     huff.nums    = av_mallocz(huff.max_num * sizeof(int));
     huff.bits    = av_mallocz(huff.max_num * sizeof(uint32_t));
     huff.lens    = av_mallocz(huff.max_num * sizeof(int));
+    if (!huff.nums || !huff.bits || !huff.lens) {
+        res = AVERROR(ENOMEM);
+        goto out;
+    }
 
     res = tm2_read_tree(ctx, 0, 0, &huff);
 
@@ -191,10 +197,16 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
             code->bits = huff.max_bits;
             code->length = huff.max_num;
             code->recode = av_malloc(code->length * sizeof(int));
+            if (!code->recode) {
+                res = AVERROR(ENOMEM);
+                goto out;
+            }
             for (i = 0; i < code->length; i++)
                 code->recode[i] = huff.nums[i];
         }
     }
+
+out:
     /* free allocated memory */
     av_free(huff.nums);
     av_free(huff.bits);
@@ -231,7 +243,8 @@ static inline int tm2_read_header(TM2Context *ctx, const uint8_t *buf)
     case TM2_NEW_HEADER_MAGIC:
         return 0;
     default:
-        av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08X\n", magic);
+        av_log(ctx->avctx, AV_LOG_ERROR, "Not a TM2 header: 0x%08"PRIX32"\n",
+               magic);
         return AVERROR_INVALIDDATA;
     }
 }
@@ -278,7 +291,7 @@ static int tm2_read_stream(TM2Context *ctx, const uint8_t *buf, int stream_id, i
     if (len == 0)
         return 4;
 
-    if (len >= INT_MAX/4-1 || len < 0 || len > buf_size) {
+    if (len >= INT_MAX / 4 - 1 || len < 0 || skip > buf_size) {
         av_log(ctx->avctx, AV_LOG_ERROR, "Error, invalid stream size.\n");
         return AVERROR_INVALIDDATA;
     }
@@ -838,12 +851,12 @@ static int decode_frame(AVCodecContext *avctx,
     TM2Context * const l = avctx->priv_data;
     const uint8_t *buf   = avpkt->data;
     int buf_size         = avpkt->size & ~3;
-    AVFrame * const p    = &l->pic;
+    AVFrame * const p    = l->pic;
     int offset           = TM2_HEADER_SIZE;
     int i, t, ret;
     uint8_t *swbuf;
 
-    swbuf = av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
+    swbuf = av_malloc(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!swbuf) {
         av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
         return AVERROR(ENOMEM);
@@ -855,7 +868,8 @@ static int decode_frame(AVCodecContext *avctx,
         return ret;
     }
 
-    l->dsp.bswap_buf((uint32_t*)swbuf, (const uint32_t*)buf, buf_size >> 2);
+    l->bdsp.bswap_buf((uint32_t *) swbuf, (const uint32_t *) buf,
+                      buf_size >> 2);
 
     if ((ret = tm2_read_header(l, swbuf)) < 0) {
         av_free(swbuf);
@@ -883,7 +897,7 @@ static int decode_frame(AVCodecContext *avctx,
 
     l->cur = !l->cur;
     *got_frame      = 1;
-    ret = av_frame_ref(data, &l->pic);
+    ret = av_frame_ref(data, l->pic);
     av_free(swbuf);
 
     return (ret < 0) ? ret : buf_size;
@@ -900,10 +914,13 @@ static av_cold int decode_init(AVCodecContext *avctx)
     }
 
     l->avctx       = avctx;
-    avcodec_get_frame_defaults(&l->pic);
     avctx->pix_fmt = AV_PIX_FMT_BGR24;
 
-    ff_dsputil_init(&l->dsp, avctx);
+    l->pic = av_frame_alloc();
+    if (!l->pic)
+        return AVERROR(ENOMEM);
+
+    ff_bswapdsp_init(&l->bdsp);
 
     l->last  = av_malloc(4 * sizeof(*l->last)  * (w >> 2));
     l->clast = av_malloc(4 * sizeof(*l->clast) * (w >> 2));
@@ -952,7 +969,6 @@ static av_cold int decode_init(AVCodecContext *avctx)
 static av_cold int decode_end(AVCodecContext *avctx)
 {
     TM2Context * const l = avctx->priv_data;
-    AVFrame *pic = &l->pic;
     int i;
 
     av_free(l->last);
@@ -968,7 +984,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
         av_free(l->V2_base);
     }
 
-    av_frame_unref(pic);
+    av_frame_free(&l->pic);
 
     return 0;
 }
@@ -982,5 +998,5 @@ AVCodec ff_truemotion2_decoder = {
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
+    .capabilities   = AV_CODEC_CAP_DR1,
 };