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