]> git.sesse.net Git - vlc/blob - modules/codec/mpeg_video/headers.c
* modules/codec/mpeg_video/headers.c: fixed non-initialized variable in
[vlc] / modules / codec / mpeg_video / headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: headers.c,v 1.9 2003/01/30 02:16:09 gbazin Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Stéphane Borel <stef@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                                /* free() */
29 #include <string.h>                                    /* memcpy(), memset() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33 #include <vlc/decoder.h>
34
35 #include "plugins.h"
36 #include "decoder.h"
37 #include "pool.h"
38 #include "parser.h"
39
40 /*
41  * Local prototypes
42  */
43 static inline void NextStartCode( bit_stream_t * );
44 static void SequenceHeader( vpar_thread_t * p_vpar );
45 static void GroupHeader( vpar_thread_t * p_vpar );
46 static void PictureHeader( vpar_thread_t * p_vpar );
47 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
48 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
49 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
50 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
51 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
52 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
53 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
54 static void CopyrightExtension( vpar_thread_t * p_vpar );
55
56 /*
57  * Standard variables
58  */
59
60 /*****************************************************************************
61  * pi_default_intra_quant : default quantization matrix
62  *****************************************************************************/
63 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
64 {
65     8,  16, 19, 22, 26, 27, 29, 34,
66     16, 16, 22, 24, 27, 29, 34, 37,
67     19, 22, 26, 27, 29, 34, 34, 38,
68     22, 22, 26, 27, 29, 34, 37, 40,
69     22, 26, 27, 29, 32, 35, 40, 48,
70     26, 27, 29, 32, 35, 40, 48, 58,
71     26, 27, 29, 34, 38, 46, 56, 69,
72     27, 29, 35, 38, 46, 56, 69, 83
73 };
74
75 /*****************************************************************************
76  * pi_default_nonintra_quant : default quantization matrix
77  *****************************************************************************/
78 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
79 {
80     16, 16, 16, 16, 16, 16, 16, 16,
81     16, 16, 16, 16, 16, 16, 16, 16,
82     16, 16, 16, 16, 16, 16, 16, 16,
83     16, 16, 16, 16, 16, 16, 16, 16,
84     16, 16, 16, 16, 16, 16, 16, 16,
85     16, 16, 16, 16, 16, 16, 16, 16,
86     16, 16, 16, 16, 16, 16, 16, 16,
87     16, 16, 16, 16, 16, 16, 16, 16
88 };
89
90 /*****************************************************************************
91  * pi_scan : zig-zag and alternate scan patterns
92  *****************************************************************************/
93 u8 pi_scan[2][64] ATTR_ALIGN(16) =
94 {
95     { /* Zig-Zag pattern */
96         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
97         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
98         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
99         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
100     },
101     { /* Alternate scan pattern */
102         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
103         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
104         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
105         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
106     }
107 };
108
109 /*
110  * Local inline functions.
111  */
112
113 /*****************************************************************************
114  * ReferenceUpdate : Update the reference pointers when we have a new picture
115  *****************************************************************************/
116 static void inline ReferenceUpdate( vpar_thread_t * p_vpar,
117                                     int i_coding_type, picture_t * p_newref )
118 {
119     if( i_coding_type != B_CODING_TYPE )
120     {
121         if( p_vpar->sequence.p_forward != NULL )
122         {
123             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
124         }
125         if( p_vpar->sequence.p_backward != NULL )
126         {
127             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
128                               vpar_SynchroDate( p_vpar ) );
129         }
130         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
131         p_vpar->sequence.p_backward = p_newref;
132         if( p_newref != NULL )
133         {
134             vout_LinkPicture( p_vpar->p_vout, p_newref );
135         }
136     }
137     else if( p_newref != NULL )
138     {
139         /* Put date immediately. */
140         vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
141     }
142 }
143
144 /*****************************************************************************
145  * ReferenceReplace : Replace the last reference pointer when we destroy
146  *                    a picture
147  *****************************************************************************/
148 static void inline ReferenceReplace( vpar_thread_t * p_vpar,
149                                      int i_coding_type, picture_t * p_newref )
150 {
151     if( i_coding_type != B_CODING_TYPE )
152     {
153         if( p_vpar->sequence.p_backward != NULL )
154         {
155             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
156         }
157         p_vpar->sequence.p_backward = p_newref;
158         if( p_newref != NULL )
159         {
160             vout_LinkPicture( p_vpar->p_vout, p_newref );
161         }
162     }
163 }
164
165 /*****************************************************************************
166  * LoadMatrix : Load a quantization matrix
167  *****************************************************************************/
168 static inline void LoadMatrix( vpar_thread_t * p_vpar,
169                                quant_matrix_t * p_matrix )
170 {
171     int i_dummy;
172
173     if( !p_matrix->b_allocated )
174     {
175         /* Allocate a piece of memory to load the matrix. */
176         if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
177         {
178             msg_Err( p_vpar->p_fifo, "out of memory" );
179             p_vpar->p_fifo->b_error = 1;
180             return;
181         }
182         p_matrix->b_allocated = 1;
183     }
184
185     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
186     {
187         p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
188              = GetBits( &p_vpar->bit_stream, 8 );
189     }
190 }
191
192 /*****************************************************************************
193  * LinkMatrix : Link a quantization matrix to another
194  *****************************************************************************/
195 static inline void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
196 {
197     if( p_matrix->b_allocated )
198     {
199         /* Deallocate the piece of memory. */
200         free( p_matrix->pi_matrix );
201         p_matrix->b_allocated = 0;
202     }
203
204     p_matrix->pi_matrix = pi_array;
205 }
206
207 /*****************************************************************************
208  * ChromaToFourCC: Return a FourCC value used by the video output.
209  *****************************************************************************/
210 static inline vlc_fourcc_t ChromaToFourCC( int i_chroma )
211 {
212     switch( i_chroma )
213     {
214         case CHROMA_420:
215             return VLC_FOURCC('I','4','2','0');
216
217         case CHROMA_422:
218             return VLC_FOURCC('I','4','2','2');
219
220         case CHROMA_444:
221             return VLC_FOURCC('I','4','4','4');
222
223         default:
224             /* This can't happen */
225             return 0xdeadbeef;
226     }
227 }
228
229 /*
230  * Exported functions.
231  */
232
233 /*****************************************************************************
234  * vpar_NextSequenceHeader : Find the next sequence header
235  *****************************************************************************/
236 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
237 {
238     while( !p_vpar->p_fifo->b_die )
239     {
240         NextStartCode( &p_vpar->bit_stream );
241         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
242         {
243             return 0;
244         }
245         RemoveBits( &p_vpar->bit_stream, 8 );
246     }
247     return 1;
248 }
249
250 /*****************************************************************************
251  * vpar_ParseHeader : Parse the next header
252  *****************************************************************************/
253 int vpar_ParseHeader( vpar_thread_t * p_vpar )
254 {
255     NextStartCode( &p_vpar->bit_stream );
256     switch( GetBits32( &p_vpar->bit_stream ) )
257     {
258     case SEQUENCE_HEADER_CODE:
259         p_vpar->c_sequences++;
260         SequenceHeader( p_vpar );
261         return 0;
262
263     case GROUP_START_CODE:
264         GroupHeader( p_vpar );
265         return 0;
266
267     case PICTURE_START_CODE:
268         PictureHeader( p_vpar );
269         return 0;
270
271     case SEQUENCE_END_CODE:
272         msg_Dbg( p_vpar->p_fifo, "sequence end code received" );
273         if( p_vpar->sequence.p_backward != NULL )
274         {
275             p_vpar->sequence.p_backward->b_force = 1;
276         }
277         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
278         return 1;
279
280     default:
281         break;
282     }
283
284     return 0;
285 }
286
287 /*
288  * Following functions are local
289  */
290
291 /*****************************************************************************
292  * SequenceHeader : Parse the next sequence header
293  *****************************************************************************/
294 static void SequenceHeader( vpar_thread_t * p_vpar )
295 {
296 #define RESERVED    -1
297     static int i_frame_rate_table[16] =
298     {
299         0,
300         23 * 1000,
301         24 * 1001,
302         25 * 1001,
303         30 * 1000,
304         30 * 1001,
305         50 * 1001,
306         60 * 1000,
307         60 * 1001,
308         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
309     };
310 #undef RESERVED
311
312     int i_aspect;
313
314     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
315     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
316     i_aspect = GetBits( &p_vpar->bit_stream, 4 );
317     p_vpar->sequence.i_frame_rate =
318             i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
319
320     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
321      * constrained_parameters_flag */
322     RemoveBits( &p_vpar->bit_stream, 30 );
323
324     /*
325      * Quantization matrices
326      */
327     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
328     {
329         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
330     }
331     else
332     {
333         /* Use default matrix. */
334         LinkMatrix( &p_vpar->sequence.intra_quant,
335                     p_vpar->pi_default_intra_quant );
336     }
337
338     if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
339     {
340         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
341     }
342     else
343     {
344         /* Use default matrix. */
345         LinkMatrix( &p_vpar->sequence.nonintra_quant,
346                     p_vpar->pi_default_nonintra_quant );
347     }
348
349     /* Unless later overwritten by a matrix extension, we have the same
350      * matrices for luminance and chrominance. */
351     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
352                 p_vpar->sequence.intra_quant.pi_matrix );
353     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
354                 p_vpar->sequence.nonintra_quant.pi_matrix );
355
356     /*
357      * Sequence Extension
358      */
359     NextStartCode( &p_vpar->bit_stream );
360     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
361     {
362         int                         i_dummy;
363
364         /* Turn the MPEG2 flag on */
365         p_vpar->sequence.b_mpeg2 = 1;
366
367         /* Parse sequence_extension */
368         RemoveBits32( &p_vpar->bit_stream );
369         /* extension_start_code_identifier, profile_and_level_indication */
370         RemoveBits( &p_vpar->bit_stream, 12 );
371         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
372         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
373         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
374         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
375         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
376          * low_delay */
377         RemoveBits( &p_vpar->bit_stream, 22 );
378         /* frame_rate_extension_n */
379         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
380         /* frame_rate_extension_d */
381         p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
382                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
383     }
384     else
385     {
386         /* It's an MPEG-1 stream. Put adequate parameters. */
387         int i_xyratio;
388         static int pi_mpeg1ratio[15] = {
389             10000, 10000,  6735,  7031,  7615,  8055,  8437, 8935,
390              9157,  9815, 10255, 10695, 10950, 11575, 12015
391         };
392
393         if( i_aspect > 1 )
394         {
395                 i_xyratio = p_vpar->sequence.i_height *
396                         pi_mpeg1ratio[i_aspect] / p_vpar->sequence.i_width;
397                 if( 7450 < i_xyratio && i_xyratio < 7550 )
398                 {
399                         i_aspect = 2;
400                 }
401                 else if( 5575 < i_xyratio && i_xyratio < 5675 )
402                 {
403                         i_aspect = 3;
404                 }
405                 else if( 4475 < i_xyratio && i_xyratio < 4575 )
406                 {
407                         i_aspect = 4;
408                 }
409         }
410
411         p_vpar->sequence.b_mpeg2 = 0;
412         p_vpar->sequence.b_progressive = 1;
413         p_vpar->sequence.i_chroma_format = CHROMA_420;
414     }
415
416     /* check whether the input gives a particular aspect ratio */
417     if( p_vpar->p_fifo->p_demux_data
418          && ( *(int*)(p_vpar->p_fifo->p_demux_data) & 0x7 ) )
419     {
420         i_aspect = *(int*)(p_vpar->p_fifo->p_demux_data);
421     }
422
423     /* Store calculated aspect ratio */
424     switch( i_aspect )
425     {
426         case AR_3_4_PICTURE:
427             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
428             break;
429
430         case AR_16_9_PICTURE:
431             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
432             break;
433
434         case AR_221_1_PICTURE:
435             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
436             break;
437
438         case AR_SQUARE_PICTURE:
439         default:
440             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR
441                     * p_vpar->sequence.i_width / p_vpar->sequence.i_height;
442             break;
443     }
444
445     /* Update sizes */
446     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
447     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
448                                    (p_vpar->sequence.i_height + 15) / 16 :
449                                    2 * ((p_vpar->sequence.i_height + 31) / 32);
450     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
451                                         * p_vpar->sequence.i_mb_height;
452     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
453     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
454     p_vpar->sequence.i_size = p_vpar->sequence.i_width
455                                         * p_vpar->sequence.i_height;
456     switch( p_vpar->sequence.i_chroma_format )
457     {
458     case CHROMA_420:
459         p_vpar->sequence.i_chroma_nb_blocks = 2;
460         p_vpar->sequence.b_chroma_h_subsampled = 1;
461         p_vpar->sequence.b_chroma_v_subsampled = 1;
462         break;
463     case CHROMA_422:
464         p_vpar->sequence.i_chroma_nb_blocks = 4;
465         p_vpar->sequence.b_chroma_h_subsampled = 1;
466         p_vpar->sequence.b_chroma_v_subsampled = 0;
467         break;
468     case CHROMA_444:
469         p_vpar->sequence.i_chroma_nb_blocks = 6;
470         p_vpar->sequence.b_chroma_h_subsampled = 0;
471         p_vpar->sequence.b_chroma_v_subsampled = 0;
472         break;
473     }
474
475     /* Extension and User data */
476     ExtensionAndUserData( p_vpar );
477
478     /* Spawn a video output if there is none. First we look for our children,
479      * then we look for any other vout that might be available. */
480     p_vpar->p_vout =
481            vout_Request( p_vpar->p_fifo, p_vpar->p_vout,
482                          p_vpar->sequence.i_width, p_vpar->sequence.i_height,
483                          ChromaToFourCC( p_vpar->sequence.i_chroma_format ),
484                          p_vpar->sequence.i_aspect );
485
486     if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
487     {
488         return;
489     }
490
491     if( p_vpar->p_vout == NULL )
492     {
493         msg_Err( p_vpar->p_fifo, "cannot open vout, aborting" );
494         p_vpar->p_fifo->b_error = 1;
495         return;
496     }
497 }
498
499 /*****************************************************************************
500  * GroupHeader : Parse the next group of pictures header
501  *****************************************************************************/
502 static void GroupHeader( vpar_thread_t * p_vpar )
503 {
504     /* Nothing to do, we don't care. */
505     RemoveBits( &p_vpar->bit_stream, 27 );
506     ExtensionAndUserData( p_vpar );
507 }
508
509 /*****************************************************************************
510  * PictureHeader : Parse the next picture header
511  *****************************************************************************/
512 static void PictureHeader( vpar_thread_t * p_vpar )
513 {
514     int                 i_structure, i_previous_coding_type;
515     vlc_bool_t          b_parsable = 0;
516
517     /* Retrieve the PTS. */
518     CurrentPTS( &p_vpar->bit_stream, &p_vpar->sequence.next_pts,
519                 &p_vpar->sequence.next_dts );
520
521     /* Recover in case of stream discontinuity. */
522     if( p_vpar->sequence.b_expect_discontinuity )
523     {
524         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
525         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
526         if( p_vpar->picture.p_picture != NULL )
527         {
528             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
529             p_vpar->picture.p_picture = NULL;
530         }
531         p_vpar->picture.i_current_structure = 0;
532         p_vpar->sequence.b_expect_discontinuity = 0;
533     }
534
535     /* Parse the picture header. */
536     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
537     i_previous_coding_type = p_vpar->picture.i_coding_type;
538     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
539     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
540
541     if( p_vpar->picture.i_coding_type == P_CODING_TYPE
542         || p_vpar->picture.i_coding_type == B_CODING_TYPE )
543     {
544         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
545         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
546                                             - 1;
547     }
548     if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
549     {
550         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
551         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
552                                             - 1;
553     }
554
555     /* extra_information_picture */
556     while( GetBits( &p_vpar->bit_stream, 1 ) )
557     {
558         RemoveBits( &p_vpar->bit_stream, 8 );
559     }
560
561     /*
562      * Picture Coding Extension
563      */
564     NextStartCode( &p_vpar->bit_stream );
565     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
566     {
567         /* Parse picture_coding_extension */
568         RemoveBits32( &p_vpar->bit_stream );
569         /* extension_start_code_identifier */
570         RemoveBits( &p_vpar->bit_stream, 4 );
571
572         /* Pre-substract 1 for later use in MotionDelta(). */
573         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
574         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
575         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
576         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
577         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
578         i_structure = GetBits( &p_vpar->bit_stream, 2 );
579         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
580         p_vpar->picture.b_frame_pred_frame_dct
581              = GetBits( &p_vpar->bit_stream, 1 );
582         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
583         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
584         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
585         /* Alternate scan */
586         p_vpar->picture.pi_scan =
587             p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
588         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
589         /* chroma_420_type (obsolete) */
590         RemoveBits( &p_vpar->bit_stream, 1 );
591         p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
592
593         /* composite_display_flag */
594         if( GetBits( &p_vpar->bit_stream, 1 ) )
595         {
596             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
597              * sub_carrier_phase */
598             RemoveBits( &p_vpar->bit_stream, 20 );
599         }
600     }
601     else
602     {
603         /* MPEG-1 compatibility flags */
604         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
605         i_structure = FRAME_STRUCTURE;
606         p_vpar->picture.b_top_field_first = 0;
607         p_vpar->picture.b_frame_pred_frame_dct = 1;
608         p_vpar->picture.b_concealment_mv = 0;
609         p_vpar->picture.b_q_scale_type = 0;
610         p_vpar->picture.b_intra_vlc_format = 0;
611         p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
612         p_vpar->picture.b_repeat_first_field = 0;
613         p_vpar->picture.b_progressive = 1;
614     }
615
616     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
617
618     /* Extension and User data. */
619     ExtensionAndUserData( p_vpar );
620
621     p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
622
623     if( p_vpar->picture.i_current_structure )
624     {
625         if ( (i_structure == FRAME_STRUCTURE ||
626               i_structure == p_vpar->picture.i_current_structure) )
627         {
628             /* We don't have the second field of the buffered frame. */
629             if( p_vpar->picture.p_picture != NULL )
630             {
631                 ReferenceReplace( p_vpar,
632                                   p_vpar->picture.i_coding_type,
633                                   NULL );
634                 vout_DestroyPicture( p_vpar->p_vout,
635                                      p_vpar->picture.p_picture );
636                 p_vpar->picture.p_picture = NULL;
637             }
638
639             p_vpar->picture.i_current_structure = 0;
640
641             msg_Warn( p_vpar->p_fifo, "odd number of field pictures" );
642         }
643         else
644         {
645             /* Second field of a frame. We will decode it if, and only if we
646              * have decoded the first field. */
647             if( p_vpar->picture.p_picture == NULL )
648             {
649                 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
650                       && p_vpar->sequence.p_backward == NULL) )
651                 {
652                     /* Exceptionnally, parse the picture if it is I. We need
653                      * this in case of an odd number of field pictures, if the
654                      * previous picture is not intra, we desperately need a
655                      * new reference picture. OK, this is kind of kludgy. */
656                     p_vpar->picture.i_current_structure = 0;
657                 }
658                 else
659                 {
660                     b_parsable = 0;
661                 }
662             }
663             else
664             {
665                 /* We suppose we have the reference pictures, since we already
666                  * decoded the first field and the second field will not need
667                  * any extra reference picture. There is a special case of
668                  * P field being the second field of an I field, but ISO/IEC
669                  * 13818-2 section 7.6.3.5 specifies that this P field will
670                  * not need any reference picture besides the I field. So far
671                  * so good. */
672                 b_parsable = 1;
673
674                 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
675                     i_previous_coding_type == I_CODING_TYPE &&
676                     p_vpar->sequence.p_forward == NULL )
677                 {
678                     /* This is the special case of section 7.6.3.5. Create
679                      * a fake reference picture (which will not be used)
680                      * but will prevent us from segfaulting in the slice
681                      * parsing. */
682                     static picture_t    fake_picture;
683                     fake_picture.i_planes = 0; /* We will use it later */
684                     p_vpar->sequence.p_forward = &fake_picture;
685                 }
686             }
687         }
688     }
689
690     if( !p_vpar->picture.i_current_structure )
691     {
692         /* First field of a frame, or new frame picture. */
693         int     i_repeat_field;
694
695         /* Do we have the reference pictures ? */
696         b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
697                          p_vpar->picture.b_concealment_mv) &&
698                         (p_vpar->sequence.p_backward == NULL)) ||
699                          /* p_backward will become p_forward later */
700                        ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
701                         (p_vpar->sequence.p_forward == NULL ||
702                          p_vpar->sequence.p_backward == NULL)));
703
704         /* Compute the number of times the frame will be emitted by the
705          * decoder (number of half-periods). */
706         if( p_vpar->sequence.b_progressive )
707         {
708             i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
709                                 + p_vpar->picture.b_top_field_first) * 2;
710         }
711         else
712         {
713             if( p_vpar->picture.b_progressive )
714             {
715                 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
716             }
717             else
718             {
719                 i_repeat_field = 2;
720             }
721         }
722
723         /* Warn synchro we have a new picture (updates pictures index). */
724         vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
725                                 i_repeat_field );
726
727         if( b_parsable )
728         {
729             /* Does synchro say we have enough time to decode it ? */
730             b_parsable = vpar_SynchroChoose( p_vpar,
731                                p_vpar->picture.i_coding_type, i_structure );
732         }
733     }
734
735     if( !b_parsable )
736     {
737         /* Update the reference pointers. */
738         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
739
740         /* Update context. */
741         if( i_structure != FRAME_STRUCTURE )
742         {
743             if( (p_vpar->picture.i_current_structure | i_structure)
744                     == FRAME_STRUCTURE )
745             {
746                 /* The frame is complete. */
747                 p_vpar->picture.i_current_structure = 0;
748
749                 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
750             }
751             else
752             {
753                 p_vpar->picture.i_current_structure = i_structure;
754             }
755         }
756         else
757         {
758             /* Warn Synchro we have trashed a picture. */
759             vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
760         }
761         p_vpar->picture.p_picture = NULL;
762
763         return;
764     }
765
766     /* OK, now we are sure we will decode the picture. */
767     p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
768
769 #define P_picture p_vpar->picture.p_picture
770     p_vpar->picture.b_error = 0;
771
772     if( !p_vpar->picture.i_current_structure )
773     {
774         /* This is a new frame. Get a structure from the video_output. */
775         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
776                                  p_vpar->picture.b_progressive,
777                                  p_vpar->picture.b_top_field_first,
778                                  p_vpar->picture.b_repeat_first_field ) )
779                  == NULL )
780         {
781             if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
782             {
783                 return;
784             }
785             msleep( VOUT_OUTMEM_SLEEP );
786         }
787
788         /* Initialize values. */
789         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
790         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
791
792         /* Update the reference pointers. */
793         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
794     }
795
796     /* Initialize picture data for decoding. */
797     p_vpar->picture.i_current_structure |= i_structure;
798     p_vpar->picture.i_structure = i_structure;
799     p_vpar->picture.b_second_field =
800         (i_structure != p_vpar->picture.i_current_structure);
801     p_vpar->picture.b_current_field =
802         (i_structure == BOTTOM_FIELD );
803     p_vpar->picture.i_lum_stride = p_vpar->picture.p_picture->Y_PITCH
804         << ( 1 - p_vpar->picture.b_frame_structure );
805     p_vpar->picture.i_chrom_stride = p_vpar->picture.p_picture->U_PITCH
806         << ( 1 - p_vpar->picture.b_frame_structure );
807     /* We suppose the pitch is the same for U and V planes. */
808     p_vpar->picture.i_field_width = p_vpar->sequence.i_width
809         << ( 1 - p_vpar->picture.b_frame_structure );
810
811     if( !p_vpar->p_fifo->p_stream_ctrl->b_grayscale )
812     {
813         switch( p_vpar->sequence.i_chroma_format )
814         {
815         case CHROMA_422:
816             p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock422;
817             break;
818         case CHROMA_444:
819             p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock444;
820             break;
821         case CHROMA_420:
822         default:
823             p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock420;
824             break;
825         }
826     }
827     else
828     {
829         p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblockBW;
830     }
831
832
833     if( p_vpar->sequence.b_mpeg2 )
834     {
835         static f_picture_data_t ppf_picture_data[4][4] =
836         {
837             {
838                 NULL, NULL, NULL, NULL
839             },
840             {
841                 /* TOP_FIELD */
842 #if (VPAR_OPTIM_LEVEL > 1)
843                 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
844                 vpar_PictureData2BT
845 #else
846                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
847                 vpar_PictureDataGENERIC
848 #endif
849             },
850             {
851                 /* BOTTOM_FIELD */
852 #if (VPAR_OPTIM_LEVEL > 1)
853                 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
854                 vpar_PictureData2BB
855 #else
856                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
857                 vpar_PictureDataGENERIC
858 #endif
859             },
860             {
861                 /* FRAME_PICTURE */
862 #if (VPAR_OPTIM_LEVEL > 0)
863                 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
864                 vpar_PictureData2BF
865 #else
866                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
867                 vpar_PictureDataGENERIC
868 #endif
869             }
870         };
871
872         ppf_picture_data[p_vpar->picture.i_structure]
873                         [p_vpar->picture.i_coding_type]( p_vpar );
874     }
875     else
876     {
877 #if (VPAR_OPTIM_LEVEL > 1)
878         static f_picture_data_t pf_picture_data[5] =
879         { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
880           vpar_PictureData1D };
881
882         pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
883 #else
884         vpar_PictureDataGENERIC( p_vpar );
885 #endif
886     }
887
888     /* Wait for all the macroblocks to be decoded. */
889     p_vpar->pool.pf_wait_pool( &p_vpar->pool );
890
891     /* Re-spawn decoder threads if the user changed settings. */
892     vpar_SpawnPool( p_vpar );
893
894     if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
895     {
896         return;
897     }
898
899     if( p_vpar->sequence.p_forward != NULL &&
900         p_vpar->sequence.p_forward->i_planes == 0 )
901     {
902         /* This can only happen with the fake picture created for section
903          * 7.6.3.5. Clean up our mess. */
904         p_vpar->sequence.p_forward = NULL;
905     }
906
907     if( p_vpar->picture.b_error )
908     {
909         /* Trash picture. */
910         p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
911
912         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
913                          p_vpar->picture.i_structure, 1 );
914         vout_DestroyPicture( p_vpar->p_vout, P_picture );
915
916         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
917
918         /* Prepare context for the next picture. */
919         P_picture = NULL;
920         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
921             p_vpar->picture.i_current_structure = 0;
922     }
923     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
924     {
925         /* Frame completely parsed. */
926         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
927                          p_vpar->picture.i_structure, 0 );
928         vout_DisplayPicture( p_vpar->p_vout, P_picture );
929
930         /* Prepare context for the next picture. */
931         P_picture = NULL;
932         p_vpar->picture.i_current_structure = 0;
933     }
934 #undef P_picture
935 }
936
937 /*****************************************************************************
938  * ExtensionAndUserData : Parse the extension_and_user_data structure
939  *****************************************************************************/
940 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
941 {
942     while( !p_vpar->p_fifo->b_die )
943     {
944         NextStartCode( &p_vpar->bit_stream );
945         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
946         {
947         case EXTENSION_START_CODE:
948             RemoveBits32( &p_vpar->bit_stream );
949             switch( GetBits( &p_vpar->bit_stream, 4 ) )
950             {
951             case SEQUENCE_DISPLAY_EXTENSION_ID:
952                 SequenceDisplayExtension( p_vpar );
953                 break;
954             case QUANT_MATRIX_EXTENSION_ID:
955                 QuantMatrixExtension( p_vpar );
956                 break;
957             case SEQUENCE_SCALABLE_EXTENSION_ID:
958                 SequenceScalableExtension( p_vpar );
959                 break;
960             case PICTURE_DISPLAY_EXTENSION_ID:
961                 PictureDisplayExtension( p_vpar );
962                 break;
963             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
964                 PictureSpatialScalableExtension( p_vpar );
965                 break;
966             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
967                 PictureTemporalScalableExtension( p_vpar );
968                 break;
969             case COPYRIGHT_EXTENSION_ID:
970                 CopyrightExtension( p_vpar );
971                 break;
972             default:
973                 break;
974             }
975             break;
976
977         case USER_DATA_START_CODE:
978             RemoveBits32( &p_vpar->bit_stream );
979             /* Wait for the next start code */
980             break;
981
982         default:
983             return;
984         }
985     }
986 }
987
988
989 /*****************************************************************************
990  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
991  *****************************************************************************/
992
993 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
994 {
995     /* We don't care sequence_display_extension. */
996     /* video_format */
997     RemoveBits( &p_vpar->bit_stream, 3 );
998     if( GetBits( &p_vpar->bit_stream, 1 ) )
999     {
1000         /* Two bytes for color_desciption */
1001         RemoveBits( &p_vpar->bit_stream, 16 );
1002         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
1003     }
1004     /* display_horizontal and vertical_size and a marker_bit */
1005     RemoveBits( &p_vpar->bit_stream, 29 );
1006 }
1007
1008
1009 /*****************************************************************************
1010  * QuantMatrixExtension : Load quantization matrices for luminance           *
1011  *                        and chrominance                                    *
1012  *****************************************************************************/
1013
1014 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
1015 {
1016     if( GetBits( &p_vpar->bit_stream, 1 ) )
1017     {
1018         /* Load intra_quantiser_matrix for luminance. */
1019         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1020     }
1021     else
1022     {
1023         /* Use the default matrix. */
1024         LinkMatrix( &p_vpar->sequence.intra_quant,
1025                     p_vpar->pi_default_intra_quant );
1026     }
1027     if( GetBits( &p_vpar->bit_stream, 1 ) )
1028     {
1029         /* Load non_intra_quantiser_matrix for luminance. */
1030         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1031     }
1032     else
1033     {
1034         /* Use the default matrix. */
1035         LinkMatrix( &p_vpar->sequence.nonintra_quant,
1036                     p_vpar->pi_default_nonintra_quant );
1037     }
1038     if( GetBits( &p_vpar->bit_stream, 1 ) )
1039     {
1040         /* Load intra_quantiser_matrix for chrominance. */
1041         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1042     }
1043     else
1044     {
1045         /* Link the chrominance intra matrix to the luminance one. */
1046         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1047                     p_vpar->sequence.intra_quant.pi_matrix );
1048     }
1049     if( GetBits( &p_vpar->bit_stream, 1 ) )
1050     {
1051         /* Load non_intra_quantiser_matrix for chrominance. */
1052         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1053     }
1054     else
1055     {
1056         /* Link the chrominance nonintra matrix to the luminance one. */
1057         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1058                     p_vpar->sequence.nonintra_quant.pi_matrix );
1059     }
1060 }
1061
1062
1063 /*****************************************************************************
1064  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
1065  *                             structure to handle scalable coding           *
1066  *****************************************************************************/
1067
1068 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1069 {
1070     /* We don't care about anything scalable except the scalable mode. */
1071     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1072     /* The length of the structure depends on the value of the scalable_mode */
1073     {
1074         case 1:
1075             RemoveBits32( &p_vpar->bit_stream );
1076             RemoveBits( &p_vpar->bit_stream, 21 );
1077             break;
1078         case 2:
1079             RemoveBits( &p_vpar->bit_stream, 12 );
1080             break;
1081         default:
1082             RemoveBits( &p_vpar->bit_stream, 4 );
1083     }
1084
1085 }
1086 /*****************************************************************************
1087  * PictureDisplayExtension : Parse the picture_display_extension structure   *
1088  *****************************************************************************/
1089
1090 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1091 {
1092     /* Number of frame center offset */
1093     int i_nb, i_dummy;
1094     /* I am not sure it works but it should
1095         (fewer tests than shown in §6.3.12) */
1096     /* FIXME: is VLC_TRUE always 1 ? */
1097     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1098                                             p_vpar->picture.b_repeat_first_field +
1099                                             p_vpar->picture.b_top_field_first
1100                            : ( p_vpar->picture.b_frame_structure + 1 ) +
1101                              p_vpar->picture.b_repeat_first_field;
1102     for( i_dummy = 0; i_dummy < i_nb && !p_vpar->p_fifo->b_die
1103                        && !p_vpar->p_fifo->b_error; i_dummy++ )
1104     {
1105         RemoveBits( &p_vpar->bit_stream, 17 );
1106         RemoveBits( &p_vpar->bit_stream, 17 );
1107     }
1108 }
1109
1110
1111 /*****************************************************************************
1112  * PictureSpatialScalableExtension                                           *
1113  *****************************************************************************/
1114
1115 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1116 {
1117     /* That's scalable, so we trash it */
1118     RemoveBits32( &p_vpar->bit_stream );
1119     RemoveBits( &p_vpar->bit_stream, 16 );
1120 }
1121
1122
1123 /*****************************************************************************
1124  * PictureTemporalScalableExtension                                          *
1125  *****************************************************************************/
1126
1127 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1128 {
1129     /* Scalable again, trashed again */
1130     RemoveBits( &p_vpar->bit_stream, 23 );
1131 }
1132
1133
1134 /*****************************************************************************
1135  * CopyrightExtension : Keeps some legal informations                        *
1136  *****************************************************************************/
1137
1138 static void CopyrightExtension( vpar_thread_t * p_vpar )
1139 {
1140     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1141     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1142         /* A flag that says whether the copyright information is significant */
1143     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1144         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1145     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1146         /* Reserved bits */
1147     RemoveBits( &p_vpar->bit_stream, 8 );
1148         /* The copyright_number is split in three parts */
1149         /* first part */
1150     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1151     RemoveBits( &p_vpar->bit_stream, 1 );
1152         /* second part */
1153     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1154     RemoveBits( &p_vpar->bit_stream, 1 );
1155         /* third part and sum */
1156     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1157                                       ( (u64)i_copyright_nb_2 << 22 ) |
1158                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1159 }