]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/cavs.c
Add a bounds check on AVProbeData input.
[ffmpeg] / libavcodec / cavs.c
index 60826f717ce6f996f6a18aa0aeabd1a93b637441..c5f48480ad5da3161b288e4f1f886a3563ec4b0f 100644 (file)
@@ -2,18 +2,20 @@
  * Chinese AVS video (AVS1-P2, JiZhun profile) decoder.
  * Copyright (c) 2006  Stefan Gehrer <stefan.gehrer@gmx.de>
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
@@ -27,6 +29,7 @@
 #include "bitstream.h"
 #include "golomb.h"
 #include "mpegvideo.h"
+#include "cavs.h"
 #include "cavsdata.h"
 
 typedef struct {
@@ -78,11 +81,13 @@ typedef struct {
     int qp;
     int qp_fixed;
     int cbp;
+    ScanTable scantable;
 
     /** intra prediction is done with un-deblocked samples
      they are saved here before deblocking the MB  */
     uint8_t *top_border_y, *top_border_u, *top_border_v;
-    uint8_t left_border_y[16], left_border_u[10], left_border_v[10];
+    uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
+    uint8_t intern_border_y[26];
     uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
 
     void (*intra_pred_l[8])(uint8_t *d,uint8_t *top,uint8_t *left,int stride);
@@ -96,6 +101,7 @@ typedef struct {
     int scale_den[2];  ///< for scaling neighbouring MVs
 
     int got_keyframe;
+    DCTELEM *block;
 } AVSContext;
 
 /*****************************************************************************
@@ -122,9 +128,9 @@ static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
 }
 
 #define SET_PARAMS                                            \
-    alpha = alpha_tab[clip(qp_avg + h->alpha_offset,0,63)];   \
-    beta  =  beta_tab[clip(qp_avg + h->beta_offset, 0,63)];   \
-    tc    =    tc_tab[clip(qp_avg + h->alpha_offset,0,63)];
+    alpha = alpha_tab[av_clip(qp_avg + h->alpha_offset,0,63)];   \
+    beta  =  beta_tab[av_clip(qp_avg + h->beta_offset, 0,63)];   \
+    tc    =    tc_tab[av_clip(qp_avg + h->alpha_offset,0,63)];
 
 /**
  * in-loop deblocking filter for a single macroblock
@@ -151,8 +157,8 @@ static void filter_mb(AVSContext *h, enum mb_t mb_type) {
     memcpy(&h->top_border_u[h->mbx*10+1], h->cu +  7* h->c_stride,8);
     memcpy(&h->top_border_v[h->mbx*10+1], h->cv +  7* h->c_stride,8);
     for(i=0;i<8;i++) {
-        h->left_border_y[i*2+0] = *(h->cy + 15 + (i*2+0)*h->l_stride);
-        h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+1)*h->l_stride);
+        h->left_border_y[i*2+1] = *(h->cy + 15 + (i*2+0)*h->l_stride);
+        h->left_border_y[i*2+2] = *(h->cy + 15 + (i*2+1)*h->l_stride);
         h->left_border_u[i+1] = *(h->cu + 7 + i*h->c_stride);
         h->left_border_v[i+1] = *(h->cv + 7 + i*h->c_stride);
     }
@@ -211,25 +217,26 @@ static void filter_mb(AVSContext *h, enum mb_t mb_type) {
  ****************************************************************************/
 
 static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
-                                        uint8_t *left, int block) {
+                                        uint8_t **left, int block) {
     int i;
 
     switch(block) {
     case 0:
-        memcpy(&left[1],h->left_border_y,16);
-        left[0] = left[1];
-        left[17] = left[16];
+        *left = h->left_border_y;
+        h->left_border_y[0] = h->left_border_y[1];
+        memset(&h->left_border_y[17],h->left_border_y[16],9);
         memcpy(&top[1],&h->top_border_y[h->mbx*16],16);
         top[17] = top[16];
         top[0] = top[1];
         if((h->flags & A_AVAIL) && (h->flags & B_AVAIL))
-            left[0] = top[0] = h->topleft_border_y;
+            h->left_border_y[0] = top[0] = h->topleft_border_y;
         break;
     case 1:
+        *left = h->intern_border_y;
         for(i=0;i<8;i++)
-            left[i+1] = *(h->cy + 7 + i*h->l_stride);
-        memset(&left[9],left[8],9);
-        left[0] = left[1];
+            h->intern_border_y[i+1] = *(h->cy + 7 + i*h->l_stride);
+        memset(&h->intern_border_y[9],h->intern_border_y[8],9);
+        h->intern_border_y[0] = h->intern_border_y[1];
         memcpy(&top[1],&h->top_border_y[h->mbx*16+8],8);
         if(h->flags & C_AVAIL)
             memcpy(&top[9],&h->top_border_y[(h->mbx + 1)*16],8);
@@ -238,22 +245,21 @@ static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
         top[17] = top[16];
         top[0] = top[1];
         if(h->flags & B_AVAIL)
-            left[0] = top[0] = h->top_border_y[h->mbx*16+7];
+            h->intern_border_y[0] = top[0] = h->top_border_y[h->mbx*16+7];
         break;
     case 2:
-        memcpy(&left[1],&h->left_border_y[8],8);
-        memset(&left[9],left[8],9);
+        *left = &h->left_border_y[8];
         memcpy(&top[1],h->cy + 7*h->l_stride,16);
         top[17] = top[16];
-        left[0] = h->left_border_y[7];
         top[0] = top[1];
         if(h->flags & A_AVAIL)
-            top[0] = left[0];
+            top[0] = h->left_border_y[8];
         break;
     case 3:
-        for(i=0;i<9;i++)
-            left[i] = *(h->cy + 7 + (i+7)*h->l_stride);
-        memset(&left[9],left[8],9);
+        *left = &h->intern_border_y[8];
+        for(i=0;i<8;i++)
+            h->intern_border_y[i+9] = *(h->cy + 7 + (i+8)*h->l_stride);
+        memset(&h->intern_border_y[17],h->intern_border_y[16],9);
         memcpy(&top[0],h->cy + 7 + 7*h->l_stride,9);
         memset(&top[9],top[8],9);
         break;
@@ -288,7 +294,7 @@ static void intra_pred_plane(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
     int x,y,ia;
     int ih = 0;
     int iv = 0;
-    uint8_t *cm = cropTbl + MAX_NEG_CROP;
+    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
 
     for(x=0; x<4; x++) {
         ih += (x+1)*(top[5+x]-top[3-x]);
@@ -648,14 +654,13 @@ static int decode_residual_block(AVSContext *h, GetBitContext *gb,
     int dqm = dequant_mul[qp];
     int dqs = dequant_shift[qp];
     int dqa = 1 << (dqs - 1);
-    const uint8_t *scantab = ff_zigzag_direct;
-    DCTELEM block[64];
+    const uint8_t *scantab = h->scantable.permutated;
+    DCTELEM *block = h->block;
 
-    memset(block,0,64*sizeof(DCTELEM));
     for(i=0;i<65;i++) {
         level_code = get_ue_code(gb,r->golomb_order);
         if(level_code >= ESCAPE_CODE) {
-            run = (level_code - ESCAPE_CODE) >> 1;
+            run = ((level_code - ESCAPE_CODE) >> 1) + 1;
             esc_code = get_ue_code(gb,esc_golomb_order);
             level = esc_code + (run > r->max_run ? 1 : r->level_add[run]);
             while(level > r->inc_limit)
@@ -663,8 +668,6 @@ static int decode_residual_block(AVSContext *h, GetBitContext *gb,
             mask = -(level_code & 1);
             level = (level^mask) - mask;
         } else {
-            if(level_code < 0)
-                return -1;
             level = r->rltab[level_code][0];
             if(!level) //end of block signal
                 break;
@@ -676,7 +679,7 @@ static int decode_residual_block(AVSContext *h, GetBitContext *gb,
     }
     /* inverse scan and dequantization */
     while(--i >= 0){
-        pos += 1 + run_buf[i];
+        pos += run_buf[i];
         if(pos > 63) {
             av_log(h->s.avctx, AV_LOG_ERROR,
                    "position out of block bounds at pic %d MB(%d,%d)\n",
@@ -818,7 +821,7 @@ static int decode_mb_i(AVSContext *h, int cbp_code) {
     GetBitContext *gb = &h->s.gb;
     int block, pred_mode_uv;
     uint8_t top[18];
-    uint8_t left[18];
+    uint8_t *left = NULL;
     uint8_t *d;
 
     init_mb(h);
@@ -877,7 +880,7 @@ static int decode_mb_i(AVSContext *h, int cbp_code) {
     /* luma intra prediction interleaved with residual decode/transform/add */
     for(block=0;block<4;block++) {
         d = h->cy + h->luma_scan[block];
-        load_intra_pred_luma(h, top, left, block);
+        load_intra_pred_luma(h, top, &left, block);
         h->intra_pred_l[h->pred_mode_Y[scan3x3[block]]]
             (d, top, left, h->l_stride);
         if(h->cbp & (1<<block))
@@ -1136,8 +1139,10 @@ static int decode_pic(AVSContext *h) {
     enum mb_t mb_type;
 
     if (!s->context_initialized) {
+        s->avctx->idct_algo = FF_IDCT_CAVS;
         if (MPV_common_init(s) < 0)
             return -1;
+        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);
     }
     get_bits(&s->gb,16);//bbv_dwlay
     if(h->stc == PIC_PB_START_CODE) {
@@ -1282,11 +1287,11 @@ static void init_top_lines(AVSContext *h) {
     /* alloc space for co-located MVs and types */
     h->col_mv       = av_malloc( h->mb_width*h->mb_height*4*sizeof(vector_t));
     h->col_type_base = av_malloc(h->mb_width*h->mb_height);
+    h->block        = av_mallocz(64*sizeof(DCTELEM));
 }
 
 static int decode_seq_header(AVSContext *h) {
     MpegEncContext *s = &h->s;
-    extern const AVRational ff_frame_rate_tab[];
     int frame_rate_code;
 
     h->profile =         get_bits(&s->gb,8);
@@ -1313,50 +1318,7 @@ static int decode_seq_header(AVSContext *h) {
     return 0;
 }
 
-/**
- * finds the end of the current frame in the bitstream.
- * @return the position of the first byte of the next frame, or -1
- */
-int ff_cavs_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size) {
-    int pic_found, i;
-    uint32_t state;
-
-    pic_found= pc->frame_start_found;
-    state= pc->state;
-
-    i=0;
-    if(!pic_found){
-        for(i=0; i<buf_size; i++){
-            state= (state<<8) | buf[i];
-            if(state == PIC_I_START_CODE || state == PIC_PB_START_CODE){
-                i++;
-                pic_found=1;
-                break;
-            }
-        }
-    }
-
-    if(pic_found){
-        /* EOF considered as end of frame */
-        if (buf_size == 0)
-            return 0;
-        for(; i<buf_size; i++){
-            state= (state<<8) | buf[i];
-            if((state&0xFFFFFF00) == 0x100){
-                if(state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE){
-                    pc->frame_start_found=0;
-                    pc->state=-1;
-                    return i-3;
-                }
-            }
-        }
-    }
-    pc->frame_start_found= pic_found;
-    pc->state= state;
-    return END_NOT_FOUND;
-}
-
-void ff_cavs_flush(AVCodecContext * avctx) {
+static void cavs_flush(AVCodecContext * avctx) {
     AVSContext *h = avctx->priv_data;
     h->got_keyframe = 0;
 }
@@ -1389,7 +1351,7 @@ static int cavs_decode_frame(AVCodecContext * avctx,void *data, int *data_size,
             return FFMAX(0, buf_ptr - buf - s->parse_context.last_index);
         input_size = (buf_end - buf_ptr)*8;
         switch(stc) {
-        case SEQ_START_CODE:
+        case CAVS_START_CODE:
             init_get_bits(&s->gb, buf_ptr, input_size);
             decode_seq_header(h);
             break;
@@ -1479,6 +1441,7 @@ static int cavs_decode_end(AVCodecContext * avctx) {
     av_free(h->top_border_v);
     av_free(h->col_mv);
     av_free(h->col_type_base);
+    av_free(h->block);
     return 0;
 }
 
@@ -1492,5 +1455,5 @@ AVCodec cavs_decoder = {
     cavs_decode_end,
     cavs_decode_frame,
     CODEC_CAP_DR1 | CODEC_CAP_DELAY,
-    .flush= ff_cavs_flush,
+    .flush= cavs_flush,
 };