]> git.sesse.net Git - vlc/blob - plugins/mpeg_vdec/vpar_headers.c
* ALL: the first libvlc commit.
[vlc] / plugins / mpeg_vdec / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: vpar_headers.c,v 1.23 2002/06/01 12:32:00 sam 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 "vdec_ext-plugins.h"
36 #include "vpar_pool.h"
37 #include "video_parser.h"
38 #include "video_decoder.h"
39
40 /*
41  * Local prototypes
42  */
43 static inline void NextStartCode( bit_stream_t * );
44 static void SequenceHeader( vpar_thread_t * p_vpar );
45 static void GroupHeader( vpar_thread_t * p_vpar );
46 static void PictureHeader( vpar_thread_t * p_vpar );
47 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
48 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
49 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
50 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
51 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
52 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
53 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
54 static void CopyrightExtension( vpar_thread_t * p_vpar );
55
56 /*
57  * Standard variables
58  */
59
60 /*****************************************************************************
61  * pi_default_intra_quant : default quantization matrix
62  *****************************************************************************/
63 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
64 {
65     8,  16, 19, 22, 26, 27, 29, 34,
66     16, 16, 22, 24, 27, 29, 34, 37,
67     19, 22, 26, 27, 29, 34, 34, 38,
68     22, 22, 26, 27, 29, 34, 37, 40,
69     22, 26, 27, 29, 32, 35, 40, 48,
70     26, 27, 29, 32, 35, 40, 48, 58,
71     26, 27, 29, 34, 38, 46, 56, 69,
72     27, 29, 35, 38, 46, 56, 69, 83
73 };
74
75 /*****************************************************************************
76  * pi_default_nonintra_quant : default quantization matrix
77  *****************************************************************************/
78 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
79 {
80     16, 16, 16, 16, 16, 16, 16, 16,
81     16, 16, 16, 16, 16, 16, 16, 16,
82     16, 16, 16, 16, 16, 16, 16, 16,
83     16, 16, 16, 16, 16, 16, 16, 16,
84     16, 16, 16, 16, 16, 16, 16, 16,
85     16, 16, 16, 16, 16, 16, 16, 16,
86     16, 16, 16, 16, 16, 16, 16, 16,
87     16, 16, 16, 16, 16, 16, 16, 16
88 };
89
90 /*****************************************************************************
91  * pi_scan : zig-zag and alternate scan patterns
92  *****************************************************************************/
93 u8 pi_scan[2][64] ATTR_ALIGN(16) =
94 {
95     { /* Zig-Zag pattern */
96         0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
97         12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
98         35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
99         58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
100     },
101     { /* Alternate scan pattern */
102         0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
103         41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
104         51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
105         53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
106     }
107 };
108
109 /*
110  * Local inline functions.
111  */
112
113 /*****************************************************************************
114  * ReferenceUpdate : Update the reference pointers when we have a new picture
115  *****************************************************************************/
116 static void inline ReferenceUpdate( vpar_thread_t * p_vpar,
117                                     int i_coding_type, picture_t * p_newref )
118 {
119     if( i_coding_type != B_CODING_TYPE )
120     {
121         if( p_vpar->sequence.p_forward != NULL )
122         {
123             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
124         }
125         if( p_vpar->sequence.p_backward != NULL )
126         {
127             vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
128                               vpar_SynchroDate( p_vpar ) );
129         }
130         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
131         p_vpar->sequence.p_backward = p_newref;
132         if( p_newref != NULL )
133         {
134             vout_LinkPicture( p_vpar->p_vout, p_newref );
135         }
136     }
137     else if( p_newref != NULL )
138     {
139         /* Put date immediately. */
140         vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
141     }
142 }
143
144 /*****************************************************************************
145  * ReferenceReplace : Replace the last reference pointer when we destroy
146  *                    a picture
147  *****************************************************************************/
148 static void inline ReferenceReplace( vpar_thread_t * p_vpar,
149                                      int i_coding_type, picture_t * p_newref )
150 {
151     if( i_coding_type != B_CODING_TYPE )
152     {
153         if( p_vpar->sequence.p_backward != NULL )
154         {
155             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
156         }
157         p_vpar->sequence.p_backward = p_newref;
158         if( p_newref != NULL )
159         {
160             vout_LinkPicture( p_vpar->p_vout, p_newref );
161         }
162     }
163 }
164
165 /*****************************************************************************
166  * LoadMatrix : Load a quantization matrix
167  *****************************************************************************/
168 static inline void LoadMatrix( vpar_thread_t * p_vpar,
169                                quant_matrix_t * p_matrix )
170 {
171     int i_dummy;
172
173     if( !p_matrix->b_allocated )
174     {
175         /* Allocate a piece of memory to load the matrix. */
176         if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
177         {
178             msg_Err( p_vpar->p_fifo, "out of memory" );
179             p_vpar->p_fifo->b_error = 1;
180             return;
181         }
182         p_matrix->b_allocated = 1;
183     }
184
185     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
186     {
187         p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
188              = GetBits( &p_vpar->bit_stream, 8 );
189     }
190 }
191
192 /*****************************************************************************
193  * LinkMatrix : Link a quantization matrix to another
194  *****************************************************************************/
195 static inline void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
196 {
197     if( p_matrix->b_allocated )
198     {
199         /* Deallocate the piece of memory. */
200         free( p_matrix->pi_matrix );
201         p_matrix->b_allocated = 0;
202     }
203
204     p_matrix->pi_matrix = pi_array;
205 }
206
207 /*****************************************************************************
208  * ChromaToFourCC: Return a FourCC value used by the video output.
209  *****************************************************************************/
210 static inline u64 ChromaToFourCC( int i_chroma )
211 {
212     switch( i_chroma )
213     {
214         case CHROMA_420:
215             return FOURCC_I420;
216
217         case CHROMA_422:
218             return FOURCC_I422;
219
220         case CHROMA_444:
221             return FOURCC_I444;
222
223         default:
224             /* This can't happen */
225             return 0xdeadbeef;
226     }
227 }
228
229 /*
230  * Exported functions.
231  */
232
233 /*****************************************************************************
234  * vpar_NextSequenceHeader : Find the next sequence header
235  *****************************************************************************/
236 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
237 {
238     while( !p_vpar->p_fifo->b_die )
239     {
240         NextStartCode( &p_vpar->bit_stream );
241         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
242         {
243             return 0;
244         }
245         RemoveBits( &p_vpar->bit_stream, 8 );
246     }
247     return 1;
248 }
249
250 /*****************************************************************************
251  * vpar_ParseHeader : Parse the next header
252  *****************************************************************************/
253 int vpar_ParseHeader( vpar_thread_t * p_vpar )
254 {
255     NextStartCode( &p_vpar->bit_stream );
256     switch( GetBits32( &p_vpar->bit_stream ) )
257     {
258     case SEQUENCE_HEADER_CODE:
259         p_vpar->c_sequences++;
260         SequenceHeader( p_vpar );
261         return 0;
262
263     case GROUP_START_CODE:
264         GroupHeader( p_vpar );
265         return 0;
266
267     case PICTURE_START_CODE:
268         PictureHeader( p_vpar );
269         return 0;
270
271     case SEQUENCE_END_CODE:
272         msg_Dbg( p_vpar->p_fifo, "sequence end code received" );
273         if( p_vpar->sequence.p_backward != NULL )
274         {
275             p_vpar->sequence.p_backward->b_force = 1;
276         }
277         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
278         return 1;
279
280     default:
281         break;
282     }
283
284     return 0;
285 }
286
287 /*
288  * Following functions are local
289  */
290
291 /*****************************************************************************
292  * SequenceHeader : Parse the next sequence header
293  *****************************************************************************/
294 static void SequenceHeader( vpar_thread_t * p_vpar )
295 {
296 #define RESERVED    -1
297     static int i_frame_rate_table[16] =
298     {
299         0,
300         23 * 1000,
301         24 * 1001,
302         25 * 1001,
303         30 * 1000,
304         30 * 1001,
305         50 * 1001,
306         60 * 1000,
307         60 * 1001,
308         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
309     };
310 #undef RESERVED
311
312     int i_height_save, i_width_save, i_aspect;
313
314     i_height_save = p_vpar->sequence.i_height;
315     i_width_save = p_vpar->sequence.i_width;
316
317     p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
318     p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
319     i_aspect = GetBits( &p_vpar->bit_stream, 4 );
320     p_vpar->sequence.i_frame_rate =
321             i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
322
323     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
324      * constrained_parameters_flag */
325     RemoveBits( &p_vpar->bit_stream, 30 );
326
327     /*
328      * Quantization matrices
329      */
330     if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
331     {
332         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
333     }
334     else
335     {
336         /* Use default matrix. */
337         LinkMatrix( &p_vpar->sequence.intra_quant,
338                     p_vpar->pi_default_intra_quant );
339     }
340
341     if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
342     {
343         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
344     }
345     else
346     {
347         /* Use default matrix. */
348         LinkMatrix( &p_vpar->sequence.nonintra_quant,
349                     p_vpar->pi_default_nonintra_quant );
350     }
351
352     /* Unless later overwritten by a matrix extension, we have the same
353      * matrices for luminance and chrominance. */
354     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
355                 p_vpar->sequence.intra_quant.pi_matrix );
356     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
357                 p_vpar->sequence.nonintra_quant.pi_matrix );
358
359     /*
360      * Sequence Extension
361      */
362     NextStartCode( &p_vpar->bit_stream );
363     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
364     {
365         int                         i_dummy;
366
367         /* Turn the MPEG2 flag on */
368         p_vpar->sequence.b_mpeg2 = 1;
369
370         /* Parse sequence_extension */
371         RemoveBits32( &p_vpar->bit_stream );
372         /* extension_start_code_identifier, profile_and_level_indication */
373         RemoveBits( &p_vpar->bit_stream, 12 );
374         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
375         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
376         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
377         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
378         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
379          * low_delay */
380         RemoveBits( &p_vpar->bit_stream, 22 );
381         /* frame_rate_extension_n */
382         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
383         /* frame_rate_extension_d */
384         p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
385                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
386     }
387     else
388     {
389         /* It's an MPEG-1 stream. Put adequate parameters. */
390         int i_xyratio;
391         static int pi_mpeg1ratio[15] = {
392             10000, 10000,  6735,  7031,  7615,  8055,  8437, 8935,
393              9157,  9815, 10255, 10695, 10950, 11575, 12015
394         };
395
396         if( i_aspect > 1 )
397         {
398                 i_xyratio = p_vpar->sequence.i_height *
399                         pi_mpeg1ratio[i_aspect] / p_vpar->sequence.i_width;
400                 if( 7450 < i_xyratio && i_xyratio < 7550 )
401                 {
402                         i_aspect = 2;
403                 }
404                 else if( 5575 < i_xyratio && i_xyratio < 5675 )
405                 {
406                         i_aspect = 3;
407                 }
408                 else if( 4475 < i_xyratio && i_xyratio < 4575 )
409                 {
410                         i_aspect = 4;
411                 }
412         }
413
414         p_vpar->sequence.b_mpeg2 = 0;
415         p_vpar->sequence.b_progressive = 1;
416         p_vpar->sequence.i_chroma_format = CHROMA_420;
417     }
418
419     /* check whether the input gives a particular aspect ratio */
420     if( p_vpar->p_fifo->p_demux_data
421          && ( *(int*)(p_vpar->p_fifo->p_demux_data) & 0x7 ) )
422     {
423         i_aspect = *(int*)(p_vpar->p_fifo->p_demux_data);
424     }
425
426     /* Store calculated aspect ratio */
427     switch( i_aspect )
428     {
429         case AR_3_4_PICTURE:
430             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
431             break;
432
433         case AR_16_9_PICTURE:
434             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
435             break;
436
437         case AR_221_1_PICTURE:
438             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
439             break;
440
441         case AR_SQUARE_PICTURE:
442         default:
443             p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR
444                     * p_vpar->sequence.i_width / p_vpar->sequence.i_height;
445             break;
446     }
447
448     /* Update sizes */
449     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
450     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
451                                    (p_vpar->sequence.i_height + 15) / 16 :
452                                    2 * ((p_vpar->sequence.i_height + 31) / 32);
453     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
454                                         * p_vpar->sequence.i_mb_height;
455     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
456     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
457     p_vpar->sequence.i_size = p_vpar->sequence.i_width
458                                         * p_vpar->sequence.i_height;
459     switch( p_vpar->sequence.i_chroma_format )
460     {
461     case CHROMA_420:
462         p_vpar->sequence.i_chroma_nb_blocks = 2;
463         p_vpar->sequence.b_chroma_h_subsampled = 1;
464         p_vpar->sequence.b_chroma_v_subsampled = 1;
465         break;
466     case CHROMA_422:
467         p_vpar->sequence.i_chroma_nb_blocks = 4;
468         p_vpar->sequence.b_chroma_h_subsampled = 1;
469         p_vpar->sequence.b_chroma_v_subsampled = 0;
470         break;
471     case CHROMA_444:
472         p_vpar->sequence.i_chroma_nb_blocks = 6;
473         p_vpar->sequence.b_chroma_h_subsampled = 0;
474         p_vpar->sequence.b_chroma_v_subsampled = 0;
475         break;
476     }
477
478 #if 0
479     if(    p_vpar->sequence.i_width != i_width_save
480         || p_vpar->sequence.i_height != i_height_save )
481     {
482          /* FIXME: Warn the video output */
483     }
484 #endif
485
486     /* Extension and User data */
487     ExtensionAndUserData( p_vpar );
488
489     /* Spawn a video output if there is none */
490
491     p_vpar->p_vout = vlc_object_find( p_vpar->p_fifo->p_vlc, VLC_OBJECT_VOUT,
492                                                              FIND_CHILD );
493     
494     if( p_vpar->p_vout )
495     {
496         if( p_vpar->p_vout->render.i_width != p_vpar->sequence.i_width
497              || p_vpar->p_vout->render.i_height != p_vpar->sequence.i_height
498              || p_vpar->p_vout->render.i_chroma != ChromaToFourCC( p_vpar->sequence.i_chroma_format )
499              || p_vpar->p_vout->render.i_aspect != p_vpar->sequence.i_aspect )
500         {
501             /* We are not interested in this format, close this vout */
502             vlc_object_unlink_all( p_vpar->p_vout );
503             vlc_object_release( p_vpar->p_vout );
504             vout_DestroyThread( p_vpar->p_vout );
505             p_vpar->p_vout = NULL;
506         }
507         else
508         {
509             /* This video output is cool! Hijack it. */
510             vlc_object_unlink_all( p_vpar->p_vout );
511             vlc_object_attach( p_vpar->p_vout, p_vpar->p_fifo );
512             vlc_object_release( p_vpar->p_vout );
513         }
514     }
515
516     if( p_vpar->p_vout == NULL )
517     {
518         msg_Dbg( p_vpar->p_fifo, "no vout present, spawning one" );
519
520         p_vpar->p_vout = vout_CreateThread( p_vpar->p_fifo->p_this,
521                            p_vpar->sequence.i_width,
522                            p_vpar->sequence.i_height,
523                            ChromaToFourCC( p_vpar->sequence.i_chroma_format ),
524                            p_vpar->sequence.i_aspect );
525
526         /* Everything failed */
527         if( p_vpar->p_vout == NULL )
528         {
529             msg_Err( p_vpar->p_fifo, "cannot open vout, aborting" );
530             p_vpar->p_fifo->b_error = 1;
531             return;
532         }
533     }
534 }
535
536 /*****************************************************************************
537  * GroupHeader : Parse the next group of pictures header
538  *****************************************************************************/
539 static void GroupHeader( vpar_thread_t * p_vpar )
540 {
541     /* Nothing to do, we don't care. */
542     RemoveBits( &p_vpar->bit_stream, 27 );
543     ExtensionAndUserData( p_vpar );
544 }
545
546 /*****************************************************************************
547  * PictureHeader : Parse the next picture header
548  *****************************************************************************/
549 static void PictureHeader( vpar_thread_t * p_vpar )
550 {
551     int                 i_structure, i_previous_coding_type;
552     vlc_bool_t          b_parsable = 0;
553
554     /* Retrieve the PTS. */
555     CurrentPTS( &p_vpar->bit_stream, &p_vpar->sequence.next_pts,
556                 &p_vpar->sequence.next_dts );
557
558     /* Recover in case of stream discontinuity. */
559     if( p_vpar->sequence.b_expect_discontinuity )
560     {
561         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
562         ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
563         if( p_vpar->picture.p_picture != NULL )
564         {
565             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
566             p_vpar->picture.p_picture = NULL;
567         }
568         p_vpar->picture.i_current_structure = 0;
569         p_vpar->sequence.b_expect_discontinuity = 0;
570     }
571
572     /* Parse the picture header. */
573     RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
574     i_previous_coding_type = p_vpar->picture.i_coding_type;
575     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
576     RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
577
578     if( p_vpar->picture.i_coding_type == P_CODING_TYPE
579         || p_vpar->picture.i_coding_type == B_CODING_TYPE )
580     {
581         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
582         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
583                                             - 1;
584     }
585     if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
586     {
587         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
588         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
589                                             - 1;
590     }
591
592     /* extra_information_picture */
593     while( GetBits( &p_vpar->bit_stream, 1 ) )
594     {
595         RemoveBits( &p_vpar->bit_stream, 8 );
596     }
597
598     /*
599      * Picture Coding Extension
600      */
601     NextStartCode( &p_vpar->bit_stream );
602     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
603     {
604         /* Parse picture_coding_extension */
605         RemoveBits32( &p_vpar->bit_stream );
606         /* extension_start_code_identifier */
607         RemoveBits( &p_vpar->bit_stream, 4 );
608
609         /* Pre-substract 1 for later use in MotionDelta(). */
610         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
611         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
612         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
613         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
614         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
615         i_structure = GetBits( &p_vpar->bit_stream, 2 );
616         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
617         p_vpar->picture.b_frame_pred_frame_dct
618              = GetBits( &p_vpar->bit_stream, 1 );
619         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
620         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
621         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
622         /* Alternate scan */
623         p_vpar->picture.pi_scan =
624             p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
625         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
626         /* chroma_420_type (obsolete) */
627         RemoveBits( &p_vpar->bit_stream, 1 );
628         p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
629
630         /* composite_display_flag */
631         if( GetBits( &p_vpar->bit_stream, 1 ) )
632         {
633             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
634              * sub_carrier_phase */
635             RemoveBits( &p_vpar->bit_stream, 20 );
636         }
637     }
638     else
639     {
640         /* MPEG-1 compatibility flags */
641         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
642         i_structure = FRAME_STRUCTURE;
643         p_vpar->picture.b_top_field_first = 0;
644         p_vpar->picture.b_frame_pred_frame_dct = 1;
645         p_vpar->picture.b_concealment_mv = 0;
646         p_vpar->picture.b_q_scale_type = 0;
647         p_vpar->picture.b_intra_vlc_format = 0;
648         p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
649         p_vpar->picture.b_repeat_first_field = 0;
650         p_vpar->picture.b_progressive = 1;
651     }
652
653     /* Extension and User data. */
654     ExtensionAndUserData( p_vpar );
655
656     p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
657
658     if( p_vpar->picture.i_current_structure )
659     {
660         if ( (i_structure == FRAME_STRUCTURE ||
661               i_structure == p_vpar->picture.i_current_structure) )
662         {
663             /* We don't have the second field of the buffered frame. */
664             if( p_vpar->picture.p_picture != NULL )
665             {
666                 ReferenceReplace( p_vpar,
667                                   p_vpar->picture.i_coding_type,
668                                   NULL );
669                 vout_DestroyPicture( p_vpar->p_vout,
670                                      p_vpar->picture.p_picture );
671                 p_vpar->picture.p_picture = NULL;
672             }
673
674             p_vpar->picture.i_current_structure = 0;
675
676             msg_Warn( p_vpar->p_fifo, "odd number of field pictures" );
677         }
678         else
679         {
680             /* Second field of a frame. We will decode it if, and only if we
681              * have decoded the first field. */
682             if( p_vpar->picture.p_picture == NULL )
683             {
684                 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
685                       && p_vpar->sequence.p_backward == NULL) )
686                 {
687                     /* Exceptionnally, parse the picture if it is I. We need
688                      * this in case of an odd number of field pictures, if the
689                      * previous picture is not intra, we desperately need a
690                      * new reference picture. OK, this is kind of kludgy. */
691                     p_vpar->picture.i_current_structure = 0;
692                 }
693                 else
694                 {
695                     b_parsable = 0;
696                 }
697             }
698             else
699             {
700                 /* We suppose we have the reference pictures, since we already
701                  * decoded the first field and the second field will not need
702                  * any extra reference picture. There is a special case of
703                  * P field being the second field of an I field, but ISO/IEC
704                  * 13818-2 section 7.6.3.5 specifies that this P field will
705                  * not need any reference picture besides the I field. So far
706                  * so good. */
707                 b_parsable = 1;
708
709                 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
710                     i_previous_coding_type == I_CODING_TYPE &&
711                     p_vpar->sequence.p_forward == NULL )
712                 {
713                     /* This is the special case of section 7.6.3.5. Create
714                      * a fake reference picture (which will not be used)
715                      * but will prevent us from segfaulting in the slice
716                      * parsing. */
717                     static picture_t    fake_picture;
718                     fake_picture.i_planes = 0; /* We will use it later */
719                     p_vpar->sequence.p_forward = &fake_picture;
720                 }
721             }
722         }
723     }
724
725     if( !p_vpar->picture.i_current_structure )
726     {
727         /* First field of a frame, or new frame picture. */
728         int     i_repeat_field;
729
730         /* Do we have the reference pictures ? */
731         b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
732                          p_vpar->picture.b_concealment_mv) &&
733                         (p_vpar->sequence.p_backward == NULL)) ||
734                          /* p_backward will become p_forward later */
735                        ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
736                         (p_vpar->sequence.p_forward == NULL ||
737                          p_vpar->sequence.p_backward == NULL)));
738
739         /* Compute the number of times the frame will be emitted by the
740          * decoder (number of half-periods). */
741         if( p_vpar->sequence.b_progressive )
742         {
743             i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
744                                 + p_vpar->picture.b_top_field_first) * 2;
745         }
746         else
747         {
748             if( p_vpar->picture.b_progressive )
749             {
750                 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
751             }
752             else
753             {
754                 i_repeat_field = 2;
755             }
756         }
757
758         /* Warn synchro we have a new picture (updates pictures index). */
759         vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
760                                 i_repeat_field );
761
762         if( b_parsable )
763         {
764             /* Does synchro say we have enough time to decode it ? */
765             b_parsable = vpar_SynchroChoose( p_vpar,
766                                p_vpar->picture.i_coding_type, i_structure );
767         }
768     }
769
770     if( !b_parsable )
771     {
772         /* Update the reference pointers. */
773         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
774
775         /* Update context. */
776         if( i_structure != FRAME_STRUCTURE )
777         {
778             if( (p_vpar->picture.i_current_structure | i_structure)
779                     == FRAME_STRUCTURE )
780             {
781                 /* The frame is complete. */
782                 p_vpar->picture.i_current_structure = 0;
783
784                 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
785             }
786             else
787             {
788                 p_vpar->picture.i_current_structure = i_structure;
789             }
790         }
791         else
792         {
793             /* Warn Synchro we have trashed a picture. */
794             vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
795         }
796         p_vpar->picture.p_picture = NULL;
797
798         return;
799     }
800
801     /* OK, now we are sure we will decode the picture. */
802     p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
803
804 #define P_picture p_vpar->picture.p_picture
805     p_vpar->picture.b_error = 0;
806     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
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     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1133                                             p_vpar->picture.b_repeat_first_field +
1134                                             p_vpar->picture.b_top_field_first
1135                            : ( p_vpar->picture.b_frame_structure + 1 ) +
1136                              p_vpar->picture.b_repeat_first_field;
1137     for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1138     {
1139         RemoveBits( &p_vpar->bit_stream, 17 );
1140         RemoveBits( &p_vpar->bit_stream, 17 );
1141     }
1142 }
1143
1144
1145 /*****************************************************************************
1146  * PictureSpatialScalableExtension                                           *
1147  *****************************************************************************/
1148
1149 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1150 {
1151     /* That's scalable, so we trash it */
1152     RemoveBits32( &p_vpar->bit_stream );
1153     RemoveBits( &p_vpar->bit_stream, 16 );
1154 }
1155
1156
1157 /*****************************************************************************
1158  * PictureTemporalScalableExtension                                          *
1159  *****************************************************************************/
1160
1161 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1162 {
1163     /* Scalable again, trashed again */
1164     RemoveBits( &p_vpar->bit_stream, 23 );
1165 }
1166
1167
1168 /*****************************************************************************
1169  * CopyrightExtension : Keeps some legal informations                        *
1170  *****************************************************************************/
1171
1172 static void CopyrightExtension( vpar_thread_t * p_vpar )
1173 {
1174     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1175     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1176         /* A flag that says whether the copyright information is significant */
1177     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1178         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1179     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1180         /* Reserved bits */
1181     RemoveBits( &p_vpar->bit_stream, 8 );
1182         /* The copyright_number is split in three parts */
1183         /* first part */
1184     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1185     RemoveBits( &p_vpar->bit_stream, 1 );
1186         /* second part */
1187     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1188     RemoveBits( &p_vpar->bit_stream, 1 );
1189         /* third part and sum */
1190     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1191                                       ( (u64)i_copyright_nb_2 << 22 ) |
1192                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1193 }