]> git.sesse.net Git - vlc/blob - src/video_decoder/vpar_blocks.c
* Totally rewrote the video decoder (inspired by walken's mpeg2dec), implying :
[vlc] / src / video_decoder / vpar_blocks.c
1 /*****************************************************************************
2  * vpar_blocks.c : blocks parsing
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: vpar_blocks.c,v 1.6 2001/08/22 17:21:45 massiot 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 "config.h"
34 #include "common.h"
35 #include "threads.h"
36 #include "mtime.h"
37
38 #include "intf_msg.h"
39
40 #include "stream_control.h"
41 #include "input_ext-dec.h"
42
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "vdec_ext-plugins.h"
47 #include "vpar_pool.h"
48 #include "video_parser.h"
49
50 #include "vpar_blocks.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( val > 2047 )                                                        \
175         val = 2047;                                                         \
176     else if( val < -2048 )                                                  \
177         val = -2048;
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 {
184     int         i_coeff, i_mismatch, i_code, i_pos, i_value, i_nc;
185     s32         i_sign;
186     dct_lookup_t * p_tab;
187
188     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
189     u8 *        pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
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;
308                 p_idct->i_sparse_pos = 63;
309             }
310             else
311             {
312                 p_idct->pf_idct = p_vpar->pf_idct;
313             }
314         }
315         else 
316         {
317             p_idct->pf_idct = p_vpar->pf_sparse_idct;
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;
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 {
332     int         i_coeff, i_mismatch, i_code, i_pos, i_value, i_nc;
333     s32         i_sign;
334     dct_lookup_t * p_tab;
335
336     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
337     u8 *        pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
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;
452                 p_idct->i_sparse_pos = 63;
453             }
454             else
455             {
456                 p_idct->pf_idct = p_vpar->pf_idct;
457             }
458         }
459         else 
460         {
461             p_idct->pf_idct = p_vpar->pf_sparse_idct;
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;
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 {
476     int         i_coeff, i_mismatch, i_code, i_pos, i_value, i_nc;
477     s32         i_sign;
478     dct_lookup_t * p_tab;
479
480     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
481     u8 *        pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
482     dctelem_t * p_dest = p_idct->pi_block;
483     u8 *        p_scan = p_vpar->picture.pi_scan;
484 static int meuh = 0;
485 meuh++;
486     if( meuh == 3745 )
487         i_coeff = 0;
488
489     i_coeff = -1;
490     i_mismatch = 1;
491     i_nc = 0;
492
493     if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
494     {
495         p_tab = DCT_B14DC_5 - 5 + i_code;
496         goto coeff_1;
497     }
498     else
499     {
500         goto coeff_2;
501     }
502
503     for( ; ; )
504     {
505         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
506         {
507             p_tab = DCT_B14AC_5 - 5 + i_code;
508 coeff_1:
509             i_coeff += p_tab->i_run;
510             if( i_coeff >= 64 )
511             {
512                 /* End of block */
513                 break;
514             }
515
516 store_coeff:
517             i_nc++;
518             i_pos = p_scan[ i_coeff ];
519             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
520             i_value = ((2 * p_tab->i_level + 1) * i_q_scale * pi_quant[i_pos])
521                             >> 5;
522
523             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
524             /* if (i_sign) i_value = -i_value; */
525             i_value = (i_value ^ i_sign) - i_sign;
526
527             SATURATE( i_value );
528             p_dest[i_pos] = i_value;
529             i_mismatch ^= i_value;
530
531             continue;
532         }
533 coeff_2:
534         if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
535         {
536             p_tab = DCT_B14_8 - 4 + i_code;
537             i_coeff += p_tab->i_run;
538             if( i_coeff < 64 )
539             {
540                 /* Normal coefficient */
541                 goto store_coeff;
542             }
543
544             /* Escape code */
545             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
546             if( i_coeff >= 64 )
547             {
548                 /* Illegal, but needed to avoid overflow */
549                 intf_WarnMsg( 2, "MPEG2NonIntra coeff is out of bound" );
550                 p_vpar->picture.b_error = 1;
551                 break;
552             }
553
554             i_nc++;
555             i_pos = p_scan[i_coeff];
556             i_value = 2 * (ShowSignedBits( &p_vpar->bit_stream, 1 )
557                             + GetSignedBits( &p_vpar->bit_stream, 12 )) + 1;
558
559             i_value = (i_value * i_q_scale * pi_quant[i_pos]) / 32;
560
561             SATURATE( i_value );
562             p_dest[i_pos] = i_value;
563             i_mismatch ^= i_value;
564             continue;
565         }
566         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
567         {
568             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
569             i_coeff += p_tab->i_run;
570             if( i_coeff < 64 )
571             {
572                 goto store_coeff;
573             }
574         }
575         else if( i_code >= 0x0080 )
576         {
577             p_tab = DCT_13 - 16 + (i_code >> 3);
578             i_coeff += p_tab->i_run;
579             if( i_coeff < 64 )
580             {
581                 goto store_coeff;
582             }
583         }
584         else if( i_code >= 0x0020 )
585         {
586             p_tab = DCT_15 - 16 + (i_code >> 1);
587             i_coeff += p_tab->i_run;
588             if( i_coeff < 64 )
589             {
590                 goto store_coeff;
591             }
592         }
593         else
594         {
595             p_tab = DCT_16 + i_code;
596             i_coeff += p_tab->i_run;
597             if( i_coeff < 64 )
598             {
599                 goto store_coeff;
600             }
601         }
602
603         intf_WarnMsg( 2, "MPEG2NonIntra coeff is out of bound" );
604         p_vpar->picture.b_error = 1;
605         break;
606     }
607
608     p_dest[63] ^= i_mismatch & 1;
609     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
610
611     if( i_nc <= 1 )
612     {
613         if( p_dest[63] )
614         {
615             if( i_nc == 0 )
616             {
617                 p_idct->pf_idct = p_vpar->pf_sparse_idct;
618                 p_idct->i_sparse_pos = 63;
619             }
620             else
621             {
622                 p_idct->pf_idct = p_vpar->pf_idct;
623             }
624         }
625         else 
626         {
627             p_idct->pf_idct = p_vpar->pf_sparse_idct;
628             if( i_nc == 0 )
629             {
630                 p_idct->i_sparse_pos = 0;
631             }
632             else
633             {
634                 p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
635             }
636         }
637     }
638     else
639     {
640         p_idct->pf_idct = p_vpar->pf_idct;
641     }
642 }
643
644 /*****************************************************************************
645  * MPEG1Intra : Decode an MPEG-1 intra block
646  *****************************************************************************/
647 static void MPEG1Intra( vpar_thread_t * p_vpar, idct_inner_t * p_idct )
648 {
649     int         i_coeff, i_code, i_pos, i_value, i_nc;
650     s32         i_sign;
651     dct_lookup_t * p_tab;
652
653     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
654     u8 *        pi_quant = p_vpar->sequence.intra_quant.pi_matrix;
655     dctelem_t * p_dest = p_idct->pi_block;
656     u8 *        p_scan = p_vpar->picture.pi_scan;
657
658     i_coeff = 0;
659     i_nc = (p_dest[0] != 0);
660
661     for( ; ; )
662     {
663         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
664         {
665             p_tab = DCT_B14AC_5 - 5 + i_code;
666             i_coeff += p_tab->i_run;
667             if( i_coeff >= 64 )
668             {
669                 /* End of block */
670                 break;
671             }
672
673 store_coeff:
674             i_nc++;
675             i_pos = p_scan[ i_coeff ];
676             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
677             i_value = (p_tab->i_level * i_q_scale * pi_quant[i_pos])
678                             >> 4;
679
680             /* Oddification */
681             i_value = (i_value - 1) | 1;
682
683             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
684             /* if (i_sign) i_value = -i_value; */
685             i_value = (i_value ^ i_sign) - i_sign;
686
687             SATURATE( i_value );
688             p_dest[i_pos] = i_value;
689
690             continue;
691         }
692         else if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
693         {
694             p_tab = DCT_B14_8 - 4 + i_code;
695             i_coeff += p_tab->i_run;
696             if( i_coeff < 64 )
697             {
698                 /* Normal coefficient */
699                 goto store_coeff;
700             }
701
702             /* Escape code */
703             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
704             if( i_coeff >= 64 )
705             {
706                 /* Illegal, but needed to avoid overflow */
707                 intf_WarnMsg( 2, "MPEG1Intra coeff is out of bound" );
708                 p_vpar->picture.b_error = 1;
709                 break;
710             }
711
712             i_nc++;
713             i_pos = p_scan[i_coeff];
714             
715             i_value = ShowSignedBits( &p_vpar->bit_stream, 8 );
716             if( !(i_value & 0x7F) )
717             {
718                 RemoveBits( &p_vpar->bit_stream, 8 );
719                 i_value = ShowBits( &p_vpar->bit_stream, 8 ) + 2 * i_value;
720             }
721
722             i_value = (i_value * i_q_scale * pi_quant[i_pos]) / 16;
723
724             /* Oddification */
725             i_value = (i_value + ~ShowSignedBits( &p_vpar->bit_stream, 1 )) | 1;
726
727             SATURATE( i_value );
728             p_dest[i_pos] = i_value;
729             RemoveBits( &p_vpar->bit_stream, 8 );
730             continue;
731         }
732         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
733         {
734             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
735             i_coeff += p_tab->i_run;
736             if( i_coeff < 64 )
737             {
738                 goto store_coeff;
739             }
740         }
741         else if( i_code >= 0x0080 )
742         {
743             p_tab = DCT_13 - 16 + (i_code >> 3);
744             i_coeff += p_tab->i_run;
745             if( i_coeff < 64 )
746             {
747                 goto store_coeff;
748             }
749         }
750         else if( i_code >= 0x0020 )
751         {
752             p_tab = DCT_15 - 16 + (i_code >> 1);
753             i_coeff += p_tab->i_run;
754             if( i_coeff < 64 )
755             {
756                 goto store_coeff;
757             }
758         }
759         else
760         {
761             p_tab = DCT_16 + i_code;
762             i_coeff += p_tab->i_run;
763             if( i_coeff < 64 )
764             {
765                 goto store_coeff;
766             }
767         }
768
769         intf_WarnMsg( 2, "MPEG1Intra coeff is out of bound" );
770         p_vpar->picture.b_error = 1;
771         break;
772     }
773
774     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
775
776     if( i_nc <= 1 )
777     {
778         p_idct->pf_idct = p_vpar->pf_sparse_idct;
779         p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
780     }
781     else
782     {
783         p_idct->pf_idct = p_vpar->pf_idct;
784     }
785 }
786
787 /*****************************************************************************
788  * MPEG1NonIntra : Decode a non-intra MPEG-1 block
789  *****************************************************************************/
790 static void MPEG1NonIntra( vpar_thread_t * p_vpar, idct_inner_t * p_idct )
791 {
792     int         i_coeff, i_code, i_pos, i_value, i_nc;
793     s32         i_sign;
794     dct_lookup_t * p_tab;
795
796     int         i_q_scale = p_vpar->mb.i_quantizer_scale;
797     u8 *        pi_quant = p_vpar->sequence.nonintra_quant.pi_matrix;
798     dctelem_t * p_dest = p_idct->pi_block;
799     u8 *        p_scan = p_vpar->picture.pi_scan;
800
801     i_coeff = -1;
802     i_nc = 0;
803
804     if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
805     {
806         p_tab = DCT_B14DC_5 - 5 + i_code;
807         goto coeff_1;
808     }
809     else
810     {
811         goto coeff_2;
812     }
813
814     for( ; ; )
815     {
816         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 )) >= 0x5 )
817         {
818             p_tab = DCT_B14AC_5 - 5 + i_code;
819 coeff_1:
820             i_coeff += p_tab->i_run;
821             if( i_coeff >= 64 )
822             {
823                 /* End of block */
824                 break;
825             }
826
827 store_coeff:
828             i_nc++;
829             i_pos = p_scan[ i_coeff ];
830             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
831             i_value = ((2 * p_tab->i_level + 1) * i_q_scale * pi_quant[i_pos])
832                             >> 5;
833
834             /* Oddification */
835             i_value = (i_value - 1) | 1;
836
837             i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
838             /* if (i_sign) i_value = -i_value; */
839             i_value = (i_value ^ i_sign) - i_sign;
840
841             SATURATE( i_value );
842             p_dest[i_pos] = i_value;
843
844             continue;
845         }
846 coeff_2:
847         if( (i_code = ShowBits( &p_vpar->bit_stream, 8 )) >= 0x4 )
848         {
849             p_tab = DCT_B14_8 - 4 + i_code;
850             i_coeff += p_tab->i_run;
851             if( i_coeff < 64 )
852             {
853                 /* Normal coefficient */
854                 goto store_coeff;
855             }
856
857             /* Escape code */
858             i_coeff += (GetBits( &p_vpar->bit_stream, 12 ) & 0x3F) - 64;
859             if( i_coeff >= 64 )
860             {
861                 /* Illegal, but needed to avoid overflow */
862                 intf_WarnMsg( 2, "MPEG1NonIntra coeff is out of bound" );
863                 p_vpar->picture.b_error = 1;
864                 break;
865             }
866
867             i_nc++;
868             i_pos = p_scan[i_coeff];
869             i_value = ShowSignedBits( &p_vpar->bit_stream, 8 );
870             if( !(i_value & 0x7F) )
871             {
872                 RemoveBits( &p_vpar->bit_stream, 8 );
873                 i_value = ShowBits( &p_vpar->bit_stream, 8 ) + 2 * i_value;
874             }
875             i_sign = ShowSignedBits( &p_vpar->bit_stream, 1 );
876             i_value = 2 * (i_sign + i_value) + 1;
877             i_value = (i_value * i_q_scale * pi_quant[i_pos]) / 32;
878
879             /* Oddification */
880             i_value = (i_value + ~i_sign) | 1;
881
882             SATURATE( i_value );
883             p_dest[i_pos] = i_value;
884             RemoveBits( &p_vpar->bit_stream, 8 );
885             continue;
886         }
887         else if( (i_code = ShowBits( &p_vpar->bit_stream, 16)) >= 0x0200 )
888         {
889             p_tab = DCT_B14_10 - 8 + (i_code >> 6);
890             i_coeff += p_tab->i_run;
891             if( i_coeff < 64 )
892             {
893                 goto store_coeff;
894             }
895         }
896         else if( i_code >= 0x0080 )
897         {
898             p_tab = DCT_13 - 16 + (i_code >> 3);
899             i_coeff += p_tab->i_run;
900             if( i_coeff < 64 )
901             {
902                 goto store_coeff;
903             }
904         }
905         else if( i_code >= 0x0020 )
906         {
907             p_tab = DCT_15 - 16 + (i_code >> 1);
908             i_coeff += p_tab->i_run;
909             if( i_coeff < 64 )
910             {
911                 goto store_coeff;
912             }
913         }
914         else
915         {
916             p_tab = DCT_16 + i_code;
917             i_coeff += p_tab->i_run;
918             if( i_coeff < 64 )
919             {
920                 goto store_coeff;
921             }
922         }
923
924         intf_WarnMsg( 2, "MPEG1NonIntra coeff is out of bound" );
925         p_vpar->picture.b_error = 1;
926         break;
927     }
928
929     RemoveBits( &p_vpar->bit_stream, 2 ); /* End of Block */
930
931     if( i_nc <= 1 )
932     {
933         p_idct->pf_idct = p_vpar->pf_sparse_idct;
934         if( i_nc == 0 )
935         {
936             p_idct->i_sparse_pos = 0;
937         }
938         else
939         {
940             p_idct->i_sparse_pos = i_coeff - p_tab->i_run;
941         }
942     }
943     else
944     {
945         p_idct->pf_idct = p_vpar->pf_idct;
946     }
947 }
948
949 #undef SATURATE
950
951 /*****************************************************************************
952  * *MB : decode all blocks of the macroblock
953  *****************************************************************************/
954 #define DECODE_LUMABLOCK( i_b, p_dest, PF_MBFUNC )                          \
955     p_idct = &p_mb->p_idcts[i_b];                                           \
956     memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                    \
957     p_idct->p_dct_data = p_dest;                                            \
958     p_vpar->mb.pi_dc_dct_pred[0] += GetLumaDCDiff( p_vpar );                \
959     p_idct->pi_block[0] = p_vpar->mb.pi_dc_dct_pred[0]                      \
960                          << (3 - p_vpar->picture.i_intra_dc_precision );    \
961     PF_MBFUNC( p_vpar, p_idct );
962
963 #define DECLARE_INTRAMB( PSZ_NAME, PF_MBFUNC )                              \
964 static __inline__ void PSZ_NAME( vpar_thread_t * p_vpar,                    \
965                                  macroblock_t * p_mb )                      \
966 {                                                                           \
967     int             i_dct_offset;                                           \
968     yuv_data_t *    p_lum_dest;                                             \
969     idct_inner_t *  p_idct;                                                 \
970                                                                             \
971     p_lum_dest = p_mb->pp_dest[0] + p_vpar->mb.i_offset;                    \
972                                                                             \
973     if( p_mb->i_mb_modes & DCT_TYPE_INTERLACED )                            \
974     {                                                                       \
975         i_dct_offset = p_vpar->picture.i_field_width;                       \
976         p_mb->i_lum_dct_stride = p_vpar->picture.i_field_width * 2;         \
977     }                                                                       \
978     else                                                                    \
979     {                                                                       \
980         i_dct_offset = p_vpar->picture.i_field_width * 8;                   \
981         p_mb->i_lum_dct_stride = p_vpar->picture.i_field_width;             \
982     }                                                                       \
983     p_mb->i_chrom_dct_stride = p_vpar->picture.i_field_width >> 1;          \
984                                                                             \
985     DECODE_LUMABLOCK( 0, p_lum_dest, PF_MBFUNC );                           \
986     DECODE_LUMABLOCK( 1, p_lum_dest + 8, PF_MBFUNC );                       \
987     DECODE_LUMABLOCK( 2, p_lum_dest + i_dct_offset, PF_MBFUNC );            \
988     DECODE_LUMABLOCK( 3, p_lum_dest + i_dct_offset + 8, PF_MBFUNC );        \
989                                                                             \
990     p_idct = &p_mb->p_idcts[4];                                             \
991     memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                    \
992     p_idct->p_dct_data = p_mb->pp_dest[1] + (p_vpar->mb.i_offset >> 1);     \
993     p_vpar->mb.pi_dc_dct_pred[1] += GetChromaDCDiff( p_vpar );              \
994     p_idct->pi_block[0] = p_vpar->mb.pi_dc_dct_pred[1]                      \
995                          << (3 - p_vpar->picture.i_intra_dc_precision );    \
996     PF_MBFUNC( p_vpar, p_idct );                                            \
997                                                                             \
998     p_idct = &p_mb->p_idcts[5];                                             \
999     memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                    \
1000     p_idct->p_dct_data = p_mb->pp_dest[2] + (p_vpar->mb.i_offset >> 1);     \
1001     p_vpar->mb.pi_dc_dct_pred[2] += GetChromaDCDiff( p_vpar );              \
1002     p_idct->pi_block[0] = p_vpar->mb.pi_dc_dct_pred[2]                      \
1003                          << (3 - p_vpar->picture.i_intra_dc_precision );    \
1004     PF_MBFUNC( p_vpar, p_idct );                                            \
1005 }
1006
1007 DECLARE_INTRAMB( MPEG1IntraMB, MPEG1Intra );
1008 DECLARE_INTRAMB( MPEG2IntraB14MB, MPEG2IntraB14 );
1009 DECLARE_INTRAMB( MPEG2IntraB15MB, MPEG2IntraB15 );
1010
1011 #undef DECLARE_INTRAMB
1012 #undef DECODE_LUMABLOCK
1013
1014 #define DECODE_BLOCK( i_b, p_dest, PF_MBFUNC )                              \
1015     if( p_mb->i_coded_block_pattern & (1 << (5 - i_b)) )                    \
1016     {                                                                       \
1017         p_idct = &p_mb->p_idcts[i_b];                                       \
1018         memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                \
1019         p_idct->p_dct_data = p_dest;                                        \
1020         PF_MBFUNC( p_vpar, p_idct );                                        \
1021     }
1022
1023 #define DECLARE_NONINTRAMB( PSZ_NAME, PF_MBFUNC )                           \
1024 static __inline__ void PSZ_NAME( vpar_thread_t * p_vpar,                    \
1025                                  macroblock_t * p_mb )                      \
1026 {                                                                           \
1027     int             i_dct_offset;                                           \
1028     yuv_data_t *    p_lum_dest;                                             \
1029     idct_inner_t *  p_idct;                                                 \
1030                                                                             \
1031     p_lum_dest = p_mb->pp_dest[0] + p_vpar->mb.i_offset;                    \
1032                                                                             \
1033     if( p_mb->i_mb_modes & DCT_TYPE_INTERLACED )                            \
1034     {                                                                       \
1035         i_dct_offset = p_vpar->picture.i_field_width;                       \
1036         p_mb->i_lum_dct_stride = p_vpar->picture.i_field_width * 2;         \
1037     }                                                                       \
1038     else                                                                    \
1039     {                                                                       \
1040         i_dct_offset = p_vpar->picture.i_field_width * 8;                   \
1041         p_mb->i_lum_dct_stride = p_vpar->picture.i_field_width;             \
1042     }                                                                       \
1043     p_mb->i_chrom_dct_stride = p_vpar->picture.i_field_width >> 1;          \
1044                                                                             \
1045     DECODE_BLOCK( 0, p_lum_dest, PF_MBFUNC );                               \
1046     DECODE_BLOCK( 1, p_lum_dest + 8, PF_MBFUNC );                           \
1047     DECODE_BLOCK( 2, p_lum_dest + i_dct_offset, PF_MBFUNC );                \
1048     DECODE_BLOCK( 3, p_lum_dest + i_dct_offset + 8, PF_MBFUNC );            \
1049     DECODE_BLOCK( 4, p_mb->pp_dest[1] + (p_vpar->mb.i_offset >> 1),         \
1050                   PF_MBFUNC );                                              \
1051     DECODE_BLOCK( 5, p_mb->pp_dest[2] + (p_vpar->mb.i_offset >> 1),         \
1052                   PF_MBFUNC );                                              \
1053 }
1054
1055 DECLARE_NONINTRAMB( MPEG1NonIntraMB, MPEG1NonIntra );
1056 DECLARE_NONINTRAMB( MPEG2NonIntraMB, MPEG2NonIntra );
1057
1058 #undef DECLARE_NONINTRAMB
1059 #undef DECODE_BLOCK
1060
1061
1062 /*
1063  * Motion vectors
1064  */
1065
1066 /****************************************************************************
1067  * MotionDelta : Parse the next motion delta
1068  ****************************************************************************/
1069 static __inline__ int MotionDelta( vpar_thread_t * p_vpar, int i_f_code )
1070 {
1071     int         i_delta, i_sign, i_code;
1072     lookup_t *  p_tab;
1073
1074     if( ShowBits( &p_vpar->bit_stream, 1 ) )
1075     {
1076         RemoveBits( &p_vpar->bit_stream, 1 );
1077         return 0;
1078     }
1079
1080     if( (i_code = ShowBits( &p_vpar->bit_stream, 6 )) >= 0x3 )
1081     {
1082         p_tab = MV_4 + (i_code >> 2);
1083         i_delta = (p_tab->i_value << i_f_code) + 1;
1084         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1085
1086         i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
1087         if( i_f_code )
1088         {
1089             i_delta += GetBits( &p_vpar->bit_stream, i_f_code );
1090         }
1091
1092         return (i_delta ^ i_sign) - i_sign;
1093
1094     }
1095     else
1096     {
1097         p_tab = MV_10 + ShowBits( &p_vpar->bit_stream, 10 );
1098         i_delta = (p_tab->i_value << i_f_code) + 1;
1099         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1100
1101         i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
1102         if( i_f_code )
1103         {
1104             i_delta += GetBits( &p_vpar->bit_stream, i_f_code );
1105         }
1106
1107         return (i_delta ^ i_sign) - i_sign;
1108     }
1109 }
1110
1111 /****************************************************************************
1112  * BoundMotionVector : Bound a motion_vector :-)
1113  ****************************************************************************/
1114 static __inline__ int BoundMotionVector( int i_vector, int i_f_code )
1115 {
1116     int i_limit;
1117
1118     i_limit = 16 << i_f_code;
1119
1120     if( i_vector >= i_limit )
1121     {
1122         return i_vector - 2 * i_limit;
1123     }
1124     else if( i_vector < -i_limit)
1125     {
1126         return i_vector + 2 * i_limit;
1127     }
1128     else
1129     {
1130         return i_vector;
1131     }
1132 }
1133
1134 /****************************************************************************
1135  * GetDMV : Decode a differential motion vector (Dual Prime Arithmetic)
1136  ****************************************************************************/
1137 static __inline__ int GetDMV( vpar_thread_t * p_vpar )
1138 {
1139     dmv_lookup_t * p_tab;
1140
1141     p_tab = DMV_2 + ShowBits( &p_vpar->bit_stream, 2 );
1142     RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1143     return( p_tab->i_value );
1144 }
1145
1146 /****************************************************************************
1147  * Motion* : Parse motion vectors
1148  ****************************************************************************/
1149 #define MOTION_BLOCK( b_aver, i_x, i_y, i_dest,                             \
1150                       pp_src, i_src, i_str, i_hei,                          \
1151                       b_s_half )                                            \
1152     do {                                                                    \
1153         motion_inner_t * p_m_inner = p_mb->p_motions + p_mb->i_nb_motions;  \
1154         p_m_inner->b_average = b_aver;                                      \
1155         p_m_inner->i_x_pred = i_x;                                          \
1156         p_m_inner->i_y_pred = i_y;                                          \
1157         p_m_inner->pp_source[0] = pp_src[0];                                \
1158         p_m_inner->pp_source[1] = pp_src[1];                                \
1159         p_m_inner->pp_source[2] = pp_src[2];                                \
1160         p_m_inner->i_dest_offset = i_dest;                                  \
1161         p_m_inner->i_src_offset = i_src;                                    \
1162         p_m_inner->i_stride = i_str;                                        \
1163         p_m_inner->i_height = i_hei;                                        \
1164         p_m_inner->b_second_half = b_s_half;                                \
1165         p_mb->i_nb_motions++;                                               \
1166     } while( 0 );
1167
1168 /* MPEG-1 predictions. */
1169
1170 static void MotionMPEG1( vpar_thread_t * p_vpar,
1171                                     macroblock_t * p_mb,
1172                                     motion_t * p_motion,
1173                                     boolean_t b_average )
1174 {
1175     int i_motion_x, i_motion_y;
1176     int i_offset = p_vpar->mb.i_offset;
1177     int i_width = p_vpar->picture.i_field_width;
1178     
1179     i_motion_x = p_motion->ppi_pmv[0][0]
1180                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1181     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1182     p_motion->ppi_pmv[0][0] = i_motion_x;
1183
1184     i_motion_y = p_motion->ppi_pmv[0][1]
1185                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1186     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[0] );
1187     p_motion->ppi_pmv[0][1] = i_motion_y;
1188
1189     if( p_motion->pi_f_code[1] )
1190     {
1191         i_motion_x <<= 1;
1192         i_motion_y <<= 1;
1193     }
1194
1195     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1196                   p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1197 }
1198
1199 static void MotionMPEG1Reuse( vpar_thread_t * p_vpar,
1200                                          macroblock_t * p_mb,
1201                                          motion_t * p_motion,
1202                                          boolean_t b_average )
1203 {
1204     int i_motion_x, i_motion_y;
1205     int i_offset = p_vpar->mb.i_offset;
1206     int i_width = p_vpar->picture.i_field_width;
1207
1208     i_motion_x = p_motion->ppi_pmv[0][0];
1209     i_motion_y = p_motion->ppi_pmv[0][1];
1210
1211     if( p_motion->pi_f_code[1] )
1212     {
1213         i_motion_x <<= 1;
1214         i_motion_y <<= 1;
1215     }
1216
1217      MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1218                   i_offset, p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1219 }
1220
1221 /* MPEG-2 frame predictions. */
1222
1223 static void MotionFrameFrame( vpar_thread_t * p_vpar,
1224                                          macroblock_t * p_mb,
1225                                          motion_t * p_motion,
1226                                          boolean_t b_average )
1227 {
1228     int i_motion_x, i_motion_y;
1229     int i_offset = p_vpar->mb.i_offset;
1230     int i_width = p_vpar->picture.i_field_width;
1231
1232     i_motion_x = p_motion->ppi_pmv[0][0]
1233                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1234     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1235     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1236
1237     i_motion_y = p_motion->ppi_pmv[0][1]
1238                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1239     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1240     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1241
1242     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1243                   p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1244 }
1245
1246 static void MotionFrameField( vpar_thread_t * p_vpar,
1247                                          macroblock_t * p_mb,
1248                                          motion_t * p_motion,
1249                                          boolean_t b_average )
1250 {
1251     int i_motion_x, i_motion_y, i_field_select;
1252     int i_offset = p_vpar->mb.i_offset;
1253     int i_width = p_vpar->picture.i_field_width;
1254
1255     i_field_select = GetSignedBits( &p_vpar->bit_stream, 1 );
1256
1257     i_motion_x = p_motion->ppi_pmv[0][0]
1258                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1259     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1260     p_motion->ppi_pmv[0][0] = i_motion_x;
1261
1262     i_motion_y = (p_motion->ppi_pmv[0][1] >> 1)
1263                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1264     /* I have no idea why this is commented out, since walken doesn't put
1265      * comments in its code. --Meuuh */
1266     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1267     p_motion->ppi_pmv[0][1] = i_motion_y << 1;
1268
1269     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1270                   p_motion->pppi_ref[0], i_offset + (i_field_select & i_width),
1271                   i_width * 2, 8, 0 );
1272
1273     i_field_select = GetSignedBits( &p_vpar->bit_stream, 1 );
1274
1275     i_motion_x = p_motion->ppi_pmv[1][0]
1276                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1277     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1278     p_motion->ppi_pmv[1][0] = i_motion_x;
1279
1280     i_motion_y = (p_motion->ppi_pmv[1][1] >> 1)
1281                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1282     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1283     p_motion->ppi_pmv[1][1] = i_motion_y << 1;
1284
1285     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset + i_width,
1286                   p_motion->pppi_ref[0], i_offset + (i_field_select & i_width),
1287                   i_width * 2, 8, 0 );
1288 }
1289
1290 static void MotionFrameDMV( vpar_thread_t * p_vpar,
1291                                        macroblock_t * p_mb,
1292                                        motion_t * p_motion,
1293                                        boolean_t b_average )
1294 {
1295     int i_motion_x, i_motion_y;
1296     int i_dmv_x, i_dmv_y;
1297     int i_tmp_x, i_tmp_y;
1298
1299     int i_offset = p_vpar->mb.i_offset;
1300     int i_width = p_vpar->picture.i_field_width;
1301     int m;
1302
1303     i_motion_x = p_motion->ppi_pmv[0][0]
1304                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1305     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1306     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1307
1308     i_dmv_x = GetDMV( p_vpar );
1309
1310     i_motion_y = (p_motion->ppi_pmv[0][1] >> 1)
1311                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1312     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1313     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y << 1;
1314
1315     i_dmv_y = GetDMV( p_vpar );
1316
1317     /* First field. */
1318     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset,
1319                   p_motion->pppi_ref[0], i_offset, i_width * 2, 8, 0 );
1320     m = p_vpar->picture.b_top_field_first ? 1 : 3;
1321     i_tmp_x = ((i_motion_x * m + (i_motion_x > 0)) >> 1) + i_dmv_x;
1322     i_tmp_y = ((i_motion_y * m + (i_motion_y > 0)) >> 1) + i_dmv_y - 1;
1323     MOTION_BLOCK( 1, i_tmp_x, i_tmp_y, i_offset, p_motion->pppi_ref[0],
1324                   i_offset + i_width, i_width * 2, 8, 0 );
1325
1326     /* Second field. */
1327     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset + i_width,
1328                   p_motion->pppi_ref[0], i_offset + i_width, i_width * 2, 8, 0 );
1329     m = p_vpar->picture.b_top_field_first ? 3 : 1;
1330     i_tmp_x = ((i_motion_x * m + (i_motion_x > 0)) >> 1) + i_dmv_x;
1331     i_tmp_y = ((i_motion_y * m + (i_motion_y > 0)) >> 1) + i_dmv_y + 1;
1332     MOTION_BLOCK( 1, i_tmp_x, i_tmp_y, i_offset + i_width,
1333                   p_motion->pppi_ref[0], i_offset, i_width * 2, 8, 0 );
1334 }
1335
1336 static void MotionFrameZero( vpar_thread_t * p_vpar,
1337                                         macroblock_t * p_mb,
1338                                         motion_t * p_motion,
1339                                         boolean_t b_average )
1340 {
1341     int i_offset = p_vpar->mb.i_offset;
1342     int i_width = p_vpar->picture.i_field_width;
1343
1344     MOTION_BLOCK( b_average, 0, 0, i_offset, p_motion->pppi_ref[0],
1345                   i_offset, i_width, 16, 0 );
1346 }
1347
1348 static void MotionFrameReuse( vpar_thread_t * p_vpar,
1349                                          macroblock_t * p_mb,
1350                                          motion_t * p_motion,
1351                                          boolean_t b_average )
1352 {
1353     int i_offset = p_vpar->mb.i_offset;
1354     int i_width = p_vpar->picture.i_field_width;
1355
1356     MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1357                   i_offset, p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1358 }
1359
1360 /* MPEG-2 field predictions. */
1361
1362 static void MotionFieldField( vpar_thread_t * p_vpar,
1363                                          macroblock_t * p_mb,
1364                                          motion_t * p_motion,
1365                                          boolean_t b_average )
1366 {
1367     int i_motion_x, i_motion_y;
1368     boolean_t b_field_select;
1369     int i_offset = p_vpar->mb.i_offset;
1370     int i_width = p_vpar->picture.i_field_width;
1371
1372     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1373
1374     i_motion_x = p_motion->ppi_pmv[0][0]
1375                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1376     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1377     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1378
1379     i_motion_y = p_motion->ppi_pmv[0][1]
1380                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1381     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1382     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1383
1384     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1385                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 16, 0 );
1386 }
1387
1388 static void MotionField16x8( vpar_thread_t * p_vpar,
1389                                         macroblock_t * p_mb,
1390                                         motion_t * p_motion,
1391                                         boolean_t b_average )
1392 {
1393     int i_motion_x, i_motion_y;
1394     boolean_t b_field_select;
1395     int i_offset = p_vpar->mb.i_offset;
1396     int i_width = p_vpar->picture.i_field_width;
1397
1398     /* First half. */
1399     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1400
1401     i_motion_x = p_motion->ppi_pmv[0][0]
1402                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1403     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1404     p_motion->ppi_pmv[0][0] = i_motion_x;
1405
1406     i_motion_y = p_motion->ppi_pmv[0][1]
1407                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1408     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1409     p_motion->ppi_pmv[0][1] = i_motion_y;
1410
1411     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1412                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 8, 0 );
1413
1414     /* Second half. */
1415     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1416
1417     i_motion_x = p_motion->ppi_pmv[1][0]
1418                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1419     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1420     p_motion->ppi_pmv[1][0] = i_motion_x;
1421
1422     i_motion_y = p_motion->ppi_pmv[1][1]
1423                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1424     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1425     p_motion->ppi_pmv[1][1] = i_motion_y;
1426
1427     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1428                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 8, 1 );
1429 }
1430
1431 static void MotionFieldDMV( vpar_thread_t * p_vpar,
1432                                        macroblock_t * p_mb,
1433                                        motion_t * p_motion,
1434                                        boolean_t b_average )
1435 {
1436     int i_motion_x, i_motion_y;
1437     int i_dmv_x, i_dmv_y;
1438     int i_offset = p_vpar->mb.i_offset;
1439     int i_width = p_vpar->picture.i_field_width;
1440     boolean_t b_current_field = p_vpar->picture.b_current_field;
1441
1442     i_motion_x = p_motion->ppi_pmv[0][0]
1443                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1444     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1445     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1446
1447     i_dmv_x = GetDMV( p_vpar );
1448
1449     i_motion_y = p_motion->ppi_pmv[0][1]
1450                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1451     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1452     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1453
1454     i_dmv_y = GetDMV( p_vpar );
1455
1456     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset,
1457                   p_motion->pppi_ref[b_current_field],
1458                   i_offset, i_width, 16, 0 );
1459
1460     i_motion_x = ((i_motion_x + (i_motion_x > 0)) >> 1) + i_dmv_x;
1461     i_motion_y = ((i_motion_y + (i_motion_y > 0)) >> 1) + i_dmv_y
1462                     + 2 * b_current_field - 1;
1463     MOTION_BLOCK( 1, i_motion_x, i_motion_y, i_offset,
1464                   p_motion->pppi_ref[!b_current_field],
1465                   i_offset, i_width, 16, 0 );
1466 }
1467
1468 static void MotionFieldZero( vpar_thread_t * p_vpar,
1469                                         macroblock_t * p_mb,
1470                                         motion_t * p_motion,
1471                                         boolean_t b_average )
1472 {
1473     int i_offset = p_vpar->mb.i_offset;
1474     int i_width = p_vpar->picture.i_field_width;
1475     boolean_t b_current_field = p_vpar->picture.b_current_field;
1476
1477     MOTION_BLOCK( b_average, 0, 0, i_offset, p_motion->pppi_ref[b_current_field],
1478                   i_offset, i_width, 16, 0 );
1479 }
1480
1481 static void MotionFieldReuse( vpar_thread_t * p_vpar,
1482                                          macroblock_t * p_mb,
1483                                          motion_t * p_motion,
1484                                          boolean_t b_average )
1485 {
1486     int i_offset = p_vpar->mb.i_offset;
1487     int i_width = p_vpar->picture.i_field_width;
1488     boolean_t b_current_field = p_vpar->picture.b_current_field;
1489
1490     MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1491                   i_offset, p_motion->pppi_ref[b_current_field],
1492                   i_offset, i_width, 16, 0 );
1493 }
1494
1495 /* MPEG-2 concealment motion vectors. */
1496
1497 static void MotionFrameConceal( vpar_thread_t * p_vpar,
1498                                            macroblock_t * p_mv,
1499                                            motion_t * p_motion )
1500 {
1501     int i_tmp;
1502
1503     i_tmp = p_motion->ppi_pmv[0][0]
1504                 + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1505     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[0] );
1506     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_tmp;
1507
1508     i_tmp = p_motion->ppi_pmv[0][1]
1509                 + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1510     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[1] );
1511     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_tmp;
1512
1513     /* Marker bit. */
1514     RemoveBits( &p_vpar->bit_stream, 1 );
1515 }
1516
1517 static void MotionFieldConceal( vpar_thread_t * p_vpar,
1518                                            macroblock_t * p_mv,
1519                                            motion_t * p_motion )
1520 {
1521     int i_tmp;
1522
1523     /* field_select */
1524     RemoveBits( &p_vpar->bit_stream, 1 );
1525
1526     i_tmp = p_motion->ppi_pmv[0][0]
1527                 + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1528     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[0] );
1529     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_tmp;
1530
1531     i_tmp = p_motion->ppi_pmv[0][1]
1532                 + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1533     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[1] );
1534     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_tmp;
1535
1536     /* Marker bit. */
1537     RemoveBits( &p_vpar->bit_stream, 1 );
1538 }
1539
1540
1541 /*
1542  * Macroblock information structures
1543  */
1544
1545 /*****************************************************************************
1546  * MacroblockAddressIncrement : Get the macroblock_address_increment field
1547  *****************************************************************************/
1548 static __inline__ int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1549 {
1550     lookup_t *  p_tab;
1551     int         i_code;
1552     int         i_mba = 0;
1553
1554     for( ; ; )
1555     {
1556         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 ) ) >= 0x2 )
1557         {
1558             p_tab = MBA_5 - 2 + i_code;
1559             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1560             return( i_mba + p_tab->i_value );
1561         }
1562         else if( (i_code = ShowBits( &p_vpar->bit_stream, 11 )) >= 0x18 )
1563         {
1564             p_tab = MBA_11 - 24 + i_code;
1565             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1566             return( i_mba + p_tab->i_value );
1567         }
1568         else switch( i_code )
1569         {
1570         case 8:
1571             /* Macroblock escape */
1572             i_mba += 33;
1573             /* continue... */
1574         case 15:
1575             /* Macroblock stuffing (MPEG-1 ONLY) */
1576             RemoveBits( &p_vpar->bit_stream, 11 );
1577             break;
1578
1579         default:
1580             /* End of slice, or error */
1581             return 0;
1582         }
1583     }
1584 }
1585
1586 /*****************************************************************************
1587  * CodedPattern : coded_block_pattern
1588  *****************************************************************************/
1589 static __inline__ int CodedPattern( vpar_thread_t * p_vpar )
1590 {
1591     lookup_t *  p_tab;
1592     int         i_code;
1593
1594     if( (i_code = ShowBits( &p_vpar->bit_stream, 7 )) >= 0x10 ) /* ? */
1595     {
1596         p_tab = CBP_7 - 16 + i_code;
1597         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1598         return( p_tab->i_value );
1599     }
1600     else
1601     {
1602         p_tab = CBP_9 + ShowBits( &p_vpar->bit_stream, 9 );
1603         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1604         return( p_tab->i_value );
1605     }
1606 }
1607
1608 /*****************************************************************************
1609  * MacroblockModes : Get the macroblock_modes structure
1610  *****************************************************************************/
1611 static __inline__ int MacroblockModes( vpar_thread_t * p_vpar,
1612                                        macroblock_t * p_mb,
1613                                        int i_coding_type,
1614                                        int i_structure )
1615 {
1616     int         i_mb_modes;
1617     lookup_t *  p_tab;
1618
1619     switch( i_coding_type )
1620     {
1621     case I_CODING_TYPE:
1622         p_tab = MB_I + ShowBits( &p_vpar->bit_stream, 1 );
1623         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1624         i_mb_modes = p_tab->i_value;
1625
1626         if( (i_structure == FRAME_STRUCTURE) &&
1627             (!p_vpar->picture.b_frame_pred_frame_dct) )
1628         {
1629             i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1630                                 * DCT_TYPE_INTERLACED;
1631         }
1632         return( i_mb_modes );
1633
1634     case P_CODING_TYPE:
1635         p_tab = MB_P + ShowBits( &p_vpar->bit_stream, 5 );
1636         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1637         i_mb_modes = p_tab->i_value;
1638
1639         if( i_structure != FRAME_STRUCTURE )
1640         {
1641             if( i_mb_modes & MB_MOTION_FORWARD )
1642             {
1643                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1644                                     * MOTION_TYPE_BASE;
1645             }
1646             return( i_mb_modes );
1647         }
1648         else if( p_vpar->picture.b_frame_pred_frame_dct )
1649         {
1650             if( i_mb_modes & MB_MOTION_FORWARD )
1651             {
1652                 i_mb_modes |= MC_FRAME;
1653             }
1654             return( i_mb_modes );
1655         }
1656         else
1657         {
1658             if( i_mb_modes & MB_MOTION_FORWARD )
1659             {
1660                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1661                                     * MOTION_TYPE_BASE;
1662             }
1663             if( i_mb_modes & (MB_INTRA | MB_PATTERN) )
1664             {
1665                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1666                                     * DCT_TYPE_INTERLACED;
1667             }
1668             return( i_mb_modes );
1669         }
1670
1671     case B_CODING_TYPE:
1672         p_tab = MB_B + ShowBits( &p_vpar->bit_stream, 6 );
1673         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1674         i_mb_modes = p_tab->i_value;
1675
1676         if( i_structure != FRAME_STRUCTURE )
1677         {
1678             if( !( i_mb_modes & MB_INTRA ) )
1679             {
1680                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1681                                     * MOTION_TYPE_BASE;
1682             }
1683             return( i_mb_modes );
1684         }
1685         else if( p_vpar->picture.b_frame_pred_frame_dct )
1686         {
1687             i_mb_modes |= MC_FRAME;
1688             return( i_mb_modes );
1689         }
1690         else
1691         {
1692             if( i_mb_modes & MB_INTRA )
1693             {
1694                 goto mb_intra;
1695             }
1696             i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1697                                 * MOTION_TYPE_BASE;
1698             if( i_mb_modes & (MB_INTRA | MB_PATTERN) )
1699             {
1700 mb_intra:
1701                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1702                                     * DCT_TYPE_INTERLACED;
1703             }
1704             return( i_mb_modes );
1705         }
1706
1707     case D_CODING_TYPE:
1708         RemoveBits( &p_vpar->bit_stream, 1 );
1709         return( MB_INTRA );
1710
1711     default:
1712         return( 0 );
1713     }
1714 }
1715
1716
1717 /*
1718  * Picture data parsing management
1719  */
1720
1721 /*****************************************************************************
1722  * ParseSlice : Parse the next slice structure
1723  *****************************************************************************/
1724 #define MOTION( pf_routine, i_direction )                                   \
1725     if( (i_direction) & MB_MOTION_FORWARD )                                 \
1726     {                                                                       \
1727         pf_routine( p_vpar, p_mb, &p_vpar->mb.f_motion, 0 );                \
1728         if( (i_coding_type == B_CODING_TYPE)                                \
1729                 && ((i_direction) & MB_MOTION_BACKWARD) )                   \
1730         {                                                                   \
1731             pf_routine( p_vpar, p_mb, &p_vpar->mb.b_motion, 1 );            \
1732         }                                                                   \
1733     }                                                                       \
1734     else if( (i_coding_type == B_CODING_TYPE)                               \
1735                  && ((i_direction) & MB_MOTION_BACKWARD) )                  \
1736     {                                                                       \
1737         pf_routine( p_vpar, p_mb, &p_vpar->mb.b_motion, 0 );                \
1738     }
1739
1740 #define CHECK_BOUNDARIES                                                    \
1741     i_offset = p_vpar->mb.i_offset;                                         \
1742     if( i_offset == i_width )                                               \
1743     {                                                                       \
1744         if( i_coding_type != I_CODING_TYPE ||                               \
1745             p_vpar->picture.b_concealment_mv )                              \
1746         {                                                                   \
1747             p_f_motion->pppi_ref[0][0] += 16 * i_offset;                    \
1748             p_f_motion->pppi_ref[0][1] += 4 * i_offset;                     \
1749             p_f_motion->pppi_ref[0][2] += 4 * i_offset;                     \
1750         }                                                                   \
1751         if( i_coding_type == B_CODING_TYPE )                                \
1752         {                                                                   \
1753             p_b_motion->pppi_ref[0][0] += 16 * i_offset;                    \
1754             p_b_motion->pppi_ref[0][1] += 4 * i_offset;                     \
1755             p_b_motion->pppi_ref[0][2] += 4 * i_offset;                     \
1756         }                                                                   \
1757         p_dest[0] += 16 * i_offset;                                         \
1758         p_dest[1] += 4 * i_offset;                                          \
1759         p_dest[2] += 4 * i_offset;                                          \
1760         i_offset = 0;                                                       \
1761     }                                                                       \
1762     p_vpar->mb.i_offset = i_offset;
1763
1764 #define PARSEERROR                                                          \
1765     if( p_vpar->picture.b_error )                                           \
1766     {                                                                       \
1767         /* Go to the next slice. */                                         \
1768         p_vpar->pool.pf_free_mb( &p_vpar->pool, p_mb );                     \
1769         return;                                                             \
1770     }
1771
1772 static __inline__ void ParseSlice( vpar_thread_t * p_vpar,
1773                                    u32 i_vert_code, boolean_t b_mpeg2,
1774                                    int i_coding_type, int i_structure )
1775 {
1776     int             i_offset, i_width;
1777     picture_t *     pp_forward_ref[2];
1778     yuv_data_t *    p_dest[3];
1779
1780     motion_t *      p_f_motion = &p_vpar->mb.f_motion;
1781     motion_t *      p_b_motion = &p_vpar->mb.b_motion;
1782
1783     /* Parse header. */
1784     LoadQuantizerScale( p_vpar );
1785
1786     if( GetBits( &p_vpar->bit_stream, 1 ) )
1787     {
1788         /* intra_slice, slice_id */
1789         RemoveBits( &p_vpar->bit_stream, 8 );
1790         /* extra_information_slice */
1791         while( GetBits( &p_vpar->bit_stream, 1 ) )
1792         {
1793             RemoveBits( &p_vpar->bit_stream, 8 );
1794         }
1795     }
1796
1797     /* Calculate the position of the macroblock. */
1798     i_width = p_vpar->sequence.i_width;
1799     i_offset = (i_vert_code - 1) * i_width * 4;
1800
1801     /* Initialize motion context. */
1802     pp_forward_ref[0] = p_vpar->sequence.p_forward;
1803
1804     if( i_structure != FRAME_STRUCTURE )
1805     {
1806         i_offset <<= 1;
1807         pp_forward_ref[1] = p_vpar->sequence.p_forward;
1808
1809         if( i_coding_type != B_CODING_TYPE && p_vpar->picture.b_second_field )
1810         {
1811             pp_forward_ref[!p_vpar->picture.b_current_field] =
1812                 p_vpar->picture.p_picture;
1813         }
1814         if( i_coding_type != I_CODING_TYPE || p_vpar->picture.b_concealment_mv )
1815         {
1816             p_f_motion->pppi_ref[1][0] =
1817                     pp_forward_ref[1]->p_y + i_offset * 4 + i_width;
1818             p_f_motion->pppi_ref[1][1] =
1819                     pp_forward_ref[1]->p_u + i_offset + (i_width >> 1);
1820             p_f_motion->pppi_ref[1][2] =
1821                     pp_forward_ref[1]->p_v + i_offset + (i_width >> 1);
1822         }
1823         if( i_coding_type == B_CODING_TYPE )
1824         {
1825             p_b_motion->pppi_ref[1][0] =
1826                 p_vpar->sequence.p_backward->p_y + i_offset * 4 + i_width;
1827             p_b_motion->pppi_ref[1][1] =
1828                 p_vpar->sequence.p_backward->p_u + i_offset + (i_width >> 1);
1829             p_b_motion->pppi_ref[1][2] =
1830                 p_vpar->sequence.p_backward->p_v + i_offset + (i_width >> 1);
1831         }
1832     }
1833
1834     if( i_coding_type != I_CODING_TYPE || p_vpar->picture.b_concealment_mv )
1835     {
1836         p_f_motion->pppi_ref[0][0] = pp_forward_ref[0]->p_y + i_offset * 4;
1837         p_f_motion->pppi_ref[0][1] = pp_forward_ref[0]->p_u + i_offset;
1838         p_f_motion->pppi_ref[0][2] = pp_forward_ref[0]->p_v + i_offset;
1839         p_f_motion->pi_f_code[0] = p_vpar->picture.ppi_f_code[0][0];
1840         p_f_motion->pi_f_code[1] = p_vpar->picture.ppi_f_code[0][1];
1841         p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1842         p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1843     }
1844
1845     if( i_coding_type == B_CODING_TYPE )
1846     {
1847         p_b_motion->pppi_ref[0][0] = p_vpar->sequence.p_backward->p_y
1848                                         + i_offset * 4;
1849         p_b_motion->pppi_ref[0][1] = p_vpar->sequence.p_backward->p_u
1850                                         + i_offset;
1851         p_b_motion->pppi_ref[0][2] = p_vpar->sequence.p_backward->p_v
1852                                         + i_offset;
1853         p_b_motion->pi_f_code[0] = p_vpar->picture.ppi_f_code[1][0];
1854         p_b_motion->pi_f_code[1] = p_vpar->picture.ppi_f_code[1][1];
1855         p_b_motion->ppi_pmv[0][0] = p_b_motion->ppi_pmv[0][1] = 0;
1856         p_b_motion->ppi_pmv[1][0] = p_b_motion->ppi_pmv[1][1] = 0;
1857     }
1858
1859     /* Initialize destination pointers. */
1860     p_dest[0] = p_vpar->picture.p_picture->p_y + i_offset * 4;
1861     p_dest[1] = p_vpar->picture.p_picture->p_u + i_offset;
1862     p_dest[2] = p_vpar->picture.p_picture->p_v + i_offset;
1863
1864     if( i_structure == BOTTOM_FIELD )
1865     {
1866         p_dest[0] += i_width;
1867         p_dest[1] += i_width >> 1;
1868         p_dest[2] += i_width >> 1;
1869     }
1870     i_width = p_vpar->picture.i_field_width;
1871
1872     /* Reset intra DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1873     p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1874         = p_vpar->mb.pi_dc_dct_pred[2]
1875         = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1876
1877     p_vpar->mb.i_offset = MacroblockAddressIncrement( p_vpar ) << 4;
1878
1879     for( ; ; )
1880     {
1881         /* Decode macroblocks. */
1882         macroblock_t *  p_mb;
1883         int             i_mb_modes;
1884
1885         /* Get a macroblock structure. */
1886         p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
1887         p_mb->i_nb_motions = 0;
1888         p_mb->pp_dest[0] = p_dest[0]; 
1889         p_mb->pp_dest[1] = p_dest[1]; 
1890         p_mb->pp_dest[2] = p_dest[2]; 
1891
1892         /* Parse off macroblock_modes structure. */
1893         p_mb->i_mb_modes = i_mb_modes =
1894                 MacroblockModes( p_vpar, p_mb, i_coding_type, i_structure );
1895
1896         if( i_mb_modes & MB_QUANT )
1897         {
1898             LoadQuantizerScale( p_vpar );
1899         }
1900
1901         if( i_mb_modes & MB_INTRA )
1902         {
1903             if( p_vpar->picture.b_concealment_mv )
1904             {
1905                 if( i_structure == FRAME_STRUCTURE )
1906                 {
1907                     MotionFrameConceal( p_vpar, p_mb, p_f_motion );
1908                 }
1909                 else
1910                 {
1911                     MotionFieldConceal( p_vpar, p_mb, p_f_motion );
1912                 }
1913             }
1914             else
1915             {
1916                 /* Reset motion vectors predictors. */
1917                 p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1918                 p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1919                 p_b_motion->ppi_pmv[0][0] = p_b_motion->ppi_pmv[0][1] = 0;
1920                 p_b_motion->ppi_pmv[1][0] = p_b_motion->ppi_pmv[1][1] = 0;
1921             }
1922
1923             /* Decode blocks */
1924             p_mb->i_coded_block_pattern = (1 << 6) - 1;
1925             if( b_mpeg2 )
1926             {
1927                 if( p_vpar->picture.b_intra_vlc_format )
1928                 {
1929                     MPEG2IntraB15MB( p_vpar, p_mb );
1930                 }
1931                 else
1932                 {
1933                     MPEG2IntraB14MB( p_vpar, p_mb );
1934                 }
1935             }
1936             else
1937             {
1938                 MPEG1IntraMB( p_vpar, p_mb );
1939             }
1940
1941             if( i_coding_type == D_CODING_TYPE )
1942             {
1943                 RemoveBits( &p_vpar->bit_stream, 1 );
1944             }
1945         }
1946         else
1947         {
1948             /* Non-intra block */
1949             if( !b_mpeg2 )
1950             {
1951                 if( (i_mb_modes & MOTION_TYPE_MASK) == MC_FRAME )
1952                 {
1953                     MOTION( MotionMPEG1, i_mb_modes );
1954                 }
1955                 else
1956                 {
1957                     /* Non-intra MB without forward mv in a P picture. */
1958                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1959                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1960                     MOTION( MotionFrameZero, MB_MOTION_FORWARD );
1961                 }
1962             }
1963             else if( i_structure == FRAME_STRUCTURE )
1964             {
1965                 switch( i_mb_modes & MOTION_TYPE_MASK )
1966                 {
1967                 case MC_FRAME:
1968                     MOTION( MotionFrameFrame, i_mb_modes );
1969                     break;
1970
1971                 case MC_FIELD:
1972                     MOTION( MotionFrameField, i_mb_modes );
1973                     break;
1974
1975                 case MC_DMV:
1976                     MOTION( MotionFrameDMV, MB_MOTION_FORWARD );
1977                     break;
1978
1979                 case 0:
1980                     /* Non-intra MB without forward mv in a P picture. */
1981                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1982                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1983                     MOTION( MotionFrameZero, MB_MOTION_FORWARD );
1984                 }
1985             }
1986             else
1987             {
1988                 /* Field structure. */
1989                 switch( i_mb_modes & MOTION_TYPE_MASK )
1990                 {
1991                 case MC_FIELD:
1992                     MOTION( MotionFieldField, i_mb_modes );
1993                     break;
1994
1995                 case MC_16X8:
1996                     MOTION( MotionField16x8, i_mb_modes );
1997                     break;
1998
1999                 case MC_DMV:
2000                     MOTION( MotionFieldDMV, i_mb_modes );
2001                     break;
2002
2003                 case 0:
2004                     /* Non-intra MB without forward mv in a P picture. */
2005                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2006                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2007                     MOTION( MotionFieldZero, MB_MOTION_FORWARD );
2008
2009                 }
2010             }
2011
2012             /* ISO/IEC 13818-2 6.3.17.4 : Coded Block Pattern */
2013             if( i_mb_modes & MB_PATTERN )
2014             {
2015                 p_mb->i_coded_block_pattern = CodedPattern( p_vpar );
2016                 if( b_mpeg2 )
2017                 {
2018                     MPEG2NonIntraMB( p_vpar, p_mb );
2019                 }
2020                 else
2021                 {
2022                     MPEG1NonIntraMB( p_vpar, p_mb );
2023                 }
2024             }
2025             else
2026             {
2027                 p_mb->i_coded_block_pattern = 0;
2028             }
2029
2030             /* Reset intra DC coefficients predictors. */
2031             p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2032                 = p_vpar->mb.pi_dc_dct_pred[2]
2033                 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2034         }
2035
2036         /* End of macroblock. */
2037         PARSEERROR;
2038         p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2039
2040         /* Prepare context for the next macroblock. */
2041         p_vpar->mb.i_offset += 16;
2042         CHECK_BOUNDARIES;
2043
2044         if( ShowBits( &p_vpar->bit_stream, 1 ) )
2045         {
2046             /* Macroblock Address Increment == 1 */
2047             RemoveBits( &p_vpar->bit_stream, 1 );
2048         }
2049         else
2050         {
2051             /* Check for skipped macroblock(s). */
2052             int i_mba_inc;
2053
2054             i_mba_inc = MacroblockAddressIncrement( p_vpar );
2055             if( !i_mba_inc )
2056             {
2057                 /* End of slice. */
2058                 break;
2059             }
2060
2061             /* Reset intra DC predictors. */
2062             p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2063                 = p_vpar->mb.pi_dc_dct_pred[2]
2064                 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2065
2066             if( i_coding_type == P_CODING_TYPE )
2067             {
2068                 p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2069                 p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2070
2071                 do {
2072                     p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
2073                     p_mb->i_mb_modes = 0;
2074                     p_mb->i_nb_motions = 0;
2075                     p_mb->i_coded_block_pattern = 0;
2076                     p_mb->pp_dest[0] = p_dest[0]; 
2077                     p_mb->pp_dest[1] = p_dest[1]; 
2078                     p_mb->pp_dest[2] = p_dest[2]; 
2079
2080                     if( i_structure == FRAME_STRUCTURE )
2081                     {
2082                         MOTION( MotionFrameZero, MB_MOTION_FORWARD );
2083                     }
2084                     else
2085                     {
2086                         MOTION( MotionFieldZero, MB_MOTION_FORWARD );
2087                     }
2088
2089                     p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2090                     p_vpar->mb.i_offset += 16;
2091                     CHECK_BOUNDARIES;
2092                 } while( --i_mba_inc );
2093             }
2094             else
2095             {
2096                 do {
2097                     p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
2098                     p_mb->i_mb_modes = 0;
2099                     p_mb->i_nb_motions = 0;
2100                     p_mb->i_coded_block_pattern = 0;
2101                     p_mb->pp_dest[0] = p_dest[0]; 
2102                     p_mb->pp_dest[1] = p_dest[1]; 
2103                     p_mb->pp_dest[2] = p_dest[2]; 
2104
2105                     if( !b_mpeg2 )
2106                     {
2107                         MOTION( MotionMPEG1Reuse, i_mb_modes );
2108                     }
2109                     else if( i_structure == FRAME_STRUCTURE )
2110                     {
2111                         MOTION( MotionFrameReuse, i_mb_modes );
2112                     }
2113                     else
2114                     {
2115                         MOTION( MotionFieldReuse, i_mb_modes );
2116                     }
2117
2118                     p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2119                     p_vpar->mb.i_offset += 16;
2120                     CHECK_BOUNDARIES;
2121                 } while( --i_mba_inc );
2122             }
2123         }
2124     }
2125
2126     NextStartCode( &p_vpar->bit_stream );
2127 }
2128
2129 /*****************************************************************************
2130  * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2131  *****************************************************************************/
2132 static __inline__ void vpar_PictureData( vpar_thread_t * p_vpar,
2133                                          boolean_t b_mpeg2,
2134                                          int i_coding_type, int i_structure )
2135 {
2136     u32         i_dummy;
2137
2138     NextStartCode( &p_vpar->bit_stream );
2139     while( !p_vpar->picture.b_error && !p_vpar->p_fifo->b_die )
2140     {
2141         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2142                  < SLICE_START_CODE_MIN) ||
2143             (i_dummy > SLICE_START_CODE_MAX) )
2144         {
2145             break;
2146         }
2147         RemoveBits32( &p_vpar->bit_stream );
2148
2149         /* Decode slice data. */
2150         ParseSlice( p_vpar, i_dummy & 255, b_mpeg2, i_coding_type,
2151                     i_structure );
2152     }
2153 }
2154
2155 #define DECLARE_PICD( FUNCNAME, B_MPEG2, I_CODING_TYPE, I_STRUCTURE )       \
2156 void FUNCNAME( vpar_thread_t * p_vpar )                                     \
2157 {                                                                           \
2158     vpar_PictureData( p_vpar, B_MPEG2, I_CODING_TYPE, I_STRUCTURE );        \
2159 }
2160
2161 DECLARE_PICD( vpar_PictureDataGENERIC, p_vpar->sequence.b_mpeg2,
2162               p_vpar->picture.i_coding_type, p_vpar->picture.i_structure );
2163 #if (VPAR_OPTIM_LEVEL > 0)
2164 DECLARE_PICD( vpar_PictureData1I, 0, I_CODING_TYPE, FRAME_STRUCTURE );
2165 DECLARE_PICD( vpar_PictureData1P, 0, P_CODING_TYPE, FRAME_STRUCTURE );
2166 DECLARE_PICD( vpar_PictureData1B, 0, B_CODING_TYPE, FRAME_STRUCTURE );
2167 DECLARE_PICD( vpar_PictureData1D, 0, D_CODING_TYPE, FRAME_STRUCTURE );
2168 DECLARE_PICD( vpar_PictureData2IF, 1, I_CODING_TYPE, FRAME_STRUCTURE );
2169 DECLARE_PICD( vpar_PictureData2PF, 1, P_CODING_TYPE, FRAME_STRUCTURE );
2170 DECLARE_PICD( vpar_PictureData2BF, 1, B_CODING_TYPE, FRAME_STRUCTURE );
2171 #endif
2172 #if (VPAR_OPTIM_LEVEL > 1)
2173 DECLARE_PICD( vpar_PictureData2IT, 1, I_CODING_TYPE, TOP_FIELD );
2174 DECLARE_PICD( vpar_PictureData2PT, 1, P_CODING_TYPE, TOP_FIELD );
2175 DECLARE_PICD( vpar_PictureData2BT, 1, B_CODING_TYPE, TOP_FIELD );
2176 DECLARE_PICD( vpar_PictureData2IB, 1, I_CODING_TYPE, BOTTOM_FIELD );
2177 DECLARE_PICD( vpar_PictureData2PB, 1, P_CODING_TYPE, BOTTOM_FIELD );
2178 DECLARE_PICD( vpar_PictureData2BB, 1, B_CODING_TYPE, BOTTOM_FIELD );
2179 #endif
2180
2181 #undef DECLARE_PICD
2182