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 P_picture.i_deccount = p_vpar->sequence.i_mb_size;
547 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
549 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
552 /* Prepare context for the next picture. */
558 /*****************************************************************************
559 * ReferenceUpdate : Update the reference pointers when we have a new picture
560 *****************************************************************************/
561 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
563 picture_t * p_newref )
565 if( i_coding_type != B_CODING_TYPE )
567 if( p_vpar->sequence.p_forward != NULL )
568 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
569 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
570 p_vpar->sequence.p_backward = p_newref;
571 if( p_newref != NULL )
572 vout_LinkPicture( p_vpar->p_vout, p_newref );
576 /*****************************************************************************
577 * ReferenceReplace : Replace the last reference pointer when we destroy
579 *****************************************************************************/
580 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
582 picture_t * p_newref )
584 if( i_coding_type != B_CODING_TYPE )
586 if( p_vpar->sequence.p_backward != NULL )
587 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
588 p_vpar->sequence.p_backward = p_newref;
589 if( p_newref != NULL )
590 vout_LinkPicture( p_vpar->p_vout, p_newref );
594 /*****************************************************************************
595 * SliceHeaderXY : Parse the next slice structure
596 *****************************************************************************
597 * X = i_height > 2800 ?
598 * Y = scalable_mode == SC_DP ?
599 *****************************************************************************/
600 static void SliceHeader00( vpar_thread_t * p_vpar,
601 int * pi_mb_address, int i_mb_base,
604 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
607 static void SliceHeader01( vpar_thread_t * p_vpar,
608 int * pi_mb_address, int i_mb_base,
611 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
612 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
615 static void SliceHeader10( vpar_thread_t * p_vpar,
616 int * pi_mb_address, int i_mb_base,
619 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
620 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
623 static void SliceHeader11( vpar_thread_t * p_vpar,
624 int * pi_mb_address, int i_mb_base,
627 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
628 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
629 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
632 /*****************************************************************************
633 * SliceHeader : Parse the next slice structure
634 *****************************************************************************/
635 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
636 int * pi_mb_address, int i_mb_base,
639 /* DC predictors initialization table */
640 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
642 int i_mb_address_save = *pi_mb_address;
644 /* slice_vertical_position_extension and priority_breakpoint already done */
645 LoadQuantizerScale( p_vpar );
647 if( GetBits( &p_vpar->bit_stream, 1 ) )
649 /* intra_slice, slice_id */
650 DumpBits( &p_vpar->bit_stream, 8 );
651 /* extra_information_slice */
652 while( GetBits( &p_vpar->bit_stream, 1 ) )
654 DumpBits( &p_vpar->bit_stream, 8 );
658 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
660 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
661 * does the reference decoder put 0 instead of the normative values ? */
662 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
663 = p_vpar->slice.pi_dc_dct_pred[2]
664 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
666 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
667 bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
671 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
673 i_mb_address_save = *pi_mb_address;
675 while( !ShowBits( &p_vpar->bit_stream, 23 ) );
678 /*****************************************************************************
679 * ExtensionAndUserData : Parse the extension_and_user_data structure
680 *****************************************************************************/
681 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
683 while( !p_vpar->b_die )
685 NextStartCode( p_vpar );
686 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
688 case EXTENSION_START_CODE:
689 DumpBits32( &p_vpar->bit_stream );
690 switch( GetBits( &p_vpar->bit_stream, 4 ) )
692 case SEQUENCE_DISPLAY_EXTENSION_ID:
693 SequenceDisplayExtension( p_vpar );
695 case QUANT_MATRIX_EXTENSION_ID:
696 QuantMatrixExtension( p_vpar );
698 case SEQUENCE_SCALABLE_EXTENSION_ID:
699 SequenceScalableExtension( p_vpar );
701 case PICTURE_DISPLAY_EXTENSION_ID:
702 PictureDisplayExtension( p_vpar );
704 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
705 PictureSpatialScalableExtension( p_vpar );
707 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
708 PictureTemporalScalableExtension( p_vpar );
710 case COPYRIGHT_EXTENSION_ID:
711 CopyrightExtension( p_vpar );
717 case USER_DATA_START_CODE:
718 DumpBits32( &p_vpar->bit_stream );
719 /* Wait for the next start code */
729 /*****************************************************************************
730 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
731 *****************************************************************************/
733 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
735 /* We don't care sequence_display_extension. */
737 DumpBits( &p_vpar->bit_stream, 3 );
738 if( GetBits( &p_vpar->bit_stream, 1 ) )
740 /* Three bytes for color_desciption */
741 DumpBits( &p_vpar->bit_stream, 24 );
743 /* display_horizontal and vertical_size and a marker_bit */
744 DumpBits( &p_vpar->bit_stream, 29 );
748 /*****************************************************************************
749 * QuantMatrixExtension : Load quantization matrices for luminance *
751 *****************************************************************************/
753 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
755 if( GetBits( &p_vpar->bit_stream, 1 ) )
757 /* Load intra_quantiser_matrix for luminance. */
758 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
762 /* Use the default matrix. */
763 LinkMatrix( &p_vpar->sequence.intra_quant,
764 pi_default_intra_quant );
766 if( GetBits( &p_vpar->bit_stream, 1 ) )
768 /* Load non_intra_quantiser_matrix for luminance. */
769 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
773 /* Use the default matrix. */
774 LinkMatrix( &p_vpar->sequence.nonintra_quant,
775 pi_default_nonintra_quant );
777 if( GetBits( &p_vpar->bit_stream, 1 ) )
779 /* Load intra_quantiser_matrix for chrominance. */
780 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
784 /* Link the chrominance intra matrix to the luminance one. */
785 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
786 p_vpar->sequence.intra_quant.pi_matrix );
788 if( GetBits( &p_vpar->bit_stream, 1 ) )
790 /* Load non_intra_quantiser_matrix for chrominance. */
791 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
795 /* Link the chrominance intra matrix to the luminance one. */
796 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
797 p_vpar->sequence.intra_quant.pi_matrix );
799 if( GetBits( &p_vpar->bit_stream, 1 ) )
801 /* Load non_intra_quantiser_matrix for chrominance. */
802 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
806 /* Link the chrominance nonintra matrix to the luminance one. */
807 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
808 p_vpar->sequence.nonintra_quant.pi_matrix );
813 /*****************************************************************************
814 * SequenceScalableExtension : Parse the sequence_scalable_extension *
815 * structure to handle scalable coding *
816 *****************************************************************************/
818 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
820 /* We don't care about anything scalable except the scalable mode. */
821 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
822 /* The length of the structure depends on the value of the scalable_mode */
825 DumpBits32( &p_vpar->bit_stream );
826 DumpBits( &p_vpar->bit_stream, 21 );
829 DumpBits( &p_vpar->bit_stream, 12 );
832 DumpBits( &p_vpar->bit_stream, 4 );
836 /*****************************************************************************
837 * PictureDisplayExtension : Parse the picture_display_extension structure *
838 *****************************************************************************/
840 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
842 /* Number of frame center offset */
844 /* I am not sure it works but it should
845 (fewer tests than shown in ยง6.3.12) */
846 nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
847 p_vpar->picture.b_repeat_first_field +
848 p_vpar->picture.b_top_field_first
849 : ( p_vpar->picture.b_frame_structure + 1 ) +
850 p_vpar->picture.b_repeat_first_field;
851 DumpBits( &p_vpar->bit_stream, 34 * nb );
855 /*****************************************************************************
856 * PictureSpatialScalableExtension *
857 *****************************************************************************/
859 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
861 /* That's scalable, so we trash it */
862 DumpBits32( &p_vpar->bit_stream );
863 DumpBits( &p_vpar->bit_stream, 14 );
867 /*****************************************************************************
868 * PictureTemporalScalableExtension *
869 *****************************************************************************/
871 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
873 /* Scalable again, trashed again */
874 DumpBits( &p_vpar->bit_stream, 23 );
878 /*****************************************************************************
879 * CopyrightExtension : Keeps some legal informations *
880 *****************************************************************************/
882 static void CopyrightExtension( vpar_thread_t * p_vpar )
884 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
885 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
886 /* A flag that says whether the copyright information is significant */
887 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
888 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
889 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
891 DumpBits( &p_vpar->bit_stream, 8 );
892 /* The copyright_number is split in three parts */
894 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
895 DumpBits( &p_vpar->bit_stream, 1 );
897 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
898 DumpBits( &p_vpar->bit_stream, 1 );
899 /* third part and sum */
900 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
901 ( (u64)i_copyright_nb_2 << 22 ) +
902 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );
906 /*****************************************************************************
907 * LoadMatrix : Load a quantization matrix
908 *****************************************************************************/
909 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
913 if( !p_matrix->b_allocated )
915 /* Allocate a piece of memory to load the matrix. */
916 p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
917 p_matrix->b_allocated = 1;
920 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
922 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
923 = GetBits( &p_vpar->bit_stream, 8 );
927 /* Discrete Fourier Transform requires the quantization matrices to
928 * be normalized before using them. */
929 vdec_NormQuantMatrix( p_matrix->pi_matrix );
933 /*****************************************************************************
934 * LinkMatrix : Link a quantization matrix to another
935 *****************************************************************************/
936 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
940 if( p_matrix->b_allocated )
942 /* Deallocate the piece of memory. */
943 free( p_matrix->pi_matrix );
944 p_matrix->b_allocated = 0;
947 p_matrix->pi_matrix = pi_array;