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