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