]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
18c9ccde2f04f92f5c056fcc1bc167d751179fa1
[vlc] / src / video_parser / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *          Stéphane Borel <stef@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <stdlib.h>                                                /* free() */
30 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h>                                            /* "input.h" */
32
33 #include "config.h"
34 #include "common.h"
35 #include "threads.h"
36 #include "mtime.h"
37 #include "plugins.h"
38
39 #include "intf_msg.h"
40
41 #include "input.h"
42 #include "decoder_fifo.h"
43 #include "video.h"
44 #include "video_output.h"
45
46 #include "vdec_idct.h"
47 #include "video_decoder.h"
48 #include "vdec_motion.h"
49
50 #include "vpar_blocks.h"
51 #include "vpar_headers.h"
52 #include "vpar_synchro.h"
53 #include "video_parser.h"
54 #include "video_fifo.h"
55
56 /*
57  * Local prototypes
58  */
59 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
60 static void SequenceHeader( vpar_thread_t * p_vpar );
61 static void GroupHeader( vpar_thread_t * p_vpar );
62 static void PictureHeader( vpar_thread_t * p_vpar );
63 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
64 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
65 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
66 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
67 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
68 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
69 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
70 static void CopyrightExtension( vpar_thread_t * p_vpar );
71
72 /*
73  * Standard variables
74  */
75
76 /*****************************************************************************
77  * pi_default_intra_quant : default quantization matrix
78  *****************************************************************************/
79 #ifndef VDEC_DFT
80 int pi_default_intra_quant[] =
81 {
82     8,  16, 19, 22, 26, 27, 29, 34,
83     16, 16, 22, 24, 27, 29, 34, 37,
84     19, 22, 26, 27, 29, 34, 34, 38,
85     22, 22, 26, 27, 29, 34, 37, 40,
86     22, 26, 27, 29, 32, 35, 40, 48,
87     26, 27, 29, 32, 35, 40, 48, 58,
88     26, 27, 29, 34, 38, 46, 56, 69,
89     27, 29, 35, 38, 46, 56, 69, 83
90 };
91 #else
92 int pi_default_intra_quant[] =
93 {
94     2048,   5681,   6355,   6623,   6656,   5431,   4018,   2401,
95     5681,   7880,   10207,  10021,  9587,   8091,   6534,   3625,
96     6355,   10207,  11363,  10619,  9700,   8935,   6155,   3507,
97     6623,   9186,   10226,  9557,   8730,   8041,   6028,   3322,
98     5632,   9232,   9031,   8730,   8192,   7040,   5542,   3390,
99     5230,   7533,   7621,   7568,   7040,   6321,   5225,   3219,
100     3602,   5189,   5250,   5539,   5265,   5007,   4199,   2638,
101     1907,   2841,   3230,   3156,   3249,   3108,   2638,   1617
102 };
103 #endif
104
105 /*****************************************************************************
106  * pi_default_nonintra_quant : default quantization matrix
107  *****************************************************************************/
108 #ifndef VDEC_DFT
109 int pi_default_nonintra_quant[] =
110 {
111     16, 16, 16, 16, 16, 16, 16, 16,
112     16, 16, 16, 16, 16, 16, 16, 16,
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 };
120 #else
121 int pi_default_nonintra_quanit[] =
122 {
123     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
124     5680,   7888,   7424,   6688,   5680,   4464,   3072,   1568,
125     5344,   7424,   6992,   6288,   5344,   4208,   2896,   1472,
126     4816,   6688,   6288,   5664,   4816,   3792,   2608,   1328,
127     4096,   5680,   5344,   4816,   4096,   3216,   2224,   1136,
128     3216,   4464,   4208,   3792,   3216,   2528,   1744,   880,
129     2224,   3072,   2896,   2608,   2224,   1744,   1200,   608,
130     1136,   1568,   1472,   1328,   1136,   880,    608,    304
131 };
132 #endif
133
134 /*****************************************************************************
135  * pi_scan : zig-zag and alternate scan patterns
136  *****************************************************************************/
137 u8 pi_scan[2][64] =
138 {
139     { /* Zig-Zag pattern */
140         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
141         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
142         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
143         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
144     },
145     { /* Alternate scan pattern */
146         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
147         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
148         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
149         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
150     }
151 };
152
153 /*
154  * Local inline functions.
155  */
156
157 /*****************************************************************************
158  * ReferenceUpdate : Update the reference pointers when we have a new picture
159  *****************************************************************************/
160 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
161                                         int i_coding_type,
162                                         picture_t * p_newref )
163 {
164     if( i_coding_type != B_CODING_TYPE )
165     {
166         if( p_vpar->sequence.p_forward != NULL )
167             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
168         if( p_vpar->sequence.p_backward != NULL )
169         {
170 #ifdef POLUX_SYNCHRO
171             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
172                               vpar_SynchroDate( p_vpar ) );
173 #endif
174 #ifdef SAM_SYNCHRO
175             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
176                               vpar_SynchroDate( p_vpar ) );
177 #endif
178 #ifdef MEUUH_SYNCHRO
179             mtime_t     date;
180             date = vpar_SynchroDate( p_vpar );
181             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
182                               date );
183             if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
184                 vpar_SynchroKludge( p_vpar, date );
185 #endif
186         }
187         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
188         p_vpar->sequence.p_backward = p_newref;
189         if( p_newref != NULL )
190             vout_LinkPicture( p_vpar->p_vout, p_newref );
191 #ifdef MEUUH_SYNCHRO
192         p_vpar->synchro.i_coding_type = i_coding_type;
193 #endif
194     }
195     else if( p_newref != NULL )
196     {
197         /* Put date immediately. */
198         vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
199     }
200 }
201
202 /*****************************************************************************
203  * ReferenceReplace : Replace the last reference pointer when we destroy
204  *                    a picture
205  *****************************************************************************/
206 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
207                                          int i_coding_type,
208                                          picture_t * p_newref )
209 {
210     if( i_coding_type != B_CODING_TYPE )
211     {
212         if( p_vpar->sequence.p_backward != NULL )
213             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
214         p_vpar->sequence.p_backward = p_newref;
215         if( p_newref != NULL )
216             vout_LinkPicture( p_vpar->p_vout, p_newref );
217     }
218 }
219
220 /*****************************************************************************
221  * LoadMatrix : Load a quantization matrix
222  *****************************************************************************/
223 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
224 {
225     int i_dummy;
226
227     if( !p_matrix->b_allocated )
228     {
229         /* Allocate a piece of memory to load the matrix. */
230         if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
231         {
232             intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
233             p_vpar->b_error = 1;
234             return;
235         }
236         p_matrix->b_allocated = 1;
237     }
238
239     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
240     {
241         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
242              = GetBits( &p_vpar->bit_stream, 8 );
243     }
244
245 #ifdef VDEC_DFT
246     /* Discrete Fourier Transform requires the quantization matrices to
247      * be normalized before using them. */
248     vdec_NormQuantMatrix( p_matrix->pi_matrix );
249 #endif
250 }
251
252 /*****************************************************************************
253  * LinkMatrix : Link a quantization matrix to another
254  *****************************************************************************/
255 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
256 {
257     if( p_matrix->b_allocated )
258     {
259         /* Deallocate the piece of memory. */
260         free( p_matrix->pi_matrix );
261         p_matrix->b_allocated = 0;
262     }
263
264     p_matrix->pi_matrix = pi_array;
265 }
266
267 /*
268  * Exported functions.
269  */
270
271 /*****************************************************************************
272  * vpar_NextSequenceHeader : Find the next sequence header
273  *****************************************************************************/
274 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
275 {
276     while( !p_vpar->b_die )
277     {
278         NextStartCode( p_vpar );
279         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
280             return 0;
281         RemoveBits( &p_vpar->bit_stream, 8 );
282     }
283     return 1;
284 }
285
286 /*****************************************************************************
287  * vpar_ParseHeader : Parse the next header
288  *****************************************************************************/
289 int vpar_ParseHeader( vpar_thread_t * p_vpar )
290 {
291     while( !p_vpar->b_die )
292     {
293         NextStartCode( p_vpar );
294         switch( GetBits32( &p_vpar->bit_stream ) )
295         {
296         case SEQUENCE_HEADER_CODE:
297             SequenceHeader( p_vpar );
298             return 0;
299             break;
300
301         case GROUP_START_CODE:
302             GroupHeader( p_vpar );
303             return 0;
304             break;
305
306         case PICTURE_START_CODE:
307             PictureHeader( p_vpar );
308             return 0;
309             break;
310
311         case SEQUENCE_END_CODE:
312             intf_DbgMsg("vpar debug: sequence end code received\n");
313             return 1;
314             break;
315
316         default:
317         }
318     }
319
320     return 0;
321 }
322
323 /*
324  * Following functions are local
325  */
326
327 /*****************************************************************************
328  * SequenceHeader : Parse the next sequence header
329  *****************************************************************************/
330 static void SequenceHeader( vpar_thread_t * p_vpar )
331 {
332 #define RESERVED    -1
333     static int i_frame_rate_table[16] =
334     {
335         0,
336         23 * 1000,
337         24 * 1001,
338         25 * 1001,
339         30 * 1000,
340         30 * 1001,
341         50 * 1001,
342         60 * 1000,
343         60 * 1001,
344         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
345     };
346 #undef RESERVED
347
348     int i_height_save, i_width_save;
349
350     i_height_save = p_vpar->sequence.i_height;
351     i_width_save = p_vpar->sequence.i_width;
352
353     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
354     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
355     p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
356     p_vpar->sequence.i_frame_rate =
357             i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
358
359     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
360      * constrained_parameters_flag */
361     RemoveBits( &p_vpar->bit_stream, 30 );
362
363     /*
364      * Quantization matrices
365      */
366     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
367     {
368         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
369     }
370     else
371     {
372         /* Use default matrix. */
373         LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
374     }
375
376     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
377     {
378         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
379     }
380     else
381     {
382         /* Use default matrix. */
383         LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
384     }
385
386     /* Unless later overwritten by a matrix extension, we have the same
387      * matrices for luminance and chrominance. */
388     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
389                 p_vpar->sequence.intra_quant.pi_matrix );
390     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
391                 p_vpar->sequence.nonintra_quant.pi_matrix );
392
393     /*
394      * Sequence Extension
395      */
396     NextStartCode( p_vpar );
397     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
398     {
399         int                         i_dummy;
400
401         /* Turn the MPEG2 flag on */
402         p_vpar->sequence.b_mpeg2 = 1;
403
404         /* Parse sequence_extension */
405         RemoveBits32( &p_vpar->bit_stream );
406         /* extension_start_code_identifier, profile_and_level_indication */
407         RemoveBits( &p_vpar->bit_stream, 12 );
408         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
409         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
410         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
411         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
412         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
413         RemoveBits( &p_vpar->bit_stream, 22 );
414         /* frame_rate_extension_n */
415         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
416         /* frame_rate_extension_d */
417         p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
418                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
419     }
420     else
421     {
422         /* It's an MPEG-1 stream. Put adequate parameters. */
423
424         p_vpar->sequence.b_mpeg2 = 0;
425         p_vpar->sequence.b_progressive = 1;
426         p_vpar->sequence.i_chroma_format = CHROMA_420;
427     }
428
429     /* Update sizes */
430     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
431     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
432                                    (p_vpar->sequence.i_height + 15) / 16 :
433                                    2 * ((p_vpar->sequence.i_height + 31) / 32);
434     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
435                                         * p_vpar->sequence.i_mb_height;
436     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
437     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
438     p_vpar->sequence.i_size = p_vpar->sequence.i_width
439                                         * p_vpar->sequence.i_height;
440
441     /* Update chromatic information. */
442     switch( p_vpar->sequence.i_chroma_format )
443     {
444     case CHROMA_420:
445         p_vpar->sequence.i_chroma_nb_blocks = 2;
446         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
447         p_vpar->sequence.i_chroma_mb_width = 8;
448         p_vpar->sequence.i_chroma_mb_height = 8;
449         break;
450
451     case CHROMA_422:
452         p_vpar->sequence.i_chroma_nb_blocks = 4;
453         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
454         p_vpar->sequence.i_chroma_mb_width = 8;
455         p_vpar->sequence.i_chroma_mb_height = 16;
456         break;
457
458     case CHROMA_444:
459         p_vpar->sequence.i_chroma_nb_blocks = 8;
460         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
461         p_vpar->sequence.i_chroma_mb_width = 16;
462         p_vpar->sequence.i_chroma_mb_height = 16;
463     }
464
465     /* Reset scalable_mode. */
466     p_vpar->sequence.i_scalable_mode = SC_NONE;
467
468 #if 0
469     if(    p_vpar->sequence.i_width != i_width_save
470         || p_vpar->sequence.i_height != i_height_save )
471     {
472          /* FIXME: What do we do in case of a size change ?? */
473     }
474 #endif
475
476     /* Extension and User data */
477     ExtensionAndUserData( p_vpar );
478 }
479
480 /*****************************************************************************
481  * GroupHeader : Parse the next group of pictures header
482  *****************************************************************************/
483 static void GroupHeader( vpar_thread_t * p_vpar )
484 {
485     /* Nothing to do, we don't care. */
486     RemoveBits( &p_vpar->bit_stream, 27 );
487     ExtensionAndUserData( p_vpar );
488 }
489
490 /*****************************************************************************
491  * PictureHeader : Parse the next picture header
492  *****************************************************************************/
493 static void PictureHeader( vpar_thread_t * p_vpar )
494 {
495     int                 i_structure;
496     int                 i_mb_base;
497     boolean_t           b_parsable;
498 #ifdef VDEC_SMP
499     int                 i_mb;
500 #endif
501
502     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
503     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
504     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
505
506     if( p_vpar->picture.i_coding_type == P_CODING_TYPE
507         || p_vpar->picture.i_coding_type == B_CODING_TYPE )
508     {
509         p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
510         p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
511     }
512     if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
513     {
514         p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
515         p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
516     }
517
518     /* extra_information_picture */
519     while( GetBits( &p_vpar->bit_stream, 1 ) )
520     {
521         RemoveBits( &p_vpar->bit_stream, 8 );
522     }
523
524     /*
525      * Picture Coding Extension
526      */
527     NextStartCode( p_vpar );
528     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
529     {
530         /* Parse picture_coding_extension */
531         RemoveBits32( &p_vpar->bit_stream );
532         /* extension_start_code_identifier */
533         RemoveBits( &p_vpar->bit_stream, 4 );
534
535         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
536         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
537         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
538         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
539         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
540         i_structure = GetBits( &p_vpar->bit_stream, 2 );
541         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
542         p_vpar->picture.b_frame_pred_frame_dct
543              = GetBits( &p_vpar->bit_stream, 1 );
544         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
545         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
546         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
547         p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
548         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
549         /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
550          * the length of the picture_display_extension structure.
551          * chroma_420_type (obsolete) */
552         RemoveBits( &p_vpar->bit_stream, 1 );
553         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
554
555         /* composite_display_flag */
556         if( GetBits( &p_vpar->bit_stream, 1 ) )
557         {
558             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
559              * sub_carrier_phase */
560             RemoveBits( &p_vpar->bit_stream, 20 );
561         }
562     }
563     else
564     {
565         /* MPEG-1 compatibility flags */
566         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
567         i_structure = FRAME_STRUCTURE;
568         p_vpar->picture.b_frame_pred_frame_dct = 1;
569         p_vpar->picture.b_concealment_mv = 0;
570         p_vpar->picture.b_q_scale_type = 0;
571         p_vpar->picture.b_intra_vlc_format = 0;
572         p_vpar->picture.b_alternate_scan = 0; /* zigzag */
573         p_vpar->picture.b_repeat_first_field = 0;
574         p_vpar->picture.b_progressive_frame = 1;
575     }
576
577     if( p_vpar->picture.i_current_structure &&
578         (i_structure == FRAME_STRUCTURE ||
579          i_structure == p_vpar->picture.i_current_structure) )
580     {
581         /* We don't have the second field of the buffered frame. */
582         if( p_vpar->picture.p_picture != NULL )
583         {
584             ReferenceReplace( p_vpar,
585                       p_vpar->picture.i_coding_type,
586                       NULL );
587
588 #ifdef VDEC_SMP
589             for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
590             {
591                 vpar_DestroyMacroblock( &p_vpar->vfifo,
592                                         p_vpar->picture.pp_mb[i_mb] );
593             }
594 #endif
595             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
596         }
597
598         p_vpar->picture.i_current_structure = 0;
599
600         intf_DbgMsg("vpar debug: odd number of field picture.\n");
601     }
602
603     /* Do we have the reference pictures ? */
604     b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
605                     (p_vpar->sequence.p_backward == NULL)) ||
606                      /* p_backward will become p_forward later */
607                    ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
608                     (p_vpar->sequence.p_forward == NULL ||
609                      p_vpar->sequence.p_backward == NULL)));
610
611     if( b_parsable )
612     {
613         if( p_vpar->picture.i_current_structure )
614         {
615             /* Second field of a frame. We will decode it if, and only if we
616             * have decoded the first field. */
617             b_parsable = (p_vpar->picture.p_picture != NULL);
618         }
619         else
620         {
621             /* Does synchro say we have enough time to decode it ? */
622             b_parsable = vpar_SynchroChoose( p_vpar,
623                                p_vpar->picture.i_coding_type, i_structure );
624         }
625     }
626 #ifdef POLUX_SYNCHRO
627     else if( !p_vpar->picture.i_current_structure )
628     {
629         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
630     }
631 #endif
632
633     if( !b_parsable )
634     {
635         /* Update the reference pointers. */
636         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
637 #ifndef POLUX_SYNCHRO
638         /* Warn Synchro we have trashed a picture. */
639         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
640 #endif
641         /* Update context. */
642         if( i_structure != FRAME_STRUCTURE )
643         {
644             if( (p_vpar->picture.i_current_structure | i_structure)
645                     == FRAME_STRUCTURE )
646             {
647                 p_vpar->picture.i_current_structure = 0;
648             }
649             else
650             {
651                 p_vpar->picture.i_current_structure = i_structure;
652             }
653         }
654         p_vpar->picture.p_picture = NULL;
655
656         return;
657     }
658
659     /* OK, now we are sure we will decode the picture. */
660 #define P_picture p_vpar->picture.p_picture
661     p_vpar->picture.b_error = 0;
662     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
663
664     if( !p_vpar->picture.i_current_structure )
665     {
666         /* This is a new frame. Get a structure from the video_output. */
667         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
668                                         99+p_vpar->sequence.i_chroma_format, /*XXX??*/
669                                         p_vpar->sequence.i_width,
670                                         p_vpar->sequence.i_height ) )
671              == NULL )
672         {
673             intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
674             if( p_vpar->b_die || p_vpar->b_error )
675             {
676                 return;
677             }
678             msleep( VPAR_OUTMEM_SLEEP );
679         }
680
681         /* Initialize values. */
682         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
683         P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
684         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
685         p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
686                     << ( 1 - p_vpar->picture.b_frame_structure ) );
687         p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
688                     << ( 1 - p_vpar->picture.b_frame_structure ));
689
690         P_picture->i_deccount = p_vpar->sequence.i_mb_size;
691         vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
692 #ifdef VDEC_SMP
693         memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
694 #endif
695 /* FIXME ! remove asap ?? */
696 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
697
698         /* Update the reference pointers. */
699         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
700
701 #ifdef VDEC_SMP
702         /* Link referenced pictures for the decoder
703          * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
704         if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
705             p_vpar->picture.i_coding_type == B_CODING_TYPE )
706         {
707             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
708         }
709         if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
710         {
711             vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
712         }
713 #endif
714     }
715     p_vpar->picture.i_current_structure |= i_structure;
716     p_vpar->picture.i_structure = i_structure;
717
718     /* Initialize picture data for decoding. */
719     if( 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     p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
731
732     /* Extension and User data. */
733     ExtensionAndUserData( p_vpar );
734
735     vpar_PictureData( p_vpar, i_mb_base );
736
737     if( p_vpar->b_die || p_vpar->b_error )
738     {
739         return;
740     }
741
742     if( p_vpar->picture.b_error )
743     {
744         /* Trash picture. */
745 //fprintf(stderr, "Image trashee\n");
746 #ifdef VDEC_SMP
747         for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
748         {
749             vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
750         }
751 #endif
752
753         if( P_picture->i_deccount != 1 )
754         {
755             vout_DestroyPicture( p_vpar->p_vout, P_picture );
756         }
757
758         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
759
760         /* Prepare context for the next picture. */
761         P_picture = NULL;
762         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
763             p_vpar->picture.i_current_structure = 0;
764     }
765     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
766     {
767 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
768         /* Frame completely parsed. */
769 #ifdef VDEC_SMP
770         for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
771         {
772             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
773         }
774
775         /* Send signal to the video_decoder. */
776         vlc_mutex_lock( &p_vpar->vfifo.lock );
777         vlc_cond_signal( &p_vpar->vfifo.wait );
778         vlc_mutex_unlock( &p_vpar->vfifo.lock );
779 #endif
780
781         /* Prepare context for the next picture. */
782         P_picture = NULL;
783         p_vpar->picture.i_current_structure = 0;
784     }
785 #undef P_picture
786 }
787
788 /*****************************************************************************
789  * ExtensionAndUserData : Parse the extension_and_user_data structure
790  *****************************************************************************/
791 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
792 {
793     while( !p_vpar->b_die )
794     {
795         NextStartCode( p_vpar );
796         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
797         {
798         case EXTENSION_START_CODE:
799             RemoveBits32( &p_vpar->bit_stream );
800             switch( GetBits( &p_vpar->bit_stream, 4 ) )
801             {
802             case SEQUENCE_DISPLAY_EXTENSION_ID:
803                 SequenceDisplayExtension( p_vpar );
804                 break;
805             case QUANT_MATRIX_EXTENSION_ID:
806                 QuantMatrixExtension( p_vpar );
807                 break;
808             case SEQUENCE_SCALABLE_EXTENSION_ID:
809                 SequenceScalableExtension( p_vpar );
810                 break;
811             case PICTURE_DISPLAY_EXTENSION_ID:
812                 PictureDisplayExtension( p_vpar );
813                 break;
814             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
815                 PictureSpatialScalableExtension( p_vpar );
816                 break;
817             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
818                 PictureTemporalScalableExtension( p_vpar );
819                 break;
820             case COPYRIGHT_EXTENSION_ID:
821                 CopyrightExtension( p_vpar );
822                 break;
823             default:
824             }
825             break;
826
827         case USER_DATA_START_CODE:
828             RemoveBits32( &p_vpar->bit_stream );
829             /* Wait for the next start code */
830             break;
831
832         default:
833             return;
834         }
835     }
836 }
837
838
839 /*****************************************************************************
840  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
841  *****************************************************************************/
842
843 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
844 {
845     /* We don't care sequence_display_extension. */
846     /* video_format */
847     RemoveBits( &p_vpar->bit_stream, 3 );
848     if( GetBits( &p_vpar->bit_stream, 1 ) )
849     {
850         /* Two bytes for color_desciption */
851         RemoveBits( &p_vpar->bit_stream, 16 );
852         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
853     }
854     /* display_horizontal and vertical_size and a marker_bit */
855     RemoveBits( &p_vpar->bit_stream, 29 );
856 }
857
858
859 /*****************************************************************************
860  * QuantMatrixExtension : Load quantization matrices for luminance           *
861  *                        and chrominance                                    *
862  *****************************************************************************/
863
864 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
865 {
866     if( GetBits( &p_vpar->bit_stream, 1 ) )
867     {
868         /* Load intra_quantiser_matrix for luminance. */
869         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
870     }
871     else
872     {
873         /* Use the default matrix. */
874         LinkMatrix( &p_vpar->sequence.intra_quant,
875                     pi_default_intra_quant );
876     }
877     if( GetBits( &p_vpar->bit_stream, 1 ) )
878     {
879         /* Load non_intra_quantiser_matrix for luminance. */
880         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
881     }
882     else
883     {
884         /* Use the default matrix. */
885         LinkMatrix( &p_vpar->sequence.nonintra_quant,
886                     pi_default_nonintra_quant );
887     }
888     if( GetBits( &p_vpar->bit_stream, 1 ) )
889     {
890         /* Load intra_quantiser_matrix for chrominance. */
891         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
892     }
893     else
894     {
895         /* Link the chrominance intra matrix to the luminance one. */
896         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
897                     p_vpar->sequence.intra_quant.pi_matrix );
898     }
899     if( GetBits( &p_vpar->bit_stream, 1 ) )
900     {
901         /* Load non_intra_quantiser_matrix for chrominance. */
902         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
903     }
904     else
905     {
906         /* Link the chrominance intra matrix to the luminance one. */
907         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
908                     p_vpar->sequence.intra_quant.pi_matrix );
909     }
910     if( GetBits( &p_vpar->bit_stream, 1 ) )
911     {
912         /* Load non_intra_quantiser_matrix for chrominance. */
913         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
914     }
915     else
916     {
917         /* Link the chrominance nonintra matrix to the luminance one. */
918         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
919                     p_vpar->sequence.nonintra_quant.pi_matrix );
920     }
921 }
922
923
924 /*****************************************************************************
925  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
926  *                             structure to handle scalable coding           *
927  *****************************************************************************/
928
929 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
930 {
931     /* We don't care about anything scalable except the scalable mode. */
932     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
933     /* The length of the structure depends on the value of the scalable_mode */
934     {
935         case 1:
936             RemoveBits32( &p_vpar->bit_stream );
937             RemoveBits( &p_vpar->bit_stream, 21 );
938             break;
939         case 2:
940             RemoveBits( &p_vpar->bit_stream, 12 );
941             break;
942         default:
943             RemoveBits( &p_vpar->bit_stream, 4 );
944     }
945
946 }
947 /*****************************************************************************
948  * PictureDisplayExtension : Parse the picture_display_extension structure   *
949  *****************************************************************************/
950
951 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
952 {
953     /* Number of frame center offset */
954     int i_nb, i_dummy;
955     /* I am not sure it works but it should
956         (fewer tests than shown in §6.3.12) */
957     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
958                                             p_vpar->picture.b_repeat_first_field +
959                                             p_vpar->picture.b_top_field_first
960                            : ( p_vpar->picture.b_frame_structure + 1 ) +
961                              p_vpar->picture.b_repeat_first_field;
962     for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
963     {
964         RemoveBits( &p_vpar->bit_stream, 17 );
965         RemoveBits( &p_vpar->bit_stream, 17 );
966     }
967 }
968
969
970 /*****************************************************************************
971  * PictureSpatialScalableExtension                                           *
972  *****************************************************************************/
973
974 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
975 {
976     /* That's scalable, so we trash it */
977     RemoveBits32( &p_vpar->bit_stream );
978     RemoveBits( &p_vpar->bit_stream, 16 );
979 }
980
981
982 /*****************************************************************************
983  * PictureTemporalScalableExtension                                          *
984  *****************************************************************************/
985
986 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
987 {
988     /* Scalable again, trashed again */
989     RemoveBits( &p_vpar->bit_stream, 23 );
990 }
991
992
993 /*****************************************************************************
994  * CopyrightExtension : Keeps some legal informations                        *
995  *****************************************************************************/
996
997 static void CopyrightExtension( vpar_thread_t * p_vpar )
998 {
999     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1000     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1001         /* A flag that says whether the copyright information is significant */
1002     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1003         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1004     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1005         /* Reserved bits */
1006     RemoveBits( &p_vpar->bit_stream, 8 );
1007         /* The copyright_number is split in three parts */
1008         /* first part */
1009     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1010     RemoveBits( &p_vpar->bit_stream, 1 );
1011         /* second part */
1012     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1013     RemoveBits( &p_vpar->bit_stream, 1 );
1014         /* third part and sum */
1015     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1016                                       ( (u64)i_copyright_nb_2 << 22 ) |
1017                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1018 }