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, 24 ) != 0x01L && !p_vpar->b_die )
169 RemoveBits( &p_vpar->bit_stream, 8 );
173 /*****************************************************************************
174 * ReferenceUpdate : Update the reference pointers when we have a new picture
175 *****************************************************************************/
176 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
178 picture_t * p_newref )
180 if( i_coding_type != B_CODING_TYPE )
182 if( p_vpar->sequence.p_forward != NULL )
183 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
184 if( p_vpar->sequence.p_backward != NULL )
186 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
187 vpar_SynchroDate( p_vpar ) );
189 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
190 p_vpar->sequence.p_backward = p_newref;
191 if( p_newref != NULL )
192 vout_LinkPicture( p_vpar->p_vout, p_newref );
196 /*****************************************************************************
197 * ReferenceReplace : Replace the last reference pointer when we destroy
199 *****************************************************************************/
200 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
202 picture_t * p_newref )
204 if( i_coding_type != B_CODING_TYPE )
206 if( p_vpar->sequence.p_forward != NULL )
207 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
208 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
209 p_vpar->sequence.p_backward = p_newref;
210 if( p_newref != NULL )
211 vout_LinkPicture( p_vpar->p_vout, p_newref );
215 /*****************************************************************************
216 * LoadMatrix : Load a quantization matrix
217 *****************************************************************************/
218 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
222 if( !p_matrix->b_allocated )
224 /* Allocate a piece of memory to load the matrix. */
225 p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
226 p_matrix->b_allocated = 1;
229 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
231 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
232 = GetBits( &p_vpar->bit_stream, 8 );
236 /* Discrete Fourier Transform requires the quantization matrices to
237 * be normalized before using them. */
238 vdec_NormQuantMatrix( p_matrix->pi_matrix );
242 /*****************************************************************************
243 * LinkMatrix : Link a quantization matrix to another
244 *****************************************************************************/
245 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
247 if( p_matrix->b_allocated )
249 /* Deallocate the piece of memory. */
250 free( p_matrix->pi_matrix );
251 p_matrix->b_allocated = 0;
254 p_matrix->pi_matrix = pi_array;
258 * Exported functions.
261 /*****************************************************************************
262 * vpar_NextSequenceHeader : Find the next sequence header
263 *****************************************************************************/
264 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
266 while( !p_vpar->b_die )
268 NextStartCode( p_vpar );
269 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
271 RemoveBits( &p_vpar->bit_stream, 8 );
276 /*****************************************************************************
277 * vpar_ParseHeader : Parse the next header
278 *****************************************************************************/
279 int vpar_ParseHeader( vpar_thread_t * p_vpar )
281 while( !p_vpar->b_die )
283 NextStartCode( p_vpar );
284 switch( GetBits32( &p_vpar->bit_stream ) )
286 case SEQUENCE_HEADER_CODE:
287 SequenceHeader( p_vpar );
291 case GROUP_START_CODE:
292 GroupHeader( p_vpar );
296 case PICTURE_START_CODE:
297 PictureHeader( p_vpar );
301 case SEQUENCE_END_CODE:
302 intf_DbgMsg("vpar debug: sequence end code received\n");
314 * Following functions are local
317 /*****************************************************************************
318 * SequenceHeader : Parse the next sequence header
319 *****************************************************************************/
320 static void SequenceHeader( vpar_thread_t * p_vpar )
323 static float r_frame_rate_table[16] =
326 ((23.0*1000.0)/1001.0),
329 ((30.0*1000.0)/1001.0),
332 ((60.0*1000.0)/1001.0),
334 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
338 int i_height_save, i_width_save;
340 i_height_save = p_vpar->sequence.i_height;
341 i_width_save = p_vpar->sequence.i_width;
343 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
344 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
345 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
346 p_vpar->sequence.r_frame_rate =
347 r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
349 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
350 * constrained_parameters_flag */
351 RemoveBits( &p_vpar->bit_stream, 30 );
354 * Quantization matrices
356 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
358 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
362 /* Use default matrix. */
363 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
366 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
368 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
372 /* Use default matrix. */
373 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
376 /* Unless later overwritten by a matrix extension, we have the same
377 * matrices for luminance and chrominance. */
378 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
379 p_vpar->sequence.intra_quant.pi_matrix );
380 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
381 p_vpar->sequence.nonintra_quant.pi_matrix );
386 NextStartCode( p_vpar );
387 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
390 static f_chroma_pattern_t ppf_chroma_pattern[4] =
391 {NULL, vpar_CodedPattern420,
392 vpar_CodedPattern422, vpar_CodedPattern444};
394 /* Turn the MPEG2 flag on */
395 p_vpar->sequence.b_mpeg2 = 1;
397 /* Parse sequence_extension */
398 RemoveBits32( &p_vpar->bit_stream );
399 /* extension_start_code_identifier, profile_and_level_indication */
400 RemoveBits( &p_vpar->bit_stream, 12 );
401 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
402 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
403 p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
404 [p_vpar->sequence.i_chroma_format];
405 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
406 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
407 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
408 RemoveBits( &p_vpar->bit_stream, 22 );
409 /* frame_rate_extension_n */
410 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
411 /* frame_rate_extension_d */
412 p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
413 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
415 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
419 /* It's an MPEG-1 stream. Put adequate parameters. */
421 p_vpar->sequence.b_mpeg2 = 0;
422 p_vpar->sequence.b_progressive = 1;
423 p_vpar->sequence.i_chroma_format = CHROMA_420;
424 p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
426 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
430 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
431 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
432 (p_vpar->sequence.i_height + 15) / 16 :
433 2 * ((p_vpar->sequence.i_height + 31) / 32);
434 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
435 * p_vpar->sequence.i_mb_height;
436 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
437 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
438 p_vpar->sequence.i_size = p_vpar->sequence.i_width
439 * p_vpar->sequence.i_height;
441 /* Update chromatic information */
442 switch( p_vpar->sequence.i_chroma_format )
445 p_vpar->sequence.i_chroma_nb_blocks = 2;
446 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
447 p_vpar->sequence.i_chroma_mb_width = 8;
448 p_vpar->sequence.i_chroma_mb_height = 8;
452 p_vpar->sequence.i_chroma_nb_blocks = 4;
453 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
454 p_vpar->sequence.i_chroma_mb_width = 8;
455 p_vpar->sequence.i_chroma_mb_height = 16;
459 p_vpar->sequence.i_chroma_nb_blocks = 8;
460 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
461 p_vpar->sequence.i_chroma_mb_width = 16;
462 p_vpar->sequence.i_chroma_mb_height = 16;
465 /* Slice Header functions */
466 if( p_vpar->sequence.i_height <= 2800 )
468 if( p_vpar->sequence.i_scalable_mode != SC_DP )
470 p_vpar->sequence.pf_slice_header = SliceHeader00;
474 p_vpar->sequence.pf_slice_header = SliceHeader01;
479 if( p_vpar->sequence.i_scalable_mode != SC_DP )
481 p_vpar->sequence.pf_slice_header = SliceHeader10;
485 p_vpar->sequence.pf_slice_header = SliceHeader11;
489 if( p_vpar->sequence.i_width != i_width_save
490 || p_vpar->sequence.i_height != i_height_save )
492 /* What do we do in case of a size change ??? */
495 /* Extension and User data */
496 ExtensionAndUserData( p_vpar );
499 /*****************************************************************************
500 * GroupHeader : Parse the next group of pictures header
501 *****************************************************************************/
502 static void GroupHeader( vpar_thread_t * p_vpar )
504 /* Nothing to do, we don't care. */
505 RemoveBits( &p_vpar->bit_stream, 27 );
506 ExtensionAndUserData( p_vpar );
509 /*****************************************************************************
510 * PictureHeader : Parse the next picture header
511 *****************************************************************************/
512 static void PictureHeader( vpar_thread_t * p_vpar )
514 static f_macroblock_type_t ppf_macroblock_type[5] = {NULL,
515 vpar_IMBType, vpar_PMBType,
516 vpar_BMBType, vpar_DMBType};
519 int i_mb_address, i_mb_base, i_mb;
520 boolean_t b_parsable;
523 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
524 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
525 p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
526 [p_vpar->picture.i_coding_type];
527 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
529 if( p_vpar->picture.i_coding_type == P_CODING_TYPE || p_vpar->picture.i_coding_type == B_CODING_TYPE )
531 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
532 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
534 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
536 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
537 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
540 /* extra_information_picture */
541 while( GetBits( &p_vpar->bit_stream, 1 ) )
543 RemoveBits( &p_vpar->bit_stream, 8 );
547 * Picture Coding Extension
549 NextStartCode( p_vpar );
550 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
552 /* Parse picture_coding_extension */
553 RemoveBits32( &p_vpar->bit_stream );
554 /* extension_start_code_identifier */
555 RemoveBits( &p_vpar->bit_stream, 4 );
557 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
558 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
559 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
560 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
561 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
562 i_structure = GetBits( &p_vpar->bit_stream, 2 );
563 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
564 p_vpar->picture.b_frame_pred_frame_dct
565 = GetBits( &p_vpar->bit_stream, 1 );
566 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
567 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
568 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
569 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
570 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
571 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
572 * the length of the picture_display_extension structure.
573 * chroma_420_type (obsolete) */
574 RemoveBits( &p_vpar->bit_stream, 1 );
575 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
577 /* composite_display_flag */
578 if( GetBits( &p_vpar->bit_stream, 1 ) )
580 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
581 * sub_carrier_phase */
582 RemoveBits( &p_vpar->bit_stream, 20 );
587 /* MPEG-1 compatibility flags */
588 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
589 i_structure = FRAME_STRUCTURE;
590 p_vpar->picture.b_frame_pred_frame_dct = 1;
591 p_vpar->picture.b_concealment_mv = 0;
592 p_vpar->picture.b_q_scale_type = 0;
593 p_vpar->picture.b_intra_vlc_format = 0;
594 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
595 p_vpar->picture.b_repeat_first_field = 0;
596 p_vpar->picture.b_progressive_frame = 1;
599 if( p_vpar->picture.i_current_structure &&
600 (i_structure == FRAME_STRUCTURE ||
601 i_structure == p_vpar->picture.i_current_structure) )
603 /* We don't have the second field of the buffered frame. */
604 if( p_vpar->picture.p_picture != NULL )
606 ReferenceReplace( p_vpar,
607 p_vpar->picture.i_coding_type,
610 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
612 vpar_DestroyMacroblock( &p_vpar->vfifo,
613 p_vpar->picture.pp_mb[i_mb] );
615 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
618 p_vpar->picture.i_current_structure = 0;
620 intf_DbgMsg("vpar debug: odd number of field picture.\n");
623 /* Do we have the reference pictures ? */
624 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
625 (p_vpar->sequence.p_backward == NULL)) ||
626 /* p_backward will become p_forward later */
627 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
628 (p_vpar->sequence.p_forward == NULL ||
629 p_vpar->sequence.p_backward == NULL)));
633 if( p_vpar->picture.i_current_structure )
635 /* Second field of a frame. We will decode it if, and only if we
636 * have decoded the first frame. */
637 b_parsable = (p_vpar->picture.p_picture != NULL);
641 /* Does synchro say we have enough time to decode it ? */
642 b_parsable = vpar_SynchroChoose( p_vpar,
643 p_vpar->picture.i_coding_type, i_structure );
649 /* Update the reference pointers. */
650 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
652 /* Warn Synchro we have trashed a picture. */
653 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
655 /* Update context. */
656 if( i_structure != FRAME_STRUCTURE )
657 p_vpar->picture.i_current_structure = i_structure;
658 p_vpar->picture.p_picture = NULL;
663 /* OK, now we are sure we will decode the picture. */
664 #define P_picture p_vpar->picture.p_picture
665 p_vpar->picture.b_error = 0;
667 if( !p_vpar->picture.i_current_structure )
669 /* This is a new frame. Get a structure from the video_output. */
670 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
671 99+p_vpar->sequence.i_chroma_format, /*???*/
672 p_vpar->sequence.i_width,
673 p_vpar->sequence.i_height ) )
676 intf_ErrMsg("vpar debug: allocation error in vout_CreatePicture\n");
681 mwait( VPAR_IDLE_SLEEP );
684 /* Initialize values. */
685 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
686 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
688 /* Put date immediately. */
689 vout_DatePicture( p_vpar->p_vout, P_picture,
690 vpar_SynchroDate( p_vpar ) );
692 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
693 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
694 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
695 << ( 1 - p_vpar->picture.b_frame_structure ) );
696 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
697 << ( 1 - p_vpar->picture.b_frame_structure ));
699 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
700 memset( p_vpar->picture.pp_mb, 0, MAX_MB );
701 memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
703 /* Update the reference pointers. */
704 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
706 p_vpar->picture.i_current_structure |= i_structure;
707 p_vpar->picture.i_structure = i_structure;
708 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
710 /* Initialize picture data for decoding. */
711 if( p_vpar->picture.b_motion_field = (i_structure == BOTTOM_FIELD) )
713 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
714 p_vpar->mb.i_l_y = 1;
715 p_vpar->mb.i_c_y = 1;
720 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
723 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
725 /* Extension and User data. */
726 ExtensionAndUserData( p_vpar );
728 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
729 NextStartCode( p_vpar );
730 while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
731 && !p_vpar->picture.b_error && !p_vpar->b_die )
733 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
734 < SLICE_START_CODE_MIN) ||
735 (i_dummy > SLICE_START_CODE_MAX) )
737 intf_DbgMsg("vpar debug: premature end of picture\n");
738 p_vpar->picture.b_error = 1;
741 RemoveBits32( &p_vpar->bit_stream );
743 /* Decode slice data. */
744 p_vpar->sequence.pf_slice_header( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
747 if( p_vpar->picture.b_error )
750 fprintf(stderr, "Image trashee\n");
751 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
753 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
755 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
757 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
759 /* Prepare context for the next picture. */
761 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
762 p_vpar->picture.i_current_structure = 0;
764 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
766 fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
767 /* Frame completely parsed. */
768 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
770 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
773 /* Link referenced pictures for the decoder
774 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
775 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
776 p_vpar->picture.i_coding_type == B_CODING_TYPE )
778 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
780 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
782 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
785 /* Send signal to the video_decoder. */
786 vlc_mutex_lock( &p_vpar->vfifo.lock );
787 vlc_cond_signal( &p_vpar->vfifo.wait );
788 vlc_mutex_unlock( &p_vpar->vfifo.lock );
790 /* Prepare context for the next picture. */
792 p_vpar->picture.i_current_structure = 0;
797 /*****************************************************************************
798 * SliceHeader : Parse the next slice structure
799 *****************************************************************************/
800 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
801 int * pi_mb_address, int i_mb_base,
804 /* DC predictors initialization table */
805 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
807 int i_mb_address_save = *pi_mb_address;
809 /* slice_vertical_position_extension and priority_breakpoint already done */
810 LoadQuantizerScale( p_vpar );
812 if( GetBits( &p_vpar->bit_stream, 1 ) )
814 /* intra_slice, slice_id */
815 RemoveBits( &p_vpar->bit_stream, 8 );
816 /* extra_information_slice */
817 while( GetBits( &p_vpar->bit_stream, 1 ) )
819 RemoveBits( &p_vpar->bit_stream, 8 );
822 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
824 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
825 * does the reference decoder put 0 instead of the normative values ? */
826 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
827 = p_vpar->slice.pi_dc_dct_pred[2]
828 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
830 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
831 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
835 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
837 i_mb_address_save = *pi_mb_address;
839 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
840 NextStartCode( p_vpar );
843 /*****************************************************************************
844 * SliceHeaderXY : Parse the next slice structure
845 *****************************************************************************
846 * X = i_height > 2800 ?
847 * Y = scalable_mode == SC_DP ?
848 *****************************************************************************/
849 static void SliceHeader00( vpar_thread_t * p_vpar,
850 int * pi_mb_address, int i_mb_base,
853 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
856 static void SliceHeader01( vpar_thread_t * p_vpar,
857 int * pi_mb_address, int i_mb_base,
860 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
861 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
864 static void SliceHeader10( vpar_thread_t * p_vpar,
865 int * pi_mb_address, int i_mb_base,
868 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
869 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
872 static void SliceHeader11( vpar_thread_t * p_vpar,
873 int * pi_mb_address, int i_mb_base,
876 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
877 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
878 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
881 /*****************************************************************************
882 * ExtensionAndUserData : Parse the extension_and_user_data structure
883 *****************************************************************************/
884 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
886 while( !p_vpar->b_die )
888 NextStartCode( p_vpar );
889 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
891 case EXTENSION_START_CODE:
892 RemoveBits32( &p_vpar->bit_stream );
893 switch( GetBits( &p_vpar->bit_stream, 4 ) )
895 case SEQUENCE_DISPLAY_EXTENSION_ID:
896 SequenceDisplayExtension( p_vpar );
898 case QUANT_MATRIX_EXTENSION_ID:
899 QuantMatrixExtension( p_vpar );
901 case SEQUENCE_SCALABLE_EXTENSION_ID:
902 SequenceScalableExtension( p_vpar );
904 case PICTURE_DISPLAY_EXTENSION_ID:
905 PictureDisplayExtension( p_vpar );
907 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
908 PictureSpatialScalableExtension( p_vpar );
910 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
911 PictureTemporalScalableExtension( p_vpar );
913 case COPYRIGHT_EXTENSION_ID:
914 CopyrightExtension( p_vpar );
920 case USER_DATA_START_CODE:
921 RemoveBits32( &p_vpar->bit_stream );
922 /* Wait for the next start code */
932 /*****************************************************************************
933 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
934 *****************************************************************************/
936 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
938 /* We don't care sequence_display_extension. */
940 RemoveBits( &p_vpar->bit_stream, 3 );
941 if( GetBits( &p_vpar->bit_stream, 1 ) )
943 /* Two bytes for color_desciption */
944 RemoveBits( &p_vpar->bit_stream, 16 );
945 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
947 /* display_horizontal and vertical_size and a marker_bit */
948 RemoveBits( &p_vpar->bit_stream, 29 );
952 /*****************************************************************************
953 * QuantMatrixExtension : Load quantization matrices for luminance *
955 *****************************************************************************/
957 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
959 if( GetBits( &p_vpar->bit_stream, 1 ) )
961 /* Load intra_quantiser_matrix for luminance. */
962 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
966 /* Use the default matrix. */
967 LinkMatrix( &p_vpar->sequence.intra_quant,
968 pi_default_intra_quant );
970 if( GetBits( &p_vpar->bit_stream, 1 ) )
972 /* Load non_intra_quantiser_matrix for luminance. */
973 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
977 /* Use the default matrix. */
978 LinkMatrix( &p_vpar->sequence.nonintra_quant,
979 pi_default_nonintra_quant );
981 if( GetBits( &p_vpar->bit_stream, 1 ) )
983 /* Load intra_quantiser_matrix for chrominance. */
984 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
988 /* Link the chrominance intra matrix to the luminance one. */
989 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
990 p_vpar->sequence.intra_quant.pi_matrix );
992 if( GetBits( &p_vpar->bit_stream, 1 ) )
994 /* Load non_intra_quantiser_matrix for chrominance. */
995 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
999 /* Link the chrominance intra matrix to the luminance one. */
1000 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1001 p_vpar->sequence.intra_quant.pi_matrix );
1003 if( GetBits( &p_vpar->bit_stream, 1 ) )
1005 /* Load non_intra_quantiser_matrix for chrominance. */
1006 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1010 /* Link the chrominance nonintra matrix to the luminance one. */
1011 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1012 p_vpar->sequence.nonintra_quant.pi_matrix );
1017 /*****************************************************************************
1018 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1019 * structure to handle scalable coding *
1020 *****************************************************************************/
1022 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1024 /* We don't care about anything scalable except the scalable mode. */
1025 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1026 /* The length of the structure depends on the value of the scalable_mode */
1029 RemoveBits32( &p_vpar->bit_stream );
1030 RemoveBits( &p_vpar->bit_stream, 21 );
1033 RemoveBits( &p_vpar->bit_stream, 12 );
1036 RemoveBits( &p_vpar->bit_stream, 4 );
1040 /*****************************************************************************
1041 * PictureDisplayExtension : Parse the picture_display_extension structure *
1042 *****************************************************************************/
1044 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1046 /* Number of frame center offset */
1048 /* I am not sure it works but it should
1049 (fewer tests than shown in ยง6.3.12) */
1050 nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1051 p_vpar->picture.b_repeat_first_field +
1052 p_vpar->picture.b_top_field_first
1053 : ( p_vpar->picture.b_frame_structure + 1 ) +
1054 p_vpar->picture.b_repeat_first_field;
1055 RemoveBits( &p_vpar->bit_stream, 34 * nb );
1059 /*****************************************************************************
1060 * PictureSpatialScalableExtension *
1061 *****************************************************************************/
1063 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1065 /* That's scalable, so we trash it */
1066 RemoveBits32( &p_vpar->bit_stream );
1067 RemoveBits( &p_vpar->bit_stream, 16 );
1071 /*****************************************************************************
1072 * PictureTemporalScalableExtension *
1073 *****************************************************************************/
1075 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1077 /* Scalable again, trashed again */
1078 RemoveBits( &p_vpar->bit_stream, 23 );
1082 /*****************************************************************************
1083 * CopyrightExtension : Keeps some legal informations *
1084 *****************************************************************************/
1086 static void CopyrightExtension( vpar_thread_t * p_vpar )
1088 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1089 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1090 /* A flag that says whether the copyright information is significant */
1091 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1092 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1093 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1095 RemoveBits( &p_vpar->bit_stream, 8 );
1096 /* The copyright_number is split in three parts */
1098 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1099 RemoveBits( &p_vpar->bit_stream, 1 );
1101 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1102 RemoveBits( &p_vpar->bit_stream, 1 );
1103 /* third part and sum */
1104 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
1105 ( (u64)i_copyright_nb_2 << 22 ) +
1106 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );