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