cbe8876a85c29140290eb474f3d5c39657a14101
[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.10 2003/02/04 11:51:21 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     }
658     p_vpar->sequence.b_after_sequence_header = 0;
659
660     if( p_vpar->picture.i_current_structure )
661     {
662         if ( (i_structure == FRAME_STRUCTURE ||
663               i_structure == p_vpar->picture.i_current_structure) )
664         {
665             /* We don't have the second field of the buffered frame. */
666             if( p_vpar->picture.p_picture != NULL )
667             {
668                 ReferenceReplace( p_vpar,
669                                   p_vpar->picture.i_coding_type,
670                                   NULL );
671                 vout_DestroyPicture( p_vpar->p_vout,
672                                      p_vpar->picture.p_picture );
673                 p_vpar->picture.p_picture = NULL;
674             }
675
676             p_vpar->picture.i_current_structure = 0;
677
678             msg_Warn( p_vpar->p_fifo, "odd number of field pictures" );
679         }
680         else
681         {
682             /* Second field of a frame. We will decode it if, and only if we
683              * have decoded the first field. */
684             if( p_vpar->picture.p_picture == NULL )
685             {
686                 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
687                       && p_vpar->sequence.p_backward == NULL) )
688                 {
689                     /* Exceptionnally, parse the picture if it is I. We need
690                      * this in case of an odd number of field pictures, if the
691                      * previous picture is not intra, we desperately need a
692                      * new reference picture. OK, this is kind of kludgy. */
693                     p_vpar->picture.i_current_structure = 0;
694                 }
695                 else
696                 {
697                     b_parsable = 0;
698                 }
699             }
700             else
701             {
702                 /* We suppose we have the reference pictures, since we already
703                  * decoded the first field and the second field will not need
704                  * any extra reference picture. There is a special case of
705                  * P field being the second field of an I field, but ISO/IEC
706                  * 13818-2 section 7.6.3.5 specifies that this P field will
707                  * not need any reference picture besides the I field. So far
708                  * so good. */
709                 b_parsable = 1;
710
711                 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
712                     i_previous_coding_type == I_CODING_TYPE &&
713                     p_vpar->sequence.p_forward == NULL )
714                 {
715                     /* This is the special case of section 7.6.3.5. Create
716                      * a fake reference picture (which will not be used)
717                      * but will prevent us from segfaulting in the slice
718                      * parsing. */
719                     static picture_t    fake_picture;
720                     fake_picture.i_planes = 0; /* We will use it later */
721                     p_vpar->sequence.p_forward = &fake_picture;
722                 }
723             }
724         }
725     }
726
727     if( !p_vpar->picture.i_current_structure )
728     {
729         /* First field of a frame, or new frame picture. */
730         int     i_repeat_field;
731
732         /* Do we have the reference pictures ? */
733         b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
734                          p_vpar->picture.b_concealment_mv) &&
735                         (p_vpar->sequence.p_backward == NULL)) ||
736                          /* p_backward will become p_forward later */
737                        ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
738                         (p_vpar->sequence.p_forward == NULL ||
739                          p_vpar->sequence.p_backward == NULL)));
740
741         /* Compute the number of times the frame will be emitted by the
742          * decoder (number of half-periods). */
743         if( p_vpar->sequence.b_progressive )
744         {
745             i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
746                                 + p_vpar->picture.b_top_field_first) * 2;
747         }
748         else
749         {
750             if( p_vpar->picture.b_progressive )
751             {
752                 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
753             }
754             else
755             {
756                 i_repeat_field = 2;
757             }
758         }
759
760         /* Warn synchro we have a new picture (updates pictures index). */
761         vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
762                                 i_repeat_field );
763
764         if( b_parsable )
765         {
766             /* Does synchro say we have enough time to decode it ? */
767             b_parsable = vpar_SynchroChoose( p_vpar,
768                                p_vpar->picture.i_coding_type, i_structure );
769         }
770     }
771
772     if( !b_parsable )
773     {
774         /* Update the reference pointers. */
775         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
776
777         /* Update context. */
778         if( i_structure != FRAME_STRUCTURE )
779         {
780             if( (p_vpar->picture.i_current_structure | i_structure)
781                     == FRAME_STRUCTURE )
782             {
783                 /* The frame is complete. */
784                 p_vpar->picture.i_current_structure = 0;
785
786                 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
787             }
788             else
789             {
790                 p_vpar->picture.i_current_structure = i_structure;
791             }
792         }
793         else
794         {
795             /* Warn Synchro we have trashed a picture. */
796             vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
797         }
798         p_vpar->picture.p_picture = NULL;
799
800         return;
801     }
802
803     /* OK, now we are sure we will decode the picture. */
804     p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
805
806     p_vpar->picture.b_error = 0;
807
808     if( !p_vpar->picture.i_current_structure )
809     {
810         /* This is a new frame. Get a structure from the video_output. */
811         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
812                                  p_vpar->picture.b_progressive,
813                                  p_vpar->picture.b_top_field_first,
814                                  p_vpar->picture.b_repeat_first_field ) )
815                  == NULL )
816         {
817             if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
818             {
819                 return;
820             }
821             msleep( VOUT_OUTMEM_SLEEP );
822         }
823
824         /* Initialize values. */
825         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
826         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
827
828         /* Update the reference pointers. */
829         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
830     }
831
832     /* Initialize picture data for decoding. */
833     p_vpar->picture.i_current_structure |= i_structure;
834     p_vpar->picture.i_structure = i_structure;
835     p_vpar->picture.b_second_field =
836         (i_structure != p_vpar->picture.i_current_structure);
837     p_vpar->picture.b_current_field =
838         (i_structure == BOTTOM_FIELD );
839     p_vpar->picture.i_lum_stride = p_vpar->picture.p_picture->Y_PITCH
840         << ( 1 - p_vpar->picture.b_frame_structure );
841     p_vpar->picture.i_chrom_stride = p_vpar->picture.p_picture->U_PITCH
842         << ( 1 - p_vpar->picture.b_frame_structure );
843     /* We suppose the pitch is the same for U and V planes. */
844     p_vpar->picture.i_field_width = p_vpar->sequence.i_width
845         << ( 1 - p_vpar->picture.b_frame_structure );
846
847     if( !p_vpar->p_fifo->p_stream_ctrl->b_grayscale )
848     {
849         switch( p_vpar->sequence.i_chroma_format )
850         {
851         case CHROMA_422:
852             p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock422;
853             break;
854         case CHROMA_444:
855             p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock444;
856             break;
857         case CHROMA_420:
858         default:
859             p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock420;
860             break;
861         }
862     }
863     else
864     {
865         p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblockBW;
866     }
867
868
869     if( p_vpar->sequence.b_mpeg2 )
870     {
871         static f_picture_data_t ppf_picture_data[4][4] =
872         {
873             {
874                 NULL, NULL, NULL, NULL
875             },
876             {
877                 /* TOP_FIELD */
878 #if (VPAR_OPTIM_LEVEL > 1)
879                 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
880                 vpar_PictureData2BT
881 #else
882                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
883                 vpar_PictureDataGENERIC
884 #endif
885             },
886             {
887                 /* BOTTOM_FIELD */
888 #if (VPAR_OPTIM_LEVEL > 1)
889                 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
890                 vpar_PictureData2BB
891 #else
892                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
893                 vpar_PictureDataGENERIC
894 #endif
895             },
896             {
897                 /* FRAME_PICTURE */
898 #if (VPAR_OPTIM_LEVEL > 0)
899                 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
900                 vpar_PictureData2BF
901 #else
902                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
903                 vpar_PictureDataGENERIC
904 #endif
905             }
906         };
907
908         ppf_picture_data[p_vpar->picture.i_structure]
909                         [p_vpar->picture.i_coding_type]( p_vpar );
910     }
911     else
912     {
913 #if (VPAR_OPTIM_LEVEL > 1)
914         static f_picture_data_t pf_picture_data[5] =
915         { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
916           vpar_PictureData1D };
917
918         pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
919 #else
920         vpar_PictureDataGENERIC( p_vpar );
921 #endif
922     }
923
924     /* Wait for all the macroblocks to be decoded. */
925     p_vpar->pool.pf_wait_pool( &p_vpar->pool );
926
927     /* Re-spawn decoder threads if the user changed settings. */
928     vpar_SpawnPool( p_vpar );
929
930     if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
931     {
932         return;
933     }
934
935     if( p_vpar->sequence.p_forward != NULL &&
936         p_vpar->sequence.p_forward->i_planes == 0 )
937     {
938         /* This can only happen with the fake picture created for section
939          * 7.6.3.5. Clean up our mess. */
940         p_vpar->sequence.p_forward = NULL;
941     }
942
943     if( p_vpar->picture.b_error )
944     {
945         /* Trash picture. */
946         p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
947
948         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
949                          p_vpar->picture.i_structure, 1 );
950         vout_DestroyPicture( p_vpar->p_vout, P_picture );
951
952         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
953
954         /* Prepare context for the next picture. */
955         P_picture = NULL;
956         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
957             p_vpar->picture.i_current_structure = 0;
958     }
959     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
960     {
961         /* Frame completely parsed. */
962         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
963                          p_vpar->picture.i_structure, 0 );
964         vout_DisplayPicture( p_vpar->p_vout, P_picture );
965
966         /* Prepare context for the next picture. */
967         P_picture = NULL;
968         p_vpar->picture.i_current_structure = 0;
969     }
970 #undef P_picture
971 }
972
973 /*****************************************************************************
974  * ExtensionAndUserData : Parse the extension_and_user_data structure
975  *****************************************************************************/
976 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
977 {
978     while( !p_vpar->p_fifo->b_die )
979     {
980         NextStartCode( &p_vpar->bit_stream );
981         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
982         {
983         case EXTENSION_START_CODE:
984             RemoveBits32( &p_vpar->bit_stream );
985             switch( GetBits( &p_vpar->bit_stream, 4 ) )
986             {
987             case SEQUENCE_DISPLAY_EXTENSION_ID:
988                 SequenceDisplayExtension( p_vpar );
989                 break;
990             case QUANT_MATRIX_EXTENSION_ID:
991                 QuantMatrixExtension( p_vpar );
992                 break;
993             case SEQUENCE_SCALABLE_EXTENSION_ID:
994                 SequenceScalableExtension( p_vpar );
995                 break;
996             case PICTURE_DISPLAY_EXTENSION_ID:
997                 PictureDisplayExtension( p_vpar );
998                 break;
999             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
1000                 PictureSpatialScalableExtension( p_vpar );
1001                 break;
1002             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
1003                 PictureTemporalScalableExtension( p_vpar );
1004                 break;
1005             case COPYRIGHT_EXTENSION_ID:
1006                 CopyrightExtension( p_vpar );
1007                 break;
1008             default:
1009                 break;
1010             }
1011             break;
1012
1013         case USER_DATA_START_CODE:
1014             RemoveBits32( &p_vpar->bit_stream );
1015             /* Wait for the next start code */
1016             break;
1017
1018         default:
1019             return;
1020         }
1021     }
1022 }
1023
1024
1025 /*****************************************************************************
1026  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
1027  *****************************************************************************/
1028
1029 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
1030 {
1031     /* We don't care sequence_display_extension. */
1032     /* video_format */
1033     RemoveBits( &p_vpar->bit_stream, 3 );
1034     if( GetBits( &p_vpar->bit_stream, 1 ) )
1035     {
1036         /* Two bytes for color_desciption */
1037         RemoveBits( &p_vpar->bit_stream, 16 );
1038         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
1039     }
1040     /* display_horizontal and vertical_size and a marker_bit */
1041     RemoveBits( &p_vpar->bit_stream, 29 );
1042 }
1043
1044
1045 /*****************************************************************************
1046  * QuantMatrixExtension : Load quantization matrices for luminance           *
1047  *                        and chrominance                                    *
1048  *****************************************************************************/
1049
1050 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
1051 {
1052     if( GetBits( &p_vpar->bit_stream, 1 ) )
1053     {
1054         /* Load intra_quantiser_matrix for luminance. */
1055         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1056     }
1057     else
1058     {
1059         /* Use the default matrix. */
1060         LinkMatrix( &p_vpar->sequence.intra_quant,
1061                     p_vpar->pi_default_intra_quant );
1062     }
1063     if( GetBits( &p_vpar->bit_stream, 1 ) )
1064     {
1065         /* Load non_intra_quantiser_matrix for luminance. */
1066         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1067     }
1068     else
1069     {
1070         /* Use the default matrix. */
1071         LinkMatrix( &p_vpar->sequence.nonintra_quant,
1072                     p_vpar->pi_default_nonintra_quant );
1073     }
1074     if( GetBits( &p_vpar->bit_stream, 1 ) )
1075     {
1076         /* Load intra_quantiser_matrix for chrominance. */
1077         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1078     }
1079     else
1080     {
1081         /* Link the chrominance intra matrix to the luminance one. */
1082         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1083                     p_vpar->sequence.intra_quant.pi_matrix );
1084     }
1085     if( GetBits( &p_vpar->bit_stream, 1 ) )
1086     {
1087         /* Load non_intra_quantiser_matrix for chrominance. */
1088         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1089     }
1090     else
1091     {
1092         /* Link the chrominance nonintra matrix to the luminance one. */
1093         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1094                     p_vpar->sequence.nonintra_quant.pi_matrix );
1095     }
1096 }
1097
1098
1099 /*****************************************************************************
1100  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
1101  *                             structure to handle scalable coding           *
1102  *****************************************************************************/
1103
1104 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1105 {
1106     /* We don't care about anything scalable except the scalable mode. */
1107     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1108     /* The length of the structure depends on the value of the scalable_mode */
1109     {
1110         case 1:
1111             RemoveBits32( &p_vpar->bit_stream );
1112             RemoveBits( &p_vpar->bit_stream, 21 );
1113             break;
1114         case 2:
1115             RemoveBits( &p_vpar->bit_stream, 12 );
1116             break;
1117         default:
1118             RemoveBits( &p_vpar->bit_stream, 4 );
1119     }
1120
1121 }
1122 /*****************************************************************************
1123  * PictureDisplayExtension : Parse the picture_display_extension structure   *
1124  *****************************************************************************/
1125
1126 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1127 {
1128     /* Number of frame center offset */
1129     int i_nb, i_dummy;
1130     /* I am not sure it works but it should
1131         (fewer tests than shown in §6.3.12) */
1132     /* FIXME: is VLC_TRUE always 1 ? */
1133     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1134                                             p_vpar->picture.b_repeat_first_field +
1135                                             p_vpar->picture.b_top_field_first
1136                            : ( p_vpar->picture.b_frame_structure + 1 ) +
1137                              p_vpar->picture.b_repeat_first_field;
1138     for( i_dummy = 0; i_dummy < i_nb && !p_vpar->p_fifo->b_die
1139                        && !p_vpar->p_fifo->b_error; i_dummy++ )
1140     {
1141         RemoveBits( &p_vpar->bit_stream, 17 );
1142         RemoveBits( &p_vpar->bit_stream, 17 );
1143     }
1144 }
1145
1146
1147 /*****************************************************************************
1148  * PictureSpatialScalableExtension                                           *
1149  *****************************************************************************/
1150
1151 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1152 {
1153     /* That's scalable, so we trash it */
1154     RemoveBits32( &p_vpar->bit_stream );
1155     RemoveBits( &p_vpar->bit_stream, 16 );
1156 }
1157
1158
1159 /*****************************************************************************
1160  * PictureTemporalScalableExtension                                          *
1161  *****************************************************************************/
1162
1163 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1164 {
1165     /* Scalable again, trashed again */
1166     RemoveBits( &p_vpar->bit_stream, 23 );
1167 }
1168
1169
1170 /*****************************************************************************
1171  * CopyrightExtension : Keeps some legal informations                        *
1172  *****************************************************************************/
1173
1174 static void CopyrightExtension( vpar_thread_t * p_vpar )
1175 {
1176     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1177     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1178         /* A flag that says whether the copyright information is significant */
1179     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1180         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1181     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1182         /* Reserved bits */
1183     RemoveBits( &p_vpar->bit_stream, 8 );
1184         /* The copyright_number is split in three parts */
1185         /* first part */
1186     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1187     RemoveBits( &p_vpar->bit_stream, 1 );
1188         /* second part */
1189     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1190     RemoveBits( &p_vpar->bit_stream, 1 );
1191         /* third part and sum */
1192     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1193                                       ( (u64)i_copyright_nb_2 << 22 ) |
1194                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1195 }