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