1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
28 #include <sys/types.h> /* on BSD, uio.h needs types.h */
29 #include <sys/uio.h> /* "input.h" */
39 #include "decoder_fifo.h"
41 #include "video_output.h"
43 #include "vdec_idct.h"
44 #include "video_decoder.h"
45 #include "vdec_motion.h"
47 #include "vpar_blocks.h"
48 #include "vpar_headers.h"
49 #include "vpar_synchro.h"
50 #include "video_parser.h"
51 #include "video_fifo.h"
56 typedef void (*f_picture_data_t)( vpar_thread_t*, int );
61 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
62 static void SequenceHeader( vpar_thread_t * p_vpar );
63 static void GroupHeader( vpar_thread_t * p_vpar );
64 static void PictureHeader( vpar_thread_t * p_vpar );
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 * ReferenceUpdate : Update the reference pointers when we have a new picture
161 *****************************************************************************/
162 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
164 picture_t * p_newref )
166 if( i_coding_type != B_CODING_TYPE )
168 if( p_vpar->sequence.p_forward != NULL )
169 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
170 if( p_vpar->sequence.p_backward != NULL )
173 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
174 vpar_SynchroDate( p_vpar ) );
177 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
178 vpar_SynchroDate( p_vpar ) );
182 date = vpar_SynchroDate( p_vpar );
183 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
185 if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
186 vpar_SynchroKludge( p_vpar, date );
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 );
194 p_vpar->synchro.i_coding_type = i_coding_type;
197 else if( p_newref != NULL )
199 /* Put date immediately. */
200 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
204 /*****************************************************************************
205 * ReferenceReplace : Replace the last reference pointer when we destroy
207 *****************************************************************************/
208 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
210 picture_t * p_newref )
212 if( i_coding_type != B_CODING_TYPE )
214 if( p_vpar->sequence.p_backward != NULL )
215 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
216 p_vpar->sequence.p_backward = p_newref;
217 if( p_newref != NULL )
218 vout_LinkPicture( p_vpar->p_vout, p_newref );
222 /*****************************************************************************
223 * LoadMatrix : Load a quantization matrix
224 *****************************************************************************/
225 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
229 if( !p_matrix->b_allocated )
231 /* Allocate a piece of memory to load the matrix. */
232 if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
234 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
238 p_matrix->b_allocated = 1;
241 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
243 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
244 = GetBits( &p_vpar->bit_stream, 8 );
248 /* Discrete Fourier Transform requires the quantization matrices to
249 * be normalized before using them. */
250 vdec_NormQuantMatrix( p_matrix->pi_matrix );
254 /*****************************************************************************
255 * LinkMatrix : Link a quantization matrix to another
256 *****************************************************************************/
257 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
259 if( p_matrix->b_allocated )
261 /* Deallocate the piece of memory. */
262 free( p_matrix->pi_matrix );
263 p_matrix->b_allocated = 0;
266 p_matrix->pi_matrix = pi_array;
270 * Exported functions.
273 /*****************************************************************************
274 * vpar_NextSequenceHeader : Find the next sequence header
275 *****************************************************************************/
276 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
278 while( !p_vpar->b_die )
280 NextStartCode( p_vpar );
281 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
283 RemoveBits( &p_vpar->bit_stream, 8 );
288 /*****************************************************************************
289 * vpar_ParseHeader : Parse the next header
290 *****************************************************************************/
291 int vpar_ParseHeader( vpar_thread_t * p_vpar )
293 while( !p_vpar->b_die )
295 NextStartCode( p_vpar );
296 switch( GetBits32( &p_vpar->bit_stream ) )
298 case SEQUENCE_HEADER_CODE:
299 SequenceHeader( p_vpar );
303 case GROUP_START_CODE:
304 GroupHeader( p_vpar );
308 case PICTURE_START_CODE:
309 PictureHeader( p_vpar );
313 case SEQUENCE_END_CODE:
314 intf_DbgMsg("vpar debug: sequence end code received\n");
326 * Following functions are local
329 /*****************************************************************************
330 * SequenceHeader : Parse the next sequence header
331 *****************************************************************************/
332 static void SequenceHeader( vpar_thread_t * p_vpar )
335 static float r_frame_rate_table[16] =
338 ((23.0*1000.0)/1001.0),
341 ((30.0*1000.0)/1001.0),
344 ((60.0*1000.0)/1001.0),
346 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
350 int i_height_save, i_width_save;
352 i_height_save = p_vpar->sequence.i_height;
353 i_width_save = p_vpar->sequence.i_width;
355 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
356 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
357 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
358 p_vpar->sequence.r_frame_rate =
359 r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
361 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
362 * constrained_parameters_flag */
363 RemoveBits( &p_vpar->bit_stream, 30 );
366 * Quantization matrices
368 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
370 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
374 /* Use default matrix. */
375 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
378 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
380 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
384 /* Use default matrix. */
385 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
388 /* Unless later overwritten by a matrix extension, we have the same
389 * matrices for luminance and chrominance. */
390 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
391 p_vpar->sequence.intra_quant.pi_matrix );
392 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
393 p_vpar->sequence.nonintra_quant.pi_matrix );
398 NextStartCode( p_vpar );
399 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
403 /* Turn the MPEG2 flag on */
404 p_vpar->sequence.b_mpeg2 = 1;
406 /* Parse sequence_extension */
407 RemoveBits32( &p_vpar->bit_stream );
408 /* extension_start_code_identifier, profile_and_level_indication */
409 RemoveBits( &p_vpar->bit_stream, 12 );
410 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
411 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
412 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
413 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
414 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
415 RemoveBits( &p_vpar->bit_stream, 22 );
416 /* frame_rate_extension_n */
417 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
418 /* frame_rate_extension_d */
419 p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
420 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
424 /* It's an MPEG-1 stream. Put adequate parameters. */
426 p_vpar->sequence.b_mpeg2 = 0;
427 p_vpar->sequence.b_progressive = 1;
428 p_vpar->sequence.i_chroma_format = CHROMA_420;
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 /* Reset scalable_mode. */
468 p_vpar->sequence.i_scalable_mode = SC_NONE;
471 if( p_vpar->sequence.i_width != i_width_save
472 || p_vpar->sequence.i_height != i_height_save )
474 /* FIXME: What do we do in case of a size change ?? */
478 /* Extension and User data */
479 ExtensionAndUserData( p_vpar );
482 /*****************************************************************************
483 * GroupHeader : Parse the next group of pictures header
484 *****************************************************************************/
485 static void GroupHeader( vpar_thread_t * p_vpar )
487 /* Nothing to do, we don't care. */
488 RemoveBits( &p_vpar->bit_stream, 27 );
489 ExtensionAndUserData( p_vpar );
492 /*****************************************************************************
493 * PictureHeader : Parse the next picture header
494 *****************************************************************************/
495 static void PictureHeader( vpar_thread_t * p_vpar )
497 /* Table of optimized PictureData functions. */
498 static f_picture_data_t ppf_picture_data[4][4] =
501 NULL, NULL, NULL, NULL
505 #if (VPAR_OPTIM_LEVEL > 1)
506 NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
507 vpar_PictureData2B420TZ
509 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
510 vpar_PictureDataGENERIC
515 #if (VPAR_OPTIM_LEVEL > 1)
516 NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
517 vpar_PictureData2B420BZ
519 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
520 vpar_PictureDataGENERIC
525 #if (VPAR_OPTIM_LEVEL > 0)
526 NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
527 vpar_PictureData2B420F0
529 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
530 vpar_PictureDataGENERIC
537 boolean_t b_parsable;
542 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
543 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
544 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
546 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
547 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
549 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
550 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
552 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
554 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
555 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
558 /* extra_information_picture */
559 while( GetBits( &p_vpar->bit_stream, 1 ) )
561 RemoveBits( &p_vpar->bit_stream, 8 );
565 * Picture Coding Extension
567 NextStartCode( p_vpar );
568 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
570 /* Parse picture_coding_extension */
571 RemoveBits32( &p_vpar->bit_stream );
572 /* extension_start_code_identifier */
573 RemoveBits( &p_vpar->bit_stream, 4 );
575 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
576 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
577 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
578 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
579 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
580 i_structure = GetBits( &p_vpar->bit_stream, 2 );
581 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
582 p_vpar->picture.b_frame_pred_frame_dct
583 = GetBits( &p_vpar->bit_stream, 1 );
584 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
585 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
586 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
587 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
588 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
589 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
590 * the length of the picture_display_extension structure.
591 * chroma_420_type (obsolete) */
592 RemoveBits( &p_vpar->bit_stream, 1 );
593 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
595 /* composite_display_flag */
596 if( GetBits( &p_vpar->bit_stream, 1 ) )
598 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
599 * sub_carrier_phase */
600 RemoveBits( &p_vpar->bit_stream, 20 );
605 /* MPEG-1 compatibility flags */
606 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
607 i_structure = FRAME_STRUCTURE;
608 p_vpar->picture.b_frame_pred_frame_dct = 1;
609 p_vpar->picture.b_concealment_mv = 0;
610 p_vpar->picture.b_q_scale_type = 0;
611 p_vpar->picture.b_intra_vlc_format = 0;
612 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
613 p_vpar->picture.b_repeat_first_field = 0;
614 p_vpar->picture.b_progressive_frame = 1;
617 if( p_vpar->picture.i_current_structure &&
618 (i_structure == FRAME_STRUCTURE ||
619 i_structure == p_vpar->picture.i_current_structure) )
621 /* We don't have the second field of the buffered frame. */
622 if( p_vpar->picture.p_picture != NULL )
624 ReferenceReplace( p_vpar,
625 p_vpar->picture.i_coding_type,
629 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
631 vpar_DestroyMacroblock( &p_vpar->vfifo,
632 p_vpar->picture.pp_mb[i_mb] );
635 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
638 p_vpar->picture.i_current_structure = 0;
640 intf_DbgMsg("vpar debug: odd number of field picture.\n");
643 /* Do we have the reference pictures ? */
644 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
645 (p_vpar->sequence.p_backward == NULL)) ||
646 /* p_backward will become p_forward later */
647 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
648 (p_vpar->sequence.p_forward == NULL ||
649 p_vpar->sequence.p_backward == NULL)));
653 if( p_vpar->picture.i_current_structure )
655 /* Second field of a frame. We will decode it if, and only if we
656 * have decoded the first field. */
657 b_parsable = (p_vpar->picture.p_picture != NULL);
661 /* Does synchro say we have enough time to decode it ? */
662 b_parsable = vpar_SynchroChoose( p_vpar,
663 p_vpar->picture.i_coding_type, i_structure );
667 else if( !p_vpar->picture.i_current_structure )
669 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
675 /* Update the reference pointers. */
676 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
677 #ifndef POLUX_SYNCHRO
678 /* Warn Synchro we have trashed a picture. */
679 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
681 /* Update context. */
682 if( i_structure != FRAME_STRUCTURE )
683 p_vpar->picture.i_current_structure = i_structure;
684 p_vpar->picture.p_picture = NULL;
689 /* OK, now we are sure we will decode the picture. */
690 #define P_picture p_vpar->picture.p_picture
691 p_vpar->picture.b_error = 0;
692 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
694 if( !p_vpar->picture.i_current_structure )
696 /* This is a new frame. Get a structure from the video_output. */
697 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
698 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
699 p_vpar->sequence.i_width,
700 p_vpar->sequence.i_height ) )
703 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
704 if( p_vpar->b_die || p_vpar->b_error )
708 msleep( VPAR_OUTMEM_SLEEP );
711 /* Initialize values. */
712 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
713 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
714 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
715 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
716 << ( 1 - p_vpar->picture.b_frame_structure ) );
717 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
718 << ( 1 - p_vpar->picture.b_frame_structure ));
720 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
721 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
723 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
725 /* FIXME ! remove asap ?? */
726 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
728 /* Update the reference pointers. */
729 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
732 /* Link referenced pictures for the decoder
733 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
734 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
735 p_vpar->picture.i_coding_type == B_CODING_TYPE )
737 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
739 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
741 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
745 p_vpar->picture.i_current_structure |= i_structure;
746 p_vpar->picture.i_structure = i_structure;
748 /* Initialize picture data for decoding. */
749 if( i_structure == BOTTOM_FIELD )
751 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
752 p_vpar->mb.i_l_y = 1;
753 p_vpar->mb.i_c_y = 1;
758 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
760 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
762 /* Extension and User data. */
763 ExtensionAndUserData( p_vpar );
765 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
766 if( p_vpar->sequence.i_chroma_format != CHROMA_420
767 || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
768 || p_vpar->sequence.i_scalable_mode == SC_DP )
770 /* Weird stream. Use the slower generic function. */
771 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
775 /* Try to find an optimized function. */
776 if( ppf_picture_data[p_vpar->picture.i_structure][p_vpar->picture.i_coding_type] == NULL )
778 intf_ErrMsg( "vpar error: bad ppf_picture_data function pointer (struct:%d, coding type:%d)\n",
779 p_vpar->picture.i_structure, p_vpar->picture.i_coding_type );
783 ppf_picture_data[p_vpar->picture.i_structure]
784 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
788 if( p_vpar->b_die || p_vpar->b_error )
793 if( p_vpar->picture.b_error )
796 //fprintf(stderr, "Image trashee\n");
798 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
800 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
804 if( P_picture->i_deccount != 1 )
806 vout_DestroyPicture( p_vpar->p_vout, P_picture );
809 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
811 /* Prepare context for the next picture. */
813 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
814 p_vpar->picture.i_current_structure = 0;
816 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
818 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
819 /* Frame completely parsed. */
821 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
823 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
826 /* Send signal to the video_decoder. */
827 vlc_mutex_lock( &p_vpar->vfifo.lock );
828 vlc_cond_signal( &p_vpar->vfifo.wait );
829 vlc_mutex_unlock( &p_vpar->vfifo.lock );
832 /* Prepare context for the next picture. */
834 p_vpar->picture.i_current_structure = 0;
839 /*****************************************************************************
840 * ExtensionAndUserData : Parse the extension_and_user_data structure
841 *****************************************************************************/
842 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
844 while( !p_vpar->b_die )
846 NextStartCode( p_vpar );
847 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
849 case EXTENSION_START_CODE:
850 RemoveBits32( &p_vpar->bit_stream );
851 switch( GetBits( &p_vpar->bit_stream, 4 ) )
853 case SEQUENCE_DISPLAY_EXTENSION_ID:
854 SequenceDisplayExtension( p_vpar );
856 case QUANT_MATRIX_EXTENSION_ID:
857 QuantMatrixExtension( p_vpar );
859 case SEQUENCE_SCALABLE_EXTENSION_ID:
860 SequenceScalableExtension( p_vpar );
862 case PICTURE_DISPLAY_EXTENSION_ID:
863 PictureDisplayExtension( p_vpar );
865 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
866 PictureSpatialScalableExtension( p_vpar );
868 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
869 PictureTemporalScalableExtension( p_vpar );
871 case COPYRIGHT_EXTENSION_ID:
872 CopyrightExtension( p_vpar );
878 case USER_DATA_START_CODE:
879 RemoveBits32( &p_vpar->bit_stream );
880 /* Wait for the next start code */
890 /*****************************************************************************
891 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
892 *****************************************************************************/
894 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
896 /* We don't care sequence_display_extension. */
898 RemoveBits( &p_vpar->bit_stream, 3 );
899 if( GetBits( &p_vpar->bit_stream, 1 ) )
901 /* Two bytes for color_desciption */
902 RemoveBits( &p_vpar->bit_stream, 16 );
903 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
905 /* display_horizontal and vertical_size and a marker_bit */
906 RemoveBits( &p_vpar->bit_stream, 29 );
910 /*****************************************************************************
911 * QuantMatrixExtension : Load quantization matrices for luminance *
913 *****************************************************************************/
915 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
917 if( GetBits( &p_vpar->bit_stream, 1 ) )
919 /* Load intra_quantiser_matrix for luminance. */
920 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
924 /* Use the default matrix. */
925 LinkMatrix( &p_vpar->sequence.intra_quant,
926 pi_default_intra_quant );
928 if( GetBits( &p_vpar->bit_stream, 1 ) )
930 /* Load non_intra_quantiser_matrix for luminance. */
931 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
935 /* Use the default matrix. */
936 LinkMatrix( &p_vpar->sequence.nonintra_quant,
937 pi_default_nonintra_quant );
939 if( GetBits( &p_vpar->bit_stream, 1 ) )
941 /* Load intra_quantiser_matrix for chrominance. */
942 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
946 /* Link the chrominance intra matrix to the luminance one. */
947 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
948 p_vpar->sequence.intra_quant.pi_matrix );
950 if( GetBits( &p_vpar->bit_stream, 1 ) )
952 /* Load non_intra_quantiser_matrix for chrominance. */
953 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
957 /* Link the chrominance intra matrix to the luminance one. */
958 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
959 p_vpar->sequence.intra_quant.pi_matrix );
961 if( GetBits( &p_vpar->bit_stream, 1 ) )
963 /* Load non_intra_quantiser_matrix for chrominance. */
964 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
968 /* Link the chrominance nonintra matrix to the luminance one. */
969 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
970 p_vpar->sequence.nonintra_quant.pi_matrix );
975 /*****************************************************************************
976 * SequenceScalableExtension : Parse the sequence_scalable_extension *
977 * structure to handle scalable coding *
978 *****************************************************************************/
980 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
982 /* We don't care about anything scalable except the scalable mode. */
983 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
984 /* The length of the structure depends on the value of the scalable_mode */
987 RemoveBits32( &p_vpar->bit_stream );
988 RemoveBits( &p_vpar->bit_stream, 21 );
991 RemoveBits( &p_vpar->bit_stream, 12 );
994 RemoveBits( &p_vpar->bit_stream, 4 );
998 /*****************************************************************************
999 * PictureDisplayExtension : Parse the picture_display_extension structure *
1000 *****************************************************************************/
1002 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1004 /* Number of frame center offset */
1006 /* I am not sure it works but it should
1007 (fewer tests than shown in §6.3.12) */
1008 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1009 p_vpar->picture.b_repeat_first_field +
1010 p_vpar->picture.b_top_field_first
1011 : ( p_vpar->picture.b_frame_structure + 1 ) +
1012 p_vpar->picture.b_repeat_first_field;
1013 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1015 RemoveBits( &p_vpar->bit_stream, 17 );
1016 RemoveBits( &p_vpar->bit_stream, 17 );
1021 /*****************************************************************************
1022 * PictureSpatialScalableExtension *
1023 *****************************************************************************/
1025 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1027 /* That's scalable, so we trash it */
1028 RemoveBits32( &p_vpar->bit_stream );
1029 RemoveBits( &p_vpar->bit_stream, 16 );
1033 /*****************************************************************************
1034 * PictureTemporalScalableExtension *
1035 *****************************************************************************/
1037 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1039 /* Scalable again, trashed again */
1040 RemoveBits( &p_vpar->bit_stream, 23 );
1044 /*****************************************************************************
1045 * CopyrightExtension : Keeps some legal informations *
1046 *****************************************************************************/
1048 static void CopyrightExtension( vpar_thread_t * p_vpar )
1050 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1051 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1052 /* A flag that says whether the copyright information is significant */
1053 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1054 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1055 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1057 RemoveBits( &p_vpar->bit_stream, 8 );
1058 /* The copyright_number is split in three parts */
1060 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1061 RemoveBits( &p_vpar->bit_stream, 1 );
1063 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1064 RemoveBits( &p_vpar->bit_stream, 1 );
1065 /* third part and sum */
1066 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1067 ( (u64)i_copyright_nb_2 << 22 ) |
1068 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );