]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpeg12.c
* using macros to shorten init part
[ffmpeg] / libavcodec / mpeg12.c
index fba90e71441c5d4149b2fa941ea8a9dec6fa9289..5a87f42873bf48265e9879d9213014689416441a 100644 (file)
@@ -1,20 +1,20 @@
 /*
  * MPEG1 encoder / MPEG2 decoder
- * Copyright (c) 2000,2001 Gerard Lantau.
+ * Copyright (c) 2000,2001 Fabrice Bellard.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This library 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.
  *
- * This program is distributed in the hope that it will be useful,
+ * This library 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 //#define DEBUG
 #include "avcodec.h"
 
 #include "mpeg12data.h"
 
+#if 1
+#define PRINT_QP(a, b) {}
+#else
+#define PRINT_QP(a, b) printf(a, b)
+#endif
+
 /* Start codes. */
 #define SEQ_END_CODE           0x000001b7
 #define SEQ_START_CODE         0x000001b3
 #define EXT_START_CODE         0x000001b5
 #define USER_START_CODE                0x000001b2
 
-#define ABS(a) ((a)<0 ? -(a) : (a))
+#define DC_VLC_BITS 9
+#define MV_VLC_BITS 9
+#define MBINCR_VLC_BITS 9
+#define MB_PAT_VLC_BITS 9
+#define MB_PTYPE_VLC_BITS 6
+#define MB_BTYPE_VLC_BITS 6
+#define TEX_VLC_BITS 9
 
 static void mpeg1_encode_block(MpegEncContext *s, 
                          DCTELEM *block, 
                          int component);
 static void mpeg1_encode_motion(MpegEncContext *s, int val);
 static void mpeg1_skip_picture(MpegEncContext *s, int pict_num);
-static int mpeg1_decode_block(MpegEncContext *s, 
+static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
+                              DCTELEM *block, 
+                              int n);
+static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
                               DCTELEM *block, 
                               int n);
-static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
+static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
                                         DCTELEM *block, 
                                         int n);
-static int mpeg2_decode_block_intra(MpegEncContext *s, 
+static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
                                     DCTELEM *block, 
                                     int n);
 static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred);
 
-static UINT16 mv_penalty[MAX_FCODE][MAX_MV*2+1];
+static UINT16 mv_penalty[MAX_FCODE+1][MAX_MV*2+1];
 static UINT8 fcode_tab[MAX_MV*2+1];
 
+static inline int get_bits_diff(MpegEncContext *s){
+    int bits,ret;
+    
+    bits= get_bit_count(&s->pb);
+    ret= bits - s->last_bits;
+    s->last_bits=bits;
+    
+    return ret;
+}
+
+static void init_2d_vlc_rl(RLTable *rl)
+{
+    int i;
+    
+    init_vlc(&rl->vlc, TEX_VLC_BITS, rl->n + 2, 
+             &rl->table_vlc[0][1], 4, 2,
+             &rl->table_vlc[0][0], 4, 2);
+
+    
+    rl->rl_vlc[0]= av_malloc(rl->vlc.table_size*sizeof(RL_VLC_ELEM));
+    for(i=0; i<rl->vlc.table_size; i++){
+        int code= rl->vlc.table[i][0];
+        int len = rl->vlc.table[i][1];
+        int level, run;
+    
+        if(len==0){ // illegal code
+            run= 65;
+            level= MAX_LEVEL;
+        }else if(len<0){ //more bits needed
+            run= 0;
+            level= code;
+        }else{
+            if(code==rl->n){ //esc
+                run= 65;
+                level= 0;
+            }else if(code==rl->n+1){ //eob
+                run= 0;
+                level= 127;
+            }else{
+                run=   rl->table_run  [code] + 1;
+                level= rl->table_level[code];
+            }
+        }
+        rl->rl_vlc[0][i].len= len;
+        rl->rl_vlc[0][i].level= level;
+        rl->rl_vlc[0][i].run= run;
+    }
+}
+
+
 static void put_header(MpegEncContext *s, int header)
 {
     align_put_bits(&s->pb);
@@ -96,9 +161,12 @@ static void mpeg1_encode_sequence_header(MpegEncContext *s)
                 v = 0x3ffff;
             put_bits(&s->pb, 18, v);
             put_bits(&s->pb, 1, 1); /* marker */
-            /* vbv buffer size: slightly greater than an I frame. We add
-               some margin just in case */
-            vbv_buffer_size = (3 * s->I_frame_bits) / (2 * 8);
+
+            if(s->avctx->rc_buffer_size)
+                vbv_buffer_size = s->avctx->rc_buffer_size;
+            else
+                /* VBV calculation: Scaled so that a VCD has the proper VBV size of 40 kilobytes */
+                vbv_buffer_size = (( 20 * s->bit_rate) / (1151929 / 2)) * 8 * 1024;     
             put_bits(&s->pb, 10, (vbv_buffer_size + 16383) / 16384); 
             put_bits(&s->pb, 1, 1); /* constrained parameter flag */
             put_bits(&s->pb, 1, 0); /* no custom intra matrix */
@@ -186,41 +254,14 @@ static void mpeg1_skip_picture(MpegEncContext *s, int pict_num)
     put_bits(&s->pb, 1, 1); 
 }
 
-void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
+static void common_init(MpegEncContext *s)
 {
-    static int done=0;
-
-    if (!done) {
-       int i;
-        done = 1;
-        init_rl(&rl_mpeg1);
-       
-       for(i=0; i<64; i++)
-       {
-               mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
-               mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
-       }
-
-       /* build unified dc encoding tables */
-       for(i=-255; i<256; i++)
-       {
-               int adiff, index;
-               int bits, code;
-               int diff=i;
-
-               adiff = ABS(diff);
-               if(diff<0) diff--;
-               index = vlc_dc_table[adiff];
+    s->y_dc_scale_table=
+    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
+}
 
-               bits= vlc_dc_lum_bits[index] + index;
-               code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
-               mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
-               
-               bits= vlc_dc_chroma_bits[index] + index;
-               code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
-               mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
-       }
-    }
+void mpeg1_encode_picture_header(MpegEncContext *s, int picture_number)
+{
     mpeg1_encode_sequence_header(s);
 
     /* mpeg1 picture header */
@@ -267,6 +308,10 @@ void mpeg1_encode_mb(MpegEncContext *s,
         (!((mb_x | mb_y) == 0 ||
            (mb_x == s->mb_width - 1 && mb_y == s->mb_height - 1)))) {
         s->mb_incr++;
+        s->qscale -= s->dquant;
+        s->skip_count++;
+        s->misc_bits++;
+        s->last_bits++;
     } else {
         /* output mb incr */
         mb_incr = s->mb_incr;
@@ -279,26 +324,58 @@ void mpeg1_encode_mb(MpegEncContext *s,
                  mbAddrIncrTable[mb_incr - 1][0]);
         
         if (s->pict_type == I_TYPE) {
-            put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
+            if(s->dquant && cbp){
+                put_bits(&s->pb, 2, 1); /* macroblock_type : macroblock_quant = 1 */
+                put_bits(&s->pb, 5, s->qscale);
+            }else{
+                put_bits(&s->pb, 1, 1); /* macroblock_type : macroblock_quant = 0 */
+                s->qscale -= s->dquant;
+            }
+            s->misc_bits+= get_bits_diff(s);
+            s->i_count++;
         } else {
             if (s->mb_intra) {
-                put_bits(&s->pb, 5, 0x03);
+                if(s->dquant && cbp){
+                    put_bits(&s->pb, 6, 0x01);
+                    put_bits(&s->pb, 5, s->qscale);
+                }else{
+                    put_bits(&s->pb, 5, 0x03);
+                    s->qscale -= s->dquant;
+                }
+                s->misc_bits+= get_bits_diff(s);
+                s->i_count++;
             } else {
                 if (cbp != 0) {
                     if (motion_x == 0 && motion_y == 0) {
-                        put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
+                        if(s->dquant){
+                            put_bits(&s->pb, 5, 1); /* macroblock_pattern & quant */
+                            put_bits(&s->pb, 5, s->qscale);
+                        }else{
+                            put_bits(&s->pb, 2, 1); /* macroblock_pattern only */
+                        }
+                        s->misc_bits+= get_bits_diff(s);
                         put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
                     } else {
-                        put_bits(&s->pb, 1, 1); /* motion + cbp */
+                        if(s->dquant){
+                            put_bits(&s->pb, 5, 2); /* motion + cbp */
+                            put_bits(&s->pb, 5, s->qscale);
+                        }else{
+                            put_bits(&s->pb, 1, 1); /* motion + cbp */
+                        }
+                        s->misc_bits+= get_bits_diff(s);
                         mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 
                         mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 
+                        s->mv_bits+= get_bits_diff(s);
                         put_bits(&s->pb, mbPatTable[cbp - 1][1], mbPatTable[cbp - 1][0]);
                     }
                 } else {
                     put_bits(&s->pb, 3, 1); /* motion only */
                     mpeg1_encode_motion(s, motion_x - s->last_mv[0][0][0]); 
                     mpeg1_encode_motion(s, motion_y - s->last_mv[0][0][1]); 
+                    s->qscale -= s->dquant;
+                    s->mv_bits+= get_bits_diff(s);
                 }
+                s->f_count++;
             }
         }
         for(i=0;i<6;i++) {
@@ -307,6 +384,10 @@ void mpeg1_encode_mb(MpegEncContext *s,
             }
         }
         s->mb_incr = 1;
+        if(s->mb_intra)
+            s->i_tex_bits+= get_bits_diff(s);
+        else
+            s->p_tex_bits+= get_bits_diff(s);
     }
     s->last_mv[0][0][0] = motion_x;
     s->last_mv[0][0][1] = motion_y;
@@ -356,14 +437,46 @@ static void mpeg1_encode_motion(MpegEncContext *s, int val)
     }
 }
 
-void mpeg1_encode_init(MpegEncContext *s)
+void ff_mpeg1_encode_init(MpegEncContext *s)
 {
     static int done=0;
+
+    common_init(s);
+
     if(!done){
         int f_code;
         int mv;
+       int i;
 
         done=1;
+        init_rl(&rl_mpeg1);
+       
+       for(i=0; i<64; i++)
+       {
+               mpeg1_max_level[0][i]= rl_mpeg1.max_level[0][i];
+               mpeg1_index_run[0][i]= rl_mpeg1.index_run[0][i];
+       }
+
+       /* build unified dc encoding tables */
+       for(i=-255; i<256; i++)
+       {
+               int adiff, index;
+               int bits, code;
+               int diff=i;
+
+               adiff = ABS(diff);
+               if(diff<0) diff--;
+               index = vlc_dc_table[adiff];
+
+               bits= vlc_dc_lum_bits[index] + index;
+               code= (vlc_dc_lum_code[index]<<index) + (diff & ((1 << index) - 1));
+               mpeg1_lum_dc_uni[i+255]= bits + (code<<8);
+               
+               bits= vlc_dc_chroma_bits[index] + index;
+               code= (vlc_dc_chroma_code[index]<<index) + (diff & ((1 << index) - 1));
+               mpeg1_chr_dc_uni[i+255]= bits + (code<<8);
+       }
+
         for(f_code=1; f_code<=MAX_FCODE; f_code++){
             for(mv=-MAX_MV; mv<=MAX_MV; mv++){
                 int len;
@@ -399,10 +512,13 @@ void mpeg1_encode_init(MpegEncContext *s)
         }
     }
     s->mv_penalty= mv_penalty;
-    
     s->fcode_tab= fcode_tab;
+    s->min_qcoeff=-255;
+    s->max_qcoeff= 255;
+    s->intra_quant_bias= 3<<(QUANT_BIAS_SHIFT-3); //(a + x*3/8)/x
+    s->inter_quant_bias= 0;
 }
+
 static inline void encode_dc(MpegEncContext *s, int diff, int component)
 {
     if (component == 0) {
@@ -455,7 +571,7 @@ static void mpeg1_encode_block(MpegEncContext *s,
     last_non_zero = i - 1;
 
     for(;i<=last_index;i++) {
-        j = zigzag_direct[i];
+        j = s->intra_scantable.permutated[i];
         level = block[j];
     next_coef:
 #if 0
@@ -465,26 +581,11 @@ static void mpeg1_encode_block(MpegEncContext *s,
         /* encode using VLC */
         if (level != 0) {
             run = i - last_non_zero - 1;
-#ifdef ARCH_X86
-            asm volatile(
-               "movl %2, %1            \n\t"
-               "movl %1, %0            \n\t"
-               "addl %1, %1            \n\t"
-               "sbbl %1, %1            \n\t"
-               "xorl %1, %0            \n\t"
-               "subl %1, %0            \n\t"
-               "andl $1, %1            \n\t"
-               : "=&r" (alevel), "=&r" (sign)
-               : "g" (level)
-           );
-#else
-            sign = 0;
-            alevel = level;
-            if (alevel < 0) {
-               sign = 1;
-                alevel = -alevel;
-           }
-#endif
+            
+            alevel= level;
+            MASK_ABS(sign, alevel)
+            sign&=1;
+
 //            code = get_rl_index(rl, 0, run, alevel);
             if (alevel > mpeg1_max_level[0][run])
                 code= 111; /*rl->n*/
@@ -527,43 +628,40 @@ static VLC mb_ptype_vlc;
 static VLC mb_btype_vlc;
 static VLC mb_pat_vlc;
 
-void mpeg1_init_vlc(MpegEncContext *s)
+static void init_vlcs(MpegEncContext *s)
 {
     static int done = 0;
 
     if (!done) {
+        done = 1;
 
-        init_vlc(&dc_lum_vlc, 9, 12, 
+        init_vlc(&dc_lum_vlc, DC_VLC_BITS, 12, 
                  vlc_dc_lum_bits, 1, 1,
                  vlc_dc_lum_code, 2, 2);
-        init_vlc(&dc_chroma_vlc, 9, 12, 
+        init_vlc(&dc_chroma_vlc,  DC_VLC_BITS, 12, 
                  vlc_dc_chroma_bits, 1, 1,
                  vlc_dc_chroma_code, 2, 2);
-        init_vlc(&mv_vlc, 9, 17, 
+        init_vlc(&mv_vlc, MV_VLC_BITS, 17, 
                  &mbMotionVectorTable[0][1], 2, 1,
                  &mbMotionVectorTable[0][0], 2, 1);
-        init_vlc(&mbincr_vlc, 9, 35, 
+        init_vlc(&mbincr_vlc, MBINCR_VLC_BITS, 35, 
                  &mbAddrIncrTable[0][1], 2, 1,
                  &mbAddrIncrTable[0][0], 2, 1);
-        init_vlc(&mb_pat_vlc, 9, 63, 
+        init_vlc(&mb_pat_vlc, MB_PAT_VLC_BITS, 63, 
                  &mbPatTable[0][1], 2, 1,
                  &mbPatTable[0][0], 2, 1);
         
-        init_vlc(&mb_ptype_vlc, 6, 32, 
+        init_vlc(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 32, 
                  &table_mb_ptype[0][1], 2, 1,
                  &table_mb_ptype[0][0], 2, 1);
-        init_vlc(&mb_btype_vlc, 6, 32, 
+        init_vlc(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 32, 
                  &table_mb_btype[0][1], 2, 1,
                  &table_mb_btype[0][0], 2, 1);
         init_rl(&rl_mpeg1);
         init_rl(&rl_mpeg2);
-        /* cannot use generic init because we must add the EOB code */
-        init_vlc(&rl_mpeg1.vlc, 9, rl_mpeg1.n + 2, 
-                 &rl_mpeg1.table_vlc[0][1], 4, 2,
-                 &rl_mpeg1.table_vlc[0][0], 4, 2);
-        init_vlc(&rl_mpeg2.vlc, 9, rl_mpeg2.n + 2, 
-                 &rl_mpeg2.table_vlc[0][1], 4, 2,
-                 &rl_mpeg2.table_vlc[0][0], 4, 2);
+
+        init_2d_vlc_rl(&rl_mpeg1);
+        init_2d_vlc_rl(&rl_mpeg2);
     }
 }
 
@@ -600,33 +698,8 @@ static inline int get_qscale(MpegEncContext *s)
 static int mpeg_decode_mb(MpegEncContext *s,
                           DCTELEM block[6][64])
 {
-    int i, j, k, cbp, val, code, mb_type, motion_type;
+    int i, j, k, cbp, val, mb_type, motion_type;
     
-    /* skip mb handling */
-    if (s->mb_incr == 0) {
-        /* read again increment */
-        s->mb_incr = 1;
-        for(;;) {
-            code = get_vlc(&s->gb, &mbincr_vlc);
-            if (code < 0)
-                return 1; /* error = end of slice */
-            if (code >= 33) {
-                if (code == 33) {
-                    s->mb_incr += 33;
-                }
-                /* otherwise, stuffing, nothing to do */
-            } else {
-                s->mb_incr += code;
-                break;
-            }
-        }
-    }
-    if (++s->mb_x >= s->mb_width) {
-        s->mb_x = 0;
-        if (s->mb_y >= (s->mb_height - 1))
-            return -1;
-        s->mb_y++;
-    }
     dprintf("decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
 
     if (--s->mb_incr != 0) {
@@ -664,14 +737,18 @@ static int mpeg_decode_mb(MpegEncContext *s,
         }
         break;
     case P_TYPE:
-        mb_type = get_vlc(&s->gb, &mb_ptype_vlc);
-        if (mb_type < 0)
+        mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
+        if (mb_type < 0){
+            fprintf(stderr, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
             return -1;
+        }
         break;
     case B_TYPE:
-        mb_type = get_vlc(&s->gb, &mb_btype_vlc);
-        if (mb_type < 0)
+        mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
+        if (mb_type < 0){
+            fprintf(stderr, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
             return -1;
+        }
         break;
     }
     dprintf("mb_type=%x\n", mb_type);
@@ -839,9 +916,11 @@ static int mpeg_decode_mb(MpegEncContext *s,
     }
     
     if (mb_type & MB_PAT) {
-        cbp = get_vlc(&s->gb, &mb_pat_vlc);
-        if (cbp < 0)
+        cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
+        if (cbp < 0){
+            fprintf(stderr, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
             return -1;
+        }
         cbp++;
     }
     dprintf("cbp=%x\n", cbp);
@@ -849,25 +928,36 @@ static int mpeg_decode_mb(MpegEncContext *s,
     if (s->mpeg2) {
         if (s->mb_intra) {
             for(i=0;i<6;i++) {
-                if (cbp & (1 << (5 - i))) {
-                    if (mpeg2_decode_block_intra(s, block[i], i) < 0)
-                        return -1;
-                }
+                if (mpeg2_decode_block_intra(s, block[i], i) < 0)
+                    return -1;
             }
         } else {
             for(i=0;i<6;i++) {
-                if (cbp & (1 << (5 - i))) {
+                if (cbp & 32) {
                     if (mpeg2_decode_block_non_intra(s, block[i], i) < 0)
                         return -1;
+                } else {
+                    s->block_last_index[i] = -1;
                 }
+                cbp+=cbp;
             }
         }
     } else {
-        for(i=0;i<6;i++) {
-            if (cbp & (1 << (5 - i))) {
-                if (mpeg1_decode_block(s, block[i], i) < 0)
+        if (s->mb_intra) {
+            for(i=0;i<6;i++) {
+                if (mpeg1_decode_block_intra(s, block[i], i) < 0)
                     return -1;
             }
+        }else{
+            for(i=0;i<6;i++) {
+                if (cbp & 32) {
+                    if (mpeg1_decode_block_inter(s, block[i], i) < 0)
+                        return -1;
+                } else {
+                    s->block_last_index[i] = -1;
+                }
+                cbp+=cbp;
+            }
         }
     }
     return 0;
@@ -878,7 +968,7 @@ static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
 {
     int code, sign, val, m, l, shift;
 
-    code = get_vlc(&s->gb, &mv_vlc);
+    code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
     if (code < 0) {
         return 0xffff;
     }
@@ -911,12 +1001,14 @@ static inline int decode_dc(MpegEncContext *s, int component)
     int code, diff;
 
     if (component == 0) {
-        code = get_vlc(&s->gb, &dc_lum_vlc);
+        code = get_vlc2(&s->gb, dc_lum_vlc.table, DC_VLC_BITS, 2);
     } else {
-        code = get_vlc(&s->gb, &dc_chroma_vlc);
+        code = get_vlc2(&s->gb, dc_chroma_vlc.table, DC_VLC_BITS, 2);
     }
-    if (code < 0)
+    if (code < 0){
+        fprintf(stderr, "invalid dc code at %d %d\n", s->mb_x, s->mb_y);
         return 0xffff;
+    }
     if (code == 0) {
         diff = 0;
     } else {
@@ -927,183 +1019,256 @@ static inline int decode_dc(MpegEncContext *s, int component)
     return diff;
 }
 
-static int mpeg1_decode_block(MpegEncContext *s, 
+static inline int mpeg1_decode_block_intra(MpegEncContext *s, 
                                DCTELEM *block, 
                                int n)
 {
     int level, dc, diff, i, j, run;
-    int code, component;
+    int component;
     RLTable *rl = &rl_mpeg1;
+    UINT8 * const scantable= s->intra_scantable.permutated;
+    const UINT16 *quant_matrix= s->intra_matrix;
+    const int qscale= s->qscale;
 
-    if (s->mb_intra) {
-        /* DC coef */
-        component = (n <= 3 ? 0 : n - 4 + 1);
-        diff = decode_dc(s, component);
-        if (diff >= 0xffff)
-            return -1;
-        dc = s->last_dc[component];
-        dc += diff;
-        s->last_dc[component] = dc;
-        block[0] = dc;
-        dprintf("dc=%d diff=%d\n", dc, diff);
-        i = 1;
-    } else {
-        int bit_cnt, v;
-        UINT32 bit_buf;
-        UINT8 *buf_ptr;
-        i = 0;
-        /* special case for the first coef. no need to add a second vlc table */
-        SAVE_BITS(&s->gb);
-        SHOW_BITS(&s->gb, v, 2);
-        if (v & 2) {
-            run = 0;
-            level = 1 - ((v & 1) << 1);
-            FLUSH_BITS(2);
-            RESTORE_BITS(&s->gb);
-            goto add_coef;
+    /* DC coef */
+    component = (n <= 3 ? 0 : n - 4 + 1);
+    diff = decode_dc(s, component);
+    if (diff >= 0xffff)
+        return -1;
+    dc = s->last_dc[component];
+    dc += diff;
+    s->last_dc[component] = dc;
+    block[0] = dc<<3;
+    dprintf("dc=%d diff=%d\n", dc, diff);
+    i = 0;
+    {
+        OPEN_READER(re, &s->gb);    
+        /* now quantify & encode AC coefs */
+        for(;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
+            
+            if(level == 127){
+                break;
+            } else if(level != 0) {
+                i += run;
+                j = scantable[i];
+                level= (level*qscale*quant_matrix[j])>>3;
+                level= (level-1)|1;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+                if (level == -128) {
+                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
+                } else if (level == 0) {
+                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
+                }
+                i += run;
+                j = scantable[i];
+                if(level<0){
+                    level= -level;
+                    level= (level*qscale*quant_matrix[j])>>3;
+                    level= (level-1)|1;
+                    level= -level;
+                }else{
+                    level= (level*qscale*quant_matrix[j])>>3;
+                    level= (level-1)|1;
+                }
+            }
+            if (i > 63){
+                fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+
+            block[j] = level;
         }
-        RESTORE_BITS(&s->gb);
+        CLOSE_READER(re, &s->gb);
     }
+    s->block_last_index[n] = i;
+   return 0;
+}
 
-    /* now quantify & encode AC coefs */
-    for(;;) {
-        code = get_vlc(&s->gb, &rl->vlc);
-        if (code < 0) {
-            return -1;
+static inline int mpeg1_decode_block_inter(MpegEncContext *s, 
+                               DCTELEM *block, 
+                               int n)
+{
+    int level, i, j, run;
+    RLTable *rl = &rl_mpeg1;
+    UINT8 * const scantable= s->intra_scantable.permutated;
+    const UINT16 *quant_matrix= s->inter_matrix;
+    const int qscale= s->qscale;
+
+    {
+        int v;
+        OPEN_READER(re, &s->gb);
+        i = -1;
+        /* special case for the first coef. no need to add a second vlc table */
+        UPDATE_CACHE(re, &s->gb);
+        v= SHOW_UBITS(re, &s->gb, 2);
+        if (v & 2) {
+            LAST_SKIP_BITS(re, &s->gb, 2);
+            level= (3*qscale*quant_matrix[0])>>4;
+            level= (level-1)|1;
+            if(v&1)
+                level= -level;
+            block[0] = level;
+            i++;
         }
-        if (code == 112) {
-            break;
-        } else if (code == 111) {
-            /* escape */
-            run = get_bits(&s->gb, 6);
-            level = get_bits(&s->gb, 8);
-            level = (level << 24) >> 24;
-            if (level == -128) {
-                level = get_bits(&s->gb, 8) - 256;
-            } else if (level == 0) {
-                level = get_bits(&s->gb, 8);
+
+        /* now quantify & encode AC coefs */
+        for(;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
+            
+            if(level == 127){
+                break;
+            } else if(level != 0) {
+                i += run;
+                j = scantable[i];
+                level= ((level*2+1)*qscale*quant_matrix[j])>>4;
+                level= (level-1)|1;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
+                if (level == -128) {
+                    level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
+                } else if (level == 0) {
+                    level = SHOW_UBITS(re, &s->gb, 8)      ; LAST_SKIP_BITS(re, &s->gb, 8);
+                }
+                i += run;
+                j = scantable[i];
+                if(level<0){
+                    level= -level;
+                    level= ((level*2+1)*qscale*quant_matrix[j])>>4;
+                    level= (level-1)|1;
+                    level= -level;
+                }else{
+                    level= ((level*2+1)*qscale*quant_matrix[j])>>4;
+                    level= (level-1)|1;
+                }
             }
-        } else {
-            run = rl->table_run[code];
-            level = rl->table_level[code];
-            if (get_bits1(&s->gb))
-                level = -level;
+            if (i > 63){
+                fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+
+            block[j] = level;
         }
-        i += run;
-        if (i >= 64)
-            return -1;
-    add_coef:
-        dprintf("%d: run=%d level=%d\n", n, run, level);
-       j = zigzag_direct[i];
-        block[j] = level;
-        i++;
+        CLOSE_READER(re, &s->gb);
     }
-    s->block_last_index[n] = i-1;
+    s->block_last_index[n] = i;
     return 0;
 }
 
 /* Also does unquantization here, since I will never support mpeg2
    encoding */
-static int mpeg2_decode_block_non_intra(MpegEncContext *s, 
-                                        DCTELEM *block, 
-                                        int n)
+static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, 
+                               DCTELEM *block, 
+                               int n)
 {
     int level, i, j, run;
-    int code;
     RLTable *rl = &rl_mpeg1;
-    const UINT8 *scan_table;
-    const UINT16 *matrix;
+    UINT8 * const scantable= s->intra_scantable.permutated;
+    const UINT16 *quant_matrix;
+    const int qscale= s->qscale;
     int mismatch;
 
-    if (s->alternate_scan)
-        scan_table = ff_alternate_vertical_scan;
-    else
-        scan_table = zigzag_direct;
     mismatch = 1;
 
     {
-        int bit_cnt, v;
-        UINT32 bit_buf;
-        UINT8 *buf_ptr;
-        i = 0;
-        if (n < 4) 
-            matrix = s->non_intra_matrix;
+        int v;
+        OPEN_READER(re, &s->gb);
+        i = -1;
+        if (n < 4)
+            quant_matrix = s->inter_matrix;
         else
-            matrix = s->chroma_non_intra_matrix;
-            
+            quant_matrix = s->chroma_inter_matrix;
+
         /* special case for the first coef. no need to add a second vlc table */
-        SAVE_BITS(&s->gb);
-        SHOW_BITS(&s->gb, v, 2);
+        UPDATE_CACHE(re, &s->gb);
+        v= SHOW_UBITS(re, &s->gb, 2);
         if (v & 2) {
-            run = 0;
-            level = 1 - ((v & 1) << 1);
-            FLUSH_BITS(2);
-            RESTORE_BITS(&s->gb);
-            goto add_coef;
+            LAST_SKIP_BITS(re, &s->gb, 2);
+            level= (3*qscale*quant_matrix[0])>>5;
+            if(v&1)
+                level= -level;
+            block[0] = level;
+            mismatch ^= level;
+            i++;
         }
-        RESTORE_BITS(&s->gb);
-    }
 
-    /* now quantify & encode AC coefs */
-    for(;;) {
-        code = get_vlc(&s->gb, &rl->vlc);
-        if (code < 0)
-            return -1;
-        if (code == 112) {
-            break;
-        } else if (code == 111) {
-            /* escape */
-            run = get_bits(&s->gb, 6);
-            level = get_bits(&s->gb, 12);
-            level = (level << 20) >> 20;
-        } else {
-            run = rl->table_run[code];
-            level = rl->table_level[code];
-            if (get_bits1(&s->gb))
-                level = -level;
-        }
-        i += run;
-        if (i >= 64)
-            return -1;
-    add_coef:
-       j = scan_table[i];
-        dprintf("%d: run=%d level=%d\n", n, run, level);
-        /* XXX: optimize */
-        if (level > 0) {
-            level = ((level * 2 + 1) * s->qscale * matrix[j]) >> 5;
-        } else {
-            level = ((-level * 2 + 1) * s->qscale * matrix[j]) >> 5;
-            level = -level;
+        /* now quantify & encode AC coefs */
+        for(;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
+            
+            if(level == 127){
+                break;
+            } else if(level != 0) {
+                i += run;
+                j = scantable[i];
+                level= ((level*2+1)*qscale*quant_matrix[j])>>5;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+
+                i += run;
+                j = scantable[i];
+                if(level<0){
+                    level= ((-level*2+1)*qscale*quant_matrix[j])>>5;
+                    level= -level;
+                }else{
+                    level= ((level*2+1)*qscale*quant_matrix[j])>>5;
+                }
+            }
+            if (i > 63){
+                fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+            
+            mismatch ^= level;
+            block[j] = level;
         }
-        /* XXX: is it really necessary to saturate since the encoder
-           knows whats going on ? */
-        mismatch ^= level;
-        block[j] = level;
-        i++;
+        CLOSE_READER(re, &s->gb);
     }
     block[63] ^= (mismatch & 1);
+    
     s->block_last_index[n] = i;
     return 0;
 }
 
-static int mpeg2_decode_block_intra(MpegEncContext *s, 
-                                    DCTELEM *block, 
-                                    int n)
+static inline int mpeg2_decode_block_intra(MpegEncContext *s, 
+                               DCTELEM *block, 
+                               int n)
 {
     int level, dc, diff, i, j, run;
-    int code, component;
+    int component;
     RLTable *rl;
-    const UINT8 *scan_table;
-    const UINT16 *matrix;
+    UINT8 * const scantable= s->intra_scantable.permutated;
+    const UINT16 *quant_matrix;
+    const int qscale= s->qscale;
     int mismatch;
 
-    if (s->alternate_scan)
-        scan_table = ff_alternate_vertical_scan;
-    else
-        scan_table = zigzag_direct;
-
     /* DC coef */
-    component = (n <= 3 ? 0 : n - 4 + 1);
+    if (n < 4){
+        quant_matrix = s->intra_matrix;
+        component = 0; 
+    }else{
+        quant_matrix = s->chroma_intra_matrix;
+        component = n - 3;
+    }
     diff = decode_dc(s, component);
     if (diff >= 0xffff)
         return -1;
@@ -1113,47 +1278,53 @@ static int mpeg2_decode_block_intra(MpegEncContext *s,
     block[0] = dc << (3 - s->intra_dc_precision);
     dprintf("dc=%d\n", block[0]);
     mismatch = block[0] ^ 1;
-    i = 1;
+    i = 0;
     if (s->intra_vlc_format)
         rl = &rl_mpeg2;
     else
         rl = &rl_mpeg1;
-    if (n < 4) 
-        matrix = s->intra_matrix;
-    else
-        matrix = s->chroma_intra_matrix;
 
-    /* now quantify & encode AC coefs */
-    for(;;) {
-        code = get_vlc(&s->gb, &rl->vlc);
-        if (code < 0)
-            return -1;
-        if (code == 112) {
-            break;
-        } else if (code == 111) {
-            /* escape */
-            run = get_bits(&s->gb, 6);
-            level = get_bits(&s->gb, 12);
-            level = (level << 20) >> 20;
-        } else {
-            run = rl->table_run[code];
-            level = rl->table_level[code];
-            if (get_bits1(&s->gb))
-                level = -level;
+    {
+        OPEN_READER(re, &s->gb);    
+        /* now quantify & encode AC coefs */
+        for(;;) {
+            UPDATE_CACHE(re, &s->gb);
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2);
+            
+            if(level == 127){
+                break;
+            } else if(level != 0) {
+                i += run;
+                j = scantable[i];
+                level= (level*qscale*quant_matrix[j])>>4;
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
+                LAST_SKIP_BITS(re, &s->gb, 1);
+            } else {
+                /* escape */
+                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
+                UPDATE_CACHE(re, &s->gb);
+                level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
+                i += run;
+                j = scantable[i];
+                if(level<0){
+                    level= (-level*qscale*quant_matrix[j])>>4;
+                    level= -level;
+                }else{
+                    level= (level*qscale*quant_matrix[j])>>4;
+                }
+            }
+            if (i > 63){
+                fprintf(stderr, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
+                return -1;
+            }
+            
+            mismatch^= level;
+            block[j] = level;
         }
-        i += run;
-        if (i >= 64)
-            return -1;
-       j = scan_table[i];
-        dprintf("%d: run=%d level=%d\n", n, run, level);
-        level = (level * s->qscale * matrix[j]) / 16;
-        /* XXX: is it really necessary to saturate since the encoder
-           knows whats going on ? */
-        mismatch ^= level;
-        block[j] = level;
-        i++;
+        CLOSE_READER(re, &s->gb);
     }
-    block[63] ^= (mismatch & 1);
+    block[63]^= mismatch&1;
+    
     s->block_last_index[n] = i;
     return 0;
 }
@@ -1175,6 +1346,10 @@ typedef struct Mpeg1Context {
 static int mpeg_decode_init(AVCodecContext *avctx)
 {
     Mpeg1Context *s = avctx->priv_data;
+    
+    s->mpeg_enc_ctx.flags= avctx->flags;
+    common_init(&s->mpeg_enc_ctx);
+    init_vlcs(&s->mpeg_enc_ctx);
 
     s->header_state = 0xff;
     s->mpeg_enc_ctx_allocated = 0;
@@ -1183,6 +1358,8 @@ static int mpeg_decode_init(AVCodecContext *avctx)
     s->buf_ptr = s->buffer;
     s->mpeg_enc_ctx.picture_number = 0;
     s->repeat_field = 0;
+    s->mpeg_enc_ctx.codec_id= avctx->codec->id;
+    avctx->mbskip_table= s->mpeg_enc_ctx.mbskip_table;
     return 0;
 }
 
@@ -1272,6 +1449,7 @@ static void mpeg_decode_sequence_extension(MpegEncContext *s)
         s->frame_rate = (s->frame_rate * frame_rate_ext_n) / frame_rate_ext_d;
     dprintf("sequence extension\n");
     s->mpeg2 = 1;
+    s->avctx->sub_id = 2; /* indicates mpeg2 found */
 }
 
 static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
@@ -1283,7 +1461,7 @@ static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
     if (get_bits1(&s->gb)) {
         for(i=0;i<64;i++) {
             v = get_bits(&s->gb, 8);
-            j = zigzag_direct[i];
+            j= s->idct_permutation[ ff_zigzag_direct[i] ];
             s->intra_matrix[j] = v;
             s->chroma_intra_matrix[j] = v;
         }
@@ -1291,23 +1469,23 @@ static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
     if (get_bits1(&s->gb)) {
         for(i=0;i<64;i++) {
             v = get_bits(&s->gb, 8);
-            j = zigzag_direct[i];
-            s->non_intra_matrix[j] = v;
-            s->chroma_non_intra_matrix[j] = v;
+            j= s->idct_permutation[ ff_zigzag_direct[i] ];
+            s->inter_matrix[j] = v;
+            s->chroma_inter_matrix[j] = v;
         }
     }
     if (get_bits1(&s->gb)) {
         for(i=0;i<64;i++) {
             v = get_bits(&s->gb, 8);
-            j = zigzag_direct[i];
+            j= s->idct_permutation[ ff_zigzag_direct[i] ];
             s->chroma_intra_matrix[j] = v;
         }
     }
     if (get_bits1(&s->gb)) {
         for(i=0;i<64;i++) {
             v = get_bits(&s->gb, 8);
-            j = zigzag_direct[i];
-            s->chroma_non_intra_matrix[j] = v;
+            j= s->idct_permutation[ ff_zigzag_direct[i] ];
+            s->chroma_inter_matrix[j] = v;
         }
     }
 }
@@ -1330,9 +1508,24 @@ static void mpeg_decode_picture_coding_extension(MpegEncContext *s)
     s->repeat_first_field = get_bits1(&s->gb);
     s->chroma_420_type = get_bits1(&s->gb);
     s->progressive_frame = get_bits1(&s->gb);
+    
+    if(s->alternate_scan){
+        ff_init_scantable(s, &s->inter_scantable  , ff_alternate_vertical_scan);
+        ff_init_scantable(s, &s->intra_scantable  , ff_alternate_vertical_scan);
+        ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_vertical_scan);
+        ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
+    }else{
+        ff_init_scantable(s, &s->inter_scantable  , ff_zigzag_direct);
+        ff_init_scantable(s, &s->intra_scantable  , ff_zigzag_direct);
+        ff_init_scantable(s, &s->intra_h_scantable, ff_alternate_horizontal_scan);
+        ff_init_scantable(s, &s->intra_v_scantable, ff_alternate_vertical_scan);
+    }
+    
     /* composite display not parsed */
     dprintf("intra_dc_precision=%d\n", s->intra_dc_precision);
     dprintf("picture_structure=%d\n", s->picture_structure);
+    dprintf("top field first=%d\n", s->top_field_first);
+    dprintf("repeat first field=%d\n", s->repeat_first_field);
     dprintf("conceal=%d\n", s->concealment_motion_vectors);
     dprintf("intra_vlc_format=%d\n", s->intra_vlc_format);
     dprintf("alternate_scan=%d\n", s->alternate_scan);
@@ -1366,7 +1559,18 @@ static void mpeg_decode_extension(AVCodecContext *avctx,
     }
 }
 
-/* return 1 if end of frame */
+#define DECODE_SLICE_FATAL_ERROR -2
+#define DECODE_SLICE_ERROR -1
+#define DECODE_SLICE_OK 0
+#define DECODE_SLICE_EOP 1
+
+/**
+ * decodes a slice.
+ * @return DECODE_SLICE_FATAL_ERROR if a non recoverable error occured<br>
+ *         DECODE_SLICE_ERROR if the slice is damaged<br>
+ *         DECODE_SLICE_OK if this slice is ok<br>
+ *         DECODE_SLICE_EOP if the end of the picture is reached
+ */
 static int mpeg_decode_slice(AVCodecContext *avctx, 
                               AVPicture *pict,
                               int start_code,
@@ -1377,20 +1581,19 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
     int ret;
 
     start_code = (start_code - 1) & 0xff;
-    if (start_code >= s->mb_height)
-        return -1;
+    if (start_code >= s->mb_height){
+        fprintf(stderr, "slice below image (%d >= %d)\n", start_code, s->mb_height);
+        return DECODE_SLICE_ERROR;
+    }
     s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
     s->last_dc[1] = s->last_dc[0];
     s->last_dc[2] = s->last_dc[0];
     memset(s->last_mv, 0, sizeof(s->last_mv));
-    s->mb_x = -1;
-    s->mb_y = start_code;
-    s->mb_incr = 0;
-
     /* start frame decoding */
     if (s->first_slice) {
         s->first_slice = 0;
-        MPV_frame_start(s);
+        if(MPV_frame_start(s, avctx) < 0)
+            return DECODE_SLICE_FATAL_ERROR;
     }
 
     init_get_bits(&s->gb, buf, buf_size);
@@ -1401,26 +1604,79 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
         skip_bits(&s->gb, 8);
     }
 
+    s->mb_x=0;
+    for(;;) {
+        int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
+        if (code < 0)
+            return -1; /* error = end of slice, but empty slice is bad or?*/
+        if (code >= 33) {
+            if (code == 33) {
+                s->mb_x += 33;
+            }
+            /* otherwise, stuffing, nothing to do */
+        } else {
+            s->mb_x += code;
+            break;
+        }
+    }
+    s->mb_y = start_code;
+    s->mb_incr= 1;
+
     for(;;) {
-        memset(s->block, 0, sizeof(s->block));
+       s->dsp.clear_blocks(s->block[0]);
+        
         ret = mpeg_decode_mb(s, s->block);
         dprintf("ret=%d\n", ret);
         if (ret < 0)
             return -1;
-        if (ret == 1)
-            break;
+    
         MPV_decode_mb(s, s->block);
+
+        if (++s->mb_x >= s->mb_width) {
+            ff_draw_horiz_band(s);
+
+            s->mb_x = 0;
+            s->mb_y++;
+            PRINT_QP("%s", "\n");
+        }
+        PRINT_QP("%2d", s->qscale);
+
+        /* skip mb handling */
+        if (s->mb_incr == 0) {
+            /* read again increment */
+            s->mb_incr = 1;
+            for(;;) {
+                int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
+                if (code < 0)
+                    goto eos; /* error = end of slice */
+                if (code >= 33) {
+                    if (code == 33) {
+                        s->mb_incr += 33;
+                    }
+                    /* otherwise, stuffing, nothing to do */
+                } else {
+                    s->mb_incr += code;
+                    break;
+                }
+            }
+        }
+        if(s->mb_y >= s->mb_height){
+            fprintf(stderr, "slice too long\n");
+            return DECODE_SLICE_ERROR;
+        }
     }
+eos: //end of slice
     
+    emms_c();
+
     /* end of slice reached */
-    if (s->mb_x == (s->mb_width - 1) &&
-        s->mb_y == (s->mb_height - 1)) {
+    if (/*s->mb_x == 0 &&*/
+        s->mb_y == s->mb_height) {
         /* end of image */
         UINT8 **picture;
 
         MPV_frame_end(s);
 
-        /* XXX: incorrect reported qscale for mpeg2 */
         if (s->pict_type == B_TYPE) {
             picture = s->current_picture;
             avctx->quality = s->qscale;
@@ -1436,19 +1692,21 @@ static int mpeg_decode_slice(AVCodecContext *avctx,
             s->last_qscale = s->qscale;
             s->picture_number++;
         }
+        if(s->mpeg2)
+            avctx->quality>>=1;
         if (picture) {
             pict->data[0] = picture[0];
             pict->data[1] = picture[1];
             pict->data[2] = picture[2];
             pict->linesize[0] = s->linesize;
-            pict->linesize[1] = s->linesize / 2;
-            pict->linesize[2] = s->linesize / 2;
-            return 1;
+            pict->linesize[1] = s->uvlinesize;
+            pict->linesize[2] = s->uvlinesize;
+            return DECODE_SLICE_EOP;
         } else {
-            return 0;
+            return DECODE_SLICE_OK;
         }
     } else {
-        return 0;
+        return DECODE_SLICE_OK;
     }
 }
 
@@ -1458,7 +1716,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
     Mpeg1Context *s1 = avctx->priv_data;
     MpegEncContext *s = &s1->mpeg_enc_ctx;
     int width, height, i, v, j;
-    
+
     init_get_bits(&s->gb, buf, buf_size);
 
     width = get_bits(&s->gb, 12);
@@ -1482,17 +1740,21 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
         }
         s->width = width;
         s->height = height;
-        s->has_b_frames = 1;
+        avctx->has_b_frames= s->has_b_frames = 1;
         s->avctx = avctx;
         avctx->width = width;
         avctx->height = height;
-        avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
+        if (s->frame_rate_index >= 9) {
+            /* at least give a valid frame rate (some old mpeg1 have this) */
+            avctx->frame_rate = 25 * FRAME_RATE_BASE;
+        } else {
+            avctx->frame_rate = frame_rate_tab[s->frame_rate_index];
+        }
         s->frame_rate = avctx->frame_rate;
         avctx->bit_rate = s->bit_rate;
         
         if (MPV_common_init(s) < 0)
             return -1;
-        mpeg1_init_vlc(s);
         s1->mpeg_enc_ctx_allocated = 1;
     }
 
@@ -1503,41 +1765,43 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
     if (get_bits1(&s->gb)) {
         for(i=0;i<64;i++) {
             v = get_bits(&s->gb, 8);
-            j = zigzag_direct[i];
+            j = s->intra_scantable.permutated[i];
             s->intra_matrix[j] = v;
             s->chroma_intra_matrix[j] = v;
         }
 #ifdef DEBUG
         dprintf("intra matrix present\n");
         for(i=0;i<64;i++)
-            dprintf(" %d", s->intra_matrix[zigzag_direct[i]]);
+            dprintf(" %d", s->intra_matrix[s->intra_scantable.permutated[i]]);
         printf("\n");
 #endif
     } else {
         for(i=0;i<64;i++) {
-            v = default_intra_matrix[i];
-            s->intra_matrix[i] = v;
-            s->chroma_intra_matrix[i] = v;
+            int j= s->idct_permutation[i];
+            v = ff_mpeg1_default_intra_matrix[i];
+            s->intra_matrix[j] = v;
+            s->chroma_intra_matrix[j] = v;
         }
     }
     if (get_bits1(&s->gb)) {
         for(i=0;i<64;i++) {
             v = get_bits(&s->gb, 8);
-            j = zigzag_direct[i];
-            s->non_intra_matrix[j] = v;
-            s->chroma_non_intra_matrix[j] = v;
+            j = s->intra_scantable.permutated[i];
+            s->inter_matrix[j] = v;
+            s->chroma_inter_matrix[j] = v;
         }
 #ifdef DEBUG
         dprintf("non intra matrix present\n");
         for(i=0;i<64;i++)
-            dprintf(" %d", s->non_intra_matrix[zigzag_direct[i]]);
+            dprintf(" %d", s->inter_matrix[s->intra_scantable.permutated[i]]);
         printf("\n");
 #endif
     } else {
         for(i=0;i<64;i++) {
-            v = default_non_intra_matrix[i];
-            s->non_intra_matrix[i] = v;
-            s->chroma_non_intra_matrix[i] = v;
+            int j= s->idct_permutation[i];
+            v = ff_mpeg1_default_non_intra_matrix[i];
+            s->inter_matrix[j] = v;
+            s->chroma_inter_matrix[j] = v;
         }
     }
 
@@ -1547,6 +1811,7 @@ static int mpeg1_decode_sequence(AVCodecContext *avctx,
     s->picture_structure = PICT_FRAME;
     s->frame_pred_frame_dct = 1;
     s->mpeg2 = 0;
+    avctx->sub_id = 1; /* indicates mpeg1 */
     return 0;
 }
 
@@ -1564,7 +1829,7 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
     dprintf("fill_buffer\n");
 
     *data_size = 0;
-    
+
     /* special case for last picture */
     if (buf_size == 0) {
         if (s2->picture_number > 0) {
@@ -1572,8 +1837,8 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
             picture->data[1] = s2->next_picture[1];
             picture->data[2] = s2->next_picture[2];
             picture->linesize[0] = s2->linesize;
-            picture->linesize[1] = s2->linesize / 2;
-            picture->linesize[2] = s2->linesize / 2;
+            picture->linesize[1] = s2->uvlinesize;
+            picture->linesize[2] = s2->uvlinesize;
             *data_size = sizeof(AVPicture);
         }
         return 0;
@@ -1581,15 +1846,18 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
 
     buf_ptr = buf;
     buf_end = buf + buf_size;
-    
-    if (s->repeat_field % 2 == 1) {
+
+#if 0    
+    if (s->repeat_field % 2 == 1) { 
         s->repeat_field++;
         //fprintf(stderr,"\nRepeating last frame: %d -> %d! pict: %d %d", avctx->frame_number-1, avctx->frame_number,
-        //                                                         s2->picture_number, s->repeat_field);
-        *data_size = sizeof(AVPicture);
-        goto the_end;
+        //        s2->picture_number, s->repeat_field);
+        if (avctx->flags & CODEC_FLAG_REPEAT_FIELD) {
+            *data_size = sizeof(AVPicture);
+            goto the_end;
+        }
     }
-        
+#endif
     while (buf_ptr < buf_end) {
         buf_start = buf_ptr;
         /* find start next code */
@@ -1609,7 +1877,17 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
         } else {
             memcpy(s->buf_ptr, buf_start, len);
             s->buf_ptr += len;
-            
+            if(   (!(s2->flags&CODEC_FLAG_TRUNCATED)) && (!start_code_found) 
+               && s->buf_ptr+4<s->buffer+s->buffer_size){
+                start_code_found= 1;
+                code= 0x1FF;
+                s->header_state=0xFF;
+                s->buf_ptr[0]=0;
+                s->buf_ptr[1]=0;
+                s->buf_ptr[2]=1;
+                s->buf_ptr[3]=0xFF;
+                s->buf_ptr+=4;
+            }
             if (start_code_found) {
                 /* prepare data for next start code */
                 input_size = s->buf_ptr - s->buffer;
@@ -1636,18 +1914,35 @@ static int mpeg_decode_frame(AVCodecContext *avctx,
                         start_code <= SLICE_MAX_START_CODE) {
                         ret = mpeg_decode_slice(avctx, picture,
                                                 start_code, s->buffer, input_size);
-                        if (ret == 1) {
+                        if (ret == DECODE_SLICE_EOP) {
                             /* got a picture: exit */
                             /* first check if we must repeat the frame */
+                            avctx->repeat_pict = 0;
+#if 0
                             if (s2->progressive_frame && s2->repeat_first_field) {
                                 //fprintf(stderr,"\nRepeat this frame: %d! pict: %d",avctx->frame_number,s2->picture_number);
-                                s2->repeat_first_field = 0;
-                                s2->progressive_frame = 0;
+                                //s2->repeat_first_field = 0;
+                                //s2->progressive_frame = 0;
                                 if (++s->repeat_field > 2)
                                     s->repeat_field = 0;
+                                avctx->repeat_pict = 1;
                             }
+#endif                      
+                            if (s2->repeat_first_field) {
+                                if (s2->progressive_sequence) {
+                                    if (s2->top_field_first)
+                                        avctx->repeat_pict = 4;
+                                    else
+                                        avctx->repeat_pict = 2;
+                                } else if (s2->progressive_frame) {
+                                    avctx->repeat_pict = 1;
+                                }
+                            }         
                             *data_size = sizeof(AVPicture);
                             goto the_end;
+                        }else if(ret<0){
+                            printf("Error while decoding slice\n");
+                           if(ret==DECODE_SLICE_FATAL_ERROR) return -1;
                         }
                     }
                     break;
@@ -1677,4 +1972,5 @@ AVCodec mpeg_decoder = {
     NULL,
     mpeg_decode_end,
     mpeg_decode_frame,
+    CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED,
 };