]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
Synchro de base.
[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
16 #include "config.h"
17 #include "common.h"
18 #include "mtime.h"
19 #include "vlc_thread.h"
20
21 #include "intf_msg.h"
22 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
23
24 #include "input.h"
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
27 #include "video.h"
28 #include "video_output.h"
29
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
33
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "video_fifo.h"
39
40 /*
41  * Function pointer
42  */
43 typedef void    (*f_picture_data_t)( vpar_thread_t*, int );
44
45 /*
46  * Local prototypes
47  */
48 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
49 static void SequenceHeader( vpar_thread_t * p_vpar );
50 static void GroupHeader( vpar_thread_t * p_vpar );
51 static void PictureHeader( vpar_thread_t * p_vpar );
52 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
53 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
54 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
55 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
56 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
57 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
58 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
59 static void CopyrightExtension( vpar_thread_t * p_vpar );
60
61 /*
62  * Standard variables
63  */
64
65 /*****************************************************************************
66  * pi_default_intra_quant : default quantization matrix
67  *****************************************************************************/
68 #ifndef VDEC_DFT
69 int pi_default_intra_quant[] =
70 {
71     8,  16, 19, 22, 26, 27, 29, 34,
72     16, 16, 22, 24, 27, 29, 34, 37,
73     19, 22, 26, 27, 29, 34, 34, 38,
74     22, 22, 26, 27, 29, 34, 37, 40,
75     22, 26, 27, 29, 32, 35, 40, 48,
76     26, 27, 29, 32, 35, 40, 48, 58,
77     26, 27, 29, 34, 38, 46, 56, 69,
78     27, 29, 35, 38, 46, 56, 69, 83
79 };      
80 #else
81 int pi_default_intra_quant[] =
82 {
83     2048,   5681,   6355,   6623,   6656,   5431,   4018,   2401,
84     5681,   7880,   10207,  10021,  9587,   8091,   6534,   3625,
85     6355,   10207,  11363,  10619,  9700,   8935,   6155,   3507,
86     6623,   9186,   10226,  9557,   8730,   8041,   6028,   3322,
87     5632,   9232,   9031,   8730,   8192,   7040,   5542,   3390,
88     5230,   7533,   7621,   7568,   7040,   6321,   5225,   3219,
89     3602,   5189,   5250,   5539,   5265,   5007,   4199,   2638,
90     1907,   2841,   3230,   3156,   3249,   3108,   2638,   1617        
91 };
92 #endif
93
94 /*****************************************************************************
95  * pi_default_nonintra_quant : default quantization matrix
96  *****************************************************************************/
97 #ifndef VDEC_DFT
98 int pi_default_nonintra_quant[] =
99 {
100     16, 16, 16, 16, 16, 16, 16, 16,
101     16, 16, 16, 16, 16, 16, 16, 16,
102     16, 16, 16, 16, 16, 16, 16, 16,
103     16, 16, 16, 16, 16, 16, 16, 16,
104     16, 16, 16, 16, 16, 16, 16, 16,
105     16, 16, 16, 16, 16, 16, 16, 16,
106     16, 16, 16, 16, 16, 16, 16, 16,
107     16, 16, 16, 16, 16, 16, 16, 16
108 };
109 #else
110 int pi_default_nonintra_quanit[] =
111 {
112     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
113     5680,   7888,   7424,   6688,   5680,   4464,   3072,   1568,
114     5344,   7424,   6992,   6288,   5344,   4208,   2896,   1472,
115     4816,   6688,   6288,   5664,   4816,   3792,   2608,   1328,
116     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
117     3216,   4464,   4208,   3792,   3216,   2528,   1744,   880,
118     2224,   3072,   2896,   2608,   2224,   1744,   1200,   608,
119     1136,   1568,   1472,   1328,   1136,   880,    608,    304 
120 };
121 #endif
122
123 /*****************************************************************************
124  * pi_scan : zig-zag and alternate scan patterns
125  *****************************************************************************/
126 u8 pi_scan[2][64] =
127 {
128     { /* Zig-Zag pattern */
129         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
130         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
131         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
132         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
133     },
134     { /* Alternate scan pattern */
135         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
136         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
137         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
138         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
139     }
140 };
141
142 /*
143  * Local inline functions.
144  */
145
146 /*****************************************************************************
147  * ReferenceUpdate : Update the reference pointers when we have a new picture
148  *****************************************************************************/
149 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
150                                         int i_coding_type,
151                                         picture_t * p_newref )
152 {
153     if( i_coding_type != B_CODING_TYPE )
154     {
155         if( p_vpar->sequence.p_forward != NULL )
156             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
157         if( p_vpar->sequence.p_backward != NULL )
158         {
159 #if 0
160             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
161                               vpar_SynchroDate( p_vpar ) );
162 #else
163             mtime_t     date;
164             date = vpar_SynchroDate( p_vpar );
165             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
166                               date );
167             if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
168                 vpar_SynchroKludge( p_vpar, date );
169 #endif
170         }
171         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
172         p_vpar->sequence.p_backward = p_newref;
173         if( p_newref != NULL )
174             vout_LinkPicture( p_vpar->p_vout, p_newref );
175 #if 1
176         p_vpar->synchro.i_coding_type = i_coding_type;
177 #endif
178     }
179     else if( p_newref != NULL )
180     {
181         /* Put date immediately. */
182         vout_DatePicture( p_vpar->p_vout, p_newref,
183                           vpar_SynchroDate( p_vpar ) );
184     }
185 }
186
187 /*****************************************************************************
188  * ReferenceReplace : Replace the last reference pointer when we destroy
189  *                    a picture
190  *****************************************************************************/
191 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
192                                          int i_coding_type,
193                                          picture_t * p_newref )
194 {
195     if( i_coding_type != B_CODING_TYPE )
196     {
197         if( p_vpar->sequence.p_backward != NULL )
198             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
199         p_vpar->sequence.p_backward = p_newref;
200         if( p_newref != NULL )
201             vout_LinkPicture( p_vpar->p_vout, p_newref );
202     }
203 }
204
205 /*****************************************************************************
206  * LoadMatrix : Load a quantization matrix
207  *****************************************************************************/
208 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
209 {
210     int i_dummy;
211     
212     if( !p_matrix->b_allocated )
213     {
214         /* Allocate a piece of memory to load the matrix. */
215         if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
216         {
217             intf_ErrMsg("vpar error: allocation error in LoadMatrix()\n");
218             p_vpar->b_error = 1;
219             return;
220         }
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
386         /* Turn the MPEG2 flag on */
387         p_vpar->sequence.b_mpeg2 = 1;
388     
389         /* Parse sequence_extension */
390         RemoveBits32( &p_vpar->bit_stream );
391         /* extension_start_code_identifier, profile_and_level_indication */
392         RemoveBits( &p_vpar->bit_stream, 12 );
393         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
394         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
395         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
396         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
397         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
398         RemoveBits( &p_vpar->bit_stream, 22 );
399         /* frame_rate_extension_n */
400         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
401         /* frame_rate_extension_d */
402         p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
403                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
404     }
405     else
406     {
407         /* It's an MPEG-1 stream. Put adequate parameters. */
408
409         p_vpar->sequence.b_mpeg2 = 0;
410         p_vpar->sequence.b_progressive = 1;
411         p_vpar->sequence.i_chroma_format = CHROMA_420;
412     }
413
414     /* Update sizes */
415     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
416     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
417                                    (p_vpar->sequence.i_height + 15) / 16 :
418                                    2 * ((p_vpar->sequence.i_height + 31) / 32);
419     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
420                                         * p_vpar->sequence.i_mb_height;
421     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
422     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
423     p_vpar->sequence.i_size = p_vpar->sequence.i_width
424                                         * p_vpar->sequence.i_height;
425
426     /* Update chromatic information. */
427     switch( p_vpar->sequence.i_chroma_format )
428     {
429     case CHROMA_420:
430         p_vpar->sequence.i_chroma_nb_blocks = 2;
431         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
432         p_vpar->sequence.i_chroma_mb_width = 8;
433         p_vpar->sequence.i_chroma_mb_height = 8;
434         break;
435
436     case CHROMA_422:
437         p_vpar->sequence.i_chroma_nb_blocks = 4;
438         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
439         p_vpar->sequence.i_chroma_mb_width = 8;
440         p_vpar->sequence.i_chroma_mb_height = 16;
441         break;
442
443     case CHROMA_444:
444         p_vpar->sequence.i_chroma_nb_blocks = 8;
445         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
446         p_vpar->sequence.i_chroma_mb_width = 16;
447         p_vpar->sequence.i_chroma_mb_height = 16;
448     }
449
450     /* Reset scalable_mode. */
451     p_vpar->sequence.i_scalable_mode = SC_NONE;
452
453 #if 0
454     if(    p_vpar->sequence.i_width != i_width_save
455         || p_vpar->sequence.i_height != i_height_save )
456     {
457          /* What do we do in case of a size change ??? */
458     }
459 #endif
460
461     /* Extension and User data */
462     ExtensionAndUserData( p_vpar );
463 }
464
465 /*****************************************************************************
466  * GroupHeader : Parse the next group of pictures header
467  *****************************************************************************/
468 static void GroupHeader( vpar_thread_t * p_vpar )
469 {
470     /* Nothing to do, we don't care. */
471     RemoveBits( &p_vpar->bit_stream, 27 );
472     ExtensionAndUserData( p_vpar );
473 }
474
475 /*****************************************************************************
476  * PictureHeader : Parse the next picture header
477  *****************************************************************************/
478 static void PictureHeader( vpar_thread_t * p_vpar )
479 {
480     /* Table of optimized PictureData functions. */
481     static f_picture_data_t ppf_picture_data[4][4] =
482     {
483         {
484             NULL, NULL, NULL, NULL
485         },
486         {
487             /* TOP_FIELD */
488 #if (VPAR_OPTIM_LEVEL > 1)
489             NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
490             vpar_PictureData2B420TZ
491 #else
492             NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
493             vpar_PictureDataGENERIC
494 #endif
495         },
496         {
497             /* BOTTOM_FIELD */
498 #if (VPAR_OPTIM_LEVEL > 1)
499             NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
500             vpar_PictureData2B420BZ
501 #else
502             NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
503             vpar_PictureDataGENERIC
504 #endif
505         },
506         {
507             /* FRAME_PICTURE */
508 #if (VPAR_OPTIM_LEVEL > 0)
509             NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
510             vpar_PictureData2B420F0
511 #else
512             NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
513             vpar_PictureDataGENERIC
514 #endif
515         }
516     };
517
518     int                 i_structure;
519     int                 i_mb_base;
520     boolean_t           b_parsable;
521 #ifdef VDEC_SMP
522     int                 i_mb;
523 #endif
524     
525     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
526     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
527     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
528
529     if( p_vpar->picture.i_coding_type == P_CODING_TYPE
530         || p_vpar->picture.i_coding_type == B_CODING_TYPE )
531     {
532         p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
533         p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
534     }
535     if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
536     {
537         p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
538         p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
539     }
540
541     /* extra_information_picture */
542     while( GetBits( &p_vpar->bit_stream, 1 ) )
543     {
544         RemoveBits( &p_vpar->bit_stream, 8 );
545     }
546
547     /* 
548      * Picture Coding Extension
549      */
550     NextStartCode( p_vpar );
551     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
552     {
553         /* Parse picture_coding_extension */
554         RemoveBits32( &p_vpar->bit_stream );
555         /* extension_start_code_identifier */
556         RemoveBits( &p_vpar->bit_stream, 4 );
557         
558         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
559         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
560         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
561         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
562         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
563         i_structure = GetBits( &p_vpar->bit_stream, 2 );
564         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
565         p_vpar->picture.b_frame_pred_frame_dct
566              = GetBits( &p_vpar->bit_stream, 1 );
567         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
568         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
569         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
570         p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
571         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
572         /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
573          * the length of the picture_display_extension structure.
574          * chroma_420_type (obsolete) */
575         RemoveBits( &p_vpar->bit_stream, 1 );
576         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
577         
578         /* composite_display_flag */
579         if( GetBits( &p_vpar->bit_stream, 1 ) )
580         {
581             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
582              * sub_carrier_phase */
583             RemoveBits( &p_vpar->bit_stream, 20 );
584         }
585     }
586     else
587     {
588         /* MPEG-1 compatibility flags */
589         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
590         i_structure = FRAME_STRUCTURE;
591         p_vpar->picture.b_frame_pred_frame_dct = 1;
592         p_vpar->picture.b_concealment_mv = 0;
593         p_vpar->picture.b_q_scale_type = 0;
594         p_vpar->picture.b_intra_vlc_format = 0;
595         p_vpar->picture.b_alternate_scan = 0; /* zigzag */
596         p_vpar->picture.b_repeat_first_field = 0;
597         p_vpar->picture.b_progressive_frame = 1;
598     }
599
600     if( p_vpar->picture.i_current_structure &&
601         (i_structure == FRAME_STRUCTURE ||
602          i_structure == p_vpar->picture.i_current_structure) )
603     {
604         /* We don't have the second field of the buffered frame. */
605         if( p_vpar->picture.p_picture != NULL )
606         {
607             ReferenceReplace( p_vpar,
608                       p_vpar->picture.i_coding_type,
609                       NULL );
610
611 #ifdef VDEC_SMP
612             for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
613             {
614                 vpar_DestroyMacroblock( &p_vpar->vfifo,
615                                         p_vpar->picture.pp_mb[i_mb] );
616             }
617 #endif
618             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
619         }
620         
621         p_vpar->picture.i_current_structure = 0;
622
623         intf_DbgMsg("vpar debug: odd number of field picture.\n");
624     }
625
626     /* Do we have the reference pictures ? */
627     b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
628                     (p_vpar->sequence.p_backward == NULL)) ||
629                      /* p_backward will become p_forward later */
630                    ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
631                     (p_vpar->sequence.p_forward == NULL ||
632                      p_vpar->sequence.p_backward == NULL)));
633
634     if( b_parsable )
635     {
636         if( p_vpar->picture.i_current_structure )
637         {
638             /* Second field of a frame. We will decode it if, and only if we
639             * have decoded the first field. */
640             b_parsable = (p_vpar->picture.p_picture != NULL);
641         }
642         else
643         {
644             /* Does synchro say we have enough time to decode it ? */
645             b_parsable = vpar_SynchroChoose( p_vpar,
646                                p_vpar->picture.i_coding_type, i_structure );
647         }
648     }
649
650     if( !b_parsable )
651     {
652         /* Update the reference pointers. */
653         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
654         
655         /* Warn Synchro we have trashed a picture. */
656         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
657
658         /* Update context. */
659         if( i_structure != FRAME_STRUCTURE )
660             p_vpar->picture.i_current_structure = i_structure;
661         p_vpar->picture.p_picture = NULL;
662
663         return;
664     }
665
666     /* OK, now we are sure we will decode the picture. */
667 #define P_picture p_vpar->picture.p_picture
668     p_vpar->picture.b_error = 0;
669     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
670
671     if( !p_vpar->picture.i_current_structure )
672     {
673         /* This is a new frame. Get a structure from the video_output. */
674         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
675                                         99+p_vpar->sequence.i_chroma_format, /*???*/
676                                         p_vpar->sequence.i_width,
677                                         p_vpar->sequence.i_height ) )
678              == NULL )
679         {
680             intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
681             if( p_vpar->b_die || p_vpar->b_error )
682             {
683                 return;
684             }
685             msleep( VPAR_OUTMEM_SLEEP );
686         }
687
688         /* Initialize values. */
689         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
690         P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
691         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
692         p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
693                     << ( 1 - p_vpar->picture.b_frame_structure ) );
694         p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
695                     << ( 1 - p_vpar->picture.b_frame_structure ));
696
697         P_picture->i_deccount = p_vpar->sequence.i_mb_size;
698         vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
699 #ifdef VDEC_SMP
700         memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
701 #endif
702 /* FIXME ! remove asap */
703 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
704
705         /* Update the reference pointers. */
706         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
707
708 #ifdef VDEC_SMP
709         /* Link referenced pictures for the decoder 
710          * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
711         if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
712             p_vpar->picture.i_coding_type == B_CODING_TYPE )
713         {
714             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
715         }
716         if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
717         {
718             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
719         } 
720 #endif
721     }
722     p_vpar->picture.i_current_structure |= i_structure;
723     p_vpar->picture.i_structure = i_structure;
724
725     /* Initialize picture data for decoding. */
726     if( i_structure == BOTTOM_FIELD )
727     {
728         i_mb_base = p_vpar->sequence.i_mb_size >> 1;
729         p_vpar->mb.i_l_y = 1;
730         p_vpar->mb.i_c_y = 1;
731     }
732     else
733     {
734         i_mb_base = 0;
735         p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
736     }
737     p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
738
739     /* Extension and User data. */
740     ExtensionAndUserData( p_vpar );
741
742     /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
743     if( p_vpar->sequence.i_chroma_format != CHROMA_420
744         || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
745         || p_vpar->sequence.i_scalable_mode == SC_DP )
746     {
747         /* Weird stream. Use the slower generic function. */
748         vpar_PictureDataGENERIC( p_vpar, i_mb_base );
749     }
750     else
751     {
752         /* Try to find an optimized function. */
753         ppf_picture_data[p_vpar->picture.i_structure]
754                         [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
755     }
756
757     if( p_vpar->b_die || p_vpar->b_error )
758     {
759         return;
760     }
761
762     if( p_vpar->picture.b_error )
763     {
764         /* Trash picture. */
765 //fprintf(stderr, "Image trashee\n");
766 #ifdef VDEC_SMP
767         for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
768         {
769             vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
770         }
771 #endif
772
773         if( P_picture->i_deccount != 1 )
774         {
775             vout_DestroyPicture( p_vpar->p_vout, P_picture );
776         }
777
778         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
779
780         /* Prepare context for the next picture. */
781         P_picture = NULL;
782         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
783             p_vpar->picture.i_current_structure = 0;
784     }
785     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
786     {
787 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
788         /* Frame completely parsed. */
789 #ifdef VDEC_SMP
790         for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
791         {
792             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
793         }
794
795         /* Send signal to the video_decoder. */
796         vlc_mutex_lock( &p_vpar->vfifo.lock );
797         vlc_cond_signal( &p_vpar->vfifo.wait );
798         vlc_mutex_unlock( &p_vpar->vfifo.lock );
799 #endif
800
801         /* Prepare context for the next picture. */
802         P_picture = NULL;
803         p_vpar->picture.i_current_structure = 0;
804     }
805 #undef P_picture
806 }
807
808 /*****************************************************************************
809  * ExtensionAndUserData : Parse the extension_and_user_data structure
810  *****************************************************************************/
811 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
812 {
813     while( !p_vpar->b_die )
814     {
815         NextStartCode( p_vpar );
816         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
817         {
818         case EXTENSION_START_CODE:
819             RemoveBits32( &p_vpar->bit_stream );
820             switch( GetBits( &p_vpar->bit_stream, 4 ) )
821             {
822             case SEQUENCE_DISPLAY_EXTENSION_ID:
823                 SequenceDisplayExtension( p_vpar );
824                 break;
825             case QUANT_MATRIX_EXTENSION_ID:
826                 QuantMatrixExtension( p_vpar );
827                 break;
828             case SEQUENCE_SCALABLE_EXTENSION_ID:
829                 SequenceScalableExtension( p_vpar );
830                 break;
831             case PICTURE_DISPLAY_EXTENSION_ID:
832                 PictureDisplayExtension( p_vpar );
833                 break;
834             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
835                 PictureSpatialScalableExtension( p_vpar );
836                 break;
837             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
838                 PictureTemporalScalableExtension( p_vpar );
839                 break;
840             case COPYRIGHT_EXTENSION_ID:
841                 CopyrightExtension( p_vpar );
842                 break;
843             default:
844             }
845             break;
846
847         case USER_DATA_START_CODE:
848             RemoveBits32( &p_vpar->bit_stream );
849             /* Wait for the next start code */
850             break;
851
852         default:
853             return;
854         }
855     }
856 }
857
858
859 /*****************************************************************************
860  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
861  *****************************************************************************/
862
863 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
864 {
865     /* We don't care sequence_display_extension. */
866     /* video_format */
867     RemoveBits( &p_vpar->bit_stream, 3 );
868     if( GetBits( &p_vpar->bit_stream, 1 ) )
869     {
870         /* Two bytes for color_desciption */
871         RemoveBits( &p_vpar->bit_stream, 16 );
872         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
873     }
874     /* display_horizontal and vertical_size and a marker_bit */
875     RemoveBits( &p_vpar->bit_stream, 29 );
876 }
877
878
879 /*****************************************************************************
880  * QuantMatrixExtension : Load quantization matrices for luminance           *
881  *                        and chrominance                                    *
882  *****************************************************************************/
883
884 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
885 {
886     if( GetBits( &p_vpar->bit_stream, 1 ) )
887     {
888         /* Load intra_quantiser_matrix for luminance. */
889         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
890     }
891     else
892     {
893         /* Use the default matrix. */
894         LinkMatrix( &p_vpar->sequence.intra_quant,
895                     pi_default_intra_quant );
896     }
897     if( GetBits( &p_vpar->bit_stream, 1 ) )
898     {
899         /* Load non_intra_quantiser_matrix for luminance. */
900         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
901     }
902     else
903     {
904         /* Use the default matrix. */
905         LinkMatrix( &p_vpar->sequence.nonintra_quant,
906                     pi_default_nonintra_quant );
907     }
908     if( GetBits( &p_vpar->bit_stream, 1 ) )
909     {
910         /* Load intra_quantiser_matrix for chrominance. */
911         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
912     }
913     else
914     {
915         /* Link the chrominance intra matrix to the luminance one. */
916         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
917                     p_vpar->sequence.intra_quant.pi_matrix );
918     }
919     if( GetBits( &p_vpar->bit_stream, 1 ) )
920     {
921         /* Load non_intra_quantiser_matrix for chrominance. */
922         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
923     }
924     else
925     {
926         /* Link the chrominance intra matrix to the luminance one. */
927         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
928                     p_vpar->sequence.intra_quant.pi_matrix );
929     }
930     if( GetBits( &p_vpar->bit_stream, 1 ) )
931     {
932         /* Load non_intra_quantiser_matrix for chrominance. */
933         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
934     }
935     else
936     {
937         /* Link the chrominance nonintra matrix to the luminance one. */
938         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
939                     p_vpar->sequence.nonintra_quant.pi_matrix );
940     }
941 }
942
943
944 /*****************************************************************************
945  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
946  *                             structure to handle scalable coding           *
947  *****************************************************************************/
948
949 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
950 {
951     /* We don't care about anything scalable except the scalable mode. */
952     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
953     /* The length of the structure depends on the value of the scalable_mode */
954     {
955         case 1:
956             RemoveBits32( &p_vpar->bit_stream );
957             RemoveBits( &p_vpar->bit_stream, 21 );
958             break;
959         case 2:
960             RemoveBits( &p_vpar->bit_stream, 12 );
961             break;
962         default:
963             RemoveBits( &p_vpar->bit_stream, 4 );
964     }
965
966 }
967 /*****************************************************************************
968  * PictureDisplayExtension : Parse the picture_display_extension structure   *
969  *****************************************************************************/
970
971 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
972 {
973     /* Number of frame center offset */
974     int i_nb, i_dummy;
975     /* I am not sure it works but it should
976         (fewer tests than shown in ยง6.3.12) */
977     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
978                                             p_vpar->picture.b_repeat_first_field +
979                                             p_vpar->picture.b_top_field_first
980                            : ( p_vpar->picture.b_frame_structure + 1 ) +
981                              p_vpar->picture.b_repeat_first_field;
982     for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
983     {
984         RemoveBits( &p_vpar->bit_stream, 17 );
985         RemoveBits( &p_vpar->bit_stream, 17 );
986     }
987 }
988
989
990 /*****************************************************************************
991  * PictureSpatialScalableExtension                                           *
992  *****************************************************************************/
993
994 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
995 {
996     /* That's scalable, so we trash it */
997     RemoveBits32( &p_vpar->bit_stream );
998     RemoveBits( &p_vpar->bit_stream, 16 );
999 }
1000
1001
1002 /*****************************************************************************
1003  * PictureTemporalScalableExtension                                          *
1004  *****************************************************************************/
1005
1006 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1007 {
1008     /* Scalable again, trashed again */
1009     RemoveBits( &p_vpar->bit_stream, 23 );
1010 }
1011
1012
1013 /*****************************************************************************
1014  * CopyrightExtension : Keeps some legal informations                        *
1015  *****************************************************************************/
1016
1017 static void CopyrightExtension( vpar_thread_t * p_vpar )
1018 {
1019     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1020     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1021         /* A flag that says whether the copyright information is significant */
1022     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1023         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1024     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1025         /* Reserved bits */
1026     RemoveBits( &p_vpar->bit_stream, 8 );
1027         /* The copyright_number is split in three parts */
1028         /* first part */
1029     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1030     RemoveBits( &p_vpar->bit_stream, 1 );
1031         /* second part */
1032     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1033     RemoveBits( &p_vpar->bit_stream, 1 );
1034         /* third part and sum */
1035     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1036                                       ( (u64)i_copyright_nb_2 << 22 ) |
1037                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1038 }