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