]> git.sesse.net Git - vlc/blob - src/video_parser/vpar_headers.c
video_fifo.c et vpar_headers.c compilent � peu pr�s. Correction d'environ
[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         for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
547         {
548             vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
549         }
550
551         /* Prepare context for the next picture. */
552         P_picture = NULL;
553     }
554 #undef P_picture
555 }
556
557 /*****************************************************************************
558  * ReferenceUpdate : Update the reference pointers when we have a new picture
559  *****************************************************************************/
560 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
561                                         int i_coding_type,
562                                         picture_t * p_newref )
563 {
564     if( i_coding_type != B_CODING_TYPE )
565     {
566         if( p_vpar->sequence.p_forward != NULL )
567             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
568         p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
569         p_vpar->sequence.p_backward = p_newref;
570         if( p_newref != NULL )
571             vout_LinkPicture( p_vpar->p_vout, p_newref );
572     }
573 }
574
575 /*****************************************************************************
576  * ReferenceReplace : Replace the last reference pointer when we destroy
577  * a picture
578  *****************************************************************************/
579 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
580                                          int i_coding_type,
581                                          picture_t * p_newref )
582 {
583     if( i_coding_type != B_CODING_TYPE )
584     {
585         if( p_vpar->sequence.p_backward != NULL )
586             vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
587         p_vpar->sequence.p_backward = p_newref;
588         if( p_newref != NULL )
589             vout_LinkPicture( p_vpar->p_vout, p_newref );
590     }
591 }
592
593 /*****************************************************************************
594  * SliceHeaderXY : Parse the next slice structure
595  *****************************************************************************
596  * X = i_height > 2800 ?
597  * Y = scalable_mode == SC_DP ?
598  *****************************************************************************/
599 static void SliceHeader00( vpar_thread_t * p_vpar,
600                            int * pi_mb_address, int i_mb_base,
601                            u32 i_vert_code )
602 {
603     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
604 }
605
606 static void SliceHeader01( vpar_thread_t * p_vpar,
607                            int * pi_mb_address, int i_mb_base,
608                            u32 i_vert_code )
609 {
610     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
611     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
612 }
613
614 static void SliceHeader10( vpar_thread_t * p_vpar,
615                            int * pi_mb_address, int i_mb_base,
616                            u32 i_vert_code )
617 {
618     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
619     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
620 }
621
622 static void SliceHeader11( vpar_thread_t * p_vpar,
623                            int * pi_mb_address, int i_mb_base,
624                            u32 i_vert_code )
625 {
626     i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
627     DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
628     SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
629 }
630
631 /*****************************************************************************
632  * SliceHeader : Parse the next slice structure
633  *****************************************************************************/
634 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
635                                     int * pi_mb_address, int i_mb_base,
636                                     u32 i_vert_code )
637 {
638     /* DC predictors initialization table */
639     static int              pi_dc_dct_reinit[4] = {128,256,512,1024};
640
641     int                     i_mb_address_save = *pi_mb_address;
642
643     /* slice_vertical_position_extension and priority_breakpoint already done */
644     LoadQuantizerScale( p_vpar );
645
646     if( GetBits( &p_vpar->bit_stream, 1 ) )
647     {
648         /* intra_slice, slice_id */
649         DumpBits( &p_vpar->bit_stream, 8 );
650         /* extra_information_slice */
651         while( GetBits( &p_vpar->bit_stream, 1 ) )
652         {
653             DumpBits( &p_vpar->bit_stream, 8 );
654         }
655     }
656
657     *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
658
659     /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
660      * does the reference decoder put 0 instead of the normative values ? */
661     p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
662         = p_vpar->slice.pi_dc_dct_pred[2]
663         = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
664
665     /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
666     bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
667
668     do
669     {
670         vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
671                               i_mb_base );
672         i_mb_address_save = *pi_mb_address;
673     }
674     while( !ShowBits( &p_vpar->bit_stream, 23 ) );
675 }
676
677 /*****************************************************************************
678  * ExtensionAndUserData : Parse the extension_and_user_data structure
679  *****************************************************************************/
680 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
681 {
682     while( !p_vpar->b_die )
683     {
684         NextStartCode( p_vpar );
685         switch( ShowBits( &p_vpar->bit_stream, 32 ) )
686         {
687         case EXTENSION_START_CODE:
688             DumpBits32( &p_vpar->bit_stream );
689             switch( GetBits( &p_vpar->bit_stream, 4 ) )
690             {
691             case SEQUENCE_DISPLAY_EXTENSION_ID:
692                 SequenceDisplayExtension( p_vpar );
693                 break;
694             case QUANT_MATRIX_EXTENSION_ID:
695                 QuantMatrixExtension( p_vpar );
696                 break;
697             case SEQUENCE_SCALABLE_EXTENSION_ID:
698                 SequenceScalableExtension( p_vpar );
699                 break;
700             case PICTURE_DISPLAY_EXTENSION_ID:
701                 PictureDisplayExtension( p_vpar );
702                 break;
703             case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
704                 PictureSpatialScalableExtension( p_vpar );
705                 break;
706             case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
707                 PictureTemporalScalableExtension( p_vpar );
708                 break;
709             case COPYRIGHT_EXTENSION_ID:
710                 CopyrightExtension( p_vpar );
711                 break;
712             default:
713             }
714             break;
715
716         case USER_DATA_START_CODE:
717             DumpBits32( &p_vpar->bit_stream );
718             /* Wait for the next start code */
719             break;
720
721         default:
722             return;
723         }
724     }
725 }
726
727
728 /*****************************************************************************
729  * SequenceDisplayExtension : Parse the sequence_display_extension structure *
730  *****************************************************************************/
731
732 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
733 {
734     /* We don't care sequence_display_extension. */
735     /* video_format */
736     DumpBits( &p_vpar->bit_stream, 3 );
737     if( GetBits( &p_vpar->bit_stream, 1 ) )
738     {
739         /* Three bytes for color_desciption */
740         DumpBits( &p_vpar->bit_stream, 24 );
741     }
742     /* display_horizontal and vertical_size and a marker_bit */
743     DumpBits( &p_vpar->bit_stream, 29 );
744 }
745
746
747 /*****************************************************************************
748  * QuantMatrixExtension : Load quantization matrices for luminance           *
749  *                        and chrominance                                    *
750  *****************************************************************************/
751
752 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
753 {
754     if( GetBits( &p_vpar->bit_stream, 1 ) )
755     {
756         /* Load intra_quantiser_matrix for luminance. */
757         LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
758     }
759     else
760     {
761         /* Use the default matrix. */
762         LinkMatrix( &p_vpar->sequence.intra_quant,
763                     pi_default_intra_quant );
764     }
765     if( GetBits( &p_vpar->bit_stream, 1 ) )
766     {
767         /* Load non_intra_quantiser_matrix for luminance. */
768         LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
769     }
770     else
771     {
772         /* Use the default matrix. */
773         LinkMatrix( &p_vpar->sequence.nonintra_quant,
774                     pi_default_nonintra_quant );
775     }
776     if( GetBits( &p_vpar->bit_stream, 1 ) )
777     {
778         /* Load intra_quantiser_matrix for chrominance. */
779         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
780     }
781     else
782     {
783         /* Link the chrominance intra matrix to the luminance one. */
784         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
785                     p_vpar->sequence.intra_quant.pi_matrix );
786     }
787     if( GetBits( &p_vpar->bit_stream, 1 ) )
788     {
789         /* Load non_intra_quantiser_matrix for chrominance. */
790         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
791     }
792     else
793     {
794         /* Link the chrominance intra matrix to the luminance one. */
795         LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
796                     p_vpar->sequence.intra_quant.pi_matrix );
797     }
798     if( GetBits( &p_vpar->bit_stream, 1 ) )
799     {
800         /* Load non_intra_quantiser_matrix for chrominance. */
801         LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
802     }
803     else
804     {
805         /* Link the chrominance nonintra matrix to the luminance one. */
806         LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
807                     p_vpar->sequence.nonintra_quant.pi_matrix );
808     }
809 }
810
811
812 /*****************************************************************************
813  * SequenceScalableExtension : Parse the sequence_scalable_extension         *
814  *                             structure to handle scalable coding           *
815  *****************************************************************************/
816
817 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
818 {
819     /* We don't care about anything scalable except the scalable mode. */
820     switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
821     /* The length of the structure depends on the value of the scalable_mode */
822     {
823         case 1:
824             DumpBits32( &p_vpar->bit_stream );
825             DumpBits( &p_vpar->bit_stream, 21 );
826             break;
827         case 2:
828             DumpBits( &p_vpar->bit_stream, 12 );
829             break;
830         default:
831             DumpBits( &p_vpar->bit_stream, 4 );
832     }
833
834 }
835 /*****************************************************************************
836  * PictureDisplayExtension : Parse the picture_display_extension structure   *
837  *****************************************************************************/
838
839 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
840 {
841     /* Number of frame center offset */
842     int nb;
843     /* I am not sure it works but it should
844         (fewer tests than shown in §6.3.12) */
845     nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
846                                           p_vpar->picture.b_repeat_first_field +
847                                           p_vpar->picture.b_top_field_first
848                          : ( p_vpar->picture.b_frame_structure + 1 ) +
849                            p_vpar->picture.b_repeat_first_field;
850     DumpBits( &p_vpar->bit_stream, 34 * nb );
851 }
852
853
854 /*****************************************************************************
855  * PictureSpatialScalableExtension                                           *
856  *****************************************************************************/
857
858 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
859 {
860     /* That's scalable, so we trash it */
861     DumpBits32( &p_vpar->bit_stream );
862     DumpBits( &p_vpar->bit_stream, 14 );
863 }
864
865
866 /*****************************************************************************
867  * PictureTemporalScalableExtension                                          *
868  *****************************************************************************/
869
870 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
871 {
872     /* Scalable again, trashed again */
873     DumpBits( &p_vpar->bit_stream, 23 );
874 }
875
876
877 /*****************************************************************************
878  * CopyrightExtension : Keeps some legal informations                        *
879  *****************************************************************************/
880
881 static void CopyrightExtension( vpar_thread_t * p_vpar )
882 {
883     u32     i_copyright_nb_1, i_copyright_nb_2; /* local integers */
884     p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
885         /* A flag that says whether the copyright information is significant */
886     p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
887         /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
888     p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
889         /* Reserved bits */
890     DumpBits( &p_vpar->bit_stream, 8 );
891         /* The copyright_number is split in three parts */
892         /* first part */
893     i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
894     DumpBits( &p_vpar->bit_stream, 1 );
895         /* second part */
896     i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
897     DumpBits( &p_vpar->bit_stream, 1 );
898         /* third part and sum */
899     p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
900                                       ( (u64)i_copyright_nb_2 << 22 ) +
901                                       ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
902 }
903
904
905 /*****************************************************************************
906  * LoadMatrix : Load a quantization matrix
907  *****************************************************************************/
908 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
909 {
910     int i_dummy;
911     
912     if( !p_matrix->b_allocated )
913     {
914         /* Allocate a piece of memory to load the matrix. */
915         p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
916         p_matrix->b_allocated = 1;
917     }
918     
919     for( i_dummy = 0; i_dummy < 64; i_dummy++ )
920     {
921         p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
922              = GetBits( &p_vpar->bit_stream, 8 );
923     }
924
925 #ifdef FOURIER_IDCT
926     /* Discrete Fourier Transform requires the quantization matrices to
927      * be normalized before using them. */
928     vdec_NormQuantMatrix( p_matrix->pi_matrix );
929 #endif
930 }
931
932 /*****************************************************************************
933  * LinkMatrix : Link a quantization matrix to another
934  *****************************************************************************/
935 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
936 {
937     int i_dummy;
938     
939     if( p_matrix->b_allocated )
940     {
941         /* Deallocate the piece of memory. */
942         free( p_matrix->pi_matrix );
943         p_matrix->b_allocated = 0;
944     }
945     
946     p_matrix->pi_matrix = pi_array;
947 }