]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/msmpeg4.c
Remove declaration of non-existing functions
[ffmpeg] / libavcodec / msmpeg4.c
index 23c41ef4055c9ab38595cc8404d168cab6e143e1..ce3756bb37bd569e286ba058ca7ec664ce55c4b3 100644 (file)
@@ -3,21 +3,23 @@
  * Copyright (c) 2001 Fabrice Bellard.
  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
  *
- * This library is free software; you can redistribute it and/or
+ * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
+ *
+ * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * msmpeg4v1 & v2 stuff by Michael Niedermayer <michaelni@gmx.at>
  */
 
 /**
@@ -28,6 +30,7 @@
 #include "avcodec.h"
 #include "dsputil.h"
 #include "mpegvideo.h"
+#include "msmpeg4.h"
 
 /*
  * You can also call this codec : MPEG4 with a twist !
@@ -40,7 +43,6 @@
 
 #define DC_VLC_BITS 9
 #define CBPY_VLC_BITS 6
-#define INTER_INTRA_VLC_BITS 3
 #define V1_INTRA_CBPC_VLC_BITS 6
 #define V1_INTER_CBPC_VLC_BITS 6
 #define V2_INTRA_CBPC_VLC_BITS 3
@@ -48,8 +50,6 @@
 #define MV_VLC_BITS 9
 #define V2_MV_VLC_BITS 9
 #define TEX_VLC_BITS 9
-#define MB_NON_INTRA_VLC_BITS 9
-#define MB_INTRA_VLC_BITS 9
 
 #define II_BITRATE 128*1024
 #define MBAC_BITRATE 50*1024
 static uint32_t v2_dc_lum_table[512][2];
 static uint32_t v2_dc_chroma_table[512][2];
 
-static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n);
-static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
-                                       int n, int coded, const uint8_t *scantable);
 static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr);
-static int msmpeg4_decode_motion(MpegEncContext * s,
-                                 int *mx_ptr, int *my_ptr);
 static void init_h263_dc_for_msmpeg4(void);
 static inline void msmpeg4_memsetw(short *tab, int val, int n);
 #ifdef CONFIG_ENCODERS
@@ -73,10 +68,9 @@ static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run,
 #endif //CONFIG_ENCODERS
 static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
 static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
-static int wmv2_decode_mb(MpegEncContext *s, DCTELEM block[6][64]);
 
 /* vc1 externs */
-extern uint8_t wmv3_dc_scale_table[32];
+extern const uint8_t wmv3_dc_scale_table[32];
 
 #ifdef DEBUG
 int intra_count = 0;
@@ -85,13 +79,15 @@ int frame_count = 0;
 
 #include "msmpeg4data.h"
 
-#ifdef CONFIG_ENCODERS //strangely gcc includes this even if its not references
+#ifdef CONFIG_ENCODERS //strangely gcc includes this even if it is not references
 static uint8_t rl_length[NB_RL_TABLES][MAX_LEVEL+1][MAX_RUN+1][2];
 #endif //CONFIG_ENCODERS
 
+static uint8_t static_rl_table_store[NB_RL_TABLES][2][2*MAX_RUN + MAX_LEVEL + 3];
+
 static void common_init(MpegEncContext * s)
 {
-    static int inited=0;
+    static int initialized=0;
 
     switch(s->msmpeg4_version){
     case 1:
@@ -131,8 +127,8 @@ static void common_init(MpegEncContext * s)
     }
     //Note the default tables are set in common_init in mpegvideo.c
 
-    if(!inited){
-        inited=1;
+    if(!initialized){
+        initialized=1;
 
         init_h263_dc_for_msmpeg4();
     }
@@ -157,7 +153,7 @@ static void init_mv_table(MVTable *tab)
     }
 }
 
-static void code012(PutBitContext *pb, int n)
+void ff_msmpeg4_code012(PutBitContext *pb, int n)
 {
     if (n == 0) {
         put_bits(pb, 1, 0);
@@ -184,7 +180,7 @@ void ff_msmpeg4_encode_init(MpegEncContext *s)
         init_mv_table(&mv_tables[0]);
         init_mv_table(&mv_tables[1]);
         for(i=0;i<NB_RL_TABLES;i++)
-            init_rl(&rl_table[i], 1);
+            init_rl(&rl_table[i], static_rl_table_store[i]);
 
         for(i=0; i<NB_RL_TABLES; i++){
             int level;
@@ -242,7 +238,7 @@ static int get_size_of_code(MpegEncContext * s, RLTable *rl, int last, int run,
     return size;
 }
 
-static void find_best_tables(MpegEncContext * s)
+void ff_find_best_tables(MpegEncContext * s)
 {
     int i;
     int best       =-1, best_size       =9999999;
@@ -267,7 +263,7 @@ static void find_best_tables(MpegEncContext * s)
                     int intra_luma_count  = s->ac_stats[1][0][level][run][last];
                     int intra_chroma_count= s->ac_stats[1][1][level][run][last];
 
-                    if(s->pict_type==I_TYPE){
+                    if(s->pict_type==FF_I_TYPE){
                         size       += intra_luma_count  *rl_length[i  ][level][run][last];
                         chroma_size+= intra_chroma_count*rl_length[i+3][level][run][last];
                     }else{
@@ -292,7 +288,7 @@ static void find_best_tables(MpegEncContext * s)
 //    printf("type:%d, best:%d, qp:%d, var:%d, mcvar:%d, size:%d //\n",
 //           s->pict_type, best, s->qscale, s->mb_var_sum, s->mc_mb_var_sum, best_size);
 
-    if(s->pict_type==P_TYPE) chroma_best= best;
+    if(s->pict_type==FF_P_TYPE) chroma_best= best;
 
     memset(s->ac_stats, 0, sizeof(int)*(MAX_LEVEL+1)*(MAX_RUN+1)*2*2*2);
 
@@ -301,7 +297,7 @@ static void find_best_tables(MpegEncContext * s)
 
     if(s->pict_type != s->last_non_b_pict_type){
         s->rl_table_index= 2;
-        if(s->pict_type==I_TYPE)
+        if(s->pict_type==FF_I_TYPE)
             s->rl_chroma_table_index= 1;
         else
             s->rl_chroma_table_index= 2;
@@ -312,7 +308,7 @@ static void find_best_tables(MpegEncContext * s)
 /* write MSMPEG4 compatible frame header */
 void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
 {
-    find_best_tables(s);
+    ff_find_best_tables(s);
 
     align_put_bits(&s->pb);
     put_bits(&s->pb, 2, s->pict_type - 1);
@@ -328,10 +324,10 @@ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
     s->use_skip_mb_code = 1; /* only if P frame */
     s->per_mb_rl_table = 0;
     if(s->msmpeg4_version==4)
-        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==P_TYPE);
+        s->inter_intra_pred= (s->width*s->height < 320*240 && s->bit_rate<=II_BITRATE && s->pict_type==FF_P_TYPE);
 //printf("%d %d %d %d %d\n", s->pict_type, s->bit_rate, s->inter_intra_pred, s->width, s->height);
 
-    if (s->pict_type == I_TYPE) {
+    if (s->pict_type == FF_I_TYPE) {
         s->slice_height= s->mb_height/1;
         put_bits(&s->pb, 5, 0x16 + s->mb_height/s->slice_height);
 
@@ -343,8 +339,8 @@ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
 
         if(s->msmpeg4_version>2){
             if(!s->per_mb_rl_table){
-                code012(&s->pb, s->rl_chroma_table_index);
-                code012(&s->pb, s->rl_table_index);
+                ff_msmpeg4_code012(&s->pb, s->rl_chroma_table_index);
+                ff_msmpeg4_code012(&s->pb, s->rl_table_index);
             }
 
             put_bits(&s->pb, 1, s->dc_table_index);
@@ -357,7 +353,7 @@ void msmpeg4_encode_picture_header(MpegEncContext * s, int picture_number)
 
         if(s->msmpeg4_version>2){
             if(!s->per_mb_rl_table)
-                code012(&s->pb, s->rl_table_index);
+                ff_msmpeg4_code012(&s->pb, s->rl_table_index);
 
             put_bits(&s->pb, 1, s->dc_table_index);
 
@@ -389,7 +385,7 @@ void msmpeg4_encode_ext_header(MpegEncContext * s)
 #endif //CONFIG_ENCODERS
 
 /* predict coded block */
-static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
+int ff_msmpeg4_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
 {
     int xy, wrap, pred, a, b, c;
 
@@ -417,7 +413,7 @@ static inline int coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_bl
 
 #ifdef CONFIG_ENCODERS
 
-static void msmpeg4_encode_motion(MpegEncContext * s,
+void ff_msmpeg4_encode_motion(MpegEncContext * s,
                                   int mx, int my)
 {
     int code;
@@ -449,13 +445,13 @@ static void msmpeg4_encode_motion(MpegEncContext * s,
              mv->table_mv_bits[code],
              mv->table_mv_code[code]);
     if (code == mv->n) {
-        /* escape : code litterally */
+        /* escape : code literally */
         put_bits(&s->pb, 6, mx);
         put_bits(&s->pb, 6, my);
     }
 }
 
-static inline void handle_slices(MpegEncContext *s){
+void ff_msmpeg4_handle_slices(MpegEncContext *s){
     if (s->mb_x == 0) {
         if (s->slice_height && (s->mb_y % s->slice_height) == 0) {
             if(s->msmpeg4_version < 4){
@@ -476,7 +472,7 @@ void msmpeg4_encode_mb(MpegEncContext * s,
     int pred_x, pred_y;
     uint8_t *coded_block;
 
-    handle_slices(s);
+    ff_msmpeg4_handle_slices(s);
 
     if (!s->mb_intra) {
         /* compute cbp */
@@ -522,14 +518,14 @@ void msmpeg4_encode_mb(MpegEncContext * s,
 
             /* motion vector */
             h263_pred_motion(s, 0, 0, &pred_x, &pred_y);
-            msmpeg4_encode_motion(s, motion_x - pred_x,
+            ff_msmpeg4_encode_motion(s, motion_x - pred_x,
                                   motion_y - pred_y);
         }
 
         s->mv_bits += get_bits_diff(s);
 
         for (i = 0; i < 6; i++) {
-            msmpeg4_encode_block(s, block[i], i);
+            ff_msmpeg4_encode_block(s, block[i], i);
         }
         s->p_tex_bits += get_bits_diff(s);
     } else {
@@ -542,7 +538,7 @@ void msmpeg4_encode_mb(MpegEncContext * s,
             cbp |= val << (5 - i);
             if (i < 4) {
                 /* predict value for close blocks only for luma */
-                pred = coded_block_pred(s, i, &coded_block);
+                pred = ff_msmpeg4_coded_block_pred(s, i, &coded_block);
                 *coded_block = val;
                 val = val ^ pred;
             }
@@ -554,7 +550,7 @@ void msmpeg4_encode_mb(MpegEncContext * s,
 #endif
 
         if(s->msmpeg4_version<=2){
-            if (s->pict_type == I_TYPE) {
+            if (s->pict_type == FF_I_TYPE) {
                 put_bits(&s->pb,
                          v2_intra_cbpc[cbp&3][1], v2_intra_cbpc[cbp&3][0]);
             } else {
@@ -569,7 +565,7 @@ void msmpeg4_encode_mb(MpegEncContext * s,
                      cbpy_tab[cbp>>2][1],
                      cbpy_tab[cbp>>2][0]);
         }else{
-            if (s->pict_type == I_TYPE) {
+            if (s->pict_type == FF_I_TYPE) {
                 put_bits(&s->pb,
                          ff_msmp4_mb_i_table[coded_cbp][1], ff_msmp4_mb_i_table[coded_cbp][0]);
             } else {
@@ -588,7 +584,7 @@ void msmpeg4_encode_mb(MpegEncContext * s,
         s->misc_bits += get_bits_diff(s);
 
         for (i = 0; i < 6; i++) {
-            msmpeg4_encode_block(s, block[i], i);
+            ff_msmpeg4_encode_block(s, block[i], i);
         }
         s->i_tex_bits += get_bits_diff(s);
         s->i_count++;
@@ -627,10 +623,10 @@ static int get_dc(uint8_t *src, int stride, int scale)
 
 /* dir = 0: left, dir = 1: top prediction */
 static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
-                             uint16_t **dc_val_ptr, int *dir_ptr)
+                             int16_t **dc_val_ptr, int *dir_ptr)
 {
     int a, b, c, wrap, pred, scale;
-    uint16_t *dc_val;
+    int16_t *dc_val;
 
     /* find prediction */
     if (n < 4) {
@@ -657,7 +653,7 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
        necessitate to modify mpegvideo.c. The problem comes from the
        fact they decided to store the quantized DC (which would lead
        to problems if Q could vary !) */
-#if (defined(ARCH_X86) || defined(ARCH_X86_64)) && !defined PIC
+#if (defined(ARCH_X86)) && !defined PIC
     asm volatile(
         "movl %3, %%eax         \n\t"
         "shrl $1, %%eax         \n\t"
@@ -673,7 +669,7 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
         "mull %4                \n\t"
         "movl %%edx, %2         \n\t"
         : "+b" (a), "+c" (b), "+D" (c)
-        : "g" (scale), "S" (inverse[scale])
+        : "g" (scale), "S" (ff_inverse[scale])
         : "%eax", "%edx"
     );
 #else
@@ -778,7 +774,8 @@ static inline int msmpeg4_pred_dc(MpegEncContext * s, int n,
 static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr)
 {
     int sign, code;
-    int pred;
+    int pred, extquant;
+    int extrabits = 0;
 
     if(s->msmpeg4_version==1){
         int32_t *dc_val;
@@ -787,7 +784,7 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr
         /* update predictor */
         *dc_val= level;
     }else{
-        uint16_t *dc_val;
+        int16_t *dc_val;
         pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
 
         /* update predictor */
@@ -820,6 +817,15 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr
         code = level;
         if (code > DC_MAX)
             code = DC_MAX;
+        else if( s->msmpeg4_version>=6 ) {
+            if( s->qscale == 1 ) {
+                extquant = (level + 3) & 0x3;
+                code  = ((level+3)>>2);
+            } else if( s->qscale == 2 ) {
+                extquant = (level + 1) & 0x1;
+                code  = ((level+1)>>1);
+            }
+        }
 
         if (s->dc_table_index == 0) {
             if (n < 4) {
@@ -835,8 +841,13 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr
             }
         }
 
+        if(s->msmpeg4_version>=6 && s->qscale<=2)
+            extrabits = 3 - s->qscale;
+
         if (code == DC_MAX)
-            put_bits(&s->pb, 8, level);
+            put_bits(&s->pb, 8 + extrabits, level);
+        else if(extrabits > 0)//== VC1 && s->qscale<=2
+            put_bits(&s->pb, extrabits, extquant);
 
         if (level != 0) {
             put_bits(&s->pb, 1, sign);
@@ -847,7 +858,7 @@ static void msmpeg4_encode_dc(MpegEncContext * s, int level, int n, int *dir_ptr
 /* Encoding of a block. Very similar to MPEG4 except for a different
    escape coding (same as H263) and more vlc tables.
  */
-static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
+void ff_msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int n)
 {
     int level, run, last, i, j, last_index;
     int last_non_zero, sign, slevel;
@@ -863,7 +874,7 @@ static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int
         } else {
             rl = &rl_table[3 + s->rl_chroma_table_index];
         }
-        run_diff = 0;
+        run_diff = s->msmpeg4_version>=4;
         scantable= s->intra_scantable.permutated;
     } else {
         i = 0;
@@ -876,7 +887,7 @@ static inline void msmpeg4_encode_block(MpegEncContext * s, DCTELEM * block, int
     }
 
     /* recalculate block_last_index for M$ wmv1 */
-    if(s->msmpeg4_version>=4 && s->block_last_index[n]>0){
+    if(s->msmpeg4_version>=4 && s->msmpeg4_version<6 && s->block_last_index[n]>0){
         for(last_index=63; last_index>=0; last_index--){
             if(block[scantable[last_index]]) break;
         }
@@ -922,6 +933,9 @@ else
                     run1 = run - rl->max_run[last][level] - run_diff;
                     if (run1 < 0)
                         goto esc3;
+                    code = get_rl_index(rl, last, run1+1, level);
+                    if (s->msmpeg4_version == 4 && code == rl->n)
+                        goto esc3;
                     code = get_rl_index(rl, last, run1, level);
                     if (code == rl->n) {
                     esc3:
@@ -932,8 +946,9 @@ else
                             if(s->esc3_level_length==0){
                                 s->esc3_level_length=8;
                                 s->esc3_run_length= 6;
+                                //ESCLVLSZ + ESCRUNSZ
                                 if(s->qscale<8)
-                                    put_bits(&s->pb, 6, 3);
+                                    put_bits(&s->pb, 6 + (s->msmpeg4_version>=6), 3);
                                 else
                                     put_bits(&s->pb, 8, 3);
                             }
@@ -967,10 +982,7 @@ else
 /****************************************/
 /* decoding stuff */
 
-static VLC mb_non_intra_vlc[4];
-VLC ff_msmp4_mb_i_vlc;
-VLC ff_msmp4_dc_luma_vlc[2];
-VLC ff_msmp4_dc_chroma_vlc[2];
+VLC ff_mb_non_intra_vlc[4];
 static VLC v2_dc_lum_vlc;
 static VLC v2_dc_chroma_vlc;
 static VLC cbpy_vlc;
@@ -979,9 +991,10 @@ static VLC v2_mb_type_vlc;
 static VLC v2_mv_vlc;
 static VLC v1_intra_cbpc_vlc;
 static VLC v1_inter_cbpc_vlc;
-static VLC inter_intra_vlc;
+VLC ff_inter_intra_vlc;
 
-/* this table is practically identical to the one from h263 except that its inverted */
+/* This table is practically identical to the one from h263
+ * except that it is inverted. */
 static void init_h263_dc_for_msmpeg4(void)
 {
         int level, uni_code, uni_len;
@@ -1004,7 +1017,7 @@ static void init_h263_dc_for_msmpeg4(void)
             /* luminance h263 */
             uni_code= DCtab_lum[size][0];
             uni_len = DCtab_lum[size][1];
-            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
+            uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
 
             if (size > 0) {
                 uni_code<<=size; uni_code|=l;
@@ -1020,7 +1033,7 @@ static void init_h263_dc_for_msmpeg4(void)
             /* chrominance h263 */
             uni_code= DCtab_chrom[size][0];
             uni_len = DCtab_chrom[size][1];
-            uni_code ^= (1<<uni_len)-1; //M$ doesnt like compatibility
+            uni_code ^= (1<<uni_len)-1; //M$ does not like compatibility
 
             if (size > 0) {
                 uni_code<<=size; uni_code|=l;
@@ -1049,7 +1062,7 @@ int ff_msmpeg4_decode_init(MpegEncContext *s)
         done = 1;
 
         for(i=0;i<NB_RL_TABLES;i++) {
-            init_rl(&rl_table[i], 1);
+            init_rl(&rl_table[i], static_rl_table_store[i]);
             init_vlc_rl(&rl_table[i], 1);
         }
         for(i=0;i<2;i++) {
@@ -1093,7 +1106,7 @@ int ff_msmpeg4_decode_init(MpegEncContext *s)
                  &mvtab[0][0], 2, 1, 1);
 
         for(i=0; i<4; i++){
-            init_vlc(&mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
+            init_vlc(&ff_mb_non_intra_vlc[i], MB_NON_INTRA_VLC_BITS, 128,
                      &wmv2_inter_table[i][0][1], 8, 4,
                      &wmv2_inter_table[i][0][0], 8, 4, 1); //FIXME name?
         }
@@ -1109,7 +1122,7 @@ int ff_msmpeg4_decode_init(MpegEncContext *s)
                  inter_MCBPC_bits, 1, 1,
                  inter_MCBPC_code, 1, 1, 1);
 
-        init_vlc(&inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
+        init_vlc(&ff_inter_intra_vlc, INTER_INTRA_VLC_BITS, 4,
                  &table_inter_intra[0][1], 2, 1,
                  &table_inter_intra[0][0], 2, 1, 1);
     }
@@ -1124,13 +1137,14 @@ int ff_msmpeg4_decode_init(MpegEncContext *s)
         s->decode_mb= msmpeg4v34_decode_mb;
         break;
     case 5:
-        s->decode_mb= wmv2_decode_mb;
+        if (ENABLE_WMV2_DECODER)
+            s->decode_mb= ff_wmv2_decode_mb;
     case 6:
         //FIXME + TODO VC1 decode mb
         break;
     }
 
-    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame isnt a keyframe
+    s->slice_height= s->mb_height; //to avoid 1/0 if the first frame is not a keyframe
 
     return 0;
 }
@@ -1162,15 +1176,15 @@ return -1;
     }
 
     s->pict_type = get_bits(&s->gb, 2) + 1;
-    if (s->pict_type != I_TYPE &&
-        s->pict_type != P_TYPE){
+    if (s->pict_type != FF_I_TYPE &&
+        s->pict_type != FF_P_TYPE){
         av_log(s->avctx, AV_LOG_ERROR, "invalid picture type\n");
         return -1;
     }
 #if 0
 {
     static int had_i=0;
-    if(s->pict_type == I_TYPE) had_i=1;
+    if(s->pict_type == FF_I_TYPE) had_i=1;
     if(!had_i) return -1;
 }
 #endif
@@ -1180,7 +1194,7 @@ return -1;
         return -1;
     }
 
-    if (s->pict_type == I_TYPE) {
+    if (s->pict_type == FF_I_TYPE) {
         code = get_bits(&s->gb, 5);
         if(s->msmpeg4_version==1){
             if(code==0 || code>s->mb_height){
@@ -1378,7 +1392,7 @@ static void msmpeg4v2_encode_motion(MpegEncContext * s, int val)
 }
 #endif
 
-/* this is identical to h263 except that its range is multiplied by 2 */
+/* This is identical to h263 except that its range is multiplied by 2. */
 static int msmpeg4v2_decode_motion(MpegEncContext * s, int pred, int f_code)
 {
     int code, val, sign, shift;
@@ -1414,7 +1428,7 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
 {
     int cbp, code, i;
 
-    if (s->pict_type == P_TYPE) {
+    if (s->pict_type == FF_P_TYPE) {
         if (s->use_skip_mb_code) {
             if (get_bits1(&s->gb)) {
                 /* skip mb */
@@ -1481,13 +1495,13 @@ static int msmpeg4v12_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
         } else{
             s->ac_pred = 0;
             cbp|= get_vlc2(&s->gb, cbpy_vlc.table, CBPY_VLC_BITS, 1)<<2; //FIXME check errors
-            if(s->pict_type==P_TYPE) cbp^=0x3C;
+            if(s->pict_type==FF_P_TYPE) cbp^=0x3C;
         }
     }
 
     s->dsp.clear_blocks(s->block[0]);
     for (i = 0; i < 6; i++) {
-        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
+        if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
         {
              av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
              return -1;
@@ -1502,7 +1516,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
     uint8_t *coded_val;
     uint32_t * const mb_type_ptr= &s->current_picture.mb_type[ s->mb_x + s->mb_y*s->mb_stride ];
 
-    if (s->pict_type == P_TYPE) {
+    if (s->pict_type == FF_P_TYPE) {
         if (s->use_skip_mb_code) {
             if (get_bits1(&s->gb)) {
                 /* skip mb */
@@ -1520,7 +1534,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
             }
         }
 
-        code = get_vlc2(&s->gb, mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
+        code = get_vlc2(&s->gb, ff_mb_non_intra_vlc[DEFAULT_INTER_INDEX].table, MB_NON_INTRA_VLC_BITS, 3);
         if (code < 0)
             return -1;
         //s->mb_intra = (code & 0x40) ? 0 : 1;
@@ -1537,7 +1551,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
         for(i=0;i<6;i++) {
             int val = ((code >> (5 - i)) & 1);
             if (i < 4) {
-                int pred = coded_block_pred(s, i, &coded_val);
+                int pred = ff_msmpeg4_coded_block_pred(s, i, &coded_val);
                 val = val ^ pred;
                 *coded_val = val;
             }
@@ -1553,7 +1567,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
             s->rl_chroma_table_index = s->rl_table_index;
         }
         h263_pred_motion(s, 0, 0, &mx, &my);
-        if (msmpeg4_decode_motion(s, &mx, &my) < 0)
+        if (ff_msmpeg4_decode_motion(s, &mx, &my) < 0)
             return -1;
         s->mv_dir = MV_DIR_FORWARD;
         s->mv_type = MV_TYPE_16X16;
@@ -1565,7 +1579,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
         s->ac_pred = get_bits1(&s->gb);
         *mb_type_ptr = MB_TYPE_INTRA;
         if(s->inter_intra_pred){
-            s->h263_aic_dir= get_vlc2(&s->gb, inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
+            s->h263_aic_dir= get_vlc2(&s->gb, ff_inter_intra_vlc.table, INTER_INTRA_VLC_BITS, 1);
 //            printf("%d%d %d %d/", s->ac_pred, s->h263_aic_dir, s->mb_x, s->mb_y);
         }
         if(s->per_mb_rl_table && cbp){
@@ -1576,7 +1590,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
 
     s->dsp.clear_blocks(s->block[0]);
     for (i = 0; i < 6; i++) {
-        if (msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
+        if (ff_msmpeg4_decode_block(s, block[i], i, (cbp >> (5 - i)) & 1, NULL) < 0)
         {
             av_log(s->avctx, AV_LOG_ERROR, "\nerror while decoding block: %d x %d (%d)\n", s->mb_x, s->mb_y, i);
             return -1;
@@ -1586,7 +1600,7 @@ static int msmpeg4v34_decode_mb(MpegEncContext *s, DCTELEM block[6][64])
     return 0;
 }
 //#define ERROR_DETAILS
-static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
+int ff_msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
                               int n, int coded, const uint8_t *scan_table)
 {
     int level, i, last, run, run_diff;
@@ -1622,7 +1636,7 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
         }
         block[0] = level;
 
-        run_diff = 0;
+        run_diff = s->msmpeg4_version >= 4;
         i = 0;
         if (!coded) {
             goto not_coded;
@@ -1714,7 +1728,7 @@ static inline int msmpeg4_decode_block(MpegEncContext * s, DCTELEM * block,
 //printf("level: %d, run: %d at %d %d\n", level, run, s->mb_x, s->mb_y);
 #if 0 // waste of time / this will detect very few errors
                     {
-                        const int abs_level= ABS(level);
+                        const int abs_level= FFABS(level);
                         const int run1= run - rl->max_run[last][abs_level] - run_diff;
                         if(abs_level<=MAX_LEVEL && run<=MAX_RUN){
                             if(abs_level <= rl->max_level[last][run]){
@@ -1875,7 +1889,7 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
         /* update predictor */
         *dc_val= level;
     }else{
-        uint16_t *dc_val;
+        int16_t *dc_val;
         pred = msmpeg4_pred_dc(s, n, &dc_val, dir_ptr);
         level += pred;
 
@@ -1890,7 +1904,7 @@ static int msmpeg4_decode_dc(MpegEncContext * s, int n, int *dir_ptr)
     return level;
 }
 
-static int msmpeg4_decode_motion(MpegEncContext * s,
+int ff_msmpeg4_decode_motion(MpegEncContext * s,
                                  int *mx_ptr, int *my_ptr)
 {
     MVTable *mv;
@@ -1928,9 +1942,3 @@ static int msmpeg4_decode_motion(MpegEncContext * s,
     *my_ptr = my;
     return 0;
 }
-
-/* cleanest way to support it
- * there is too much shared between versions so that we cant have 1 file per version & 1 common
- * as allmost everything would be in the common file
- */
-#include "wmv2.c"