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 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <sys/types.h> /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h> /* "input.h" */
42 #include "decoder_fifo.h"
44 #include "video_output.h"
46 #include "vdec_idct.h"
47 #include "video_decoder.h"
48 #include "vdec_motion.h"
50 #include "vpar_blocks.h"
51 #include "vpar_headers.h"
52 #include "vpar_synchro.h"
53 #include "video_parser.h"
54 #include "video_fifo.h"
59 typedef void (*f_picture_data_t)( vpar_thread_t*, int );
64 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
65 static void SequenceHeader( vpar_thread_t * p_vpar );
66 static void GroupHeader( vpar_thread_t * p_vpar );
67 static void PictureHeader( vpar_thread_t * p_vpar );
68 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
69 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
70 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
71 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
72 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
73 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
74 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
75 static void CopyrightExtension( vpar_thread_t * p_vpar );
81 /*****************************************************************************
82 * pi_default_intra_quant : default quantization matrix
83 *****************************************************************************/
85 int pi_default_intra_quant[] =
87 8, 16, 19, 22, 26, 27, 29, 34,
88 16, 16, 22, 24, 27, 29, 34, 37,
89 19, 22, 26, 27, 29, 34, 34, 38,
90 22, 22, 26, 27, 29, 34, 37, 40,
91 22, 26, 27, 29, 32, 35, 40, 48,
92 26, 27, 29, 32, 35, 40, 48, 58,
93 26, 27, 29, 34, 38, 46, 56, 69,
94 27, 29, 35, 38, 46, 56, 69, 83
97 int pi_default_intra_quant[] =
99 2048, 5681, 6355, 6623, 6656, 5431, 4018, 2401,
100 5681, 7880, 10207, 10021, 9587, 8091, 6534, 3625,
101 6355, 10207, 11363, 10619, 9700, 8935, 6155, 3507,
102 6623, 9186, 10226, 9557, 8730, 8041, 6028, 3322,
103 5632, 9232, 9031, 8730, 8192, 7040, 5542, 3390,
104 5230, 7533, 7621, 7568, 7040, 6321, 5225, 3219,
105 3602, 5189, 5250, 5539, 5265, 5007, 4199, 2638,
106 1907, 2841, 3230, 3156, 3249, 3108, 2638, 1617
110 /*****************************************************************************
111 * pi_default_nonintra_quant : default quantization matrix
112 *****************************************************************************/
114 int pi_default_nonintra_quant[] =
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,
123 16, 16, 16, 16, 16, 16, 16, 16
126 int pi_default_nonintra_quanit[] =
128 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
129 5680, 7888, 7424, 6688, 5680, 4464, 3072, 1568,
130 5344, 7424, 6992, 6288, 5344, 4208, 2896, 1472,
131 4816, 6688, 6288, 5664, 4816, 3792, 2608, 1328,
132 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
133 3216, 4464, 4208, 3792, 3216, 2528, 1744, 880,
134 2224, 3072, 2896, 2608, 2224, 1744, 1200, 608,
135 1136, 1568, 1472, 1328, 1136, 880, 608, 304
139 /*****************************************************************************
140 * pi_scan : zig-zag and alternate scan patterns
141 *****************************************************************************/
144 { /* Zig-Zag pattern */
145 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
146 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
147 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
148 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
150 { /* Alternate scan pattern */
151 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
152 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
153 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
154 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
159 * Local inline functions.
162 /*****************************************************************************
163 * ReferenceUpdate : Update the reference pointers when we have a new picture
164 *****************************************************************************/
165 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
167 picture_t * p_newref )
169 if( i_coding_type != B_CODING_TYPE )
171 if( p_vpar->sequence.p_forward != NULL )
172 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
173 if( p_vpar->sequence.p_backward != NULL )
176 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
177 vpar_SynchroDate( p_vpar ) );
180 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
181 vpar_SynchroDate( p_vpar ) );
185 date = vpar_SynchroDate( p_vpar );
186 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
188 if( p_vpar->synchro.i_coding_type == I_CODING_TYPE )
189 vpar_SynchroKludge( p_vpar, date );
192 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
193 p_vpar->sequence.p_backward = p_newref;
194 if( p_newref != NULL )
195 vout_LinkPicture( p_vpar->p_vout, p_newref );
197 p_vpar->synchro.i_coding_type = i_coding_type;
200 else if( p_newref != NULL )
202 /* Put date immediately. */
203 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
207 /*****************************************************************************
208 * ReferenceReplace : Replace the last reference pointer when we destroy
210 *****************************************************************************/
211 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
213 picture_t * p_newref )
215 if( i_coding_type != B_CODING_TYPE )
217 if( p_vpar->sequence.p_backward != NULL )
218 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
219 p_vpar->sequence.p_backward = p_newref;
220 if( p_newref != NULL )
221 vout_LinkPicture( p_vpar->p_vout, p_newref );
225 /*****************************************************************************
226 * LoadMatrix : Load a quantization matrix
227 *****************************************************************************/
228 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
232 if( !p_matrix->b_allocated )
234 /* Allocate a piece of memory to load the matrix. */
235 if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
237 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
241 p_matrix->b_allocated = 1;
244 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
246 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
247 = GetBits( &p_vpar->bit_stream, 8 );
251 /* Discrete Fourier Transform requires the quantization matrices to
252 * be normalized before using them. */
253 vdec_NormQuantMatrix( p_matrix->pi_matrix );
257 /*****************************************************************************
258 * LinkMatrix : Link a quantization matrix to another
259 *****************************************************************************/
260 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
262 if( p_matrix->b_allocated )
264 /* Deallocate the piece of memory. */
265 free( p_matrix->pi_matrix );
266 p_matrix->b_allocated = 0;
269 p_matrix->pi_matrix = pi_array;
273 * Exported functions.
276 /*****************************************************************************
277 * vpar_NextSequenceHeader : Find the next sequence header
278 *****************************************************************************/
279 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
281 while( !p_vpar->b_die )
283 NextStartCode( p_vpar );
284 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
286 RemoveBits( &p_vpar->bit_stream, 8 );
291 /*****************************************************************************
292 * vpar_ParseHeader : Parse the next header
293 *****************************************************************************/
294 int vpar_ParseHeader( vpar_thread_t * p_vpar )
296 while( !p_vpar->b_die )
298 NextStartCode( p_vpar );
299 switch( GetBits32( &p_vpar->bit_stream ) )
301 case SEQUENCE_HEADER_CODE:
302 SequenceHeader( p_vpar );
306 case GROUP_START_CODE:
307 GroupHeader( p_vpar );
311 case PICTURE_START_CODE:
312 PictureHeader( p_vpar );
316 case SEQUENCE_END_CODE:
317 intf_DbgMsg("vpar debug: sequence end code received\n");
329 * Following functions are local
332 /*****************************************************************************
333 * SequenceHeader : Parse the next sequence header
334 *****************************************************************************/
335 static void SequenceHeader( vpar_thread_t * p_vpar )
338 static float r_frame_rate_table[16] =
341 ((23.0*1000.0)/1001.0),
344 ((30.0*1000.0)/1001.0),
347 ((60.0*1000.0)/1001.0),
349 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
353 int i_height_save, i_width_save;
355 i_height_save = p_vpar->sequence.i_height;
356 i_width_save = p_vpar->sequence.i_width;
358 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
359 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
360 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
361 p_vpar->sequence.r_frame_rate =
362 r_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
364 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
365 * constrained_parameters_flag */
366 RemoveBits( &p_vpar->bit_stream, 30 );
369 * Quantization matrices
371 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
373 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
377 /* Use default matrix. */
378 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
381 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
383 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
387 /* Use default matrix. */
388 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
391 /* Unless later overwritten by a matrix extension, we have the same
392 * matrices for luminance and chrominance. */
393 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
394 p_vpar->sequence.intra_quant.pi_matrix );
395 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
396 p_vpar->sequence.nonintra_quant.pi_matrix );
401 NextStartCode( p_vpar );
402 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
406 /* Turn the MPEG2 flag on */
407 p_vpar->sequence.b_mpeg2 = 1;
409 /* Parse sequence_extension */
410 RemoveBits32( &p_vpar->bit_stream );
411 /* extension_start_code_identifier, profile_and_level_indication */
412 RemoveBits( &p_vpar->bit_stream, 12 );
413 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
414 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
415 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
416 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
417 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
418 RemoveBits( &p_vpar->bit_stream, 22 );
419 /* frame_rate_extension_n */
420 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
421 /* frame_rate_extension_d */
422 p_vpar->sequence.r_frame_rate *= (i_dummy + 1)
423 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
427 /* It's an MPEG-1 stream. Put adequate parameters. */
429 p_vpar->sequence.b_mpeg2 = 0;
430 p_vpar->sequence.b_progressive = 1;
431 p_vpar->sequence.i_chroma_format = CHROMA_420;
435 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
436 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
437 (p_vpar->sequence.i_height + 15) / 16 :
438 2 * ((p_vpar->sequence.i_height + 31) / 32);
439 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
440 * p_vpar->sequence.i_mb_height;
441 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
442 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
443 p_vpar->sequence.i_size = p_vpar->sequence.i_width
444 * p_vpar->sequence.i_height;
446 /* Update chromatic information. */
447 switch( p_vpar->sequence.i_chroma_format )
450 p_vpar->sequence.i_chroma_nb_blocks = 2;
451 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
452 p_vpar->sequence.i_chroma_mb_width = 8;
453 p_vpar->sequence.i_chroma_mb_height = 8;
457 p_vpar->sequence.i_chroma_nb_blocks = 4;
458 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
459 p_vpar->sequence.i_chroma_mb_width = 8;
460 p_vpar->sequence.i_chroma_mb_height = 16;
464 p_vpar->sequence.i_chroma_nb_blocks = 8;
465 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
466 p_vpar->sequence.i_chroma_mb_width = 16;
467 p_vpar->sequence.i_chroma_mb_height = 16;
470 /* Reset scalable_mode. */
471 p_vpar->sequence.i_scalable_mode = SC_NONE;
474 if( p_vpar->sequence.i_width != i_width_save
475 || p_vpar->sequence.i_height != i_height_save )
477 /* FIXME: What do we do in case of a size change ?? */
481 /* Extension and User data */
482 ExtensionAndUserData( p_vpar );
485 /*****************************************************************************
486 * GroupHeader : Parse the next group of pictures header
487 *****************************************************************************/
488 static void GroupHeader( vpar_thread_t * p_vpar )
490 /* Nothing to do, we don't care. */
491 RemoveBits( &p_vpar->bit_stream, 27 );
492 ExtensionAndUserData( p_vpar );
495 /*****************************************************************************
496 * PictureHeader : Parse the next picture header
497 *****************************************************************************/
498 static void PictureHeader( vpar_thread_t * p_vpar )
500 /* Table of optimized PictureData functions. */
501 static f_picture_data_t ppf_picture_data[4][4] =
504 NULL, NULL, NULL, NULL
508 #if (VPAR_OPTIM_LEVEL > 1)
509 NULL, vpar_PictureData2I420TZ, vpar_PictureData2P420TZ,
510 vpar_PictureData2B420TZ
512 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
513 vpar_PictureDataGENERIC
518 #if (VPAR_OPTIM_LEVEL > 1)
519 NULL, vpar_PictureData2I420BZ, vpar_PictureData2P420BZ,
520 vpar_PictureData2B420BZ
522 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
523 vpar_PictureDataGENERIC
528 #if (VPAR_OPTIM_LEVEL > 0)
529 NULL, vpar_PictureData2I420F0, vpar_PictureData2P420F0,
530 vpar_PictureData2B420F0
532 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
533 vpar_PictureDataGENERIC
540 boolean_t b_parsable;
545 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
546 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
547 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
549 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
550 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
552 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
553 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
555 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
557 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
558 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
561 /* extra_information_picture */
562 while( GetBits( &p_vpar->bit_stream, 1 ) )
564 RemoveBits( &p_vpar->bit_stream, 8 );
568 * Picture Coding Extension
570 NextStartCode( p_vpar );
571 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
573 /* Parse picture_coding_extension */
574 RemoveBits32( &p_vpar->bit_stream );
575 /* extension_start_code_identifier */
576 RemoveBits( &p_vpar->bit_stream, 4 );
578 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
579 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
580 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
581 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
582 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
583 i_structure = GetBits( &p_vpar->bit_stream, 2 );
584 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
585 p_vpar->picture.b_frame_pred_frame_dct
586 = GetBits( &p_vpar->bit_stream, 1 );
587 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
588 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
589 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
590 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
591 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
592 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
593 * the length of the picture_display_extension structure.
594 * chroma_420_type (obsolete) */
595 RemoveBits( &p_vpar->bit_stream, 1 );
596 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
598 /* composite_display_flag */
599 if( GetBits( &p_vpar->bit_stream, 1 ) )
601 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
602 * sub_carrier_phase */
603 RemoveBits( &p_vpar->bit_stream, 20 );
608 /* MPEG-1 compatibility flags */
609 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
610 i_structure = FRAME_STRUCTURE;
611 p_vpar->picture.b_frame_pred_frame_dct = 1;
612 p_vpar->picture.b_concealment_mv = 0;
613 p_vpar->picture.b_q_scale_type = 0;
614 p_vpar->picture.b_intra_vlc_format = 0;
615 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
616 p_vpar->picture.b_repeat_first_field = 0;
617 p_vpar->picture.b_progressive_frame = 1;
620 if( p_vpar->picture.i_current_structure &&
621 (i_structure == FRAME_STRUCTURE ||
622 i_structure == p_vpar->picture.i_current_structure) )
624 /* We don't have the second field of the buffered frame. */
625 if( p_vpar->picture.p_picture != NULL )
627 ReferenceReplace( p_vpar,
628 p_vpar->picture.i_coding_type,
632 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
634 vpar_DestroyMacroblock( &p_vpar->vfifo,
635 p_vpar->picture.pp_mb[i_mb] );
638 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
641 p_vpar->picture.i_current_structure = 0;
643 intf_DbgMsg("vpar debug: odd number of field picture.\n");
646 /* Do we have the reference pictures ? */
647 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
648 (p_vpar->sequence.p_backward == NULL)) ||
649 /* p_backward will become p_forward later */
650 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
651 (p_vpar->sequence.p_forward == NULL ||
652 p_vpar->sequence.p_backward == NULL)));
656 if( p_vpar->picture.i_current_structure )
658 /* Second field of a frame. We will decode it if, and only if we
659 * have decoded the first field. */
660 b_parsable = (p_vpar->picture.p_picture != NULL);
664 /* Does synchro say we have enough time to decode it ? */
665 b_parsable = vpar_SynchroChoose( p_vpar,
666 p_vpar->picture.i_coding_type, i_structure );
670 else if( !p_vpar->picture.i_current_structure )
672 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
678 /* Update the reference pointers. */
679 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
680 #ifndef POLUX_SYNCHRO
681 /* Warn Synchro we have trashed a picture. */
682 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
684 /* Update context. */
685 if( i_structure != FRAME_STRUCTURE )
686 p_vpar->picture.i_current_structure = i_structure;
687 p_vpar->picture.p_picture = NULL;
692 /* OK, now we are sure we will decode the picture. */
693 #define P_picture p_vpar->picture.p_picture
694 p_vpar->picture.b_error = 0;
695 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
697 if( !p_vpar->picture.i_current_structure )
699 /* This is a new frame. Get a structure from the video_output. */
700 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
701 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
702 p_vpar->sequence.i_width,
703 p_vpar->sequence.i_height ) )
706 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
707 if( p_vpar->b_die || p_vpar->b_error )
711 msleep( VPAR_OUTMEM_SLEEP );
714 /* Initialize values. */
715 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
716 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
717 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
718 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
719 << ( 1 - p_vpar->picture.b_frame_structure ) );
720 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
721 << ( 1 - p_vpar->picture.b_frame_structure ));
723 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
724 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
726 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
728 /* FIXME ! remove asap ?? */
729 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
731 /* Update the reference pointers. */
732 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
735 /* Link referenced pictures for the decoder
736 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
737 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
738 p_vpar->picture.i_coding_type == B_CODING_TYPE )
740 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
742 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
744 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
748 p_vpar->picture.i_current_structure |= i_structure;
749 p_vpar->picture.i_structure = i_structure;
751 /* Initialize picture data for decoding. */
752 if( i_structure == BOTTOM_FIELD )
754 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
755 p_vpar->mb.i_l_y = 1;
756 p_vpar->mb.i_c_y = 1;
761 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
763 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
765 /* Extension and User data. */
766 ExtensionAndUserData( p_vpar );
768 /* Picture data (ISO/IEC 13818-2 6.2.3.7). */
769 if( p_vpar->sequence.i_chroma_format != CHROMA_420
770 || !p_vpar->sequence.b_mpeg2 || p_vpar->sequence.i_height > 2800
771 || p_vpar->sequence.i_scalable_mode == SC_DP )
773 /* Weird stream. Use the slower generic function. */
774 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
778 /* Try to find an optimized function. */
779 if( ppf_picture_data[p_vpar->picture.i_structure][p_vpar->picture.i_coding_type] == NULL )
781 intf_ErrMsg( "vpar error: bad ppf_picture_data function pointer (struct:%d, coding type:%d)\n",
782 p_vpar->picture.i_structure, p_vpar->picture.i_coding_type );
786 ppf_picture_data[p_vpar->picture.i_structure]
787 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
791 if( p_vpar->b_die || p_vpar->b_error )
796 if( p_vpar->picture.b_error )
799 //fprintf(stderr, "Image trashee\n");
801 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
803 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
807 if( P_picture->i_deccount != 1 )
809 vout_DestroyPicture( p_vpar->p_vout, P_picture );
812 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
814 /* Prepare context for the next picture. */
816 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
817 p_vpar->picture.i_current_structure = 0;
819 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
821 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
822 /* Frame completely parsed. */
824 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
826 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
829 /* Send signal to the video_decoder. */
830 vlc_mutex_lock( &p_vpar->vfifo.lock );
831 vlc_cond_signal( &p_vpar->vfifo.wait );
832 vlc_mutex_unlock( &p_vpar->vfifo.lock );
835 /* Prepare context for the next picture. */
837 p_vpar->picture.i_current_structure = 0;
842 /*****************************************************************************
843 * ExtensionAndUserData : Parse the extension_and_user_data structure
844 *****************************************************************************/
845 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
847 while( !p_vpar->b_die )
849 NextStartCode( p_vpar );
850 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
852 case EXTENSION_START_CODE:
853 RemoveBits32( &p_vpar->bit_stream );
854 switch( GetBits( &p_vpar->bit_stream, 4 ) )
856 case SEQUENCE_DISPLAY_EXTENSION_ID:
857 SequenceDisplayExtension( p_vpar );
859 case QUANT_MATRIX_EXTENSION_ID:
860 QuantMatrixExtension( p_vpar );
862 case SEQUENCE_SCALABLE_EXTENSION_ID:
863 SequenceScalableExtension( p_vpar );
865 case PICTURE_DISPLAY_EXTENSION_ID:
866 PictureDisplayExtension( p_vpar );
868 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
869 PictureSpatialScalableExtension( p_vpar );
871 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
872 PictureTemporalScalableExtension( p_vpar );
874 case COPYRIGHT_EXTENSION_ID:
875 CopyrightExtension( p_vpar );
881 case USER_DATA_START_CODE:
882 RemoveBits32( &p_vpar->bit_stream );
883 /* Wait for the next start code */
893 /*****************************************************************************
894 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
895 *****************************************************************************/
897 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
899 /* We don't care sequence_display_extension. */
901 RemoveBits( &p_vpar->bit_stream, 3 );
902 if( GetBits( &p_vpar->bit_stream, 1 ) )
904 /* Two bytes for color_desciption */
905 RemoveBits( &p_vpar->bit_stream, 16 );
906 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
908 /* display_horizontal and vertical_size and a marker_bit */
909 RemoveBits( &p_vpar->bit_stream, 29 );
913 /*****************************************************************************
914 * QuantMatrixExtension : Load quantization matrices for luminance *
916 *****************************************************************************/
918 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
920 if( GetBits( &p_vpar->bit_stream, 1 ) )
922 /* Load intra_quantiser_matrix for luminance. */
923 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
927 /* Use the default matrix. */
928 LinkMatrix( &p_vpar->sequence.intra_quant,
929 pi_default_intra_quant );
931 if( GetBits( &p_vpar->bit_stream, 1 ) )
933 /* Load non_intra_quantiser_matrix for luminance. */
934 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
938 /* Use the default matrix. */
939 LinkMatrix( &p_vpar->sequence.nonintra_quant,
940 pi_default_nonintra_quant );
942 if( GetBits( &p_vpar->bit_stream, 1 ) )
944 /* Load intra_quantiser_matrix for chrominance. */
945 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
949 /* Link the chrominance intra matrix to the luminance one. */
950 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
951 p_vpar->sequence.intra_quant.pi_matrix );
953 if( GetBits( &p_vpar->bit_stream, 1 ) )
955 /* Load non_intra_quantiser_matrix for chrominance. */
956 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
960 /* Link the chrominance intra matrix to the luminance one. */
961 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
962 p_vpar->sequence.intra_quant.pi_matrix );
964 if( GetBits( &p_vpar->bit_stream, 1 ) )
966 /* Load non_intra_quantiser_matrix for chrominance. */
967 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
971 /* Link the chrominance nonintra matrix to the luminance one. */
972 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
973 p_vpar->sequence.nonintra_quant.pi_matrix );
978 /*****************************************************************************
979 * SequenceScalableExtension : Parse the sequence_scalable_extension *
980 * structure to handle scalable coding *
981 *****************************************************************************/
983 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
985 /* We don't care about anything scalable except the scalable mode. */
986 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
987 /* The length of the structure depends on the value of the scalable_mode */
990 RemoveBits32( &p_vpar->bit_stream );
991 RemoveBits( &p_vpar->bit_stream, 21 );
994 RemoveBits( &p_vpar->bit_stream, 12 );
997 RemoveBits( &p_vpar->bit_stream, 4 );
1001 /*****************************************************************************
1002 * PictureDisplayExtension : Parse the picture_display_extension structure *
1003 *****************************************************************************/
1005 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1007 /* Number of frame center offset */
1009 /* I am not sure it works but it should
1010 (fewer tests than shown in §6.3.12) */
1011 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1012 p_vpar->picture.b_repeat_first_field +
1013 p_vpar->picture.b_top_field_first
1014 : ( p_vpar->picture.b_frame_structure + 1 ) +
1015 p_vpar->picture.b_repeat_first_field;
1016 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1018 RemoveBits( &p_vpar->bit_stream, 17 );
1019 RemoveBits( &p_vpar->bit_stream, 17 );
1024 /*****************************************************************************
1025 * PictureSpatialScalableExtension *
1026 *****************************************************************************/
1028 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1030 /* That's scalable, so we trash it */
1031 RemoveBits32( &p_vpar->bit_stream );
1032 RemoveBits( &p_vpar->bit_stream, 16 );
1036 /*****************************************************************************
1037 * PictureTemporalScalableExtension *
1038 *****************************************************************************/
1040 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1042 /* Scalable again, trashed again */
1043 RemoveBits( &p_vpar->bit_stream, 23 );
1047 /*****************************************************************************
1048 * CopyrightExtension : Keeps some legal informations *
1049 *****************************************************************************/
1051 static void CopyrightExtension( vpar_thread_t * p_vpar )
1053 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1054 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1055 /* A flag that says whether the copyright information is significant */
1056 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1057 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1058 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1060 RemoveBits( &p_vpar->bit_stream, 8 );
1061 /* The copyright_number is split in three parts */
1063 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1064 RemoveBits( &p_vpar->bit_stream, 1 );
1066 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1067 RemoveBits( &p_vpar->bit_stream, 1 );
1068 /* third part and sum */
1069 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1070 ( (u64)i_copyright_nb_2 << 22 ) |
1071 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );