]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
J'en ai chi� mais j'ai enfin pondu le AddBlock du video_decoder.
[vlc] / src / video_parser / vpar_headers.c
1 /*****************************************************************************
2  * vpar_headers.c : headers parsing
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*****************************************************************************
7  * Preamble
8  *****************************************************************************/
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <sys/uio.h>
15 #include <X11/Xlib.h>
16 #include <X11/extensions/XShm.h>
17
18 #include "config.h"
19 #include "common.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22
23 #include "intf_msg.h"
24 #include "debug.h"                    /* ?? temporaire, requis par netlist.h */
25
26 #include "input.h"
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
29 #include "video.h"
30 #include "video_output.h"
31
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 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.pf_decode_pattern = ppf_chroma_pattern
233                                     [p_vpar->sequence.i_chroma_format];
234         p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
235         p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
236         /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
237         DumpBits( &p_vpar->bit_stream, 22 );
238         /* frame_rate_extension_n */
239         i_dummy = GetBits( &p_vpar->bit_stream, 2 );
240         /* frame_rate_extension_d */
241         p_vpar->sequence.d_frame_rate *= (i_dummy + 1)
242                                   / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
243
244         p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
245     }
246     else
247     {
248         /* It's an MPEG-1 stream. Put adequate parameters. */
249         p_vpar->sequence.b_progressive = 1;
250         p_vpar->sequence.i_chroma_format = CHROMA_420;
251         p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
252
253         p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
254     }
255
256     /* Update sizes */
257     p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
258     p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
259                                    (p_vpar->sequence.i_height + 15) / 16 :
260                                    2 * (p_vpar->sequence.i_height + 31) / 32;
261     p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
262                                         * p_vpar->sequence.i_mb_height;
263     p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
264     p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
265     p_vpar->sequence.i_size = p_vpar->sequence.i_width
266                                         * p_vpar->sequence.i_height;
267
268     /* Update chromatic information */
269     switch( p_vpar->sequence.i_chroma_format )
270     {
271     case CHROMA_420:
272         p_vpar->sequence.i_chroma_nb_blocks = 2;
273         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 2;
274         p_vpar->i_chroma_mb_width = 8;
275         p_vpar->i_chroma_mb_height = 8;
276         break;
277
278     case CHROMA_422:
279         p_vpar->sequence.i_chroma_nb_blocks = 4;
280         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
281         p_vpar->i_chroma_mb_width = 8;
282         p_vpar->i_chroma_mb_height = 16;
283         break;
284
285     case CHROMA_444:
286         p_vpar->sequence.i_chroma_nb_blocks = 8;
287         p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
288         p_vpar->i_chroma_mb_width = 16;
289         p_vpar->i_chroma_mb_height = 16;
290     }
291
292     /* Slice Header functions */
293     if( p_vpar->sequence.i_height <= 2800 )
294     {
295         if( p_vpar->sequence.i_scalable_mode != SC_DP )
296         {
297             p_vpar->sequence.pf_slice_header = SliceHeader00;
298         }
299         else
300         {
301             p_vpar->sequence.pf_slice_header = SliceHeader01;
302         }
303     }
304     else
305     {
306         if( p_vpar->sequence.i_scalable_mode != SC_DP )
307         {
308             p_vpar->sequence.pf_slice_header = SliceHeader10;
309         }
310         else
311         {
312             p_vpar->sequence.pf_slice_header = SliceHeader11;
313         }
314     }
315
316     if(    p_vpar->sequence.i_width != i_width_save
317         || p_vpar->sequence.i_height != i_height_save )
318     {
319          /* What do we do in case of a size change ??? */
320     }
321
322     /* Extension and User data */
323     ExtensionAndUserData( p_vpar );
324 }
325
326 /*****************************************************************************
327  * GroupHeader : Parse the next group of pictures header
328  *****************************************************************************/
329 static void GroupHeader( vpar_thread_t * p_vpar )
330 {
331     /* Nothing to do, we don't care. */
332     DumpBits( &p_vpar->bit_stream, 27 );
333     ExtensionAndUserData( p_vpar );
334 }
335
336 /*****************************************************************************
337  * PictureHeader : Parse the next picture header
338  *****************************************************************************/
339 static void PictureHeader( vpar_thread_t * p_vpar )
340 {
341     static f_macroblock_type_t ppf_macroblock_type[4] =
342                                                  {vpar_IMBType, vpar_PMBType,
343                                                   vpar_BMBType, vpar_DMBType};
344
345     int                 i_structure;
346     int                 i_mb_address, i_mb_base, i_mb;
347     elem_t *            p_y, p_u, p_v;
348     boolean_t           b_parsable;
349     u32                 i_dummy;
350     
351     DumpBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
352     p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
353     p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
354                                          [p_vpar->picture.i_coding_type];
355     
356     DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
357     
358     p_vpar->picture.b_full_pel_forward_vector = GetBits( &p_vpar->bit_stream, 1 );
359     p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
360     p_vpar->picture.b_full_pel_backward_vector = GetBits( &p_vpar->bit_stream, 1 );
361     p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
362
363     /* extra_information_picture */
364     while( GetBits( &p_vpar->bit_stream, 1 ) )
365     {
366         DumpBits( &p_vpar->bit_stream, 8 );
367     }
368
369     /* 
370      * Picture Coding Extension
371      */
372     NextStartCode( p_vpar );
373     if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
374     {
375         /* Parse picture_coding_extension */
376         DumpBits32( &p_vpar->bit_stream );
377         /* extension_start_code_identifier */
378         DumpBits( &p_vpar->bit_stream, 4 );
379         
380         p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
381         p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
382         p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
383         p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
384         p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
385         i_structure = GetBits( &p_vpar->bit_stream, 2 );
386         p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
387         p_vpar->picture.b_frame_pred_frame_dct
388              = GetBits( &p_vpar->bit_stream, 1 );
389         p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
390         p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
391         p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
392         p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
393         p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
394         /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
395          * the length of the picture_display_extension structure.
396          * chroma_420_type (obsolete) */
397         DumpBits( &p_vpar->bit_stream, 1 );
398         p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
399         
400         /* composite_display_flag */
401         if( GetBits( &p_vpar->bit_stream, 1 ) )
402         {
403             /* v_axis, field_sequence, sub_carrier, burst_amplitude,
404              * sub_carrier_phase */
405             DumpBits( &p_vpar->bit_stream, 20 );
406         }
407     }
408     else
409     {
410         /* MPEG-1 compatibility flags */
411         p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
412         i_structure = FRAME_STRUCTURE;
413         p_vpar->picture.b_frame_pred_frame_dct = 1;
414         p_vpar->picture.b_concealment_mv = 0;
415         p_vpar->picture.b_q_scale_type = 0;
416         p_vpar->picture.b_intra_vlc_format = 0;
417         p_vpar->picture.b_alternate_scan = 0; /* zigzag */
418         p_vpar->picture.b_repeat_first_field = 0;
419         p_vpar->picture.b_progressive_frame = 1;
420     }
421
422     if( p_vpar->picture.i_current_structure &&
423         (i_structure == FRAME_STRUCTURE ||
424          i_structure == p_vpar->picture.i_current_structure) )
425     {
426         /* We don't have the second field of the buffered frame. */
427         if( p_vpar->picture.p_picture != NULL )
428         {
429             ReferenceReplace( p_vpar,
430                       p_vpar->picture.i_coding_type,
431                       NULL );
432
433             for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
434             {
435                 vpar_DestroyMacroblock( &p_vpar->vfifo,
436                                         p_vpar->picture.pp_mb[i_mb] );
437             }
438             vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
439         }
440         
441         p_vpar->picture.i_current_structure = 0;
442
443         intf_DbgMsg("vpar debug: odd number of field picture.");
444     }
445
446     if( p_vpar->picture.i_current_structure )
447     {
448         /* Second field of a frame. We will decode it if, and only if we
449          * have decoded the first frame. */
450         b_parsable = (p_vpar->picture.p_picture != NULL);
451     }
452     else
453     {
454         /* Do we have the reference pictures ? */
455         b_parsable = !((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
456                        (p_vpar->sequence.p_forward == NULL)) ||
457                       ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
458                        (p_vpar->sequence.p_forward == NULL ||
459                         p_vpar->sequence.p_backward == NULL));
460
461         if( b_parsable )
462         {
463             /* Does synchro say we have enough time to decode it ? */
464             b_parsable = vpar_SynchroChoose( p_vpar,
465                                p_vpar->picture.i_coding_type, i_structure );
466         }
467     }
468
469     if( !b_parsable )
470     {
471         /* Update the reference pointers. */
472         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
473         
474         /* Warn Synchro we have trashed a picture. */
475         vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
476
477         /* Update context. */
478         if( i_structure != FRAME_STRUCTURE )
479             p_vpar->picture.i_current_structure = i_structure;
480         p_vpar->picture.p_picture = NULL;
481
482         return;
483     }
484
485     /* OK, now we are sure we will decode the picture. */
486 #define P_picture p_vpar->picture.p_picture
487     p_vpar->picture.b_error = 0;
488
489     if( !p_vpar->picture.i_current_structure )
490     {
491         /* This is a new frame. Get a structure from the video_output. */
492         P_picture = vout_CreatePicture( p_vpar->p_vout,
493                                         SPLITTED_YUV_PICTURE,
494                                         p_vpar->sequence.i_width,
495                                         p_vpar->sequence.i_height,
496                                         p_vpar->sequence.i_chroma_format );
497
498         /* Initialize values. */
499         P_picture->date = vpar_SynchroDecode( p_vpar,
500                                               p_vpar->picture.i_coding_type,
501                                               i_structure );
502         p_vpar->picture.i_l_stride = - 8 + ( p_vpar->sequence.i_width
503                     << ( 1 - p_vpar->picture.b_frame_structure ) );
504         p_vpar->picture.i_c_stride = -8 + ( p_vpar->sequence.i_width
505                     << (( 1 - p_vpar->picture.b_frame_structure ) +
506                         ( 3 - p_vpar->sequence.i_chroma_format )) );
507
508         /* Update the reference pointers. */
509         ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
510     }
511     p_vpar->picture.i_current_structure |= i_structure;
512     p_vpar->picture.i_structure = i_structure;
513     p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
514
515     /* Initialize picture data for decoding. */
516     if( i_structure == BOTTOM_FIELD )
517     {
518         i_mb_base = p_vpar->sequence.i_mb_size >> 1;
519         p_vpar->mb.i_l_y = 16;
520         p_vpar->mb.i_c_y = p_vpar->sequence.i_chroma_mb_height;
521     }
522     else
523     {
524         i_mb_base = 0;
525         p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
526     }
527     i_mb_address = 0;
528     p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
529
530     /* Extension and User data. */
531     ExtensionAndUserData( p_vpar );
532
533     /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
534     NextStartCode( p_vpar );
535     while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
536            && !p_vpar->picture.b_error)
537     {
538         if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
539                  < SLICE_START_CODE_MIN) ||
540             (i_dummy > SLICE_START_CODE_MAX) )
541         {
542             intf_DbgMsg("vpar debug: premature end of picture");
543             p_vpar->picture.b_error = 1;
544             break;
545         }
546         DumpBits32( &p_vpar->bit_stream );
547         
548         /* Decode slice data. */
549         SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
550     }
551
552     if( p_vpar->picture.b_error )
553     {
554         /* Trash picture. */
555         for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
556         {
557             vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
558         }
559
560         ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
561         vout_DestroyPicture( p_vpar->p_vout, P_picture );
562
563         /* Prepare context for the next picture. */
564         P_picture = NULL;
565     }
566     else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
567     {
568         /* Frame completely parsed. */
569         P_picture.i_deccount = p_vpar->sequence.i_mb_size;
570         for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
571         {
572             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
573         }
574
575         /* Prepare context for the next picture. */
576         P_picture = NULL;
577     }
578 #undef P_picture
579 }
580
581 /*****************************************************************************
582  * ReferenceUpdate : Update the reference pointers when we have a new picture
583  *****************************************************************************/
584 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
585                                         int i_coding_type,
586                                         picture_t * p_newref )
587 {
588     if( i_coding_type != B_CODING_TYPE )
589     {
590         if( p_vpar->sequence.p_forward != NULL )
591             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
592         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
593         p_vpar->sequence.p_backward = p_newref;
594         if( p_newref != NULL )
595             vout_LinkPicture( p_vpar->p_vout, p_newref );
596     }
597 }
598
599 /*****************************************************************************
600  * ReferenceReplace : Replace the last reference pointer when we destroy
601  * a picture
602  *****************************************************************************/
603 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
604                                          int i_coding_type,
605                                          picture_t * p_newref )
606 {
607     if( i_coding_type != B_CODING_TYPE )
608     {
609         if( p_vpar->sequence.p_backward != NULL )
610             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
611         p_vpar->sequence.p_backward = p_newref;
612         if( p_newref != NULL )
613             vout_LinkPicture( p_vpar->p_vout, p_newref );
614     }
615 }
616
617 /*****************************************************************************
618  * SliceHeaderXY : Parse the next slice structure
619  *****************************************************************************
620  * X = i_height > 2800 ?
621  * Y = scalable_mode == SC_DP ?
622  *****************************************************************************/
623 static void SliceHeader00( vpar_thread_t * p_vpar,
624                            int * pi_mb_address, int i_mb_base,
625                            u32 i_vert_code )
626 {
627     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
628 }
629
630 static void SliceHeader01( vpar_thread_t * p_vpar,
631                            int * pi_mb_address, int i_mb_base,
632                            u32 i_vert_code )
633 {
634     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
635     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
636 }
637
638 static void SliceHeader10( vpar_thread_t * p_vpar,
639                            int * pi_mb_address, int i_mb_base,
640                            u32 i_vert_code )
641 {
642     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
643     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
644 }
645
646 static void SliceHeader11( vpar_thread_t * p_vpar,
647                            int * pi_mb_address, int i_mb_base,
648                            u32 i_vert_code )
649 {
650     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
651     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
652     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
653 }
654
655 /*****************************************************************************
656  * SliceHeader : Parse the next slice structure
657  *****************************************************************************/
658 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
659                                     int * pi_mb_address, int i_mb_base,
660                                     u32 i_vert_code )
661 {
662     /* DC predictors initialization table */
663     static int              pi_dc_dct_reinit[4] = {128,256,512,1024};
664
665     int                     i_mb_address_save = *pi_mb_address;
666
667     /* slice_vertical_position_extension and priority_breakpoint already done */
668     LoadQuantizerScale( p_vpar );
669
670     if( GetBits( &p_vpar->bit_stream, 1 ) )
671     {
672         /* intra_slice, slice_id */
673         DumpBits( &p_vpar->bit_stream, 8 );
674         /* extra_information_slice */
675         while( GetBits( &p_vpar->bit_stream, 1 ) )
676         {
677             DumpBits( &p_vpar->bit_stream, 8 );
678         }
679     }
680
681     *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
682
683     /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
684      * does the reference decoder put 0 instead of the normative values ? */
685     p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
686         = p_vpar->slice.pi_dc_dct_pred[2]
687         = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
688
689     /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
690     bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
691
692     do
693     {
694         vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
695                               i_mb_base );
696         i_mb_address_save = *pi_mb_address;
697     }
698     while( !ShowBits( &p_vpar->bit_stream, 23 ) );
699 }
700
701 /*****************************************************************************
702  * ExtensionAndUserData : Parse the extension_and_user_data structure
703  *****************************************************************************/
704 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
705 {
706     while( !p_vpar->b_die )
707     {
708         NextStartCode( p_vpar );
709         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
710         {
711         case EXTENSION_START_CODE:
712             DumpBits32( &p_vpar->bit_stream );
713             switch( GetBits( &p_vpar->bit_stream, 4 ) )
714             {
715             case SEQUENCE_DISPLAY_EXTENSION_ID:
716                 SequenceDisplayExtension( p_vpar );
717                 break;
718             case QUANT_MATRIX_EXTENSION_ID:
719                 QuantMatrixExtension( p_vpar );
720                 break;
721             case SEQUENCE_SCALABLE_EXTENSION_ID:
722                 SequenceScalableExtension( p_vpar );
723                 break;
724             case PICTURE_DISPLAY_EXTENSION_ID:
725                 PictureDisplayExtension( p_vpar );
726                 break;
727             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
728                 PictureSpatialScalableExtension( p_vpar );
729                 break;
730             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
731                 PictureTemporalScalableExtension( p_vpar );
732                 break;
733             case COPYRIGHT_EXTENSION_ID:
734                 CopyrightExtension( p_vpar );
735                 break;
736             default:
737             }
738             break;
739
740         case USER_DATA_START_CODE:
741             DumpBits32( &p_vpar->bit_stream );
742             /* Wait for the next start code */
743             break;
744
745         default:
746             return;
747         }
748     }
749 }
750
751
752 /*****************************************************************************
753  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
754  *****************************************************************************/
755
756 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
757 {
758     /* We don't care sequence_display_extension. */
759     /* video_format */
760     DumpBits( &p_vpar->bit_stream, 3 );
761     if( GetBits( &p_vpar->bit_stream, 1 ) )
762     {
763         /* Three bytes for color_desciption */
764         DumpBits( &p_vpar->bit_stream, 24 );
765     }
766     /* display_horizontal and vertical_size and a marker_bit */
767     DumpBits( &p_vpar->bit_stream, 29 );
768 }
769
770
771 /*****************************************************************************
772  * QuantMatrixExtension : Load quantization matrices for luminance           *
773  *                        and chrominance                                    *
774  *****************************************************************************/
775
776 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
777 {
778     if( GetBits( &p_vpar->bit_stream, 1 ) )
779     {
780         /* Load intra_quantiser_matrix for luminance. */
781         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
782     }
783     else
784     {
785         /* Use the default matrix. */
786         LinkMatrix( &p_vpar->sequence.intra_quant,
787                     pi_default_intra_quant );
788     }
789     if( GetBits( &p_vpar->bit_stream, 1 ) )
790     {
791         /* Load non_intra_quantiser_matrix for luminance. */
792         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
793     }
794     else
795     {
796         /* Use the default matrix. */
797         LinkMatrix( &p_vpar->sequence.nonintra_quant,
798                     pi_default_nonintra_quant );
799     }
800     if( GetBits( &p_vpar->bit_stream, 1 ) )
801     {
802         /* Load intra_quantiser_matrix for chrominance. */
803         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
804     }
805     else
806     {
807         /* Link the chrominance intra matrix to the luminance one. */
808         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
809                     p_vpar->sequence.intra_quant.pi_matrix );
810     }
811     if( GetBits( &p_vpar->bit_stream, 1 ) )
812     {
813         /* Load non_intra_quantiser_matrix for chrominance. */
814         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
815     }
816     else
817     {
818         /* Link the chrominance intra matrix to the luminance one. */
819         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
820                     p_vpar->sequence.intra_quant.pi_matrix );
821     }
822     if( GetBits( &p_vpar->bit_stream, 1 ) )
823     {
824         /* Load non_intra_quantiser_matrix for chrominance. */
825         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
826     }
827     else
828     {
829         /* Link the chrominance nonintra matrix to the luminance one. */
830         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
831                     p_vpar->sequence.nonintra_quant.pi_matrix );
832     }
833 }
834
835
836 /*****************************************************************************
837  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
838  *                             structure to handle scalable coding           *
839  *****************************************************************************/
840
841 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
842 {
843     /* We don't care about anything scalable except the scalable mode. */
844     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
845     /* The length of the structure depends on the value of the scalable_mode */
846     {
847         case 1:
848             DumpBits32( &p_vpar->bit_stream );
849             DumpBits( &p_vpar->bit_stream, 21 );
850             break;
851         case 2:
852             DumpBits( &p_vpar->bit_stream, 12 );
853             break;
854         default:
855             DumpBits( &p_vpar->bit_stream, 4 );
856     }
857
858 }
859 /*****************************************************************************
860  * PictureDisplayExtension : Parse the picture_display_extension structure   *
861  *****************************************************************************/
862
863 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
864 {
865     /* Number of frame center offset */
866     int nb;
867     /* I am not sure it works but it should
868         (fewer tests than shown in §6.3.12) */
869     nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
870                                           p_vpar->picture.b_repeat_first_field +
871                                           p_vpar->picture.b_top_field_first
872                          : ( p_vpar->picture.b_frame_structure + 1 ) +
873                            p_vpar->picture.b_repeat_first_field;
874     DumpBits( &p_vpar->bit_stream, 34 * nb );
875 }
876
877
878 /*****************************************************************************
879  * PictureSpatialScalableExtension                                           *
880  *****************************************************************************/
881
882 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
883 {
884     /* That's scalable, so we trash it */
885     DumpBits32( &p_vpar->bit_stream );
886     DumpBits( &p_vpar->bit_stream, 14 );
887 }
888
889
890 /*****************************************************************************
891  * PictureTemporalScalableExtension                                          *
892  *****************************************************************************/
893
894 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
895 {
896     /* Scalable again, trashed again */
897     DumpBits( &p_vpar->bit_stream, 23 );
898 }
899
900
901 /*****************************************************************************
902  * CopyrightExtension : Keeps some legal informations                        *
903  *****************************************************************************/
904
905 static void CopyrightExtension( vpar_thread_t * p_vpar )
906 {
907     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
908     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
909         /* A flag that says whether the copyright information is significant */
910     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
911         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
912     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
913         /* Reserved bits */
914     DumpBits( &p_vpar->bit_stream, 8 );
915         /* The copyright_number is split in three parts */
916         /* first part */
917     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
918     DumpBits( &p_vpar->bit_stream, 1 );
919         /* second part */
920     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
921     DumpBits( &p_vpar->bit_stream, 1 );
922         /* third part and sum */
923     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
924                                       ( (u64)i_copyright_nb_2 << 22 ) +
925                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
926 }
927
928
929 /*****************************************************************************
930  * LoadMatrix : Load a quantization matrix
931  *****************************************************************************/
932 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
933 {
934     int i_dummy;
935     
936     if( !p_matrix->b_allocated )
937     {
938         /* Allocate a piece of memory to load the matrix. */
939         p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
940         p_matrix->b_allocated = 1;
941     }
942     
943     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
944     {
945         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
946              = GetBits( &p_vpar->bit_stream, 8 );
947     }
948
949 #ifdef VDEC_DFT
950     /* Discrete Fourier Transform requires the quantization matrices to
951      * be normalized before using them. */
952     vdec_NormQuantMatrix( p_matrix->pi_matrix );
953 #endif
954 }
955
956 /*****************************************************************************
957  * LinkMatrix : Link a quantization matrix to another
958  *****************************************************************************/
959 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
960 {
961     int i_dummy;
962     
963     if( p_matrix->b_allocated )
964     {
965         /* Deallocate the piece of memory. */
966         free( p_matrix->pi_matrix );
967         p_matrix->b_allocated = 0;
968     }
969     
970     p_matrix->pi_matrix = pi_array;
971 }