]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/4xm.c
network: Use ff_neterrno instead of AVERROR(errno) for poll errors
[ffmpeg] / libavcodec / 4xm.c
index 36d9dc123aeba9c0fc3e82711d73ba933c09eb73..4a9a14a79b954a9c1a9446169932c41ccb44b92c 100644 (file)
 #include "libavutil/frame.h"
 #include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+
 #include "avcodec.h"
+#include "bitstream.h"
 #include "blockdsp.h"
 #include "bswapdsp.h"
 #include "bytestream.h"
-#include "get_bits.h"
 #include "internal.h"
+#include "vlc.h"
 
 #define BLOCK_TYPE_VLC_BITS 5
 #define ACDC_VLC_BITS 9
@@ -136,8 +138,8 @@ typedef struct FourXContext {
     BswapDSPContext bbdsp;
     uint16_t *frame_buffer;
     uint16_t *last_frame_buffer;
-    GetBitContext pre_gb;          ///< ac/dc prefix
-    GetBitContext gb;
+    BitstreamContext pre_bc;    // ac/dc prefix
+    BitstreamContext bc;
     GetByteContext g;
     GetByteContext g2;
     int mv[256];
@@ -340,22 +342,29 @@ static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w,
 static int decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src,
                           int log2w, int log2h, int stride)
 {
-    const int index = size2index[log2h][log2w];
-    const int h     = 1 << log2h;
-    int code        = get_vlc2(&f->gb,
-                               block_type_vlc[1 - (f->version > 1)][index].table,
-                               BLOCK_TYPE_VLC_BITS, 1);
-    uint16_t *start = f->last_frame_buffer;
-    uint16_t *end   = start + stride * (f->avctx->height - h + 1) - (1 << log2w);
-    int ret;
-    int scale   = 1;
+    int index, h, code, ret, scale = 1;
+    uint16_t *start, *end;
     unsigned dc = 0;
 
-    if (code < 0 || code > 6 || log2w < 0)
+    if (log2h < 0 || log2w < 0)
+        return AVERROR_INVALIDDATA;
+
+    index = size2index[log2h][log2w];
+    if (index < 0)
+        return AVERROR_INVALIDDATA;
+
+    h     = 1 << log2h;
+    code  = bitstream_read_vlc(&f->bc, block_type_vlc[1 - (f->version > 1)][index].table,
+                               BLOCK_TYPE_VLC_BITS, 1);
+    if (code < 0 || code > 6)
         return AVERROR_INVALIDDATA;
 
+    start = f->last_frame_buffer;
+    end   = start + stride * (f->avctx->height - h + 1) - (1 << log2w);
+
     if (code == 1) {
-        log2h--;
+        if (--log2h < 0)
+            return AVERROR_INVALIDDATA;
         if ((ret = decode_p_block(f, dst, src, log2w, log2h, stride)) < 0)
             return ret;
         return decode_p_block(f, dst + (stride << log2h),
@@ -439,14 +448,14 @@ static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length)
     }
 
     av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
-                   bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
+                   bitstream_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!f->bitstream_buffer)
         return AVERROR(ENOMEM);
     f->bbdsp.bswap_buf(f->bitstream_buffer, (const uint32_t *) (buf + extra),
                        bitstream_size / 4);
     memset((uint8_t*)f->bitstream_buffer + bitstream_size,
-           0, FF_INPUT_BUFFER_PADDING_SIZE);
-    init_get_bits(&f->gb, f->bitstream_buffer, 8 * bitstream_size);
+           0, AV_INPUT_BUFFER_PADDING_SIZE);
+    bitstream_init8(&f->bc, f->bitstream_buffer, bitstream_size);
 
     wordstream_offset = extra + bitstream_size;
     bytestream_offset = extra + bitstream_size + wordstream_size;
@@ -477,19 +486,19 @@ static int decode_i_block(FourXContext *f, int16_t *block)
     int code, i, j, level, val;
 
     /* DC coef */
-    val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
+    val = bitstream_read_vlc(&f->pre_bc, f->pre_vlc.table, ACDC_VLC_BITS, 3);
     if (val >> 4)
         av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
 
     if (val)
-        val = get_xbits(&f->gb, val);
+        val = bitstream_read_xbits(&f->bc, val);
 
     val        = val * dequant_table[0] + f->last_dc;
     f->last_dc = block[0] = val;
     /* AC coefs */
     i = 1;
     for (;;) {
-        code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
+        code = bitstream_read_vlc(&f->pre_bc, f->pre_vlc.table, ACDC_VLC_BITS, 3);
 
         /* EOB */
         if (code == 0)
@@ -497,7 +506,7 @@ static int decode_i_block(FourXContext *f, int16_t *block)
         if (code == 0xf0) {
             i += 16;
         } else {
-            level = get_xbits(&f->gb, code & 0xf);
+            level = bitstream_read_xbits(&f->bc, code & 0xf);
             i    += code >> 4;
             if (i >= 64) {
                 av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
@@ -527,7 +536,7 @@ static inline void idct_put(FourXContext *f, int x, int y)
         idct(block[i]);
     }
 
-    if (!(f->avctx->flags & CODEC_FLAG_GRAY)) {
+    if (!(f->avctx->flags & AV_CODEC_FLAG_GRAY)) {
         for (i = 4; i < 6; i++)
             idct(block[i]);
     }
@@ -757,19 +766,19 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
         return AVERROR_INVALIDDATA;
     }
 
-    init_get_bits(&f->gb, buf + 4, 8 * bitstream_size);
+    bitstream_init8(&f->bc, buf + 4, bitstream_size);
 
     prestream_size = length + buf - prestream;
 
     av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
-                   prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
+                   prestream_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!f->bitstream_buffer)
         return AVERROR(ENOMEM);
     f->bbdsp.bswap_buf(f->bitstream_buffer, (const uint32_t *) prestream,
                        prestream_size / 4);
     memset((uint8_t*)f->bitstream_buffer + prestream_size,
-           0, FF_INPUT_BUFFER_PADDING_SIZE);
-    init_get_bits(&f->pre_gb, f->bitstream_buffer, 8 * prestream_size);
+           0, AV_INPUT_BUFFER_PADDING_SIZE);
+    bitstream_init8(&f->pre_bc, f->bitstream_buffer, prestream_size);
 
     f->last_dc = 0 * 128 * 8 * 8;
 
@@ -782,7 +791,7 @@ static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
         }
     }
 
-    if (get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
+    if (bitstream_read_vlc(&f->pre_bc, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
         av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
 
     return 0;
@@ -820,9 +829,6 @@ static int decode_frame(AVCodecContext *avctx, void *data,
         const int data_size  = buf_size - 20;
         CFrameBuffer *cfrm;
 
-        if (data_size < 0)
-            return AVERROR_INVALIDDATA;
-
         id         = AV_RL32(buf + 12);
         whole_size = AV_RL32(buf + 16);
 
@@ -845,7 +851,7 @@ static int decode_frame(AVCodecContext *avctx, void *data,
         cfrm = &f->cfrm[i];
 
         cfrm->data = av_fast_realloc(cfrm->data, &cfrm->allocated_size,
-                                     cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
+                                     cfrm->size + data_size + AV_INPUT_BUFFER_PADDING_SIZE);
         // explicit check needed as memcpy below might not catch a NULL
         if (!cfrm->data) {
             av_log(f->avctx, AV_LOG_ERROR, "realloc failure");
@@ -955,7 +961,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
     }
 
     f->version = AV_RL32(avctx->extradata) >> 16;
-    ff_blockdsp_init(&f->bdsp, avctx);
+    ff_blockdsp_init(&f->bdsp);
     ff_bswapdsp_init(&f->bbdsp);
     f->avctx = avctx;
     init_vlcs(f);
@@ -977,5 +983,5 @@ AVCodec ff_fourxm_decoder = {
     .init           = decode_init,
     .close          = decode_end,
     .decode         = decode_frame,
-    .capabilities   = CODEC_CAP_DR1,
+    .capabilities   = AV_CODEC_CAP_DR1,
 };