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