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 "vpar_synchro.h"
40 #include "video_parser.h"
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,
51 picture_t * p_newref );
52 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
54 picture_t * p_newref );
55 static void SliceHeader00( vpar_thread_t * p_vpar,
56 int * pi_mb_address, int i_mb_base,
58 static void SliceHeader01( vpar_thread_t * p_vpar,
59 int * pi_mb_address, int i_mb_base,
61 static void SliceHeader10( vpar_thread_t * p_vpar,
62 int * pi_mb_address, int i_mb_base,
64 static void SliceHeader11( vpar_thread_t * p_vpar,
65 int * pi_mb_address, int i_mb_base,
67 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
68 int * pi_mb_address, int i_mb_base,
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 );
81 /*****************************************************************************
82 * vpar_NextSequenceHeader : Find the next sequence header
83 *****************************************************************************/
84 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
86 while( !p_vpar->b_die )
88 NextStartCode( p_vpar );
89 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
95 /*****************************************************************************
96 * vpar_ParseHeader : Parse the next header
97 *****************************************************************************/
98 int vpar_ParseHeader( vpar_thread_t * p_vpar )
100 while( !p_vpar->b_die )
102 NextStartCode( p_vpar );
103 switch( GetBits32( &p_vpar->bit_stream ) )
105 case SEQUENCE_HEADER_CODE:
106 SequenceHeader( p_vpar );
110 case GROUP_START_CODE:
111 GroupHeader( p_vpar );
115 case PICTURE_START_CODE:
116 PictureHeader( p_vpar );
120 case SEQUENCE_END_CODE:
132 * Following functions are local
135 /*****************************************************************************
136 * NextStartCode : Find the next start code
137 *****************************************************************************/
138 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
140 /* Re-align the buffer on an 8-bit boundary */
141 RealignBits( &p_vpar->bit_stream );
143 while( ShowBits( &p_vpar->bit_stream, 24 ) != 0x01L && !p_vpar->b_die )
145 DumpBits( &p_vpar->bit_stream, 8 );
149 /*****************************************************************************
150 * SequenceHeader : Parse the next sequence header
151 *****************************************************************************/
152 static void SequenceHeader( vpar_thread_t * p_vpar )
155 static double d_frame_rate_table[16] =
158 ((23.0*1000.0)/1001.0),
161 ((30.0*1000.0)/1001.0),
164 ((60.0*1000.0)/1001.0),
166 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
170 int i_height_save, i_width_save;
172 i_height_save = p_vpar->sequence.i_height;
173 i_width_save = p_vpar->sequence.i_width;
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 ) ];
181 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
182 * constrained_parameters_flag */
183 DumpBits( &p_vpar->bit_stream, 30 );
186 * Quantization matrices
188 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
190 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
194 /* Use default matrix. */
195 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
198 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
200 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
204 /* Use default matrix. */
205 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
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 );
218 NextStartCode( p_vpar );
219 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
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.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);
244 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
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;
253 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
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;
268 /* Update chromatic information */
269 switch( p_vpar->sequence.i_chroma_format )
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;
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;
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;
292 /* Slice Header functions */
293 if( p_vpar->sequence.i_height <= 2800 )
295 if( p_vpar->sequence.i_scalable_mode != SC_DP )
297 p_vpar->sequence.pf_slice_header = SliceHeader00;
301 p_vpar->sequence.pf_slice_header = SliceHeader01;
306 if( p_vpar->sequence.i_scalable_mode != SC_DP )
308 p_vpar->sequence.pf_slice_header = SliceHeader10;
312 p_vpar->sequence.pf_slice_header = SliceHeader11;
316 if( p_vpar->sequence.i_width != i_width_save
317 || p_vpar->sequence.i_height != i_height_save )
319 /* What do we do in case of a size change ??? */
322 /* Extension and User data */
323 ExtensionAndUserData( p_vpar );
326 /*****************************************************************************
327 * GroupHeader : Parse the next group of pictures header
328 *****************************************************************************/
329 static void GroupHeader( vpar_thread_t * p_vpar )
331 /* Nothing to do, we don't care. */
332 DumpBits( &p_vpar->bit_stream, 27 );
333 ExtensionAndUserData( p_vpar );
336 /*****************************************************************************
337 * PictureHeader : Parse the next picture header
338 *****************************************************************************/
339 static void PictureHeader( vpar_thread_t * p_vpar )
341 static f_macroblock_type_t ppf_macroblock_type[4] =
342 {vpar_IMBType, vpar_PMBType,
343 vpar_BMBType, vpar_DMBType};
346 int i_mb_address, i_mb_base, i_mb;
347 elem_t * p_y, p_u, p_v;
348 boolean_t b_parsable;
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];
356 DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
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 );
363 /* extra_information_picture */
364 while( GetBits( &p_vpar->bit_stream, 1 ) )
366 DumpBits( &p_vpar->bit_stream, 8 );
370 * Picture Coding Extension
372 NextStartCode( p_vpar );
373 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
375 /* Parse picture_coding_extension */
376 DumpBits32( &p_vpar->bit_stream );
377 /* extension_start_code_identifier */
378 DumpBits( &p_vpar->bit_stream, 4 );
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 );
400 /* composite_display_flag */
401 if( GetBits( &p_vpar->bit_stream, 1 ) )
403 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
404 * sub_carrier_phase */
405 DumpBits( &p_vpar->bit_stream, 20 );
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;
422 if( p_vpar->picture.i_current_structure &&
423 (i_structure == FRAME_STRUCTURE ||
424 i_structure == p_vpar->picture.i_current_structure) )
426 /* We don't have the second field of the buffered frame. */
427 if( p_vpar->picture.p_picture != NULL )
429 ReferenceReplace( p_vpar,
430 p_vpar->picture.i_coding_type,
433 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
435 vpar_DestroyMacroblock( &p_vpar->vfifo,
436 p_vpar->picture.pp_mb[i_mb] );
438 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
441 p_vpar->picture.i_current_structure = 0;
443 intf_DbgMsg("vpar debug: odd number of field picture.");
446 if( p_vpar->picture.i_current_structure )
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);
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));
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 );
471 /* Update the reference pointers. */
472 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
474 /* Warn Synchro we have trashed a picture. */
475 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
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;
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;
489 if( !p_vpar->picture.i_current_structure )
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 );
498 /* Initialize values. */
499 P_picture->date = vpar_SynchroDecode( p_vpar,
500 p_vpar->picture.i_coding_type,
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 )) );
508 /* Update the reference pointers. */
509 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
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);
515 /* Initialize picture data for decoding. */
516 if( i_structure == BOTTOM_FIELD )
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;
525 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
528 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
530 /* Extension and User data. */
531 ExtensionAndUserData( p_vpar );
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)
538 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
539 < SLICE_START_CODE_MIN) ||
540 (i_dummy > SLICE_START_CODE_MAX) )
542 intf_DbgMsg("vpar debug: premature end of picture");
543 p_vpar->picture.b_error = 1;
546 DumpBits32( &p_vpar->bit_stream );
548 /* Decode slice data. */
549 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
552 if( p_vpar->picture.b_error )
555 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
557 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
560 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
561 vout_DestroyPicture( p_vpar->p_vout, P_picture );
563 /* Prepare context for the next picture. */
566 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
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++ )
572 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
575 /* Prepare context for the next picture. */
581 /*****************************************************************************
582 * ReferenceUpdate : Update the reference pointers when we have a new picture
583 *****************************************************************************/
584 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
586 picture_t * p_newref )
588 if( i_coding_type != B_CODING_TYPE )
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 );
599 /*****************************************************************************
600 * ReferenceReplace : Replace the last reference pointer when we destroy
602 *****************************************************************************/
603 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
605 picture_t * p_newref )
607 if( i_coding_type != B_CODING_TYPE )
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 );
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,
627 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
630 static void SliceHeader01( vpar_thread_t * p_vpar,
631 int * pi_mb_address, int i_mb_base,
634 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
635 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
638 static void SliceHeader10( vpar_thread_t * p_vpar,
639 int * pi_mb_address, int i_mb_base,
642 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
643 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
646 static void SliceHeader11( vpar_thread_t * p_vpar,
647 int * pi_mb_address, int i_mb_base,
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 );
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,
662 /* DC predictors initialization table */
663 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
665 int i_mb_address_save = *pi_mb_address;
667 /* slice_vertical_position_extension and priority_breakpoint already done */
668 LoadQuantizerScale( p_vpar );
670 if( GetBits( &p_vpar->bit_stream, 1 ) )
672 /* intra_slice, slice_id */
673 DumpBits( &p_vpar->bit_stream, 8 );
674 /* extra_information_slice */
675 while( GetBits( &p_vpar->bit_stream, 1 ) )
677 DumpBits( &p_vpar->bit_stream, 8 );
681 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
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];
689 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
690 bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
694 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
696 i_mb_address_save = *pi_mb_address;
698 while( !ShowBits( &p_vpar->bit_stream, 23 ) );
701 /*****************************************************************************
702 * ExtensionAndUserData : Parse the extension_and_user_data structure
703 *****************************************************************************/
704 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
706 while( !p_vpar->b_die )
708 NextStartCode( p_vpar );
709 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
711 case EXTENSION_START_CODE:
712 DumpBits32( &p_vpar->bit_stream );
713 switch( GetBits( &p_vpar->bit_stream, 4 ) )
715 case SEQUENCE_DISPLAY_EXTENSION_ID:
716 SequenceDisplayExtension( p_vpar );
718 case QUANT_MATRIX_EXTENSION_ID:
719 QuantMatrixExtension( p_vpar );
721 case SEQUENCE_SCALABLE_EXTENSION_ID:
722 SequenceScalableExtension( p_vpar );
724 case PICTURE_DISPLAY_EXTENSION_ID:
725 PictureDisplayExtension( p_vpar );
727 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
728 PictureSpatialScalableExtension( p_vpar );
730 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
731 PictureTemporalScalableExtension( p_vpar );
733 case COPYRIGHT_EXTENSION_ID:
734 CopyrightExtension( p_vpar );
740 case USER_DATA_START_CODE:
741 DumpBits32( &p_vpar->bit_stream );
742 /* Wait for the next start code */
752 /*****************************************************************************
753 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
754 *****************************************************************************/
756 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
758 /* We don't care sequence_display_extension. */
760 DumpBits( &p_vpar->bit_stream, 3 );
761 if( GetBits( &p_vpar->bit_stream, 1 ) )
763 /* Three bytes for color_desciption */
764 DumpBits( &p_vpar->bit_stream, 24 );
766 /* display_horizontal and vertical_size and a marker_bit */
767 DumpBits( &p_vpar->bit_stream, 29 );
771 /*****************************************************************************
772 * QuantMatrixExtension : Load quantization matrices for luminance *
774 *****************************************************************************/
776 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
778 if( GetBits( &p_vpar->bit_stream, 1 ) )
780 /* Load intra_quantiser_matrix for luminance. */
781 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
785 /* Use the default matrix. */
786 LinkMatrix( &p_vpar->sequence.intra_quant,
787 pi_default_intra_quant );
789 if( GetBits( &p_vpar->bit_stream, 1 ) )
791 /* Load non_intra_quantiser_matrix for luminance. */
792 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
796 /* Use the default matrix. */
797 LinkMatrix( &p_vpar->sequence.nonintra_quant,
798 pi_default_nonintra_quant );
800 if( GetBits( &p_vpar->bit_stream, 1 ) )
802 /* Load intra_quantiser_matrix for chrominance. */
803 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
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 );
811 if( GetBits( &p_vpar->bit_stream, 1 ) )
813 /* Load non_intra_quantiser_matrix for chrominance. */
814 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
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 );
822 if( GetBits( &p_vpar->bit_stream, 1 ) )
824 /* Load non_intra_quantiser_matrix for chrominance. */
825 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
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 );
836 /*****************************************************************************
837 * SequenceScalableExtension : Parse the sequence_scalable_extension *
838 * structure to handle scalable coding *
839 *****************************************************************************/
841 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
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 */
848 DumpBits32( &p_vpar->bit_stream );
849 DumpBits( &p_vpar->bit_stream, 21 );
852 DumpBits( &p_vpar->bit_stream, 12 );
855 DumpBits( &p_vpar->bit_stream, 4 );
859 /*****************************************************************************
860 * PictureDisplayExtension : Parse the picture_display_extension structure *
861 *****************************************************************************/
863 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
865 /* Number of frame center offset */
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 );
878 /*****************************************************************************
879 * PictureSpatialScalableExtension *
880 *****************************************************************************/
882 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
884 /* That's scalable, so we trash it */
885 DumpBits32( &p_vpar->bit_stream );
886 DumpBits( &p_vpar->bit_stream, 14 );
890 /*****************************************************************************
891 * PictureTemporalScalableExtension *
892 *****************************************************************************/
894 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
896 /* Scalable again, trashed again */
897 DumpBits( &p_vpar->bit_stream, 23 );
901 /*****************************************************************************
902 * CopyrightExtension : Keeps some legal informations *
903 *****************************************************************************/
905 static void CopyrightExtension( vpar_thread_t * p_vpar )
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 );
914 DumpBits( &p_vpar->bit_stream, 8 );
915 /* The copyright_number is split in three parts */
917 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
918 DumpBits( &p_vpar->bit_stream, 1 );
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 ) );
929 /*****************************************************************************
930 * LoadMatrix : Load a quantization matrix
931 *****************************************************************************/
932 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
936 if( !p_matrix->b_allocated )
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;
943 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
945 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
946 = GetBits( &p_vpar->bit_stream, 8 );
950 /* Discrete Fourier Transform requires the quantization matrices to
951 * be normalized before using them. */
952 vdec_NormQuantMatrix( p_matrix->pi_matrix );
956 /*****************************************************************************
957 * LinkMatrix : Link a quantization matrix to another
958 *****************************************************************************/
959 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
963 if( p_matrix->b_allocated )
965 /* Deallocate the piece of memory. */
966 free( p_matrix->pi_matrix );
967 p_matrix->b_allocated = 0;
970 p_matrix->pi_matrix = pi_array;