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