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