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_backward != NULL )
207 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
208 p_vpar->sequence.p_backward = p_newref;
209 if( p_newref != NULL )
210 vout_LinkPicture( p_vpar->p_vout, p_newref );
214 /*****************************************************************************
215 * LoadMatrix : Load a quantization matrix
216 *****************************************************************************/
217 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
221 if( !p_matrix->b_allocated )
223 /* Allocate a piece of memory to load the matrix. */
224 p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) );
225 p_matrix->b_allocated = 1;
228 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
230 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
231 = GetBits( &p_vpar->bit_stream, 8 );
235 /* Discrete Fourier Transform requires the quantization matrices to
236 * be normalized before using them. */
237 vdec_NormQuantMatrix( p_matrix->pi_matrix );
241 /*****************************************************************************
242 * LinkMatrix : Link a quantization matrix to another
243 *****************************************************************************/
244 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
246 if( p_matrix->b_allocated )
248 /* Deallocate the piece of memory. */
249 free( p_matrix->pi_matrix );
250 p_matrix->b_allocated = 0;
253 p_matrix->pi_matrix = pi_array;
257 * Exported functions.
260 /*****************************************************************************
261 * vpar_NextSequenceHeader : Find the next sequence header
262 *****************************************************************************/
263 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
265 while( !p_vpar->b_die )
267 NextStartCode( p_vpar );
268 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
270 RemoveBits( &p_vpar->bit_stream, 8 );
275 /*****************************************************************************
276 * vpar_ParseHeader : Parse the next header
277 *****************************************************************************/
278 int vpar_ParseHeader( vpar_thread_t * p_vpar )
280 while( !p_vpar->b_die )
282 NextStartCode( p_vpar );
283 switch( GetBits32( &p_vpar->bit_stream ) )
285 case SEQUENCE_HEADER_CODE:
286 SequenceHeader( p_vpar );
290 case GROUP_START_CODE:
291 GroupHeader( p_vpar );
295 case PICTURE_START_CODE:
296 PictureHeader( p_vpar );
300 case SEQUENCE_END_CODE:
301 intf_DbgMsg("vpar debug: sequence end code received\n");
313 * Following functions are local
316 /*****************************************************************************
317 * SequenceHeader : Parse the next sequence header
318 *****************************************************************************/
319 static void SequenceHeader( vpar_thread_t * p_vpar )
322 static float r_frame_rate_table[16] =
325 ((23.0*1000.0)/1001.0),
328 ((30.0*1000.0)/1001.0),
331 ((60.0*1000.0)/1001.0),
333 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
337 int i_height_save, i_width_save;
339 i_height_save = p_vpar->sequence.i_height;
340 i_width_save = p_vpar->sequence.i_width;
342 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
343 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
344 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
345 p_vpar->sequence.r_frame_rate =
346 r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
348 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
349 * constrained_parameters_flag */
350 RemoveBits( &p_vpar->bit_stream, 30 );
353 * Quantization matrices
355 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
357 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
361 /* Use default matrix. */
362 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
365 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
367 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
371 /* Use default matrix. */
372 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
375 /* Unless later overwritten by a matrix extension, we have the same
376 * matrices for luminance and chrominance. */
377 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
378 p_vpar->sequence.intra_quant.pi_matrix );
379 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
380 p_vpar->sequence.nonintra_quant.pi_matrix );
385 NextStartCode( p_vpar );
386 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
389 static f_chroma_pattern_t ppf_chroma_pattern[4] =
390 {NULL, vpar_CodedPattern420,
391 vpar_CodedPattern422, vpar_CodedPattern444};
393 /* Turn the MPEG2 flag on */
394 p_vpar->sequence.b_mpeg2 = 1;
396 /* Parse sequence_extension */
397 RemoveBits32( &p_vpar->bit_stream );
398 /* extension_start_code_identifier, profile_and_level_indication */
399 RemoveBits( &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 RemoveBits( &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.r_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. */
420 p_vpar->sequence.b_mpeg2 = 0;
421 p_vpar->sequence.b_progressive = 1;
422 p_vpar->sequence.i_chroma_format = CHROMA_420;
423 p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
425 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
429 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
430 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
431 (p_vpar->sequence.i_height + 15) / 16 :
432 2 * ((p_vpar->sequence.i_height + 31) / 32);
433 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
434 * p_vpar->sequence.i_mb_height;
435 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
436 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
437 p_vpar->sequence.i_size = p_vpar->sequence.i_width
438 * p_vpar->sequence.i_height;
440 /* Update chromatic information */
441 switch( p_vpar->sequence.i_chroma_format )
444 p_vpar->sequence.i_chroma_nb_blocks = 2;
445 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
446 p_vpar->sequence.i_chroma_mb_width = 8;
447 p_vpar->sequence.i_chroma_mb_height = 8;
451 p_vpar->sequence.i_chroma_nb_blocks = 4;
452 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
453 p_vpar->sequence.i_chroma_mb_width = 8;
454 p_vpar->sequence.i_chroma_mb_height = 16;
458 p_vpar->sequence.i_chroma_nb_blocks = 8;
459 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
460 p_vpar->sequence.i_chroma_mb_width = 16;
461 p_vpar->sequence.i_chroma_mb_height = 16;
464 /* Slice Header functions */
465 if( p_vpar->sequence.i_height <= 2800 )
467 if( p_vpar->sequence.i_scalable_mode != SC_DP )
469 p_vpar->sequence.pf_slice_header = SliceHeader00;
473 p_vpar->sequence.pf_slice_header = SliceHeader01;
478 if( p_vpar->sequence.i_scalable_mode != SC_DP )
480 p_vpar->sequence.pf_slice_header = SliceHeader10;
484 p_vpar->sequence.pf_slice_header = SliceHeader11;
488 if( p_vpar->sequence.i_width != i_width_save
489 || p_vpar->sequence.i_height != i_height_save )
491 /* What do we do in case of a size change ??? */
494 /* Extension and User data */
495 ExtensionAndUserData( p_vpar );
498 /*****************************************************************************
499 * GroupHeader : Parse the next group of pictures header
500 *****************************************************************************/
501 static void GroupHeader( vpar_thread_t * p_vpar )
503 /* Nothing to do, we don't care. */
504 RemoveBits( &p_vpar->bit_stream, 27 );
505 ExtensionAndUserData( p_vpar );
508 /*****************************************************************************
509 * PictureHeader : Parse the next picture header
510 *****************************************************************************/
511 static void PictureHeader( vpar_thread_t * p_vpar )
513 static f_macroblock_type_t ppf_macroblock_type[5] = {NULL,
514 vpar_IMBType, vpar_PMBType,
515 vpar_BMBType, vpar_DMBType};
518 int i_mb_address, i_mb_base, i_mb;
519 boolean_t b_parsable;
522 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
523 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
524 p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
525 [p_vpar->picture.i_coding_type];
526 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
528 fprintf( stderr, "coding type: %d\n", p_vpar->picture.i_coding_type );
530 if( p_vpar->picture.i_coding_type == P_CODING_TYPE || p_vpar->picture.i_coding_type == B_CODING_TYPE )
532 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
533 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
535 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
537 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
538 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
541 /* extra_information_picture */
542 while( GetBits( &p_vpar->bit_stream, 1 ) )
544 RemoveBits( &p_vpar->bit_stream, 8 );
548 * Picture Coding Extension
550 NextStartCode( p_vpar );
551 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
553 /* Parse picture_coding_extension */
554 RemoveBits32( &p_vpar->bit_stream );
555 /* extension_start_code_identifier */
556 RemoveBits( &p_vpar->bit_stream, 4 );
558 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
559 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
560 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
561 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
562 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
563 i_structure = GetBits( &p_vpar->bit_stream, 2 );
564 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
565 p_vpar->picture.b_frame_pred_frame_dct
566 = GetBits( &p_vpar->bit_stream, 1 );
567 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
568 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
569 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
570 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
571 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
572 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
573 * the length of the picture_display_extension structure.
574 * chroma_420_type (obsolete) */
575 RemoveBits( &p_vpar->bit_stream, 1 );
576 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
578 /* composite_display_flag */
579 if( GetBits( &p_vpar->bit_stream, 1 ) )
581 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
582 * sub_carrier_phase */
583 RemoveBits( &p_vpar->bit_stream, 20 );
588 /* MPEG-1 compatibility flags */
589 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
590 i_structure = FRAME_STRUCTURE;
591 p_vpar->picture.b_frame_pred_frame_dct = 1;
592 p_vpar->picture.b_concealment_mv = 0;
593 p_vpar->picture.b_q_scale_type = 0;
594 p_vpar->picture.b_intra_vlc_format = 0;
595 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
596 p_vpar->picture.b_repeat_first_field = 0;
597 p_vpar->picture.b_progressive_frame = 1;
600 if( p_vpar->picture.i_current_structure &&
601 (i_structure == FRAME_STRUCTURE ||
602 i_structure == p_vpar->picture.i_current_structure) )
604 /* We don't have the second field of the buffered frame. */
605 if( p_vpar->picture.p_picture != NULL )
607 ReferenceReplace( p_vpar,
608 p_vpar->picture.i_coding_type,
611 for( i_mb = 0; i_mb < p_vpar->sequence.i_mb_size >> 1; i_mb++ )
613 vpar_DestroyMacroblock( &p_vpar->vfifo,
614 p_vpar->picture.pp_mb[i_mb] );
616 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
619 p_vpar->picture.i_current_structure = 0;
621 intf_DbgMsg("vpar debug: odd number of field picture.\n");
624 /* Do we have the reference pictures ? */
625 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
626 (p_vpar->sequence.p_backward == NULL)) ||
627 /* p_backward will become p_forward later */
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 if( p_vpar->picture.i_current_structure )
636 /* Second field of a frame. We will decode it if, and only if we
637 * have decoded the first frame. */
638 b_parsable = (p_vpar->picture.p_picture != NULL);
642 /* Does synchro say we have enough time to decode it ? */
643 b_parsable = vpar_SynchroChoose( p_vpar,
644 p_vpar->picture.i_coding_type, i_structure );
650 /* Update the reference pointers. */
651 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
653 /* Warn Synchro we have trashed a picture. */
654 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
656 /* Update context. */
657 if( i_structure != FRAME_STRUCTURE )
658 p_vpar->picture.i_current_structure = i_structure;
659 p_vpar->picture.p_picture = NULL;
664 /* OK, now we are sure we will decode the picture. */
665 #define P_picture p_vpar->picture.p_picture
666 p_vpar->picture.b_error = 0;
668 if( !p_vpar->picture.i_current_structure )
670 /* This is a new frame. Get a structure from the video_output. */
671 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
672 99+p_vpar->sequence.i_chroma_format, /*???*/
673 p_vpar->sequence.i_width,
674 p_vpar->sequence.i_height ) )
677 intf_ErrMsg("vpar debug: allocation error in vout_CreatePicture\n");
682 mwait( VPAR_IDLE_SLEEP );
685 /* Initialize values. */
686 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
687 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
689 /* Put date immediately. */
690 vout_DatePicture( p_vpar->p_vout, P_picture,
691 vpar_SynchroDate( p_vpar ) );
693 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
694 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
695 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
696 << ( 1 - p_vpar->picture.b_frame_structure ) );
697 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
698 << ( 1 - p_vpar->picture.b_frame_structure ));
700 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
701 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
702 memset( p_vpar->picture.pp_mb, 0, MAX_MB );
703 memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
705 /* Update the reference pointers. */
706 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
708 p_vpar->picture.i_current_structure |= i_structure;
709 p_vpar->picture.i_structure = i_structure;
710 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
712 /* Initialize picture data for decoding. */
713 if( p_vpar->picture.b_motion_field = (i_structure == BOTTOM_FIELD) )
715 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
716 p_vpar->mb.i_l_y = 1;
717 p_vpar->mb.i_c_y = 1;
722 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
725 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
727 /* Extension and User data. */
728 ExtensionAndUserData( p_vpar );
730 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
731 NextStartCode( p_vpar );
732 while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
733 && !p_vpar->picture.b_error && !p_vpar->b_die )
735 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
736 < SLICE_START_CODE_MIN) ||
737 (i_dummy > SLICE_START_CODE_MAX) )
739 intf_DbgMsg("vpar debug: premature end of picture\n");
740 p_vpar->picture.b_error = 1;
743 RemoveBits32( &p_vpar->bit_stream );
745 /* Decode slice data. */
746 p_vpar->sequence.pf_slice_header( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
749 if( p_vpar->picture.b_error )
752 fprintf(stderr, "Image trashee\n");
753 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
755 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
757 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
759 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
761 /* Prepare context for the next picture. */
763 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
764 p_vpar->picture.i_current_structure = 0;
766 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
768 fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
769 /* Frame completely parsed. */
770 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb]; i_mb++ )
772 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
775 /* Link referenced pictures for the decoder
776 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
777 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
778 p_vpar->picture.i_coding_type == B_CODING_TYPE )
780 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
782 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
784 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
787 /* Send signal to the video_decoder. */
788 vlc_mutex_lock( &p_vpar->vfifo.lock );
789 vlc_cond_signal( &p_vpar->vfifo.wait );
790 vlc_mutex_unlock( &p_vpar->vfifo.lock );
792 /* Prepare context for the next picture. */
794 p_vpar->picture.i_current_structure = 0;
799 /*****************************************************************************
800 * SliceHeader : Parse the next slice structure
801 *****************************************************************************/
802 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
803 int * pi_mb_address, int i_mb_base,
806 /* DC predictors initialization table */
807 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
809 int i_mb_address_save = *pi_mb_address;
811 /* slice_vertical_position_extension and priority_breakpoint already done */
812 LoadQuantizerScale( p_vpar );
814 if( GetBits( &p_vpar->bit_stream, 1 ) )
816 /* intra_slice, slice_id */
817 RemoveBits( &p_vpar->bit_stream, 8 );
818 /* extra_information_slice */
819 while( GetBits( &p_vpar->bit_stream, 1 ) )
821 RemoveBits( &p_vpar->bit_stream, 8 );
824 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
826 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
827 * does the reference decoder put 0 instead of the normative values ? */
828 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
829 = p_vpar->slice.pi_dc_dct_pred[2]
830 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
832 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
833 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
837 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
839 i_mb_address_save = *pi_mb_address;
841 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
842 NextStartCode( p_vpar );
845 /*****************************************************************************
846 * SliceHeaderXY : Parse the next slice structure
847 *****************************************************************************
848 * X = i_height > 2800 ?
849 * Y = scalable_mode == SC_DP ?
850 *****************************************************************************/
851 static void SliceHeader00( vpar_thread_t * p_vpar,
852 int * pi_mb_address, int i_mb_base,
855 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
858 static void SliceHeader01( vpar_thread_t * p_vpar,
859 int * pi_mb_address, int i_mb_base,
862 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
863 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
866 static void SliceHeader10( vpar_thread_t * p_vpar,
867 int * pi_mb_address, int i_mb_base,
870 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
871 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
874 static void SliceHeader11( vpar_thread_t * p_vpar,
875 int * pi_mb_address, int i_mb_base,
878 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
879 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
880 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
883 /*****************************************************************************
884 * ExtensionAndUserData : Parse the extension_and_user_data structure
885 *****************************************************************************/
886 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
888 while( !p_vpar->b_die )
890 NextStartCode( p_vpar );
891 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
893 case EXTENSION_START_CODE:
894 RemoveBits32( &p_vpar->bit_stream );
895 switch( GetBits( &p_vpar->bit_stream, 4 ) )
897 case SEQUENCE_DISPLAY_EXTENSION_ID:
898 SequenceDisplayExtension( p_vpar );
900 case QUANT_MATRIX_EXTENSION_ID:
901 QuantMatrixExtension( p_vpar );
903 case SEQUENCE_SCALABLE_EXTENSION_ID:
904 SequenceScalableExtension( p_vpar );
906 case PICTURE_DISPLAY_EXTENSION_ID:
907 PictureDisplayExtension( p_vpar );
909 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
910 PictureSpatialScalableExtension( p_vpar );
912 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
913 PictureTemporalScalableExtension( p_vpar );
915 case COPYRIGHT_EXTENSION_ID:
916 CopyrightExtension( p_vpar );
922 case USER_DATA_START_CODE:
923 RemoveBits32( &p_vpar->bit_stream );
924 /* Wait for the next start code */
934 /*****************************************************************************
935 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
936 *****************************************************************************/
938 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
940 /* We don't care sequence_display_extension. */
942 RemoveBits( &p_vpar->bit_stream, 3 );
943 if( GetBits( &p_vpar->bit_stream, 1 ) )
945 /* Two bytes for color_desciption */
946 RemoveBits( &p_vpar->bit_stream, 16 );
947 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
949 /* display_horizontal and vertical_size and a marker_bit */
950 RemoveBits( &p_vpar->bit_stream, 29 );
954 /*****************************************************************************
955 * QuantMatrixExtension : Load quantization matrices for luminance *
957 *****************************************************************************/
959 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
961 if( GetBits( &p_vpar->bit_stream, 1 ) )
963 /* Load intra_quantiser_matrix for luminance. */
964 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
968 /* Use the default matrix. */
969 LinkMatrix( &p_vpar->sequence.intra_quant,
970 pi_default_intra_quant );
972 if( GetBits( &p_vpar->bit_stream, 1 ) )
974 /* Load non_intra_quantiser_matrix for luminance. */
975 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
979 /* Use the default matrix. */
980 LinkMatrix( &p_vpar->sequence.nonintra_quant,
981 pi_default_nonintra_quant );
983 if( GetBits( &p_vpar->bit_stream, 1 ) )
985 /* Load intra_quantiser_matrix for chrominance. */
986 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
990 /* Link the chrominance intra matrix to the luminance one. */
991 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
992 p_vpar->sequence.intra_quant.pi_matrix );
994 if( GetBits( &p_vpar->bit_stream, 1 ) )
996 /* Load non_intra_quantiser_matrix for chrominance. */
997 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1001 /* Link the chrominance intra matrix to the luminance one. */
1002 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1003 p_vpar->sequence.intra_quant.pi_matrix );
1005 if( GetBits( &p_vpar->bit_stream, 1 ) )
1007 /* Load non_intra_quantiser_matrix for chrominance. */
1008 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1012 /* Link the chrominance nonintra matrix to the luminance one. */
1013 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1014 p_vpar->sequence.nonintra_quant.pi_matrix );
1019 /*****************************************************************************
1020 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1021 * structure to handle scalable coding *
1022 *****************************************************************************/
1024 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1026 /* We don't care about anything scalable except the scalable mode. */
1027 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1028 /* The length of the structure depends on the value of the scalable_mode */
1031 RemoveBits32( &p_vpar->bit_stream );
1032 RemoveBits( &p_vpar->bit_stream, 21 );
1035 RemoveBits( &p_vpar->bit_stream, 12 );
1038 RemoveBits( &p_vpar->bit_stream, 4 );
1042 /*****************************************************************************
1043 * PictureDisplayExtension : Parse the picture_display_extension structure *
1044 *****************************************************************************/
1046 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1048 /* Number of frame center offset */
1050 /* I am not sure it works but it should
1051 (fewer tests than shown in ยง6.3.12) */
1052 nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1053 p_vpar->picture.b_repeat_first_field +
1054 p_vpar->picture.b_top_field_first
1055 : ( p_vpar->picture.b_frame_structure + 1 ) +
1056 p_vpar->picture.b_repeat_first_field;
1057 RemoveBits( &p_vpar->bit_stream, 34 * nb );
1061 /*****************************************************************************
1062 * PictureSpatialScalableExtension *
1063 *****************************************************************************/
1065 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1067 /* That's scalable, so we trash it */
1068 RemoveBits32( &p_vpar->bit_stream );
1069 RemoveBits( &p_vpar->bit_stream, 16 );
1073 /*****************************************************************************
1074 * PictureTemporalScalableExtension *
1075 *****************************************************************************/
1077 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1079 /* Scalable again, trashed again */
1080 RemoveBits( &p_vpar->bit_stream, 23 );
1084 /*****************************************************************************
1085 * CopyrightExtension : Keeps some legal informations *
1086 *****************************************************************************/
1088 static void CopyrightExtension( vpar_thread_t * p_vpar )
1090 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1091 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1092 /* A flag that says whether the copyright information is significant */
1093 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1094 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1095 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1097 RemoveBits( &p_vpar->bit_stream, 8 );
1098 /* The copyright_number is split in three parts */
1100 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1101 RemoveBits( &p_vpar->bit_stream, 1 );
1103 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1104 RemoveBits( &p_vpar->bit_stream, 1 );
1105 /* third part and sum */
1106 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) +
1107 ( (u64)i_copyright_nb_2 << 22 ) +
1108 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );