]> git.sesse.net Git - vlc/blob - plugins/mpeg_vdec/vpar_blocks.c
* Removed unused code (intf_channels.c, keystrokes.h).
[vlc] / plugins / mpeg_vdec / vpar_blocks.c
1 /*****************************************************************************
2  * vpar_blocks.c : blocks parsing
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: vpar_blocks.c,v 1.4 2001/12/10 04:53:11 sam Exp $
6  *
7  * Authors: Michel Lespinasse <walken@zoy.org>
8  *          Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <string.h>                                                /* memset */
32
33 #include "common.h"
34 #include "intf_msg.h"
35 #include "threads.h"
36 #include "mtime.h"
37
38 #include "video.h"
39 #include "video_output.h"
40
41 #include "stream_control.h"
42 #include "input_ext-dec.h"
43
44 #include "vdec_ext-plugins.h"
45 #include "vpar_pool.h"
46 #include "video_parser.h"
47
48 #include "vpar_blocks.h"
49
50 #include "modules_export.h"
51
52 /*
53  * Welcome to vpar_blocks.c ! Here's where the heavy processor-critical parsing
54  * task is done. This file is divided in several parts :
55  *  - Decoding of coded blocks
56  *  - Decoding of motion vectors
57  *  - Decoding of the other macroblock structures
58  *  - Picture data parsing management (slices and error handling)
59  * It's a pretty long file. Good luck and have a nice day.
60  */
61
62
63 /*****************************************************************************
64  * vpar_InitScanTable : Initialize scan table
65  *****************************************************************************/
66 void vpar_InitScanTable( vpar_thread_t * p_vpar )
67 {
68     int     i;
69
70     memcpy( p_vpar->ppi_scan, pi_scan, sizeof(pi_scan) );
71     p_vpar->pf_norm_scan( p_vpar->ppi_scan );
72
73     /* If scan table has changed, we must change the quantization matrices. */
74     for( i = 0; i < 64; i++ )
75     {
76         p_vpar->pi_default_intra_quant[ p_vpar->ppi_scan[0][i] ] =
77             pi_default_intra_quant[ pi_scan[0][i] ];
78         p_vpar->pi_default_nonintra_quant[ p_vpar->ppi_scan[0][i] ] =
79             pi_default_nonintra_quant[ pi_scan[0][i] ];
80     }
81 }
82
83
84 /*
85  * Block parsing
86  */
87
88 /*****************************************************************************
89  * GetLumaDCDiff : Get the luminance DC coefficient difference
90  *****************************************************************************/
91 static __inline__ int GetLumaDCDiff( vpar_thread_t * p_vpar )
92 {
93     lookup_t *  p_tab;
94     int         i_size, i_dc_diff, i_code;
95
96     if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) < 0x1F )
97     {
98         p_tab = DC_lum_5 + i_code;
99         i_size = p_tab->i_value;
100         if( i_size )
101         {
102             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
103             i_dc_diff = GetBits( &p_vpar->bit_stream, i_size );
104             if ((i_dc_diff & (1 << (i_size - 1))) == 0)
105             {
106                 i_dc_diff -= (1 << i_size) - 1;
107             }
108             return( i_dc_diff );
109         }
110         else
111         {
112             RemoveBits( &p_vpar->bit_stream, 3 );
113             return 0;
114         }
115     }
116     else
117     {
118         p_tab = DC_long - 0x1e0 + ShowBits( &p_vpar->bit_stream, 9 );
119         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
120         i_size = p_tab->i_value;
121         i_dc_diff = GetBits( &p_vpar->bit_stream, i_size );
122         if ((i_dc_diff & (1 << (i_size - 1))) == 0)
123         {
124             i_dc_diff -= (1 << i_size) - 1;
125         }
126         return( i_dc_diff );
127     }
128 }
129
130 /*****************************************************************************
131  * GetChromaDCDiff : Get the chrominance DC coefficient difference
132  *****************************************************************************/
133 static __inline__ int GetChromaDCDiff( vpar_thread_t * p_vpar )
134 {
135     lookup_t *  p_tab;
136     int         i_size, i_dc_diff, i_code;
137
138     if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) < 0x1F )
139     {
140         p_tab = DC_chrom_5 + i_code;
141         i_size = p_tab->i_value;
142         if( i_size )
143         {
144             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
145             i_dc_diff = GetBits( &p_vpar->bit_stream, i_size );
146             if ((i_dc_diff & (1 << (i_size - 1))) == 0)
147             {
148                 i_dc_diff -= (1 << i_size) - 1;
149             }
150             return( i_dc_diff );
151         }
152         else
153         {
154             RemoveBits( &p_vpar->bit_stream, 2 );
155             return 0;
156         }
157     }
158     else
159     {
160         p_tab = DC_long - 0x3e0 + ShowBits( &p_vpar->bit_stream, 10 );
161         RemoveBits( &p_vpar->bit_stream, p_tab->i_length + 1 );
162         i_size = p_tab->i_value;
163         i_dc_diff = GetBits( &p_vpar->bit_stream, i_size );
164         if ((i_dc_diff & (1 << (i_size - 1))) == 0)
165         {
166             i_dc_diff -= (1 << i_size) - 1;
167         }
168         return( i_dc_diff );
169     }
170 }
171
172
173 #define SATURATE(val)                                                       \
174     if ((u32)(val + 2048) > 4095)                                           \
175     {                                                                       \
176        val = (val > 0) ? 2047 : -2048;                                      \
177     }
178
179 /*****************************************************************************
180  * MPEG2IntraB14 : Decode an intra block according to ISO/IEC 13818-2 table B14
181  *****************************************************************************/
182 static void MPEG2IntraB14( vpar_thread_t * p_vpar, idct_inner_t * p_idct,
183                            u8 * pi_quant )
184 {
185     int         i_coeff, i_mismatch, i_code, i_pos, i_value, i_nc;
186     s32         i_sign;
187     dct_lookup_t * p_tab;
188
189     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
190     dctelem_t * p_dest = p_idct->pi_block;
191     u8 *        p_scan = p_vpar->picture.pi_scan;
192
193     i_coeff = 0;
194     i_mismatch = ~p_dest[0];
195     i_nc = (p_dest[0] != 0);
196
197     for( ; ; )
198     {
199         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
200         {
201             p_tab = DCT_B14AC_5 - 5 + i_code;
202             i_coeff += p_tab->i_run;
203             if( i_coeff >= 64 )
204             {
205                 /* End of block */
206                 break;
207             }
208
209 store_coeff:
210             i_nc++;
211             i_pos = p_scan[ i_coeff ];
212             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
213             i_value = (p_tab->i_level * i_q_scale * pi_quant[i_pos])
214                             >> 4;
215
216             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
217             /* if (i_sign) i_value = -i_value; */
218             i_value = (i_value ^ i_sign) - i_sign;
219
220             SATURATE( i_value );
221             p_dest[i_pos] = i_value;
222             i_mismatch ^= i_value;
223
224             continue;
225         }
226         else if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
227         {
228             p_tab = DCT_B14_8 - 4 + i_code;
229             i_coeff += p_tab->i_run;
230             if( i_coeff < 64 )
231             {
232                 /* Normal coefficient */
233                 goto store_coeff;
234             }
235
236             /* Escape code */
237             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
238             if( i_coeff >= 64 )
239             {
240                 /* Illegal, but needed to avoid overflow */
241                 intf_WarnMsg( 2, "Intra-B14 coeff is out of bound" );
242                 p_vpar->picture.b_error = 1;
243                 break;
244             }
245
246             i_nc++;
247             i_pos = p_scan[i_coeff];
248             i_value = (GetSignedBits( &p_vpar->bit_stream, 12 )
249                         * i_q_scale * pi_quant[i_pos]) / 16;
250
251             SATURATE( i_value );
252             p_dest[i_pos] = i_value;
253             i_mismatch ^= i_value;
254             continue;
255         }
256         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
257         {
258             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
259             i_coeff += p_tab->i_run;
260             if( i_coeff < 64 )
261             {
262                 goto store_coeff;
263             }
264         }
265         else if( i_code >= 0x0080 )
266         {
267             p_tab = DCT_13 - 16 + (i_code >> 3);
268             i_coeff += p_tab->i_run;
269             if( i_coeff < 64 )
270             {
271                 goto store_coeff;
272             }
273         }
274         else if( i_code >= 0x0020 )
275         {
276             p_tab = DCT_15 - 16 + (i_code >> 1);
277             i_coeff += p_tab->i_run;
278             if( i_coeff < 64 )
279             {
280                 goto store_coeff;
281             }
282         }
283         else
284         {
285             p_tab = DCT_16 + i_code;
286             i_coeff += p_tab->i_run;
287             if( i_coeff < 64 )
288             {
289                 goto store_coeff;
290             }
291         }
292
293         intf_WarnMsg( 2, "Intra-B14 coeff is out of bound" );
294         p_vpar->picture.b_error = 1;
295         break;
296     }
297
298     p_dest[63] ^= i_mismatch & 1;
299     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
300
301     if( i_nc <= 1 )
302     {
303         if( p_dest[63] )
304         {
305             if( i_nc == 0 )
306             {
307                 p_idct->pf_idct = p_vpar->pf_sparse_idct_copy;
308                 p_idct->i_sparse_pos = 63;
309             }
310             else
311             {
312                 p_idct->pf_idct = p_vpar->pf_idct_copy;
313             }
314         }
315         else 
316         {
317             p_idct->pf_idct = p_vpar->pf_sparse_idct_copy;
318             p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
319         }
320     }
321     else
322     {
323         p_idct->pf_idct = p_vpar->pf_idct_copy;
324     }
325 }
326
327 /*****************************************************************************
328  * MPEG2IntraB15 : Decode an intra block according to ISO/IEC 13818-2 table B15
329  *****************************************************************************/
330 static void MPEG2IntraB15( vpar_thread_t * p_vpar, idct_inner_t * p_idct,
331                            u8 * pi_quant )
332 {
333     int         i_coeff, i_mismatch, i_code, i_pos, i_value, i_nc;
334     s32         i_sign;
335     dct_lookup_t * p_tab;
336
337     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
338     dctelem_t * p_dest = p_idct->pi_block;
339     u8 *        p_scan = p_vpar->picture.pi_scan;
340
341     i_coeff = 0;
342     i_mismatch = ~p_dest[0];
343     i_nc = (p_dest[0] != 0);
344
345     for( ; ; )
346     {
347         if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
348         {
349             p_tab = DCT_B15_8 - 4 + i_code;
350             i_coeff += p_tab->i_run;
351             if( i_coeff < 64 )
352             {
353
354 store_coeff:
355                 i_nc++;
356                 i_pos = p_scan[ i_coeff ];
357                 RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
358                 i_value = (p_tab->i_level * i_q_scale * pi_quant[i_pos])
359                                 >> 4;
360
361                 i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
362                 /* if (i_sign) i_value = -i_value; */
363                 i_value = (i_value ^ i_sign) - i_sign;
364
365                 SATURATE( i_value );
366                 p_dest[i_pos] = i_value;
367                 i_mismatch ^= i_value;
368
369                 continue;
370             }
371             else
372             {
373                 if( i_coeff >= 128 )
374                 {
375                     /* End of block */
376                     break;
377                 }
378
379                 /* Escape code */
380                 i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
381                 if( i_coeff >= 64 )
382                 {
383                     /* Illegal, but needed to avoid overflow */
384                     intf_WarnMsg( 2, "Intra-B15 coeff is out of bound" );
385                     p_vpar->picture.b_error = 1;
386                     break;
387                 }
388
389                 i_nc++;
390                 i_pos = p_scan[i_coeff];
391                 i_value = (GetSignedBits( &p_vpar->bit_stream, 12 )
392                             * i_q_scale * pi_quant[i_pos]) / 16;
393
394                 SATURATE( i_value );
395                 p_dest[i_pos] = i_value;
396                 i_mismatch ^= i_value;
397                 continue;
398             }
399         }
400         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
401         {
402             p_tab = DCT_B15_10 - 8 + (i_code >> 6);
403             i_coeff += p_tab->i_run;
404             if( i_coeff < 64 )
405             {
406                 goto store_coeff;
407             }
408         }
409         else if( i_code >= 0x0080 )
410         {
411             p_tab = DCT_13 - 16 + (i_code >> 3);
412             i_coeff += p_tab->i_run;
413             if( i_coeff < 64 )
414             {
415                 goto store_coeff;
416             }
417         }
418         else if( i_code >= 0x0020 )
419         {
420             p_tab = DCT_15 - 16 + (i_code >> 1);
421             i_coeff += p_tab->i_run;
422             if( i_coeff < 64 )
423             {
424                 goto store_coeff;
425             }
426         }
427         else
428         {
429             p_tab = DCT_16 + i_code;
430             i_coeff += p_tab->i_run;
431             if( i_coeff < 64 )
432             {
433                 goto store_coeff;
434             }
435         }
436
437         intf_WarnMsg( 2, "Intra-B15 coeff is out of bound" );
438         p_vpar->picture.b_error = 1;
439         break;
440     }
441
442     p_dest[63] ^= i_mismatch & 1;
443     RemoveBits( &p_vpar->bit_stream, 4 ); /* End of Block */
444
445     if( i_nc <= 1 )
446     {
447         if( p_dest[63] )
448         {
449             if( i_nc == 0 )
450             {
451                 p_idct->pf_idct = p_vpar->pf_sparse_idct_copy;
452                 p_idct->i_sparse_pos = 63;
453             }
454             else
455             {
456                 p_idct->pf_idct = p_vpar->pf_idct_copy;
457             }
458         }
459         else 
460         {
461             p_idct->pf_idct = p_vpar->pf_sparse_idct_copy;
462             p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
463         }
464     }
465     else
466     {
467         p_idct->pf_idct = p_vpar->pf_idct_copy;
468     }
469 }
470
471 /*****************************************************************************
472  * MPEG2NonIntra : Decode a non-intra MPEG-2 block
473  *****************************************************************************/
474 static void MPEG2NonIntra( vpar_thread_t * p_vpar, idct_inner_t * p_idct,
475                            u8 * pi_quant )
476 {
477     int         i_coeff, i_mismatch, i_code, i_pos, i_value, i_nc;
478     s32         i_sign;
479     dct_lookup_t * p_tab;
480
481     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
482     dctelem_t * p_dest = p_idct->pi_block;
483     u8 *        p_scan = p_vpar->picture.pi_scan;
484
485     i_coeff = -1;
486     i_mismatch = 1;
487     i_nc = 0;
488
489     if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
490     {
491         p_tab = DCT_B14DC_5 - 5 + i_code;
492         goto coeff_1;
493     }
494     else
495     {
496         goto coeff_2;
497     }
498
499     for( ; ; )
500     {
501         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
502         {
503             p_tab = DCT_B14AC_5 - 5 + i_code;
504 coeff_1:
505             i_coeff += p_tab->i_run;
506             if( i_coeff >= 64 )
507             {
508                 /* End of block */
509                 break;
510             }
511
512 store_coeff:
513             i_nc++;
514             i_pos = p_scan[ i_coeff ];
515             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
516             i_value = ((2 * p_tab->i_level + 1) * i_q_scale * pi_quant[i_pos])
517                             >> 5;
518
519             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
520             /* if (i_sign) i_value = -i_value; */
521             i_value = (i_value ^ i_sign) - i_sign;
522
523             SATURATE( i_value );
524             p_dest[i_pos] = i_value;
525             i_mismatch ^= i_value;
526
527             continue;
528         }
529 coeff_2:
530         if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
531         {
532             p_tab = DCT_B14_8 - 4 + i_code;
533             i_coeff += p_tab->i_run;
534             if( i_coeff < 64 )
535             {
536                 /* Normal coefficient */
537                 goto store_coeff;
538             }
539
540             /* Escape code */
541             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
542             if( i_coeff >= 64 )
543             {
544                 /* Illegal, but needed to avoid overflow */
545                 intf_WarnMsg( 2, "MPEG2NonIntra coeff is out of bound" );
546                 p_vpar->picture.b_error = 1;
547                 break;
548             }
549
550             i_nc++;
551             i_pos = p_scan[i_coeff];
552             i_value = 2 * (ShowSignedBits( &p_vpar->bit_stream, 1 )
553                             + GetSignedBits( &p_vpar->bit_stream, 12 )) + 1;
554
555             i_value = (i_value * i_q_scale * pi_quant[i_pos]) / 32;
556
557             SATURATE( i_value );
558             p_dest[i_pos] = i_value;
559             i_mismatch ^= i_value;
560             continue;
561         }
562         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
563         {
564             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
565             i_coeff += p_tab->i_run;
566             if( i_coeff < 64 )
567             {
568                 goto store_coeff;
569             }
570         }
571         else if( i_code >= 0x0080 )
572         {
573             p_tab = DCT_13 - 16 + (i_code >> 3);
574             i_coeff += p_tab->i_run;
575             if( i_coeff < 64 )
576             {
577                 goto store_coeff;
578             }
579         }
580         else if( i_code >= 0x0020 )
581         {
582             p_tab = DCT_15 - 16 + (i_code >> 1);
583             i_coeff += p_tab->i_run;
584             if( i_coeff < 64 )
585             {
586                 goto store_coeff;
587             }
588         }
589         else
590         {
591             p_tab = DCT_16 + i_code;
592             i_coeff += p_tab->i_run;
593             if( i_coeff < 64 )
594             {
595                 goto store_coeff;
596             }
597         }
598
599         intf_WarnMsg( 2, "MPEG2NonIntra coeff is out of bound" );
600         p_vpar->picture.b_error = 1;
601         break;
602     }
603
604     p_dest[63] ^= i_mismatch & 1;
605     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
606
607     if( i_nc <= 1 )
608     {
609         if( p_dest[63] )
610         {
611             if( i_nc == 0 )
612             {
613                 p_idct->pf_idct = p_vpar->pf_sparse_idct_add;
614                 p_idct->i_sparse_pos = 63;
615             }
616             else
617             {
618                 p_idct->pf_idct = p_vpar->pf_idct_add;
619             }
620         }
621         else 
622         {
623             p_idct->pf_idct = p_vpar->pf_sparse_idct_add;
624             if( i_nc == 0 )
625             {
626                 p_idct->i_sparse_pos = 0;
627             }
628             else
629             {
630                 p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
631             }
632         }
633     }
634     else
635     {
636         p_idct->pf_idct = p_vpar->pf_idct_add;
637     }
638 }
639
640 /*****************************************************************************
641  * MPEG1Intra : Decode an MPEG-1 intra block
642  *****************************************************************************/
643 static void MPEG1Intra( vpar_thread_t * p_vpar, idct_inner_t * p_idct,
644                         u8 * pi_quant )
645 {
646     int         i_coeff, i_code, i_pos, i_value, i_nc;
647     s32         i_sign;
648     dct_lookup_t * p_tab;
649
650     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
651     dctelem_t * p_dest = p_idct->pi_block;
652     u8 *        p_scan = p_vpar->picture.pi_scan;
653
654     i_coeff = 0;
655     i_nc = (p_dest[0] != 0);
656
657     for( ; ; )
658     {
659         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
660         {
661             p_tab = DCT_B14AC_5 - 5 + i_code;
662             i_coeff += p_tab->i_run;
663             if( i_coeff >= 64 )
664             {
665                 /* End of block */
666                 break;
667             }
668
669 store_coeff:
670             i_nc++;
671             i_pos = p_scan[ i_coeff ];
672             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
673             i_value = (p_tab->i_level * i_q_scale * pi_quant[i_pos])
674                             >> 4;
675
676             /* Oddification */
677             i_value = (i_value - 1) | 1;
678
679             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
680             /* if (i_sign) i_value = -i_value; */
681             i_value = (i_value ^ i_sign) - i_sign;
682
683             SATURATE( i_value );
684             p_dest[i_pos] = i_value;
685
686             continue;
687         }
688         else if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
689         {
690             p_tab = DCT_B14_8 - 4 + i_code;
691             i_coeff += p_tab->i_run;
692             if( i_coeff < 64 )
693             {
694                 /* Normal coefficient */
695                 goto store_coeff;
696             }
697
698             /* Escape code */
699             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
700             if( i_coeff >= 64 )
701             {
702                 /* Illegal, but needed to avoid overflow */
703                 intf_WarnMsg( 2, "MPEG1Intra coeff is out of bound" );
704                 p_vpar->picture.b_error = 1;
705                 break;
706             }
707
708             i_nc++;
709             i_pos = p_scan[i_coeff];
710             
711             i_value = ShowSignedBits( &p_vpar->bit_stream, 8 );
712             if( !(i_value & 0x7F) )
713             {
714                 RemoveBits( &p_vpar->bit_stream, 8 );
715                 i_value = ShowBits( &p_vpar->bit_stream, 8 ) + 2 * i_value;
716             }
717
718             i_value = (i_value * i_q_scale * pi_quant[i_pos]) / 16;
719
720             /* Oddification */
721             i_value = (i_value + ~ShowSignedBits( &p_vpar->bit_stream, 1 )) | 1;
722
723             SATURATE( i_value );
724             p_dest[i_pos] = i_value;
725             RemoveBits( &p_vpar->bit_stream, 8 );
726             continue;
727         }
728         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
729         {
730             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
731             i_coeff += p_tab->i_run;
732             if( i_coeff < 64 )
733             {
734                 goto store_coeff;
735             }
736         }
737         else if( i_code >= 0x0080 )
738         {
739             p_tab = DCT_13 - 16 + (i_code >> 3);
740             i_coeff += p_tab->i_run;
741             if( i_coeff < 64 )
742             {
743                 goto store_coeff;
744             }
745         }
746         else if( i_code >= 0x0020 )
747         {
748             p_tab = DCT_15 - 16 + (i_code >> 1);
749             i_coeff += p_tab->i_run;
750             if( i_coeff < 64 )
751             {
752                 goto store_coeff;
753             }
754         }
755         else
756         {
757             p_tab = DCT_16 + i_code;
758             i_coeff += p_tab->i_run;
759             if( i_coeff < 64 )
760             {
761                 goto store_coeff;
762             }
763         }
764
765         intf_WarnMsg( 2, "MPEG1Intra coeff is out of bound" );
766         p_vpar->picture.b_error = 1;
767         break;
768     }
769
770     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
771
772     if( i_nc <= 1 )
773     {
774         p_idct->pf_idct = p_vpar->pf_sparse_idct_copy;
775         p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
776     }
777     else
778     {
779         p_idct->pf_idct = p_vpar->pf_idct_copy;
780     }
781 }
782
783 /*****************************************************************************
784  * MPEG1NonIntra : Decode a non-intra MPEG-1 block
785  *****************************************************************************/
786 static void MPEG1NonIntra( vpar_thread_t * p_vpar, idct_inner_t * p_idct,
787                            u8 * pi_quant )
788 {
789     int         i_coeff, i_code, i_pos, i_value, i_nc;
790     s32         i_sign;
791     dct_lookup_t * p_tab;
792
793     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
794     dctelem_t * p_dest = p_idct->pi_block;
795     u8 *        p_scan = p_vpar->picture.pi_scan;
796
797     i_coeff = -1;
798     i_nc = 0;
799
800     if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
801     {
802         p_tab = DCT_B14DC_5 - 5 + i_code;
803         goto coeff_1;
804     }
805     else
806     {
807         goto coeff_2;
808     }
809
810     for( ; ; )
811     {
812         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
813         {
814             p_tab = DCT_B14AC_5 - 5 + i_code;
815 coeff_1:
816             i_coeff += p_tab->i_run;
817             if( i_coeff >= 64 )
818             {
819                 /* End of block */
820                 break;
821             }
822
823 store_coeff:
824             i_nc++;
825             i_pos = p_scan[ i_coeff ];
826             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
827             i_value = ((2 * p_tab->i_level + 1) * i_q_scale * pi_quant[i_pos])
828                             >> 5;
829
830             /* Oddification */
831             i_value = (i_value - 1) | 1;
832
833             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
834             /* if (i_sign) i_value = -i_value; */
835             i_value = (i_value ^ i_sign) - i_sign;
836
837             SATURATE( i_value );
838             p_dest[i_pos] = i_value;
839
840             continue;
841         }
842 coeff_2:
843         if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
844         {
845             p_tab = DCT_B14_8 - 4 + i_code;
846             i_coeff += p_tab->i_run;
847             if( i_coeff < 64 )
848             {
849                 /* Normal coefficient */
850                 goto store_coeff;
851             }
852
853             /* Escape code */
854             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
855             if( i_coeff >= 64 )
856             {
857                 /* Illegal, but needed to avoid overflow */
858                 intf_WarnMsg( 2, "MPEG1NonIntra coeff is out of bound" );
859                 p_vpar->picture.b_error = 1;
860                 break;
861             }
862
863             i_nc++;
864             i_pos = p_scan[i_coeff];
865             i_value = ShowSignedBits( &p_vpar->bit_stream, 8 );
866             if( !(i_value & 0x7F) )
867             {
868                 RemoveBits( &p_vpar->bit_stream, 8 );
869                 i_value = ShowBits( &p_vpar->bit_stream, 8 ) + 2 * i_value;
870             }
871             i_sign = ShowSignedBits( &p_vpar->bit_stream, 1 );
872             i_value = 2 * (i_sign + i_value) + 1;
873             i_value = (i_value * i_q_scale * pi_quant[i_pos]) / 32;
874
875             /* Oddification */
876             i_value = (i_value + ~i_sign) | 1;
877
878             SATURATE( i_value );
879             p_dest[i_pos] = i_value;
880             RemoveBits( &p_vpar->bit_stream, 8 );
881             continue;
882         }
883         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
884         {
885             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
886             i_coeff += p_tab->i_run;
887             if( i_coeff < 64 )
888             {
889                 goto store_coeff;
890             }
891         }
892         else if( i_code >= 0x0080 )
893         {
894             p_tab = DCT_13 - 16 + (i_code >> 3);
895             i_coeff += p_tab->i_run;
896             if( i_coeff < 64 )
897             {
898                 goto store_coeff;
899             }
900         }
901         else if( i_code >= 0x0020 )
902         {
903             p_tab = DCT_15 - 16 + (i_code >> 1);
904             i_coeff += p_tab->i_run;
905             if( i_coeff < 64 )
906             {
907                 goto store_coeff;
908             }
909         }
910         else
911         {
912             p_tab = DCT_16 + i_code;
913             i_coeff += p_tab->i_run;
914             if( i_coeff < 64 )
915             {
916                 goto store_coeff;
917             }
918         }
919
920         intf_WarnMsg( 2, "MPEG1NonIntra coeff is out of bound" );
921         p_vpar->picture.b_error = 1;
922         break;
923     }
924
925     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
926
927     if( i_nc <= 1 )
928     {
929         p_idct->pf_idct = p_vpar->pf_sparse_idct_add;
930         if( i_nc == 0 )
931         {
932             p_idct->i_sparse_pos = 0;
933         }
934         else
935         {
936             p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
937         }
938     }
939     else
940     {
941         p_idct->pf_idct = p_vpar->pf_idct_add;
942     }
943 }
944
945 #undef SATURATE
946
947 /*****************************************************************************
948  * *MB : decode all blocks of the macroblock
949  *****************************************************************************/
950 #define DECODE_LUMABLOCK( I_B, PF_MBFUNC )                                  \
951     p_idct = &p_mb->p_idcts[I_B];                                           \
952     memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                    \
953     p_vpar->mb.pi_dc_dct_pred[0] += GetLumaDCDiff( p_vpar );                \
954     p_idct->pi_block[0] = p_vpar->mb.pi_dc_dct_pred[0]                      \
955                          << (3 - p_vpar->picture.i_intra_dc_precision );    \
956     PF_MBFUNC( p_vpar, p_idct, p_vpar->sequence.intra_quant.pi_matrix );
957
958 #define DECODE_CHROMABLOCK( I_B, PF_MBFUNC, I_CC )                          \
959     p_idct = &p_mb->p_idcts[I_B];                                           \
960     memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                    \
961     p_vpar->mb.pi_dc_dct_pred[I_CC] += GetChromaDCDiff( p_vpar );           \
962     p_idct->pi_block[0] = p_vpar->mb.pi_dc_dct_pred[I_CC]                   \
963                          << (3 - p_vpar->picture.i_intra_dc_precision );    \
964     PF_MBFUNC( p_vpar, p_idct,                                              \
965                p_vpar->sequence.chroma_intra_quant.pi_matrix );
966
967 #define DECLARE_INTRAMB( PSZ_NAME, PF_MBFUNC )                              \
968 static __inline__ void PSZ_NAME( vpar_thread_t * p_vpar,                    \
969                                  macroblock_t * p_mb )                      \
970 {                                                                           \
971     idct_inner_t *  p_idct;                                                 \
972     int             i_b = 4;                                                \
973                                                                             \
974     p_mb->p_y_data = p_mb->pp_dest[0] + p_vpar->mb.i_offset;                \
975     p_mb->p_u_data = p_mb->pp_dest[1] + (p_vpar->mb.i_offset                \
976                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
977     p_mb->p_v_data = p_mb->pp_dest[2] + (p_vpar->mb.i_offset                \
978                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
979                                                                             \
980     DECODE_LUMABLOCK( 0, PF_MBFUNC );                                       \
981     DECODE_LUMABLOCK( 1, PF_MBFUNC );                                       \
982     DECODE_LUMABLOCK( 2, PF_MBFUNC );                                       \
983     DECODE_LUMABLOCK( 3, PF_MBFUNC );                                       \
984                                                                             \
985     do                                                                      \
986     {                                                                       \
987         DECODE_CHROMABLOCK( i_b, PF_MBFUNC, 1 );                            \
988         DECODE_CHROMABLOCK( i_b + 1, PF_MBFUNC, 2 );                        \
989         i_b += 2;                                                           \
990     }                                                                       \
991     while( i_b < 4 + p_vpar->sequence.i_chroma_nb_blocks );                 \
992 }
993
994 DECLARE_INTRAMB( MPEG1IntraMB, MPEG1Intra );
995 DECLARE_INTRAMB( MPEG2IntraB14MB, MPEG2IntraB14 );
996 DECLARE_INTRAMB( MPEG2IntraB15MB, MPEG2IntraB15 );
997
998 #undef DECLARE_INTRAMB
999 #undef DECODE_LUMABLOCK
1000 #undef DECODE_CHROMABLOCK
1001
1002 #define DECODE_LUMABLOCK( I_B, PF_MBFUNC )                                  \
1003     if( p_mb->i_coded_block_pattern & (1 << (11 - (I_B))) )                 \
1004     {                                                                       \
1005         p_idct = &p_mb->p_idcts[I_B];                                       \
1006         memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                \
1007         PF_MBFUNC( p_vpar, p_idct,                                          \
1008                    p_vpar->sequence.nonintra_quant.pi_matrix );             \
1009     }
1010
1011 #define DECODE_CHROMABLOCK( I_B, PF_MBFUNC )                                \
1012     if( p_mb->i_coded_block_pattern & (1 << (11 - (I_B))) )                 \
1013     {                                                                       \
1014         p_idct = &p_mb->p_idcts[I_B];                                       \
1015         memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                \
1016         PF_MBFUNC( p_vpar, p_idct,                                          \
1017                    p_vpar->sequence.chroma_nonintra_quant.pi_matrix );      \
1018     }
1019
1020 #define DECLARE_NONINTRAMB( PSZ_NAME, PF_MBFUNC )                           \
1021 static __inline__ void PSZ_NAME( vpar_thread_t * p_vpar,                    \
1022                                  macroblock_t * p_mb )                      \
1023 {                                                                           \
1024     idct_inner_t *  p_idct;                                                 \
1025     int             i_b = 4;                                                \
1026                                                                             \
1027     p_mb->p_y_data = p_mb->pp_dest[0] + p_vpar->mb.i_offset;                \
1028     p_mb->p_u_data = p_mb->pp_dest[1] + (p_vpar->mb.i_offset                \
1029                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
1030     p_mb->p_v_data = p_mb->pp_dest[2] + (p_vpar->mb.i_offset                \
1031                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
1032                                                                             \
1033     DECODE_LUMABLOCK( 0, PF_MBFUNC );                                       \
1034     DECODE_LUMABLOCK( 1, PF_MBFUNC );                                       \
1035     DECODE_LUMABLOCK( 2, PF_MBFUNC );                                       \
1036     DECODE_LUMABLOCK( 3, PF_MBFUNC );                                       \
1037                                                                             \
1038     do                                                                      \
1039     {                                                                       \
1040         DECODE_CHROMABLOCK( i_b, PF_MBFUNC );                               \
1041         DECODE_CHROMABLOCK( i_b + 1, PF_MBFUNC );                           \
1042         i_b += 2;                                                           \
1043     }                                                                       \
1044     while( i_b < 4 + p_vpar->sequence.i_chroma_nb_blocks );                 \
1045 }
1046
1047 DECLARE_NONINTRAMB( MPEG1NonIntraMB, MPEG1NonIntra );
1048 DECLARE_NONINTRAMB( MPEG2NonIntraMB, MPEG2NonIntra );
1049
1050 #undef DECLARE_NONINTRAMB
1051 #undef DECODE_LUMABLOCK
1052 #undef DECODE_CHROMABLOCK
1053
1054
1055 /*
1056  * Motion vectors
1057  */
1058
1059 /****************************************************************************
1060  * MotionDelta : Parse the next motion delta
1061  ****************************************************************************/
1062 static __inline__ int MotionDelta( vpar_thread_t * p_vpar, int i_f_code )
1063 {
1064     int         i_delta, i_sign, i_code;
1065     lookup_t *  p_tab;
1066
1067     if( ShowBits( &p_vpar->bit_stream, 1 ) )
1068     {
1069         RemoveBits( &p_vpar->bit_stream, 1 );
1070         return 0;
1071     }
1072
1073     if( (i_code = ShowBits( &p_vpar->bit_stream, 6 )) >= 0x3 )
1074     {
1075         p_tab = MV_4 + (i_code >> 2);
1076         i_delta = (p_tab->i_value << i_f_code) + 1;
1077         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1078
1079         i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
1080         if( i_f_code )
1081         {
1082             i_delta += GetBits( &p_vpar->bit_stream, i_f_code );
1083         }
1084
1085         return (i_delta ^ i_sign) - i_sign;
1086
1087     }
1088     else
1089     {
1090         p_tab = MV_10 + ShowBits( &p_vpar->bit_stream, 10 );
1091         i_delta = (p_tab->i_value << i_f_code) + 1;
1092         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1093
1094         i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
1095         if( i_f_code )
1096         {
1097             i_delta += GetBits( &p_vpar->bit_stream, i_f_code );
1098         }
1099
1100         return (i_delta ^ i_sign) - i_sign;
1101     }
1102 }
1103
1104 /****************************************************************************
1105  * BoundMotionVector : Bound a motion_vector :-)
1106  ****************************************************************************/
1107 static __inline__ int BoundMotionVector( int i_vector, int i_f_code )
1108 {
1109     int i_limit;
1110
1111     i_limit = 16 << i_f_code;
1112
1113     if( i_vector >= i_limit )
1114     {
1115         return i_vector - 2 * i_limit;
1116     }
1117     else if( i_vector < -i_limit)
1118     {
1119         return i_vector + 2 * i_limit;
1120     }
1121     else
1122     {
1123         return i_vector;
1124     }
1125 }
1126
1127 /****************************************************************************
1128  * GetDMV : Decode a differential motion vector (Dual Prime Arithmetic)
1129  ****************************************************************************/
1130 static __inline__ int GetDMV( vpar_thread_t * p_vpar )
1131 {
1132     dmv_lookup_t * p_tab;
1133
1134     p_tab = DMV_2 + ShowBits( &p_vpar->bit_stream, 2 );
1135     RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1136     return( p_tab->i_value );
1137 }
1138
1139 /****************************************************************************
1140  * Motion* : Parse motion vectors
1141  ****************************************************************************/
1142 #define MOTION_BLOCK( b_aver, i_x, i_y, i_dest,                             \
1143                       pp_src, i_src, i_str, i_hei,                          \
1144                       b_s_half )                                            \
1145     do {                                                                    \
1146         motion_inner_t * p_m_inner = p_mb->p_motions + p_mb->i_nb_motions;  \
1147         p_m_inner->b_average = b_aver;                                      \
1148         p_m_inner->i_x_pred = i_x;                                          \
1149         p_m_inner->i_y_pred = i_y;                                          \
1150         p_m_inner->pp_source[0] = pp_src[0];                                \
1151         p_m_inner->pp_source[1] = pp_src[1];                                \
1152         p_m_inner->pp_source[2] = pp_src[2];                                \
1153         p_m_inner->i_dest_offset = i_dest;                                  \
1154         p_m_inner->i_src_offset = i_src;                                    \
1155         p_m_inner->i_stride = i_str;                                        \
1156         p_m_inner->i_height = i_hei;                                        \
1157         p_m_inner->b_second_half = b_s_half;                                \
1158         p_mb->i_nb_motions++;                                               \
1159     } while( 0 );
1160
1161 /* MPEG-1 predictions. */
1162
1163 static void MotionMPEG1( vpar_thread_t * p_vpar,
1164                                     macroblock_t * p_mb,
1165                                     motion_t * p_motion,
1166                                     boolean_t b_average )
1167 {
1168     int i_motion_x, i_motion_y;
1169     int i_offset = p_vpar->mb.i_offset;
1170     int i_width = p_vpar->picture.i_field_width;
1171     
1172     i_motion_x = p_motion->ppi_pmv[0][0]
1173                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1174     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1175     p_motion->ppi_pmv[0][0] = i_motion_x;
1176
1177     i_motion_y = p_motion->ppi_pmv[0][1]
1178                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1179     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[0] );
1180     p_motion->ppi_pmv[0][1] = i_motion_y;
1181
1182     if( p_motion->pi_f_code[1] )
1183     {
1184         i_motion_x <<= 1;
1185         i_motion_y <<= 1;
1186     }
1187
1188     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1189                   p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1190 }
1191
1192 static void MotionMPEG1Reuse( vpar_thread_t * p_vpar,
1193                                          macroblock_t * p_mb,
1194                                          motion_t * p_motion,
1195                                          boolean_t b_average )
1196 {
1197     int i_motion_x, i_motion_y;
1198     int i_offset = p_vpar->mb.i_offset;
1199     int i_width = p_vpar->picture.i_field_width;
1200
1201     i_motion_x = p_motion->ppi_pmv[0][0];
1202     i_motion_y = p_motion->ppi_pmv[0][1];
1203
1204     if( p_motion->pi_f_code[1] )
1205     {
1206         i_motion_x <<= 1;
1207         i_motion_y <<= 1;
1208     }
1209
1210      MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1211                   i_offset, p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1212 }
1213
1214 /* MPEG-2 frame predictions. */
1215
1216 static void MotionFrameFrame( vpar_thread_t * p_vpar,
1217                                          macroblock_t * p_mb,
1218                                          motion_t * p_motion,
1219                                          boolean_t b_average )
1220 {
1221     int i_motion_x, i_motion_y;
1222     int i_offset = p_vpar->mb.i_offset;
1223     int i_width = p_vpar->picture.i_field_width;
1224
1225     i_motion_x = p_motion->ppi_pmv[0][0]
1226                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1227     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1228     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1229
1230     i_motion_y = p_motion->ppi_pmv[0][1]
1231                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1232     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1233     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1234
1235     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1236                   p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1237 }
1238
1239 static void MotionFrameField( vpar_thread_t * p_vpar,
1240                                          macroblock_t * p_mb,
1241                                          motion_t * p_motion,
1242                                          boolean_t b_average )
1243 {
1244     int i_motion_x, i_motion_y, i_field_select;
1245     int i_offset = p_vpar->mb.i_offset;
1246     int i_width = p_vpar->picture.i_field_width;
1247
1248     i_field_select = GetSignedBits( &p_vpar->bit_stream, 1 );
1249
1250     i_motion_x = p_motion->ppi_pmv[0][0]
1251                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1252     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1253     p_motion->ppi_pmv[0][0] = i_motion_x;
1254
1255     i_motion_y = (p_motion->ppi_pmv[0][1] >> 1)
1256                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1257     /* According to ISO/IEC 13818-2 section 7.6.3.2, the vertical motion
1258      * vector is restricted to a range that implies it doesn't need to
1259      * be bound. */
1260     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1261     p_motion->ppi_pmv[0][1] = i_motion_y << 1;
1262
1263     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1264                   p_motion->pppi_ref[0], i_offset + (i_field_select & i_width),
1265                   i_width * 2, 8, 0 );
1266
1267     i_field_select = GetSignedBits( &p_vpar->bit_stream, 1 );
1268
1269     i_motion_x = p_motion->ppi_pmv[1][0]
1270                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1271     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1272     p_motion->ppi_pmv[1][0] = i_motion_x;
1273
1274     i_motion_y = (p_motion->ppi_pmv[1][1] >> 1)
1275                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1276     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1277     p_motion->ppi_pmv[1][1] = i_motion_y << 1;
1278
1279     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset + i_width,
1280                   p_motion->pppi_ref[0], i_offset + (i_field_select & i_width),
1281                   i_width * 2, 8, 0 );
1282 }
1283
1284 static void MotionFrameDMV( vpar_thread_t * p_vpar,
1285                                        macroblock_t * p_mb,
1286                                        motion_t * p_motion,
1287                                        boolean_t b_average )
1288 {
1289     int i_motion_x, i_motion_y;
1290     int i_dmv_x, i_dmv_y;
1291     int i_tmp_x, i_tmp_y;
1292
1293     int i_offset = p_vpar->mb.i_offset;
1294     int i_width = p_vpar->picture.i_field_width;
1295     int m;
1296
1297     i_motion_x = p_motion->ppi_pmv[0][0]
1298                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1299     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1300     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1301
1302     i_dmv_x = GetDMV( p_vpar );
1303
1304     i_motion_y = (p_motion->ppi_pmv[0][1] >> 1)
1305                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1306     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1307     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y << 1;
1308
1309     i_dmv_y = GetDMV( p_vpar );
1310
1311     /* First field. */
1312     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset,
1313                   p_motion->pppi_ref[0], i_offset, i_width * 2, 8, 0 );
1314     m = p_vpar->picture.b_top_field_first ? 1 : 3;
1315     i_tmp_x = ((i_motion_x * m + (i_motion_x > 0)) >> 1) + i_dmv_x;
1316     i_tmp_y = ((i_motion_y * m + (i_motion_y > 0)) >> 1) + i_dmv_y - 1;
1317     MOTION_BLOCK( 1, i_tmp_x, i_tmp_y, i_offset, p_motion->pppi_ref[0],
1318                   i_offset + i_width, i_width * 2, 8, 0 );
1319
1320     /* Second field. */
1321     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset + i_width,
1322                   p_motion->pppi_ref[0], i_offset + i_width, i_width * 2, 8, 0 );
1323     m = p_vpar->picture.b_top_field_first ? 3 : 1;
1324     i_tmp_x = ((i_motion_x * m + (i_motion_x > 0)) >> 1) + i_dmv_x;
1325     i_tmp_y = ((i_motion_y * m + (i_motion_y > 0)) >> 1) + i_dmv_y + 1;
1326     MOTION_BLOCK( 1, i_tmp_x, i_tmp_y, i_offset + i_width,
1327                   p_motion->pppi_ref[0], i_offset, i_width * 2, 8, 0 );
1328 }
1329
1330 static void MotionFrameZero( vpar_thread_t * p_vpar,
1331                                         macroblock_t * p_mb,
1332                                         motion_t * p_motion,
1333                                         boolean_t b_average )
1334 {
1335     int i_offset = p_vpar->mb.i_offset;
1336     int i_width = p_vpar->picture.i_field_width;
1337
1338     MOTION_BLOCK( b_average, 0, 0, i_offset, p_motion->pppi_ref[0],
1339                   i_offset, i_width, 16, 0 );
1340 }
1341
1342 static void MotionFrameReuse( vpar_thread_t * p_vpar,
1343                                          macroblock_t * p_mb,
1344                                          motion_t * p_motion,
1345                                          boolean_t b_average )
1346 {
1347     int i_offset = p_vpar->mb.i_offset;
1348     int i_width = p_vpar->picture.i_field_width;
1349
1350     MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1351                   i_offset, p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1352 }
1353
1354 /* MPEG-2 field predictions. */
1355
1356 static void MotionFieldField( vpar_thread_t * p_vpar,
1357                                          macroblock_t * p_mb,
1358                                          motion_t * p_motion,
1359                                          boolean_t b_average )
1360 {
1361     int i_motion_x, i_motion_y;
1362     boolean_t b_field_select;
1363     int i_offset = p_vpar->mb.i_offset;
1364     int i_width = p_vpar->picture.i_field_width;
1365
1366     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1367
1368     i_motion_x = p_motion->ppi_pmv[0][0]
1369                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1370     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1371     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1372
1373     i_motion_y = p_motion->ppi_pmv[0][1]
1374                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1375     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1376     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1377
1378     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1379                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 16, 0 );
1380 }
1381
1382 static void MotionField16x8( vpar_thread_t * p_vpar,
1383                                         macroblock_t * p_mb,
1384                                         motion_t * p_motion,
1385                                         boolean_t b_average )
1386 {
1387     int i_motion_x, i_motion_y;
1388     boolean_t b_field_select;
1389     int i_offset = p_vpar->mb.i_offset;
1390     int i_width = p_vpar->picture.i_field_width;
1391
1392     /* First half. */
1393     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1394
1395     i_motion_x = p_motion->ppi_pmv[0][0]
1396                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1397     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1398     p_motion->ppi_pmv[0][0] = i_motion_x;
1399
1400     i_motion_y = p_motion->ppi_pmv[0][1]
1401                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1402     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1403     p_motion->ppi_pmv[0][1] = i_motion_y;
1404
1405     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1406                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 8, 0 );
1407
1408     /* Second half. */
1409     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1410
1411     i_motion_x = p_motion->ppi_pmv[1][0]
1412                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1413     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1414     p_motion->ppi_pmv[1][0] = i_motion_x;
1415
1416     i_motion_y = p_motion->ppi_pmv[1][1]
1417                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1418     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1419     p_motion->ppi_pmv[1][1] = i_motion_y;
1420
1421     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1422                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 8, 1 );
1423 }
1424
1425 static void MotionFieldDMV( vpar_thread_t * p_vpar,
1426                                        macroblock_t * p_mb,
1427                                        motion_t * p_motion,
1428                                        boolean_t b_average )
1429 {
1430     int i_motion_x, i_motion_y;
1431     int i_dmv_x, i_dmv_y;
1432     int i_offset = p_vpar->mb.i_offset;
1433     int i_width = p_vpar->picture.i_field_width;
1434     boolean_t b_current_field = p_vpar->picture.b_current_field;
1435
1436     i_motion_x = p_motion->ppi_pmv[0][0]
1437                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1438     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1439     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1440
1441     i_dmv_x = GetDMV( p_vpar );
1442
1443     i_motion_y = p_motion->ppi_pmv[0][1]
1444                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1445     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1446     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1447
1448     i_dmv_y = GetDMV( p_vpar );
1449
1450     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset,
1451                   p_motion->pppi_ref[b_current_field],
1452                   i_offset, i_width, 16, 0 );
1453
1454     i_motion_x = ((i_motion_x + (i_motion_x > 0)) >> 1) + i_dmv_x;
1455     i_motion_y = ((i_motion_y + (i_motion_y > 0)) >> 1) + i_dmv_y
1456                     + 2 * b_current_field - 1;
1457     MOTION_BLOCK( 1, i_motion_x, i_motion_y, i_offset,
1458                   p_motion->pppi_ref[!b_current_field],
1459                   i_offset, i_width, 16, 0 );
1460 }
1461
1462 static void MotionFieldZero( vpar_thread_t * p_vpar,
1463                                         macroblock_t * p_mb,
1464                                         motion_t * p_motion,
1465                                         boolean_t b_average )
1466 {
1467     int i_offset = p_vpar->mb.i_offset;
1468     int i_width = p_vpar->picture.i_field_width;
1469     boolean_t b_current_field = p_vpar->picture.b_current_field;
1470
1471     MOTION_BLOCK( b_average, 0, 0, i_offset, p_motion->pppi_ref[b_current_field],
1472                   i_offset, i_width, 16, 0 );
1473 }
1474
1475 static void MotionFieldReuse( vpar_thread_t * p_vpar,
1476                                          macroblock_t * p_mb,
1477                                          motion_t * p_motion,
1478                                          boolean_t b_average )
1479 {
1480     int i_offset = p_vpar->mb.i_offset;
1481     int i_width = p_vpar->picture.i_field_width;
1482     boolean_t b_current_field = p_vpar->picture.b_current_field;
1483
1484     MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1485                   i_offset, p_motion->pppi_ref[b_current_field],
1486                   i_offset, i_width, 16, 0 );
1487 }
1488
1489 /* MPEG-2 concealment motion vectors. */
1490
1491 static void MotionFrameConceal( vpar_thread_t * p_vpar,
1492                                            macroblock_t * p_mv,
1493                                            motion_t * p_motion )
1494 {
1495     int i_tmp;
1496
1497     i_tmp = p_motion->ppi_pmv[0][0]
1498                 + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1499     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[0] );
1500     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_tmp;
1501
1502     i_tmp = p_motion->ppi_pmv[0][1]
1503                 + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1504     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[1] );
1505     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_tmp;
1506
1507     /* Marker bit. */
1508     RemoveBits( &p_vpar->bit_stream, 1 );
1509 }
1510
1511 static void MotionFieldConceal( vpar_thread_t * p_vpar,
1512                                            macroblock_t * p_mv,
1513                                            motion_t * p_motion )
1514 {
1515     int i_tmp;
1516
1517     /* field_select */
1518     RemoveBits( &p_vpar->bit_stream, 1 );
1519
1520     i_tmp = p_motion->ppi_pmv[0][0]
1521                 + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1522     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[0] );
1523     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_tmp;
1524
1525     i_tmp = p_motion->ppi_pmv[0][1]
1526                 + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1527     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[1] );
1528     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_tmp;
1529
1530     /* Marker bit. */
1531     RemoveBits( &p_vpar->bit_stream, 1 );
1532 }
1533
1534
1535 /*
1536  * Macroblock information structures
1537  */
1538
1539 /*****************************************************************************
1540  * MacroblockAddressIncrement : Get the macroblock_address_increment field
1541  *****************************************************************************/
1542 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1543 {
1544     lookup_t *  p_tab;
1545     int         i_code;
1546     int         i_mba = 0;
1547
1548     for( ; ; )
1549     {
1550         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 ) ) >= 0x2 )
1551         {
1552             p_tab = MBA_5 - 2 + i_code;
1553             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1554             return( i_mba + p_tab->i_value );
1555         }
1556         else if( (i_code = ShowBits( &p_vpar->bit_stream, 11 )) >= 0x18 )
1557         {
1558             p_tab = MBA_11 - 24 + i_code;
1559             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1560             return( i_mba + p_tab->i_value );
1561         }
1562         else switch( i_code )
1563         {
1564         case 8:
1565             /* Macroblock escape */
1566             i_mba += 33;
1567             /* continue... */
1568         case 15:
1569             /* Macroblock stuffing (MPEG-1 ONLY) */
1570             RemoveBits( &p_vpar->bit_stream, 11 );
1571             break;
1572
1573         default:
1574             /* End of slice, or error */
1575             return 0;
1576         }
1577     }
1578 }
1579
1580 /*****************************************************************************
1581  * CodedPattern : coded_block_pattern
1582  *****************************************************************************/
1583 static __inline__ int CodedPattern( vpar_thread_t * p_vpar )
1584 {
1585     lookup_t *  p_tab;
1586     int         i_code;
1587
1588     if( (i_code = ShowBits( &p_vpar->bit_stream, 7 )) >= 0x10 )
1589     {
1590         p_tab = CBP_7 - 16 + i_code;
1591         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1592     }
1593     else
1594     {
1595         p_tab = CBP_9 + ShowBits( &p_vpar->bit_stream, 9 );
1596         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1597     }
1598     if( p_vpar->sequence.i_chroma_format == CHROMA_420 )
1599     {
1600         return( p_tab->i_value << 6 );
1601     }
1602     if( p_vpar->sequence.i_chroma_format == CHROMA_422 )
1603     {
1604         return( (p_tab->i_value << 6)
1605                  | (GetBits( &p_vpar->bit_stream, 2 ) << 4) );
1606     }
1607     return( (p_tab->i_value << 6)
1608              | GetBits( &p_vpar->bit_stream, 6 ) );
1609 }
1610
1611 /*****************************************************************************
1612  * MacroblockModes : Get the macroblock_modes structure
1613  *****************************************************************************/
1614 static __inline__ int MacroblockModes( vpar_thread_t * p_vpar,
1615                                        macroblock_t * p_mb,
1616                                        int i_coding_type,
1617                                        int i_structure )
1618 {
1619     int         i_mb_modes;
1620     lookup_t *  p_tab;
1621
1622     switch( i_coding_type )
1623     {
1624     case I_CODING_TYPE:
1625         p_tab = MB_I + ShowBits( &p_vpar->bit_stream, 1 );
1626         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1627         i_mb_modes = p_tab->i_value;
1628
1629         if( (i_structure == FRAME_STRUCTURE) &&
1630             (!p_vpar->picture.b_frame_pred_frame_dct) )
1631         {
1632             i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1633                                 * DCT_TYPE_INTERLACED;
1634         }
1635         return( i_mb_modes );
1636
1637     case P_CODING_TYPE:
1638         p_tab = MB_P + ShowBits( &p_vpar->bit_stream, 5 );
1639         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1640         i_mb_modes = p_tab->i_value;
1641
1642         if( i_structure != FRAME_STRUCTURE )
1643         {
1644             if( i_mb_modes & MB_MOTION_FORWARD )
1645             {
1646                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1647                                     * MOTION_TYPE_BASE;
1648             }
1649             return( i_mb_modes );
1650         }
1651         else if( p_vpar->picture.b_frame_pred_frame_dct )
1652         {
1653             if( i_mb_modes & MB_MOTION_FORWARD )
1654             {
1655                 i_mb_modes |= MC_FRAME;
1656             }
1657             return( i_mb_modes );
1658         }
1659         else
1660         {
1661             if( i_mb_modes & MB_MOTION_FORWARD )
1662             {
1663                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1664                                     * MOTION_TYPE_BASE;
1665             }
1666             if( i_mb_modes & (MB_INTRA | MB_PATTERN) )
1667             {
1668                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1669                                     * DCT_TYPE_INTERLACED;
1670             }
1671             return( i_mb_modes );
1672         }
1673
1674     case B_CODING_TYPE:
1675         p_tab = MB_B + ShowBits( &p_vpar->bit_stream, 6 );
1676         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1677         i_mb_modes = p_tab->i_value;
1678
1679         if( i_structure != FRAME_STRUCTURE )
1680         {
1681             if( !( i_mb_modes & MB_INTRA ) )
1682             {
1683                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1684                                     * MOTION_TYPE_BASE;
1685             }
1686             return( i_mb_modes );
1687         }
1688         else if( p_vpar->picture.b_frame_pred_frame_dct )
1689         {
1690             i_mb_modes |= MC_FRAME;
1691             return( i_mb_modes );
1692         }
1693         else
1694         {
1695             if( i_mb_modes & MB_INTRA )
1696             {
1697                 goto mb_intra;
1698             }
1699             i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1700                                 * MOTION_TYPE_BASE;
1701             if( i_mb_modes & (MB_INTRA | MB_PATTERN) )
1702             {
1703 mb_intra:
1704                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1705                                     * DCT_TYPE_INTERLACED;
1706             }
1707             return( i_mb_modes );
1708         }
1709
1710     case D_CODING_TYPE:
1711         RemoveBits( &p_vpar->bit_stream, 1 );
1712         return( MB_INTRA );
1713
1714     default:
1715         return( 0 );
1716     }
1717 }
1718
1719
1720 /*
1721  * Picture data parsing management
1722  */
1723
1724 /*****************************************************************************
1725  * ParseSlice : Parse the next slice structure
1726  *****************************************************************************/
1727 #define MOTION( pf_routine, i_direction )                                   \
1728     if( (i_direction) & MB_MOTION_FORWARD )                                 \
1729     {                                                                       \
1730         pf_routine( p_vpar, p_mb, &p_vpar->mb.f_motion, 0 );                \
1731         if( (i_coding_type == B_CODING_TYPE)                                \
1732                 && ((i_direction) & MB_MOTION_BACKWARD) )                   \
1733         {                                                                   \
1734             pf_routine( p_vpar, p_mb, &p_vpar->mb.b_motion, 1 );            \
1735         }                                                                   \
1736     }                                                                       \
1737     else if( (i_coding_type == B_CODING_TYPE)                               \
1738                  && ((i_direction) & MB_MOTION_BACKWARD) )                  \
1739     {                                                                       \
1740         pf_routine( p_vpar, p_mb, &p_vpar->mb.b_motion, 0 );                \
1741     }
1742
1743 #define CHECK_BOUNDARIES                                                    \
1744     i_offset = p_vpar->mb.i_offset;                                         \
1745     if( i_offset == i_width )                                               \
1746     {                                                                       \
1747         if( i_coding_type != I_CODING_TYPE ||                               \
1748             p_vpar->picture.b_concealment_mv )                              \
1749         {                                                                   \
1750             p_f_motion->pppi_ref[0][0] += 16 * i_offset;                    \
1751             p_f_motion->pppi_ref[0][1] += i_chroma_tmp;                  \
1752             p_f_motion->pppi_ref[0][2] += i_chroma_tmp;                  \
1753         }                                                                   \
1754         if( i_coding_type == B_CODING_TYPE )                                \
1755         {                                                                   \
1756             p_b_motion->pppi_ref[0][0] += 16 * i_offset;                    \
1757             p_b_motion->pppi_ref[0][1] += i_chroma_tmp;                  \
1758             p_b_motion->pppi_ref[0][2] += i_chroma_tmp;                  \
1759         }                                                                   \
1760         p_dest[0] += 16 * i_offset;                                         \
1761         p_dest[1] += 4 * i_offset;                                       \
1762         p_dest[2] += 4 * i_offset;                                       \
1763         i_offset = 0;                                                       \
1764     }                                                                       \
1765     p_vpar->mb.i_offset = i_offset;
1766
1767 #define PARSEERROR                                                          \
1768     if( p_vpar->picture.b_error )                                           \
1769     {                                                                       \
1770         /* Go to the next slice. */                                         \
1771         p_vpar->pool.pf_free_mb( &p_vpar->pool, p_mb );                     \
1772         return;                                                             \
1773     }
1774
1775 static __inline__ void ParseSlice( vpar_thread_t * p_vpar,
1776                                    u32 i_vert_code, boolean_t b_mpeg2,
1777                                    int i_coding_type, int i_structure )
1778 {
1779     int             i_offset, i_width, i_chroma_tmp;
1780     picture_t *     pp_forward_ref[2];
1781     yuv_data_t *    p_dest[3];
1782
1783     motion_t *      p_f_motion = &p_vpar->mb.f_motion;
1784     motion_t *      p_b_motion = &p_vpar->mb.b_motion;
1785
1786     /* Parse header. */
1787     LoadQuantizerScale( p_vpar );
1788
1789     if( GetBits( &p_vpar->bit_stream, 1 ) )
1790     {
1791         /* intra_slice, slice_id */
1792         RemoveBits( &p_vpar->bit_stream, 8 );
1793         /* extra_information_slice */
1794         while( GetBits( &p_vpar->bit_stream, 1 ) )
1795         {
1796             RemoveBits( &p_vpar->bit_stream, 8 );
1797         }
1798     }
1799
1800     /* Calculate the position of the macroblock. */
1801     i_width = p_vpar->sequence.i_width;
1802     i_offset = (i_vert_code - 1) * i_width * 4;
1803
1804     /* Initialize motion context. */
1805     pp_forward_ref[0] = p_vpar->sequence.p_forward;
1806
1807     if( i_structure != FRAME_STRUCTURE )
1808     {
1809         i_offset <<= 1;
1810         i_chroma_tmp =
1811             i_offset * (2 - p_vpar->sequence.b_chroma_v_subsampled)
1812              * (2 - p_vpar->sequence.b_chroma_h_subsampled)
1813               + (i_width >> p_vpar->sequence.b_chroma_h_subsampled);
1814         pp_forward_ref[1] = p_vpar->sequence.p_forward;
1815
1816         if( i_coding_type != B_CODING_TYPE && p_vpar->picture.b_second_field )
1817         {
1818             pp_forward_ref[!p_vpar->picture.b_current_field] =
1819                 p_vpar->picture.p_picture;
1820         }
1821         if( i_coding_type != I_CODING_TYPE || p_vpar->picture.b_concealment_mv )
1822         {
1823             p_f_motion->pppi_ref[1][0] =
1824                 pp_forward_ref[1]->P_Y + i_offset * 4 + i_width;
1825             p_f_motion->pppi_ref[1][1] =
1826                 pp_forward_ref[1]->P_U + i_chroma_tmp;
1827             p_f_motion->pppi_ref[1][2] =
1828                 pp_forward_ref[1]->P_V + i_chroma_tmp;
1829         }
1830         if( i_coding_type == B_CODING_TYPE )
1831         {
1832             p_b_motion->pppi_ref[1][0] =
1833                 p_vpar->sequence.p_backward->P_Y + i_offset * 4 + i_width;
1834             p_b_motion->pppi_ref[1][1] =
1835                 p_vpar->sequence.p_backward->P_U + i_chroma_tmp;
1836             p_b_motion->pppi_ref[1][2] =
1837                 p_vpar->sequence.p_backward->P_V + i_chroma_tmp;
1838         }
1839     }
1840
1841     i_chroma_tmp = i_offset
1842                         * (2 - p_vpar->sequence.b_chroma_v_subsampled)
1843                         * (2 - p_vpar->sequence.b_chroma_h_subsampled);
1844     if( i_coding_type != I_CODING_TYPE || p_vpar->picture.b_concealment_mv )
1845     {
1846         p_f_motion->pppi_ref[0][0] = pp_forward_ref[0]->P_Y + i_offset * 4;
1847         p_f_motion->pppi_ref[0][1] = pp_forward_ref[0]->P_U + i_chroma_tmp;
1848         p_f_motion->pppi_ref[0][2] = pp_forward_ref[0]->P_V + i_chroma_tmp;
1849         p_f_motion->pi_f_code[0] = p_vpar->picture.ppi_f_code[0][0];
1850         p_f_motion->pi_f_code[1] = p_vpar->picture.ppi_f_code[0][1];
1851         p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1852         p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1853     }
1854
1855     if( i_coding_type == B_CODING_TYPE )
1856     {
1857         p_b_motion->pppi_ref[0][0] = p_vpar->sequence.p_backward->P_Y
1858                                         + i_offset * 4;
1859         p_b_motion->pppi_ref[0][1] = p_vpar->sequence.p_backward->P_U
1860             + i_chroma_tmp;
1861         p_b_motion->pppi_ref[0][2] = p_vpar->sequence.p_backward->P_V
1862             + i_chroma_tmp;
1863         p_b_motion->pi_f_code[0] = p_vpar->picture.ppi_f_code[1][0];
1864         p_b_motion->pi_f_code[1] = p_vpar->picture.ppi_f_code[1][1];
1865         p_b_motion->ppi_pmv[0][0] = p_b_motion->ppi_pmv[0][1] = 0;
1866         p_b_motion->ppi_pmv[1][0] = p_b_motion->ppi_pmv[1][1] = 0;
1867     }
1868
1869     /* Initialize destination pointers. */
1870     p_dest[0] = p_vpar->picture.p_picture->P_Y + i_offset * 4;
1871     p_dest[1] = p_vpar->picture.p_picture->P_U + i_chroma_tmp;
1872     p_dest[2] = p_vpar->picture.p_picture->P_V + i_chroma_tmp;
1873
1874     if( i_structure == BOTTOM_FIELD )
1875     {
1876         p_dest[0] += i_width;
1877         p_dest[1] += i_width >> p_vpar->sequence.b_chroma_h_subsampled;
1878         p_dest[2] += i_width >> p_vpar->sequence.b_chroma_h_subsampled;
1879     }
1880     i_width = p_vpar->picture.i_field_width;
1881
1882     /* Reset intra DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1883     p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1884         = p_vpar->mb.pi_dc_dct_pred[2]
1885         = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1886
1887     p_vpar->mb.i_offset = MacroblockAddressIncrement( p_vpar ) << 4;
1888
1889     i_chroma_tmp = i_width * 4
1890                         * (2 - p_vpar->sequence.b_chroma_v_subsampled)
1891                         * (2 - p_vpar->sequence.b_chroma_h_subsampled);
1892     while( (int)(p_vpar->mb.i_offset - p_vpar->sequence.i_width) >= 0 )
1893     {
1894         /* Unusual construct at the start of some slices. Jump one line. */
1895         p_vpar->mb.i_offset -= p_vpar->sequence.i_width;
1896         p_dest[0] += i_width * 16;
1897         p_dest[1] += i_chroma_tmp;
1898         p_dest[2] += i_chroma_tmp;
1899         p_f_motion->pppi_ref[0][0] += i_width * 16;
1900         p_f_motion->pppi_ref[0][1] += i_chroma_tmp;
1901         p_f_motion->pppi_ref[0][2] += i_chroma_tmp;
1902         p_f_motion->pppi_ref[1][0] += i_width * 16;
1903         p_f_motion->pppi_ref[1][1] += i_chroma_tmp;
1904         p_f_motion->pppi_ref[1][2] += i_chroma_tmp;
1905     }
1906
1907     for( ; ; )
1908     {
1909         /* Decode macroblocks. */
1910         macroblock_t *  p_mb;
1911         int             i_mb_modes;
1912
1913         /* Get a macroblock structure. */
1914         p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
1915         p_mb->i_nb_motions = 0;
1916         p_mb->pp_dest[0] = p_dest[0]; 
1917         p_mb->pp_dest[1] = p_dest[1]; 
1918         p_mb->pp_dest[2] = p_dest[2]; 
1919
1920         /* Parse off macroblock_modes structure. */
1921         p_mb->i_mb_modes = i_mb_modes =
1922                 MacroblockModes( p_vpar, p_mb, i_coding_type, i_structure );
1923
1924         if( i_mb_modes & MB_QUANT )
1925         {
1926             LoadQuantizerScale( p_vpar );
1927         }
1928
1929         if( i_mb_modes & MB_INTRA )
1930         {
1931             if( p_vpar->picture.b_concealment_mv )
1932             {
1933                 if( i_structure == FRAME_STRUCTURE )
1934                 {
1935                     MotionFrameConceal( p_vpar, p_mb, p_f_motion );
1936                 }
1937                 else
1938                 {
1939                     MotionFieldConceal( p_vpar, p_mb, p_f_motion );
1940                 }
1941             }
1942             else
1943             {
1944                 /* Reset motion vectors predictors. */
1945                 p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1946                 p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1947                 p_b_motion->ppi_pmv[0][0] = p_b_motion->ppi_pmv[0][1] = 0;
1948                 p_b_motion->ppi_pmv[1][0] = p_b_motion->ppi_pmv[1][1] = 0;
1949             }
1950
1951             /* Decode blocks */
1952             if( b_mpeg2 )
1953             {
1954                 if( p_vpar->picture.b_intra_vlc_format )
1955                 {
1956                     MPEG2IntraB15MB( p_vpar, p_mb );
1957                 }
1958                 else
1959                 {
1960                     MPEG2IntraB14MB( p_vpar, p_mb );
1961                 }
1962             }
1963             else
1964             {
1965                 MPEG1IntraMB( p_vpar, p_mb );
1966             }
1967
1968             if( i_coding_type == D_CODING_TYPE )
1969             {
1970                 RemoveBits( &p_vpar->bit_stream, 1 );
1971             }
1972         }
1973         else
1974         {
1975             /* Non-intra block */
1976             if( !b_mpeg2 )
1977             {
1978                 if( (i_mb_modes & MOTION_TYPE_MASK) == MC_FRAME )
1979                 {
1980                     MOTION( MotionMPEG1, i_mb_modes );
1981                 }
1982                 else
1983                 {
1984                     /* Non-intra MB without forward mv in a P picture. */
1985                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1986                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1987                     MOTION( MotionFrameZero, MB_MOTION_FORWARD );
1988                 }
1989             }
1990             else if( i_structure == FRAME_STRUCTURE )
1991             {
1992                 switch( i_mb_modes & MOTION_TYPE_MASK )
1993                 {
1994                 case MC_FRAME:
1995                     MOTION( MotionFrameFrame, i_mb_modes );
1996                     break;
1997
1998                 case MC_FIELD:
1999                     MOTION( MotionFrameField, i_mb_modes );
2000                     break;
2001
2002                 case MC_DMV:
2003                     MOTION( MotionFrameDMV, MB_MOTION_FORWARD );
2004                     break;
2005
2006                 case 0:
2007                     /* Non-intra MB without forward mv in a P picture. */
2008                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2009                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2010                     MOTION( MotionFrameZero, MB_MOTION_FORWARD );
2011                 }
2012             }
2013             else
2014             {
2015                 /* Field structure. */
2016                 switch( i_mb_modes & MOTION_TYPE_MASK )
2017                 {
2018                 case MC_FIELD:
2019                     MOTION( MotionFieldField, i_mb_modes );
2020                     break;
2021
2022                 case MC_16X8:
2023                     MOTION( MotionField16x8, i_mb_modes );
2024                     break;
2025
2026                 case MC_DMV:
2027                     MOTION( MotionFieldDMV, i_mb_modes );
2028                     break;
2029
2030                 case 0:
2031                     /* Non-intra MB without forward mv in a P picture. */
2032                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2033                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2034                     MOTION( MotionFieldZero, MB_MOTION_FORWARD );
2035                 }
2036             }
2037
2038             /* ISO/IEC 13818-2 6.3.17.4 : Coded Block Pattern */
2039             if( i_mb_modes & MB_PATTERN )
2040             {
2041                 p_mb->i_coded_block_pattern = CodedPattern( p_vpar );
2042                 if( b_mpeg2 )
2043                 {
2044                     MPEG2NonIntraMB( p_vpar, p_mb );
2045                 }
2046                 else
2047                 {
2048                     MPEG1NonIntraMB( p_vpar, p_mb );
2049                 }
2050             }
2051             else
2052             {
2053                 p_mb->i_coded_block_pattern = 0;
2054             }
2055
2056             /* Reset intra DC coefficients predictors. */
2057             p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2058                 = p_vpar->mb.pi_dc_dct_pred[2]
2059                 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2060         }
2061
2062         /* End of macroblock. */
2063         PARSEERROR;
2064         p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2065
2066         /* Prepare context for the next macroblock. */
2067         p_vpar->mb.i_offset += 16;
2068         CHECK_BOUNDARIES;
2069
2070         if( ShowBits( &p_vpar->bit_stream, 1 ) )
2071         {
2072             /* Macroblock Address Increment == 1 */
2073             RemoveBits( &p_vpar->bit_stream, 1 );
2074         }
2075         else
2076         {
2077             /* Check for skipped macroblock(s). */
2078             int i_mba_inc;
2079
2080             i_mba_inc = MacroblockAddressIncrement( p_vpar );
2081             if( !i_mba_inc )
2082             {
2083                 /* End of slice. */
2084                 break;
2085             }
2086
2087             /* Reset intra DC predictors. */
2088             p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2089                 = p_vpar->mb.pi_dc_dct_pred[2]
2090                 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2091
2092             if( i_coding_type == P_CODING_TYPE )
2093             {
2094                 p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2095                 p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2096
2097                 do {
2098                     p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
2099                     p_mb->i_mb_modes = 0;
2100                     p_mb->i_nb_motions = 0;
2101                     p_mb->i_coded_block_pattern = 0;
2102                     p_mb->pp_dest[0] = p_dest[0]; 
2103                     p_mb->pp_dest[1] = p_dest[1]; 
2104                     p_mb->pp_dest[2] = p_dest[2]; 
2105
2106                     if( i_structure == FRAME_STRUCTURE )
2107                     {
2108                         MOTION( MotionFrameZero, MB_MOTION_FORWARD );
2109                     }
2110                     else
2111                     {
2112                         MOTION( MotionFieldZero, MB_MOTION_FORWARD );
2113                     }
2114
2115                     p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2116                     p_vpar->mb.i_offset += 16;
2117                     CHECK_BOUNDARIES;
2118                 } while( --i_mba_inc );
2119             }
2120             else
2121             {
2122                 do {
2123                     p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
2124                     p_mb->i_mb_modes = 0;
2125                     p_mb->i_nb_motions = 0;
2126                     p_mb->i_coded_block_pattern = 0;
2127                     p_mb->pp_dest[0] = p_dest[0]; 
2128                     p_mb->pp_dest[1] = p_dest[1]; 
2129                     p_mb->pp_dest[2] = p_dest[2]; 
2130
2131                     if( !b_mpeg2 )
2132                     {
2133                         MOTION( MotionMPEG1Reuse, i_mb_modes );
2134                     }
2135                     else if( i_structure == FRAME_STRUCTURE )
2136                     {
2137                         MOTION( MotionFrameReuse, i_mb_modes );
2138                     }
2139                     else
2140                     {
2141                         MOTION( MotionFieldReuse, i_mb_modes );
2142                     }
2143
2144                     p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2145                     p_vpar->mb.i_offset += 16;
2146                     CHECK_BOUNDARIES;
2147                 } while( --i_mba_inc );
2148             }
2149         }
2150     }
2151
2152     NextStartCode( &p_vpar->bit_stream );
2153 }
2154
2155 /*****************************************************************************
2156  * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2157  *****************************************************************************/
2158 static __inline__ void vpar_PictureData( vpar_thread_t * p_vpar,
2159                                          boolean_t b_mpeg2,
2160                                          int i_coding_type, int i_structure )
2161 {
2162     u32         i_dummy;
2163
2164     NextStartCode( &p_vpar->bit_stream );
2165     while( !p_vpar->picture.b_error && !p_vpar->p_fifo->b_die )
2166     {
2167         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2168                  < SLICE_START_CODE_MIN) ||
2169             (i_dummy > SLICE_START_CODE_MAX) )
2170         {
2171             break;
2172         }
2173         RemoveBits32( &p_vpar->bit_stream );
2174
2175         /* Decode slice data. */
2176         ParseSlice( p_vpar, i_dummy & 255, b_mpeg2, i_coding_type,
2177                     i_structure );
2178     }
2179 }
2180
2181 #define DECLARE_PICD( FUNCNAME, B_MPEG2, I_CODING_TYPE, I_STRUCTURE )       \
2182 void FUNCNAME( vpar_thread_t * p_vpar )                                     \
2183 {                                                                           \
2184     vpar_PictureData( p_vpar, B_MPEG2, I_CODING_TYPE, I_STRUCTURE );        \
2185 }
2186
2187 DECLARE_PICD( vpar_PictureDataGENERIC, p_vpar->sequence.b_mpeg2,
2188               p_vpar->picture.i_coding_type, p_vpar->picture.i_structure );
2189 #if (VPAR_OPTIM_LEVEL > 0)
2190 DECLARE_PICD( vpar_PictureData2IF, 1, I_CODING_TYPE, FRAME_STRUCTURE );
2191 DECLARE_PICD( vpar_PictureData2PF, 1, P_CODING_TYPE, FRAME_STRUCTURE );
2192 DECLARE_PICD( vpar_PictureData2BF, 1, B_CODING_TYPE, FRAME_STRUCTURE );
2193 #endif
2194 #if (VPAR_OPTIM_LEVEL > 1)
2195 DECLARE_PICD( vpar_PictureData2IT, 1, I_CODING_TYPE, TOP_FIELD );
2196 DECLARE_PICD( vpar_PictureData2PT, 1, P_CODING_TYPE, TOP_FIELD );
2197 DECLARE_PICD( vpar_PictureData2BT, 1, B_CODING_TYPE, TOP_FIELD );
2198 DECLARE_PICD( vpar_PictureData2IB, 1, I_CODING_TYPE, BOTTOM_FIELD );
2199 DECLARE_PICD( vpar_PictureData2PB, 1, P_CODING_TYPE, BOTTOM_FIELD );
2200 DECLARE_PICD( vpar_PictureData2BB, 1, B_CODING_TYPE, BOTTOM_FIELD );
2201 DECLARE_PICD( vpar_PictureData1I, 0, I_CODING_TYPE, FRAME_STRUCTURE );
2202 DECLARE_PICD( vpar_PictureData1P, 0, P_CODING_TYPE, FRAME_STRUCTURE );
2203 DECLARE_PICD( vpar_PictureData1B, 0, B_CODING_TYPE, FRAME_STRUCTURE );
2204 DECLARE_PICD( vpar_PictureData1D, 0, D_CODING_TYPE, FRAME_STRUCTURE );
2205 #endif
2206
2207 #undef DECLARE_PICD
2208