]> git.sesse.net Git - vlc/blob - src/video_decoder/vpar_headers.c
d3bb919e2d29043c01ec52c05d787b287885d376
[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.6 2001/07/26 11:36:52 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             p_vpar->picture.p_picture = NULL;
606         }
607
608         p_vpar->picture.i_current_structure = 0;
609
610         intf_WarnMsg( 2, "Odd number of field pictures." );
611     }
612
613     /* Do we have the reference pictures ? */
614     b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
615                     (p_vpar->sequence.p_backward == NULL)) ||
616                      /* p_backward will become p_forward later */
617                    ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
618                     (p_vpar->sequence.p_forward == NULL ||
619                      p_vpar->sequence.p_backward == NULL)));
620
621     if( p_vpar->picture.i_current_structure )
622     {
623         /* Second field of a frame. We will decode it if, and only if we
624          * have decoded the first field. */
625         if( b_parsable )
626         {
627             if( p_vpar->picture.p_picture == NULL )
628             {
629                 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
630                       && p_vpar->sequence.p_backward == NULL) )
631                 {
632                     /* Exceptionnally, parse the picture if it is I. We need
633                      * this in case of an odd number of field pictures, if the
634                      * previous picture is not intra, we desperately need a
635                      * new reference picture. OK, this is kind of kludgy. */
636                     p_vpar->picture.i_current_structure = 0;
637                 }
638                 else
639                 {
640                     b_parsable = 0;
641                 }
642             }
643         }
644     }
645     else
646     {
647         int     i_repeat_field;
648
649         /* Compute the number of times the frame will be emitted by the
650          * decoder (number of half-periods). */
651         if( p_vpar->sequence.b_progressive )
652         {
653             i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
654                                 + p_vpar->picture.b_top_field_first) * 2;
655         }
656         else
657         {
658             if( p_vpar->picture.b_progressive )
659             {
660                 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
661             }
662             else
663             {
664                 i_repeat_field = 2;
665             }
666         }
667
668         /* Warn synchro we have a new picture (updates pictures index). */
669         vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
670                                 i_repeat_field );
671
672         if( b_parsable )
673         {
674             /* Does synchro say we have enough time to decode it ? */
675             b_parsable = vpar_SynchroChoose( p_vpar,
676                                p_vpar->picture.i_coding_type, i_structure );
677         }
678     }
679
680     if( !b_parsable )
681     {
682         /* Update the reference pointers. */
683         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
684
685         /* Update context. */
686         if( i_structure != FRAME_STRUCTURE )
687         {
688             if( (p_vpar->picture.i_current_structure | i_structure)
689                     == FRAME_STRUCTURE )
690             {
691                 /* The frame is complete. */
692                 p_vpar->picture.i_current_structure = 0;
693
694                 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
695             }
696             else
697             {
698                 p_vpar->picture.i_current_structure = i_structure;
699             }
700         }
701         else
702         {
703             /* Warn Synchro we have trashed a picture. */
704             vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
705         }
706         p_vpar->picture.p_picture = NULL;
707
708         return;
709     }
710
711     /* OK, now we are sure we will decode the picture. */
712 #ifdef STATS
713     p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
714 #endif
715
716 #define P_picture p_vpar->picture.p_picture
717     p_vpar->picture.b_error = 0;
718     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
719
720     if( !p_vpar->picture.i_current_structure )
721     {
722         /* This is a new frame. Get a structure from the video_output. */
723         while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
724                               /* XXX */ 99+p_vpar->sequence.i_chroma_format,
725                                         p_vpar->sequence.i_width,
726                                         p_vpar->sequence.i_height ) )
727              == NULL )
728         {
729             intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
730             if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
731             {
732                 return;
733             }
734             msleep( VPAR_OUTMEM_SLEEP );
735         }
736
737         /* Initialize values. */
738         vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
739         P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
740         P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
741         p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
742                     << ( 1 - p_vpar->picture.b_frame_structure ) );
743         p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
744                     << ( 1 - p_vpar->picture.b_frame_structure ));
745
746 /* FIXME ! remove asap ?? */
747 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
748
749         /* Update the reference pointers. */
750         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
751     }
752     p_vpar->picture.i_current_structure |= i_structure;
753     p_vpar->picture.i_structure = i_structure;
754
755     /* Initialize picture data for decoding. */
756     if( i_structure == BOTTOM_FIELD )
757     {
758         i_mb_base = p_vpar->sequence.i_mb_size >> 1;
759         p_vpar->mb.i_l_y = 1;
760         p_vpar->mb.i_c_y = 1;
761     }
762     else
763     {
764         i_mb_base = 0;
765         p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
766     }
767     p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
768
769     /* Extension and User data. */
770     ExtensionAndUserData( p_vpar );
771
772     /* This is an MP@ML decoder, please note that neither of the following
773      * assertions can be true :
774      *   p_vpar->sequence.i_chroma_format != CHROMA_420
775      *   p_vpar->sequence.i_height > 2800
776      *   p_vpar->sequence.i_scalable_mode == SC_DP
777      * Be cautious if you try to use the decoder for other profiles and
778      * levels.
779      */
780     if( p_vpar->sequence.b_mpeg2 )
781     {
782         static f_picture_data_t ppf_picture_data[4][4] =
783         {
784             {
785                 NULL, NULL, NULL, NULL
786             },
787             {
788                 /* TOP_FIELD */
789 #if (VPAR_OPTIM_LEVEL > 1)
790                 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
791                 vpar_PictureData2BT
792 #else
793                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
794                 vpar_PictureDataGENERIC
795 #endif
796             },
797             {
798                 /* BOTTOM_FIELD */
799 #if (VPAR_OPTIM_LEVEL > 1)
800                 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
801                 vpar_PictureData2BB
802 #else
803                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
804                 vpar_PictureDataGENERIC
805 #endif
806             },
807             {
808                 /* FRAME_PICTURE */
809 #if (VPAR_OPTIM_LEVEL > 0)
810                 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
811                 vpar_PictureData2BF
812 #else
813                 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
814                 vpar_PictureDataGENERIC
815 #endif
816             }
817         };
818
819         ppf_picture_data[p_vpar->picture.i_structure]
820                         [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
821     }
822     else
823     {
824 #if (VPAR_OPTIM_LEVEL > 0)
825         static f_picture_data_t pf_picture_data[5] =
826         { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
827           vpar_PictureData1D };
828
829         pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
830 #else
831         vpar_PictureDataGENERIC( p_vpar, i_mb_base );
832 #endif
833     }
834
835     /* Wait for all the macroblocks to be decoded. */
836     p_vpar->pool.pf_wait_pool( &p_vpar->pool );
837
838     /* Re-spawn decoder threads if the user changed settings. */
839     vpar_SpawnPool( p_vpar );
840
841     if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
842     {
843         return;
844     }
845
846     if( p_vpar->picture.b_error )
847     {
848         /* Trash picture. */
849 #ifdef STATS
850         p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
851 #endif
852
853         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
854                          p_vpar->picture.i_structure, 1 );
855         vout_DestroyPicture( p_vpar->p_vout, P_picture );
856
857         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
858
859         /* Prepare context for the next picture. */
860         P_picture = NULL;
861         if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
862             p_vpar->picture.i_current_structure = 0;
863     }
864     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
865     {
866         /* Frame completely parsed. */
867         vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
868                          p_vpar->picture.i_structure, 0 );
869         vout_DisplayPicture( p_vpar->p_vout, P_picture );
870
871         /* Prepare context for the next picture. */
872         P_picture = NULL;
873         p_vpar->picture.i_current_structure = 0;
874     }
875 #undef P_picture
876 }
877
878 /*****************************************************************************
879  * ExtensionAndUserData : Parse the extension_and_user_data structure
880  *****************************************************************************/
881 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
882 {
883     while( !p_vpar->p_fifo->b_die )
884     {
885         NextStartCode( &p_vpar->bit_stream );
886         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
887         {
888         case EXTENSION_START_CODE:
889             RemoveBits32( &p_vpar->bit_stream );
890             switch( GetBits( &p_vpar->bit_stream, 4 ) )
891             {
892             case SEQUENCE_DISPLAY_EXTENSION_ID:
893                 SequenceDisplayExtension( p_vpar );
894                 break;
895             case QUANT_MATRIX_EXTENSION_ID:
896                 QuantMatrixExtension( p_vpar );
897                 break;
898             case SEQUENCE_SCALABLE_EXTENSION_ID:
899                 SequenceScalableExtension( p_vpar );
900                 break;
901             case PICTURE_DISPLAY_EXTENSION_ID:
902                 PictureDisplayExtension( p_vpar );
903                 break;
904             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
905                 PictureSpatialScalableExtension( p_vpar );
906                 break;
907             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
908                 PictureTemporalScalableExtension( p_vpar );
909                 break;
910             case COPYRIGHT_EXTENSION_ID:
911                 CopyrightExtension( p_vpar );
912                 break;
913             default:
914                 break;
915             }
916             break;
917
918         case USER_DATA_START_CODE:
919             RemoveBits32( &p_vpar->bit_stream );
920             /* Wait for the next start code */
921             break;
922
923         default:
924             return;
925         }
926     }
927 }
928
929
930 /*****************************************************************************
931  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
932  *****************************************************************************/
933
934 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
935 {
936     /* We don't care sequence_display_extension. */
937     /* video_format */
938     RemoveBits( &p_vpar->bit_stream, 3 );
939     if( GetBits( &p_vpar->bit_stream, 1 ) )
940     {
941         /* Two bytes for color_desciption */
942         RemoveBits( &p_vpar->bit_stream, 16 );
943         p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
944     }
945     /* display_horizontal and vertical_size and a marker_bit */
946     RemoveBits( &p_vpar->bit_stream, 29 );
947 }
948
949
950 /*****************************************************************************
951  * QuantMatrixExtension : Load quantization matrices for luminance           *
952  *                        and chrominance                                    *
953  *****************************************************************************/
954
955 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
956 {
957     if( GetBits( &p_vpar->bit_stream, 1 ) )
958     {
959         /* Load intra_quantiser_matrix for luminance. */
960         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
961     }
962     else
963     {
964         /* Use the default matrix. */
965         LinkMatrix( &p_vpar->sequence.intra_quant,
966                     p_vpar->pi_default_intra_quant );
967     }
968     if( GetBits( &p_vpar->bit_stream, 1 ) )
969     {
970         /* Load non_intra_quantiser_matrix for luminance. */
971         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
972     }
973     else
974     {
975         /* Use the default matrix. */
976         LinkMatrix( &p_vpar->sequence.nonintra_quant,
977                     p_vpar->pi_default_nonintra_quant );
978     }
979     if( GetBits( &p_vpar->bit_stream, 1 ) )
980     {
981         /* Load intra_quantiser_matrix for chrominance. */
982         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
983     }
984     else
985     {
986         /* Link the chrominance intra matrix to the luminance one. */
987         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
988                     p_vpar->sequence.intra_quant.pi_matrix );
989     }
990     if( GetBits( &p_vpar->bit_stream, 1 ) )
991     {
992         /* Load non_intra_quantiser_matrix for chrominance. */
993         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
994     }
995     else
996     {
997         /* Link the chrominance intra matrix to the luminance one. */
998         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
999                     p_vpar->sequence.intra_quant.pi_matrix );
1000     }
1001     if( GetBits( &p_vpar->bit_stream, 1 ) )
1002     {
1003         /* Load non_intra_quantiser_matrix for chrominance. */
1004         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1005     }
1006     else
1007     {
1008         /* Link the chrominance nonintra matrix to the luminance one. */
1009         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1010                     p_vpar->sequence.nonintra_quant.pi_matrix );
1011     }
1012 }
1013
1014
1015 /*****************************************************************************
1016  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
1017  *                             structure to handle scalable coding           *
1018  *****************************************************************************/
1019
1020 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1021 {
1022     /* We don't care about anything scalable except the scalable mode. */
1023     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1024     /* The length of the structure depends on the value of the scalable_mode */
1025     {
1026         case 1:
1027             RemoveBits32( &p_vpar->bit_stream );
1028             RemoveBits( &p_vpar->bit_stream, 21 );
1029             break;
1030         case 2:
1031             RemoveBits( &p_vpar->bit_stream, 12 );
1032             break;
1033         default:
1034             RemoveBits( &p_vpar->bit_stream, 4 );
1035     }
1036
1037 }
1038 /*****************************************************************************
1039  * PictureDisplayExtension : Parse the picture_display_extension structure   *
1040  *****************************************************************************/
1041
1042 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1043 {
1044     /* Number of frame center offset */
1045     int i_nb, i_dummy;
1046     /* I am not sure it works but it should
1047         (fewer tests than shown in §6.3.12) */
1048     i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1049                                             p_vpar->picture.b_repeat_first_field +
1050                                             p_vpar->picture.b_top_field_first
1051                            : ( p_vpar->picture.b_frame_structure + 1 ) +
1052                              p_vpar->picture.b_repeat_first_field;
1053     for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1054     {
1055         RemoveBits( &p_vpar->bit_stream, 17 );
1056         RemoveBits( &p_vpar->bit_stream, 17 );
1057     }
1058 }
1059
1060
1061 /*****************************************************************************
1062  * PictureSpatialScalableExtension                                           *
1063  *****************************************************************************/
1064
1065 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1066 {
1067     /* That's scalable, so we trash it */
1068     RemoveBits32( &p_vpar->bit_stream );
1069     RemoveBits( &p_vpar->bit_stream, 16 );
1070 }
1071
1072
1073 /*****************************************************************************
1074  * PictureTemporalScalableExtension                                          *
1075  *****************************************************************************/
1076
1077 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1078 {
1079     /* Scalable again, trashed again */
1080     RemoveBits( &p_vpar->bit_stream, 23 );
1081 }
1082
1083
1084 /*****************************************************************************
1085  * CopyrightExtension : Keeps some legal informations                        *
1086  *****************************************************************************/
1087
1088 static void CopyrightExtension( vpar_thread_t * p_vpar )
1089 {
1090     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1091     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1092         /* A flag that says whether the copyright information is significant */
1093     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1094         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1095     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1096         /* Reserved bits */
1097     RemoveBits( &p_vpar->bit_stream, 8 );
1098         /* The copyright_number is split in three parts */
1099         /* first part */
1100     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1101     RemoveBits( &p_vpar->bit_stream, 1 );
1102         /* second part */
1103     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1104     RemoveBits( &p_vpar->bit_stream, 1 );
1105         /* third part and sum */
1106     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1107                                       ( (u64)i_copyright_nb_2 << 22 ) |
1108                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
1109 }