]> git.sesse.net Git - vlc/blobdiff - src/video_parser/vpar_blocks.c
The motion compensation routines are now modules as well ; choose your
[vlc] / src / video_parser / vpar_blocks.c
index 9c478f942c4e79d275923e0a5aff15cf107f6191..12d0bbf9565ef3710d2748563f56bdaf80dfcce3 100644 (file)
@@ -2,23 +2,25 @@
  * vpar_blocks.c : blocks parsing
  *****************************************************************************
  * Copyright (C) 1999, 2000 VideoLAN
+ * $Id: vpar_blocks.c,v 1.72 2001/01/18 05:13:23 sam Exp $
  *
- * Authors:
+ * Authors: Christophe Massiot <massiot@via.ecp.fr>
+ *          Jean-Marc Dressler <polux@via.ecp.fr>
+ *          Stéphane Borel <stef@via.ecp.fr>
  *
  * 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 program 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 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., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -26,8 +28,7 @@
  *****************************************************************************/
 #include "defs.h"
 
-#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
-#include <sys/uio.h>                                            /* "input.h" */
+#include <string.h>                                                /* memset */
 
 #include "config.h"
 #include "common.h"
 #include "plugins.h"
 
 #include "intf_msg.h"
-#include "debug.h"                 /* XXX?? temporaire, requis par netlist.h */
 
-#include "input.h"
-#include "input_netlist.h"
-#include "decoder_fifo.h"
+#include "stream_control.h"
+#include "input_ext-dec.h"
+
 #include "video.h"
 #include "video_output.h"
 
-#include "vdec_idct.h"
 #include "video_decoder.h"
 #include "vdec_motion.h"
+#include "../video_decoder/vdec_idct.h"
 
 #include "vpar_blocks.h"
-#include "vpar_headers.h"
-#include "vpar_synchro.h"
-#include "video_parser.h"
-#include "video_fifo.h"
-
-static int i_count = 0;
+#include "../video_decoder/vpar_headers.h"
+#include "../video_decoder/vpar_synchro.h"
+#include "../video_decoder/video_parser.h"
+#include "../video_decoder/video_fifo.h"
 
 /*
  * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
@@ -611,6 +609,26 @@ void vpar_InitDCTTables( vpar_thread_t * p_vpar )
     FillDCTTable( p_vpar->ppl_dct_coef[1], pl_DCT_tab6,    1, 16, 16 );
 }
 
+/*****************************************************************************
+ * vpar_InitScanTable : Initialize scan table
+ *****************************************************************************/
+void vpar_InitScanTable( vpar_thread_t * p_vpar )
+{
+    int     i;
+
+    memcpy( p_vpar->ppi_scan, pi_scan, sizeof(pi_scan) );
+    p_vpar->pf_norm_scan( p_vpar->ppi_scan );
+
+    /* If scan table has changed, we must change the quantization matrices. */
+    for( i = 0; i < 64; i++ )
+    {
+        p_vpar->pi_default_intra_quant[ p_vpar->ppi_scan[0][i] ] =
+            pi_default_intra_quant[ pi_scan[0][i] ];
+        p_vpar->pi_default_nonintra_quant[ p_vpar->ppi_scan[0][i] ] =
+            pi_default_nonintra_quant[ pi_scan[0][i] ];
+    }
+}
+
 
 /*
  * Block parsing
@@ -619,7 +637,7 @@ void vpar_InitDCTTables( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * DecodeMPEG1NonIntra : decode MPEG-1 non-intra blocks
  *****************************************************************************/
-static void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
+static __inline__ void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
                                             macroblock_t * p_mb, int i_b,
                                             int i_chroma_format )
 {
@@ -644,7 +662,7 @@ static void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
 
     /* There should be no D picture in non-intra blocks */
     if( p_vpar->picture.i_coding_type == D_CODING_TYPE )
-        intf_ErrMsg("vpar error : D-picture in non intra block\n");
+        intf_ErrMsg("vpar error : D-picture in non intra block");
     
     /* Decoding of the AC coefficients */
 
@@ -652,12 +670,12 @@ static void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
     i_coef = 0;
     b_sign = 0;
 
-    for( i_parse = 0; !p_vpar->b_die; i_parse++ )
+    for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
     {
         i_code = ShowBits( &p_vpar->bit_stream, 16 );
         if( i_code >= 16384 )
         {
-            b_dc = !((i_parse + 62)/63);
+            b_dc = (i_parse == 0);
             i_run =     ppl_dct_tab2[b_dc][(i_code>>12)-4].i_run;
             i_level =   ppl_dct_tab2[b_dc][(i_code>>12)-4].i_level;
             i_length =  ppl_dct_tab2[b_dc][(i_code>>12)-4].i_length;
@@ -683,24 +701,19 @@ static void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
                 else if (i_level > 128)
                     i_level -= 256;
 
-                if (i_level<0)
+                if( (b_sign = i_level < 0) )
                     i_level = -i_level;
                 
                 break;
             case DCT_EOB:
-#ifdef HAVE_MMX
-                /* The MMX IDCT has a precision problem with non-intra
-                 * blocks. */
-                p_mb->ppi_blocks[i_b][0] += 4;
-#endif
                 if( i_nc <= 1 )
                 {
-                    p_mb->pf_idct[i_b] = vdec_SparseIDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
                     p_mb->pi_sparse_pos[i_b] = i_coef;
                 }
                 else
                 {
-                    p_mb->pf_idct[i_b] = vdec_IDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_idct;
                 }
                 return;
 
@@ -717,20 +730,28 @@ static void DecodeMPEG1NonIntra( vpar_thread_t * p_vpar,
             break;
         }
 
-        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
+        i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
         i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
                     * p_vpar->sequence.nonintra_quant.pi_matrix[i_pos] ) >> 4;
+
+        /* Mismatch control */
+        if( i_level ) /* Should always be true */
+        {
+            /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
+            i_level = (i_level - 1) | 1;
+        }
+
         p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
     }
 
-    intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
+    intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds");
     p_vpar->picture.b_error = 1;
 }
 
 /*****************************************************************************
  * DecodeMPEG1Intra : decode MPEG-1 intra blocks
  *****************************************************************************/
-static void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
+static __inline__ void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
                                          macroblock_t * p_mb, int i_b ,
                                          int i_chroma_format )
 {
@@ -783,7 +804,7 @@ static void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
     /* Read the actual code with the good length */
     p_vpar->mb.pi_dc_dct_pred[i_cc] += i_dct_dc_diff;
 
-    p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc];
+    p_mb->ppi_blocks[i_b][0] = p_vpar->mb.pi_dc_dct_pred[i_cc] << 3;
 
     i_nc = ( p_vpar->mb.pi_dc_dct_pred[i_cc] != 0 );
 
@@ -800,10 +821,9 @@ static void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
     
     /* Decoding of the AC coefficients */
     i_coef = 0;
-    i_nc = 0;
     b_sign = 0;
 
-    for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
+    for( i_parse = 1; !p_vpar->p_fifo->b_die/*i_parse < 64*/; i_parse++ )
     {
         i_code = ShowBits( &p_vpar->bit_stream, 16 );
         /* We use 2 main tables for the coefficients */
@@ -833,16 +853,18 @@ static void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
                     i_level = GetBits( &p_vpar->bit_stream, 8 ) - 256;
                 else if (i_level > 128)
                     i_level -= 256;
+                if( (b_sign = i_level < 0) )
+                    i_level = -i_level;
                 break;
             case DCT_EOB:
                 if( i_nc <= 1 )
                 {
-                    p_mb->pf_idct[i_b] = vdec_SparseIDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
                     p_mb->pi_sparse_pos[i_b] = i_coef;
                 }
                 else
                 {
-                    p_mb->pf_idct[i_b] = vdec_IDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_idct;
                 }
                 return;
 
@@ -863,21 +885,29 @@ static void DecodeMPEG1Intra( vpar_thread_t * p_vpar,
         }
 
         /* Determine the position of the block in the frame */
-        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
+        i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
         i_level = ( i_level *
                     p_vpar->mb.i_quantizer_scale *
                     p_vpar->sequence.intra_quant.pi_matrix[i_pos] ) >> 3;
+
+        /* Mismatch control */
+        if( i_level ) /* Should always be true */
+        {
+            /* Equivalent to : if( (val & 1) == 0 ) val = val - 1; */
+            i_level = (i_level - 1) | 1;
+        }
+
         p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
     }
 
-    intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
+    intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds");
     p_vpar->picture.b_error = 1;
 }
 
 /*****************************************************************************
  * DecodeMPEG2NonIntra : decode MPEG-2 non-intra blocks
  *****************************************************************************/
-static void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
+static __inline__ void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
                                             macroblock_t * p_mb, int i_b,
                                             int i_chroma_format )
 {
@@ -893,7 +923,7 @@ static void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
     boolean_t   b_dc;
     boolean_t   b_sign;
     boolean_t   b_chroma;
-    int *       pi_quant;
+    u8 *        pi_quant;
 
     /* Give the chromatic component (0, 1, 2) */
     i_cc = pi_cc_index[i_b];
@@ -915,7 +945,7 @@ static void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
 
     i_nc = 0;
     i_coef = 0;
-    for( i_parse = 0; !p_vpar->b_die; i_parse++ )
+    for( i_parse = 0; !p_vpar->p_fifo->b_die; i_parse++ )
     {
         i_code = ShowBits( &p_vpar->bit_stream, 16 );
         if( i_code >= 16384 )
@@ -951,12 +981,12 @@ static void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
 #endif
                 if( i_nc <= 1 )
                 {
-                    p_mb->pf_idct[i_b] = vdec_SparseIDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
                     p_mb->pi_sparse_pos[i_b] = i_coef;
                 }
                 else
                 {
-                    p_mb->pf_idct[i_b] = vdec_IDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_idct;
                 }
                 return;
 
@@ -973,20 +1003,20 @@ static void DecodeMPEG2NonIntra( vpar_thread_t * p_vpar,
             break;
         }
 
-        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
+        i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
         i_level = ( ((i_level << 1) + 1) * p_vpar->mb.i_quantizer_scale
                     * pi_quant[i_pos] ) >> 5;
         p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
     }
 
-    intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds\n");
+    intf_ErrMsg("vpar error: DCT coeff (non-intra) is out of bounds");
     p_vpar->picture.b_error = 1;
 }
 
 /*****************************************************************************
  * DecodeMPEG2Intra : decode MPEG-2 intra blocks
  *****************************************************************************/
-static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
+static __inline__ void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
                                          macroblock_t * p_mb, int i_b,
                                          int i_chroma_format )
 {
@@ -1004,7 +1034,7 @@ static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
     boolean_t       b_vlc_intra;
     boolean_t       b_sign;
     boolean_t       b_chroma;
-    int *           pi_quant;
+    u8 *            pi_quant;
     
     /* Give the chromatic component (0, 1, 2) */
     i_cc = pi_cc_index[i_b];
@@ -1061,7 +1091,7 @@ static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
 
     i_coef = 0;
     b_vlc_intra = p_vpar->picture.b_intra_vlc_format;
-    for( i_parse = 1; !p_vpar->b_die/*i_parse < 64*/; i_parse++ )
+    for( i_parse = 1; !p_vpar->p_fifo->b_die/*i_parse < 64*/; i_parse++ )
     {
         i_code = ShowBits( &p_vpar->bit_stream, 16 );
         /* We use 2 main tables for the coefficients */
@@ -1091,12 +1121,12 @@ static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
             case DCT_EOB:
                 if( i_nc <= 1 )
                 {
-                    p_mb->pf_idct[i_b] = vdec_SparseIDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_sparse_idct;
                     p_mb->pi_sparse_pos[i_b] = i_coef;
                 }
                 else
                 {
-                    p_mb->pf_idct[i_b] = vdec_IDCT;
+                    p_mb->pf_idct[i_b] = p_vpar->pf_idct;
                 }
                 return;
 
@@ -1117,14 +1147,14 @@ static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
         }
 
         /* Determine the position of the block in the frame */
-        i_pos = pi_scan[p_vpar->picture.b_alternate_scan][i_parse];
+        i_pos = p_vpar->ppi_scan[p_vpar->picture.b_alternate_scan][i_parse];
         i_level = ( i_level *
                     p_vpar->mb.i_quantizer_scale *
                     pi_quant[i_pos] ) >> 4;
         p_mb->ppi_blocks[i_b][i_pos] = b_sign ? -i_level : i_level;
     }
 
-    intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds\n");
+    intf_ErrMsg("vpar error: DCT coeff (intra) is out of bounds");
     p_vpar->picture.b_error = 1;
 }
 
@@ -1136,7 +1166,7 @@ static void DecodeMPEG2Intra( vpar_thread_t * p_vpar,
 /****************************************************************************
  * MotionCode : Parse the next motion code
  ****************************************************************************/
-static int MotionCode( vpar_thread_t * p_vpar )
+static __inline__ int MotionCode( vpar_thread_t * p_vpar )
 {
     int i_code;
     static lookup_t pl_mv_tab0[8] =
@@ -1173,7 +1203,7 @@ static int MotionCode( vpar_thread_t * p_vpar )
     if( (i_code -= 12) < 0 )
     {
         p_vpar->picture.b_error = 1;
-        intf_DbgMsg( "vpar debug: Invalid motion_vector code\n" );
+        intf_DbgMsg( "vpar debug: Invalid motion_vector code" );
         return 0;
     }
 
@@ -1185,7 +1215,7 @@ static int MotionCode( vpar_thread_t * p_vpar )
 /****************************************************************************
  * DecodeMotionVector : Decode a motion_vector
  ****************************************************************************/
-static void DecodeMotionVector( int * pi_prediction, int i_r_size,
+static __inline__ void DecodeMotionVector( int * pi_prediction, int i_r_size,
         int i_motion_code, int i_motion_residual, int i_full_pel )
 {
     int i_limit, i_vector;
@@ -1212,19 +1242,18 @@ static void DecodeMotionVector( int * pi_prediction, int i_r_size,
 /****************************************************************************
  * MotionVector : Parse the next motion_vector field
  ****************************************************************************/
-static void MotionVector( vpar_thread_t * p_vpar,
+static __inline__ void MotionVector( vpar_thread_t * p_vpar,
                                      macroblock_t * p_mb, int i_r,
-                                     int i_s, int i_full_pel, int i_structure )
+                                     int i_s, int i_full_pel, int i_structure,
+                                     int i_h_r_size, int i_v_r_size )
 {
     int i_motion_code, i_motion_residual;
-    int i_r_size;
     int pi_dm_vector[2];
 
-    i_r_size = p_vpar->picture.ppi_f_code[i_s][0] - 1;
     i_motion_code = MotionCode( p_vpar );
-    i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
-                        GetBits( &p_vpar->bit_stream, i_r_size) : 0;
-    DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_r_size,
+    i_motion_residual = (i_h_r_size != 0 && i_motion_code != 0) ?
+                        GetBits( &p_vpar->bit_stream, i_h_r_size) : 0;
+    DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][0], i_h_r_size,
                         i_motion_code, i_motion_residual, i_full_pel );
     p_mb->pppi_motion_vectors[i_r][i_s][0] = p_vpar->mb.pppi_pmv[i_r][i_s][0];
 
@@ -1240,10 +1269,9 @@ static void MotionVector( vpar_thread_t * p_vpar,
         }
     }
 
-    i_r_size = p_vpar->picture.ppi_f_code[i_s][1]-1;
     i_motion_code = MotionCode( p_vpar );
-    i_motion_residual = (i_r_size != 0 && i_motion_code != 0) ?
-                        GetBits( &p_vpar->bit_stream, i_r_size) : 0;
+    i_motion_residual = (i_v_r_size != 0 && i_motion_code != 0) ?
+                        GetBits( &p_vpar->bit_stream, i_v_r_size) : 0;
 
 
     if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
@@ -1252,7 +1280,7 @@ static void MotionVector( vpar_thread_t * p_vpar,
          p_vpar->mb.pppi_pmv[i_r][i_s][1] >>= 1;
     }
 
-    DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_r_size,
+    DecodeMotionVector( &p_vpar->mb.pppi_pmv[i_r][i_s][1], i_v_r_size,
                         i_motion_code, i_motion_residual, i_full_pel );
 
     if( (p_vpar->mb.i_mv_format == MOTION_FIELD)
@@ -1322,17 +1350,20 @@ static void MotionVector( vpar_thread_t * p_vpar,
  * DecodeMVMPEG1 : Parse the next MPEG-1 motion vectors
  *****************************************************************************/
 static void DecodeMVMPEG1( vpar_thread_t * p_vpar,
-                            macroblock_t * p_mb, int i_s, int i_structure )
+                           macroblock_t * p_mb, int i_s, int i_structure )
 {
+    int i_r_size = i_s ? p_vpar->picture.i_backward_f_code - 1 :
+                         p_vpar->picture.i_forward_f_code - 1;
     MotionVector( p_vpar, p_mb, 0, i_s,
-                  p_vpar->picture.pb_full_pel_vector[i_s], i_structure );
+                  p_vpar->picture.pb_full_pel_vector[i_s], FRAME_STRUCTURE,
+                  i_r_size, i_r_size );
 }
 
 /*****************************************************************************
  * DecodeMVMPEG2 : Parse the next MPEG-2 motion_vectors field
  *****************************************************************************/
 static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
-                            macroblock_t * p_mb, int i_s, int i_structure )
+                           macroblock_t * p_mb, int i_s, int i_structure )
 {
     if( p_vpar->mb.i_mv_count == 1 )
     {
@@ -1341,7 +1372,9 @@ static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
             p_mb->ppi_field_select[0][i_s] = p_mb->ppi_field_select[1][i_s]
                                             = GetBits( &p_vpar->bit_stream, 1 );
         }
-        MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
+        MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
+                      p_vpar->picture.ppi_f_code[i_s][0] - 1,
+                      p_vpar->picture.ppi_f_code[i_s][1] - 1 );
         p_vpar->mb.pppi_pmv[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
         p_vpar->mb.pppi_pmv[1][i_s][1] = p_vpar->mb.pppi_pmv[0][i_s][1];
         p_mb->pppi_motion_vectors[1][i_s][0] = p_vpar->mb.pppi_pmv[0][i_s][0];
@@ -1350,9 +1383,13 @@ static void DecodeMVMPEG2( vpar_thread_t * p_vpar,
     else
     {
         p_mb->ppi_field_select[0][i_s] = GetBits( &p_vpar->bit_stream, 1 );
-        MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure );
+        MotionVector( p_vpar, p_mb, 0, i_s, 0, i_structure,
+                      p_vpar->picture.ppi_f_code[i_s][0] - 1,
+                      p_vpar->picture.ppi_f_code[i_s][1] - 1 );
         p_mb->ppi_field_select[1][i_s] = GetBits( &p_vpar->bit_stream, 1 );
-        MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure );
+        MotionVector( p_vpar, p_mb, 1, i_s, 0, i_structure,
+                      p_vpar->picture.ppi_f_code[i_s][0] - 1,
+                      p_vpar->picture.ppi_f_code[i_s][1] - 1 );
     }
 }
 
@@ -1390,7 +1427,7 @@ static int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * IMBType : macroblock_type in I pictures
  *****************************************************************************/
-static int IMBType( vpar_thread_t * p_vpar )
+static __inline__ int IMBType( vpar_thread_t * p_vpar )
 {
     /* Take two bits for testing */
     int                 i_type = ShowBits( &p_vpar->bit_stream, 2 );
@@ -1408,7 +1445,7 @@ static int IMBType( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * PMBType : macroblock_type in P pictures
  *****************************************************************************/
-static int PMBType( vpar_thread_t * p_vpar )
+static __inline__ int PMBType( vpar_thread_t * p_vpar )
 {
     /* Testing on 6 bits */
     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
@@ -1422,7 +1459,7 @@ static int PMBType( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * BMBType : macroblock_type in B pictures
  *****************************************************************************/
-static int BMBType( vpar_thread_t * p_vpar )
+static __inline__ int BMBType( vpar_thread_t * p_vpar )
 {
      /* Testing on 6 bits */
     int                i_type = ShowBits( &p_vpar->bit_stream, 6 );
@@ -1437,7 +1474,7 @@ static int BMBType( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * DMBType : macroblock_type in D pictures
  *****************************************************************************/
-static int DMBType( vpar_thread_t * p_vpar )
+static __inline__ int DMBType( vpar_thread_t * p_vpar )
 {
     return GetBits( &p_vpar->bit_stream, 1 );
 }
@@ -1445,7 +1482,7 @@ static int DMBType( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * CodedPattern420 : coded_block_pattern with 4:2:0 chroma
  *****************************************************************************/
-static int CodedPattern420( vpar_thread_t * p_vpar )
+static __inline__ int CodedPattern420( vpar_thread_t * p_vpar )
 {
     /* Take the max 9 bits length vlc code for testing */
     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
@@ -1460,7 +1497,7 @@ static int CodedPattern420( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * CodedPattern422 : coded_block_pattern with 4:2:2 chroma
  *****************************************************************************/
-static int CodedPattern422( vpar_thread_t * p_vpar )
+static __inline__ int CodedPattern422( vpar_thread_t * p_vpar )
 {
     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
 
@@ -1474,7 +1511,7 @@ static int CodedPattern422( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * CodedPattern444 : coded_block_pattern with 4:4:4 chroma
  *****************************************************************************/
-static int CodedPattern444( vpar_thread_t * p_vpar )
+static __inline__ int CodedPattern444( vpar_thread_t * p_vpar )
 {
     int      i_vlc = ShowBits( &p_vpar->bit_stream, 9 );
 
@@ -1487,7 +1524,7 @@ static int CodedPattern444( vpar_thread_t * p_vpar )
 /*****************************************************************************
  * InitMacroblock : Initialize macroblock values
  *****************************************************************************/
-static void InitMacroblock( vpar_thread_t * p_vpar,
+static __inline__ void InitMacroblock( vpar_thread_t * p_vpar,
                                        macroblock_t * p_mb, int i_coding_type,
                                        int i_chroma_format,
                                        int i_structure,
@@ -1522,7 +1559,7 @@ static void InitMacroblock( vpar_thread_t * p_vpar,
 /*****************************************************************************
  * UpdateContext : Update the p_vpar contextual values
  *****************************************************************************/
-static void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
+static __inline__ void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
 {
     /* Update macroblock real position. */
     p_vpar->mb.i_l_x += 16;
@@ -1540,7 +1577,7 @@ static void UpdateContext( vpar_thread_t * p_vpar, int i_structure )
 /*****************************************************************************
  * SkippedMacroblock : Generate a skipped macroblock with NULL motion vector
  *****************************************************************************/
-static void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
+static __inline__ void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
                                           int i_mb_base, int i_coding_type,
                                           int i_chroma_format,
                                           int i_structure,
@@ -1548,20 +1585,9 @@ static void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
 {
     macroblock_t *  p_mb;
 
-    static f_motion_t   pf_motion_skipped[4][4] =
-    {
-        {NULL, NULL, NULL, NULL},
-        {NULL, vdec_MotionFieldField420, vdec_MotionFieldField420,
-            vdec_MotionFrameFrame420},
-        {NULL, vdec_MotionFieldField422, vdec_MotionFieldField422,
-            vdec_MotionFrameFrame422},
-        {NULL, vdec_MotionFieldField444, vdec_MotionFieldField444,
-            vdec_MotionFrameFrame444},
-    };
-
     if( i_coding_type == I_CODING_TYPE )
     {
-        intf_DbgMsg("vpar error: skipped macroblock in I-picture\n");
+        intf_DbgMsg("vpar error: skipped macroblock in I-picture");
         p_vpar->picture.b_error = 1;
         return;
     }
@@ -1579,11 +1605,25 @@ static void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
                     i_structure, b_second_field );
 
     /* Motion type is picture structure. */
-    p_mb->pf_motion = pf_motion_skipped[i_chroma_format]
-                                       [i_structure];
-    p_mb->i_mb_type = MB_MOTION_FORWARD;
+    p_mb->pf_motion = p_vpar->ppf_motion_skipped[i_chroma_format]
+                                                [i_structure];
     p_mb->i_coded_block_pattern = 0;
-    memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
+
+    /* Motion direction and motion vectors depend on the coding type. */
+    if( i_coding_type == B_CODING_TYPE )
+    {
+        int i, j, k;
+        p_mb->i_mb_type = p_vpar->mb.i_motion_dir;
+        for( i = 0; i < 2; i++ )
+            for( j = 0; j < 2; j++ )
+                for( k = 0; k < 2; k++ )
+                    p_mb->pppi_motion_vectors[i][j][k] = p_vpar->mb.pppi_pmv[i][j][k];
+    }
+    else if( i_coding_type == P_CODING_TYPE )
+    {
+        p_mb->i_mb_type = MB_MOTION_FORWARD;
+        memset( p_mb->pppi_motion_vectors, 0, 8*sizeof(int) );
+    }
 
     /* Set the field we use for motion compensation */
     p_mb->ppi_field_select[0][0] = p_mb->ppi_field_select[0][1]
@@ -1593,14 +1633,14 @@ static void SkippedMacroblock( vpar_thread_t * p_vpar, int i_mb,
 
 #ifndef VDEC_SMP
     /* Decode the macroblock NOW ! */
-    vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
+    vpar_DecodeMacroblock ( &p_vpar->vfifo, p_mb );
 #endif
 }
 
 /*****************************************************************************
  * MacroblockModes : Get the macroblock_modes structure
  *****************************************************************************/
-static void MacroblockModes( vpar_thread_t * p_vpar,
+static __inline__ void MacroblockModes( vpar_thread_t * p_vpar,
                                         macroblock_t * p_mb,
                                         int i_chroma_format,
                                         int i_coding_type,
@@ -1625,6 +1665,14 @@ static void MacroblockModes( vpar_thread_t * p_vpar,
         p_mb->i_mb_type = DMBType( p_vpar );
     }
 
+    if( i_coding_type == B_CODING_TYPE )
+    {
+        /* We need to remember the motion direction of the last macroblock
+         * before a skipped macroblock (ISO/IEC 13818-2 7.6.6) */
+        p_vpar->mb.i_motion_dir = p_mb->i_mb_type
+                              & (MB_MOTION_FORWARD | MB_MOTION_BACKWARD);
+    }
+
     /* SCALABILITY : warning, we don't know if spatial_temporal_weight_code
      * has to be dropped, take care if you use scalable streams. */
     /* RemoveBits( &p_vpar->bit_stream, 2 ); */
@@ -1684,7 +1732,6 @@ if( p_vpar->picture.b_error )                                           \
     return;                                                             \
 }
 
-
 #define PARSEBLOCKS( MPEG1FUNC, MPEG2FUNC )                             \
 {                                                                       \
     i_mask = 1 << (3 + (1 << i_chroma_format));                         \
@@ -1706,7 +1753,7 @@ if( p_vpar->picture.b_error )                                           \
             /* Calculate block coordinates. */                          \
             p_mb->p_data[i_b] = p_data1                                 \
                                 + pi_y[p_vpar->mb.b_dct_type][i_b]      \
-                                * p_vpar->sequence.i_width              \
+                                * p_vpar->picture.i_l_stride            \
                                 + pi_x[i_b];                            \
                                                                         \
             PARSEERROR                                                  \
@@ -1739,7 +1786,7 @@ if( p_vpar->picture.b_error )                                           \
             /* Calculate block coordinates. */                          \
             p_mb->p_data[i_b] = pp_data[i_b & 1]                        \
                                  + pi_y[p_vpar->mb.b_dct_type][i_b]     \
-                                   * p_vpar->sequence.i_chroma_width    \
+                                   * p_vpar->picture.i_c_stride         \
                                  + pi_x[i_b];                           \
                                                                         \
             PARSEERROR                                                  \
@@ -1747,7 +1794,7 @@ if( p_vpar->picture.b_error )                                           \
     }                                                                   \
 }
 
-static void ParseMacroblock(
+static __inline__ void ParseMacroblock(
                            vpar_thread_t * p_vpar,
                            int * pi_mb_address,     /* previous address to be
                                                      * used for mb_addr_incr */
@@ -1762,27 +1809,6 @@ static void ParseMacroblock(
                            boolean_t b_second_field )     /* second field of a
                                                            * field picture   */
 {
-    static f_motion_t   pppf_motion[4][2][4] =
-      {
-        { {NULL, NULL, NULL, NULL},
-          {NULL, NULL, NULL, NULL}
-        },
-        { {NULL, vdec_MotionFieldField420, vdec_MotionField16x8420,
-            vdec_MotionFieldDMV420},
-          {NULL, vdec_MotionFrameField420, vdec_MotionFrameFrame420,
-            vdec_MotionFrameDMV420}
-        },
-        { {NULL, vdec_MotionFieldField422, vdec_MotionField16x8422,
-            vdec_MotionFieldDMV422},
-          {NULL, vdec_MotionFrameField422, vdec_MotionFrameFrame422,
-            vdec_MotionFrameDMV422}
-        },
-        { {NULL, vdec_MotionFieldField444, vdec_MotionField16x8444,
-            vdec_MotionFieldDMV444},
-          {NULL, vdec_MotionFrameField444, vdec_MotionFrameFrame444,
-            vdec_MotionFrameDMV444}
-        }
-      };
     static int      pi_x[12] = {0,8,0,8,0,0,0,0,8,8,8,8};
     static int      pi_y[2][12] = { {0,0,8,8,0,0,8,8,0,0,8,8},
                                     {0,0,1,1,0,0,1,1,0,0,1,1} };
@@ -1792,14 +1818,12 @@ static void ParseMacroblock(
     yuv_data_t *    p_data1;
     yuv_data_t *    p_data2;
 
-i_count++;
-
     i_inc = MacroblockAddressIncrement( p_vpar );
     *pi_mb_address += i_inc;
 
     if( i_inc < 0 )
     {
-        intf_ErrMsg( "vpar error: bad address increment (%d)\n", i_inc );
+        intf_ErrMsg( "vpar error: bad address increment (%d)", i_inc );
         p_vpar->picture.b_error = 1;
         return;
     }
@@ -1887,9 +1911,9 @@ i_count++;
             = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
 
         /* Motion function pointer. */
-        p_mb->pf_motion = pppf_motion[i_chroma_format]
-                                     [i_structure == FRAME_STRUCTURE]
-                                     [p_vpar->mb.i_motion_type];
+        p_mb->pf_motion = p_vpar->pppf_motion[i_chroma_format]
+                                             [i_structure == FRAME_STRUCTURE]
+                                             [p_vpar->mb.i_motion_type];
 
         if( p_mb->i_mb_type & MB_PATTERN )
         {
@@ -1962,7 +1986,7 @@ i_count++;
         UpdateContext( p_vpar, i_structure );
 #ifndef VDEC_SMP
         /* Decode the macroblock NOW ! */
-        vdec_DecodeMacroblock( p_vpar->pp_vdec[0], p_mb );
+        vpar_DecodeMacroblock ( &p_vpar->vfifo, p_mb );
 #endif
     }
     else
@@ -1978,10 +2002,137 @@ i_count++;
  * Picture data parsing management
  */
 
+/*****************************************************************************
+ * ParseMacroblockVWXYZ : Parse the next macroblock ; specific functions
+ *****************************************************************************
+ * V = MPEG2 ?
+ * W = coding type ?
+ * X = chroma format ?
+ * Y = structure ?
+ * Z = second field ?
+ *****************************************************************************/
+void ParseMacroblockGENERIC( vpar_thread_t * p_vpar, int * pi_mb_address,
+                             int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     b_mpeg2, i_coding_type, i_chroma_format,
+                     i_structure, b_second_field );
+}
+
+#if (VPAR_OPTIM_LEVEL > 0)
+/* Optimizations for frame pictures */
+void ParseMacroblock2I420F0( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, I_CODING_TYPE, CHROMA_420,
+                     FRAME_STRUCTURE, 0 );
+}
+
+void ParseMacroblock2P420F0( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, P_CODING_TYPE, CHROMA_420,
+                     FRAME_STRUCTURE, 0 );
+}
+
+void ParseMacroblock2B420F0( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, B_CODING_TYPE, CHROMA_420,
+                     FRAME_STRUCTURE, 0 );
+}
+#endif
+
+#if (VPAR_OPTIM_LEVEL > 1)
+/* Optimizations for field pictures */
+void ParseMacroblock2I420TZ( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, I_CODING_TYPE, CHROMA_420,
+                     TOP_FIELD, (p_vpar->picture.i_structure !=
+                                 p_vpar->picture.i_current_structure) );
+}
+
+void ParseMacroblock2P420TZ( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, P_CODING_TYPE, CHROMA_420,
+                     TOP_FIELD, (p_vpar->picture.i_structure !=
+                                 p_vpar->picture.i_current_structure) );
+}
+
+void ParseMacroblock2B420TZ( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, B_CODING_TYPE, CHROMA_420,
+                     TOP_FIELD, (p_vpar->picture.i_structure !=
+                                 p_vpar->picture.i_current_structure) );
+}
+
+void ParseMacroblock2I420BZ( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, I_CODING_TYPE, CHROMA_420,
+                     BOTTOM_FIELD, (p_vpar->picture.i_structure !=
+                                    p_vpar->picture.i_current_structure) );
+}
+
+void ParseMacroblock2P420BZ( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, P_CODING_TYPE, CHROMA_420,
+                     BOTTOM_FIELD, (p_vpar->picture.i_structure !=
+                                    p_vpar->picture.i_current_structure) );
+}
+
+void ParseMacroblock2B420BZ( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                             boolean_t b_mpeg2, int i_coding_type,
+                             int i_chroma_format, int i_structure,
+                             boolean_t b_second_field )
+{
+    ParseMacroblock( p_vpar, pi_mb_address, i_mb_previous, i_mb_base,
+                     1, B_CODING_TYPE, CHROMA_420,
+                     BOTTOM_FIELD, (p_vpar->picture.i_structure !=
+                                    p_vpar->picture.i_current_structure) );
+}
+#endif
+
 /*****************************************************************************
  * SliceHeader : Parse the next slice structure
  *****************************************************************************/
-static void SliceHeader( vpar_thread_t * p_vpar,
+typedef void    (*f_parse_mb_t)( vpar_thread_t * p_vpar, int * pi_mb_address,                                    int i_mb_previous, int i_mb_base,
+                                 boolean_t b_mpeg2, int i_coding_type,
+                                 int i_chroma_format, int i_structure,
+                                 boolean_t b_second_field );
+
+static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
                                     int * pi_mb_address, int i_mb_base,
                                     u32 i_vert_code, boolean_t b_high,
                                     boolean_t b_dp_scalable,
@@ -1989,6 +2140,43 @@ static void SliceHeader( vpar_thread_t * p_vpar,
                                     int i_chroma_format, int i_structure,
                                     boolean_t b_second_field )
 {
+    static f_parse_mb_t ppf_parse_mb[4][4] =
+    {
+        {
+            NULL, NULL, NULL, NULL
+        },
+        {
+            /* TOP_FIELD */
+#if (VPAR_OPTIM_LEVEL > 1)
+            NULL, ParseMacroblock2I420TZ, ParseMacroblock2P420TZ,
+            ParseMacroblock2B420TZ
+#else
+            NULL, ParseMacroblockGENERIC, ParseMacroblockGENERIC,
+            ParseMacroblockGENERIC
+#endif
+        },
+        {
+            /* BOTTOM_FIELD */
+#if (VPAR_OPTIM_LEVEL > 1)
+            NULL, ParseMacroblock2I420BZ, ParseMacroblock2P420BZ,
+            ParseMacroblock2B420BZ
+#else
+            NULL, ParseMacroblockGENERIC, ParseMacroblockGENERIC,
+            ParseMacroblockGENERIC
+#endif
+        },
+        {
+            /* FRAME_PICTURE */
+#if (VPAR_OPTIM_LEVEL > 0)
+            NULL, ParseMacroblock2I420F0, ParseMacroblock2P420F0,
+            ParseMacroblock2B420F0
+#else
+            NULL, ParseMacroblockGENERIC, ParseMacroblockGENERIC,
+            ParseMacroblockGENERIC
+#endif
+        }
+    };
+
     int                     i_mb_address_save = *pi_mb_address;
 
     p_vpar->picture.b_error = 0;
@@ -2020,7 +2208,7 @@ static void SliceHeader( vpar_thread_t * p_vpar,
 
     if( *pi_mb_address < i_mb_address_save )
     {
-        intf_ErrMsg( "vpar error: slices do not follow, maybe a PES has been trashed\n" );
+        intf_ErrMsg( "vpar error: slices do not follow, maybe a PES has been trashed" );
         p_vpar->picture.b_error = 1;
         return;
     }
@@ -2040,43 +2228,68 @@ static void SliceHeader( vpar_thread_t * p_vpar,
             p_vpar->picture.b_error = 1;
             return;
         }
-        ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
+
+        if( p_vpar->sequence.i_chroma_format != CHROMA_420
+            || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
+            || p_vpar->sequence.i_scalable_mode == SC_DP )
+        {
+            /* Weird stream. Use the slower generic function. */
+            ParseMacroblockGENERIC( p_vpar, pi_mb_address, i_mb_address_save,
                          i_mb_base, b_mpeg2, i_coding_type,
                          i_chroma_format, i_structure,
                          b_second_field );
+        }
+        else
+        {
+            /* Try to find an optimized function. */
+            if( ppf_parse_mb[i_structure]
+                            [p_vpar->picture.i_coding_type] == NULL )
+            {
+                intf_ErrMsg( "vpar error: bad ppf_parse_mb function pointer (struct:%d, coding type:%d)",
+                         i_structure, i_coding_type );
+            }
+            else
+            {
+                ppf_parse_mb[i_structure][i_coding_type]
+                       ( p_vpar, pi_mb_address, i_mb_address_save,
+                         i_mb_base, b_mpeg2, i_coding_type,
+                         i_chroma_format, i_structure,
+                         b_second_field );
+            }
+        }
+
         i_mb_address_save = *pi_mb_address;
         if( p_vpar->picture.b_error )
         {
             return;
         }
     }
-    while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
-    NextStartCode( p_vpar );
+    while( ShowBits( &p_vpar->bit_stream, 23 )
+            && !p_vpar->p_fifo->b_die );
+    NextStartCode( &p_vpar->bit_stream );
 }
 
 /*****************************************************************************
  * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
  *****************************************************************************/
-static void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
-                                    boolean_t b_high, boolean_t b_dp_scalable,
-                                    boolean_t b_mpeg2, int i_coding_type,
-                                    int i_chroma_format, int i_structure,
-                                    boolean_t b_second_field )
+void vpar_PictureData( vpar_thread_t * p_vpar, int i_mb_base )
 {
     int         i_mb_address = 0;
     u32         i_dummy;
 
-    NextStartCode( p_vpar );
-    while( ((i_coding_type != I_CODING_TYPE && i_coding_type != D_CODING_TYPE)
+    NextStartCode( &p_vpar->bit_stream );
+    while( ((p_vpar->picture.i_coding_type != I_CODING_TYPE
+                    && p_vpar->picture.i_coding_type != D_CODING_TYPE)
              || !p_vpar->picture.b_error)
-           && i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
-           && !p_vpar->b_die )
+           && i_mb_address < (p_vpar->sequence.i_mb_size
+                    >> (p_vpar->picture.i_structure != FRAME_STRUCTURE))
+           && !p_vpar->p_fifo->b_die )
     {
         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
                  < SLICE_START_CODE_MIN) ||
             (i_dummy > SLICE_START_CODE_MAX) )
         {
-            intf_DbgMsg("vpar debug: premature end of picture\n");
+            intf_DbgMsg("vpar debug: premature end of picture");
             p_vpar->picture.b_error = 1;
             break;
         }
@@ -2084,17 +2297,23 @@ static void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
 
         /* Decode slice data. */
         SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255,
-                     b_high, b_dp_scalable, b_mpeg2, i_coding_type,
-                     i_chroma_format, i_structure, b_second_field );
+                     (p_vpar->sequence.i_height > 2800),
+                     (p_vpar->sequence.i_scalable_mode == SC_DP),
+                     p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
+                     p_vpar->sequence.i_chroma_format,
+                     p_vpar->picture.i_structure,
+                     (p_vpar->picture.i_structure !=
+                        p_vpar->picture.i_current_structure) );
     }
 
     /* Try to recover from error. If we missed less than half the
      * number of macroblocks of the picture, mark the missed ones
      * as skipped. */
-    if( (i_coding_type == P_CODING_TYPE || i_coding_type == B_CODING_TYPE)
+    if( (p_vpar->picture.i_coding_type == P_CODING_TYPE
+                || p_vpar->picture.i_coding_type == B_CODING_TYPE)
         && p_vpar->picture.b_error &&
         ( (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 1)
-           || (i_structure != FRAME_STRUCTURE
+           || (p_vpar->picture.i_structure != FRAME_STRUCTURE
                && (i_mb_address-i_mb_base) > (p_vpar->sequence.i_mb_size >> 2) ) ) )
     {
         int         i_mb;
@@ -2102,97 +2321,16 @@ static void PictureData( vpar_thread_t * p_vpar, int i_mb_base,
         p_vpar->picture.b_error = 0;
         for( i_mb = i_mb_address + 1;
              i_mb < (p_vpar->sequence.i_mb_size
-                     << (i_structure != FRAME_STRUCTURE));
+                     << (p_vpar->picture.i_structure != FRAME_STRUCTURE));
              i_mb++ )
         {
-            SkippedMacroblock( p_vpar, i_mb, i_mb_base, i_coding_type,
-                               i_chroma_format, i_structure, b_second_field );
+            SkippedMacroblock( p_vpar, i_mb, i_mb_base,
+                               p_vpar->picture.i_coding_type,
+                               p_vpar->sequence.i_chroma_format,
+                               p_vpar->picture.i_structure,
+                               (p_vpar->picture.i_structure !=
+                                p_vpar->picture.i_current_structure) );
         }
     }
 }
 
-/*****************************************************************************
- * vpar_PictureDataVWXYZ : Parse the next macroblock ; specific functions
- *****************************************************************************
- * V = MPEG2 ?
- * W = coding type ?
- * X = chroma format ?
- * Y = structure ?
- * Z = second field ?
- *****************************************************************************/
-void vpar_PictureDataGENERIC( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, (p_vpar->sequence.i_height > 2800),
-                 (p_vpar->sequence.i_scalable_mode == SC_DP),
-                 p_vpar->sequence.b_mpeg2, p_vpar->picture.i_coding_type,
-                 p_vpar->sequence.i_chroma_format,
-                 p_vpar->picture.i_structure,
-                 (p_vpar->picture.i_structure !=
-                    p_vpar->picture.i_current_structure) );
-}
-
-#if (VPAR_OPTIM_LEVEL > 0)
-/* Optimizations for frame pictures */
-void vpar_PictureData2I420F0( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, 0, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
-                 FRAME_STRUCTURE, 0 );
-}
-
-void vpar_PictureData2P420F0( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, 0, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
-                 FRAME_STRUCTURE, 0 );
-}
-
-void vpar_PictureData2B420F0( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, 0, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
-                 FRAME_STRUCTURE, 0 );
-}
-#endif
-
-#if (VPAR_OPTIM_LEVEL > 1)
-/* Optimizations for field pictures */
-void vpar_PictureData2I420TZ( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
-                 TOP_FIELD, (p_vpar->picture.i_structure !=
-                                p_vpar->picture.i_current_structure) );
-}
-
-void vpar_PictureData2P420TZ( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
-                 TOP_FIELD, (p_vpar->picture.i_structure !=
-                                p_vpar->picture.i_current_structure) );
-}
-
-void vpar_PictureData2B420TZ( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
-                 TOP_FIELD, (p_vpar->picture.i_structure !=
-                                p_vpar->picture.i_current_structure) );
-}
-
-void vpar_PictureData2I420BZ( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, 0, 0, 1, I_CODING_TYPE, CHROMA_420,
-                 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
-                                p_vpar->picture.i_current_structure) );
-}
-
-void vpar_PictureData2P420BZ( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, 0, 0, 1, P_CODING_TYPE, CHROMA_420,
-                 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
-                                p_vpar->picture.i_current_structure) );
-}
-
-void vpar_PictureData2B420BZ( vpar_thread_t * p_vpar, int i_mb_base )
-{
-    PictureData( p_vpar, i_mb_base, 0, 0, 1, B_CODING_TYPE, CHROMA_420,
-                 BOTTOM_FIELD, (p_vpar->picture.i_structure !=
-                                p_vpar->picture.i_current_structure) );
-}
-#endif