]> git.sesse.net Git - vlc/blob - plugins/mpeg_vdec/vpar_blocks.c
d845233f854b53d7be7a420cea2dfbe50d9df671
[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.9 2002/05/18 17:47:47 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, "vpar warning: 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, "vpar warning: 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, "vpar warning: 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, "vpar warning: 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, "vpar warning: 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, "vpar warning: 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, macroblock_t * p_mb )  \
962 {                                                                           \
963     idct_inner_t *  p_idct;                                                 \
964     int             i_b = 4;                                                \
965                                                                             \
966     p_mb->p_y_data = p_mb->pp_dest[0] + p_vpar->mb.i_offset;                \
967     p_mb->p_u_data = p_mb->pp_dest[1] + (p_vpar->mb.i_offset                \
968                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
969     p_mb->p_v_data = p_mb->pp_dest[2] + (p_vpar->mb.i_offset                \
970                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
971                                                                             \
972     DECODE_LUMABLOCK( 0, PF_MBFUNC );                                       \
973     DECODE_LUMABLOCK( 1, PF_MBFUNC );                                       \
974     DECODE_LUMABLOCK( 2, PF_MBFUNC );                                       \
975     DECODE_LUMABLOCK( 3, PF_MBFUNC );                                       \
976                                                                             \
977     do                                                                      \
978     {                                                                       \
979         DECODE_CHROMABLOCK( i_b, PF_MBFUNC, 1 );                            \
980         DECODE_CHROMABLOCK( i_b + 1, PF_MBFUNC, 2 );                        \
981         i_b += 2;                                                           \
982     }                                                                       \
983     while( i_b < 4 + p_vpar->sequence.i_chroma_nb_blocks );                 \
984 }
985
986 DECLARE_INTRAMB( MPEG1IntraMB, MPEG1Intra );
987 DECLARE_INTRAMB( MPEG2IntraB14MB, MPEG2IntraB14 );
988 DECLARE_INTRAMB( MPEG2IntraB15MB, MPEG2IntraB15 );
989
990 #undef DECLARE_INTRAMB
991 #undef DECODE_LUMABLOCK
992 #undef DECODE_CHROMABLOCK
993
994 #define DECODE_LUMABLOCK( I_B, PF_MBFUNC )                                  \
995     if( p_mb->i_coded_block_pattern & (1 << (11 - (I_B))) )                 \
996     {                                                                       \
997         p_idct = &p_mb->p_idcts[I_B];                                       \
998         memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                \
999         PF_MBFUNC( p_vpar, p_idct,                                          \
1000                    p_vpar->sequence.nonintra_quant.pi_matrix );             \
1001     }
1002
1003 #define DECODE_CHROMABLOCK( I_B, PF_MBFUNC )                                \
1004     if( p_mb->i_coded_block_pattern & (1 << (11 - (I_B))) )                 \
1005     {                                                                       \
1006         p_idct = &p_mb->p_idcts[I_B];                                       \
1007         memset( p_idct->pi_block, 0, 64*sizeof(dctelem_t) );                \
1008         PF_MBFUNC( p_vpar, p_idct,                                          \
1009                    p_vpar->sequence.chroma_nonintra_quant.pi_matrix );      \
1010     }
1011
1012 #define DECLARE_NONINTRAMB( PSZ_NAME, PF_MBFUNC )                           \
1013 static inline void PSZ_NAME( vpar_thread_t * p_vpar, macroblock_t * p_mb )  \
1014 {                                                                           \
1015     idct_inner_t *  p_idct;                                                 \
1016     int             i_b = 4;                                                \
1017                                                                             \
1018     p_mb->p_y_data = p_mb->pp_dest[0] + p_vpar->mb.i_offset;                \
1019     p_mb->p_u_data = p_mb->pp_dest[1] + (p_vpar->mb.i_offset                \
1020                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
1021     p_mb->p_v_data = p_mb->pp_dest[2] + (p_vpar->mb.i_offset                \
1022                             >> p_vpar->sequence.b_chroma_h_subsampled);     \
1023                                                                             \
1024     DECODE_LUMABLOCK( 0, PF_MBFUNC );                                       \
1025     DECODE_LUMABLOCK( 1, PF_MBFUNC );                                       \
1026     DECODE_LUMABLOCK( 2, PF_MBFUNC );                                       \
1027     DECODE_LUMABLOCK( 3, PF_MBFUNC );                                       \
1028                                                                             \
1029     do                                                                      \
1030     {                                                                       \
1031         DECODE_CHROMABLOCK( i_b, PF_MBFUNC );                               \
1032         DECODE_CHROMABLOCK( i_b + 1, PF_MBFUNC );                           \
1033         i_b += 2;                                                           \
1034     }                                                                       \
1035     while( i_b < 4 + p_vpar->sequence.i_chroma_nb_blocks );                 \
1036 }
1037
1038 DECLARE_NONINTRAMB( MPEG1NonIntraMB, MPEG1NonIntra );
1039 DECLARE_NONINTRAMB( MPEG2NonIntraMB, MPEG2NonIntra );
1040
1041 #undef DECLARE_NONINTRAMB
1042 #undef DECODE_LUMABLOCK
1043 #undef DECODE_CHROMABLOCK
1044
1045
1046 /*
1047  * Motion vectors
1048  */
1049
1050 /****************************************************************************
1051  * MotionDelta : Parse the next motion delta
1052  ****************************************************************************/
1053 static inline int MotionDelta( vpar_thread_t * p_vpar, int i_f_code )
1054 {
1055     int         i_delta, i_sign, i_code;
1056     lookup_t *  p_tab;
1057
1058     if( ShowBits( &p_vpar->bit_stream, 1 ) )
1059     {
1060         RemoveBits( &p_vpar->bit_stream, 1 );
1061         return 0;
1062     }
1063
1064     if( (i_code = ShowBits( &p_vpar->bit_stream, 6 )) >= 0x3 )
1065     {
1066         p_tab = MV_4 + (i_code >> 2);
1067         i_delta = (p_tab->i_value << i_f_code) + 1;
1068         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1069
1070         i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
1071         if( i_f_code )
1072         {
1073             i_delta += GetBits( &p_vpar->bit_stream, i_f_code );
1074         }
1075
1076         return (i_delta ^ i_sign) - i_sign;
1077
1078     }
1079     else
1080     {
1081         p_tab = MV_10 + ShowBits( &p_vpar->bit_stream, 10 );
1082         i_delta = (p_tab->i_value << i_f_code) + 1;
1083         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1084
1085         i_sign = GetSignedBits( &p_vpar->bit_stream, 1 );
1086         if( i_f_code )
1087         {
1088             i_delta += GetBits( &p_vpar->bit_stream, i_f_code );
1089         }
1090
1091         return (i_delta ^ i_sign) - i_sign;
1092     }
1093 }
1094
1095 /****************************************************************************
1096  * BoundMotionVector : Bound a motion_vector :-)
1097  ****************************************************************************/
1098 static inline int BoundMotionVector( int i_vector, int i_f_code )
1099 {
1100     int i_limit;
1101
1102     i_limit = 16 << i_f_code;
1103
1104     if( i_vector >= i_limit )
1105     {
1106         return i_vector - 2 * i_limit;
1107     }
1108     else if( i_vector < -i_limit)
1109     {
1110         return i_vector + 2 * i_limit;
1111     }
1112     else
1113     {
1114         return i_vector;
1115     }
1116 }
1117
1118 /****************************************************************************
1119  * GetDMV : Decode a differential motion vector (Dual Prime Arithmetic)
1120  ****************************************************************************/
1121 static inline int GetDMV( vpar_thread_t * p_vpar )
1122 {
1123     dmv_lookup_t * p_tab;
1124
1125     p_tab = DMV_2 + ShowBits( &p_vpar->bit_stream, 2 );
1126     RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1127     return( p_tab->i_value );
1128 }
1129
1130 /****************************************************************************
1131  * Motion* : Parse motion vectors
1132  ****************************************************************************/
1133 #define MOTION_BLOCK( b_aver, i_x, i_y, i_dest,                             \
1134                       pp_src, i_src, i_str, i_hei,                          \
1135                       b_s_half )                                            \
1136     do {                                                                    \
1137         motion_inner_t * p_m_inner = p_mb->p_motions + p_mb->i_nb_motions;  \
1138         p_m_inner->b_average = b_aver;                                      \
1139         p_m_inner->i_x_pred = i_x;                                          \
1140         p_m_inner->i_y_pred = i_y;                                          \
1141         p_m_inner->pp_source[0] = pp_src[0];                                \
1142         p_m_inner->pp_source[1] = pp_src[1];                                \
1143         p_m_inner->pp_source[2] = pp_src[2];                                \
1144         p_m_inner->i_dest_offset = i_dest;                                  \
1145         p_m_inner->i_src_offset = i_src;                                    \
1146         p_m_inner->i_stride = i_str;                                        \
1147         p_m_inner->i_height = i_hei;                                        \
1148         p_m_inner->b_second_half = b_s_half;                                \
1149         p_mb->i_nb_motions++;                                               \
1150     } while( 0 );
1151
1152 /* MPEG-1 predictions. */
1153
1154 static void MotionMPEG1( vpar_thread_t * p_vpar,
1155                                     macroblock_t * p_mb,
1156                                     motion_t * p_motion,
1157                                     boolean_t b_average )
1158 {
1159     int i_motion_x, i_motion_y;
1160     int i_offset = p_vpar->mb.i_offset;
1161     int i_width = p_vpar->picture.i_lum_stride;
1162
1163     i_motion_x = p_motion->ppi_pmv[0][0]
1164                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1165     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1166     p_motion->ppi_pmv[0][0] = i_motion_x;
1167
1168     i_motion_y = p_motion->ppi_pmv[0][1]
1169                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1170     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[0] );
1171     p_motion->ppi_pmv[0][1] = i_motion_y;
1172
1173     if( p_motion->pi_f_code[1] )
1174     {
1175         i_motion_x <<= 1;
1176         i_motion_y <<= 1;
1177     }
1178
1179     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1180                   p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1181 }
1182
1183 static void MotionMPEG1Reuse( vpar_thread_t * p_vpar,
1184                                          macroblock_t * p_mb,
1185                                          motion_t * p_motion,
1186                                          boolean_t b_average )
1187 {
1188     int i_motion_x, i_motion_y;
1189     int i_offset = p_vpar->mb.i_offset;
1190     int i_width = p_vpar->picture.i_lum_stride;
1191
1192     i_motion_x = p_motion->ppi_pmv[0][0];
1193     i_motion_y = p_motion->ppi_pmv[0][1];
1194
1195     if( p_motion->pi_f_code[1] )
1196     {
1197         i_motion_x <<= 1;
1198         i_motion_y <<= 1;
1199     }
1200
1201      MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1202                   i_offset, p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1203 }
1204
1205 /* MPEG-2 frame predictions. */
1206
1207 static void MotionFrameFrame( vpar_thread_t * p_vpar,
1208                                          macroblock_t * p_mb,
1209                                          motion_t * p_motion,
1210                                          boolean_t b_average )
1211 {
1212     int i_motion_x, i_motion_y;
1213     int i_offset = p_vpar->mb.i_offset;
1214     int i_width = p_vpar->picture.i_lum_stride;
1215
1216     i_motion_x = p_motion->ppi_pmv[0][0]
1217                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1218     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1219     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1220
1221     i_motion_y = p_motion->ppi_pmv[0][1]
1222                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1223     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1224     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1225
1226     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1227                   p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1228 }
1229
1230 static void MotionFrameField( vpar_thread_t * p_vpar,
1231                                          macroblock_t * p_mb,
1232                                          motion_t * p_motion,
1233                                          boolean_t b_average )
1234 {
1235     int i_motion_x, i_motion_y, i_field_select;
1236     int i_offset = p_vpar->mb.i_offset;
1237     int i_width = p_vpar->picture.i_lum_stride;
1238
1239     i_field_select = GetSignedBits( &p_vpar->bit_stream, 1 );
1240
1241     i_motion_x = p_motion->ppi_pmv[0][0]
1242                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1243     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1244     p_motion->ppi_pmv[0][0] = i_motion_x;
1245
1246     i_motion_y = (p_motion->ppi_pmv[0][1] >> 1)
1247                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1248     /* According to ISO/IEC 13818-2 section 7.6.3.2, the vertical motion
1249      * vector is restricted to a range that implies it doesn't need to
1250      * be bound. */
1251     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1252     p_motion->ppi_pmv[0][1] = i_motion_y << 1;
1253
1254     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1255                   p_motion->pppi_ref[0], i_offset + (i_field_select & i_width),
1256                   i_width * 2, 8, 0 );
1257
1258     i_field_select = GetSignedBits( &p_vpar->bit_stream, 1 );
1259
1260     i_motion_x = p_motion->ppi_pmv[1][0]
1261                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1262     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1263     p_motion->ppi_pmv[1][0] = i_motion_x;
1264
1265     i_motion_y = (p_motion->ppi_pmv[1][1] >> 1)
1266                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1267     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1268     p_motion->ppi_pmv[1][1] = i_motion_y << 1;
1269
1270     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset + i_width,
1271                   p_motion->pppi_ref[0], i_offset + (i_field_select & i_width),
1272                   i_width * 2, 8, 0 );
1273 }
1274
1275 static void MotionFrameDMV( vpar_thread_t * p_vpar,
1276                                        macroblock_t * p_mb,
1277                                        motion_t * p_motion,
1278                                        boolean_t b_average )
1279 {
1280     int i_motion_x, i_motion_y;
1281     int i_dmv_x, i_dmv_y;
1282     int i_tmp_x, i_tmp_y;
1283
1284     int i_offset = p_vpar->mb.i_offset;
1285     int i_width = p_vpar->picture.i_lum_stride;
1286     int m;
1287
1288     i_motion_x = p_motion->ppi_pmv[0][0]
1289                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1290     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1291     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1292
1293     i_dmv_x = GetDMV( p_vpar );
1294
1295     i_motion_y = (p_motion->ppi_pmv[0][1] >> 1)
1296                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1297     /* i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] ); */
1298     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y << 1;
1299
1300     i_dmv_y = GetDMV( p_vpar );
1301
1302     /* First field. */
1303     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset,
1304                   p_motion->pppi_ref[0], i_offset, i_width * 2, 8, 0 );
1305     m = p_vpar->picture.b_top_field_first ? 1 : 3;
1306     i_tmp_x = ((i_motion_x * m + (i_motion_x > 0)) >> 1) + i_dmv_x;
1307     i_tmp_y = ((i_motion_y * m + (i_motion_y > 0)) >> 1) + i_dmv_y - 1;
1308     MOTION_BLOCK( 1, i_tmp_x, i_tmp_y, i_offset, p_motion->pppi_ref[0],
1309                   i_offset + i_width, i_width * 2, 8, 0 );
1310
1311     /* Second field. */
1312     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset + i_width,
1313                   p_motion->pppi_ref[0], i_offset + i_width, i_width * 2, 8, 0 );
1314     m = p_vpar->picture.b_top_field_first ? 3 : 1;
1315     i_tmp_x = ((i_motion_x * m + (i_motion_x > 0)) >> 1) + i_dmv_x;
1316     i_tmp_y = ((i_motion_y * m + (i_motion_y > 0)) >> 1) + i_dmv_y + 1;
1317     MOTION_BLOCK( 1, i_tmp_x, i_tmp_y, i_offset + i_width,
1318                   p_motion->pppi_ref[0], i_offset, i_width * 2, 8, 0 );
1319 }
1320
1321 static void MotionFrameZero( vpar_thread_t * p_vpar,
1322                                         macroblock_t * p_mb,
1323                                         motion_t * p_motion,
1324                                         boolean_t b_average )
1325 {
1326     int i_offset = p_vpar->mb.i_offset;
1327     int i_width = p_vpar->picture.i_lum_stride;
1328
1329     MOTION_BLOCK( b_average, 0, 0, i_offset, p_motion->pppi_ref[0],
1330                   i_offset, i_width, 16, 0 );
1331 }
1332
1333 static void MotionFrameReuse( vpar_thread_t * p_vpar,
1334                                          macroblock_t * p_mb,
1335                                          motion_t * p_motion,
1336                                          boolean_t b_average )
1337 {
1338     int i_offset = p_vpar->mb.i_offset;
1339     int i_width = p_vpar->picture.i_lum_stride;
1340
1341     MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1342                   i_offset, p_motion->pppi_ref[0], i_offset, i_width, 16, 0 );
1343 }
1344
1345 /* MPEG-2 field predictions. */
1346
1347 static void MotionFieldField( vpar_thread_t * p_vpar,
1348                                          macroblock_t * p_mb,
1349                                          motion_t * p_motion,
1350                                          boolean_t b_average )
1351 {
1352     int i_motion_x, i_motion_y;
1353     boolean_t b_field_select;
1354     int i_offset = p_vpar->mb.i_offset;
1355     int i_width = p_vpar->picture.i_lum_stride;
1356
1357     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1358
1359     i_motion_x = p_motion->ppi_pmv[0][0]
1360                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1361     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1362     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1363
1364     i_motion_y = p_motion->ppi_pmv[0][1]
1365                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1366     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1367     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1368
1369     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1370                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 16, 0 );
1371 }
1372
1373 static void MotionField16x8( vpar_thread_t * p_vpar,
1374                                         macroblock_t * p_mb,
1375                                         motion_t * p_motion,
1376                                         boolean_t b_average )
1377 {
1378     int i_motion_x, i_motion_y;
1379     boolean_t b_field_select;
1380     int i_offset = p_vpar->mb.i_offset;
1381     int i_width = p_vpar->picture.i_lum_stride;
1382
1383     /* First half. */
1384     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1385
1386     i_motion_x = p_motion->ppi_pmv[0][0]
1387                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1388     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1389     p_motion->ppi_pmv[0][0] = i_motion_x;
1390
1391     i_motion_y = p_motion->ppi_pmv[0][1]
1392                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1393     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1394     p_motion->ppi_pmv[0][1] = i_motion_y;
1395
1396     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1397                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 8, 0 );
1398
1399     /* Second half. */
1400     b_field_select = GetBits( &p_vpar->bit_stream, 1 );
1401
1402     i_motion_x = p_motion->ppi_pmv[1][0]
1403                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1404     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1405     p_motion->ppi_pmv[1][0] = i_motion_x;
1406
1407     i_motion_y = p_motion->ppi_pmv[1][1]
1408                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1409     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1410     p_motion->ppi_pmv[1][1] = i_motion_y;
1411
1412     MOTION_BLOCK( b_average, i_motion_x, i_motion_y, i_offset,
1413                   p_motion->pppi_ref[b_field_select], i_offset, i_width, 8, 1 );
1414 }
1415
1416 static void MotionFieldDMV( vpar_thread_t * p_vpar,
1417                                        macroblock_t * p_mb,
1418                                        motion_t * p_motion,
1419                                        boolean_t b_average )
1420 {
1421     int i_motion_x, i_motion_y;
1422     int i_dmv_x, i_dmv_y;
1423     int i_offset = p_vpar->mb.i_offset;
1424     int i_width = p_vpar->picture.i_lum_stride;
1425     boolean_t b_current_field = p_vpar->picture.b_current_field;
1426
1427     i_motion_x = p_motion->ppi_pmv[0][0]
1428                         + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1429     i_motion_x = BoundMotionVector( i_motion_x, p_motion->pi_f_code[0] );
1430     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_motion_x;
1431
1432     i_dmv_x = GetDMV( p_vpar );
1433
1434     i_motion_y = p_motion->ppi_pmv[0][1]
1435                         + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1436     i_motion_y = BoundMotionVector( i_motion_y, p_motion->pi_f_code[1] );
1437     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_motion_y;
1438
1439     i_dmv_y = GetDMV( p_vpar );
1440
1441     MOTION_BLOCK( 0, i_motion_x, i_motion_y, i_offset,
1442                   p_motion->pppi_ref[b_current_field],
1443                   i_offset, i_width, 16, 0 );
1444
1445     i_motion_x = ((i_motion_x + (i_motion_x > 0)) >> 1) + i_dmv_x;
1446     i_motion_y = ((i_motion_y + (i_motion_y > 0)) >> 1) + i_dmv_y
1447                     + 2 * b_current_field - 1;
1448     MOTION_BLOCK( 1, i_motion_x, i_motion_y, i_offset,
1449                   p_motion->pppi_ref[!b_current_field],
1450                   i_offset, i_width, 16, 0 );
1451 }
1452
1453 static void MotionFieldZero( vpar_thread_t * p_vpar,
1454                                         macroblock_t * p_mb,
1455                                         motion_t * p_motion,
1456                                         boolean_t b_average )
1457 {
1458     int i_offset = p_vpar->mb.i_offset;
1459     int i_width = p_vpar->picture.i_lum_stride;
1460     boolean_t b_current_field = p_vpar->picture.b_current_field;
1461
1462     MOTION_BLOCK( b_average, 0, 0, i_offset, p_motion->pppi_ref[b_current_field],
1463                   i_offset, i_width, 16, 0 );
1464 }
1465
1466 static void MotionFieldReuse( vpar_thread_t * p_vpar,
1467                                          macroblock_t * p_mb,
1468                                          motion_t * p_motion,
1469                                          boolean_t b_average )
1470 {
1471     int i_offset = p_vpar->mb.i_offset;
1472     int i_width = p_vpar->picture.i_lum_stride;
1473     boolean_t b_current_field = p_vpar->picture.b_current_field;
1474
1475     MOTION_BLOCK( b_average, p_motion->ppi_pmv[0][0], p_motion->ppi_pmv[0][1],
1476                   i_offset, p_motion->pppi_ref[b_current_field],
1477                   i_offset, i_width, 16, 0 );
1478 }
1479
1480 /* MPEG-2 concealment motion vectors. */
1481
1482 static void MotionFrameConceal( vpar_thread_t * p_vpar,
1483                                            macroblock_t * p_mv,
1484                                            motion_t * p_motion )
1485 {
1486     int i_tmp;
1487
1488     i_tmp = p_motion->ppi_pmv[0][0]
1489                 + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1490     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[0] );
1491     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_tmp;
1492
1493     i_tmp = p_motion->ppi_pmv[0][1]
1494                 + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1495     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[1] );
1496     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_tmp;
1497
1498     /* Marker bit. */
1499     RemoveBits( &p_vpar->bit_stream, 1 );
1500 }
1501
1502 static void MotionFieldConceal( vpar_thread_t * p_vpar,
1503                                            macroblock_t * p_mv,
1504                                            motion_t * p_motion )
1505 {
1506     int i_tmp;
1507
1508     /* field_select */
1509     RemoveBits( &p_vpar->bit_stream, 1 );
1510
1511     i_tmp = p_motion->ppi_pmv[0][0]
1512                 + MotionDelta( p_vpar, p_motion->pi_f_code[0] );
1513     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[0] );
1514     p_motion->ppi_pmv[1][0] = p_motion->ppi_pmv[0][0] = i_tmp;
1515
1516     i_tmp = p_motion->ppi_pmv[0][1]
1517                 + MotionDelta( p_vpar, p_motion->pi_f_code[1] );
1518     i_tmp = BoundMotionVector( i_tmp, p_motion->pi_f_code[1] );
1519     p_motion->ppi_pmv[1][1] = p_motion->ppi_pmv[0][1] = i_tmp;
1520
1521     /* Marker bit. */
1522     RemoveBits( &p_vpar->bit_stream, 1 );
1523 }
1524
1525
1526 /*
1527  * Macroblock information structures
1528  */
1529
1530 /*****************************************************************************
1531  * MacroblockAddressIncrement : Get the macroblock_address_increment field
1532  *****************************************************************************/
1533 static inline int MacroblockAddressIncrement( vpar_thread_t * p_vpar )
1534 {
1535     lookup_t *  p_tab;
1536     int         i_code;
1537     int         i_mba = 0;
1538
1539     for( ; ; )
1540     {
1541         if( (i_code = ShowBits( &p_vpar->bit_stream, 5 ) ) >= 0x2 )
1542         {
1543             p_tab = MBA_5 - 2 + i_code;
1544             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1545             return( i_mba + p_tab->i_value );
1546         }
1547         else if( (i_code = ShowBits( &p_vpar->bit_stream, 11 )) >= 0x18 )
1548         {
1549             p_tab = MBA_11 - 24 + i_code;
1550             RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1551             return( i_mba + p_tab->i_value );
1552         }
1553         else switch( i_code )
1554         {
1555         case 8:
1556             /* Macroblock escape */
1557             i_mba += 33;
1558             /* continue... */
1559         case 15:
1560             /* Macroblock stuffing (MPEG-1 ONLY) */
1561             RemoveBits( &p_vpar->bit_stream, 11 );
1562             break;
1563
1564         default:
1565             /* End of slice, or error */
1566             return 0;
1567         }
1568     }
1569 }
1570
1571 /*****************************************************************************
1572  * CodedPattern : coded_block_pattern
1573  *****************************************************************************/
1574 static inline int CodedPattern( vpar_thread_t * p_vpar )
1575 {
1576     lookup_t *  p_tab;
1577     int         i_code;
1578
1579     if( (i_code = ShowBits( &p_vpar->bit_stream, 7 )) >= 0x10 )
1580     {
1581         p_tab = CBP_7 - 16 + i_code;
1582         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1583     }
1584     else
1585     {
1586         p_tab = CBP_9 + ShowBits( &p_vpar->bit_stream, 9 );
1587         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1588     }
1589     if( p_vpar->sequence.i_chroma_format == CHROMA_420 )
1590     {
1591         return( p_tab->i_value << 6 );
1592     }
1593     if( p_vpar->sequence.i_chroma_format == CHROMA_422 )
1594     {
1595         return( (p_tab->i_value << 6)
1596                  | (GetBits( &p_vpar->bit_stream, 2 ) << 4) );
1597     }
1598     return( (p_tab->i_value << 6)
1599              | GetBits( &p_vpar->bit_stream, 6 ) );
1600 }
1601
1602 /*****************************************************************************
1603  * MacroblockModes : Get the macroblock_modes structure
1604  *****************************************************************************/
1605 static inline int MacroblockModes( vpar_thread_t * p_vpar, macroblock_t * p_mb,
1606                                    int i_coding_type, int i_structure )
1607 {
1608     int         i_mb_modes;
1609     lookup_t *  p_tab;
1610
1611     switch( i_coding_type )
1612     {
1613     case I_CODING_TYPE:
1614         p_tab = MB_I + ShowBits( &p_vpar->bit_stream, 1 );
1615         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1616         i_mb_modes = p_tab->i_value;
1617
1618         if( (i_structure == FRAME_STRUCTURE) &&
1619             (!p_vpar->picture.b_frame_pred_frame_dct) )
1620         {
1621             i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1622                                 * DCT_TYPE_INTERLACED;
1623         }
1624         return( i_mb_modes );
1625
1626     case P_CODING_TYPE:
1627         p_tab = MB_P + ShowBits( &p_vpar->bit_stream, 5 );
1628         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1629         i_mb_modes = p_tab->i_value;
1630
1631         if( i_structure != FRAME_STRUCTURE )
1632         {
1633             if( i_mb_modes & MB_MOTION_FORWARD )
1634             {
1635                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1636                                     * MOTION_TYPE_BASE;
1637             }
1638             return( i_mb_modes );
1639         }
1640         else if( p_vpar->picture.b_frame_pred_frame_dct )
1641         {
1642             if( i_mb_modes & MB_MOTION_FORWARD )
1643             {
1644                 i_mb_modes |= MC_FRAME;
1645             }
1646             return( i_mb_modes );
1647         }
1648         else
1649         {
1650             if( i_mb_modes & MB_MOTION_FORWARD )
1651             {
1652                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1653                                     * MOTION_TYPE_BASE;
1654             }
1655             if( i_mb_modes & (MB_INTRA | MB_PATTERN) )
1656             {
1657                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1658                                     * DCT_TYPE_INTERLACED;
1659             }
1660             return( i_mb_modes );
1661         }
1662
1663     case B_CODING_TYPE:
1664         p_tab = MB_B + ShowBits( &p_vpar->bit_stream, 6 );
1665         RemoveBits( &p_vpar->bit_stream, p_tab->i_length );
1666         i_mb_modes = p_tab->i_value;
1667
1668         if( i_structure != FRAME_STRUCTURE )
1669         {
1670             if( !( i_mb_modes & MB_INTRA ) )
1671             {
1672                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1673                                     * MOTION_TYPE_BASE;
1674             }
1675             return( i_mb_modes );
1676         }
1677         else if( p_vpar->picture.b_frame_pred_frame_dct )
1678         {
1679             i_mb_modes |= MC_FRAME;
1680             return( i_mb_modes );
1681         }
1682         else
1683         {
1684             if( i_mb_modes & MB_INTRA )
1685             {
1686                 goto mb_intra;
1687             }
1688             i_mb_modes |= GetBits( &p_vpar->bit_stream, 2 )
1689                                 * MOTION_TYPE_BASE;
1690             if( i_mb_modes & (MB_INTRA | MB_PATTERN) )
1691             {
1692 mb_intra:
1693                 i_mb_modes |= GetBits( &p_vpar->bit_stream, 1 )
1694                                     * DCT_TYPE_INTERLACED;
1695             }
1696             return( i_mb_modes );
1697         }
1698
1699     case D_CODING_TYPE:
1700         RemoveBits( &p_vpar->bit_stream, 1 );
1701         return( MB_INTRA );
1702
1703     default:
1704         return( 0 );
1705     }
1706 }
1707
1708
1709 /*
1710  * Picture data parsing management
1711  */
1712
1713 /*****************************************************************************
1714  * ParseSlice : Parse the next slice structure
1715  *****************************************************************************/
1716 #define MOTION( pf_routine, i_direction )                                   \
1717     if( (i_direction) & MB_MOTION_FORWARD )                                 \
1718     {                                                                       \
1719         pf_routine( p_vpar, p_mb, &p_vpar->mb.f_motion, 0 );                \
1720         if( (i_coding_type == B_CODING_TYPE)                                \
1721                 && ((i_direction) & MB_MOTION_BACKWARD) )                   \
1722         {                                                                   \
1723             pf_routine( p_vpar, p_mb, &p_vpar->mb.b_motion, 1 );            \
1724         }                                                                   \
1725     }                                                                       \
1726     else if( (i_coding_type == B_CODING_TYPE)                               \
1727                  && ((i_direction) & MB_MOTION_BACKWARD) )                  \
1728     {                                                                       \
1729         pf_routine( p_vpar, p_mb, &p_vpar->mb.b_motion, 0 );                \
1730     }
1731
1732 #define CHECK_BOUNDARIES                                                    \
1733     i_offset = p_vpar->mb.i_offset;                                         \
1734     if( i_offset == p_vpar->picture.i_field_width )                         \
1735     {                                                                       \
1736         if( i_coding_type != I_CODING_TYPE ||                               \
1737             p_vpar->picture.b_concealment_mv )                              \
1738         {                                                                   \
1739             p_f_motion->pppi_ref[0][0] += i_lum_vsize;                      \
1740             p_f_motion->pppi_ref[0][1] += i_chrom_vsize;                    \
1741             p_f_motion->pppi_ref[0][2] += i_chrom_vsize;                    \
1742         }                                                                   \
1743         if( i_coding_type == B_CODING_TYPE )                                \
1744         {                                                                   \
1745             p_b_motion->pppi_ref[0][0] += i_lum_vsize;                      \
1746             p_b_motion->pppi_ref[0][1] += i_chrom_vsize;                    \
1747             p_b_motion->pppi_ref[0][2] += i_chrom_vsize;                    \
1748         }                                                                   \
1749         p_dest[0] += i_lum_vsize;                                           \
1750         p_dest[1] += i_chrom_vsize;                                         \
1751         p_dest[2] += i_chrom_vsize;                                         \
1752         i_offset = 0;                                                       \
1753     }                                                                       \
1754     p_vpar->mb.i_offset = i_offset;
1755
1756 #define PARSEERROR                                                          \
1757     if( p_vpar->picture.b_error )                                           \
1758     {                                                                       \
1759         /* Go to the next slice. */                                         \
1760         p_vpar->pool.pf_free_mb( &p_vpar->pool, p_mb );                     \
1761         return;                                                             \
1762     }
1763
1764 static inline void ParseSlice( vpar_thread_t * p_vpar,
1765                                u32 i_vert_code, boolean_t b_mpeg2,
1766                                int i_coding_type, int i_structure )
1767 {
1768     int             i_lum_offset, i_chrom_offset, i_offset, i_lum_vsize, i_chrom_vsize;
1769     picture_t *     pp_forward_ref[2];
1770     yuv_data_t *    p_dest[3];
1771
1772     motion_t *      p_f_motion = &p_vpar->mb.f_motion;
1773     motion_t *      p_b_motion = &p_vpar->mb.b_motion;
1774
1775     /* Parse header. */
1776     LoadQuantizerScale( p_vpar );
1777
1778     if( GetBits( &p_vpar->bit_stream, 1 ) )
1779     {
1780         /* intra_slice, slice_id */
1781         RemoveBits( &p_vpar->bit_stream, 8 );
1782         /* extra_information_slice */
1783         while( GetBits( &p_vpar->bit_stream, 1 ) )
1784         {
1785             RemoveBits( &p_vpar->bit_stream, 8 );
1786         }
1787     }
1788
1789     /* Calculate the position of the macroblock. */
1790     i_lum_offset = (i_vert_code - 1) * p_vpar->picture.p_picture->Y_PITCH * 16;
1791     i_chrom_offset = (i_vert_code - 1) * p_vpar->picture.p_picture->U_PITCH
1792                        * 8 * (2 - p_vpar->sequence.b_chroma_v_subsampled);
1793     i_lum_vsize = 16 * p_vpar->picture.p_picture->Y_PITCH;
1794     i_chrom_vsize = 8 * (2 - p_vpar->sequence.b_chroma_v_subsampled)
1795                       * p_vpar->picture.p_picture->U_PITCH;
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_lum_offset <<= 1;
1803         i_chrom_offset <<= 1;
1804         i_lum_vsize <<= 1;
1805         i_chrom_vsize <<= 1;
1806
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             int i_tmp;
1817             p_f_motion->pppi_ref[1][0] = pp_forward_ref[1]->Y_PIXELS +
1818                 i_lum_offset + pp_forward_ref[1]->Y_PITCH;
1819             p_f_motion->pppi_ref[1][1] = pp_forward_ref[1]->U_PIXELS +
1820                 (i_tmp = i_chrom_offset + pp_forward_ref[1]->U_PITCH);
1821             p_f_motion->pppi_ref[1][2] = pp_forward_ref[1]->V_PIXELS +
1822                 i_tmp;
1823         }
1824         if( i_coding_type == B_CODING_TYPE )
1825         {
1826             int i_tmp;
1827             p_b_motion->pppi_ref[1][0] = p_vpar->sequence.p_backward->Y_PIXELS
1828                 + i_lum_offset + p_vpar->sequence.p_backward->Y_PITCH;
1829             p_b_motion->pppi_ref[1][1] = p_vpar->sequence.p_backward->U_PIXELS
1830                 + (i_tmp = i_chrom_offset + p_vpar->sequence.p_backward->U_PITCH);
1831             p_b_motion->pppi_ref[1][2] = p_vpar->sequence.p_backward->V_PIXELS
1832                 + i_tmp;
1833         }
1834     }
1835
1836     if( i_coding_type != I_CODING_TYPE || p_vpar->picture.b_concealment_mv )
1837     {
1838         p_f_motion->pppi_ref[0][0] = pp_forward_ref[0]->Y_PIXELS +
1839             i_lum_offset;
1840         p_f_motion->pppi_ref[0][1] = pp_forward_ref[0]->U_PIXELS +
1841             i_chrom_offset;
1842         p_f_motion->pppi_ref[0][2] = pp_forward_ref[0]->V_PIXELS +
1843             i_chrom_offset;
1844         p_f_motion->pi_f_code[0] = p_vpar->picture.ppi_f_code[0][0];
1845         p_f_motion->pi_f_code[1] = p_vpar->picture.ppi_f_code[0][1];
1846         p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1847         p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1848     }
1849
1850     if( i_coding_type == B_CODING_TYPE )
1851     {
1852         p_b_motion->pppi_ref[0][0] = p_vpar->sequence.p_backward->Y_PIXELS +
1853             i_lum_offset;
1854         p_b_motion->pppi_ref[0][1] = p_vpar->sequence.p_backward->U_PIXELS +
1855             i_chrom_offset;
1856         p_b_motion->pppi_ref[0][2] = p_vpar->sequence.p_backward->V_PIXELS +
1857             i_chrom_offset;
1858         p_b_motion->pi_f_code[0] = p_vpar->picture.ppi_f_code[1][0];
1859         p_b_motion->pi_f_code[1] = p_vpar->picture.ppi_f_code[1][1];
1860         p_b_motion->ppi_pmv[0][0] = p_b_motion->ppi_pmv[0][1] = 0;
1861         p_b_motion->ppi_pmv[1][0] = p_b_motion->ppi_pmv[1][1] = 0;
1862     }
1863
1864     /* Initialize destination pointers. */
1865     p_dest[0] = p_vpar->picture.p_picture->Y_PIXELS +
1866         i_lum_offset;
1867     p_dest[1] = p_vpar->picture.p_picture->U_PIXELS +
1868         i_chrom_offset;
1869     p_dest[2] = p_vpar->picture.p_picture->V_PIXELS +
1870         i_chrom_offset;
1871
1872     if( i_structure == BOTTOM_FIELD )
1873     {
1874         p_dest[0] += p_vpar->picture.p_picture->Y_PITCH;
1875         p_dest[1] += p_vpar->picture.p_picture->U_PITCH;
1876         p_dest[2] += p_vpar->picture.p_picture->U_PITCH;
1877     }
1878
1879     /* Reset intra DC coefficients predictors (ISO/IEC 13818-2 7.2.1). */
1880     p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
1881         = p_vpar->mb.pi_dc_dct_pred[2]
1882         = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
1883
1884     p_vpar->mb.i_offset = MacroblockAddressIncrement( p_vpar ) << 4;
1885
1886     while( (int)(p_vpar->mb.i_offset - p_vpar->sequence.i_width) >= 0 )
1887     {
1888         /* Unusual construct at the start of some slices. Jump one line. */
1889         p_vpar->mb.i_offset -= p_vpar->sequence.i_width;
1890
1891         p_dest[0] += i_lum_vsize;
1892         p_dest[1] += i_chrom_vsize;
1893         p_dest[2] += i_chrom_vsize;
1894
1895         p_f_motion->pppi_ref[0][0] += i_lum_vsize;
1896         p_f_motion->pppi_ref[0][1] += i_chrom_vsize;
1897         p_f_motion->pppi_ref[0][2] += i_chrom_vsize;
1898
1899         p_f_motion->pppi_ref[1][0] += i_lum_vsize;
1900         p_f_motion->pppi_ref[1][1] += i_chrom_vsize;
1901         p_f_motion->pppi_ref[1][2] += i_chrom_vsize;
1902     }
1903
1904     for( ; ; )
1905     {
1906         /* Decode macroblocks. */
1907         macroblock_t *  p_mb;
1908         int             i_mb_modes;
1909
1910         /* Get a macroblock structure. */
1911         p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
1912         p_mb->i_nb_motions = 0;
1913         p_mb->pp_dest[0] = p_dest[0];
1914         p_mb->pp_dest[1] = p_dest[1];
1915         p_mb->pp_dest[2] = p_dest[2];
1916
1917         /* Parse off macroblock_modes structure. */
1918         p_mb->i_mb_modes = i_mb_modes =
1919                 MacroblockModes( p_vpar, p_mb, i_coding_type, i_structure );
1920
1921         if( i_mb_modes & MB_QUANT )
1922         {
1923             LoadQuantizerScale( p_vpar );
1924         }
1925
1926         if( i_mb_modes & MB_INTRA )
1927         {
1928             if( p_vpar->picture.b_concealment_mv )
1929             {
1930                 if( i_structure == FRAME_STRUCTURE )
1931                 {
1932                     MotionFrameConceal( p_vpar, p_mb, p_f_motion );
1933                 }
1934                 else
1935                 {
1936                     MotionFieldConceal( p_vpar, p_mb, p_f_motion );
1937                 }
1938             }
1939             else
1940             {
1941                 /* Reset motion vectors predictors. */
1942                 p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1943                 p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1944                 p_b_motion->ppi_pmv[0][0] = p_b_motion->ppi_pmv[0][1] = 0;
1945                 p_b_motion->ppi_pmv[1][0] = p_b_motion->ppi_pmv[1][1] = 0;
1946             }
1947
1948             /* Decode blocks */
1949             if( b_mpeg2 )
1950             {
1951                 if( p_vpar->picture.b_intra_vlc_format )
1952                 {
1953                     MPEG2IntraB15MB( p_vpar, p_mb );
1954                 }
1955                 else
1956                 {
1957                     MPEG2IntraB14MB( p_vpar, p_mb );
1958                 }
1959             }
1960             else
1961             {
1962                 MPEG1IntraMB( p_vpar, p_mb );
1963             }
1964
1965             if( i_coding_type == D_CODING_TYPE )
1966             {
1967                 RemoveBits( &p_vpar->bit_stream, 1 );
1968             }
1969         }
1970         else
1971         {
1972             /* Non-intra block */
1973             if( !b_mpeg2 )
1974             {
1975                 if( (i_mb_modes & MOTION_TYPE_MASK) == MC_FRAME )
1976                 {
1977                     MOTION( MotionMPEG1, i_mb_modes );
1978                 }
1979                 else
1980                 {
1981                     /* Non-intra MB without forward mv in a P picture. */
1982                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
1983                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
1984                     MOTION( MotionFrameZero, MB_MOTION_FORWARD );
1985                 }
1986             }
1987             else if( i_structure == FRAME_STRUCTURE )
1988             {
1989                 switch( i_mb_modes & MOTION_TYPE_MASK )
1990                 {
1991                 case MC_FRAME:
1992                     MOTION( MotionFrameFrame, i_mb_modes );
1993                     break;
1994
1995                 case MC_FIELD:
1996                     MOTION( MotionFrameField, i_mb_modes );
1997                     break;
1998
1999                 case MC_DMV:
2000                     MOTION( MotionFrameDMV, MB_MOTION_FORWARD );
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( MotionFrameZero, MB_MOTION_FORWARD );
2008                 }
2009             }
2010             else
2011             {
2012                 /* Field structure. */
2013                 switch( i_mb_modes & MOTION_TYPE_MASK )
2014                 {
2015                 case MC_FIELD:
2016                     MOTION( MotionFieldField, i_mb_modes );
2017                     break;
2018
2019                 case MC_16X8:
2020                     MOTION( MotionField16x8, i_mb_modes );
2021                     break;
2022
2023                 case MC_DMV:
2024                     MOTION( MotionFieldDMV, i_mb_modes );
2025                     break;
2026
2027                 case 0:
2028                     /* Non-intra MB without forward mv in a P picture. */
2029                     p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2030                     p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2031                     MOTION( MotionFieldZero, MB_MOTION_FORWARD );
2032                 }
2033             }
2034
2035             /* ISO/IEC 13818-2 6.3.17.4 : Coded Block Pattern */
2036             if( i_mb_modes & MB_PATTERN )
2037             {
2038                 p_mb->i_coded_block_pattern = CodedPattern( p_vpar );
2039                 if( b_mpeg2 )
2040                 {
2041                     MPEG2NonIntraMB( p_vpar, p_mb );
2042                 }
2043                 else
2044                 {
2045                     MPEG1NonIntraMB( p_vpar, p_mb );
2046                 }
2047             }
2048             else
2049             {
2050                 p_mb->i_coded_block_pattern = 0;
2051             }
2052
2053             /* Reset intra DC coefficients predictors. */
2054             p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2055                 = p_vpar->mb.pi_dc_dct_pred[2]
2056                 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2057         }
2058
2059         /* End of macroblock. */
2060         PARSEERROR;
2061         p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2062
2063         /* Prepare context for the next macroblock. */
2064         p_vpar->mb.i_offset += 16;
2065         CHECK_BOUNDARIES;
2066
2067         if( ShowBits( &p_vpar->bit_stream, 1 ) )
2068         {
2069             /* Macroblock Address Increment == 1 */
2070             RemoveBits( &p_vpar->bit_stream, 1 );
2071         }
2072         else
2073         {
2074             /* Check for skipped macroblock(s). */
2075             int i_mba_inc;
2076
2077             i_mba_inc = MacroblockAddressIncrement( p_vpar );
2078             if( !i_mba_inc )
2079             {
2080                 /* End of slice. */
2081                 break;
2082             }
2083
2084             /* Reset intra DC predictors. */
2085             p_vpar->mb.pi_dc_dct_pred[0] = p_vpar->mb.pi_dc_dct_pred[1]
2086                 = p_vpar->mb.pi_dc_dct_pred[2]
2087                 = 1 << (7 + p_vpar->picture.i_intra_dc_precision);
2088
2089             if( i_coding_type == P_CODING_TYPE )
2090             {
2091                 p_f_motion->ppi_pmv[0][0] = p_f_motion->ppi_pmv[0][1] = 0;
2092                 p_f_motion->ppi_pmv[1][0] = p_f_motion->ppi_pmv[1][1] = 0;
2093
2094                 do {
2095                     p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
2096                     p_mb->i_mb_modes = 0;
2097                     p_mb->i_nb_motions = 0;
2098                     p_mb->i_coded_block_pattern = 0;
2099                     p_mb->pp_dest[0] = p_dest[0]; 
2100                     p_mb->pp_dest[1] = p_dest[1]; 
2101                     p_mb->pp_dest[2] = p_dest[2]; 
2102
2103                     if( i_structure == FRAME_STRUCTURE )
2104                     {
2105                         MOTION( MotionFrameZero, MB_MOTION_FORWARD );
2106                     }
2107                     else
2108                     {
2109                         MOTION( MotionFieldZero, MB_MOTION_FORWARD );
2110                     }
2111
2112                     p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2113                     p_vpar->mb.i_offset += 16;
2114                     CHECK_BOUNDARIES;
2115                 } while( --i_mba_inc );
2116             }
2117             else
2118             {
2119                 do {
2120                     p_mb = p_vpar->pool.pf_new_mb( &p_vpar->pool );
2121                     p_mb->i_mb_modes = 0;
2122                     p_mb->i_nb_motions = 0;
2123                     p_mb->i_coded_block_pattern = 0;
2124                     p_mb->pp_dest[0] = p_dest[0]; 
2125                     p_mb->pp_dest[1] = p_dest[1]; 
2126                     p_mb->pp_dest[2] = p_dest[2]; 
2127
2128                     if( !b_mpeg2 )
2129                     {
2130                         MOTION( MotionMPEG1Reuse, i_mb_modes );
2131                     }
2132                     else if( i_structure == FRAME_STRUCTURE )
2133                     {
2134                         MOTION( MotionFrameReuse, i_mb_modes );
2135                     }
2136                     else
2137                     {
2138                         MOTION( MotionFieldReuse, i_mb_modes );
2139                     }
2140
2141                     p_vpar->pool.pf_decode_mb( &p_vpar->pool, p_mb );
2142                     p_vpar->mb.i_offset += 16;
2143                     CHECK_BOUNDARIES;
2144                 } while( --i_mba_inc );
2145             }
2146         }
2147     }
2148
2149     NextStartCode( &p_vpar->bit_stream );
2150 }
2151
2152 /*****************************************************************************
2153  * PictureData : Parse off all macroblocks (ISO/IEC 13818-2 6.2.3.7)
2154  *****************************************************************************/
2155 static inline void vpar_PictureData( vpar_thread_t * p_vpar, boolean_t b_mpeg2,
2156                                      int i_coding_type, int i_structure )
2157 {
2158     u32         i_dummy;
2159
2160     NextStartCode( &p_vpar->bit_stream );
2161     while( !p_vpar->picture.b_error && !p_vpar->p_fifo->b_die )
2162     {
2163         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
2164                  < SLICE_START_CODE_MIN) ||
2165             (i_dummy > SLICE_START_CODE_MAX) )
2166         {
2167             break;
2168         }
2169         RemoveBits32( &p_vpar->bit_stream );
2170
2171         /* Decode slice data. */
2172         ParseSlice( p_vpar, i_dummy & 255, b_mpeg2, i_coding_type,
2173                     i_structure );
2174     }
2175 }
2176
2177 #define DECLARE_PICD( FUNCNAME, B_MPEG2, I_CODING_TYPE, I_STRUCTURE )       \
2178 void FUNCNAME( vpar_thread_t * p_vpar )                                     \
2179 {                                                                           \
2180     vpar_PictureData( p_vpar, B_MPEG2, I_CODING_TYPE, I_STRUCTURE );        \
2181 }
2182
2183 DECLARE_PICD( vpar_PictureDataGENERIC, p_vpar->sequence.b_mpeg2,
2184               p_vpar->picture.i_coding_type, p_vpar->picture.i_structure );
2185 #if (VPAR_OPTIM_LEVEL > 0)
2186 DECLARE_PICD( vpar_PictureData2IF, 1, I_CODING_TYPE, FRAME_STRUCTURE );
2187 DECLARE_PICD( vpar_PictureData2PF, 1, P_CODING_TYPE, FRAME_STRUCTURE );
2188 DECLARE_PICD( vpar_PictureData2BF, 1, B_CODING_TYPE, FRAME_STRUCTURE );
2189 #endif
2190 #if (VPAR_OPTIM_LEVEL > 1)
2191 DECLARE_PICD( vpar_PictureData2IT, 1, I_CODING_TYPE, TOP_FIELD );
2192 DECLARE_PICD( vpar_PictureData2PT, 1, P_CODING_TYPE, TOP_FIELD );
2193 DECLARE_PICD( vpar_PictureData2BT, 1, B_CODING_TYPE, TOP_FIELD );
2194 DECLARE_PICD( vpar_PictureData2IB, 1, I_CODING_TYPE, BOTTOM_FIELD );
2195 DECLARE_PICD( vpar_PictureData2PB, 1, P_CODING_TYPE, BOTTOM_FIELD );
2196 DECLARE_PICD( vpar_PictureData2BB, 1, B_CODING_TYPE, BOTTOM_FIELD );
2197 DECLARE_PICD( vpar_PictureData1I, 0, I_CODING_TYPE, FRAME_STRUCTURE );
2198 DECLARE_PICD( vpar_PictureData1P, 0, P_CODING_TYPE, FRAME_STRUCTURE );
2199 DECLARE_PICD( vpar_PictureData1B, 0, B_CODING_TYPE, FRAME_STRUCTURE );
2200 DECLARE_PICD( vpar_PictureData1D, 0, D_CODING_TYPE, FRAME_STRUCTURE );
2201 #endif
2202
2203 #undef DECLARE_PICD
2204