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