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