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"
31 #include "vpar_blocks.h"
32 #include "video_parser.h"
34 #include "macroblock.h"
35 #include "video_fifo.h"
36 #include "video_decoder.h"
41 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
42 static void GroupHeader( vpar_thread_t * p_vpar )
43 static void PictureHeader( vpar_thread_t * p_vpar )
44 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
46 picture_t * p_newref )
47 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
49 picture_t * p_newref )
50 static void SliceHeader00( vpar_thread_t * p_vpar,
51 int * pi_mb_address, int i_mb_base,
52 elem_t * p_y, p_u, p_v, u32 i_vert_code )
53 static void SliceHeader01( vpar_thread_t * p_vpar,
54 int * pi_mb_address, int i_mb_base,
55 elem_t * p_y, p_u, p_v, u32 i_vert_code )
56 static void SliceHeader10( vpar_thread_t * p_vpar,
57 int * pi_mb_address, int i_mb_base,
58 elem_t * p_y, p_u, p_v, u32 i_vert_code )
59 static void SliceHeader11( vpar_thread_t * p_vpar,
60 int * pi_mb_address, int i_mb_base,
61 elem_t * p_y, p_u, p_v, u32 i_vert_code )
62 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
63 int * pi_mb_address, int i_mb_base,
64 elem_t * p_y, p_u, p_v, u32 i_vert_code )
65 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
67 /*****************************************************************************
68 * vpar_NextSequenceHeader : Find the next sequence header
69 *****************************************************************************/
70 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
72 while( !p_vpar->b_die )
74 NextStartCode( p_vpar );
75 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_START_CODE )
81 /*****************************************************************************
82 * vpar_ParseHeader : Parse the next header
83 *****************************************************************************/
84 int vpar_ParseHeader( vpar_thread_t * p_vpar )
86 while( !p_vpar->b_die )
88 NextStartCode( p_vpar );
89 switch( GetBits32( &p_vpar->bit_stream ) )
91 case SEQUENCE_HEADER_CODE:
92 SequenceHeader( p_vpar );
96 case GROUP_START_CODE:
97 GroupHeader( p_vpar );
101 case PICTURE_START_CODE:
102 PictureHeader( p_vpar );
106 case SEQUENCE_END_CODE:
118 * Following functions are local
121 /*****************************************************************************
122 * NextStartCode : Find the next start code
123 *****************************************************************************/
124 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
126 /* Re-align the buffer on an 8-bit boundary */
127 RealignBits( &p_vpar->bit_stream );
129 while( ShowBits( &p_vpar->bit_stream, 24 ) != 0x01L && !p_vpar->b_die )
131 DumpBits( &p_vpar->bit_stream, 8 );
135 /*****************************************************************************
136 * SequenceHeader : Parse the next sequence header
137 *****************************************************************************/
138 static void SequenceHeader( vpar_thread_t * p_vpar )
141 static double d_frame_rate_table[16] =
144 ((23.0*1000.0)/1001.0),
147 ((30.0*1000.0)/1001.0),
150 ((60.0*1000.0)/1001.0),
152 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
156 int i_height_save, i_width_save;
158 i_height_save = p_vpar->sequence.i_height;
159 i_width_save = p_vpar->sequence.i_width;
161 p_vpar->sequence.i_height = GetBits( p_vpar->bit_stream, 12 );
162 p_vpar->sequence.i_width = GetBits( p_vpar->bit_stream, 12 );
163 p_vpar->sequence.i_ratio = GetBits( p_vpar->bit_stream, 4 );
164 p_vpar->sequence.d_frame_rate =
165 d_frame_rate_table( GetBits( p_vpar->bit_stream, 4 ) );
167 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
168 * constrained_parameters_flag */
169 DumpBits( p_vpar->bit_stream, 30 );
172 * Quantization matrices
174 if( GetBits( p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
176 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
180 /* Use default matrix. */
181 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
184 if( GetBits( p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
186 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
190 /* Use default matrix. */
191 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
194 /* Unless later overwritten by a matrix extension, we have the same
195 * matrices for luminance and chrominance. */
196 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
197 p_vpar->sequence.intra_quant.pi_matrix );
198 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
199 p_vpar->sequence.nonintra_quant.pi_matrix );
204 NextStartCode( p_vpar );
205 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
208 static int pi_chroma_nb_blocks[4] = {0, 1, 2, 4};
209 static (void *) ppf_chroma_pattern[4]( vpar_thread_t * ) =
210 {NULL, vpar_CodedPattern420,
211 vpar_CodedPattern422, vpar_CodedPattern444};
213 /* Parse sequence_extension */
214 DumpBits32( &p_vpar->bit_stream );
215 /* extension_start_code_identifier, profile_and_level_indication */
216 DumpBits( &p_vpar->bit_stream, 12 );
217 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
218 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
219 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width
220 >> (3-p_vpar->sequence.i_chroma_format);
221 p_vpar->sequence.i_chroma_nb_blocks = pi_chroma_nb_blocks
222 [p_vpar->sequence.i_chroma_format];
223 p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
224 [p_vpar->sequence.i_chroma_format];
225 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
226 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
227 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
228 DumpBits( &p_vpar->bit_stream, 22 );
229 /* frame_rate_extension_n */
230 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
231 /* frame_rate_extension_d */
232 p_vpar->sequence.d_frame_rate *= (i_dummy + 1)
233 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
235 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
239 /* It's an MPEG-1 stream. Put adequate parameters. */
240 p_vpar->sequence.b_progressive = 1;
241 p_vpar->sequence.i_chroma_format = CHROMA_420;
242 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i->width >> 2;
243 p_vpar->sequence.i_chroma_nb_blocks = 2;
244 p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
246 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
249 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
250 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
251 (p_vpar->sequence.i_height + 15) / 16 :
252 2 * (p_vpar->sequence.i_height + 31) / 32;
253 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
254 * p_vpar->sequence.i_mb_height;
255 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
256 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
257 p_vpar->sequence.i_size = p_vpar->sequence.i_width
258 * p_vpar->sequence.i_height;
260 /* Slice Header functions */
261 if( p_vpar->sequence.i_height <= 2800 )
263 if( p_vpar->sequence.i_scalable_mode != SC_DP )
265 p_vpar->sequence.pf_slice_header = SliceHeader00;
269 p_vpar->sequence.pf_slice_header = SliceHeader01;
274 if( p_vpar->sequence.i_scalable_mode != SC_DP )
276 p_vpar->sequence.pf_slice_header = SliceHeader10;
280 p_vpar->sequence.pf_slice_header = SliceHeader11;
284 if( p_vpar->sequence.i_width != i_width_save
285 || p_vpar->sequence.i_height != i_height_save
286 || p_vpar->sequence.p_frame_lum_lookup == NULL )
291 /* Extension and User data */
292 ExtensionAndUserData( p_vpar );
295 /*****************************************************************************
296 * GroupHeader : Parse the next group of pictures header
297 *****************************************************************************/
298 static void GroupHeader( vpar_thread_t * p_vpar )
300 /* Nothing to do, we don't care. */
301 DumpBits( &p_vpar->bit_stream, 27 );
302 ExtensionAndUserData( p_vpar );
305 /*****************************************************************************
306 * PictureHeader : Parse the next picture header
307 *****************************************************************************/
308 static void PictureHeader( vpar_thread_t * p_vpar )
310 static (int *) ppf_macroblock_type[4] = {vpar_IMBType, vpar_PMBType,
311 vpar_BMBType, vpar_DMBType};
314 int i_mb_address, i_mb_base, i_mb;
315 elem_t * p_y, p_u, p_v;
316 boolean_t b_parsable;
319 DumpBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
320 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
321 p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
322 [p_vpar->picture.i_coding_type];
324 DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
326 p_vpar->picture.b_full_pel_forward_vector = GetBits( &p_vpar->bit_stream, 1 );
327 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
328 p_vpar->picture.b_full_pel_backward_vector = GetBits( &p_vpar->bit_stream, 1 );
329 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
331 /* extra_information_picture */
332 while( GetBits( &p_vpar->bit_stream, 1 ) )
334 DumpBits( &p_vpar->bit_stream, 8 );
338 * Picture Coding Extension
340 NextStartCode( p_vpar );
341 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
343 /* Parse picture_coding_extension */
344 DumpBits32( &p_vpar->bit_stream );
345 /* extension_start_code_identifier */
346 DumpBits( &p_vpar->bit_stream, 4 );
348 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
349 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
350 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
351 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
352 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
353 i_structure = GetBits( &p_vpar->bit_stream, 2 );
354 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
355 p_vpar->picture.b_frame_pred_frame_dct
356 = GetBits( &p_vpar->bit_stream, 1 );
357 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
358 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
359 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
360 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
361 p_vpar->picture.b_repeat_first_field = GetBits( &vpar->bit_stream, 1 );
362 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
363 * the length of the picture_display_extension structure.
364 * chroma_420_type (obsolete) */
365 DumpBits( &p_vpar->bit_stream, 1 );
366 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
368 /* composite_display_flag */
369 if( GetBits( &p_vpar->bit_stream, 1 ) )
371 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
372 * sub_carrier_phase */
373 DumpBits( &p_vpar->bit_stream, 20 );
378 /* MPEG-1 compatibility flags */
379 p_vpar->i_intra_dc_precision = 0; /* 8 bits */
380 i_structure = FRAME_STRUCTURE;
381 p_vpar->b_frame_pred_frame_dct = TRUE;
382 p_vpar->picture.b_concealment_mv = 0;
383 p_vpar->picture.b_q_scale_type = 0;
384 p_vpar->picture.b_intra_vlc_format = 0;
385 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
386 b_repeat_first_field = FALSE;
387 p_vpar->picture.b_progressive_frame = TRUE;
390 if( p_vpar->picture.i_current_structure &&
391 (i_structure == FRAME_STRUCTURE ||
392 i_structure == p_vpar->picture.i_current_structure) )
394 /* We don't have the second field of the buffered frame. */
395 if( p_pvar->picture.p_picture != NULL )
397 ReferenceReplace( p_vpar,
398 p_vpar->picture.p_second_field_buffer->i_coding_type,
401 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
403 vpar_DestroyMacroblock( &p_vpar->vfifo,
404 p_vpar->picture.pp_mb[i_mb] );
406 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
409 p_pvar->picture.i_current_structure = 0;
411 intf_DbgMsg("vpar debug: odd number of field picture.");
414 if( p_vpar->picture.i_current_structure )
416 /* Second field of a frame. We will decode it if, and only if we
417 * have decoded the first frame. */
418 b_parsable = (p_vpar->picture.p_second_field_buffer != NULL);
422 /* Do we have the reference pictures ? */
423 b_parsable = !((i_coding_type == P_CODING_TYPE) &&
424 (p_vpar->sequence.p_forward == NULL)) ||
425 ((i_coding_type == B_CODING_TYPE) &&
426 (p_vpar->sequence.p_forward == NULL ||
427 p_vpar->sequence.p_backward == NULL));
429 /* Does synchro say we have enough time to decode it ? */
430 b_parsable &&= vpar_SynchroChoose( p_vpar, i_coding_type, i_structure );
435 /* Update the reference pointers. */
436 ReferenceUpdate( p_vpar, i_coding_type, NULL );
438 /* Warn Synchro we have trashed a picture. */
439 vpar_SynchroTrash( p_vpar, i_coding_type, i_structure );
441 /* Update context. */
442 if( i_structure != FRAME_STRUCTURE )
443 p_vpar->picture.i_current_structure = i_structure;
444 p_vpar->picture.p_picture = NULL;
449 /* OK, now we are sure we will decode the picture. */
450 #define P_picture p_vpar->picture.p_picture
451 p_vpar->picture.b_error = FALSE;
453 if( !p_vpar->picture.i_current_structure )
455 /* This is a new frame. Get a structure from the video_output. */
456 P_picture = vout_CreatePicture( p_vpar->p_vout,
457 SPLITTED_YUV_PICTURE,
458 p_vpar->sequence.i_width,
459 p_vpar->sequence.i_height,
460 p_vpar->sequence.i_chroma_format );
462 /* Initialize values. */
463 P_picture->date = vpar_SynchroDecode( p_vpar, i_coding_type,
465 p_vpar->picture.i_lum_incr = - 8 + ( p_vpar->sequence.i_width
466 << ( i_structure != FRAME_STRUCTURE ) );
467 p_vpar->picture.i_chroma_incr = -8 + ( p_vpar->sequence.i_width
468 << (( i_structure != FRAME_STRUCTURE ) +
469 ( 3 - p_vpar->sequence.i_chroma_format )) );
471 /* Update the reference pointers. */
472 ReferenceUpdate( p_vpar, i_coding_type, p_picture );
474 p_vpar->picture.i_current_structure |= i_structure;
475 p_vpar->picture.i_structure = i_structure;
476 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
478 /* Initialize picture data for decoding. */
479 if( i_structure == BOTTOM_FIELD )
481 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
489 /* Extension and User data. */
490 ExtensionAndUserData( p_vpar );
492 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
493 NextStartCode( p_vpar );
494 while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
495 && !p_vpar->picture.b_error)
497 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
498 < SLICE_START_CODE_MIN) ||
499 (i_dummy > SLICE_START_CODE_MAX) )
501 intf_DbgMsg("vpar debug: premature end of picture");
502 p_vpar->picture.b_error = TRUE;
505 DumpBits32( &p_vpar->bit_stream );
507 /* Decode slice data. */
508 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
511 if( p_vpar->picture.b_error )
514 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
516 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
519 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
520 vout_DestroyPicture( p_vpar->p_vout, P_picture );
522 /* Prepare context for the next picture. */
525 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
527 /* Frame completely parsed. */
528 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
530 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
533 /* Prepare context for the next picture. */
539 /*****************************************************************************
540 * ReferenceUpdate : Update the reference pointers when we have a new picture
541 *****************************************************************************/
542 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
544 picture_t * p_newref )
546 if( i_coding_type != B_CODING_TYPE )
548 if( p_vpar->sequence.p_forward != NULL )
549 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
550 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
551 p_vpar->sequence.p_backward = p_newref;
552 if( p_newref != NULL )
553 vout_LinkPicture( p_vpar->p_vout, p_newref );
557 /*****************************************************************************
558 * ReferenceReplace : Replace the last reference pointer when we destroy
560 *****************************************************************************/
561 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
563 picture_t * p_newref )
565 if( i_coding_type != B_CODING_TYPE )
567 if( p_vpar->sequence.p_backward != NULL )
568 vout_UnlinkPicture( p_vpar->p_vout, 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 * SliceHeaderXY : Parse the next slice structure
577 *****************************************************************************
578 * X = i_height > 2800 ?
579 * Y = scalable_mode == SC_DP ?
580 *****************************************************************************/
581 static void SliceHeader00( vpar_thread_t * p_vpar,
582 int * pi_mb_address, int i_mb_base,
585 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
588 static void SliceHeader01( vpar_thread_t * p_vpar,
589 int * pi_mb_address, int i_mb_base,
592 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
593 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
596 static void SliceHeader10( vpar_thread_t * p_vpar,
597 int * pi_mb_address, int i_mb_base,
600 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
601 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
604 static void SliceHeader11( vpar_thread_t * p_vpar,
605 int * pi_mb_address, int i_mb_base,
608 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
609 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
610 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
613 /*****************************************************************************
614 * SliceHeader : Parse the next slice structure
615 *****************************************************************************/
616 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
617 int * pi_mb_address, int i_mb_base,
620 /* DC predictors initialization table */
621 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
623 int i_mb_address_save = *pi_mb_address;
625 /* slice_vertical_position_extension and priority_breakpoint already done */
626 LoadQuantizerScale( p_vpar );
628 if( GetBits( &p_vpar->bit_stream, 1 ) )
630 /* intra_slice, slice_id */
631 DumpBits( &p_vpar->bit_stream, 8 );
632 /* extra_information_slice */
633 while( GetBits( &p_vpar->bit_stream, 1 ) )
635 DumpBits( &p_vpar->bit_stream, 8 );
639 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
641 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
642 * does the reference decoder put 0 instead of the normative values ? */
643 p_vpar->slice.pi_dct_pred[0] = p_vpar->slice.pi_dct_pred[1]
644 = p_vpar->slice.pi_dct_pred[2]
645 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
647 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
648 bzero( p_vpar->slice.pppi_pmv, 8*sizeof(int) );
652 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
654 i_mb_address_save = *pi_mb_address;
656 while( !ShowBits( &p_vpar->bit_stream, 23 ) );
659 /*****************************************************************************
660 * ExtensionAndUserData : Parse the extension_and_user_data structure
661 *****************************************************************************/
662 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
664 while( !p_vpar->b_die )
666 NextStartCode( p_vpar );
667 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
669 case EXTENSION_START_CODE:
670 DumpBits32( &p_vpar->bit_stream );
671 switch( GetBits( &p_vpar->bit_stream, 4 ) )
673 case SEQUENCE_DISPLAY_EXTENSION_ID:
674 SequenceDisplayExtension( p_vpar );
676 case QUANT_MATRIX_EXTENSION_ID:
677 QuantMatrixExtension( p_vpar );
679 case SEQUENCE_SCALABLE_EXTENSION_ID:
680 SequenceScalableExtension( p_vpar );
682 case PICTURE_DISPLAY_EXTENSION_ID:
683 PictureDisplayExtension( p_vpar );
685 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
686 PictureSpatialScalableExtension( p_vpar );
688 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
689 PictureTemporalScalableExtension( p_vpar );
691 case COPYRIGHT_EXTENSION_ID:
692 CopyrightExtension( p_vpar );
698 case USER_DATA_START_CODE:
699 DumpBits32( &p_vpar->bit_stream );
700 /* Wait for the next start code */
710 /*****************************************************************************
711 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
712 *****************************************************************************/
714 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
716 /* We don't care sequence_display_extension. */
718 DumpBits( &p_vpar->bit_stream, 3 );
719 if( GetBits( &p_vpar->bit_stream, 1 ) )
721 /* Three bytes for color_desciption */
722 DumpBits( &p_vpar->bit_stream, 24 );
724 /* display_horizontal and vertical_size and a marker_bit */
725 DumpBits( &p_vpar->bit_stream, 29 );
729 /*****************************************************************************
730 * QuantMatrixExtension : Load quantization matrices for luminance *
732 *****************************************************************************/
734 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
736 if( GetBits( &p_vpar->bit_stream, 1 ) )
738 /* Load intra_quantiser_matrix for luminance. */
739 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
743 /* Use the default matrix. */
744 LinkMatrix( &p_vpar->sequence.intra_quant,
745 pi_default_intra_quant );
747 if( GetBits( &p_vpar->bit_stream, 1 ) )
749 /* Load non_intra_quantiser_matrix for luminance. */
750 LoadMatrix( p_vpar, &p_vpar->sequence.non_intra_quant );
754 /* Use the default matrix. */
755 LinkMatrix( &p_vpar->sequence.nonintra_quant,
756 pi_default_nonintra_quant );
758 if( GetBits( &p_vpar->bit_stream, 1 ) )
760 /* Load intra_quantiser_matrix for chrominance. */
761 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
765 /* Link the chrominance intra matrix to the luminance one. */
766 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
767 &p_vpar->sequence.intra_quant );
769 if( GetBits( &p_vpar->bit_stream, 1 ) )
771 /* Load non_intra_quantiser_matrix for chrominance. */
772 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
776 /* Link the chrominance intra matrix to the luminance one. */
777 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
778 &p_vpar->sequence.intra_quant );
780 if( GetBits( &p_vpar->bit_stream, 1 ) )
782 /* Load non_intra_quantiser_matrix for chrominance. */
783 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
787 /* Link the chrominance nonintra matrix to the luminance one. */
788 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
789 &p_vpar->sequence.nonintra_quant );
794 /*****************************************************************************
795 * SequenceScalableExtension : Parse the sequence_scalable_extension *
796 * structure to handle scalable coding *
797 *****************************************************************************/
799 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
801 /* We don't care about anything scalable except the scalable mode. */
802 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
803 /* The length of the structure depends on the value of the scalable_mode */
806 DumpBits32( &p_vpar->bit_stream );
807 DumpBits( &p_vpar->bit_stream, 21 );
810 DumpBits( &p_vpar->bit_stream, 12 );
813 DumpBits( &p_vpar->bit_stream, 4 );
817 /*****************************************************************************
818 * PictureDisplayExtension : Parse the picture_display_extension structure *
819 *****************************************************************************/
821 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
823 /* Number of frame center offset */
825 /* I am not sure it works but it should
826 (fewer tests than shown in §6.3.12) */
827 nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
828 p_vpar->picture.b_repeat_first_field +
829 p_vpar->picture.b_top_field_first
830 : ( p_vpar->picture.b_frame_structure + 1 ) +
831 p_vpar->picture.b_repeat_first_field;
832 DumpBits( &p_vpar->bit_stream, 34 * nb );
836 /*****************************************************************************
837 * PictureSpatialScalableExtension *
838 *****************************************************************************/
840 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
842 /* That's scalable, so we trash it */
843 DumpBits32( &p_vpar->bit_stream );
844 DumpBits( &p_vpar->bit_stream, 14 );
848 /*****************************************************************************
849 * PictureTemporalScalableExtension *
850 *****************************************************************************/
852 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
854 /* Scalable again, trashed again */
855 DumpBits( &p_vpar->bit_stream, 23 );
859 /*****************************************************************************
860 * CopyrightExtension : Keeps some legal informations *
861 *****************************************************************************/
863 static void CopytrightExtension( vpar_thread_t * p_vpar )
865 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
866 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
867 /* A flag that says whether the copyright information is significant */
868 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
869 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
870 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
872 DumpBits( &p_vpar->bit_stream, 8 );
873 /* The copyright_number is split in three parts */
875 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
876 DumpBits( &p_vpar->bit_stream, 1 );
878 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
879 DumpBits( &p_vpar->bit_stream, 1 );
880 /* third part and sum */
881 p_vpar->sequence.i_copyright_nb = ( i_copyright_nb_1 << 44 ) +
882 ( i_copyright_nb_2 << 22 ) +
883 ( GetBits( &p_vpar->bit_stream, 22 ) );
887 /*****************************************************************************
888 * LoadMatrix : Load a quantization matrix
889 *****************************************************************************/
890 static void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
894 if( !p_matrix->b_allocated )
896 /* Allocate a piece of memory to load the matrix. */
897 p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
898 p_matrix->b_allocated = TRUE;
901 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
903 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
904 = GetBits( p_vpar->bit_stream, 8 );
908 /* Discrete Fourier Transform requires the quantization matrices to
909 * be normalized before using them. */
910 vdec_NormQuantMatrix( p_matrix->pi_matrix );
914 /*****************************************************************************
915 * LinkMatrix : Link a quantization matrix to another
916 *****************************************************************************/
917 static void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
921 if( p_matrix->b_allocated )
923 /* Deallocate the piece of memory. */
924 free( p_matrix->pi_matrix );
925 p_matrix->b_allocated = FALSE;
928 p_matrix->pi_matrix = pi_array;