1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: vpar_headers.c,v 1.8 2001/12/30 07:09:56 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Stéphane Borel <stef@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* free() */
29 #include <string.h> /* memcpy(), memset() */
31 #include <videolan/vlc.h>
34 #include "video_output.h"
36 #include "stream_control.h"
37 #include "input_ext-dec.h"
39 #include "vdec_ext-plugins.h"
40 #include "vpar_pool.h"
41 #include "video_parser.h"
42 #include "video_decoder.h"
47 static __inline__ void NextStartCode( bit_stream_t * );
48 static void SequenceHeader( vpar_thread_t * p_vpar );
49 static void GroupHeader( vpar_thread_t * p_vpar );
50 static void PictureHeader( vpar_thread_t * p_vpar );
51 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
52 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
53 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
54 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
55 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
56 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
57 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
58 static void CopyrightExtension( vpar_thread_t * p_vpar );
64 /*****************************************************************************
65 * pi_default_intra_quant : default quantization matrix
66 *****************************************************************************/
67 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
69 8, 16, 19, 22, 26, 27, 29, 34,
70 16, 16, 22, 24, 27, 29, 34, 37,
71 19, 22, 26, 27, 29, 34, 34, 38,
72 22, 22, 26, 27, 29, 34, 37, 40,
73 22, 26, 27, 29, 32, 35, 40, 48,
74 26, 27, 29, 32, 35, 40, 48, 58,
75 26, 27, 29, 34, 38, 46, 56, 69,
76 27, 29, 35, 38, 46, 56, 69, 83
79 /*****************************************************************************
80 * pi_default_nonintra_quant : default quantization matrix
81 *****************************************************************************/
82 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
84 16, 16, 16, 16, 16, 16, 16, 16,
85 16, 16, 16, 16, 16, 16, 16, 16,
86 16, 16, 16, 16, 16, 16, 16, 16,
87 16, 16, 16, 16, 16, 16, 16, 16,
88 16, 16, 16, 16, 16, 16, 16, 16,
89 16, 16, 16, 16, 16, 16, 16, 16,
90 16, 16, 16, 16, 16, 16, 16, 16,
91 16, 16, 16, 16, 16, 16, 16, 16
94 /*****************************************************************************
95 * pi_scan : zig-zag and alternate scan patterns
96 *****************************************************************************/
97 u8 pi_scan[2][64] ATTR_ALIGN(16) =
99 { /* Zig-Zag pattern */
100 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
101 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
102 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
103 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
105 { /* Alternate scan pattern */
106 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
107 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
108 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
109 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
114 * Local inline functions.
117 /*****************************************************************************
118 * ReferenceUpdate : Update the reference pointers when we have a new picture
119 *****************************************************************************/
120 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
122 picture_t * p_newref )
124 if( i_coding_type != B_CODING_TYPE )
126 if( p_vpar->sequence.p_forward != NULL )
128 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
130 if( p_vpar->sequence.p_backward != NULL )
132 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
133 vpar_SynchroDate( p_vpar ) );
135 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
136 p_vpar->sequence.p_backward = p_newref;
137 if( p_newref != NULL )
139 vout_LinkPicture( p_vpar->p_vout, p_newref );
142 else if( p_newref != NULL )
144 /* Put date immediately. */
145 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
149 /*****************************************************************************
150 * ReferenceReplace : Replace the last reference pointer when we destroy
152 *****************************************************************************/
153 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
155 picture_t * p_newref )
157 if( i_coding_type != B_CODING_TYPE )
159 if( p_vpar->sequence.p_backward != NULL )
161 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
163 p_vpar->sequence.p_backward = p_newref;
164 if( p_newref != NULL )
166 vout_LinkPicture( p_vpar->p_vout, p_newref );
171 /*****************************************************************************
172 * LoadMatrix : Load a quantization matrix
173 *****************************************************************************/
174 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
175 quant_matrix_t * p_matrix )
179 if( !p_matrix->b_allocated )
181 /* Allocate a piece of memory to load the matrix. */
182 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
184 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
185 p_vpar->p_fifo->b_error = 1;
188 p_matrix->b_allocated = 1;
191 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
193 p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
194 = GetBits( &p_vpar->bit_stream, 8 );
198 /*****************************************************************************
199 * LinkMatrix : Link a quantization matrix to another
200 *****************************************************************************/
201 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
203 if( p_matrix->b_allocated )
205 /* Deallocate the piece of memory. */
206 free( p_matrix->pi_matrix );
207 p_matrix->b_allocated = 0;
210 p_matrix->pi_matrix = pi_array;
214 * Exported functions.
217 /*****************************************************************************
218 * vpar_NextSequenceHeader : Find the next sequence header
219 *****************************************************************************/
220 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
222 while( !p_vpar->p_fifo->b_die )
224 NextStartCode( &p_vpar->bit_stream );
225 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
229 RemoveBits( &p_vpar->bit_stream, 8 );
234 /*****************************************************************************
235 * vpar_ParseHeader : Parse the next header
236 *****************************************************************************/
237 int vpar_ParseHeader( vpar_thread_t * p_vpar )
239 while( !p_vpar->p_fifo->b_die )
241 NextStartCode( &p_vpar->bit_stream );
242 switch( GetBits32( &p_vpar->bit_stream ) )
244 case SEQUENCE_HEADER_CODE:
245 p_vpar->c_sequences++;
247 SequenceHeader( p_vpar );
251 case GROUP_START_CODE:
252 GroupHeader( p_vpar );
256 case PICTURE_START_CODE:
257 PictureHeader( p_vpar );
261 case SEQUENCE_END_CODE:
262 intf_DbgMsg("vpar debug: sequence end code received");
275 * Following functions are local
278 /*****************************************************************************
279 * SequenceHeader : Parse the next sequence header
280 *****************************************************************************/
281 static void SequenceHeader( vpar_thread_t * p_vpar )
284 static int i_frame_rate_table[16] =
295 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
299 int i_height_save, i_width_save, i_aspect;
301 i_height_save = p_vpar->sequence.i_height;
302 i_width_save = p_vpar->sequence.i_width;
304 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
305 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
306 i_aspect = GetBits( &p_vpar->bit_stream, 4 );
307 p_vpar->sequence.i_frame_rate =
308 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
310 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
311 * constrained_parameters_flag */
312 RemoveBits( &p_vpar->bit_stream, 30 );
315 * Quantization matrices
317 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
319 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
323 /* Use default matrix. */
324 LinkMatrix( &p_vpar->sequence.intra_quant,
325 p_vpar->pi_default_intra_quant );
328 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
330 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
334 /* Use default matrix. */
335 LinkMatrix( &p_vpar->sequence.nonintra_quant,
336 p_vpar->pi_default_nonintra_quant );
339 /* Unless later overwritten by a matrix extension, we have the same
340 * matrices for luminance and chrominance. */
341 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
342 p_vpar->sequence.intra_quant.pi_matrix );
343 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
344 p_vpar->sequence.nonintra_quant.pi_matrix );
349 NextStartCode( &p_vpar->bit_stream );
350 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
354 /* Turn the MPEG2 flag on */
355 p_vpar->sequence.b_mpeg2 = 1;
357 /* Parse sequence_extension */
358 RemoveBits32( &p_vpar->bit_stream );
359 /* extension_start_code_identifier, profile_and_level_indication */
360 RemoveBits( &p_vpar->bit_stream, 12 );
361 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
362 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
363 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
364 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
365 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
367 RemoveBits( &p_vpar->bit_stream, 22 );
368 /* frame_rate_extension_n */
369 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
370 /* frame_rate_extension_d */
371 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
372 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
376 /* It's an MPEG-1 stream. Put adequate parameters. */
378 static int pi_mpeg1ratio[15] = {
379 10000, 10000, 6735, 7031, 7615, 8055, 8437, 8935,
380 9157, 9815, 10255, 10695, 10950, 11575, 12015
385 i_xyratio = p_vpar->sequence.i_height *
386 pi_mpeg1ratio[i_aspect] / p_vpar->sequence.i_width;
387 if( 7450 < i_xyratio && i_xyratio < 7550 )
391 else if( 5575 < i_xyratio && i_xyratio < 5675 )
395 else if( 4475 < i_xyratio && i_xyratio < 4575 )
401 p_vpar->sequence.b_mpeg2 = 0;
402 p_vpar->sequence.b_progressive = 1;
403 p_vpar->sequence.i_chroma_format = CHROMA_420;
406 /* Store calculated aspect ratio */
410 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
413 case AR_16_9_PICTURE:
414 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
417 case AR_221_1_PICTURE:
418 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
421 case AR_SQUARE_PICTURE:
423 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR
424 * p_vpar->sequence.i_width / p_vpar->sequence.i_height;
429 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
430 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
431 (p_vpar->sequence.i_height + 15) / 16 :
432 2 * ((p_vpar->sequence.i_height + 31) / 32);
433 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
434 * p_vpar->sequence.i_mb_height;
435 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
436 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
437 p_vpar->sequence.i_size = p_vpar->sequence.i_width
438 * p_vpar->sequence.i_height;
439 switch( p_vpar->sequence.i_chroma_format )
442 p_vpar->sequence.i_chroma_nb_blocks = 2;
443 p_vpar->sequence.b_chroma_h_subsampled = 1;
444 p_vpar->sequence.b_chroma_v_subsampled = 1;
447 p_vpar->sequence.i_chroma_nb_blocks = 4;
448 p_vpar->sequence.b_chroma_h_subsampled = 1;
449 p_vpar->sequence.b_chroma_v_subsampled = 0;
452 p_vpar->sequence.i_chroma_nb_blocks = 6;
453 p_vpar->sequence.b_chroma_h_subsampled = 0;
454 p_vpar->sequence.b_chroma_v_subsampled = 0;
459 if( p_vpar->sequence.i_width != i_width_save
460 || p_vpar->sequence.i_height != i_height_save )
462 /* FIXME: Warn the video output */
466 /* Extension and User data */
467 ExtensionAndUserData( p_vpar );
469 /* XXX: The vout request and fifo opening will eventually be here */
471 /* Spawn a video output if there is none */
472 vlc_mutex_lock( &p_vout_bank->lock );
474 if( p_vout_bank->i_count == 0 )
476 intf_WarnMsg( 1, "vpar: no vout present, spawning one" );
479 vout_CreateThread( NULL, p_vpar->sequence.i_width,
480 p_vpar->sequence.i_height,
481 99 + p_vpar->sequence.i_chroma_format,
482 p_vpar->sequence.i_aspect );
484 /* Everything failed */
485 if( p_vpar->p_vout == NULL )
487 intf_ErrMsg( "vpar error: can't open vout, aborting" );
488 vlc_mutex_unlock( &p_vout_bank->lock );
490 p_vpar->p_fifo->b_error = 1;
494 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
495 p_vout_bank->i_count++;
499 /* Take the first video output FIXME: take the best one */
500 p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
503 vlc_mutex_unlock( &p_vout_bank->lock );
506 /*****************************************************************************
507 * GroupHeader : Parse the next group of pictures header
508 *****************************************************************************/
509 static void GroupHeader( vpar_thread_t * p_vpar )
511 /* Nothing to do, we don't care. */
512 RemoveBits( &p_vpar->bit_stream, 27 );
513 ExtensionAndUserData( p_vpar );
516 /*****************************************************************************
517 * PictureHeader : Parse the next picture header
518 *****************************************************************************/
519 static void PictureHeader( vpar_thread_t * p_vpar )
521 int i_structure, i_previous_coding_type;
522 boolean_t b_parsable = 0;
524 /* Recover in case of stream discontinuity. */
525 if( p_vpar->sequence.b_expect_discontinuity )
527 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
528 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
529 if( p_vpar->picture.p_picture != NULL )
531 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
532 p_vpar->picture.p_picture = NULL;
534 p_vpar->picture.i_current_structure = 0;
535 p_vpar->sequence.b_expect_discontinuity = 0;
538 /* Parse the picture header. */
539 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
540 i_previous_coding_type = p_vpar->picture.i_coding_type;
541 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
542 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
544 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
545 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
547 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
548 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
551 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
553 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
554 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
558 /* extra_information_picture */
559 while( GetBits( &p_vpar->bit_stream, 1 ) )
561 RemoveBits( &p_vpar->bit_stream, 8 );
565 * Picture Coding Extension
567 NextStartCode( &p_vpar->bit_stream );
568 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
570 /* Parse picture_coding_extension */
571 RemoveBits32( &p_vpar->bit_stream );
572 /* extension_start_code_identifier */
573 RemoveBits( &p_vpar->bit_stream, 4 );
575 /* Pre-substract 1 for later use in MotionDelta(). */
576 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
577 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
578 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
579 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
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 );
589 p_vpar->picture.pi_scan =
590 p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
591 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
592 /* chroma_420_type (obsolete) */
593 RemoveBits( &p_vpar->bit_stream, 1 );
594 p_vpar->picture.b_progressive = 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_top_field_first = 0;
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.pi_scan = p_vpar->ppi_scan[0];
615 p_vpar->picture.b_repeat_first_field = 0;
616 p_vpar->picture.b_progressive = 1;
619 /* Extension and User data. */
620 ExtensionAndUserData( p_vpar );
622 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
624 if( p_vpar->picture.i_current_structure )
626 if ( (i_structure == FRAME_STRUCTURE ||
627 i_structure == p_vpar->picture.i_current_structure) )
629 /* We don't have the second field of the buffered frame. */
630 if( p_vpar->picture.p_picture != NULL )
632 ReferenceReplace( p_vpar,
633 p_vpar->picture.i_coding_type,
635 vout_DestroyPicture( p_vpar->p_vout,
636 p_vpar->picture.p_picture );
637 p_vpar->picture.p_picture = NULL;
640 p_vpar->picture.i_current_structure = 0;
642 intf_WarnMsg( 2, "Odd number of field pictures." );
646 /* Second field of a frame. We will decode it if, and only if we
647 * have decoded the first field. */
648 if( p_vpar->picture.p_picture == NULL )
650 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
651 && p_vpar->sequence.p_backward == NULL) )
653 /* Exceptionnally, parse the picture if it is I. We need
654 * this in case of an odd number of field pictures, if the
655 * previous picture is not intra, we desperately need a
656 * new reference picture. OK, this is kind of kludgy. */
657 p_vpar->picture.i_current_structure = 0;
666 /* We suppose we have the reference pictures, since we already
667 * decoded the first field and the second field will not need
668 * any extra reference picture. There is a special case of
669 * P field being the second field of an I field, but ISO/IEC
670 * 13818-2 section 7.6.3.5 specifies that this P field will
671 * not need any reference picture besides the I field. So far
675 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
676 i_previous_coding_type == I_CODING_TYPE &&
677 p_vpar->sequence.p_forward == NULL )
679 /* This is the special case of section 7.6.3.5. Create
680 * a fake reference picture (which will not be used)
681 * but will prevent us from segfaulting in the slice
683 static picture_t fake_picture;
684 fake_picture.i_planes = 0; /* We will use it later */
685 p_vpar->sequence.p_forward = &fake_picture;
691 if( !p_vpar->picture.i_current_structure )
693 /* First field of a frame, or new frame picture. */
696 /* Do we have the reference pictures ? */
697 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
698 p_vpar->picture.b_concealment_mv) &&
699 (p_vpar->sequence.p_backward == NULL)) ||
700 /* p_backward will become p_forward later */
701 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
702 (p_vpar->sequence.p_forward == NULL ||
703 p_vpar->sequence.p_backward == NULL)));
705 /* Compute the number of times the frame will be emitted by the
706 * decoder (number of half-periods). */
707 if( p_vpar->sequence.b_progressive )
709 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
710 + p_vpar->picture.b_top_field_first) * 2;
714 if( p_vpar->picture.b_progressive )
716 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
724 /* Warn synchro we have a new picture (updates pictures index). */
725 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
730 /* Does synchro say we have enough time to decode it ? */
731 b_parsable = vpar_SynchroChoose( p_vpar,
732 p_vpar->picture.i_coding_type, i_structure );
738 /* Update the reference pointers. */
739 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
741 /* Update context. */
742 if( i_structure != FRAME_STRUCTURE )
744 if( (p_vpar->picture.i_current_structure | i_structure)
747 /* The frame is complete. */
748 p_vpar->picture.i_current_structure = 0;
750 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
754 p_vpar->picture.i_current_structure = i_structure;
759 /* Warn Synchro we have trashed a picture. */
760 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
762 p_vpar->picture.p_picture = NULL;
767 /* OK, now we are sure we will decode the picture. */
768 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
770 #define P_picture p_vpar->picture.p_picture
771 p_vpar->picture.b_error = 0;
772 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
774 if( !p_vpar->picture.i_current_structure )
776 /* This is a new frame. Get a structure from the video_output. */
777 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
778 p_vpar->picture.b_progressive,
779 p_vpar->picture.b_top_field_first,
780 p_vpar->picture.b_repeat_first_field ) )
783 intf_DbgMsg("vpar debug: vout_CreatePicture failed, delaying");
784 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
788 msleep( VOUT_OUTMEM_SLEEP );
791 /* Initialize values. */
792 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
793 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
794 p_vpar->picture.i_field_width = ( p_vpar->sequence.i_width
795 << ( 1 - p_vpar->picture.b_frame_structure ) );
797 /* Update the reference pointers. */
798 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
801 /* Initialize picture data for decoding. */
802 p_vpar->picture.i_current_structure |= i_structure;
803 p_vpar->picture.i_structure = i_structure;
804 p_vpar->picture.b_second_field =
805 (i_structure != p_vpar->picture.i_current_structure);
806 p_vpar->picture.b_current_field =
807 (i_structure == BOTTOM_FIELD );
809 if( !p_vpar->p_config->p_stream_ctrl->b_grayscale )
811 switch( p_vpar->sequence.i_chroma_format )
814 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock422;
817 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock444;
821 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock420;
827 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblockBW;
831 if( p_vpar->sequence.b_mpeg2 )
833 static f_picture_data_t ppf_picture_data[4][4] =
836 NULL, NULL, NULL, NULL
840 #if (VPAR_OPTIM_LEVEL > 1)
841 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
844 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
845 vpar_PictureDataGENERIC
850 #if (VPAR_OPTIM_LEVEL > 1)
851 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
854 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
855 vpar_PictureDataGENERIC
860 #if (VPAR_OPTIM_LEVEL > 0)
861 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
864 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
865 vpar_PictureDataGENERIC
870 ppf_picture_data[p_vpar->picture.i_structure]
871 [p_vpar->picture.i_coding_type]( p_vpar );
875 #if (VPAR_OPTIM_LEVEL > 1)
876 static f_picture_data_t pf_picture_data[5] =
877 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
878 vpar_PictureData1D };
880 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
882 vpar_PictureDataGENERIC( p_vpar );
886 /* Wait for all the macroblocks to be decoded. */
887 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
889 /* Re-spawn decoder threads if the user changed settings. */
890 vpar_SpawnPool( p_vpar );
892 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
897 if( p_vpar->sequence.p_forward != NULL &&
898 p_vpar->sequence.p_forward->i_planes == 0 )
900 /* This can only happen with the fake picture created for section
901 * 7.6.3.5. Clean up our mess. */
902 p_vpar->sequence.p_forward = NULL;
905 if( p_vpar->picture.b_error )
908 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
910 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
911 p_vpar->picture.i_structure, 1 );
912 vout_DestroyPicture( p_vpar->p_vout, P_picture );
914 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
916 /* Prepare context for the next picture. */
918 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
919 p_vpar->picture.i_current_structure = 0;
921 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
923 /* Frame completely parsed. */
924 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
925 p_vpar->picture.i_structure, 0 );
926 vout_DisplayPicture( p_vpar->p_vout, P_picture );
928 /* Prepare context for the next picture. */
930 p_vpar->picture.i_current_structure = 0;
935 /*****************************************************************************
936 * ExtensionAndUserData : Parse the extension_and_user_data structure
937 *****************************************************************************/
938 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
940 while( !p_vpar->p_fifo->b_die )
942 NextStartCode( &p_vpar->bit_stream );
943 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
945 case EXTENSION_START_CODE:
946 RemoveBits32( &p_vpar->bit_stream );
947 switch( GetBits( &p_vpar->bit_stream, 4 ) )
949 case SEQUENCE_DISPLAY_EXTENSION_ID:
950 SequenceDisplayExtension( p_vpar );
952 case QUANT_MATRIX_EXTENSION_ID:
953 QuantMatrixExtension( p_vpar );
955 case SEQUENCE_SCALABLE_EXTENSION_ID:
956 SequenceScalableExtension( p_vpar );
958 case PICTURE_DISPLAY_EXTENSION_ID:
959 PictureDisplayExtension( p_vpar );
961 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
962 PictureSpatialScalableExtension( p_vpar );
964 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
965 PictureTemporalScalableExtension( p_vpar );
967 case COPYRIGHT_EXTENSION_ID:
968 CopyrightExtension( p_vpar );
975 case USER_DATA_START_CODE:
976 RemoveBits32( &p_vpar->bit_stream );
977 /* Wait for the next start code */
987 /*****************************************************************************
988 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
989 *****************************************************************************/
991 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
993 /* We don't care sequence_display_extension. */
995 RemoveBits( &p_vpar->bit_stream, 3 );
996 if( GetBits( &p_vpar->bit_stream, 1 ) )
998 /* Two bytes for color_desciption */
999 RemoveBits( &p_vpar->bit_stream, 16 );
1000 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
1002 /* display_horizontal and vertical_size and a marker_bit */
1003 RemoveBits( &p_vpar->bit_stream, 29 );
1007 /*****************************************************************************
1008 * QuantMatrixExtension : Load quantization matrices for luminance *
1010 *****************************************************************************/
1012 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
1014 if( GetBits( &p_vpar->bit_stream, 1 ) )
1016 /* Load intra_quantiser_matrix for luminance. */
1017 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1021 /* Use the default matrix. */
1022 LinkMatrix( &p_vpar->sequence.intra_quant,
1023 p_vpar->pi_default_intra_quant );
1025 if( GetBits( &p_vpar->bit_stream, 1 ) )
1027 /* Load non_intra_quantiser_matrix for luminance. */
1028 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1032 /* Use the default matrix. */
1033 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1034 p_vpar->pi_default_nonintra_quant );
1036 if( GetBits( &p_vpar->bit_stream, 1 ) )
1038 /* Load intra_quantiser_matrix for chrominance. */
1039 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1043 /* Link the chrominance intra matrix to the luminance one. */
1044 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1045 p_vpar->sequence.intra_quant.pi_matrix );
1047 if( GetBits( &p_vpar->bit_stream, 1 ) )
1049 /* Load non_intra_quantiser_matrix for chrominance. */
1050 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1054 /* Link the chrominance intra matrix to the luminance one. */
1055 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1056 p_vpar->sequence.intra_quant.pi_matrix );
1058 if( GetBits( &p_vpar->bit_stream, 1 ) )
1060 /* Load non_intra_quantiser_matrix for chrominance. */
1061 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1065 /* Link the chrominance nonintra matrix to the luminance one. */
1066 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1067 p_vpar->sequence.nonintra_quant.pi_matrix );
1072 /*****************************************************************************
1073 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1074 * structure to handle scalable coding *
1075 *****************************************************************************/
1077 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1079 /* We don't care about anything scalable except the scalable mode. */
1080 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1081 /* The length of the structure depends on the value of the scalable_mode */
1084 RemoveBits32( &p_vpar->bit_stream );
1085 RemoveBits( &p_vpar->bit_stream, 21 );
1088 RemoveBits( &p_vpar->bit_stream, 12 );
1091 RemoveBits( &p_vpar->bit_stream, 4 );
1095 /*****************************************************************************
1096 * PictureDisplayExtension : Parse the picture_display_extension structure *
1097 *****************************************************************************/
1099 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1101 /* Number of frame center offset */
1103 /* I am not sure it works but it should
1104 (fewer tests than shown in §6.3.12) */
1105 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1106 p_vpar->picture.b_repeat_first_field +
1107 p_vpar->picture.b_top_field_first
1108 : ( p_vpar->picture.b_frame_structure + 1 ) +
1109 p_vpar->picture.b_repeat_first_field;
1110 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1112 RemoveBits( &p_vpar->bit_stream, 17 );
1113 RemoveBits( &p_vpar->bit_stream, 17 );
1118 /*****************************************************************************
1119 * PictureSpatialScalableExtension *
1120 *****************************************************************************/
1122 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1124 /* That's scalable, so we trash it */
1125 RemoveBits32( &p_vpar->bit_stream );
1126 RemoveBits( &p_vpar->bit_stream, 16 );
1130 /*****************************************************************************
1131 * PictureTemporalScalableExtension *
1132 *****************************************************************************/
1134 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1136 /* Scalable again, trashed again */
1137 RemoveBits( &p_vpar->bit_stream, 23 );
1141 /*****************************************************************************
1142 * CopyrightExtension : Keeps some legal informations *
1143 *****************************************************************************/
1145 static void CopyrightExtension( vpar_thread_t * p_vpar )
1147 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1148 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1149 /* A flag that says whether the copyright information is significant */
1150 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1151 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1152 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1154 RemoveBits( &p_vpar->bit_stream, 8 );
1155 /* The copyright_number is split in three parts */
1157 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1158 RemoveBits( &p_vpar->bit_stream, 1 );
1160 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1161 RemoveBits( &p_vpar->bit_stream, 1 );
1162 /* third part and sum */
1163 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1164 ( (u64)i_copyright_nb_2 << 22 ) |
1165 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );