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