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