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" */
40 #include "decoder_fifo.h"
42 #include "video_output.h"
44 #include "vdec_idct.h"
45 #include "video_decoder.h"
46 #include "vdec_motion.h"
48 #include "vpar_blocks.h"
49 #include "vpar_headers.h"
50 #include "vpar_synchro.h"
51 #include "video_parser.h"
52 #include "video_fifo.h"
57 typedef void (*f_picture_data_t)( vpar_thread_t*, int );
62 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
63 static void SequenceHeader( vpar_thread_t * p_vpar );
64 static void GroupHeader( vpar_thread_t * p_vpar );
65 static void PictureHeader( vpar_thread_t * p_vpar );
66 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
67 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
68 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
69 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
70 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
71 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
72 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
73 static void CopyrightExtension( vpar_thread_t * p_vpar );
79 /*****************************************************************************
80 * pi_default_intra_quant : default quantization matrix
81 *****************************************************************************/
83 int pi_default_intra_quant[] =
85 8, 16, 19, 22, 26, 27, 29, 34,
86 16, 16, 22, 24, 27, 29, 34, 37,
87 19, 22, 26, 27, 29, 34, 34, 38,
88 22, 22, 26, 27, 29, 34, 37, 40,
89 22, 26, 27, 29, 32, 35, 40, 48,
90 26, 27, 29, 32, 35, 40, 48, 58,
91 26, 27, 29, 34, 38, 46, 56, 69,
92 27, 29, 35, 38, 46, 56, 69, 83
95 int pi_default_intra_quant[] =
97 2048, 5681, 6355, 6623, 6656, 5431, 4018, 2401,
98 5681, 7880, 10207, 10021, 9587, 8091, 6534, 3625,
99 6355, 10207, 11363, 10619, 9700, 8935, 6155, 3507,
100 6623, 9186, 10226, 9557, 8730, 8041, 6028, 3322,
101 5632, 9232, 9031, 8730, 8192, 7040, 5542, 3390,
102 5230, 7533, 7621, 7568, 7040, 6321, 5225, 3219,
103 3602, 5189, 5250, 5539, 5265, 5007, 4199, 2638,
104 1907, 2841, 3230, 3156, 3249, 3108, 2638, 1617
108 /*****************************************************************************
109 * pi_default_nonintra_quant : default quantization matrix
110 *****************************************************************************/
112 int pi_default_nonintra_quant[] =
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,
121 16, 16, 16, 16, 16, 16, 16, 16
124 int pi_default_nonintra_quanit[] =
126 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
127 5680, 7888, 7424, 6688, 5680, 4464, 3072, 1568,
128 5344, 7424, 6992, 6288, 5344, 4208, 2896, 1472,
129 4816, 6688, 6288, 5664, 4816, 3792, 2608, 1328,
130 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
131 3216, 4464, 4208, 3792, 3216, 2528, 1744, 880,
132 2224, 3072, 2896, 2608, 2224, 1744, 1200, 608,
133 1136, 1568, 1472, 1328, 1136, 880, 608, 304
137 /*****************************************************************************
138 * pi_scan : zig-zag and alternate scan patterns
139 *****************************************************************************/
142 { /* Zig-Zag pattern */
143 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
144 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
145 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
146 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
148 { /* Alternate scan pattern */
149 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
150 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
151 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
152 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
157 * Local inline functions.
160 /*****************************************************************************
161 * ReferenceUpdate : Update the reference pointers when we have a new picture
162 *****************************************************************************/
163 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
165 picture_t * p_newref )
167 if( i_coding_type != B_CODING_TYPE )
169 if( p_vpar->sequence.p_forward != NULL )
170 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
171 if( p_vpar->sequence.p_backward != NULL )
174 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
175 vpar_SynchroDate( p_vpar ) );
178 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
179 vpar_SynchroDate( p_vpar ) );
183 date = vpar_SynchroDate( p_vpar );
184 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
186 if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
187 vpar_SynchroKludge( p_vpar, date );
190 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
191 p_vpar->sequence.p_backward = p_newref;
192 if( p_newref != NULL )
193 vout_LinkPicture( p_vpar->p_vout, p_newref );
195 p_vpar->synchro.i_coding_type = i_coding_type;
198 else if( p_newref != NULL )
200 /* Put date immediately. */
201 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
205 /*****************************************************************************
206 * ReferenceReplace : Replace the last reference pointer when we destroy
208 *****************************************************************************/
209 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
211 picture_t * p_newref )
213 if( i_coding_type != B_CODING_TYPE )
215 if( p_vpar->sequence.p_backward != NULL )
216 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
217 p_vpar->sequence.p_backward = p_newref;
218 if( p_newref != NULL )
219 vout_LinkPicture( p_vpar->p_vout, p_newref );
223 /*****************************************************************************
224 * LoadMatrix : Load a quantization matrix
225 *****************************************************************************/
226 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
230 if( !p_matrix->b_allocated )
232 /* Allocate a piece of memory to load the matrix. */
233 if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
235 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
239 p_matrix->b_allocated = 1;
242 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
244 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
245 = GetBits( &p_vpar->bit_stream, 8 );
249 /* Discrete Fourier Transform requires the quantization matrices to
250 * be normalized before using them. */
251 vdec_NormQuantMatrix( p_matrix->pi_matrix );
255 /*****************************************************************************
256 * LinkMatrix : Link a quantization matrix to another
257 *****************************************************************************/
258 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
260 if( p_matrix->b_allocated )
262 /* Deallocate the piece of memory. */
263 free( p_matrix->pi_matrix );
264 p_matrix->b_allocated = 0;
267 p_matrix->pi_matrix = pi_array;
271 * Exported functions.
274 /*****************************************************************************
275 * vpar_NextSequenceHeader : Find the next sequence header
276 *****************************************************************************/
277 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
279 while( !p_vpar->b_die )
281 NextStartCode( p_vpar );
282 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
284 RemoveBits( &p_vpar->bit_stream, 8 );
289 /*****************************************************************************
290 * vpar_ParseHeader : Parse the next header
291 *****************************************************************************/
292 int vpar_ParseHeader( vpar_thread_t * p_vpar )
294 while( !p_vpar->b_die )
296 NextStartCode( p_vpar );
297 switch( GetBits32( &p_vpar->bit_stream ) )
299 case SEQUENCE_HEADER_CODE:
300 SequenceHeader( p_vpar );
304 case GROUP_START_CODE:
305 GroupHeader( p_vpar );
309 case PICTURE_START_CODE:
310 PictureHeader( p_vpar );
314 case SEQUENCE_END_CODE:
315 intf_DbgMsg("vpar debug: sequence end code received\n");
327 * Following functions are local
330 /*****************************************************************************
331 * SequenceHeader : Parse the next sequence header
332 *****************************************************************************/
333 static void SequenceHeader( vpar_thread_t * p_vpar )
336 static float r_frame_rate_table[16] =
339 ((23.0*1000.0)/1001.0),
342 ((30.0*1000.0)/1001.0),
345 ((60.0*1000.0)/1001.0),
347 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
351 int i_height_save, i_width_save;
353 i_height_save = p_vpar->sequence.i_height;
354 i_width_save = p_vpar->sequence.i_width;
356 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
357 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
358 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
359 p_vpar->sequence.r_frame_rate =
360 r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
362 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
363 * constrained_parameters_flag */
364 RemoveBits( &p_vpar->bit_stream, 30 );
367 * Quantization matrices
369 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
371 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
375 /* Use default matrix. */
376 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
379 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
381 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
385 /* Use default matrix. */
386 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
389 /* Unless later overwritten by a matrix extension, we have the same
390 * matrices for luminance and chrominance. */
391 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
392 p_vpar->sequence.intra_quant.pi_matrix );
393 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
394 p_vpar->sequence.nonintra_quant.pi_matrix );
399 NextStartCode( p_vpar );
400 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
404 /* Turn the MPEG2 flag on */
405 p_vpar->sequence.b_mpeg2 = 1;
407 /* Parse sequence_extension */
408 RemoveBits32( &p_vpar->bit_stream );
409 /* extension_start_code_identifier, profile_and_level_indication */
410 RemoveBits( &p_vpar->bit_stream, 12 );
411 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
412 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
413 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
414 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
415 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
416 RemoveBits( &p_vpar->bit_stream, 22 );
417 /* frame_rate_extension_n */
418 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
419 /* frame_rate_extension_d */
420 p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
421 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
425 /* It's an MPEG-1 stream. Put adequate parameters. */
427 p_vpar->sequence.b_mpeg2 = 0;
428 p_vpar->sequence.b_progressive = 1;
429 p_vpar->sequence.i_chroma_format = CHROMA_420;
433 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
434 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
435 (p_vpar->sequence.i_height + 15) / 16 :
436 2 * ((p_vpar->sequence.i_height + 31) / 32);
437 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
438 * p_vpar->sequence.i_mb_height;
439 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
440 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
441 p_vpar->sequence.i_size = p_vpar->sequence.i_width
442 * p_vpar->sequence.i_height;
444 /* Update chromatic information. */
445 switch( p_vpar->sequence.i_chroma_format )
448 p_vpar->sequence.i_chroma_nb_blocks = 2;
449 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
450 p_vpar->sequence.i_chroma_mb_width = 8;
451 p_vpar->sequence.i_chroma_mb_height = 8;
455 p_vpar->sequence.i_chroma_nb_blocks = 4;
456 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
457 p_vpar->sequence.i_chroma_mb_width = 8;
458 p_vpar->sequence.i_chroma_mb_height = 16;
462 p_vpar->sequence.i_chroma_nb_blocks = 8;
463 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
464 p_vpar->sequence.i_chroma_mb_width = 16;
465 p_vpar->sequence.i_chroma_mb_height = 16;
468 /* Reset scalable_mode. */
469 p_vpar->sequence.i_scalable_mode = SC_NONE;
472 if( p_vpar->sequence.i_width != i_width_save
473 || p_vpar->sequence.i_height != i_height_save )
475 /* FIXME: What do we do in case of a size change ?? */
479 /* Extension and User data */
480 ExtensionAndUserData( p_vpar );
483 /*****************************************************************************
484 * GroupHeader : Parse the next group of pictures header
485 *****************************************************************************/
486 static void GroupHeader( vpar_thread_t * p_vpar )
488 /* Nothing to do, we don't care. */
489 RemoveBits( &p_vpar->bit_stream, 27 );
490 ExtensionAndUserData( p_vpar );
493 /*****************************************************************************
494 * PictureHeader : Parse the next picture header
495 *****************************************************************************/
496 static void PictureHeader( vpar_thread_t * p_vpar )
498 /* Table of optimized PictureData functions. */
499 static f_picture_data_t ppf_picture_data[4][4] =
502 NULL, NULL, NULL, NULL
506 #if (VPAR_OPTIM_LEVEL > 1)
507 NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
508 vpar_PictureData2B420TZ
510 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
511 vpar_PictureDataGENERIC
516 #if (VPAR_OPTIM_LEVEL > 1)
517 NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
518 vpar_PictureData2B420BZ
520 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
521 vpar_PictureDataGENERIC
526 #if (VPAR_OPTIM_LEVEL > 0)
527 NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
528 vpar_PictureData2B420F0
530 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
531 vpar_PictureDataGENERIC
538 boolean_t b_parsable;
543 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
544 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
545 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
547 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
548 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
550 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
551 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
553 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
555 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
556 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
559 /* extra_information_picture */
560 while( GetBits( &p_vpar->bit_stream, 1 ) )
562 RemoveBits( &p_vpar->bit_stream, 8 );
566 * Picture Coding Extension
568 NextStartCode( p_vpar );
569 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
571 /* Parse picture_coding_extension */
572 RemoveBits32( &p_vpar->bit_stream );
573 /* extension_start_code_identifier */
574 RemoveBits( &p_vpar->bit_stream, 4 );
576 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
577 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
578 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
579 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
580 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
581 i_structure = GetBits( &p_vpar->bit_stream, 2 );
582 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
583 p_vpar->picture.b_frame_pred_frame_dct
584 = GetBits( &p_vpar->bit_stream, 1 );
585 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
586 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
587 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
588 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
589 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
590 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
591 * the length of the picture_display_extension structure.
592 * chroma_420_type (obsolete) */
593 RemoveBits( &p_vpar->bit_stream, 1 );
594 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
596 /* composite_display_flag */
597 if( GetBits( &p_vpar->bit_stream, 1 ) )
599 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
600 * sub_carrier_phase */
601 RemoveBits( &p_vpar->bit_stream, 20 );
606 /* MPEG-1 compatibility flags */
607 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
608 i_structure = FRAME_STRUCTURE;
609 p_vpar->picture.b_frame_pred_frame_dct = 1;
610 p_vpar->picture.b_concealment_mv = 0;
611 p_vpar->picture.b_q_scale_type = 0;
612 p_vpar->picture.b_intra_vlc_format = 0;
613 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
614 p_vpar->picture.b_repeat_first_field = 0;
615 p_vpar->picture.b_progressive_frame = 1;
618 if( p_vpar->picture.i_current_structure &&
619 (i_structure == FRAME_STRUCTURE ||
620 i_structure == p_vpar->picture.i_current_structure) )
622 /* We don't have the second field of the buffered frame. */
623 if( p_vpar->picture.p_picture != NULL )
625 ReferenceReplace( p_vpar,
626 p_vpar->picture.i_coding_type,
630 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
632 vpar_DestroyMacroblock( &p_vpar->vfifo,
633 p_vpar->picture.pp_mb[i_mb] );
636 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
639 p_vpar->picture.i_current_structure = 0;
641 intf_DbgMsg("vpar debug: odd number of field picture.\n");
644 /* Do we have the reference pictures ? */
645 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
646 (p_vpar->sequence.p_backward == NULL)) ||
647 /* p_backward will become p_forward later */
648 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
649 (p_vpar->sequence.p_forward == NULL ||
650 p_vpar->sequence.p_backward == NULL)));
654 if( p_vpar->picture.i_current_structure )
656 /* Second field of a frame. We will decode it if, and only if we
657 * have decoded the first field. */
658 b_parsable = (p_vpar->picture.p_picture != NULL);
662 /* Does synchro say we have enough time to decode it ? */
663 b_parsable = vpar_SynchroChoose( p_vpar,
664 p_vpar->picture.i_coding_type, i_structure );
668 else if( !p_vpar->picture.i_current_structure )
670 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
676 /* Update the reference pointers. */
677 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
678 #ifndef POLUX_SYNCHRO
679 /* Warn Synchro we have trashed a picture. */
680 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
682 /* Update context. */
683 if( i_structure != FRAME_STRUCTURE )
684 p_vpar->picture.i_current_structure = i_structure;
685 p_vpar->picture.p_picture = NULL;
690 /* OK, now we are sure we will decode the picture. */
691 #define P_picture p_vpar->picture.p_picture
692 p_vpar->picture.b_error = 0;
693 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
695 if( !p_vpar->picture.i_current_structure )
697 /* This is a new frame. Get a structure from the video_output. */
698 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
699 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
700 p_vpar->sequence.i_width,
701 p_vpar->sequence.i_height ) )
704 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
705 if( p_vpar->b_die || p_vpar->b_error )
709 msleep( VPAR_OUTMEM_SLEEP );
712 /* Initialize values. */
713 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
714 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
715 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
716 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
717 << ( 1 - p_vpar->picture.b_frame_structure ) );
718 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
719 << ( 1 - p_vpar->picture.b_frame_structure ));
721 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
722 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
724 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
726 /* FIXME ! remove asap ?? */
727 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
729 /* Update the reference pointers. */
730 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
733 /* Link referenced pictures for the decoder
734 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
735 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
736 p_vpar->picture.i_coding_type == B_CODING_TYPE )
738 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
740 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
742 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
746 p_vpar->picture.i_current_structure |= i_structure;
747 p_vpar->picture.i_structure = i_structure;
749 /* Initialize picture data for decoding. */
750 if( i_structure == BOTTOM_FIELD )
752 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
753 p_vpar->mb.i_l_y = 1;
754 p_vpar->mb.i_c_y = 1;
759 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
761 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
763 /* Extension and User data. */
764 ExtensionAndUserData( p_vpar );
766 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
767 if( p_vpar->sequence.i_chroma_format != CHROMA_420
768 || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
769 || p_vpar->sequence.i_scalable_mode == SC_DP )
771 /* Weird stream. Use the slower generic function. */
772 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
776 /* Try to find an optimized function. */
777 if( ppf_picture_data[p_vpar->picture.i_structure][p_vpar->picture.i_coding_type] == NULL )
779 intf_ErrMsg( "vpar error: bad ppf_picture_data function pointer (struct:%d, coding type:%d)\n",
780 p_vpar->picture.i_structure, p_vpar->picture.i_coding_type );
784 ppf_picture_data[p_vpar->picture.i_structure]
785 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
789 if( p_vpar->b_die || p_vpar->b_error )
794 if( p_vpar->picture.b_error )
797 //fprintf(stderr, "Image trashee\n");
799 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
801 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
805 if( P_picture->i_deccount != 1 )
807 vout_DestroyPicture( p_vpar->p_vout, P_picture );
810 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
812 /* Prepare context for the next picture. */
814 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
815 p_vpar->picture.i_current_structure = 0;
817 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
819 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
820 /* Frame completely parsed. */
822 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
824 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
827 /* Send signal to the video_decoder. */
828 vlc_mutex_lock( &p_vpar->vfifo.lock );
829 vlc_cond_signal( &p_vpar->vfifo.wait );
830 vlc_mutex_unlock( &p_vpar->vfifo.lock );
833 /* Prepare context for the next picture. */
835 p_vpar->picture.i_current_structure = 0;
840 /*****************************************************************************
841 * ExtensionAndUserData : Parse the extension_and_user_data structure
842 *****************************************************************************/
843 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
845 while( !p_vpar->b_die )
847 NextStartCode( p_vpar );
848 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
850 case EXTENSION_START_CODE:
851 RemoveBits32( &p_vpar->bit_stream );
852 switch( GetBits( &p_vpar->bit_stream, 4 ) )
854 case SEQUENCE_DISPLAY_EXTENSION_ID:
855 SequenceDisplayExtension( p_vpar );
857 case QUANT_MATRIX_EXTENSION_ID:
858 QuantMatrixExtension( p_vpar );
860 case SEQUENCE_SCALABLE_EXTENSION_ID:
861 SequenceScalableExtension( p_vpar );
863 case PICTURE_DISPLAY_EXTENSION_ID:
864 PictureDisplayExtension( p_vpar );
866 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
867 PictureSpatialScalableExtension( p_vpar );
869 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
870 PictureTemporalScalableExtension( p_vpar );
872 case COPYRIGHT_EXTENSION_ID:
873 CopyrightExtension( p_vpar );
879 case USER_DATA_START_CODE:
880 RemoveBits32( &p_vpar->bit_stream );
881 /* Wait for the next start code */
891 /*****************************************************************************
892 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
893 *****************************************************************************/
895 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
897 /* We don't care sequence_display_extension. */
899 RemoveBits( &p_vpar->bit_stream, 3 );
900 if( GetBits( &p_vpar->bit_stream, 1 ) )
902 /* Two bytes for color_desciption */
903 RemoveBits( &p_vpar->bit_stream, 16 );
904 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
906 /* display_horizontal and vertical_size and a marker_bit */
907 RemoveBits( &p_vpar->bit_stream, 29 );
911 /*****************************************************************************
912 * QuantMatrixExtension : Load quantization matrices for luminance *
914 *****************************************************************************/
916 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
918 if( GetBits( &p_vpar->bit_stream, 1 ) )
920 /* Load intra_quantiser_matrix for luminance. */
921 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
925 /* Use the default matrix. */
926 LinkMatrix( &p_vpar->sequence.intra_quant,
927 pi_default_intra_quant );
929 if( GetBits( &p_vpar->bit_stream, 1 ) )
931 /* Load non_intra_quantiser_matrix for luminance. */
932 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
936 /* Use the default matrix. */
937 LinkMatrix( &p_vpar->sequence.nonintra_quant,
938 pi_default_nonintra_quant );
940 if( GetBits( &p_vpar->bit_stream, 1 ) )
942 /* Load intra_quantiser_matrix for chrominance. */
943 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
947 /* Link the chrominance intra matrix to the luminance one. */
948 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
949 p_vpar->sequence.intra_quant.pi_matrix );
951 if( GetBits( &p_vpar->bit_stream, 1 ) )
953 /* Load non_intra_quantiser_matrix for chrominance. */
954 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
958 /* Link the chrominance intra matrix to the luminance one. */
959 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
960 p_vpar->sequence.intra_quant.pi_matrix );
962 if( GetBits( &p_vpar->bit_stream, 1 ) )
964 /* Load non_intra_quantiser_matrix for chrominance. */
965 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
969 /* Link the chrominance nonintra matrix to the luminance one. */
970 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
971 p_vpar->sequence.nonintra_quant.pi_matrix );
976 /*****************************************************************************
977 * SequenceScalableExtension : Parse the sequence_scalable_extension *
978 * structure to handle scalable coding *
979 *****************************************************************************/
981 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
983 /* We don't care about anything scalable except the scalable mode. */
984 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
985 /* The length of the structure depends on the value of the scalable_mode */
988 RemoveBits32( &p_vpar->bit_stream );
989 RemoveBits( &p_vpar->bit_stream, 21 );
992 RemoveBits( &p_vpar->bit_stream, 12 );
995 RemoveBits( &p_vpar->bit_stream, 4 );
999 /*****************************************************************************
1000 * PictureDisplayExtension : Parse the picture_display_extension structure *
1001 *****************************************************************************/
1003 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1005 /* Number of frame center offset */
1007 /* I am not sure it works but it should
1008 (fewer tests than shown in §6.3.12) */
1009 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1010 p_vpar->picture.b_repeat_first_field +
1011 p_vpar->picture.b_top_field_first
1012 : ( p_vpar->picture.b_frame_structure + 1 ) +
1013 p_vpar->picture.b_repeat_first_field;
1014 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1016 RemoveBits( &p_vpar->bit_stream, 17 );
1017 RemoveBits( &p_vpar->bit_stream, 17 );
1022 /*****************************************************************************
1023 * PictureSpatialScalableExtension *
1024 *****************************************************************************/
1026 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1028 /* That's scalable, so we trash it */
1029 RemoveBits32( &p_vpar->bit_stream );
1030 RemoveBits( &p_vpar->bit_stream, 16 );
1034 /*****************************************************************************
1035 * PictureTemporalScalableExtension *
1036 *****************************************************************************/
1038 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1040 /* Scalable again, trashed again */
1041 RemoveBits( &p_vpar->bit_stream, 23 );
1045 /*****************************************************************************
1046 * CopyrightExtension : Keeps some legal informations *
1047 *****************************************************************************/
1049 static void CopyrightExtension( vpar_thread_t * p_vpar )
1051 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1052 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1053 /* A flag that says whether the copyright information is significant */
1054 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1055 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1056 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1058 RemoveBits( &p_vpar->bit_stream, 8 );
1059 /* The copyright_number is split in three parts */
1061 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1062 RemoveBits( &p_vpar->bit_stream, 1 );
1064 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1065 RemoveBits( &p_vpar->bit_stream, 1 );
1066 /* third part and sum */
1067 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1068 ( (u64)i_copyright_nb_2 << 22 ) |
1069 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );