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"
41 #include "vpar_motion.h"
46 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
47 static void SequenceHeader( vpar_thread_t * p_vpar );
48 static void GroupHeader( vpar_thread_t * p_vpar );
49 static void PictureHeader( vpar_thread_t * p_vpar );
50 static void SliceHeader00( vpar_thread_t * p_vpar,
51 int * pi_mb_address, int i_mb_base,
53 static void SliceHeader01( vpar_thread_t * p_vpar,
54 int * pi_mb_address, int i_mb_base,
56 static void SliceHeader10( vpar_thread_t * p_vpar,
57 int * pi_mb_address, int i_mb_base,
59 static void SliceHeader11( vpar_thread_t * p_vpar,
60 int * pi_mb_address, int i_mb_base,
62 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
63 int * pi_mb_address, int i_mb_base,
65 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
66 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
67 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
68 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
69 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
70 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
71 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
72 static void CopyrightExtension( vpar_thread_t * p_vpar );
78 /*****************************************************************************
79 * pi_default_intra_quant : default quantization matrix
80 *****************************************************************************/
82 int pi_default_intra_quant[] =
84 8, 16, 19, 22, 26, 27, 29, 34,
85 16, 16, 22, 24, 27, 29, 34, 37,
86 19, 22, 26, 27, 29, 34, 34, 38,
87 22, 22, 26, 27, 29, 34, 37, 40,
88 22, 26, 27, 29, 32, 35, 40, 48,
89 26, 27, 29, 32, 35, 40, 48, 58,
90 26, 27, 29, 34, 38, 46, 56, 69,
91 27, 29, 35, 38, 46, 56, 69, 83
94 int pi_default_intra_quant[] =
96 2048, 5681, 6355, 6623, 6656, 5431, 4018, 2401,
97 5681, 7880, 10207, 10021, 9587, 8091, 6534, 3625,
98 6355, 10207, 11363, 10619, 9700, 8935, 6155, 3507,
99 6623, 9186, 10226, 9557, 8730, 8041, 6028, 3322,
100 5632, 9232, 9031, 8730, 8192, 7040, 5542, 3390,
101 5230, 7533, 7621, 7568, 7040, 6321, 5225, 3219,
102 3602, 5189, 5250, 5539, 5265, 5007, 4199, 2638,
103 1907, 2841, 3230, 3156, 3249, 3108, 2638, 1617
107 /*****************************************************************************
108 * pi_default_nonintra_quant : default quantization matrix
109 *****************************************************************************/
111 int pi_default_nonintra_quant[] =
113 16, 16, 16, 16, 16, 16, 16, 16,
114 16, 16, 16, 16, 16, 16, 16, 16,
115 16, 16, 16, 16, 16, 16, 16, 16,
116 16, 16, 16, 16, 16, 16, 16, 16,
117 16, 16, 16, 16, 16, 16, 16, 16,
118 16, 16, 16, 16, 16, 16, 16, 16,
119 16, 16, 16, 16, 16, 16, 16, 16,
120 16, 16, 16, 16, 16, 16, 16, 16
123 int pi_default_nonintra_quanit[] =
125 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
126 5680, 7888, 7424, 6688, 5680, 4464, 3072, 1568,
127 5344, 7424, 6992, 6288, 5344, 4208, 2896, 1472,
128 4816, 6688, 6288, 5664, 4816, 3792, 2608, 1328,
129 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
130 3216, 4464, 4208, 3792, 3216, 2528, 1744, 880,
131 2224, 3072, 2896, 2608, 2224, 1744, 1200, 608,
132 1136, 1568, 1472, 1328, 1136, 880, 608, 304
136 /*****************************************************************************
137 * pi_scan : zig-zag and alternate scan patterns
138 *****************************************************************************/
141 { /* Zig-Zag pattern */
142 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
143 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
144 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
145 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
147 { /* Alternate scan pattern */
148 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
149 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
150 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
151 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
156 * Local inline functions.
159 /*****************************************************************************
160 * NextStartCode : Find the next start code
161 *****************************************************************************/
162 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
164 /* Re-align the buffer on an 8-bit boundary */
165 RealignBits( &p_vpar->bit_stream );
167 while( ShowBits( &p_vpar->bit_stream, 16 ) != 0 && !p_vpar->b_die )
169 DumpBits( &p_vpar->bit_stream, 8 );
171 DumpBits( &p_vpar->bit_stream, 16 );
174 /*****************************************************************************
175 * ReferenceUpdate : Update the reference pointers when we have a new picture
176 *****************************************************************************/
177 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
179 picture_t * p_newref )
181 if( i_coding_type != B_CODING_TYPE )
183 if( p_vpar->sequence.p_forward != NULL )
184 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
185 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
186 p_vpar->sequence.p_backward = p_newref;
187 if( p_newref != NULL )
188 vout_LinkPicture( p_vpar->p_vout, p_newref );
192 /*****************************************************************************
193 * ReferenceReplace : Replace the last reference pointer when we destroy
195 *****************************************************************************/
196 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
198 picture_t * p_newref )
200 if( i_coding_type != B_CODING_TYPE )
202 if( p_vpar->sequence.p_backward != NULL )
203 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
204 p_vpar->sequence.p_backward = p_newref;
205 if( p_newref != NULL )
206 vout_LinkPicture( p_vpar->p_vout, p_newref );
210 /*****************************************************************************
211 * LoadMatrix : Load a quantization matrix
212 *****************************************************************************/
213 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
217 if( !p_matrix->b_allocated )
219 /* Allocate a piece of memory to load the matrix. */
220 p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
221 p_matrix->b_allocated = 1;
224 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
226 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
227 = GetBits( &p_vpar->bit_stream, 8 );
231 /* Discrete Fourier Transform requires the quantization matrices to
232 * be normalized before using them. */
233 vdec_NormQuantMatrix( p_matrix->pi_matrix );
237 /*****************************************************************************
238 * LinkMatrix : Link a quantization matrix to another
239 *****************************************************************************/
240 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
244 if( p_matrix->b_allocated )
246 /* Deallocate the piece of memory. */
247 free( p_matrix->pi_matrix );
248 p_matrix->b_allocated = 0;
251 p_matrix->pi_matrix = pi_array;
255 * Exported functions.
258 /*****************************************************************************
259 * vpar_NextSequenceHeader : Find the next sequence header
260 *****************************************************************************/
261 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
263 while( !p_vpar->b_die )
265 NextStartCode( p_vpar );
266 if( ShowBits( &p_vpar->bit_stream, 16 ) == SEQUENCE_HEADER_CODE )
272 /*****************************************************************************
273 * vpar_ParseHeader : Parse the next header
274 *****************************************************************************/
275 int vpar_ParseHeader( vpar_thread_t * p_vpar )
277 while( !p_vpar->b_die )
279 NextStartCode( p_vpar );
280 switch( GetBits32( &p_vpar->bit_stream ) )
282 case SEQUENCE_HEADER_CODE:
283 fprintf( stderr, "begin sequence header\n" );
284 SequenceHeader( p_vpar );
285 fprintf( stderr, "end sequence header\n" );
289 case GROUP_START_CODE:
290 fprintf( stderr, "begin group\n" );
291 GroupHeader( p_vpar );
292 fprintf( stderr, "end group\n" );
296 case PICTURE_START_CODE:
297 fprintf( stderr, "begin picture\n" );
298 PictureHeader( p_vpar );
299 fprintf( stderr, "end picture\n" );
303 case SEQUENCE_END_CODE:
304 fprintf( stderr, "sequence header end code\n" );
316 * Following functions are local
319 /*****************************************************************************
320 * SequenceHeader : Parse the next sequence header
321 *****************************************************************************/
322 static void SequenceHeader( vpar_thread_t * p_vpar )
325 static double d_frame_rate_table[16] =
328 ((23.0*1000.0)/1001.0),
331 ((30.0*1000.0)/1001.0),
334 ((60.0*1000.0)/1001.0),
336 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
340 int i_height_save, i_width_save;
342 i_height_save = p_vpar->sequence.i_height;
343 i_width_save = p_vpar->sequence.i_width;
345 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
346 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
347 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
348 p_vpar->sequence.d_frame_rate =
349 d_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
351 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
352 * constrained_parameters_flag */
353 DumpBits( &p_vpar->bit_stream, 30 );
356 * Quantization matrices
358 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
360 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
364 /* Use default matrix. */
365 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
368 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
370 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
374 /* Use default matrix. */
375 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
378 /* Unless later overwritten by a matrix extension, we have the same
379 * matrices for luminance and chrominance. */
380 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
381 p_vpar->sequence.intra_quant.pi_matrix );
382 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
383 p_vpar->sequence.nonintra_quant.pi_matrix );
388 NextStartCode( p_vpar );
389 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
392 static f_chroma_pattern_t ppf_chroma_pattern[4] =
393 {NULL, vpar_CodedPattern420,
394 vpar_CodedPattern422, vpar_CodedPattern444};
396 /* Parse sequence_extension */
397 DumpBits32( &p_vpar->bit_stream );
398 /* extension_start_code_identifier, profile_and_level_indication */
399 DumpBits( &p_vpar->bit_stream, 12 );
400 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
401 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
402 p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
403 [p_vpar->sequence.i_chroma_format];
404 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
405 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
406 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
407 DumpBits( &p_vpar->bit_stream, 22 );
408 /* frame_rate_extension_n */
409 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
410 /* frame_rate_extension_d */
411 p_vpar->sequence.d_frame_rate *= (i_dummy + 1)
412 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
414 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
418 /* It's an MPEG-1 stream. Put adequate parameters. */
419 p_vpar->sequence.b_progressive = 1;
420 p_vpar->sequence.i_chroma_format = CHROMA_420;
421 p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
423 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
427 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
428 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
429 (p_vpar->sequence.i_height + 15) / 16 :
430 2 * (p_vpar->sequence.i_height + 31) / 32;
431 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
432 * p_vpar->sequence.i_mb_height;
433 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
434 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
435 p_vpar->sequence.i_size = p_vpar->sequence.i_width
436 * p_vpar->sequence.i_height;
438 /* Update chromatic information */
439 switch( p_vpar->sequence.i_chroma_format )
442 p_vpar->sequence.i_chroma_nb_blocks = 2;
443 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 2;
444 p_vpar->sequence.i_chroma_mb_width = 8;
445 p_vpar->sequence.i_chroma_mb_height = 8;
449 p_vpar->sequence.i_chroma_nb_blocks = 4;
450 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
451 p_vpar->sequence.i_chroma_mb_width = 8;
452 p_vpar->sequence.i_chroma_mb_height = 16;
456 p_vpar->sequence.i_chroma_nb_blocks = 8;
457 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
458 p_vpar->sequence.i_chroma_mb_width = 16;
459 p_vpar->sequence.i_chroma_mb_height = 16;
462 /* Slice Header functions */
463 if( p_vpar->sequence.i_height <= 2800 )
465 if( p_vpar->sequence.i_scalable_mode != SC_DP )
467 p_vpar->sequence.pf_slice_header = SliceHeader00;
471 p_vpar->sequence.pf_slice_header = SliceHeader01;
476 if( p_vpar->sequence.i_scalable_mode != SC_DP )
478 p_vpar->sequence.pf_slice_header = SliceHeader10;
482 p_vpar->sequence.pf_slice_header = SliceHeader11;
486 if( p_vpar->sequence.i_width != i_width_save
487 || p_vpar->sequence.i_height != i_height_save )
489 /* What do we do in case of a size change ??? */
492 /* Extension and User data */
493 ExtensionAndUserData( p_vpar );
496 /*****************************************************************************
497 * GroupHeader : Parse the next group of pictures header
498 *****************************************************************************/
499 static void GroupHeader( vpar_thread_t * p_vpar )
501 /* Nothing to do, we don't care. */
502 DumpBits( &p_vpar->bit_stream, 27 );
503 ExtensionAndUserData( p_vpar );
506 /*****************************************************************************
507 * PictureHeader : Parse the next picture header
508 *****************************************************************************/
509 static void PictureHeader( vpar_thread_t * p_vpar )
511 static f_macroblock_type_t ppf_macroblock_type[4] =
512 {vpar_IMBType, vpar_PMBType,
513 vpar_BMBType, vpar_DMBType};
516 int i_mb_address, i_mb_base, i_mb;
517 elem_t * p_y, p_u, p_v;
518 boolean_t b_parsable;
521 DumpBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
522 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
523 p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
524 [p_vpar->picture.i_coding_type];
526 DumpBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
528 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
529 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
530 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
531 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
533 /* extra_information_picture */
534 while( GetBits( &p_vpar->bit_stream, 1 ) )
536 DumpBits( &p_vpar->bit_stream, 8 );
540 * Picture Coding Extension
542 fprintf( stderr, "picture1\n" );
543 NextStartCode( p_vpar );
544 if( ShowBits( &p_vpar->bit_stream, 16 ) == EXTENSION_START_CODE )
546 /* Parse picture_coding_extension */
547 DumpBits32( &p_vpar->bit_stream );
548 /* extension_start_code_identifier */
549 DumpBits( &p_vpar->bit_stream, 4 );
551 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
552 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
553 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
554 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
555 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
556 i_structure = GetBits( &p_vpar->bit_stream, 2 );
557 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
558 p_vpar->picture.b_frame_pred_frame_dct
559 = GetBits( &p_vpar->bit_stream, 1 );
560 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
561 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
562 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
563 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
564 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
565 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
566 * the length of the picture_display_extension structure.
567 * chroma_420_type (obsolete) */
568 DumpBits( &p_vpar->bit_stream, 1 );
569 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
571 /* composite_display_flag */
572 if( GetBits( &p_vpar->bit_stream, 1 ) )
574 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
575 * sub_carrier_phase */
576 DumpBits( &p_vpar->bit_stream, 20 );
581 /* MPEG-1 compatibility flags */
582 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
583 i_structure = FRAME_STRUCTURE;
584 p_vpar->picture.b_frame_pred_frame_dct = 1;
585 p_vpar->picture.b_concealment_mv = 0;
586 p_vpar->picture.b_q_scale_type = 0;
587 p_vpar->picture.b_intra_vlc_format = 0;
588 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
589 p_vpar->picture.b_repeat_first_field = 0;
590 p_vpar->picture.b_progressive_frame = 1;
593 if( p_vpar->picture.i_current_structure &&
594 (i_structure == FRAME_STRUCTURE ||
595 i_structure == p_vpar->picture.i_current_structure) )
597 /* We don't have the second field of the buffered frame. */
598 if( p_vpar->picture.p_picture != NULL )
600 ReferenceReplace( p_vpar,
601 p_vpar->picture.i_coding_type,
604 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
606 vpar_DestroyMacroblock( &p_vpar->vfifo,
607 p_vpar->picture.pp_mb[i_mb] );
609 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
612 p_vpar->picture.i_current_structure = 0;
614 intf_DbgMsg("vpar debug: odd number of field picture.");
617 if( p_vpar->picture.i_current_structure )
619 /* Second field of a frame. We will decode it if, and only if we
620 * have decoded the first frame. */
621 b_parsable = (p_vpar->picture.p_picture != NULL);
625 /* Do we have the reference pictures ? */
626 b_parsable = !((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
627 (p_vpar->sequence.p_forward == NULL)) ||
628 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
629 (p_vpar->sequence.p_forward == NULL ||
630 p_vpar->sequence.p_backward == NULL));
634 /* Does synchro say we have enough time to decode it ? */
635 b_parsable = vpar_SynchroChoose( p_vpar,
636 p_vpar->picture.i_coding_type, i_structure );
642 /* Update the reference pointers. */
643 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
645 /* Warn Synchro we have trashed a picture. */
646 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
648 /* Update context. */
649 if( i_structure != FRAME_STRUCTURE )
650 p_vpar->picture.i_current_structure = i_structure;
651 p_vpar->picture.p_picture = NULL;
656 /* OK, now we are sure we will decode the picture. */
657 #define P_picture p_vpar->picture.p_picture
658 p_vpar->picture.b_error = 0;
660 if( !p_vpar->picture.i_current_structure )
662 /* This is a new frame. Get a structure from the video_output. */
663 P_picture = vout_CreatePicture( p_vpar->p_vout,
664 99+p_vpar->sequence.i_chroma_format, /*???*/
665 p_vpar->sequence.i_width,
666 p_vpar->sequence.i_height,
667 p_vpar->sequence.i_width*sizeof(yuv_data_t) );
669 /* Initialize values. */
670 P_picture->date = vpar_SynchroDecode( p_vpar,
671 p_vpar->picture.i_coding_type,
673 p_vpar->picture.i_l_stride = - 8 + ( p_vpar->sequence.i_width
674 << ( 1 - p_vpar->picture.b_frame_structure ) );
675 p_vpar->picture.i_c_stride = -8 + ( p_vpar->sequence.i_width
676 << (( 1 - p_vpar->picture.b_frame_structure ) +
677 ( 3 - p_vpar->sequence.i_chroma_format )) );
679 /* Update the reference pointers. */
680 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
682 p_vpar->picture.i_current_structure |= i_structure;
683 p_vpar->picture.i_structure = i_structure;
684 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
686 /* Initialize picture data for decoding. */
687 if( i_structure == BOTTOM_FIELD )
689 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
690 p_vpar->mb.i_l_y = 16;
691 p_vpar->mb.i_c_y = p_vpar->sequence.i_chroma_mb_height;
696 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
699 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
701 /* Extension and User data. */
702 ExtensionAndUserData( p_vpar );
704 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
705 NextStartCode( p_vpar );
706 while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
707 && !p_vpar->picture.b_error)
709 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
710 < SLICE_START_CODE_MIN) ||
711 (i_dummy > SLICE_START_CODE_MAX) )
713 intf_DbgMsg("vpar debug: premature end of picture");
714 p_vpar->picture.b_error = 1;
717 DumpBits32( &p_vpar->bit_stream );
719 /* Decode slice data. */
720 SliceHeader( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
723 /* Link referenced pictures for the decoder
724 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
725 if( p_vpar->sequence.p_forward != NULL )
727 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
729 if( p_vpar->sequence.p_backward != NULL )
731 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
734 if( p_vpar->picture.b_error )
737 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
739 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
742 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
743 vout_DestroyPicture( p_vpar->p_vout, P_picture );
745 /* Unlink referenced pictures */
746 if( p_vpar->sequence.p_forward != NULL )
748 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
750 if( p_vpar->sequence.p_backward != NULL )
752 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
755 /* Prepare context for the next picture. */
758 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
760 /* Frame completely parsed. */
761 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
762 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size; i_mb++ )
764 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
767 /* Prepare context for the next picture. */
773 /*****************************************************************************
774 * SliceHeader : Parse the next slice structure
775 *****************************************************************************/
776 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
777 int * pi_mb_address, int i_mb_base,
780 /* DC predictors initialization table */
781 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
783 int i_mb_address_save = *pi_mb_address;
785 /* slice_vertical_position_extension and priority_breakpoint already done */
786 LoadQuantizerScale( p_vpar );
788 if( GetBits( &p_vpar->bit_stream, 1 ) )
790 /* intra_slice, slice_id */
791 DumpBits( &p_vpar->bit_stream, 8 );
792 /* extra_information_slice */
793 while( GetBits( &p_vpar->bit_stream, 1 ) )
795 DumpBits( &p_vpar->bit_stream, 8 );
799 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
801 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
802 * does the reference decoder put 0 instead of the normative values ? */
803 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
804 = p_vpar->slice.pi_dc_dct_pred[2]
805 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
807 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
808 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
812 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
814 i_mb_address_save = *pi_mb_address;
816 while( !ShowBits( &p_vpar->bit_stream, 23 ) );
819 /*****************************************************************************
820 * SliceHeaderXY : Parse the next slice structure
821 *****************************************************************************
822 * X = i_height > 2800 ?
823 * Y = scalable_mode == SC_DP ?
824 *****************************************************************************/
825 static void SliceHeader00( vpar_thread_t * p_vpar,
826 int * pi_mb_address, int i_mb_base,
829 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
832 static void SliceHeader01( vpar_thread_t * p_vpar,
833 int * pi_mb_address, int i_mb_base,
836 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
837 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
840 static void SliceHeader10( vpar_thread_t * p_vpar,
841 int * pi_mb_address, int i_mb_base,
844 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
845 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
848 static void SliceHeader11( vpar_thread_t * p_vpar,
849 int * pi_mb_address, int i_mb_base,
852 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
853 DumpBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
854 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
857 /*****************************************************************************
858 * ExtensionAndUserData : Parse the extension_and_user_data structure
859 *****************************************************************************/
860 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
862 while( !p_vpar->b_die )
864 NextStartCode( p_vpar );
865 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
867 case EXTENSION_START_CODE:
868 DumpBits32( &p_vpar->bit_stream );
869 switch( GetBits( &p_vpar->bit_stream, 4 ) )
871 case SEQUENCE_DISPLAY_EXTENSION_ID:
872 SequenceDisplayExtension( p_vpar );
874 case QUANT_MATRIX_EXTENSION_ID:
875 QuantMatrixExtension( p_vpar );
877 case SEQUENCE_SCALABLE_EXTENSION_ID:
878 SequenceScalableExtension( p_vpar );
880 case PICTURE_DISPLAY_EXTENSION_ID:
881 PictureDisplayExtension( p_vpar );
883 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
884 PictureSpatialScalableExtension( p_vpar );
886 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
887 PictureTemporalScalableExtension( p_vpar );
889 case COPYRIGHT_EXTENSION_ID:
890 CopyrightExtension( p_vpar );
896 case USER_DATA_START_CODE:
897 DumpBits32( &p_vpar->bit_stream );
898 /* Wait for the next start code */
908 /*****************************************************************************
909 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
910 *****************************************************************************/
912 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
914 /* We don't care sequence_display_extension. */
916 DumpBits( &p_vpar->bit_stream, 3 );
917 if( GetBits( &p_vpar->bit_stream, 1 ) )
919 /* Three bytes for color_desciption */
920 DumpBits( &p_vpar->bit_stream, 24 );
922 /* display_horizontal and vertical_size and a marker_bit */
923 DumpBits( &p_vpar->bit_stream, 29 );
927 /*****************************************************************************
928 * QuantMatrixExtension : Load quantization matrices for luminance *
930 *****************************************************************************/
932 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
934 if( GetBits( &p_vpar->bit_stream, 1 ) )
936 /* Load intra_quantiser_matrix for luminance. */
937 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
941 /* Use the default matrix. */
942 LinkMatrix( &p_vpar->sequence.intra_quant,
943 pi_default_intra_quant );
945 if( GetBits( &p_vpar->bit_stream, 1 ) )
947 /* Load non_intra_quantiser_matrix for luminance. */
948 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
952 /* Use the default matrix. */
953 LinkMatrix( &p_vpar->sequence.nonintra_quant,
954 pi_default_nonintra_quant );
956 if( GetBits( &p_vpar->bit_stream, 1 ) )
958 /* Load intra_quantiser_matrix for chrominance. */
959 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
963 /* Link the chrominance intra matrix to the luminance one. */
964 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
965 p_vpar->sequence.intra_quant.pi_matrix );
967 if( GetBits( &p_vpar->bit_stream, 1 ) )
969 /* Load non_intra_quantiser_matrix for chrominance. */
970 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
974 /* Link the chrominance intra matrix to the luminance one. */
975 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
976 p_vpar->sequence.intra_quant.pi_matrix );
978 if( GetBits( &p_vpar->bit_stream, 1 ) )
980 /* Load non_intra_quantiser_matrix for chrominance. */
981 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
985 /* Link the chrominance nonintra matrix to the luminance one. */
986 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
987 p_vpar->sequence.nonintra_quant.pi_matrix );
992 /*****************************************************************************
993 * SequenceScalableExtension : Parse the sequence_scalable_extension *
994 * structure to handle scalable coding *
995 *****************************************************************************/
997 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
999 /* We don't care about anything scalable except the scalable mode. */
1000 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1001 /* The length of the structure depends on the value of the scalable_mode */
1004 DumpBits32( &p_vpar->bit_stream );
1005 DumpBits( &p_vpar->bit_stream, 21 );
1008 DumpBits( &p_vpar->bit_stream, 12 );
1011 DumpBits( &p_vpar->bit_stream, 4 );
1015 /*****************************************************************************
1016 * PictureDisplayExtension : Parse the picture_display_extension structure *
1017 *****************************************************************************/
1019 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1021 /* Number of frame center offset */
1023 /* I am not sure it works but it should
1024 (fewer tests than shown in §6.3.12) */
1025 nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1026 p_vpar->picture.b_repeat_first_field +
1027 p_vpar->picture.b_top_field_first
1028 : ( p_vpar->picture.b_frame_structure + 1 ) +
1029 p_vpar->picture.b_repeat_first_field;
1030 DumpBits( &p_vpar->bit_stream, 34 * nb );
1034 /*****************************************************************************
1035 * PictureSpatialScalableExtension *
1036 *****************************************************************************/
1038 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1040 /* That's scalable, so we trash it */
1041 DumpBits32( &p_vpar->bit_stream );
1042 DumpBits( &p_vpar->bit_stream, 16 );
1046 /*****************************************************************************
1047 * PictureTemporalScalableExtension *
1048 *****************************************************************************/
1050 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1052 /* Scalable again, trashed again */
1053 DumpBits( &p_vpar->bit_stream, 23 );
1057 /*****************************************************************************
1058 * CopyrightExtension : Keeps some legal informations *
1059 *****************************************************************************/
1061 static void CopyrightExtension( vpar_thread_t * p_vpar )
1063 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1064 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1065 /* A flag that says whether the copyright information is significant */
1066 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1067 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1068 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1070 DumpBits( &p_vpar->bit_stream, 8 );
1071 /* The copyright_number is split in three parts */
1073 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1074 DumpBits( &p_vpar->bit_stream, 1 );
1076 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1077 DumpBits( &p_vpar->bit_stream, 1 );
1078 /* third part and sum */
1079 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
1080 ( (u64)i_copyright_nb_2 << 22 ) +
1081 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );