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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 #include <stdlib.h> /* free() */
29 #include <sys/types.h> /* on BSD, uio.h needs types.h */
30 #include <sys/uio.h> /* "input.h" */
41 #include "decoder_fifo.h"
43 #include "video_output.h"
45 #include "vdec_idct.h"
46 #include "video_decoder.h"
47 #include "vdec_motion.h"
49 #include "vpar_blocks.h"
50 #include "vpar_headers.h"
51 #include "vpar_synchro.h"
52 #include "video_parser.h"
53 #include "video_fifo.h"
58 typedef void (*f_picture_data_t)( vpar_thread_t*, int );
63 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
64 static void SequenceHeader( vpar_thread_t * p_vpar );
65 static void GroupHeader( vpar_thread_t * p_vpar );
66 static void PictureHeader( vpar_thread_t * p_vpar );
67 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
68 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
69 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
70 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
71 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
72 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
73 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
74 static void CopyrightExtension( vpar_thread_t * p_vpar );
80 /*****************************************************************************
81 * pi_default_intra_quant : default quantization matrix
82 *****************************************************************************/
84 int pi_default_intra_quant[] =
86 8, 16, 19, 22, 26, 27, 29, 34,
87 16, 16, 22, 24, 27, 29, 34, 37,
88 19, 22, 26, 27, 29, 34, 34, 38,
89 22, 22, 26, 27, 29, 34, 37, 40,
90 22, 26, 27, 29, 32, 35, 40, 48,
91 26, 27, 29, 32, 35, 40, 48, 58,
92 26, 27, 29, 34, 38, 46, 56, 69,
93 27, 29, 35, 38, 46, 56, 69, 83
96 int pi_default_intra_quant[] =
98 2048, 5681, 6355, 6623, 6656, 5431, 4018, 2401,
99 5681, 7880, 10207, 10021, 9587, 8091, 6534, 3625,
100 6355, 10207, 11363, 10619, 9700, 8935, 6155, 3507,
101 6623, 9186, 10226, 9557, 8730, 8041, 6028, 3322,
102 5632, 9232, 9031, 8730, 8192, 7040, 5542, 3390,
103 5230, 7533, 7621, 7568, 7040, 6321, 5225, 3219,
104 3602, 5189, 5250, 5539, 5265, 5007, 4199, 2638,
105 1907, 2841, 3230, 3156, 3249, 3108, 2638, 1617
109 /*****************************************************************************
110 * pi_default_nonintra_quant : default quantization matrix
111 *****************************************************************************/
113 int pi_default_nonintra_quant[] =
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,
122 16, 16, 16, 16, 16, 16, 16, 16
125 int pi_default_nonintra_quanit[] =
127 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
128 5680, 7888, 7424, 6688, 5680, 4464, 3072, 1568,
129 5344, 7424, 6992, 6288, 5344, 4208, 2896, 1472,
130 4816, 6688, 6288, 5664, 4816, 3792, 2608, 1328,
131 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
132 3216, 4464, 4208, 3792, 3216, 2528, 1744, 880,
133 2224, 3072, 2896, 2608, 2224, 1744, 1200, 608,
134 1136, 1568, 1472, 1328, 1136, 880, 608, 304
138 /*****************************************************************************
139 * pi_scan : zig-zag and alternate scan patterns
140 *****************************************************************************/
143 { /* Zig-Zag pattern */
144 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
145 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
146 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
147 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
149 { /* Alternate scan pattern */
150 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
151 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
152 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
153 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
158 * Local inline functions.
161 /*****************************************************************************
162 * ReferenceUpdate : Update the reference pointers when we have a new picture
163 *****************************************************************************/
164 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
166 picture_t * p_newref )
168 if( i_coding_type != B_CODING_TYPE )
170 if( p_vpar->sequence.p_forward != NULL )
171 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
172 if( p_vpar->sequence.p_backward != NULL )
175 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
176 vpar_SynchroDate( p_vpar ) );
179 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
180 vpar_SynchroDate( p_vpar ) );
184 date = vpar_SynchroDate( p_vpar );
185 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
187 if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
188 vpar_SynchroKludge( p_vpar, date );
191 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
192 p_vpar->sequence.p_backward = p_newref;
193 if( p_newref != NULL )
194 vout_LinkPicture( p_vpar->p_vout, p_newref );
196 p_vpar->synchro.i_coding_type = i_coding_type;
199 else if( p_newref != NULL )
201 /* Put date immediately. */
202 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
206 /*****************************************************************************
207 * ReferenceReplace : Replace the last reference pointer when we destroy
209 *****************************************************************************/
210 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
212 picture_t * p_newref )
214 if( i_coding_type != B_CODING_TYPE )
216 if( p_vpar->sequence.p_backward != NULL )
217 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
218 p_vpar->sequence.p_backward = p_newref;
219 if( p_newref != NULL )
220 vout_LinkPicture( p_vpar->p_vout, p_newref );
224 /*****************************************************************************
225 * LoadMatrix : Load a quantization matrix
226 *****************************************************************************/
227 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
231 if( !p_matrix->b_allocated )
233 /* Allocate a piece of memory to load the matrix. */
234 if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
236 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
240 p_matrix->b_allocated = 1;
243 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
245 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
246 = GetBits( &p_vpar->bit_stream, 8 );
250 /* Discrete Fourier Transform requires the quantization matrices to
251 * be normalized before using them. */
252 vdec_NormQuantMatrix( p_matrix->pi_matrix );
256 /*****************************************************************************
257 * LinkMatrix : Link a quantization matrix to another
258 *****************************************************************************/
259 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
261 if( p_matrix->b_allocated )
263 /* Deallocate the piece of memory. */
264 free( p_matrix->pi_matrix );
265 p_matrix->b_allocated = 0;
268 p_matrix->pi_matrix = pi_array;
272 * Exported functions.
275 /*****************************************************************************
276 * vpar_NextSequenceHeader : Find the next sequence header
277 *****************************************************************************/
278 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
280 while( !p_vpar->b_die )
282 NextStartCode( p_vpar );
283 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
285 RemoveBits( &p_vpar->bit_stream, 8 );
290 /*****************************************************************************
291 * vpar_ParseHeader : Parse the next header
292 *****************************************************************************/
293 int vpar_ParseHeader( vpar_thread_t * p_vpar )
295 while( !p_vpar->b_die )
297 NextStartCode( p_vpar );
298 switch( GetBits32( &p_vpar->bit_stream ) )
300 case SEQUENCE_HEADER_CODE:
301 SequenceHeader( p_vpar );
305 case GROUP_START_CODE:
306 GroupHeader( p_vpar );
310 case PICTURE_START_CODE:
311 PictureHeader( p_vpar );
315 case SEQUENCE_END_CODE:
316 intf_DbgMsg("vpar debug: sequence end code received\n");
328 * Following functions are local
331 /*****************************************************************************
332 * SequenceHeader : Parse the next sequence header
333 *****************************************************************************/
334 static void SequenceHeader( vpar_thread_t * p_vpar )
337 static float r_frame_rate_table[16] =
340 ((23.0*1000.0)/1001.0),
343 ((30.0*1000.0)/1001.0),
346 ((60.0*1000.0)/1001.0),
348 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
352 int i_height_save, i_width_save;
354 i_height_save = p_vpar->sequence.i_height;
355 i_width_save = p_vpar->sequence.i_width;
357 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
358 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
359 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
360 p_vpar->sequence.r_frame_rate =
361 r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
363 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
364 * constrained_parameters_flag */
365 RemoveBits( &p_vpar->bit_stream, 30 );
368 * Quantization matrices
370 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
372 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
376 /* Use default matrix. */
377 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
380 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
382 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
386 /* Use default matrix. */
387 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
390 /* Unless later overwritten by a matrix extension, we have the same
391 * matrices for luminance and chrominance. */
392 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
393 p_vpar->sequence.intra_quant.pi_matrix );
394 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
395 p_vpar->sequence.nonintra_quant.pi_matrix );
400 NextStartCode( p_vpar );
401 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
405 /* Turn the MPEG2 flag on */
406 p_vpar->sequence.b_mpeg2 = 1;
408 /* Parse sequence_extension */
409 RemoveBits32( &p_vpar->bit_stream );
410 /* extension_start_code_identifier, profile_and_level_indication */
411 RemoveBits( &p_vpar->bit_stream, 12 );
412 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
413 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
414 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
415 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
416 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
417 RemoveBits( &p_vpar->bit_stream, 22 );
418 /* frame_rate_extension_n */
419 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
420 /* frame_rate_extension_d */
421 p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
422 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
426 /* It's an MPEG-1 stream. Put adequate parameters. */
428 p_vpar->sequence.b_mpeg2 = 0;
429 p_vpar->sequence.b_progressive = 1;
430 p_vpar->sequence.i_chroma_format = CHROMA_420;
434 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
435 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
436 (p_vpar->sequence.i_height + 15) / 16 :
437 2 * ((p_vpar->sequence.i_height + 31) / 32);
438 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
439 * p_vpar->sequence.i_mb_height;
440 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
441 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
442 p_vpar->sequence.i_size = p_vpar->sequence.i_width
443 * p_vpar->sequence.i_height;
445 /* Update chromatic information. */
446 switch( p_vpar->sequence.i_chroma_format )
449 p_vpar->sequence.i_chroma_nb_blocks = 2;
450 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
451 p_vpar->sequence.i_chroma_mb_width = 8;
452 p_vpar->sequence.i_chroma_mb_height = 8;
456 p_vpar->sequence.i_chroma_nb_blocks = 4;
457 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
458 p_vpar->sequence.i_chroma_mb_width = 8;
459 p_vpar->sequence.i_chroma_mb_height = 16;
463 p_vpar->sequence.i_chroma_nb_blocks = 8;
464 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
465 p_vpar->sequence.i_chroma_mb_width = 16;
466 p_vpar->sequence.i_chroma_mb_height = 16;
469 /* Reset scalable_mode. */
470 p_vpar->sequence.i_scalable_mode = SC_NONE;
473 if( p_vpar->sequence.i_width != i_width_save
474 || p_vpar->sequence.i_height != i_height_save )
476 /* FIXME: What do we do in case of a size change ?? */
480 /* Extension and User data */
481 ExtensionAndUserData( p_vpar );
484 /*****************************************************************************
485 * GroupHeader : Parse the next group of pictures header
486 *****************************************************************************/
487 static void GroupHeader( vpar_thread_t * p_vpar )
489 /* Nothing to do, we don't care. */
490 RemoveBits( &p_vpar->bit_stream, 27 );
491 ExtensionAndUserData( p_vpar );
494 /*****************************************************************************
495 * PictureHeader : Parse the next picture header
496 *****************************************************************************/
497 static void PictureHeader( vpar_thread_t * p_vpar )
499 /* Table of optimized PictureData functions. */
500 static f_picture_data_t ppf_picture_data[4][4] =
503 NULL, NULL, NULL, NULL
507 #if (VPAR_OPTIM_LEVEL > 1)
508 NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
509 vpar_PictureData2B420TZ
511 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
512 vpar_PictureDataGENERIC
517 #if (VPAR_OPTIM_LEVEL > 1)
518 NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
519 vpar_PictureData2B420BZ
521 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
522 vpar_PictureDataGENERIC
527 #if (VPAR_OPTIM_LEVEL > 0)
528 NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
529 vpar_PictureData2B420F0
531 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
532 vpar_PictureDataGENERIC
539 boolean_t b_parsable;
544 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
545 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
546 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
548 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
549 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
551 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
552 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
554 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
556 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
557 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
560 /* extra_information_picture */
561 while( GetBits( &p_vpar->bit_stream, 1 ) )
563 RemoveBits( &p_vpar->bit_stream, 8 );
567 * Picture Coding Extension
569 NextStartCode( p_vpar );
570 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
572 /* Parse picture_coding_extension */
573 RemoveBits32( &p_vpar->bit_stream );
574 /* extension_start_code_identifier */
575 RemoveBits( &p_vpar->bit_stream, 4 );
577 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
578 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
579 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
580 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
581 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
582 i_structure = GetBits( &p_vpar->bit_stream, 2 );
583 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
584 p_vpar->picture.b_frame_pred_frame_dct
585 = GetBits( &p_vpar->bit_stream, 1 );
586 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
587 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
588 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
589 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
590 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
591 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
592 * the length of the picture_display_extension structure.
593 * chroma_420_type (obsolete) */
594 RemoveBits( &p_vpar->bit_stream, 1 );
595 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
597 /* composite_display_flag */
598 if( GetBits( &p_vpar->bit_stream, 1 ) )
600 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
601 * sub_carrier_phase */
602 RemoveBits( &p_vpar->bit_stream, 20 );
607 /* MPEG-1 compatibility flags */
608 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
609 i_structure = FRAME_STRUCTURE;
610 p_vpar->picture.b_frame_pred_frame_dct = 1;
611 p_vpar->picture.b_concealment_mv = 0;
612 p_vpar->picture.b_q_scale_type = 0;
613 p_vpar->picture.b_intra_vlc_format = 0;
614 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
615 p_vpar->picture.b_repeat_first_field = 0;
616 p_vpar->picture.b_progressive_frame = 1;
619 if( p_vpar->picture.i_current_structure &&
620 (i_structure == FRAME_STRUCTURE ||
621 i_structure == p_vpar->picture.i_current_structure) )
623 /* We don't have the second field of the buffered frame. */
624 if( p_vpar->picture.p_picture != NULL )
626 ReferenceReplace( p_vpar,
627 p_vpar->picture.i_coding_type,
631 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
633 vpar_DestroyMacroblock( &p_vpar->vfifo,
634 p_vpar->picture.pp_mb[i_mb] );
637 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
640 p_vpar->picture.i_current_structure = 0;
642 intf_DbgMsg("vpar debug: odd number of field picture.\n");
645 /* Do we have the reference pictures ? */
646 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
647 (p_vpar->sequence.p_backward == NULL)) ||
648 /* p_backward will become p_forward later */
649 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
650 (p_vpar->sequence.p_forward == NULL ||
651 p_vpar->sequence.p_backward == NULL)));
655 if( p_vpar->picture.i_current_structure )
657 /* Second field of a frame. We will decode it if, and only if we
658 * have decoded the first field. */
659 b_parsable = (p_vpar->picture.p_picture != NULL);
663 /* Does synchro say we have enough time to decode it ? */
664 b_parsable = vpar_SynchroChoose( p_vpar,
665 p_vpar->picture.i_coding_type, i_structure );
669 else if( !p_vpar->picture.i_current_structure )
671 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
677 /* Update the reference pointers. */
678 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
679 #ifndef POLUX_SYNCHRO
680 /* Warn Synchro we have trashed a picture. */
681 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
683 /* Update context. */
684 if( i_structure != FRAME_STRUCTURE )
685 p_vpar->picture.i_current_structure = i_structure;
686 p_vpar->picture.p_picture = NULL;
691 /* OK, now we are sure we will decode the picture. */
692 #define P_picture p_vpar->picture.p_picture
693 p_vpar->picture.b_error = 0;
694 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
696 if( !p_vpar->picture.i_current_structure )
698 /* This is a new frame. Get a structure from the video_output. */
699 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
700 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
701 p_vpar->sequence.i_width,
702 p_vpar->sequence.i_height ) )
705 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
706 if( p_vpar->b_die || p_vpar->b_error )
710 msleep( VPAR_OUTMEM_SLEEP );
713 /* Initialize values. */
714 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
715 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
716 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
717 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
718 << ( 1 - p_vpar->picture.b_frame_structure ) );
719 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
720 << ( 1 - p_vpar->picture.b_frame_structure ));
722 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
723 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
725 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
727 /* FIXME ! remove asap ?? */
728 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
730 /* Update the reference pointers. */
731 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
734 /* Link referenced pictures for the decoder
735 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
736 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
737 p_vpar->picture.i_coding_type == B_CODING_TYPE )
739 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
741 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
743 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
747 p_vpar->picture.i_current_structure |= i_structure;
748 p_vpar->picture.i_structure = i_structure;
750 /* Initialize picture data for decoding. */
751 if( i_structure == BOTTOM_FIELD )
753 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
754 p_vpar->mb.i_l_y = 1;
755 p_vpar->mb.i_c_y = 1;
760 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
762 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
764 /* Extension and User data. */
765 ExtensionAndUserData( p_vpar );
767 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
768 if( p_vpar->sequence.i_chroma_format != CHROMA_420
769 || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
770 || p_vpar->sequence.i_scalable_mode == SC_DP )
772 /* Weird stream. Use the slower generic function. */
773 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
777 /* Try to find an optimized function. */
778 if( ppf_picture_data[p_vpar->picture.i_structure][p_vpar->picture.i_coding_type] == NULL )
780 intf_ErrMsg( "vpar error: bad ppf_picture_data function pointer (struct:%d, coding type:%d)\n",
781 p_vpar->picture.i_structure, p_vpar->picture.i_coding_type );
785 ppf_picture_data[p_vpar->picture.i_structure]
786 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
790 if( p_vpar->b_die || p_vpar->b_error )
795 if( p_vpar->picture.b_error )
798 //fprintf(stderr, "Image trashee\n");
800 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
802 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
806 if( P_picture->i_deccount != 1 )
808 vout_DestroyPicture( p_vpar->p_vout, P_picture );
811 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
813 /* Prepare context for the next picture. */
815 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
816 p_vpar->picture.i_current_structure = 0;
818 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
820 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
821 /* Frame completely parsed. */
823 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
825 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
828 /* Send signal to the video_decoder. */
829 vlc_mutex_lock( &p_vpar->vfifo.lock );
830 vlc_cond_signal( &p_vpar->vfifo.wait );
831 vlc_mutex_unlock( &p_vpar->vfifo.lock );
834 /* Prepare context for the next picture. */
836 p_vpar->picture.i_current_structure = 0;
841 /*****************************************************************************
842 * ExtensionAndUserData : Parse the extension_and_user_data structure
843 *****************************************************************************/
844 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
846 while( !p_vpar->b_die )
848 NextStartCode( p_vpar );
849 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
851 case EXTENSION_START_CODE:
852 RemoveBits32( &p_vpar->bit_stream );
853 switch( GetBits( &p_vpar->bit_stream, 4 ) )
855 case SEQUENCE_DISPLAY_EXTENSION_ID:
856 SequenceDisplayExtension( p_vpar );
858 case QUANT_MATRIX_EXTENSION_ID:
859 QuantMatrixExtension( p_vpar );
861 case SEQUENCE_SCALABLE_EXTENSION_ID:
862 SequenceScalableExtension( p_vpar );
864 case PICTURE_DISPLAY_EXTENSION_ID:
865 PictureDisplayExtension( p_vpar );
867 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
868 PictureSpatialScalableExtension( p_vpar );
870 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
871 PictureTemporalScalableExtension( p_vpar );
873 case COPYRIGHT_EXTENSION_ID:
874 CopyrightExtension( p_vpar );
880 case USER_DATA_START_CODE:
881 RemoveBits32( &p_vpar->bit_stream );
882 /* Wait for the next start code */
892 /*****************************************************************************
893 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
894 *****************************************************************************/
896 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
898 /* We don't care sequence_display_extension. */
900 RemoveBits( &p_vpar->bit_stream, 3 );
901 if( GetBits( &p_vpar->bit_stream, 1 ) )
903 /* Two bytes for color_desciption */
904 RemoveBits( &p_vpar->bit_stream, 16 );
905 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
907 /* display_horizontal and vertical_size and a marker_bit */
908 RemoveBits( &p_vpar->bit_stream, 29 );
912 /*****************************************************************************
913 * QuantMatrixExtension : Load quantization matrices for luminance *
915 *****************************************************************************/
917 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
919 if( GetBits( &p_vpar->bit_stream, 1 ) )
921 /* Load intra_quantiser_matrix for luminance. */
922 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
926 /* Use the default matrix. */
927 LinkMatrix( &p_vpar->sequence.intra_quant,
928 pi_default_intra_quant );
930 if( GetBits( &p_vpar->bit_stream, 1 ) )
932 /* Load non_intra_quantiser_matrix for luminance. */
933 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
937 /* Use the default matrix. */
938 LinkMatrix( &p_vpar->sequence.nonintra_quant,
939 pi_default_nonintra_quant );
941 if( GetBits( &p_vpar->bit_stream, 1 ) )
943 /* Load intra_quantiser_matrix for chrominance. */
944 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
948 /* Link the chrominance intra matrix to the luminance one. */
949 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
950 p_vpar->sequence.intra_quant.pi_matrix );
952 if( GetBits( &p_vpar->bit_stream, 1 ) )
954 /* Load non_intra_quantiser_matrix for chrominance. */
955 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
959 /* Link the chrominance intra matrix to the luminance one. */
960 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
961 p_vpar->sequence.intra_quant.pi_matrix );
963 if( GetBits( &p_vpar->bit_stream, 1 ) )
965 /* Load non_intra_quantiser_matrix for chrominance. */
966 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
970 /* Link the chrominance nonintra matrix to the luminance one. */
971 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
972 p_vpar->sequence.nonintra_quant.pi_matrix );
977 /*****************************************************************************
978 * SequenceScalableExtension : Parse the sequence_scalable_extension *
979 * structure to handle scalable coding *
980 *****************************************************************************/
982 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
984 /* We don't care about anything scalable except the scalable mode. */
985 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
986 /* The length of the structure depends on the value of the scalable_mode */
989 RemoveBits32( &p_vpar->bit_stream );
990 RemoveBits( &p_vpar->bit_stream, 21 );
993 RemoveBits( &p_vpar->bit_stream, 12 );
996 RemoveBits( &p_vpar->bit_stream, 4 );
1000 /*****************************************************************************
1001 * PictureDisplayExtension : Parse the picture_display_extension structure *
1002 *****************************************************************************/
1004 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1006 /* Number of frame center offset */
1008 /* I am not sure it works but it should
1009 (fewer tests than shown in ยง6.3.12) */
1010 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1011 p_vpar->picture.b_repeat_first_field +
1012 p_vpar->picture.b_top_field_first
1013 : ( p_vpar->picture.b_frame_structure + 1 ) +
1014 p_vpar->picture.b_repeat_first_field;
1015 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1017 RemoveBits( &p_vpar->bit_stream, 17 );
1018 RemoveBits( &p_vpar->bit_stream, 17 );
1023 /*****************************************************************************
1024 * PictureSpatialScalableExtension *
1025 *****************************************************************************/
1027 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1029 /* That's scalable, so we trash it */
1030 RemoveBits32( &p_vpar->bit_stream );
1031 RemoveBits( &p_vpar->bit_stream, 16 );
1035 /*****************************************************************************
1036 * PictureTemporalScalableExtension *
1037 *****************************************************************************/
1039 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1041 /* Scalable again, trashed again */
1042 RemoveBits( &p_vpar->bit_stream, 23 );
1046 /*****************************************************************************
1047 * CopyrightExtension : Keeps some legal informations *
1048 *****************************************************************************/
1050 static void CopyrightExtension( vpar_thread_t * p_vpar )
1052 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1053 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1054 /* A flag that says whether the copyright information is significant */
1055 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1056 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1057 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1059 RemoveBits( &p_vpar->bit_stream, 8 );
1060 /* The copyright_number is split in three parts */
1062 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1063 RemoveBits( &p_vpar->bit_stream, 1 );
1065 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1066 RemoveBits( &p_vpar->bit_stream, 1 );
1067 /* third part and sum */
1068 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1069 ( (u64)i_copyright_nb_2 << 22 ) |
1070 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );