]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/4xm.c
check for request_channels at codec init
[ffmpeg] / libavcodec / 4xm.c
index b82797d49301d1e480cc5f4c7d74b52e720d75d4..72c8dbf9dd7df9211c82d323c23b11ec155a1df6 100644 (file)
@@ -27,6 +27,7 @@
 #include "avcodec.h"
 #include "dsputil.h"
 #include "mpegvideo.h"
+#include "bytestream.h"
 
 //#undef NDEBUG
 //#include <assert.h>
@@ -248,7 +249,10 @@ static void init_mv(FourXContext *f){
     int i;
 
     for(i=0; i<256; i++){
-        f->mv[i] = mv[i][0] + mv[i][1]*f->current_picture.linesize[0]/2;
+        if(f->version>1)
+            f->mv[i] = mv[i][0]   + mv[i][1]  *f->current_picture.linesize[0]/2;
+        else
+            f->mv[i] = (i&15) - 8 + ((i>>4)-8)*f->current_picture.linesize[0]/2;
     }
 }
 
@@ -296,12 +300,18 @@ static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, int h, int stri
 static void 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][index].table, BLOCK_TYPE_VLC_BITS, 1);
+    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_picture.data[0];
+    uint16_t *end= start + stride*(f->avctx->height-h+1) - (1<<log2w);
 
     assert(code>=0 && code<=6);
 
     if(code == 0){
         src += f->mv[ *f->bytestream++ ];
+        if(start > src || src > end){
+            av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
+            return;
+        }
         mcdc(dst, src, log2w, h, stride, 1, 0);
     }else if(code == 1){
         log2h--;
@@ -311,8 +321,14 @@ static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int lo
         log2w--;
         decode_p_block(f, dst             , src             , log2w, log2h, stride);
         decode_p_block(f, dst + (1<<log2w), src + (1<<log2w), log2w, log2h, stride);
+    }else if(code == 3 && f->version<2){
+        mcdc(dst, src, log2w, h, stride, 1, 0);
     }else if(code == 4){
         src += f->mv[ *f->bytestream++ ];
+        if(start > src || src > end){
+            av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
+            return;
+        }
         mcdc(dst, src, log2w, h, stride, 1, le2me_16(*f->wordstream++));
     }else if(code == 5){
         mcdc(dst, src, log2w, h, stride, 0, le2me_16(*f->wordstream++));
@@ -340,7 +356,7 @@ static int decode_p_frame(FourXContext *f, uint8_t *buf, int length){
     const int stride= f->current_picture.linesize[0]>>1;
     unsigned int bitstream_size, bytestream_size, wordstream_size, extra;
 
-    if(f->version){
+    if(f->version>1){
         extra=20;
         bitstream_size= get32(buf+8);
         wordstream_size= get32(buf+12);
@@ -592,8 +608,8 @@ static int decode_i2_frame(FourXContext *f, uint8_t *buf, int length){
             unsigned int color[4], bits;
             memset(color, 0, sizeof(color));
 //warning following is purely guessed ...
-            color[0]= AV_RN16(buf); buf+=2; //FIXME use bytestream
-            color[1]= AV_RN16(buf); buf+=2;
+            color[0]= bytestream_get_le16(&buf);
+            color[1]= bytestream_get_le16(&buf);
 
             if(color[0]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
             if(color[1]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
@@ -601,7 +617,7 @@ static int decode_i2_frame(FourXContext *f, uint8_t *buf, int length){
             color[2]= mix(color[0], color[1]);
             color[3]= mix(color[1], color[0]);
 
-            bits= AV_RL32(buf); buf+= 4;
+            bits= bytestream_get_le32(&buf);
             for(y2=0; y2<16; y2++){
                 for(x2=0; x2<16; x2++){
                     int index= 2*(x2>>2) + 8*(y2>>2);
@@ -783,12 +799,17 @@ static void common_init(AVCodecContext *avctx){
 static int decode_init(AVCodecContext *avctx){
     FourXContext * const f = avctx->priv_data;
 
-    f->version= avctx->codec_tag == 0x40000;
+    if(avctx->extradata_size != 4 || !avctx->extradata) {
+        av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
+        return 1;
+    }
+
+    f->version= AV_RL32(avctx->extradata)>>16;
     common_init(avctx);
     init_vlcs(f);
 
-    if(f->version) avctx->pix_fmt= PIX_FMT_RGB565;
-    else           avctx->pix_fmt= PIX_FMT_RGB555;
+    if(f->version>2) avctx->pix_fmt= PIX_FMT_RGB565;
+    else             avctx->pix_fmt= PIX_FMT_RGB555;
 
     return 0;
 }