]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/xan.c
aarch64: vp9dsp: Fix vertical alignment in the init file
[ffmpeg] / libavcodec / xan.c
index 2bdced736c1cba19c207ce64051a04a708cf2674..33149e5ff7e32b8a2637c8b2eeb2be2c6a5b15a6 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Wing Commander/Xan Video Decoder
- * Copyright (C) 2003 the ffmpeg project
+ * Copyright (C) 2003 The FFmpeg project
  *
  * This file is part of Libav.
  *
 
 #include "libavutil/intreadwrite.h"
 #include "libavutil/mem.h"
+
+#define BITSTREAM_READER_LE
 #include "avcodec.h"
+#include "bitstream.h"
 #include "bytestream.h"
-#define BITSTREAM_READER_LE
-#include "get_bits.h"
 #include "internal.h"
 
 #define RUNTIME_GAMMA 0
@@ -52,7 +53,7 @@
 typedef struct XanContext {
 
     AVCodecContext *avctx;
-    AVFrame last_frame;
+    AVFrame *last_frame;
 
     const unsigned char *buf;
     int size;
@@ -71,6 +72,19 @@ typedef struct XanContext {
 
 } XanContext;
 
+static av_cold int xan_decode_end(AVCodecContext *avctx)
+{
+    XanContext *s = avctx->priv_data;
+
+    av_frame_free(&s->last_frame);
+
+    av_freep(&s->buffer1);
+    av_freep(&s->buffer2);
+    av_freep(&s->palettes);
+
+    return 0;
+}
+
 static av_cold int xan_decode_init(AVCodecContext *avctx)
 {
     XanContext *s = avctx->priv_data;
@@ -91,6 +105,12 @@ static av_cold int xan_decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
     }
 
+    s->last_frame = av_frame_alloc();
+    if (!s->last_frame) {
+        xan_decode_end(avctx);
+        return AVERROR(ENOMEM);
+    }
+
     return 0;
 }
 
@@ -103,28 +123,29 @@ static int xan_huffman_decode(unsigned char *dest, int dest_len,
     int ptr_len = src_len - 1 - byte*2;
     unsigned char val = ival;
     unsigned char *dest_end = dest + dest_len;
-    GetBitContext gb;
+    unsigned char *dest_start = dest;
+    BitstreamContext bc;
 
     if (ptr_len < 0)
         return AVERROR_INVALIDDATA;
 
-    init_get_bits(&gb, ptr, ptr_len * 8);
+    bitstream_init(&bc, ptr, ptr_len * 8);
 
     while (val != 0x16) {
-        unsigned idx = val - 0x17 + get_bits1(&gb) * byte;
+        unsigned idx = val - 0x17 + bitstream_read_bit(&bc) * byte;
         if (idx >= 2 * byte)
             return AVERROR_INVALIDDATA;
         val = src[idx];
 
         if (val < 0x16) {
             if (dest >= dest_end)
-                return 0;
+                return dest_len;
             *dest++ = val;
             val = ival;
         }
     }
 
-    return 0;
+    return dest - dest_start;
 }
 
 /**
@@ -233,7 +254,7 @@ static inline void xan_wc3_copy_pixel_run(XanContext *s, AVFrame *frame,
         return;
 
     palette_plane = frame->data[0];
-    prev_palette_plane = s->last_frame.data[0];
+    prev_palette_plane = s->last_frame->data[0];
     if (!prev_palette_plane)
         prev_palette_plane = palette_plane;
     stride = frame->linesize[0];
@@ -278,7 +299,7 @@ static int xan_wc3_decode_frame(XanContext *s, AVFrame *frame)
     unsigned char flag = 0;
     int size = 0;
     int motion_x, motion_y;
-    int x, y;
+    int x, y, ret;
 
     unsigned char *opcode_buffer = s->buffer1;
     unsigned char *opcode_buffer_end = s->buffer1 + s->buffer1_size;
@@ -312,9 +333,10 @@ static int xan_wc3_decode_frame(XanContext *s, AVFrame *frame)
     bytestream2_init(&vector_segment, s->buf + vector_offset, s->size - vector_offset);
     imagedata_segment = s->buf + imagedata_offset;
 
-    if (xan_huffman_decode(opcode_buffer, opcode_buffer_size,
-                           huffman_segment, s->size - huffman_offset) < 0)
+    if ((ret = xan_huffman_decode(opcode_buffer, opcode_buffer_size,
+                                  huffman_segment, s->size - huffman_offset)) < 0)
         return AVERROR_INVALIDDATA;
+    opcode_buffer_end = opcode_buffer + ret;
 
     if (imagedata_segment[0] == 2) {
         xan_unpack(s->buffer2, s->buffer2_size,
@@ -580,8 +602,8 @@ static int xan_decode_frame(AVCodecContext *avctx,
     if (xan_wc3_decode_frame(s, frame) < 0)
         return AVERROR_INVALIDDATA;
 
-    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;
@@ -590,27 +612,14 @@ static int xan_decode_frame(AVCodecContext *avctx,
     return buf_size;
 }
 
-static av_cold int xan_decode_end(AVCodecContext *avctx)
-{
-    XanContext *s = avctx->priv_data;
-
-    av_frame_unref(&s->last_frame);
-
-    av_freep(&s->buffer1);
-    av_freep(&s->buffer2);
-    av_freep(&s->palettes);
-
-    return 0;
-}
-
 AVCodec ff_xan_wc3_decoder = {
     .name           = "xan_wc3",
+    .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = AV_CODEC_ID_XAN_WC3,
     .priv_data_size = sizeof(XanContext),
     .init           = xan_decode_init,
     .close          = xan_decode_end,
     .decode         = xan_decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
-    .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III / Xan"),
+    .capabilities   = AV_CODEC_CAP_DR1,
 };