]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/cavs.c
Add a bounds check on AVProbeData input.
[ffmpeg] / libavcodec / cavs.c
index 0ed152217659f9ed96d2b030f6ef8da6af868f3e..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;
 
 /*****************************************************************************
@@ -104,35 +110,27 @@ typedef struct {
  *
  ****************************************************************************/
 
-static inline int get_bs_p(vector_t *mvP, vector_t *mvQ) {
+static inline int get_bs(vector_t *mvP, vector_t *mvQ, int b) {
     if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA))
         return 2;
-    if(mvP->ref != mvQ->ref)
-        return 1;
     if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
         return 1;
-    return 0;
-}
-
-static inline int get_bs_b(vector_t *mvP, vector_t *mvQ) {
-    if((mvP->ref == REF_INTRA) || (mvQ->ref == REF_INTRA)) {
-        return 2;
-    } else {
-        vector_t *mvPbw = mvP + MV_BWD_OFFS;
-        vector_t *mvQbw = mvQ + MV_BWD_OFFS;
-        if( (abs(  mvP->x -   mvQ->x) >= 4) ||
-            (abs(  mvP->y -   mvQ->y) >= 4) ||
-            (abs(mvPbw->x - mvQbw->x) >= 4) ||
-            (abs(mvPbw->y - mvQbw->y) >= 4) )
+    if(b){
+        mvP += MV_BWD_OFFS;
+        mvQ += MV_BWD_OFFS;
+        if( (abs(mvP->x - mvQ->x) >= 4) ||  (abs(mvP->y - mvQ->y) >= 4) )
+            return 1;
+    }else{
+        if(mvP->ref != mvQ->ref)
             return 1;
     }
     return 0;
 }
 
 #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
@@ -159,58 +157,29 @@ 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);
     }
     if(!h->loop_filter_disable) {
-        /* clear bs */
-        *((uint64_t *)bs) = 0;
         /* determine bs */
-        switch(mb_type) {
-        case I_8X8:
+        if(mb_type == I_8X8)
             *((uint64_t *)bs) = 0x0202020202020202ULL;
-            break;
-        case P_8X8:
-        case P_8X16:
-            bs[2] = get_bs_p(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
-            bs[3] = get_bs_p(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3]);
-        case P_16X8:
-            bs[6] = get_bs_p(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
-            bs[7] = get_bs_p(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3]);
-        case P_16X16:
-        case P_SKIP:
-            bs[0] = get_bs_p(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
-            bs[1] = get_bs_p(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
-            bs[4] = get_bs_p(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
-            bs[5] = get_bs_p(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
-            break;
-        case B_SKIP:
-        case B_DIRECT:
-        case B_8X8:
-            bs[2] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
-            bs[3] = get_bs_b(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3]);
-            bs[6] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
-            bs[7] = get_bs_b(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3]);
-        case B_FWD_16X16:
-        case B_BWD_16X16:
-        case B_SYM_16X16:
-            bs[0] = get_bs_b(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
-            bs[1] = get_bs_b(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
-            bs[4] = get_bs_b(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
-            bs[5] = get_bs_b(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
-            break;
-        default:
-            if(mb_type & 1) { //16X8
-                bs[6] = bs[7] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2]);
-            } else {          //8X16
-                bs[2] = bs[3] = get_bs_b(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1]);
+        else{
+            *((uint64_t *)bs) = 0;
+            if(partition_flags[mb_type] & SPLITV){
+                bs[2] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X1], mb_type > P_8X8);
+                bs[3] = get_bs(&h->mv[MV_FWD_X2], &h->mv[MV_FWD_X3], mb_type > P_8X8);
+            }
+            if(partition_flags[mb_type] & SPLITH){
+                bs[6] = get_bs(&h->mv[MV_FWD_X0], &h->mv[MV_FWD_X2], mb_type > P_8X8);
+                bs[7] = get_bs(&h->mv[MV_FWD_X1], &h->mv[MV_FWD_X3], mb_type > P_8X8);
             }
-            bs[0] = get_bs_b(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0]);
-            bs[1] = get_bs_b(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2]);
-            bs[4] = get_bs_b(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0]);
-            bs[5] = get_bs_b(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1]);
+            bs[0] = get_bs(&h->mv[MV_FWD_A1], &h->mv[MV_FWD_X0], mb_type > P_8X8);
+            bs[1] = get_bs(&h->mv[MV_FWD_A3], &h->mv[MV_FWD_X2], mb_type > P_8X8);
+            bs[4] = get_bs(&h->mv[MV_FWD_B2], &h->mv[MV_FWD_X0], mb_type > P_8X8);
+            bs[5] = get_bs(&h->mv[MV_FWD_B3], &h->mv[MV_FWD_X1], mb_type > P_8X8);
         }
         if( *((uint64_t *)bs) ) {
             if(h->flags & A_AVAIL) {
@@ -248,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);
@@ -275,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;
@@ -299,7 +268,7 @@ static inline void load_intra_pred_luma(AVSContext *h, uint8_t *top,
 
 static void intra_pred_vert(uint8_t *d,uint8_t *top,uint8_t *left,int stride) {
     int y;
-    uint64_t a = *((uint64_t *)(&top[1]));
+    uint64_t a = unaligned64(&top[1]);
     for(y=0;y<8;y++) {
         *((uint64_t *)(d+y*stride)) = a;
     }
@@ -325,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]);
@@ -485,28 +454,35 @@ static inline void mc_part_std(AVSContext *h,int square,int chroma_height,int de
     }
 }
 
-static void inter_pred(AVSContext *h) {
-    /* always do 8x8 blocks TODO: are larger blocks worth it? */
-    mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
+static void inter_pred(AVSContext *h, enum mb_t mb_type) {
+    if(partition_flags[mb_type] == 0){ // 16x16
+        mc_part_std(h, 1, 8, 0, h->cy, h->cu, h->cv, 0, 0,
+                h->s.dsp.put_cavs_qpel_pixels_tab[0],
+                h->s.dsp.put_h264_chroma_pixels_tab[0],
+                h->s.dsp.avg_cavs_qpel_pixels_tab[0],
+                h->s.dsp.avg_h264_chroma_pixels_tab[0],&h->mv[MV_FWD_X0]);
+    }else{
+        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 0,
                 h->s.dsp.put_cavs_qpel_pixels_tab[1],
                 h->s.dsp.put_h264_chroma_pixels_tab[1],
                 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
                 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X0]);
-    mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
+        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 0,
                 h->s.dsp.put_cavs_qpel_pixels_tab[1],
                 h->s.dsp.put_h264_chroma_pixels_tab[1],
                 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
                 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X1]);
-    mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
+        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 0, 4,
                 h->s.dsp.put_cavs_qpel_pixels_tab[1],
                 h->s.dsp.put_h264_chroma_pixels_tab[1],
                 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
                 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X2]);
-    mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
+        mc_part_std(h, 1, 4, 0, h->cy, h->cu, h->cv, 4, 4,
                 h->s.dsp.put_cavs_qpel_pixels_tab[1],
                 h->s.dsp.put_h264_chroma_pixels_tab[1],
                 h->s.dsp.avg_cavs_qpel_pixels_tab[1],
                 h->s.dsp.avg_h264_chroma_pixels_tab[1],&h->mv[MV_FWD_X3]);
+    }
     /* set intra prediction modes to default values */
     h->pred_mode_Y[3] =  h->pred_mode_Y[6] = INTRA_L_LP;
     h->top_pred_Y[h->mbx*2+0] = h->top_pred_Y[h->mbx*2+1] = INTRA_L_LP;
@@ -606,62 +582,37 @@ static void mv_pred(AVSContext *h, enum mv_loc_t nP, enum mv_loc_t nC,
     vector_t *mvA = &h->mv[nP-1];
     vector_t *mvB = &h->mv[nP-4];
     vector_t *mvC = &h->mv[nC];
-    int mvAref = mvA->ref;
-    int mvBref = mvB->ref;
-    int mvCref;
+    const vector_t *mvP2 = NULL;
 
     mvP->ref = ref;
     mvP->dist = h->dist[mvP->ref];
     if(mvC->ref == NOT_AVAIL)
         mvC = &h->mv[nP-5]; // set to top-left (mvD)
-    mvCref = mvC->ref;
-    if(mode == MV_PRED_PSKIP) {
-        if((mvAref == NOT_AVAIL) || (mvBref == NOT_AVAIL) ||
+    if((mode == MV_PRED_PSKIP) &&
+       ((mvA->ref == NOT_AVAIL) || (mvB->ref == NOT_AVAIL) ||
            ((mvA->x | mvA->y | mvA->ref) == 0)  ||
-           ((mvB->x | mvB->y | mvB->ref) == 0) ) {
-            mvP->x = mvP->y = 0;
-            set_mvs(mvP,size);
-            return;
-        }
-    }
+           ((mvB->x | mvB->y | mvB->ref) == 0) )) {
+        mvP2 = &un_mv;
     /* if there is only one suitable candidate, take it */
-    if((mvAref >= 0) && (mvBref < 0) && (mvCref < 0)) {
-        mvP->x = mvA->x;
-        mvP->y = mvA->y;
-    } else if((mvAref < 0) && (mvBref >= 0) && (mvCref < 0)) {
-        mvP->x = mvB->x;
-        mvP->y = mvB->y;
-    } else if((mvAref < 0) && (mvBref < 0) && (mvCref >= 0)) {
-        mvP->x = mvC->x;
-        mvP->y = mvC->y;
-    } else {
-        switch(mode) {
-        case MV_PRED_LEFT:
-            if(mvAref == mvP->ref) {
-                mvP->x = mvA->x;
-                mvP->y = mvA->y;
-            } else
-                mv_pred_median(h, mvP, mvA, mvB, mvC);
-            break;
-        case MV_PRED_TOP:
-            if(mvBref == mvP->ref) {
-                mvP->x = mvB->x;
-                mvP->y = mvB->y;
-            } else
-                mv_pred_median(h, mvP, mvA, mvB, mvC);
-            break;
-        case MV_PRED_TOPRIGHT:
-            if(mvCref == mvP->ref) {
-                mvP->x = mvC->x;
-                mvP->y = mvC->y;
-            } else
-                mv_pred_median(h, mvP, mvA, mvB, mvC);
-            break;
-        default:
-            mv_pred_median(h, mvP, mvA, mvB, mvC);
-            break;
-        }
+    } else if((mvA->ref >= 0) && (mvB->ref < 0) && (mvC->ref < 0)) {
+        mvP2= mvA;
+    } else if((mvA->ref < 0) && (mvB->ref >= 0) && (mvC->ref < 0)) {
+        mvP2= mvB;
+    } else if((mvA->ref < 0) && (mvB->ref < 0) && (mvC->ref >= 0)) {
+        mvP2= mvC;
+    } else if(mode == MV_PRED_LEFT     && mvA->ref == ref){
+        mvP2= mvA;
+    } else if(mode == MV_PRED_TOP      && mvB->ref == ref){
+        mvP2= mvB;
+    } else if(mode == MV_PRED_TOPRIGHT && mvC->ref == ref){
+        mvP2= mvC;
     }
+    if(mvP2){
+        mvP->x = mvP2->x;
+        mvP->y = mvP2->y;
+    }else
+        mv_pred_median(h, mvP, mvA, mvB, mvC);
+
     if(mode < MV_PRED_PSKIP) {
         mvP->x += get_se_golomb(&h->s.gb);
         mvP->y += get_se_golomb(&h->s.gb);
@@ -703,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)
@@ -718,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;
@@ -731,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",
@@ -767,7 +715,7 @@ static inline int decode_residual_inter(AVSContext *h) {
 
     /* get quantizer */
     if(h->cbp && !h->qp_fixed)
-        h->qp += get_se_golomb(&h->s.gb);
+        h->qp = (h->qp + get_se_golomb(&h->s.gb)) & 63;
     for(block=0;block<4;block++)
         if(h->cbp & (1<<block))
             decode_residual_block(h,&h->s.gb,inter_2dvlc,0,h->qp,
@@ -873,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);
@@ -927,12 +875,12 @@ static int decode_mb_i(AVSContext *h, int cbp_code) {
     }
     h->cbp = cbp_tab[cbp_code][0];
     if(h->cbp && !h->qp_fixed)
-        h->qp += get_se_golomb(gb); //qp_delta
+        h->qp = (h->qp + get_se_golomb(gb)) & 63; //qp_delta
 
     /* 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))
@@ -1008,7 +956,7 @@ static void decode_mb_p(AVSContext *h, enum mb_t mb_type) {
         mv_pred(h, MV_FWD_X2, MV_FWD_X1, MV_PRED_MEDIAN,   BLK_8X8, ref[2]);
         mv_pred(h, MV_FWD_X3, MV_FWD_X0, MV_PRED_MEDIAN,   BLK_8X8, ref[3]);
     }
-    inter_pred(h);
+    inter_pred(h, mb_type);
     store_mvs(h);
     if(mb_type != P_SKIP)
         decode_residual_inter(h);
@@ -1088,20 +1036,16 @@ static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
         break;
     default:
         assert((mb_type > B_SYM_16X16) && (mb_type < B_8X8));
-        flags = b_partition_flags[(mb_type-1)>>1];
+        flags = partition_flags[mb_type];
         if(mb_type & 1) { /* 16x8 macroblock types */
             if(flags & FWD0)
                 mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
-            if(flags & SYM0) {
-                mv_pred(h, MV_FWD_X0, MV_FWD_C2, MV_PRED_TOP,  BLK_16X8, 1);
+            if(flags & SYM0)
                 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_16X8);
-            }
             if(flags & FWD1)
                 mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
-            if(flags & SYM1) {
-                mv_pred(h, MV_FWD_X2, MV_FWD_A1, MV_PRED_LEFT, BLK_16X8, 1);
-                mv_pred_sym(h, &h->mv[9], BLK_16X8);
-            }
+            if(flags & SYM1)
+                mv_pred_sym(h, &h->mv[MV_FWD_X2], BLK_16X8);
             if(flags & BWD0)
                 mv_pred(h, MV_BWD_X0, MV_BWD_C2, MV_PRED_TOP,  BLK_16X8, 0);
             if(flags & BWD1)
@@ -1109,23 +1053,19 @@ static void decode_mb_b(AVSContext *h, enum mb_t mb_type) {
         } else {          /* 8x16 macroblock types */
             if(flags & FWD0)
                 mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
-            if(flags & SYM0) {
-                mv_pred(h, MV_FWD_X0, MV_FWD_B3, MV_PRED_LEFT, BLK_8X16, 1);
+            if(flags & SYM0)
                 mv_pred_sym(h, &h->mv[MV_FWD_X0], BLK_8X16);
-            }
             if(flags & FWD1)
                 mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
-            if(flags & SYM1) {
-                mv_pred(h, MV_FWD_X1, MV_FWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 1);
-                mv_pred_sym(h, &h->mv[6], BLK_8X16);
-            }
+            if(flags & SYM1)
+                mv_pred_sym(h, &h->mv[MV_FWD_X1], BLK_8X16);
             if(flags & BWD0)
                 mv_pred(h, MV_BWD_X0, MV_BWD_B3, MV_PRED_LEFT, BLK_8X16, 0);
             if(flags & BWD1)
                 mv_pred(h, MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT,BLK_8X16, 0);
         }
     }
-    inter_pred(h);
+    inter_pred(h, mb_type);
     if(mb_type != B_SKIP)
         decode_residual_inter(h);
     filter_mb(h,mb_type);
@@ -1199,12 +1139,18 @@ 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) {
         h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;
+        if(h->pic_type > FF_B_TYPE) {
+            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");
+            return -1;
+        }
         /* make sure we have the reference frames we need */
         if(!h->DPB[0].data[0] ||
           (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))
@@ -1341,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);
@@ -1372,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;
 }
@@ -1448,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;
@@ -1538,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;
 }
 
@@ -1551,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,
 };