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