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