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