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