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