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