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 int pi_chroma_nb_blocks[4] = {0, 1, 2, 4};
223 static f_chroma_pattern_t ppf_chroma_pattern[4] =
224 {NULL, vpar_CodedPattern420,
225 vpar_CodedPattern422, vpar_CodedPattern444};
227 /* Parse sequence_extension */
228 DumpBits32( &p_vpar->bit_stream );
229 /* extension_start_code_identifier, profile_and_level_indication */
230 DumpBits( &p_vpar->bit_stream, 12 );
231 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
232 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
233 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width
234 >> (3-p_vpar->sequence.i_chroma_format);
235 p_vpar->sequence.i_chroma_nb_blocks = pi_chroma_nb_blocks
236 [p_vpar->sequence.i_chroma_format];
237 p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
238 [p_vpar->sequence.i_chroma_format];
239 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
240 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
241 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
242 DumpBits( &p_vpar->bit_stream, 22 );
243 /* frame_rate_extension_n */
244 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
245 /* frame_rate_extension_d */
246 p_vpar->sequence.d_frame_rate *= (i_dummy + 1)
247 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
249 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
253 /* It's an MPEG-1 stream. Put adequate parameters. */
254 p_vpar->sequence.b_progressive = 1;
255 p_vpar->sequence.i_chroma_format = CHROMA_420;
256 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 2;
257 p_vpar->sequence.i_chroma_nb_blocks = 2;
258 p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
260 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
263 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
264 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
265 (p_vpar->sequence.i_height + 15) / 16 :
266 2 * (p_vpar->sequence.i_height + 31) / 32;
267 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
268 * p_vpar->sequence.i_mb_height;
269 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
270 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
271 p_vpar->sequence.i_size = p_vpar->sequence.i_width
272 * p_vpar->sequence.i_height;
274 /* Slice Header functions */
275 if( p_vpar->sequence.i_height <= 2800 )
277 if( p_vpar->sequence.i_scalable_mode != SC_DP )
279 p_vpar->sequence.pf_slice_header = SliceHeader00;
283 p_vpar->sequence.pf_slice_header = SliceHeader01;
288 if( p_vpar->sequence.i_scalable_mode != SC_DP )
290 p_vpar->sequence.pf_slice_header = SliceHeader10;
294 p_vpar->sequence.pf_slice_header = SliceHeader11;
298 if( p_vpar->sequence.i_width != i_width_save
299 || p_vpar->sequence.i_height != i_height_save )
301 /* What do we do in case of a size change ??? */
304 /* Extension and User data */
305 ExtensionAndUserData( p_vpar );
308 /*****************************************************************************
309 * GroupHeader : Parse the next group of pictures header
310 *****************************************************************************/
311 static void GroupHeader( vpar_thread_t * p_vpar )
313 /* Nothing to do, we don't care. */
314 DumpBits( &p_vpar->bit_stream, 27 );
315 ExtensionAndUserData( p_vpar );
318 /*****************************************************************************
319 * PictureHeader : Parse the next picture header
320 *****************************************************************************/
321 static void PictureHeader( vpar_thread_t * p_vpar )
323 static f_macroblock_type_t ppf_macroblock_type[4] =
324 {vpar_IMBType, vpar_PMBType,
325 vpar_BMBType, vpar_DMBType};
328 int i_mb_address, i_mb_base, i_mb;
329 elem_t * p_y, p_u, p_v;
330 boolean_t b_parsable;
333 DumpBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
334 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
335 p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
336 [p_vpar->picture.i_coding_type];
338 DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
340 p_vpar->picture.b_full_pel_forward_vector = GetBits( &p_vpar->bit_stream, 1 );
341 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
342 p_vpar->picture.b_full_pel_backward_vector = GetBits( &p_vpar->bit_stream, 1 );
343 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
345 /* extra_information_picture */
346 while( GetBits( &p_vpar->bit_stream, 1 ) )
348 DumpBits( &p_vpar->bit_stream, 8 );
352 * Picture Coding Extension
354 NextStartCode( p_vpar );
355 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
357 /* Parse picture_coding_extension */
358 DumpBits32( &p_vpar->bit_stream );
359 /* extension_start_code_identifier */
360 DumpBits( &p_vpar->bit_stream, 4 );
362 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
363 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
364 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
365 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
366 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
367 i_structure = GetBits( &p_vpar->bit_stream, 2 );
368 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
369 p_vpar->picture.b_frame_pred_frame_dct
370 = GetBits( &p_vpar->bit_stream, 1 );
371 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
372 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
373 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
374 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
375 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
376 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
377 * the length of the picture_display_extension structure.
378 * chroma_420_type (obsolete) */
379 DumpBits( &p_vpar->bit_stream, 1 );
380 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
382 /* composite_display_flag */
383 if( GetBits( &p_vpar->bit_stream, 1 ) )
385 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
386 * sub_carrier_phase */
387 DumpBits( &p_vpar->bit_stream, 20 );
392 /* MPEG-1 compatibility flags */
393 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
394 i_structure = FRAME_STRUCTURE;
395 p_vpar->picture.b_frame_pred_frame_dct = 1;
396 p_vpar->picture.b_concealment_mv = 0;
397 p_vpar->picture.b_q_scale_type = 0;
398 p_vpar->picture.b_intra_vlc_format = 0;
399 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
400 p_vpar->picture.b_repeat_first_field = 0;
401 p_vpar->picture.b_progressive_frame = 1;
404 if( p_vpar->picture.i_current_structure &&
405 (i_structure == FRAME_STRUCTURE ||
406 i_structure == p_vpar->picture.i_current_structure) )
408 /* We don't have the second field of the buffered frame. */
409 if( p_vpar->picture.p_picture != NULL )
411 ReferenceReplace( p_vpar,
412 p_vpar->picture.i_coding_type,
415 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
417 vpar_DestroyMacroblock( &p_vpar->vfifo,
418 p_vpar->picture.pp_mb[i_mb] );
420 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
423 p_vpar->picture.i_current_structure = 0;
425 intf_DbgMsg("vpar debug: odd number of field picture.");
428 if( p_vpar->picture.i_current_structure )
430 /* Second field of a frame. We will decode it if, and only if we
431 * have decoded the first frame. */
432 b_parsable = (p_vpar->picture.p_picture != NULL);
436 /* Do we have the reference pictures ? */
437 b_parsable = !((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
438 (p_vpar->sequence.p_forward == NULL)) ||
439 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
440 (p_vpar->sequence.p_forward == NULL ||
441 p_vpar->sequence.p_backward == NULL));
445 /* Does synchro say we have enough time to decode it ? */
446 b_parsable = vpar_SynchroChoose( p_vpar,
447 p_vpar->picture.i_coding_type, i_structure );
453 /* Update the reference pointers. */
454 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
456 /* Warn Synchro we have trashed a picture. */
457 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
459 /* Update context. */
460 if( i_structure != FRAME_STRUCTURE )
461 p_vpar->picture.i_current_structure = i_structure;
462 p_vpar->picture.p_picture = NULL;
467 /* OK, now we are sure we will decode the picture. */
468 #define P_picture p_vpar->picture.p_picture
469 p_vpar->picture.b_error = 0;
471 if( !p_vpar->picture.i_current_structure )
473 /* This is a new frame. Get a structure from the video_output. */
474 P_picture = vout_CreatePicture( p_vpar->p_vout,
475 SPLITTED_YUV_PICTURE,
476 p_vpar->sequence.i_width,
477 p_vpar->sequence.i_height,
478 p_vpar->sequence.i_chroma_format );
480 /* Initialize values. */
481 P_picture->date = vpar_SynchroDecode( p_vpar,
482 p_vpar->picture.i_coding_type,
484 p_vpar->picture.i_lum_incr = - 8 + ( p_vpar->sequence.i_width
485 << ( i_structure != FRAME_STRUCTURE ) );
486 p_vpar->picture.i_chroma_incr = -8 + ( p_vpar->sequence.i_width
487 << (( i_structure != FRAME_STRUCTURE ) +
488 ( 3 - p_vpar->sequence.i_chroma_format )) );
490 /* Update the reference pointers. */
491 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
493 p_vpar->picture.i_current_structure |= i_structure;
494 p_vpar->picture.i_structure = i_structure;
495 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
497 /* Initialize picture data for decoding. */
498 if( i_structure == BOTTOM_FIELD )
500 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
508 /* Extension and User data. */
509 ExtensionAndUserData( p_vpar );
511 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
512 NextStartCode( p_vpar );
513 while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
514 && !p_vpar->picture.b_error)
516 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
517 < SLICE_START_CODE_MIN) ||
518 (i_dummy > SLICE_START_CODE_MAX) )
520 intf_DbgMsg("vpar debug: premature end of picture");
521 p_vpar->picture.b_error = 1;
524 DumpBits32( &p_vpar->bit_stream );
526 /* Decode slice data. */
527 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
530 if( p_vpar->picture.b_error )
533 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
535 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
538 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
539 vout_DestroyPicture( p_vpar->p_vout, P_picture );
541 /* Prepare context for the next picture. */
544 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
546 /* Frame completely parsed. */
547 P_picture.i_deccount = p_vpar->sequence.i_mb_size;
548 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
550 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
553 /* Prepare context for the next picture. */
559 /*****************************************************************************
560 * ReferenceUpdate : Update the reference pointers when we have a new picture
561 *****************************************************************************/
562 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
564 picture_t * p_newref )
566 if( i_coding_type != B_CODING_TYPE )
568 if( p_vpar->sequence.p_forward != NULL )
569 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
570 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
571 p_vpar->sequence.p_backward = p_newref;
572 if( p_newref != NULL )
573 vout_LinkPicture( p_vpar->p_vout, p_newref );
577 /*****************************************************************************
578 * ReferenceReplace : Replace the last reference pointer when we destroy
580 *****************************************************************************/
581 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
583 picture_t * p_newref )
585 if( i_coding_type != B_CODING_TYPE )
587 if( p_vpar->sequence.p_backward != NULL )
588 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
589 p_vpar->sequence.p_backward = p_newref;
590 if( p_newref != NULL )
591 vout_LinkPicture( p_vpar->p_vout, p_newref );
595 /*****************************************************************************
596 * SliceHeaderXY : Parse the next slice structure
597 *****************************************************************************
598 * X = i_height > 2800 ?
599 * Y = scalable_mode == SC_DP ?
600 *****************************************************************************/
601 static void SliceHeader00( vpar_thread_t * p_vpar,
602 int * pi_mb_address, int i_mb_base,
605 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
608 static void SliceHeader01( vpar_thread_t * p_vpar,
609 int * pi_mb_address, int i_mb_base,
612 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
613 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
616 static void SliceHeader10( vpar_thread_t * p_vpar,
617 int * pi_mb_address, int i_mb_base,
620 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
621 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
624 static void SliceHeader11( vpar_thread_t * p_vpar,
625 int * pi_mb_address, int i_mb_base,
628 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
629 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
630 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
633 /*****************************************************************************
634 * SliceHeader : Parse the next slice structure
635 *****************************************************************************/
636 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
637 int * pi_mb_address, int i_mb_base,
640 /* DC predictors initialization table */
641 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
643 int i_mb_address_save = *pi_mb_address;
645 /* slice_vertical_position_extension and priority_breakpoint already done */
646 LoadQuantizerScale( p_vpar );
648 if( GetBits( &p_vpar->bit_stream, 1 ) )
650 /* intra_slice, slice_id */
651 DumpBits( &p_vpar->bit_stream, 8 );
652 /* extra_information_slice */
653 while( GetBits( &p_vpar->bit_stream, 1 ) )
655 DumpBits( &p_vpar->bit_stream, 8 );
659 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
661 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
662 * does the reference decoder put 0 instead of the normative values ? */
663 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
664 = p_vpar->slice.pi_dc_dct_pred[2]
665 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
667 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
668 bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
672 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
674 i_mb_address_save = *pi_mb_address;
676 while( !ShowBits( &p_vpar->bit_stream, 23 ) );
679 /*****************************************************************************
680 * ExtensionAndUserData : Parse the extension_and_user_data structure
681 *****************************************************************************/
682 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
684 while( !p_vpar->b_die )
686 NextStartCode( p_vpar );
687 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
689 case EXTENSION_START_CODE:
690 DumpBits32( &p_vpar->bit_stream );
691 switch( GetBits( &p_vpar->bit_stream, 4 ) )
693 case SEQUENCE_DISPLAY_EXTENSION_ID:
694 SequenceDisplayExtension( p_vpar );
696 case QUANT_MATRIX_EXTENSION_ID:
697 QuantMatrixExtension( p_vpar );
699 case SEQUENCE_SCALABLE_EXTENSION_ID:
700 SequenceScalableExtension( p_vpar );
702 case PICTURE_DISPLAY_EXTENSION_ID:
703 PictureDisplayExtension( p_vpar );
705 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
706 PictureSpatialScalableExtension( p_vpar );
708 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
709 PictureTemporalScalableExtension( p_vpar );
711 case COPYRIGHT_EXTENSION_ID:
712 CopyrightExtension( p_vpar );
718 case USER_DATA_START_CODE:
719 DumpBits32( &p_vpar->bit_stream );
720 /* Wait for the next start code */
730 /*****************************************************************************
731 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
732 *****************************************************************************/
734 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
736 /* We don't care sequence_display_extension. */
738 DumpBits( &p_vpar->bit_stream, 3 );
739 if( GetBits( &p_vpar->bit_stream, 1 ) )
741 /* Three bytes for color_desciption */
742 DumpBits( &p_vpar->bit_stream, 24 );
744 /* display_horizontal and vertical_size and a marker_bit */
745 DumpBits( &p_vpar->bit_stream, 29 );
749 /*****************************************************************************
750 * QuantMatrixExtension : Load quantization matrices for luminance *
752 *****************************************************************************/
754 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
756 if( GetBits( &p_vpar->bit_stream, 1 ) )
758 /* Load intra_quantiser_matrix for luminance. */
759 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
763 /* Use the default matrix. */
764 LinkMatrix( &p_vpar->sequence.intra_quant,
765 pi_default_intra_quant );
767 if( GetBits( &p_vpar->bit_stream, 1 ) )
769 /* Load non_intra_quantiser_matrix for luminance. */
770 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
774 /* Use the default matrix. */
775 LinkMatrix( &p_vpar->sequence.nonintra_quant,
776 pi_default_nonintra_quant );
778 if( GetBits( &p_vpar->bit_stream, 1 ) )
780 /* Load intra_quantiser_matrix for chrominance. */
781 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
785 /* Link the chrominance intra matrix to the luminance one. */
786 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
787 p_vpar->sequence.intra_quant.pi_matrix );
789 if( GetBits( &p_vpar->bit_stream, 1 ) )
791 /* Load non_intra_quantiser_matrix for chrominance. */
792 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
796 /* Link the chrominance intra matrix to the luminance one. */
797 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
798 p_vpar->sequence.intra_quant.pi_matrix );
800 if( GetBits( &p_vpar->bit_stream, 1 ) )
802 /* Load non_intra_quantiser_matrix for chrominance. */
803 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
807 /* Link the chrominance nonintra matrix to the luminance one. */
808 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
809 p_vpar->sequence.nonintra_quant.pi_matrix );
814 /*****************************************************************************
815 * SequenceScalableExtension : Parse the sequence_scalable_extension *
816 * structure to handle scalable coding *
817 *****************************************************************************/
819 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
821 /* We don't care about anything scalable except the scalable mode. */
822 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
823 /* The length of the structure depends on the value of the scalable_mode */
826 DumpBits32( &p_vpar->bit_stream );
827 DumpBits( &p_vpar->bit_stream, 21 );
830 DumpBits( &p_vpar->bit_stream, 12 );
833 DumpBits( &p_vpar->bit_stream, 4 );
837 /*****************************************************************************
838 * PictureDisplayExtension : Parse the picture_display_extension structure *
839 *****************************************************************************/
841 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
843 /* Number of frame center offset */
845 /* I am not sure it works but it should
846 (fewer tests than shown in ยง6.3.12) */
847 nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
848 p_vpar->picture.b_repeat_first_field +
849 p_vpar->picture.b_top_field_first
850 : ( p_vpar->picture.b_frame_structure + 1 ) +
851 p_vpar->picture.b_repeat_first_field;
852 DumpBits( &p_vpar->bit_stream, 34 * nb );
856 /*****************************************************************************
857 * PictureSpatialScalableExtension *
858 *****************************************************************************/
860 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
862 /* That's scalable, so we trash it */
863 DumpBits32( &p_vpar->bit_stream );
864 DumpBits( &p_vpar->bit_stream, 14 );
868 /*****************************************************************************
869 * PictureTemporalScalableExtension *
870 *****************************************************************************/
872 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
874 /* Scalable again, trashed again */
875 DumpBits( &p_vpar->bit_stream, 23 );
879 /*****************************************************************************
880 * CopyrightExtension : Keeps some legal informations *
881 *****************************************************************************/
883 static void CopyrightExtension( vpar_thread_t * p_vpar )
885 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
886 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
887 /* A flag that says whether the copyright information is significant */
888 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
889 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
890 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
892 DumpBits( &p_vpar->bit_stream, 8 );
893 /* The copyright_number is split in three parts */
895 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
896 DumpBits( &p_vpar->bit_stream, 1 );
898 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
899 DumpBits( &p_vpar->bit_stream, 1 );
900 /* third part and sum */
901 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
902 ( (u64)i_copyright_nb_2 << 22 ) +
903 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
907 /*****************************************************************************
908 * LoadMatrix : Load a quantization matrix
909 *****************************************************************************/
910 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
914 if( !p_matrix->b_allocated )
916 /* Allocate a piece of memory to load the matrix. */
917 p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
918 p_matrix->b_allocated = 1;
921 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
923 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
924 = GetBits( &p_vpar->bit_stream, 8 );
928 /* Discrete Fourier Transform requires the quantization matrices to
929 * be normalized before using them. */
930 vdec_NormQuantMatrix( p_matrix->pi_matrix );
934 /*****************************************************************************
935 * LinkMatrix : Link a quantization matrix to another
936 *****************************************************************************/
937 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
941 if( p_matrix->b_allocated )
943 /* Deallocate the piece of memory. */
944 free( p_matrix->pi_matrix );
945 p_matrix->b_allocated = 0;
948 p_matrix->pi_matrix = pi_array;