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