]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
Suite du video_parser et du video_decoder.
[vlc] / src / video_parser / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15 #include <X11/Xlib.h>
16 #include <X11/extensions/XShm.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31 #include "vpar_blocks.h"
32 #include "video_parser.h"
33
34 #include "macroblock.h"
35 #include "video_fifo.h"
36 #include "video_decoder.h"
37
38 /*
39  * Local prototypes
40  */
41 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
42 static void GroupHeader( vpar_thread_t * p_vpar )
43 static void PictureHeader( vpar_thread_t * p_vpar )
44 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
45                                         int i_coding_type,
46                                         picture_t * p_newref )
47 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
48                                          int i_coding_type,
49                                          picture_t * p_newref )
50 static void SliceHeader00( vpar_thread_t * p_vpar,
51                            int * pi_mb_address, int i_mb_base,
52                            elem_t * p_y, p_u, p_v, u32 i_vert_code )
53 static void SliceHeader01( vpar_thread_t * p_vpar,
54                            int * pi_mb_address, int i_mb_base,
55                            elem_t * p_y, p_u, p_v, u32 i_vert_code )
56 static void SliceHeader10( vpar_thread_t * p_vpar,
57                            int * pi_mb_address, int i_mb_base,
58                            elem_t * p_y, p_u, p_v, u32 i_vert_code )
59 static void SliceHeader11( vpar_thread_t * p_vpar,
60                            int * pi_mb_address, int i_mb_base,
61                            elem_t * p_y, p_u, p_v, u32 i_vert_code )
62 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
63                                     int * pi_mb_address, int i_mb_base,
64                                     elem_t * p_y, p_u, p_v, u32 i_vert_code )
65 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
66
67 /*****************************************************************************
68  * vpar_NextSequenceHeader : Find the next sequence header
69  *****************************************************************************/
70 void vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
71 {
72     while( !p_vpar->b_die )
73     {
74         NextStartCode( p_vpar );
75         if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_START_CODE )
76             return;
77     }
78 }
79
80 /*****************************************************************************
81  * vpar_ParseHeader : Parse the next header
82  *****************************************************************************/
83 int vpar_ParseHeader( vpar_thread_t * p_vpar )
84 {
85     while( !p_vpar->b_die )
86     {
87         NextStartCode( p_vpar );
88         switch( GetBits32( &p_vpar->bit_stream ) )
89         {
90         case SEQUENCE_HEADER_CODE:
91             SequenceHeader( p_vpar );
92             return 0;
93             break;
94
95         case GROUP_START_CODE:
96             GroupHeader( p_vpar );
97             return 0;
98             break;
99
100         case PICTURE_START_CODE:
101             PictureHeader( p_vpar );
102             return 0;
103             break;
104
105         case SEQUENCE_END_CODE:
106             return 1;
107             break;
108
109         default:
110         }
111     }
112
113     return 0;
114 }
115
116 /*
117  * Following functions are local
118  */
119
120 /*****************************************************************************
121  * NextStartCode : Find the next start code
122  *****************************************************************************/
123 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
124 {
125     /* Re-align the buffer on an 8-bit boundary */
126     DumpBits( &p_vpar->bit_stream, p_vpar->bit_stream.fifo.i_available & 7 );
127
128     while( ShowBits( &p_vpar->bit_stream, 24 ) != 0x01L && !p_vpar->b_die )
129     {
130         DumpBits( &p_vpar->bit_stream, 8 );
131     }
132 }
133
134 /*****************************************************************************
135  * SequenceHeader : Parse the next sequence header
136  *****************************************************************************/
137 static void SequenceHeader( vpar_thread_t * p_vpar )
138 {
139 #define RESERVED    -1 
140     static double d_frame_rate_table[16] =
141     {
142         0.0,
143         ((23.0*1000.0)/1001.0),
144         24.0,
145         25.0,
146         ((30.0*1000.0)/1001.0),
147         30.0,
148         50.0,
149         ((60.0*1000.0)/1001.0),
150         60.0,
151         RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
152     };
153 #undef RESERVED
154
155     int i_height_save, i_width_save;
156     
157     i_height_save = p_vpar->sequence.i_height;
158     i_width_save = p_vpar->sequence.i_width;
159
160     p_vpar->sequence.i_height = GetBits( p_vpar->bit_stream, 12 );
161     p_vpar->sequence.i_width = GetBits( p_vpar->bit_stream, 12 );
162     p_vpar->sequence.i_ratio = GetBits( p_vpar->bit_stream, 4 );
163     p_vpar->sequence.d_frame_rate =
164             d_frame_rate_table( GetBits( p_vpar->bit_stream, 4 ) );
165
166     /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
167      * constrained_parameters_flag */
168     DumpBits( p_vpar->bit_stream, 30 );
169     
170     /*
171      * Quantization matrices
172      */
173     if( GetBits( p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
174     {
175         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
176     }
177     else
178     {
179         /* Use default matrix. */
180         LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
181     }
182     
183     if( GetBits( p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
184     {
185         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
186     }
187     else
188     {
189         /* Use default matrix. */
190         LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
191     }
192     
193     /* Unless later overwritten by a matrix extension, we have the same
194      * matrices for luminance and chrominance. */
195     LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
196                 p_vpar->sequence.intra_quant.pi_matrix );
197     LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
198                 p_vpar->sequence.nonintra_quant.pi_matrix );
199
200     /*
201      * Sequence Extension
202      */
203     NextStartCode( p_vpar );
204     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
205     {
206         int             i_dummy;
207         static int      pi_chroma_nb_blocks[4] = {0, 1, 2, 4};
208         static (void *) ppf_chroma_pattern[4]( vpar_thread_t * ) =
209                             {NULL, vpar_CodedPattern420,
210                              vpar_CodedPattern422, vpar_CodedPattern444};
211     
212         /* Parse sequence_extension */
213         DumpBits32( &p_vpar->bit_stream );
214         /* extension_start_code_identifier, profile_and_level_indication */
215         DumpBits( &p_vpar->bit_stream, 12 );
216         p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
217         p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
218         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width
219                     >> (3-p_vpar->sequence.i_chroma_format);
220         p_vpar->sequence.i_chroma_nb_blocks = pi_chroma_nb_blocks
221                                     [p_vpar->sequence.i_chroma_format];
222         p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
223                                     [p_vpar->sequence.i_chroma_format];
224         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
225         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
226         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
227         DumpBits( &p_vpar->bit_stream, 22 );
228         /* frame_rate_extension_n */
229         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
230         /* frame_rate_extension_d */
231         p_vpar->sequence.d_frame_rate *= (i_dummy + 1)
232                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
233
234         p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
235     }
236     else
237     {
238         /* It's an MPEG-1 stream. Put adequate parameters. */
239         p_vpar->sequence.b_progressive = 1;
240         p_vpar->sequence.i_chroma_format = CHROMA_420;
241         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i->width >> 2;
242         p_vpar->sequence.i_chroma_nb_blocks = 2;
243         p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
244
245         p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
246     }
247
248     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
249     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
250                                    (p_vpar->sequence.i_height + 15) / 16 :
251                                    2 * (p_vpar->sequence.i_height + 31) / 32;
252     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
253                                         * p_vpar->sequence.i_mb_height;
254     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
255     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
256     p_vpar->sequence.i_size = p_vpar->sequence.i_width
257                                         * p_vpar->sequence.i_height;
258
259     /* Slice Header functions */
260     if( p_vpar->sequence.i_height <= 2800 )
261     {
262         if( p_vpar->sequence.i_scalable_mode != SC_DP )
263         {
264             p_vpar->sequence.pf_slice_header = SliceHeader00;
265         }
266         else
267         {
268             p_vpar->sequence.pf_slice_header = SliceHeader01;
269         }
270     }
271     else
272     {
273         if( p_vpar->sequence.i_scalable_mode != SC_DP )
274         {
275             p_vpar->sequence.pf_slice_header = SliceHeader10;
276         }
277         else
278         {
279             p_vpar->sequence.pf_slice_header = SliceHeader11;
280         }
281     }
282
283     if(    p_vpar->sequence.i_width != i_width_save
284         || p_vpar->sequence.i_height != i_height_save
285         || p_vpar->sequence.p_frame_lum_lookup == NULL )
286     {
287
288     }
289
290     /* Extension and User data */
291     ExtensionAndUserData( p_vpar );
292 }
293
294 /*****************************************************************************
295  * GroupHeader : Parse the next group of pictures header
296  *****************************************************************************/
297 static void GroupHeader( vpar_thread_t * p_vpar )
298 {
299     /* Nothing to do, we don't care. */
300     DumpBits( &p_vpar->bit_stream, 27 );
301     ExtensionAndUserData( p_vpar );
302 }
303
304 /*****************************************************************************
305  * PictureHeader : Parse the next picture header
306  *****************************************************************************/
307 static void PictureHeader( vpar_thread_t * p_vpar )
308 {
309     static (int *)      ppf_macroblock_type[4] = {vpar_IMBType, vpar_PMBType,
310                                                   vpar_BMBType, vpar_DMBType};
311
312     int                 i_structure;
313     int                 i_mb_address, i_mb_base, i_mb;
314     elem_t *            p_y, p_u, p_v;
315     boolean_t           b_parsable;
316     u32                 i_dummy;
317     
318     DumpBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
319     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
320     p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
321                                          [p_vpar->picture.i_coding_type];
322     
323     DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
324     
325     p_vpar->picture.b_full_pel_forward_vector = GetBits( &p_vpar->bit_stream, 1 );
326     p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
327     p_vpar->picture.b_full_pel_backward_vector = GetBits( &p_vpar->bit_stream, 1 );
328     p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
329
330     /* extra_information_picture */
331     while( GetBits( &p_vpar->bit_stream, 1 ) )
332     {
333         DumpBits( &p_vpar->bit_stream, 8 );
334     }
335
336     /* 
337      * Picture Coding Extension
338      */
339     NextStartCode( p_vpar );
340     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
341     {
342         /* Parse picture_coding_extension */
343         DumpBits32( &p_vpar->bit_stream );
344         /* extension_start_code_identifier */
345         DumpBits( &p_vpar->bit_stream, 4 );
346         
347         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
348         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
349         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
350         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
351         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
352         i_structure = GetBits( &p_vpar->bit_stream, 2 );
353         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
354         p_vpar->picture.b_frame_pred_frame_dct
355              = GetBits( &p_vpar->bit_stream, 1 );
356         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
357         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
358         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
359         p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
360         /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is cryptic and
361          * apparently the reference decoder doesn't use it, so trash it),
362          * chroma_420_type (obsolete) */
363         DumpBits( &p_vpar->bit_stream, 2 );
364         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
365         
366         /* composite_display_flag */
367         if( GetBits( &p_vpar->bit_stream, 1 ) )
368         {
369             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
370              * sub_carrier_phase */
371             DumpBits( &p_vpar->bit_stream, 20 );
372         }
373     }
374     else
375     {
376         /* MPEG-1 compatibility flags */
377         p_vpar->i_intra_dc_precision = 0; /* 8 bits */
378         i_structure = FRAME_STRUCTURE;
379         p_vpar->b_frame_pred_frame_dct = TRUE;
380         p_vpar->picture.b_concealment_mv = 0;
381         p_vpar->picture.b_q_scale_type = 0;
382         p_vpar->picture.b_intra_vlc_format = 0;
383         p_vpar->picture.b_alternate_scan = 0; /* zigzag */
384         b_repeat_first_field = FALSE;
385         p_vpar->picture.b_progressive_frame = TRUE;
386     }
387
388     if( p_vpar->picture.i_current_structure &&
389         (i_structure == FRAME_STRUCTURE ||
390          i_structure == p_vpar->picture.i_current_structure) )
391     {
392         /* We don't have the second field of the buffered frame. */
393         if( p_pvar->picture.p_picture != NULL )
394         {
395             ReferenceReplace( p_vpar,
396                       p_vpar->picture.p_second_field_buffer->i_coding_type,
397                       NULL );
398
399             for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
400             {
401                 vpar_DestroyMacroblock( &p_vpar->vfifo,
402                                         p_vpar->picture.pp_mb[i_mb] );
403             }
404             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
405         }
406         
407         p_pvar->picture.i_current_structure = 0;
408
409         intf_DbgMsg("vpar debug: odd number of field picture.");
410     }
411
412     if( p_vpar->picture.i_current_structure )
413     {
414         /* Second field of a frame. We will decode it if, and only if we
415          * have decoded the first frame. */
416         b_parsable = (p_vpar->picture.p_second_field_buffer != NULL);
417     }
418     else
419     {
420         /* Do we have the reference pictures ? */
421         b_parsable = !((i_coding_type == P_CODING_TYPE) &&
422                        (p_vpar->sequence.p_forward == NULL)) ||
423                       ((i_coding_type == B_CODING_TYPE) &&
424                        (p_vpar->sequence.p_forward == NULL ||
425                         p_vpar->sequence.p_backward == NULL));
426
427         /* Does synchro say we have enough time to decode it ? */
428         b_parsable &&= vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
429     }
430
431     if( !b_parsable )
432     {
433         /* Update the reference pointers. */
434         ReferenceUpdate( p_vpar, i_coding_type, NULL );
435         
436         /* Warn Synchro we have trashed a picture. */
437         vpar_SynchroTrash( p_vpar, i_coding_type, i_structure );
438
439         /* Update context. */
440         if( i_structure != FRAME_STRUCTURE )
441             p_vpar->picture.i_current_structure = i_structure;
442         p_vpar->picture.p_picture = NULL;
443
444         return;
445     }
446
447     /* OK, now we are sure we will decode the picture. */
448 #define P_picture p_vpar->picture.p_picture
449     p_vpar->picture.b_error = FALSE;
450
451     if( !p_vpar->picture.i_current_structure )
452     {
453         /* This is a new frame. Get a structure from the video_output. */
454         P_picture = vout_CreatePicture( p_vpar->p_vout,
455                                         SPLITTED_YUV_PICTURE,
456                                         p_vpar->sequence.i_width,
457                                         p_vpar->sequence.i_height,
458                                         p_vpar->sequence.i_chroma_format );
459
460         /* Initialize values. */
461         P_picture->date = vpar_SynchroDecode( p_vpar, i_coding_type,
462                                               i_structure );
463         bzero( p_vpar->picture.pp_mb, MAX_MB*sizeof( macroblock_t * ) );
464         p_vpar->picture.i_lum_incr = - 8 + ( p_vpar->sequence.i_width
465                     << ( i_structure != FRAME_STRUCTURE ) );
466         p_vpar->picture.i_chroma_incr = -8 + ( p_vpar->sequence.i_width
467                     << (( i_structure != FRAME_STRUCTURE ) +
468                         ( 3 - p_vpar->sequence.i_chroma_format )) );
469
470         /* Update the reference pointers. */
471         ReferenceUpdate( p_vpar, i_coding_type, p_undec_p );
472     }
473     p_vpar->picture.i_current_structure |= i_structure;
474     p_vpar->picture.i_structure = i_structure;
475     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
476
477     /* Initialize picture data for decoding. */
478     if( i_structure == BOTTOM_FIELD )
479     {
480         i_mb_base = p_vpar->sequence.i_mb_size >> 1;
481     }
482     else
483     {
484         i_mb_base = 0;
485     }
486     i_mb_address = 0;
487
488     /* Extension and User data. */
489     ExtensionAndUserData( p_vpar );
490
491     /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
492     NextStartCode( p_vpar );
493     while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
494            && !p_vpar->picture.b_error)
495     {
496         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
497                  < SLICE_START_CODE_MIN) ||
498             (i_dummy > SLICE_START_CODE_MAX) )
499         {
500             intf_DbgMsg("vpar debug: premature end of picture");
501             p_vpar->picture.b_error = TRUE;
502             break;
503         }
504         DumpBits32( &p_vpar->bit_stream );
505         
506         /* Decode slice data. */
507         SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
508     }
509
510     if( p_vpar->picture.b_error )
511     {
512         /* Trash picture. */
513         for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
514         {
515             vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
516         }
517
518         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
519         vout_DestroyPicture( p_vpar->p_vout, P_picture );
520
521         /* Prepare context for the next picture. */
522         P_picture = NULL:
523     }
524     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
525     {
526         /* Frame completely parsed. */
527         for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
528         {
529             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
530         }
531
532         /* Prepare context for the next picture. */
533         P_picture = NULL;
534     }
535 #undef P_picture
536 }
537
538 /*****************************************************************************
539  * ReferenceUpdate : Update the reference pointers when we have a new picture
540  *****************************************************************************/
541 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
542                                         int i_coding_type,
543                                         picture_t * p_newref )
544 {
545     if( i_coding_type != B_CODING_TYPE )
546     {
547         if( p_vpar->sequence.p_forward != NULL )
548             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
549         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
550         p_vpar->sequence.p_backward = p_newref;
551         if( p_newref != NULL )
552             vout_LinkPicture( p_vpar->p_vout, p_newref );
553     }
554 }
555
556 /*****************************************************************************
557  * ReferenceReplace : Replace the last reference pointer when we destroy
558  * a picture
559  *****************************************************************************/
560 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
561                                          int i_coding_type,
562                                          picture_t * p_newref )
563 {
564     if( i_coding_type != B_CODING_TYPE )
565     {
566         if( p_vpar->sequence.p_backward != NULL )
567             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
568         p_vpar->sequence.p_backward = p_newref;
569         if( p_newref != NULL )
570             vout_LinkPicture( p_vpar->p_vout, p_newref );
571     }
572 }
573
574 /*****************************************************************************
575  * SliceHeaderXY : Parse the next slice structure
576  *****************************************************************************
577  * X = i_height > 2800 ?
578  * Y = scalable_mode == SC_DP ?
579  *****************************************************************************/
580 static void SliceHeader00( vpar_thread_t * p_vpar,
581                            int * pi_mb_address, int i_mb_base,
582                            u32 i_vert_code )
583 {
584     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
585 }
586
587 static void SliceHeader01( vpar_thread_t * p_vpar,
588                            int * pi_mb_address, int i_mb_base,
589                            u32 i_vert_code )
590 {
591     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
592     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
593 }
594
595 static void SliceHeader10( vpar_thread_t * p_vpar,
596                            int * pi_mb_address, int i_mb_base,
597                            u32 i_vert_code )
598 {
599     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
600     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
601 }
602
603 static void SliceHeader11( vpar_thread_t * p_vpar,
604                            int * pi_mb_address, int i_mb_base,
605                            u32 i_vert_code )
606 {
607     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
608     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
609     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
610 }
611
612 /*****************************************************************************
613  * SliceHeader : Parse the next slice structure
614  *****************************************************************************/
615 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
616                                     int * pi_mb_address, int i_mb_base,
617                                     u32 i_vert_code )
618 {
619     /* DC predictors initialization table */
620     static int              pi_dc_dct_reinit[4] = {128,256,512,1024};
621
622     int                     i_mb_address_save = *pi_mb_address;
623
624     /* slice_vertical_position_extension and priority_breakpoint already done */
625     LoadQuantizerScale( p_vpar );
626
627     if( GetBits( &p_vpar->bit_stream, 1 ) )
628     {
629         /* intra_slice, slice_id */
630         DumpBits( &p_vpar->bit_stream, 8 );
631         /* extra_information_slice */
632         while( GetBits( &p_vpar->bit_stream, 1 ) )
633         {
634             DumpBits( &p_vpar->bit_stream, 8 );
635         }
636     }
637
638     *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
639
640     /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
641      * does the reference decoder put 0 instead of the normative values ? */
642     p_vpar->slice.pi_dct_pred[0] = p_vpar->slice.pi_dct_pred[1]
643         = p_vpar->slice.pi_dct_pred[2]
644         = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
645
646     /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
647     bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
648
649     do
650     {
651         vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
652                               i_mb_base );
653         i_mb_address_save = *pi_mb_address;
654     }
655     while( !ShowBits( &p_vpar->bit_stream, 23 ) );
656 }
657
658 /*****************************************************************************
659  * ExtensionAndUserData : Parse the extension_and_user_data structure
660  *****************************************************************************/
661 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
662 {
663     while( !p_vpar->b_die )
664     {
665         NextStartCode( p_vpar );
666         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
667         {
668         case EXTENSION_START_CODE:
669             DumpBits32( &p_vpar->bit_stream );
670             switch( GetBits( &p_vpar->bit_stream, 4 ) )
671             {
672             case SEQUENCE_DISPLAY_EXTENSION_ID:
673                 SequenceDisplayExtension( p_vpar );
674                 break;
675             case QUANT_MATRIX_EXTENSION_ID:
676                 QuantMatrixExtension( p_vpar );
677                 break;
678             case SEQUENCE_SCALABLE_EXTENSION_ID:
679                 SequenceScalableExtension( p_vpar );
680                 break;
681             case PICTURE_DISPLAY_EXTENSION_ID:
682                 PictureDisplayExtension( p_vpar );
683                 break;
684             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
685                 PictureSpatialScalableExtension( p_vpar );
686                 break;
687             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
688                 PictureTemporalScalableExtension( p_vpar );
689                 break;
690             case COPYRIGHT_EXTENSION_ID:
691                 CopyrightExtension( p_vpar );
692                 break;
693             default:
694             }
695             break;
696
697         case USER_DATA_START_CODE:
698             DumpBits32( &p_vpar->bit_stream );
699             /* Wait for the next start code */
700             break;
701
702         default:
703             return;
704         }
705     }
706 }
707
708 /*****************************************************************************
709  * SequenceDisplayExtension : Parse the sequence_display_extension structure
710  *****************************************************************************/
711 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
712 {
713
714 }
715
716 /*****************************************************************************
717  * LoadMatrix : Load a quantization matrix
718  *****************************************************************************/
719 static void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
720 {
721     int i_dummy;
722     
723     if( !p_matrix->b_allocated )
724     {
725         /* Allocate a piece of memory to load the matrix. */
726         p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
727         p_matrix->b_allocated = TRUE;
728     }
729     
730     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
731     {
732         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
733              = GetBits( p_vpar->bit_stream, 8 );
734     }
735
736 #ifdef FOURIER_IDCT
737     /* Discrete Fourier Transform requires the quantization matrices to
738      * be normalized before using them. */
739     vdec_NormQuantMatrix( p_matrix->pi_matrix );
740 #endif
741 }
742
743 /*****************************************************************************
744  * LinkMatrix : Link a quantization matrix to another
745  *****************************************************************************/
746 static void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
747 {
748     int i_dummy;
749     
750     if( p_matrix->b_allocated )
751     {
752         /* Deallocate the piece of memory. */
753         free( p_matrix->pi_matrix );
754         p_matrix->b_allocated = FALSE;
755     }
756     
757     p_matrix->pi_matrix = pi_array;
758 }