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