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