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