]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
169afb700d126e621bc5febd6f25f050bff92aec
[vlc] / src / video_parser / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15 #include <X11/Xlib.h>
16 #include <X11/extensions/XShm.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
35
36 #include "vpar_blocks.h"
37 #include "vpar_headers.h"
38 #include "video_fifo.h"
39 #include "vpar_synchro.h"
40 #include "video_parser.h"
41 #include "vpar_motion.h"
42
43 /*
44  * Local prototypes
45  */
46 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
47 static void SequenceHeader( vpar_thread_t * p_vpar );
48 static void GroupHeader( vpar_thread_t * p_vpar );
49 static void PictureHeader( vpar_thread_t * p_vpar );
50 static void SliceHeader00( vpar_thread_t * p_vpar,
51                            int * pi_mb_address, int i_mb_base,
52                            u32 i_vert_code );
53 static void SliceHeader01( vpar_thread_t * p_vpar,
54                            int * pi_mb_address, int i_mb_base,
55                            u32 i_vert_code );
56 static void SliceHeader10( vpar_thread_t * p_vpar,
57                            int * pi_mb_address, int i_mb_base,
58                            u32 i_vert_code );
59 static void SliceHeader11( vpar_thread_t * p_vpar,
60                            int * pi_mb_address, int i_mb_base,
61                            u32 i_vert_code );
62 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
63                                     int * pi_mb_address, int i_mb_base,
64                                     u32 i_vert_code );
65 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
66 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
67 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
68 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
69 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
70 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
71 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
72 static void CopyrightExtension( vpar_thread_t * p_vpar );
73
74 /*
75  * Standard variables
76  */
77
78 /*****************************************************************************
79  * pi_default_intra_quant : default quantization matrix
80  *****************************************************************************/
81 #ifndef VDEC_DFT
82 int pi_default_intra_quant[] =
83 {
84     8,  16, 19, 22, 26, 27, 29, 34,
85     16, 16, 22, 24, 27, 29, 34, 37,
86     19, 22, 26, 27, 29, 34, 34, 38,
87     22, 22, 26, 27, 29, 34, 37, 40,
88     22, 26, 27, 29, 32, 35, 40, 48,
89     26, 27, 29, 32, 35, 40, 48, 58,
90     26, 27, 29, 34, 38, 46, 56, 69,
91     27, 29, 35, 38, 46, 56, 69, 83
92 };      
93 #else
94 int pi_default_intra_quant[] =
95 {
96     2048,   5681,   6355,   6623,   6656,   5431,   4018,   2401,
97     5681,   7880,   10207,  10021,  9587,   8091,   6534,   3625,
98     6355,   10207,  11363,  10619,  9700,   8935,   6155,   3507,
99     6623,   9186,   10226,  9557,   8730,   8041,   6028,   3322,
100     5632,   9232,   9031,   8730,   8192,   7040,   5542,   3390,
101     5230,   7533,   7621,   7568,   7040,   6321,   5225,   3219,
102     3602,   5189,   5250,   5539,   5265,   5007,   4199,   2638,
103     1907,   2841,   3230,   3156,   3249,   3108,   2638,   1617        
104 };
105 #endif
106
107 /*****************************************************************************
108  * pi_default_nonintra_quant : default quantization matrix
109  *****************************************************************************/
110 #ifndef VDEC_DFT
111 int pi_default_nonintra_quant[] =
112 {
113     16, 16, 16, 16, 16, 16, 16, 16,
114     16, 16, 16, 16, 16, 16, 16, 16,
115     16, 16, 16, 16, 16, 16, 16, 16,
116     16, 16, 16, 16, 16, 16, 16, 16,
117     16, 16, 16, 16, 16, 16, 16, 16,
118     16, 16, 16, 16, 16, 16, 16, 16,
119     16, 16, 16, 16, 16, 16, 16, 16,
120     16, 16, 16, 16, 16, 16, 16, 16
121 };
122 #else
123 int pi_default_nonintra_quanit[] =
124 {
125     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
126     5680,   7888,   7424,   6688,   5680,   4464,   3072,   1568,
127     5344,   7424,   6992,   6288,   5344,   4208,   2896,   1472,
128     4816,   6688,   6288,   5664,   4816,   3792,   2608,   1328,
129     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
130     3216,   4464,   4208,   3792,   3216,   2528,   1744,   880,
131     2224,   3072,   2896,   2608,   2224,   1744,   1200,   608,
132     1136,   1568,   1472,   1328,   1136,   880,    608,    304 
133 };
134 #endif
135
136 /*****************************************************************************
137  * pi_scan : zig-zag and alternate scan patterns
138  *****************************************************************************/
139 u8 pi_scan[2][64] =
140 {
141     { /* Zig-Zag pattern */
142         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
143         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
144         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
145         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
146     },
147     { /* Alternate scan pattern */
148         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
149         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
150         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
151         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
152     }
153 };
154
155 /*
156  * Local inline functions.
157  */
158
159 /*****************************************************************************
160  * NextStartCode : Find the next start code
161  *****************************************************************************/
162 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
163 {
164     /* Re-align the buffer on an 8-bit boundary */
165     RealignBits( &p_vpar->bit_stream );
166
167     while( ShowBits( &p_vpar->bit_stream, 16 ) != 0 && !p_vpar->b_die )
168     {
169         DumpBits( &p_vpar->bit_stream, 8 );
170     }
171     DumpBits( &p_vpar->bit_stream, 16 );
172 }
173
174 /*****************************************************************************
175  * ReferenceUpdate : Update the reference pointers when we have a new picture
176  *****************************************************************************/
177 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
178                                         int i_coding_type,
179                                         picture_t * p_newref )
180 {
181     if( i_coding_type != B_CODING_TYPE )
182     {
183         if( p_vpar->sequence.p_forward != NULL )
184             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
185         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
186         p_vpar->sequence.p_backward = p_newref;
187         if( p_newref != NULL )
188             vout_LinkPicture( p_vpar->p_vout, p_newref );
189     }
190 }
191
192 /*****************************************************************************
193  * ReferenceReplace : Replace the last reference pointer when we destroy
194  * a picture
195  *****************************************************************************/
196 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
197                                          int i_coding_type,
198                                          picture_t * p_newref )
199 {
200     if( i_coding_type != B_CODING_TYPE )
201     {
202         if( p_vpar->sequence.p_backward != NULL )
203             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
204         p_vpar->sequence.p_backward = p_newref;
205         if( p_newref != NULL )
206             vout_LinkPicture( p_vpar->p_vout, p_newref );
207     }
208 }
209
210 /*****************************************************************************
211  * LoadMatrix : Load a quantization matrix
212  *****************************************************************************/
213 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
214 {
215     int i_dummy;
216     
217     if( !p_matrix->b_allocated )
218     {
219         /* Allocate a piece of memory to load the matrix. */
220         p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
221         p_matrix->b_allocated = 1;
222     }
223     
224     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
225     {
226         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
227              = GetBits( &p_vpar->bit_stream, 8 );
228     }
229
230 #ifdef VDEC_DFT
231     /* Discrete Fourier Transform requires the quantization matrices to
232      * be normalized before using them. */
233     vdec_NormQuantMatrix( p_matrix->pi_matrix );
234 #endif
235 }
236
237 /*****************************************************************************
238  * LinkMatrix : Link a quantization matrix to another
239  *****************************************************************************/
240 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
241 {
242     int i_dummy;
243     
244     if( p_matrix->b_allocated )
245     {
246         /* Deallocate the piece of memory. */
247         free( p_matrix->pi_matrix );
248         p_matrix->b_allocated = 0;
249     }
250     
251     p_matrix->pi_matrix = pi_array;
252 }
253
254 /*
255  * Exported functions.
256  */
257
258 /*****************************************************************************
259  * vpar_NextSequenceHeader : Find the next sequence header
260  *****************************************************************************/
261 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
262 {
263     while( !p_vpar->b_die )
264     {
265         NextStartCode( p_vpar );
266         if( ShowBits( &p_vpar->bit_stream, 16 ) == SEQUENCE_HEADER_CODE )
267             return 0;
268     }
269     return 1;
270 }
271
272 /*****************************************************************************
273  * vpar_ParseHeader : Parse the next header
274  *****************************************************************************/
275 int vpar_ParseHeader( vpar_thread_t * p_vpar )
276 {
277     while( !p_vpar->b_die )
278     {
279         NextStartCode( p_vpar );
280         switch( GetBits32( &p_vpar->bit_stream ) )
281         {
282         case SEQUENCE_HEADER_CODE:
283             fprintf( stderr, "begin sequence header\n" );
284             SequenceHeader( p_vpar );
285             fprintf( stderr, "end sequence header\n" );
286             return 0;
287             break;
288
289         case GROUP_START_CODE:
290             fprintf( stderr, "begin group\n" );
291             GroupHeader( p_vpar );
292             fprintf( stderr, "end group\n" );
293             return 0;
294             break;
295
296         case PICTURE_START_CODE:
297             fprintf( stderr, "begin picture\n" );
298             PictureHeader( p_vpar );
299             fprintf( stderr, "end picture\n" );
300             return 0;
301             break;
302
303         case SEQUENCE_END_CODE:
304             fprintf( stderr, "sequence header end code\n" );
305             return 1;
306             break;
307
308         default:
309         }
310     }
311
312     return 0;
313 }
314
315 /*
316  * Following functions are local
317  */
318
319 /*****************************************************************************
320  * SequenceHeader : Parse the next sequence header
321  *****************************************************************************/
322 static void SequenceHeader( vpar_thread_t * p_vpar )
323 {
324 #define RESERVED    -1 
325     static double d_frame_rate_table[16] =
326     {
327         0.0,
328         ((23.0*1000.0)/1001.0),
329         24.0,
330         25.0,
331         ((30.0*1000.0)/1001.0),
332         30.0,
333         50.0,
334         ((60.0*1000.0)/1001.0),
335         60.0,
336         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
337     };
338 #undef RESERVED
339
340     int i_height_save, i_width_save;
341     
342     i_height_save = p_vpar->sequence.i_height;
343     i_width_save = p_vpar->sequence.i_width;
344
345     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
346     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
347     p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
348     p_vpar->sequence.d_frame_rate =
349             d_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
350
351     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
352      * constrained_parameters_flag */
353     DumpBits( &p_vpar->bit_stream, 30 );
354     
355     /*
356      * Quantization matrices
357      */
358     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
359     {
360         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
361     }
362     else
363     {
364         /* Use default matrix. */
365         LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
366     }
367     
368     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
369     {
370         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
371     }
372     else
373     {
374         /* Use default matrix. */
375         LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
376     }
377     
378     /* Unless later overwritten by a matrix extension, we have the same
379      * matrices for luminance and chrominance. */
380     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
381                 p_vpar->sequence.intra_quant.pi_matrix );
382     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
383                 p_vpar->sequence.nonintra_quant.pi_matrix );
384
385     /*
386      * Sequence Extension
387      */
388     NextStartCode( p_vpar );
389     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
390     {
391         int                         i_dummy;
392         static f_chroma_pattern_t   ppf_chroma_pattern[4] =
393                             {NULL, vpar_CodedPattern420,
394                              vpar_CodedPattern422, vpar_CodedPattern444};
395
396         /* Turn the MPEG2 flag on */
397         p_vpar->sequence.b_mpeg2 = 1;
398     
399         /* Parse sequence_extension */
400         DumpBits32( &p_vpar->bit_stream );
401         /* extension_start_code_identifier, profile_and_level_indication */
402         DumpBits( &p_vpar->bit_stream, 12 );
403         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
404         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
405         p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
406                                     [p_vpar->sequence.i_chroma_format];
407         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
408         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
409         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
410         DumpBits( &p_vpar->bit_stream, 22 );
411         /* frame_rate_extension_n */
412         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
413         /* frame_rate_extension_d */
414         p_vpar->sequence.d_frame_rate *= (i_dummy + 1)
415                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
416
417         p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
418     }
419     else
420     {
421         /* It's an MPEG-1 stream. Put adequate parameters. */
422
423         p_vpar->sequence.b_mpeg2 = 0;
424         p_vpar->sequence.b_progressive = 1;
425         p_vpar->sequence.i_chroma_format = CHROMA_420;
426         p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
427
428         p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
429     }
430
431     /* Update sizes */
432     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
433     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
434                                    (p_vpar->sequence.i_height + 15) / 16 :
435                                    2 * (p_vpar->sequence.i_height + 31) / 32;
436     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
437                                         * p_vpar->sequence.i_mb_height;
438     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
439     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
440     p_vpar->sequence.i_size = p_vpar->sequence.i_width
441                                         * p_vpar->sequence.i_height;
442
443     /* Update chromatic information */
444     switch( p_vpar->sequence.i_chroma_format )
445     {
446     case CHROMA_420:
447         p_vpar->sequence.i_chroma_nb_blocks = 2;
448         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 2;
449         p_vpar->sequence.i_chroma_mb_width = 8;
450         p_vpar->sequence.i_chroma_mb_height = 8;
451         break;
452
453     case CHROMA_422:
454         p_vpar->sequence.i_chroma_nb_blocks = 4;
455         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
456         p_vpar->sequence.i_chroma_mb_width = 8;
457         p_vpar->sequence.i_chroma_mb_height = 16;
458         break;
459
460     case CHROMA_444:
461         p_vpar->sequence.i_chroma_nb_blocks = 8;
462         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
463         p_vpar->sequence.i_chroma_mb_width = 16;
464         p_vpar->sequence.i_chroma_mb_height = 16;
465     }
466
467     /* Slice Header functions */
468     if( p_vpar->sequence.i_height <= 2800 )
469     {
470         if( p_vpar->sequence.i_scalable_mode != SC_DP )
471         {
472             p_vpar->sequence.pf_slice_header = SliceHeader00;
473         }
474         else
475         {
476             p_vpar->sequence.pf_slice_header = SliceHeader01;
477         }
478     }
479     else
480     {
481         if( p_vpar->sequence.i_scalable_mode != SC_DP )
482         {
483             p_vpar->sequence.pf_slice_header = SliceHeader10;
484         }
485         else
486         {
487             p_vpar->sequence.pf_slice_header = SliceHeader11;
488         }
489     }
490
491     if(    p_vpar->sequence.i_width != i_width_save
492         || p_vpar->sequence.i_height != i_height_save )
493     {
494          /* What do we do in case of a size change ??? */
495     }
496
497     /* Extension and User data */
498     ExtensionAndUserData( p_vpar );
499 }
500
501 /*****************************************************************************
502  * GroupHeader : Parse the next group of pictures header
503  *****************************************************************************/
504 static void GroupHeader( vpar_thread_t * p_vpar )
505 {
506     /* Nothing to do, we don't care. */
507     DumpBits( &p_vpar->bit_stream, 27 );
508     ExtensionAndUserData( p_vpar );
509 }
510
511 /*****************************************************************************
512  * PictureHeader : Parse the next picture header
513  *****************************************************************************/
514 static void PictureHeader( vpar_thread_t * p_vpar )
515 {
516     static f_macroblock_type_t ppf_macroblock_type[4] =
517                                                  {vpar_IMBType, vpar_PMBType,
518                                                   vpar_BMBType, vpar_DMBType};
519
520     int                 i_structure;
521     int                 i_mb_address, i_mb_base, i_mb;
522     elem_t *            p_y, p_u, p_v;
523     boolean_t           b_parsable;
524     u32                 i_dummy;
525     
526     DumpBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
527     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
528     p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
529                                          [p_vpar->picture.i_coding_type];
530     
531     DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
532     
533     p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
534     p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
535     p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
536     p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
537
538     /* extra_information_picture */
539     while( GetBits( &p_vpar->bit_stream, 1 ) )
540     {
541         DumpBits( &p_vpar->bit_stream, 8 );
542     }
543
544     /* 
545      * Picture Coding Extension
546      */
547     fprintf( stderr, "picture1\n" );
548     NextStartCode( p_vpar );
549     if( ShowBits( &p_vpar->bit_stream, 16 ) == EXTENSION_START_CODE )
550     {
551         /* Parse picture_coding_extension */
552         DumpBits32( &p_vpar->bit_stream );
553         /* extension_start_code_identifier */
554         DumpBits( &p_vpar->bit_stream, 4 );
555         
556         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
557         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
558         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
559         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
560         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
561         i_structure = GetBits( &p_vpar->bit_stream, 2 );
562         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
563         p_vpar->picture.b_frame_pred_frame_dct
564              = GetBits( &p_vpar->bit_stream, 1 );
565         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
566         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
567         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
568         p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
569         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
570         /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
571          * the length of the picture_display_extension structure.
572          * chroma_420_type (obsolete) */
573         DumpBits( &p_vpar->bit_stream, 1 );
574         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
575         
576         /* composite_display_flag */
577         if( GetBits( &p_vpar->bit_stream, 1 ) )
578         {
579             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
580              * sub_carrier_phase */
581             DumpBits( &p_vpar->bit_stream, 20 );
582         }
583     }
584     else
585     {
586         /* MPEG-1 compatibility flags */
587         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
588         i_structure = FRAME_STRUCTURE;
589         p_vpar->picture.b_frame_pred_frame_dct = 1;
590         p_vpar->picture.b_concealment_mv = 0;
591         p_vpar->picture.b_q_scale_type = 0;
592         p_vpar->picture.b_intra_vlc_format = 0;
593         p_vpar->picture.b_alternate_scan = 0; /* zigzag */
594         p_vpar->picture.b_repeat_first_field = 0;
595         p_vpar->picture.b_progressive_frame = 1;
596     }
597
598     if( p_vpar->picture.i_current_structure &&
599         (i_structure == FRAME_STRUCTURE ||
600          i_structure == p_vpar->picture.i_current_structure) )
601     {
602         /* We don't have the second field of the buffered frame. */
603         if( p_vpar->picture.p_picture != NULL )
604         {
605             ReferenceReplace( p_vpar,
606                       p_vpar->picture.i_coding_type,
607                       NULL );
608
609             for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
610             {
611                 vpar_DestroyMacroblock( &p_vpar->vfifo,
612                                         p_vpar->picture.pp_mb[i_mb] );
613             }
614             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
615         }
616         
617         p_vpar->picture.i_current_structure = 0;
618
619         intf_DbgMsg("vpar debug: odd number of field picture.");
620     }
621
622     if( p_vpar->picture.i_current_structure )
623     {
624         /* Second field of a frame. We will decode it if, and only if we
625          * have decoded the first frame. */
626         b_parsable = (p_vpar->picture.p_picture != NULL);
627     }
628     else
629     {
630         /* Do we have the reference pictures ? */
631         b_parsable = !((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
632                        (p_vpar->sequence.p_forward == NULL)) ||
633                       ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
634                        (p_vpar->sequence.p_forward == NULL ||
635                         p_vpar->sequence.p_backward == NULL));
636
637         if( b_parsable )
638         {
639             /* Does synchro say we have enough time to decode it ? */
640             b_parsable = vpar_SynchroChoose( p_vpar,
641                                p_vpar->picture.i_coding_type, i_structure );
642         }
643     }
644
645     if( !b_parsable )
646     {
647         /* Update the reference pointers. */
648         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
649         
650         /* Warn Synchro we have trashed a picture. */
651         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
652
653         /* Update context. */
654         if( i_structure != FRAME_STRUCTURE )
655             p_vpar->picture.i_current_structure = i_structure;
656         p_vpar->picture.p_picture = NULL;
657
658         return;
659     }
660
661     /* OK, now we are sure we will decode the picture. */
662 #define P_picture p_vpar->picture.p_picture
663     p_vpar->picture.b_error = 0;
664
665     if( !p_vpar->picture.i_current_structure )
666     {
667         /* This is a new frame. Get a structure from the video_output. */
668         P_picture = vout_CreatePicture( p_vpar->p_vout,
669                                         99+p_vpar->sequence.i_chroma_format, /*???*/
670                                         p_vpar->sequence.i_width,
671                                         p_vpar->sequence.i_height,
672                                         p_vpar->sequence.i_width*sizeof(yuv_data_t) );
673
674         /* Initialize values. */
675         P_picture->date = vpar_SynchroDecode( p_vpar,
676                                               p_vpar->picture.i_coding_type,
677                                               i_structure );
678         p_vpar->picture.i_l_stride = - 8 + ( p_vpar->sequence.i_width
679                     << ( 1 - p_vpar->picture.b_frame_structure ) );
680         p_vpar->picture.i_c_stride = -8 + ( p_vpar->sequence.i_width
681                     << (( 1 - p_vpar->picture.b_frame_structure ) +
682                         ( 3 - p_vpar->sequence.i_chroma_format )) );
683
684         /* Update the reference pointers. */
685         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
686     }
687     p_vpar->picture.i_current_structure |= i_structure;
688     p_vpar->picture.i_structure = i_structure;
689     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
690
691     /* Initialize picture data for decoding. */
692     if( i_structure == BOTTOM_FIELD )
693     {
694         i_mb_base = p_vpar->sequence.i_mb_size >> 1;
695         p_vpar->mb.i_l_y = 16;
696         p_vpar->mb.i_c_y = p_vpar->sequence.i_chroma_mb_height;
697     }
698     else
699     {
700         i_mb_base = 0;
701         p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
702     }
703     i_mb_address = 0;
704     p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
705
706     /* Extension and User data. */
707     ExtensionAndUserData( p_vpar );
708
709     /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
710     NextStartCode( p_vpar );
711     while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
712            && !p_vpar->picture.b_error)
713     {
714         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
715                  < SLICE_START_CODE_MIN) ||
716             (i_dummy > SLICE_START_CODE_MAX) )
717         {
718             intf_DbgMsg("vpar debug: premature end of picture");
719             p_vpar->picture.b_error = 1;
720             break;
721         }
722         DumpBits32( &p_vpar->bit_stream );
723         
724         /* Decode slice data. */
725         SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
726     }
727     
728     /* Link referenced pictures for the decoder 
729      * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
730     if( p_vpar->sequence.p_forward != NULL )
731     {
732             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
733     }
734     if( p_vpar->sequence.p_backward != NULL )
735     {
736         vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
737     }
738     
739     if( p_vpar->picture.b_error )
740     {
741         /* Trash picture. */
742         for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
743         {
744             vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
745         }
746
747         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
748         vout_DestroyPicture( p_vpar->p_vout, P_picture );
749
750         /* Unlink referenced pictures */
751         if(  p_vpar->sequence.p_forward != NULL  )
752         {
753                 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
754         }
755         if( p_vpar->sequence.p_backward != NULL )
756         {
757             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
758         }
759
760         /* Prepare context for the next picture. */
761         P_picture = NULL;
762     }
763     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
764     {
765         /* Frame completely parsed. */
766         P_picture->i_deccount = p_vpar->sequence.i_mb_size;
767         for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
768         {
769             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
770         }
771
772         /* Prepare context for the next picture. */
773         P_picture = NULL;
774     }
775 #undef P_picture
776 }
777
778 /*****************************************************************************
779  * SliceHeader : Parse the next slice structure
780  *****************************************************************************/
781 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
782                                     int * pi_mb_address, int i_mb_base,
783                                     u32 i_vert_code )
784 {
785     /* DC predictors initialization table */
786     static int              pi_dc_dct_reinit[4] = {128,256,512,1024};
787
788     int                     i_mb_address_save = *pi_mb_address;
789
790     /* slice_vertical_position_extension and priority_breakpoint already done */
791     LoadQuantizerScale( p_vpar );
792
793     if( GetBits( &p_vpar->bit_stream, 1 ) )
794     {
795         /* intra_slice, slice_id */
796         DumpBits( &p_vpar->bit_stream, 8 );
797         /* extra_information_slice */
798         while( GetBits( &p_vpar->bit_stream, 1 ) )
799         {
800             DumpBits( &p_vpar->bit_stream, 8 );
801         }
802     }
803
804     *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
805
806     /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
807      * does the reference decoder put 0 instead of the normative values ? */
808     p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
809         = p_vpar->slice.pi_dc_dct_pred[2]
810         = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
811
812     /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
813     memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
814
815     do
816     {
817         vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
818                               i_mb_base );
819         i_mb_address_save = *pi_mb_address;
820     }
821     while( !ShowBits( &p_vpar->bit_stream, 23 ) );
822 }
823
824 /*****************************************************************************
825  * SliceHeaderXY : Parse the next slice structure
826  *****************************************************************************
827  * X = i_height > 2800 ?
828  * Y = scalable_mode == SC_DP ?
829  *****************************************************************************/
830 static void SliceHeader00( vpar_thread_t * p_vpar,
831                            int * pi_mb_address, int i_mb_base,
832                            u32 i_vert_code )
833 {
834     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
835 }
836
837 static void SliceHeader01( vpar_thread_t * p_vpar,
838                            int * pi_mb_address, int i_mb_base,
839                            u32 i_vert_code )
840 {
841     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
842     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
843 }
844
845 static void SliceHeader10( vpar_thread_t * p_vpar,
846                            int * pi_mb_address, int i_mb_base,
847                            u32 i_vert_code )
848 {
849     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
850     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
851 }
852
853 static void SliceHeader11( vpar_thread_t * p_vpar,
854                            int * pi_mb_address, int i_mb_base,
855                            u32 i_vert_code )
856 {
857     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
858     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
859     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
860 }
861
862 /*****************************************************************************
863  * ExtensionAndUserData : Parse the extension_and_user_data structure
864  *****************************************************************************/
865 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
866 {
867     while( !p_vpar->b_die )
868     {
869         NextStartCode( p_vpar );
870         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
871         {
872         case EXTENSION_START_CODE:
873             DumpBits32( &p_vpar->bit_stream );
874             switch( GetBits( &p_vpar->bit_stream, 4 ) )
875             {
876             case SEQUENCE_DISPLAY_EXTENSION_ID:
877                 SequenceDisplayExtension( p_vpar );
878                 break;
879             case QUANT_MATRIX_EXTENSION_ID:
880                 QuantMatrixExtension( p_vpar );
881                 break;
882             case SEQUENCE_SCALABLE_EXTENSION_ID:
883                 SequenceScalableExtension( p_vpar );
884                 break;
885             case PICTURE_DISPLAY_EXTENSION_ID:
886                 PictureDisplayExtension( p_vpar );
887                 break;
888             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
889                 PictureSpatialScalableExtension( p_vpar );
890                 break;
891             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
892                 PictureTemporalScalableExtension( p_vpar );
893                 break;
894             case COPYRIGHT_EXTENSION_ID:
895                 CopyrightExtension( p_vpar );
896                 break;
897             default:
898             }
899             break;
900
901         case USER_DATA_START_CODE:
902             DumpBits32( &p_vpar->bit_stream );
903             /* Wait for the next start code */
904             break;
905
906         default:
907             return;
908         }
909     }
910 }
911
912
913 /*****************************************************************************
914  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
915  *****************************************************************************/
916
917 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
918 {
919     /* We don't care sequence_display_extension. */
920     /* video_format */
921     DumpBits( &p_vpar->bit_stream, 3 );
922     if( GetBits( &p_vpar->bit_stream, 1 ) )
923     {
924         /* Three bytes for color_desciption */
925         DumpBits( &p_vpar->bit_stream, 24 );
926     }
927     /* display_horizontal and vertical_size and a marker_bit */
928     DumpBits( &p_vpar->bit_stream, 29 );
929 }
930
931
932 /*****************************************************************************
933  * QuantMatrixExtension : Load quantization matrices for luminance           *
934  *                        and chrominance                                    *
935  *****************************************************************************/
936
937 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
938 {
939     if( GetBits( &p_vpar->bit_stream, 1 ) )
940     {
941         /* Load intra_quantiser_matrix for luminance. */
942         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
943     }
944     else
945     {
946         /* Use the default matrix. */
947         LinkMatrix( &p_vpar->sequence.intra_quant,
948                     pi_default_intra_quant );
949     }
950     if( GetBits( &p_vpar->bit_stream, 1 ) )
951     {
952         /* Load non_intra_quantiser_matrix for luminance. */
953         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
954     }
955     else
956     {
957         /* Use the default matrix. */
958         LinkMatrix( &p_vpar->sequence.nonintra_quant,
959                     pi_default_nonintra_quant );
960     }
961     if( GetBits( &p_vpar->bit_stream, 1 ) )
962     {
963         /* Load intra_quantiser_matrix for chrominance. */
964         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
965     }
966     else
967     {
968         /* Link the chrominance intra matrix to the luminance one. */
969         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
970                     p_vpar->sequence.intra_quant.pi_matrix );
971     }
972     if( GetBits( &p_vpar->bit_stream, 1 ) )
973     {
974         /* Load non_intra_quantiser_matrix for chrominance. */
975         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
976     }
977     else
978     {
979         /* Link the chrominance intra matrix to the luminance one. */
980         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
981                     p_vpar->sequence.intra_quant.pi_matrix );
982     }
983     if( GetBits( &p_vpar->bit_stream, 1 ) )
984     {
985         /* Load non_intra_quantiser_matrix for chrominance. */
986         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
987     }
988     else
989     {
990         /* Link the chrominance nonintra matrix to the luminance one. */
991         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
992                     p_vpar->sequence.nonintra_quant.pi_matrix );
993     }
994 }
995
996
997 /*****************************************************************************
998  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
999  *                             structure to handle scalable coding           *
1000  *****************************************************************************/
1001
1002 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1003 {
1004     /* We don't care about anything scalable except the scalable mode. */
1005     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1006     /* The length of the structure depends on the value of the scalable_mode */
1007     {
1008         case 1:
1009             DumpBits32( &p_vpar->bit_stream );
1010             DumpBits( &p_vpar->bit_stream, 21 );
1011             break;
1012         case 2:
1013             DumpBits( &p_vpar->bit_stream, 12 );
1014             break;
1015         default:
1016             DumpBits( &p_vpar->bit_stream, 4 );
1017     }
1018
1019 }
1020 /*****************************************************************************
1021  * PictureDisplayExtension : Parse the picture_display_extension structure   *
1022  *****************************************************************************/
1023
1024 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1025 {
1026     /* Number of frame center offset */
1027     int nb;
1028     /* I am not sure it works but it should
1029         (fewer tests than shown in ยง6.3.12) */
1030     nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1031                                           p_vpar->picture.b_repeat_first_field +
1032                                           p_vpar->picture.b_top_field_first
1033                          : ( p_vpar->picture.b_frame_structure + 1 ) +
1034                            p_vpar->picture.b_repeat_first_field;
1035     DumpBits( &p_vpar->bit_stream, 34 * nb );
1036 }
1037
1038
1039 /*****************************************************************************
1040  * PictureSpatialScalableExtension                                           *
1041  *****************************************************************************/
1042
1043 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1044 {
1045     /* That's scalable, so we trash it */
1046     DumpBits32( &p_vpar->bit_stream );
1047     DumpBits( &p_vpar->bit_stream, 16 );
1048 }
1049
1050
1051 /*****************************************************************************
1052  * PictureTemporalScalableExtension                                          *
1053  *****************************************************************************/
1054
1055 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1056 {
1057     /* Scalable again, trashed again */
1058     DumpBits( &p_vpar->bit_stream, 23 );
1059 }
1060
1061
1062 /*****************************************************************************
1063  * CopyrightExtension : Keeps some legal informations                        *
1064  *****************************************************************************/
1065
1066 static void CopyrightExtension( vpar_thread_t * p_vpar )
1067 {
1068     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1069     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1070         /* A flag that says whether the copyright information is significant */
1071     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1072         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1073     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1074         /* Reserved bits */
1075     DumpBits( &p_vpar->bit_stream, 8 );
1076         /* The copyright_number is split in three parts */
1077         /* first part */
1078     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1079     DumpBits( &p_vpar->bit_stream, 1 );
1080         /* second part */
1081     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1082     DumpBits( &p_vpar->bit_stream, 1 );
1083         /* third part and sum */
1084     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
1085                                       ( (u64)i_copyright_nb_2 << 22 ) +
1086                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1087 }