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