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