1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
16 #include <X11/extensions/XShm.h>
21 #include "vlc_thread.h"
24 #include "debug.h" /* ?? temporaire, requis par netlist.h */
27 #include "input_netlist.h"
28 #include "decoder_fifo.h"
30 #include "video_output.h"
32 #include "vdec_idct.h"
33 #include "video_decoder.h"
34 #include "vdec_motion.h"
36 #include "vpar_blocks.h"
37 #include "vpar_headers.h"
38 #include "video_fifo.h"
39 #include "video_parser.h"
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,
50 picture_t * p_newref );
51 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
53 picture_t * p_newref );
54 static void SliceHeader00( vpar_thread_t * p_vpar,
55 int * pi_mb_address, int i_mb_base,
57 static void SliceHeader01( vpar_thread_t * p_vpar,
58 int * pi_mb_address, int i_mb_base,
60 static void SliceHeader10( vpar_thread_t * p_vpar,
61 int * pi_mb_address, int i_mb_base,
63 static void SliceHeader11( vpar_thread_t * p_vpar,
64 int * pi_mb_address, int i_mb_base,
66 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
67 int * pi_mb_address, int i_mb_base,
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 );
80 /*****************************************************************************
81 * vpar_NextSequenceHeader : Find the next sequence header
82 *****************************************************************************/
83 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
85 while( !p_vpar->b_die )
87 NextStartCode( p_vpar );
88 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
94 /*****************************************************************************
95 * vpar_ParseHeader : Parse the next header
96 *****************************************************************************/
97 int vpar_ParseHeader( vpar_thread_t * p_vpar )
99 while( !p_vpar->b_die )
101 NextStartCode( p_vpar );
102 switch( GetBits32( &p_vpar->bit_stream ) )
104 case SEQUENCE_HEADER_CODE:
105 SequenceHeader( p_vpar );
109 case GROUP_START_CODE:
110 GroupHeader( p_vpar );
114 case PICTURE_START_CODE:
115 PictureHeader( p_vpar );
119 case SEQUENCE_END_CODE:
131 * Following functions are local
134 /*****************************************************************************
135 * NextStartCode : Find the next start code
136 *****************************************************************************/
137 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
139 /* Re-align the buffer on an 8-bit boundary */
140 RealignBits( &p_vpar->bit_stream );
142 while( ShowBits( &p_vpar->bit_stream, 24 ) != 0x01L && !p_vpar->b_die )
144 DumpBits( &p_vpar->bit_stream, 8 );
148 /*****************************************************************************
149 * SequenceHeader : Parse the next sequence header
150 *****************************************************************************/
151 static void SequenceHeader( vpar_thread_t * p_vpar )
154 static double d_frame_rate_table[16] =
157 ((23.0*1000.0)/1001.0),
160 ((30.0*1000.0)/1001.0),
163 ((60.0*1000.0)/1001.0),
165 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
169 int i_height_save, i_width_save;
171 i_height_save = p_vpar->sequence.i_height;
172 i_width_save = p_vpar->sequence.i_width;
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 ) ];
180 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
181 * constrained_parameters_flag */
182 DumpBits( &p_vpar->bit_stream, 30 );
185 * Quantization matrices
187 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
189 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
193 /* Use default matrix. */
194 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
197 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
199 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
203 /* Use default matrix. */
204 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
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 );
217 NextStartCode( p_vpar );
218 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
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};
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);
248 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
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;
259 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
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;
273 /* Slice Header functions */
274 if( p_vpar->sequence.i_height <= 2800 )
276 if( p_vpar->sequence.i_scalable_mode != SC_DP )
278 p_vpar->sequence.pf_slice_header = SliceHeader00;
282 p_vpar->sequence.pf_slice_header = SliceHeader01;
287 if( p_vpar->sequence.i_scalable_mode != SC_DP )
289 p_vpar->sequence.pf_slice_header = SliceHeader10;
293 p_vpar->sequence.pf_slice_header = SliceHeader11;
297 if( p_vpar->sequence.i_width != i_width_save
298 || p_vpar->sequence.i_height != i_height_save )
300 /* What do we do in case of a size change ??? */
303 /* Extension and User data */
304 ExtensionAndUserData( p_vpar );
307 /*****************************************************************************
308 * GroupHeader : Parse the next group of pictures header
309 *****************************************************************************/
310 static void GroupHeader( vpar_thread_t * p_vpar )
312 /* Nothing to do, we don't care. */
313 DumpBits( &p_vpar->bit_stream, 27 );
314 ExtensionAndUserData( p_vpar );
317 /*****************************************************************************
318 * PictureHeader : Parse the next picture header
319 *****************************************************************************/
320 static void PictureHeader( vpar_thread_t * p_vpar )
322 static f_macroblock_type_t ppf_macroblock_type[4] =
323 {vpar_IMBType, vpar_PMBType,
324 vpar_BMBType, vpar_DMBType};
327 int i_mb_address, i_mb_base, i_mb;
328 elem_t * p_y, p_u, p_v;
329 boolean_t b_parsable;
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];
337 DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
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 );
344 /* extra_information_picture */
345 while( GetBits( &p_vpar->bit_stream, 1 ) )
347 DumpBits( &p_vpar->bit_stream, 8 );
351 * Picture Coding Extension
353 NextStartCode( p_vpar );
354 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
356 /* Parse picture_coding_extension */
357 DumpBits32( &p_vpar->bit_stream );
358 /* extension_start_code_identifier */
359 DumpBits( &p_vpar->bit_stream, 4 );
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 );
381 /* composite_display_flag */
382 if( GetBits( &p_vpar->bit_stream, 1 ) )
384 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
385 * sub_carrier_phase */
386 DumpBits( &p_vpar->bit_stream, 20 );
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;
403 if( p_vpar->picture.i_current_structure &&
404 (i_structure == FRAME_STRUCTURE ||
405 i_structure == p_vpar->picture.i_current_structure) )
407 /* We don't have the second field of the buffered frame. */
408 if( p_vpar->picture.p_picture != NULL )
410 ReferenceReplace( p_vpar,
411 p_vpar->picture.i_coding_type,
414 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
416 vpar_DestroyMacroblock( &p_vpar->vfifo,
417 p_vpar->picture.pp_mb[i_mb] );
419 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
422 p_vpar->picture.i_current_structure = 0;
424 intf_DbgMsg("vpar debug: odd number of field picture.");
427 if( p_vpar->picture.i_current_structure )
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);
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));
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 );
452 /* Update the reference pointers. */
453 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
455 /* Warn Synchro we have trashed a picture. */
456 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
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;
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;
470 if( !p_vpar->picture.i_current_structure )
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 );
479 /* Initialize values. */
480 P_picture->date = vpar_SynchroDecode( p_vpar,
481 p_vpar->picture.i_coding_type,
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 )) );
489 /* Update the reference pointers. */
490 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
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);
496 /* Initialize picture data for decoding. */
497 if( i_structure == BOTTOM_FIELD )
499 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
507 /* Extension and User data. */
508 ExtensionAndUserData( p_vpar );
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)
515 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
516 < SLICE_START_CODE_MIN) ||
517 (i_dummy > SLICE_START_CODE_MAX) )
519 intf_DbgMsg("vpar debug: premature end of picture");
520 p_vpar->picture.b_error = 1;
523 DumpBits32( &p_vpar->bit_stream );
525 /* Decode slice data. */
526 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
529 if( p_vpar->picture.b_error )
532 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
534 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
537 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
538 vout_DestroyPicture( p_vpar->p_vout, P_picture );
540 /* Prepare context for the next picture. */
543 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
545 /* Frame completely parsed. */
546 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
548 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
551 /* Prepare context for the next picture. */
557 /*****************************************************************************
558 * ReferenceUpdate : Update the reference pointers when we have a new picture
559 *****************************************************************************/
560 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
562 picture_t * p_newref )
564 if( i_coding_type != B_CODING_TYPE )
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 );
575 /*****************************************************************************
576 * ReferenceReplace : Replace the last reference pointer when we destroy
578 *****************************************************************************/
579 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
581 picture_t * p_newref )
583 if( i_coding_type != B_CODING_TYPE )
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 );
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,
603 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
606 static void SliceHeader01( vpar_thread_t * p_vpar,
607 int * pi_mb_address, int i_mb_base,
610 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
611 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
614 static void SliceHeader10( vpar_thread_t * p_vpar,
615 int * pi_mb_address, int i_mb_base,
618 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
619 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
622 static void SliceHeader11( vpar_thread_t * p_vpar,
623 int * pi_mb_address, int i_mb_base,
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 );
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,
638 /* DC predictors initialization table */
639 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
641 int i_mb_address_save = *pi_mb_address;
643 /* slice_vertical_position_extension and priority_breakpoint already done */
644 LoadQuantizerScale( p_vpar );
646 if( GetBits( &p_vpar->bit_stream, 1 ) )
648 /* intra_slice, slice_id */
649 DumpBits( &p_vpar->bit_stream, 8 );
650 /* extra_information_slice */
651 while( GetBits( &p_vpar->bit_stream, 1 ) )
653 DumpBits( &p_vpar->bit_stream, 8 );
657 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
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];
665 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
666 bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
670 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
672 i_mb_address_save = *pi_mb_address;
674 while( !ShowBits( &p_vpar->bit_stream, 23 ) );
677 /*****************************************************************************
678 * ExtensionAndUserData : Parse the extension_and_user_data structure
679 *****************************************************************************/
680 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
682 while( !p_vpar->b_die )
684 NextStartCode( p_vpar );
685 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
687 case EXTENSION_START_CODE:
688 DumpBits32( &p_vpar->bit_stream );
689 switch( GetBits( &p_vpar->bit_stream, 4 ) )
691 case SEQUENCE_DISPLAY_EXTENSION_ID:
692 SequenceDisplayExtension( p_vpar );
694 case QUANT_MATRIX_EXTENSION_ID:
695 QuantMatrixExtension( p_vpar );
697 case SEQUENCE_SCALABLE_EXTENSION_ID:
698 SequenceScalableExtension( p_vpar );
700 case PICTURE_DISPLAY_EXTENSION_ID:
701 PictureDisplayExtension( p_vpar );
703 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
704 PictureSpatialScalableExtension( p_vpar );
706 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
707 PictureTemporalScalableExtension( p_vpar );
709 case COPYRIGHT_EXTENSION_ID:
710 CopyrightExtension( p_vpar );
716 case USER_DATA_START_CODE:
717 DumpBits32( &p_vpar->bit_stream );
718 /* Wait for the next start code */
728 /*****************************************************************************
729 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
730 *****************************************************************************/
732 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
734 /* We don't care sequence_display_extension. */
736 DumpBits( &p_vpar->bit_stream, 3 );
737 if( GetBits( &p_vpar->bit_stream, 1 ) )
739 /* Three bytes for color_desciption */
740 DumpBits( &p_vpar->bit_stream, 24 );
742 /* display_horizontal and vertical_size and a marker_bit */
743 DumpBits( &p_vpar->bit_stream, 29 );
747 /*****************************************************************************
748 * QuantMatrixExtension : Load quantization matrices for luminance *
750 *****************************************************************************/
752 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
754 if( GetBits( &p_vpar->bit_stream, 1 ) )
756 /* Load intra_quantiser_matrix for luminance. */
757 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
761 /* Use the default matrix. */
762 LinkMatrix( &p_vpar->sequence.intra_quant,
763 pi_default_intra_quant );
765 if( GetBits( &p_vpar->bit_stream, 1 ) )
767 /* Load non_intra_quantiser_matrix for luminance. */
768 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
772 /* Use the default matrix. */
773 LinkMatrix( &p_vpar->sequence.nonintra_quant,
774 pi_default_nonintra_quant );
776 if( GetBits( &p_vpar->bit_stream, 1 ) )
778 /* Load intra_quantiser_matrix for chrominance. */
779 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
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 );
787 if( GetBits( &p_vpar->bit_stream, 1 ) )
789 /* Load non_intra_quantiser_matrix for chrominance. */
790 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
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 );
798 if( GetBits( &p_vpar->bit_stream, 1 ) )
800 /* Load non_intra_quantiser_matrix for chrominance. */
801 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
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 );
812 /*****************************************************************************
813 * SequenceScalableExtension : Parse the sequence_scalable_extension *
814 * structure to handle scalable coding *
815 *****************************************************************************/
817 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
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 */
824 DumpBits32( &p_vpar->bit_stream );
825 DumpBits( &p_vpar->bit_stream, 21 );
828 DumpBits( &p_vpar->bit_stream, 12 );
831 DumpBits( &p_vpar->bit_stream, 4 );
835 /*****************************************************************************
836 * PictureDisplayExtension : Parse the picture_display_extension structure *
837 *****************************************************************************/
839 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
841 /* Number of frame center offset */
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 );
854 /*****************************************************************************
855 * PictureSpatialScalableExtension *
856 *****************************************************************************/
858 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
860 /* That's scalable, so we trash it */
861 DumpBits32( &p_vpar->bit_stream );
862 DumpBits( &p_vpar->bit_stream, 14 );
866 /*****************************************************************************
867 * PictureTemporalScalableExtension *
868 *****************************************************************************/
870 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
872 /* Scalable again, trashed again */
873 DumpBits( &p_vpar->bit_stream, 23 );
877 /*****************************************************************************
878 * CopyrightExtension : Keeps some legal informations *
879 *****************************************************************************/
881 static void CopyrightExtension( vpar_thread_t * p_vpar )
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 );
890 DumpBits( &p_vpar->bit_stream, 8 );
891 /* The copyright_number is split in three parts */
893 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
894 DumpBits( &p_vpar->bit_stream, 1 );
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 ) );
905 /*****************************************************************************
906 * LoadMatrix : Load a quantization matrix
907 *****************************************************************************/
908 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
912 if( !p_matrix->b_allocated )
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;
919 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
921 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
922 = GetBits( &p_vpar->bit_stream, 8 );
926 /* Discrete Fourier Transform requires the quantization matrices to
927 * be normalized before using them. */
928 vdec_NormQuantMatrix( p_matrix->pi_matrix );
932 /*****************************************************************************
933 * LinkMatrix : Link a quantization matrix to another
934 *****************************************************************************/
935 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
939 if( p_matrix->b_allocated )
941 /* Deallocate the piece of memory. */
942 free( p_matrix->pi_matrix );
943 p_matrix->b_allocated = 0;
946 p_matrix->pi_matrix = pi_array;