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