1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
4 *****************************************************************************/
6 /*****************************************************************************
8 *****************************************************************************/
19 #include "vlc_thread.h"
22 #include "debug.h" /* ?? temporaire, requis par netlist.h */
25 #include "input_netlist.h"
26 #include "decoder_fifo.h"
28 #include "video_output.h"
30 #include "vdec_idct.h"
31 #include "video_decoder.h"
32 #include "vdec_motion.h"
34 #include "vpar_blocks.h"
35 #include "vpar_headers.h"
36 #include "vpar_synchro.h"
37 #include "video_parser.h"
38 #include "vpar_motion.h"
39 #include "video_fifo.h"
44 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
45 static void SequenceHeader( vpar_thread_t * p_vpar );
46 static void GroupHeader( vpar_thread_t * p_vpar );
47 static void PictureHeader( vpar_thread_t * p_vpar );
48 static void SliceHeader00( vpar_thread_t * p_vpar,
49 int * pi_mb_address, int i_mb_base,
51 static void SliceHeader01( vpar_thread_t * p_vpar,
52 int * pi_mb_address, int i_mb_base,
54 static void SliceHeader10( vpar_thread_t * p_vpar,
55 int * pi_mb_address, int i_mb_base,
57 static void SliceHeader11( vpar_thread_t * p_vpar,
58 int * pi_mb_address, int i_mb_base,
60 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
61 int * pi_mb_address, int i_mb_base,
63 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
64 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
65 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
66 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
67 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
68 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
69 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
70 static void CopyrightExtension( vpar_thread_t * p_vpar );
76 /*****************************************************************************
77 * pi_default_intra_quant : default quantization matrix
78 *****************************************************************************/
80 int pi_default_intra_quant[] =
82 8, 16, 19, 22, 26, 27, 29, 34,
83 16, 16, 22, 24, 27, 29, 34, 37,
84 19, 22, 26, 27, 29, 34, 34, 38,
85 22, 22, 26, 27, 29, 34, 37, 40,
86 22, 26, 27, 29, 32, 35, 40, 48,
87 26, 27, 29, 32, 35, 40, 48, 58,
88 26, 27, 29, 34, 38, 46, 56, 69,
89 27, 29, 35, 38, 46, 56, 69, 83
92 int pi_default_intra_quant[] =
94 2048, 5681, 6355, 6623, 6656, 5431, 4018, 2401,
95 5681, 7880, 10207, 10021, 9587, 8091, 6534, 3625,
96 6355, 10207, 11363, 10619, 9700, 8935, 6155, 3507,
97 6623, 9186, 10226, 9557, 8730, 8041, 6028, 3322,
98 5632, 9232, 9031, 8730, 8192, 7040, 5542, 3390,
99 5230, 7533, 7621, 7568, 7040, 6321, 5225, 3219,
100 3602, 5189, 5250, 5539, 5265, 5007, 4199, 2638,
101 1907, 2841, 3230, 3156, 3249, 3108, 2638, 1617
105 /*****************************************************************************
106 * pi_default_nonintra_quant : default quantization matrix
107 *****************************************************************************/
109 int pi_default_nonintra_quant[] =
111 16, 16, 16, 16, 16, 16, 16, 16,
112 16, 16, 16, 16, 16, 16, 16, 16,
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
121 int pi_default_nonintra_quanit[] =
123 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
124 5680, 7888, 7424, 6688, 5680, 4464, 3072, 1568,
125 5344, 7424, 6992, 6288, 5344, 4208, 2896, 1472,
126 4816, 6688, 6288, 5664, 4816, 3792, 2608, 1328,
127 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
128 3216, 4464, 4208, 3792, 3216, 2528, 1744, 880,
129 2224, 3072, 2896, 2608, 2224, 1744, 1200, 608,
130 1136, 1568, 1472, 1328, 1136, 880, 608, 304
134 /*****************************************************************************
135 * pi_scan : zig-zag and alternate scan patterns
136 *****************************************************************************/
139 { /* Zig-Zag pattern */
140 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
141 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
142 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
143 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
145 { /* Alternate scan pattern */
146 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
147 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
148 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
149 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
154 * Local inline functions.
157 /*****************************************************************************
158 * NextStartCode : Find the next start code
159 *****************************************************************************/
160 static __inline__ void NextStartCode( vpar_thread_t * p_vpar )
162 /* Re-align the buffer on an 8-bit boundary */
163 RealignBits( &p_vpar->bit_stream );
165 while( ShowBits( &p_vpar->bit_stream, 24 ) != 0x01L && !p_vpar->b_die )
167 RemoveBits( &p_vpar->bit_stream, 8 );
171 /*****************************************************************************
172 * ReferenceUpdate : Update the reference pointers when we have a new picture
173 *****************************************************************************/
174 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
176 picture_t * p_newref )
178 if( i_coding_type != B_CODING_TYPE )
180 if( p_vpar->sequence.p_forward != NULL )
181 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
182 if( p_vpar->sequence.p_backward != NULL )
184 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
185 vpar_SynchroDate( p_vpar ) );
187 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
188 p_vpar->sequence.p_backward = p_newref;
189 if( p_newref != NULL )
190 vout_LinkPicture( p_vpar->p_vout, p_newref );
194 /*****************************************************************************
195 * ReferenceReplace : Replace the last reference pointer when we destroy
197 *****************************************************************************/
198 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
200 picture_t * p_newref )
202 if( i_coding_type != B_CODING_TYPE )
204 if( p_vpar->sequence.p_backward != NULL )
205 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
206 p_vpar->sequence.p_backward = p_newref;
207 if( p_newref != NULL )
208 vout_LinkPicture( p_vpar->p_vout, p_newref );
212 /*****************************************************************************
213 * LoadMatrix : Load a quantization matrix
214 *****************************************************************************/
215 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
219 if( !p_matrix->b_allocated )
221 /* Allocate a piece of memory to load the matrix. */
222 if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
224 intf_ErrMsg("vpar error: allocation error in LoadMatrix()\n");
228 p_matrix->b_allocated = 1;
231 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
233 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
234 = GetBits( &p_vpar->bit_stream, 8 );
238 /* Discrete Fourier Transform requires the quantization matrices to
239 * be normalized before using them. */
240 vdec_NormQuantMatrix( p_matrix->pi_matrix );
244 /*****************************************************************************
245 * LinkMatrix : Link a quantization matrix to another
246 *****************************************************************************/
247 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
249 if( p_matrix->b_allocated )
251 /* Deallocate the piece of memory. */
252 free( p_matrix->pi_matrix );
253 p_matrix->b_allocated = 0;
256 p_matrix->pi_matrix = pi_array;
260 * Exported functions.
263 /*****************************************************************************
264 * vpar_NextSequenceHeader : Find the next sequence header
265 *****************************************************************************/
266 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
268 while( !p_vpar->b_die )
270 NextStartCode( p_vpar );
271 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
273 RemoveBits( &p_vpar->bit_stream, 8 );
278 /*****************************************************************************
279 * vpar_ParseHeader : Parse the next header
280 *****************************************************************************/
281 int vpar_ParseHeader( vpar_thread_t * p_vpar )
283 while( !p_vpar->b_die )
285 NextStartCode( p_vpar );
286 switch( GetBits32( &p_vpar->bit_stream ) )
288 case SEQUENCE_HEADER_CODE:
289 SequenceHeader( p_vpar );
293 case GROUP_START_CODE:
294 GroupHeader( p_vpar );
298 case PICTURE_START_CODE:
299 PictureHeader( p_vpar );
303 case SEQUENCE_END_CODE:
304 intf_DbgMsg("vpar debug: sequence end code received\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 float r_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_width = GetBits( &p_vpar->bit_stream, 12 );
346 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
347 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
348 p_vpar->sequence.r_frame_rate =
349 r_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 RemoveBits( &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 /* Turn the MPEG2 flag on */
397 p_vpar->sequence.b_mpeg2 = 1;
399 /* Parse sequence_extension */
400 RemoveBits32( &p_vpar->bit_stream );
401 /* extension_start_code_identifier, profile_and_level_indication */
402 RemoveBits( &p_vpar->bit_stream, 12 );
403 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
404 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
405 p_vpar->sequence.pf_decode_pattern = ppf_chroma_pattern
406 [p_vpar->sequence.i_chroma_format];
407 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
408 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
409 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
410 RemoveBits( &p_vpar->bit_stream, 22 );
411 /* frame_rate_extension_n */
412 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
413 /* frame_rate_extension_d */
414 p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
415 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
417 p_vpar->sequence.pf_decode_mv = vpar_MPEG2MotionVector;
421 /* It's an MPEG-1 stream. Put adequate parameters. */
423 p_vpar->sequence.b_mpeg2 = 0;
424 p_vpar->sequence.b_progressive = 1;
425 p_vpar->sequence.i_chroma_format = CHROMA_420;
426 p_vpar->sequence.pf_decode_pattern = vpar_CodedPattern420;
428 p_vpar->sequence.pf_decode_mv = vpar_MPEG1MotionVector;
432 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
433 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
434 (p_vpar->sequence.i_height + 15) / 16 :
435 2 * ((p_vpar->sequence.i_height + 31) / 32);
436 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
437 * p_vpar->sequence.i_mb_height;
438 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
439 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
440 p_vpar->sequence.i_size = p_vpar->sequence.i_width
441 * p_vpar->sequence.i_height;
443 /* Update chromatic information */
444 switch( p_vpar->sequence.i_chroma_format )
447 p_vpar->sequence.i_chroma_nb_blocks = 2;
448 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
449 p_vpar->sequence.i_chroma_mb_width = 8;
450 p_vpar->sequence.i_chroma_mb_height = 8;
454 p_vpar->sequence.i_chroma_nb_blocks = 4;
455 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
456 p_vpar->sequence.i_chroma_mb_width = 8;
457 p_vpar->sequence.i_chroma_mb_height = 16;
461 p_vpar->sequence.i_chroma_nb_blocks = 8;
462 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
463 p_vpar->sequence.i_chroma_mb_width = 16;
464 p_vpar->sequence.i_chroma_mb_height = 16;
467 /* Slice Header functions */
468 if( p_vpar->sequence.i_height <= 2800 )
470 if( p_vpar->sequence.i_scalable_mode != SC_DP )
472 p_vpar->sequence.pf_slice_header = SliceHeader00;
476 p_vpar->sequence.pf_slice_header = SliceHeader01;
481 if( p_vpar->sequence.i_scalable_mode != SC_DP )
483 p_vpar->sequence.pf_slice_header = SliceHeader10;
487 p_vpar->sequence.pf_slice_header = SliceHeader11;
491 if( p_vpar->sequence.i_width != i_width_save
492 || p_vpar->sequence.i_height != i_height_save )
494 /* What do we do in case of a size change ??? */
497 /* Extension and User data */
498 ExtensionAndUserData( p_vpar );
501 /*****************************************************************************
502 * GroupHeader : Parse the next group of pictures header
503 *****************************************************************************/
504 static void GroupHeader( vpar_thread_t * p_vpar )
506 /* Nothing to do, we don't care. */
507 RemoveBits( &p_vpar->bit_stream, 27 );
508 ExtensionAndUserData( p_vpar );
511 /*****************************************************************************
512 * PictureHeader : Parse the next picture header
513 *****************************************************************************/
514 static void PictureHeader( vpar_thread_t * p_vpar )
516 static f_macroblock_type_t ppf_macroblock_type[5] = {NULL,
517 vpar_IMBType, vpar_PMBType,
518 vpar_BMBType, vpar_DMBType};
521 int i_mb_address, i_mb_base;
522 boolean_t b_parsable;
528 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
529 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
530 p_vpar->picture.pf_macroblock_type = ppf_macroblock_type
531 [p_vpar->picture.i_coding_type];
532 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
534 if( p_vpar->picture.i_coding_type == P_CODING_TYPE || p_vpar->picture.i_coding_type == B_CODING_TYPE )
536 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
537 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
539 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
541 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
542 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
545 /* extra_information_picture */
546 while( GetBits( &p_vpar->bit_stream, 1 ) )
548 RemoveBits( &p_vpar->bit_stream, 8 );
552 * Picture Coding Extension
554 NextStartCode( p_vpar );
555 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
557 /* Parse picture_coding_extension */
558 RemoveBits32( &p_vpar->bit_stream );
559 /* extension_start_code_identifier */
560 RemoveBits( &p_vpar->bit_stream, 4 );
562 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
563 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
564 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
565 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
566 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
567 i_structure = GetBits( &p_vpar->bit_stream, 2 );
568 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
569 p_vpar->picture.b_frame_pred_frame_dct
570 = GetBits( &p_vpar->bit_stream, 1 );
571 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
572 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
573 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
574 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
575 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
576 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
577 * the length of the picture_display_extension structure.
578 * chroma_420_type (obsolete) */
579 RemoveBits( &p_vpar->bit_stream, 1 );
580 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
582 /* composite_display_flag */
583 if( GetBits( &p_vpar->bit_stream, 1 ) )
585 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
586 * sub_carrier_phase */
587 RemoveBits( &p_vpar->bit_stream, 20 );
592 /* MPEG-1 compatibility flags */
593 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
594 i_structure = FRAME_STRUCTURE;
595 p_vpar->picture.b_frame_pred_frame_dct = 1;
596 p_vpar->picture.b_concealment_mv = 0;
597 p_vpar->picture.b_q_scale_type = 0;
598 p_vpar->picture.b_intra_vlc_format = 0;
599 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
600 p_vpar->picture.b_repeat_first_field = 0;
601 p_vpar->picture.b_progressive_frame = 1;
604 if( p_vpar->picture.i_current_structure &&
605 (i_structure == FRAME_STRUCTURE ||
606 i_structure == p_vpar->picture.i_current_structure) )
608 /* We don't have the second field of the buffered frame. */
609 if( p_vpar->picture.p_picture != NULL )
611 ReferenceReplace( p_vpar,
612 p_vpar->picture.i_coding_type,
616 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
618 vpar_DestroyMacroblock( &p_vpar->vfifo,
619 p_vpar->picture.pp_mb[i_mb] );
622 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
625 p_vpar->picture.i_current_structure = 0;
627 intf_DbgMsg("vpar debug: odd number of field picture.\n");
630 /* Do we have the reference pictures ? */
631 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
632 (p_vpar->sequence.p_backward == NULL)) ||
633 /* p_backward will become p_forward later */
634 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
635 (p_vpar->sequence.p_forward == NULL ||
636 p_vpar->sequence.p_backward == NULL)));
640 if( p_vpar->picture.i_current_structure )
642 /* Second field of a frame. We will decode it if, and only if we
643 * have decoded the first field. */
644 b_parsable = (p_vpar->picture.p_picture != NULL);
648 /* Does synchro say we have enough time to decode it ? */
649 b_parsable = vpar_SynchroChoose( p_vpar,
650 p_vpar->picture.i_coding_type, i_structure );
656 /* Update the reference pointers. */
657 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
659 /* Warn Synchro we have trashed a picture. */
660 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
662 /* Update context. */
663 if( i_structure != FRAME_STRUCTURE )
664 p_vpar->picture.i_current_structure = i_structure;
665 p_vpar->picture.p_picture = NULL;
670 /* OK, now we are sure we will decode the picture. */
671 #define P_picture p_vpar->picture.p_picture
672 p_vpar->picture.b_error = 0;
673 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
675 if( !p_vpar->picture.i_current_structure )
677 /* This is a new frame. Get a structure from the video_output. */
678 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
679 99+p_vpar->sequence.i_chroma_format, /*???*/
680 p_vpar->sequence.i_width,
681 p_vpar->sequence.i_height ) )
684 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture\n");
685 if( p_vpar->b_die || p_vpar->b_error )
689 msleep( VPAR_OUTMEM_SLEEP );
692 /* Initialize values. */
693 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
694 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
696 /* Put date immediately. */
697 vout_DatePicture( p_vpar->p_vout, P_picture,
698 vpar_SynchroDate( p_vpar ) );
700 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
701 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
702 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
703 << ( 1 - p_vpar->picture.b_frame_structure ) );
704 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
705 << ( 1 - p_vpar->picture.b_frame_structure ));
707 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
708 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
710 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
712 /* FIXME ! remove asap */
713 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
715 /* Update the reference pointers. */
716 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
719 /* Link referenced pictures for the decoder
720 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
721 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
722 p_vpar->picture.i_coding_type == B_CODING_TYPE )
724 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
726 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
728 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
732 p_vpar->picture.i_current_structure |= i_structure;
733 p_vpar->picture.i_structure = i_structure;
735 /* Initialize picture data for decoding. */
736 if( (p_vpar->picture.b_motion_field = (i_structure == BOTTOM_FIELD)) )
738 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
739 p_vpar->mb.i_l_y = 1;
740 p_vpar->mb.i_c_y = 1;
745 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
748 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
750 /* Extension and User data. */
751 ExtensionAndUserData( p_vpar );
753 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
754 NextStartCode( p_vpar );
755 while( i_mb_address+i_mb_base < p_vpar->sequence.i_mb_size
756 && !p_vpar->picture.b_error && !p_vpar->b_die )
758 if( ((i_dummy = ShowBits( &p_vpar->bit_stream, 32 ))
759 < SLICE_START_CODE_MIN) ||
760 (i_dummy > SLICE_START_CODE_MAX) )
762 intf_DbgMsg("vpar debug: premature end of picture\n");
763 p_vpar->picture.b_error = 1;
766 RemoveBits32( &p_vpar->bit_stream );
768 /* Decode slice data. */
769 p_vpar->sequence.pf_slice_header( p_vpar, &i_mb_address, i_mb_base, i_dummy & 255 );
772 if( p_vpar->picture.b_error )
775 //fprintf(stderr, "Image trashee\n");
777 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
779 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
783 if( P_picture->i_deccount != 1 )
785 vout_DestroyPicture( p_vpar->p_vout, P_picture );
788 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
790 /* Prepare context for the next picture. */
792 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
793 p_vpar->picture.i_current_structure = 0;
795 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
797 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
798 /* Frame completely parsed. */
800 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
802 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
805 /* Send signal to the video_decoder. */
806 vlc_mutex_lock( &p_vpar->vfifo.lock );
807 vlc_cond_signal( &p_vpar->vfifo.wait );
808 vlc_mutex_unlock( &p_vpar->vfifo.lock );
811 /* Prepare context for the next picture. */
813 p_vpar->picture.i_current_structure = 0;
818 /*****************************************************************************
819 * SliceHeader : Parse the next slice structure
820 *****************************************************************************/
821 static __inline__ void SliceHeader( vpar_thread_t * p_vpar,
822 int * pi_mb_address, int i_mb_base,
825 /* DC predictors initialization table */
826 static int pi_dc_dct_reinit[4] = {128,256,512,1024};
828 int i_mb_address_save = *pi_mb_address;
830 /* slice_vertical_position_extension and priority_breakpoint already done */
831 LoadQuantizerScale( p_vpar );
833 if( GetBits( &p_vpar->bit_stream, 1 ) )
835 /* intra_slice, slice_id */
836 RemoveBits( &p_vpar->bit_stream, 8 );
837 /* extra_information_slice */
838 while( GetBits( &p_vpar->bit_stream, 1 ) )
840 RemoveBits( &p_vpar->bit_stream, 8 );
843 *pi_mb_address = (i_vert_code - 1)*p_vpar->sequence.i_mb_width;
845 /* Reset DC coefficients predictors (ISO/IEC 13818-2 7.2.1). Why
846 * does the reference decoder put 0 instead of the normative values ? */
847 p_vpar->slice.pi_dc_dct_pred[0] = p_vpar->slice.pi_dc_dct_pred[1]
848 = p_vpar->slice.pi_dc_dct_pred[2]
849 = pi_dc_dct_reinit[p_vpar->picture.i_intra_dc_precision];
851 /* Reset motion vector predictors (ISO/IEC 13818-2 7.6.3.4). */
852 memset( p_vpar->slice.pppi_pmv, 0, 8*sizeof(int) );
856 vpar_ParseMacroblock( p_vpar, pi_mb_address, i_mb_address_save,
858 i_mb_address_save = *pi_mb_address;
860 while( ShowBits( &p_vpar->bit_stream, 23 ) && !p_vpar->b_die );
861 NextStartCode( p_vpar );
864 /*****************************************************************************
865 * SliceHeaderXY : Parse the next slice structure
866 *****************************************************************************
867 * X = i_height > 2800 ?
868 * Y = scalable_mode == SC_DP ?
869 *****************************************************************************/
870 static void SliceHeader00( vpar_thread_t * p_vpar,
871 int * pi_mb_address, int i_mb_base,
874 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
877 static void SliceHeader01( vpar_thread_t * p_vpar,
878 int * pi_mb_address, int i_mb_base,
881 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
882 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
885 static void SliceHeader10( vpar_thread_t * p_vpar,
886 int * pi_mb_address, int i_mb_base,
889 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
890 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
893 static void SliceHeader11( vpar_thread_t * p_vpar,
894 int * pi_mb_address, int i_mb_base,
897 i_vert_code += GetBits( &p_vpar->bit_stream, 3 ) << 7;
898 RemoveBits( &p_vpar->bit_stream, 7 ); /* priority_breakpoint */
899 SliceHeader( p_vpar, pi_mb_address, i_mb_base, i_vert_code );
902 /*****************************************************************************
903 * ExtensionAndUserData : Parse the extension_and_user_data structure
904 *****************************************************************************/
905 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
907 while( !p_vpar->b_die )
909 NextStartCode( p_vpar );
910 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
912 case EXTENSION_START_CODE:
913 RemoveBits32( &p_vpar->bit_stream );
914 switch( GetBits( &p_vpar->bit_stream, 4 ) )
916 case SEQUENCE_DISPLAY_EXTENSION_ID:
917 SequenceDisplayExtension( p_vpar );
919 case QUANT_MATRIX_EXTENSION_ID:
920 QuantMatrixExtension( p_vpar );
922 case SEQUENCE_SCALABLE_EXTENSION_ID:
923 SequenceScalableExtension( p_vpar );
925 case PICTURE_DISPLAY_EXTENSION_ID:
926 PictureDisplayExtension( p_vpar );
928 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
929 PictureSpatialScalableExtension( p_vpar );
931 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
932 PictureTemporalScalableExtension( p_vpar );
934 case COPYRIGHT_EXTENSION_ID:
935 CopyrightExtension( p_vpar );
941 case USER_DATA_START_CODE:
942 RemoveBits32( &p_vpar->bit_stream );
943 /* Wait for the next start code */
953 /*****************************************************************************
954 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
955 *****************************************************************************/
957 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
959 /* We don't care sequence_display_extension. */
961 RemoveBits( &p_vpar->bit_stream, 3 );
962 if( GetBits( &p_vpar->bit_stream, 1 ) )
964 /* Two bytes for color_desciption */
965 RemoveBits( &p_vpar->bit_stream, 16 );
966 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
968 /* display_horizontal and vertical_size and a marker_bit */
969 RemoveBits( &p_vpar->bit_stream, 29 );
973 /*****************************************************************************
974 * QuantMatrixExtension : Load quantization matrices for luminance *
976 *****************************************************************************/
978 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
980 if( GetBits( &p_vpar->bit_stream, 1 ) )
982 /* Load intra_quantiser_matrix for luminance. */
983 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
987 /* Use the default matrix. */
988 LinkMatrix( &p_vpar->sequence.intra_quant,
989 pi_default_intra_quant );
991 if( GetBits( &p_vpar->bit_stream, 1 ) )
993 /* Load non_intra_quantiser_matrix for luminance. */
994 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
998 /* Use the default matrix. */
999 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1000 pi_default_nonintra_quant );
1002 if( GetBits( &p_vpar->bit_stream, 1 ) )
1004 /* Load intra_quantiser_matrix for chrominance. */
1005 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1009 /* Link the chrominance intra matrix to the luminance one. */
1010 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1011 p_vpar->sequence.intra_quant.pi_matrix );
1013 if( GetBits( &p_vpar->bit_stream, 1 ) )
1015 /* Load non_intra_quantiser_matrix for chrominance. */
1016 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1020 /* Link the chrominance intra matrix to the luminance one. */
1021 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1022 p_vpar->sequence.intra_quant.pi_matrix );
1024 if( GetBits( &p_vpar->bit_stream, 1 ) )
1026 /* Load non_intra_quantiser_matrix for chrominance. */
1027 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1031 /* Link the chrominance nonintra matrix to the luminance one. */
1032 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1033 p_vpar->sequence.nonintra_quant.pi_matrix );
1038 /*****************************************************************************
1039 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1040 * structure to handle scalable coding *
1041 *****************************************************************************/
1043 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1045 /* We don't care about anything scalable except the scalable mode. */
1046 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1047 /* The length of the structure depends on the value of the scalable_mode */
1050 RemoveBits32( &p_vpar->bit_stream );
1051 RemoveBits( &p_vpar->bit_stream, 21 );
1054 RemoveBits( &p_vpar->bit_stream, 12 );
1057 RemoveBits( &p_vpar->bit_stream, 4 );
1061 /*****************************************************************************
1062 * PictureDisplayExtension : Parse the picture_display_extension structure *
1063 *****************************************************************************/
1065 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1067 /* Number of frame center offset */
1069 /* I am not sure it works but it should
1070 (fewer tests than shown in §6.3.12) */
1071 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1072 p_vpar->picture.b_repeat_first_field +
1073 p_vpar->picture.b_top_field_first
1074 : ( p_vpar->picture.b_frame_structure + 1 ) +
1075 p_vpar->picture.b_repeat_first_field;
1076 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1078 RemoveBits( &p_vpar->bit_stream, 17 );
1079 RemoveBits( &p_vpar->bit_stream, 17 );
1084 /*****************************************************************************
1085 * PictureSpatialScalableExtension *
1086 *****************************************************************************/
1088 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1090 /* That's scalable, so we trash it */
1091 RemoveBits32( &p_vpar->bit_stream );
1092 RemoveBits( &p_vpar->bit_stream, 16 );
1096 /*****************************************************************************
1097 * PictureTemporalScalableExtension *
1098 *****************************************************************************/
1100 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1102 /* Scalable again, trashed again */
1103 RemoveBits( &p_vpar->bit_stream, 23 );
1107 /*****************************************************************************
1108 * CopyrightExtension : Keeps some legal informations *
1109 *****************************************************************************/
1111 static void CopyrightExtension( vpar_thread_t * p_vpar )
1113 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1114 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1115 /* A flag that says whether the copyright information is significant */
1116 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1117 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1118 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1120 RemoveBits( &p_vpar->bit_stream, 8 );
1121 /* The copyright_number is split in three parts */
1123 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1124 RemoveBits( &p_vpar->bit_stream, 1 );
1126 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1127 RemoveBits( &p_vpar->bit_stream, 1 );
1128 /* third part and sum */
1129 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1130 ( (u64)i_copyright_nb_2 << 22 ) |
1131 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );