1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: vpar_headers.c,v 1.2.2.1 2001/12/10 10:59:14 massiot 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 *****************************************************************************/
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* memcpy(), memset() */
40 #include "video_output.h"
42 #include "modules_export.h"
43 #include "stream_control.h"
44 #include "input_ext-dec.h"
47 #include "vdec_ext-plugins.h"
48 #include "vpar_pool.h"
49 #include "video_parser.h"
50 #include "video_decoder.h"
55 static __inline__ void NextStartCode( bit_stream_t * );
56 static void SequenceHeader( vpar_thread_t * p_vpar );
57 static void GroupHeader( vpar_thread_t * p_vpar );
58 static void PictureHeader( vpar_thread_t * p_vpar );
59 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
60 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
61 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
62 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
63 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
64 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
65 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
66 static void CopyrightExtension( vpar_thread_t * p_vpar );
72 /*****************************************************************************
73 * pi_default_intra_quant : default quantization matrix
74 *****************************************************************************/
75 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
77 8, 16, 19, 22, 26, 27, 29, 34,
78 16, 16, 22, 24, 27, 29, 34, 37,
79 19, 22, 26, 27, 29, 34, 34, 38,
80 22, 22, 26, 27, 29, 34, 37, 40,
81 22, 26, 27, 29, 32, 35, 40, 48,
82 26, 27, 29, 32, 35, 40, 48, 58,
83 26, 27, 29, 34, 38, 46, 56, 69,
84 27, 29, 35, 38, 46, 56, 69, 83
87 /*****************************************************************************
88 * pi_default_nonintra_quant : default quantization matrix
89 *****************************************************************************/
90 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
92 16, 16, 16, 16, 16, 16, 16, 16,
93 16, 16, 16, 16, 16, 16, 16, 16,
94 16, 16, 16, 16, 16, 16, 16, 16,
95 16, 16, 16, 16, 16, 16, 16, 16,
96 16, 16, 16, 16, 16, 16, 16, 16,
97 16, 16, 16, 16, 16, 16, 16, 16,
98 16, 16, 16, 16, 16, 16, 16, 16,
99 16, 16, 16, 16, 16, 16, 16, 16
102 /*****************************************************************************
103 * pi_scan : zig-zag and alternate scan patterns
104 *****************************************************************************/
105 u8 pi_scan[2][64] ATTR_ALIGN(16) =
107 { /* Zig-Zag pattern */
108 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
109 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
110 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
111 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
113 { /* Alternate scan pattern */
114 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
115 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
116 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
117 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
122 * Local inline functions.
125 /*****************************************************************************
126 * ReferenceUpdate : Update the reference pointers when we have a new picture
127 *****************************************************************************/
128 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
130 picture_t * p_newref )
132 if( i_coding_type != B_CODING_TYPE )
134 if( p_vpar->sequence.p_forward != NULL )
136 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
138 if( p_vpar->sequence.p_backward != NULL )
140 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
141 vpar_SynchroDate( p_vpar ) );
143 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
144 p_vpar->sequence.p_backward = p_newref;
145 if( p_newref != NULL )
147 vout_LinkPicture( p_vpar->p_vout, p_newref );
150 else if( p_newref != NULL )
152 /* Put date immediately. */
153 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
157 /*****************************************************************************
158 * ReferenceReplace : Replace the last reference pointer when we destroy
160 *****************************************************************************/
161 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
163 picture_t * p_newref )
165 if( i_coding_type != B_CODING_TYPE )
167 if( p_vpar->sequence.p_backward != NULL )
169 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
171 p_vpar->sequence.p_backward = p_newref;
172 if( p_newref != NULL )
174 vout_LinkPicture( p_vpar->p_vout, p_newref );
179 /*****************************************************************************
180 * LoadMatrix : Load a quantization matrix
181 *****************************************************************************/
182 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
183 quant_matrix_t * p_matrix )
187 if( !p_matrix->b_allocated )
189 /* Allocate a piece of memory to load the matrix. */
190 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
192 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
193 p_vpar->p_fifo->b_error = 1;
196 p_matrix->b_allocated = 1;
199 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
201 p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
202 = GetBits( &p_vpar->bit_stream, 8 );
206 /*****************************************************************************
207 * LinkMatrix : Link a quantization matrix to another
208 *****************************************************************************/
209 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
211 if( p_matrix->b_allocated )
213 /* Deallocate the piece of memory. */
214 free( p_matrix->pi_matrix );
215 p_matrix->b_allocated = 0;
218 p_matrix->pi_matrix = pi_array;
222 * Exported functions.
225 /*****************************************************************************
226 * vpar_NextSequenceHeader : Find the next sequence header
227 *****************************************************************************/
228 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
230 while( !p_vpar->p_fifo->b_die )
232 NextStartCode( &p_vpar->bit_stream );
233 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
237 RemoveBits( &p_vpar->bit_stream, 8 );
242 /*****************************************************************************
243 * vpar_ParseHeader : Parse the next header
244 *****************************************************************************/
245 int vpar_ParseHeader( vpar_thread_t * p_vpar )
247 while( !p_vpar->p_fifo->b_die )
249 NextStartCode( &p_vpar->bit_stream );
250 switch( GetBits32( &p_vpar->bit_stream ) )
252 case SEQUENCE_HEADER_CODE:
253 p_vpar->c_sequences++;
255 SequenceHeader( p_vpar );
259 case GROUP_START_CODE:
260 GroupHeader( p_vpar );
264 case PICTURE_START_CODE:
265 PictureHeader( p_vpar );
269 case SEQUENCE_END_CODE:
270 intf_DbgMsg("vpar debug: sequence end code received");
283 * Following functions are local
286 /*****************************************************************************
287 * SequenceHeader : Parse the next sequence header
288 *****************************************************************************/
289 static void SequenceHeader( vpar_thread_t * p_vpar )
292 static int i_frame_rate_table[16] =
303 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
307 int i_height_save, i_width_save;
309 i_height_save = p_vpar->sequence.i_height;
310 i_width_save = p_vpar->sequence.i_width;
312 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
313 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
314 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
315 p_vpar->sequence.i_frame_rate =
316 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
318 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
319 * constrained_parameters_flag */
320 RemoveBits( &p_vpar->bit_stream, 30 );
323 * Quantization matrices
325 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
327 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
331 /* Use default matrix. */
332 LinkMatrix( &p_vpar->sequence.intra_quant,
333 p_vpar->pi_default_intra_quant );
336 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
338 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
342 /* Use default matrix. */
343 LinkMatrix( &p_vpar->sequence.nonintra_quant,
344 p_vpar->pi_default_nonintra_quant );
347 /* Unless later overwritten by a matrix extension, we have the same
348 * matrices for luminance and chrominance. */
349 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
350 p_vpar->sequence.intra_quant.pi_matrix );
351 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
352 p_vpar->sequence.nonintra_quant.pi_matrix );
357 NextStartCode( &p_vpar->bit_stream );
358 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
362 /* Turn the MPEG2 flag on */
363 p_vpar->sequence.b_mpeg2 = 1;
365 /* Parse sequence_extension */
366 RemoveBits32( &p_vpar->bit_stream );
367 /* extension_start_code_identifier, profile_and_level_indication */
368 RemoveBits( &p_vpar->bit_stream, 12 );
369 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
370 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
371 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
372 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
373 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
375 RemoveBits( &p_vpar->bit_stream, 22 );
376 /* frame_rate_extension_n */
377 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
378 /* frame_rate_extension_d */
379 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
380 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
384 /* It's an MPEG-1 stream. Put adequate parameters. */
386 static int pi_mpeg1ratio[15] = {
404 if( p_vpar->sequence.i_aspect_ratio > 1 )
406 i_xyratio = p_vpar->sequence.i_height *
407 pi_mpeg1ratio[p_vpar->sequence.i_aspect_ratio] /
408 p_vpar->sequence.i_width;
409 if( 7450 < i_xyratio && i_xyratio < 7550 )
411 p_vpar->sequence.i_aspect_ratio = 2;
413 else if( 5575 < i_xyratio && i_xyratio < 5675 )
415 p_vpar->sequence.i_aspect_ratio = 3;
417 else if( 4475 < i_xyratio && i_xyratio < 4575 )
419 p_vpar->sequence.i_aspect_ratio = 4;
423 p_vpar->sequence.b_mpeg2 = 0;
424 p_vpar->sequence.b_progressive = 1;
425 p_vpar->sequence.i_chroma_format = CHROMA_420;
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" );
478 p_vpar->p_vout = vout_CreateThread( NULL, p_vpar->sequence.i_width,
479 p_vpar->sequence.i_height );
481 /* Everything failed */
482 if( p_vpar->p_vout == NULL )
484 intf_ErrMsg( "vpar error: can't open vout, aborting" );
485 vlc_mutex_unlock( &p_vout_bank->lock );
487 /* XXX ! XXX ! XXX ! what to do here ? */
491 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
492 p_vout_bank->i_count++;
496 /* Take the first video output FIXME: take the best one */
497 p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
500 vlc_mutex_unlock( &p_vout_bank->lock );
503 /*****************************************************************************
504 * GroupHeader : Parse the next group of pictures header
505 *****************************************************************************/
506 static void GroupHeader( vpar_thread_t * p_vpar )
508 /* Nothing to do, we don't care. */
509 RemoveBits( &p_vpar->bit_stream, 27 );
510 ExtensionAndUserData( p_vpar );
513 /*****************************************************************************
514 * PictureHeader : Parse the next picture header
515 *****************************************************************************/
516 static void PictureHeader( vpar_thread_t * p_vpar )
518 int i_structure, i_previous_coding_type;
519 boolean_t b_parsable = 0;
521 /* Recover in case of stream discontinuity. */
522 if( p_vpar->sequence.b_expect_discontinuity )
524 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
525 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
526 if( p_vpar->picture.p_picture != NULL )
528 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
529 p_vpar->picture.p_picture = NULL;
531 p_vpar->picture.i_current_structure = 0;
532 p_vpar->sequence.b_expect_discontinuity = 0;
535 /* Parse the picture header. */
536 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
537 i_previous_coding_type = p_vpar->picture.i_coding_type;
538 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
539 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
541 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
542 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
544 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
545 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
548 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
550 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
551 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
555 /* extra_information_picture */
556 while( GetBits( &p_vpar->bit_stream, 1 ) )
558 RemoveBits( &p_vpar->bit_stream, 8 );
562 * Picture Coding Extension
564 NextStartCode( &p_vpar->bit_stream );
565 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
567 /* Parse picture_coding_extension */
568 RemoveBits32( &p_vpar->bit_stream );
569 /* extension_start_code_identifier */
570 RemoveBits( &p_vpar->bit_stream, 4 );
572 /* Pre-substract 1 for later use in MotionDelta(). */
573 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
574 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
575 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
576 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
577 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
578 i_structure = GetBits( &p_vpar->bit_stream, 2 );
579 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
580 p_vpar->picture.b_frame_pred_frame_dct
581 = GetBits( &p_vpar->bit_stream, 1 );
582 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
583 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
584 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
586 p_vpar->picture.pi_scan =
587 p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
588 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
589 /* chroma_420_type (obsolete) */
590 RemoveBits( &p_vpar->bit_stream, 1 );
591 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
593 /* composite_display_flag */
594 if( GetBits( &p_vpar->bit_stream, 1 ) )
596 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
597 * sub_carrier_phase */
598 RemoveBits( &p_vpar->bit_stream, 20 );
603 /* MPEG-1 compatibility flags */
604 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
605 i_structure = FRAME_STRUCTURE;
606 p_vpar->picture.b_top_field_first = 0;
607 p_vpar->picture.b_frame_pred_frame_dct = 1;
608 p_vpar->picture.b_concealment_mv = 0;
609 p_vpar->picture.b_q_scale_type = 0;
610 p_vpar->picture.b_intra_vlc_format = 0;
611 p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
612 p_vpar->picture.b_repeat_first_field = 0;
613 p_vpar->picture.b_progressive = 1;
616 /* Extension and User data. */
617 ExtensionAndUserData( p_vpar );
619 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
621 if( p_vpar->picture.i_current_structure )
623 if ( (i_structure == FRAME_STRUCTURE ||
624 i_structure == p_vpar->picture.i_current_structure) )
626 /* We don't have the second field of the buffered frame. */
627 if( p_vpar->picture.p_picture != NULL )
629 ReferenceReplace( p_vpar,
630 p_vpar->picture.i_coding_type,
632 vout_DestroyPicture( p_vpar->p_vout,
633 p_vpar->picture.p_picture );
634 p_vpar->picture.p_picture = NULL;
637 p_vpar->picture.i_current_structure = 0;
639 intf_WarnMsg( 2, "Odd number of field pictures." );
643 /* Second field of a frame. We will decode it if, and only if we
644 * have decoded the first field. */
645 if( p_vpar->picture.p_picture == NULL )
647 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
648 && p_vpar->sequence.p_backward == NULL) )
650 /* Exceptionnally, parse the picture if it is I. We need
651 * this in case of an odd number of field pictures, if the
652 * previous picture is not intra, we desperately need a
653 * new reference picture. OK, this is kind of kludgy. */
654 p_vpar->picture.i_current_structure = 0;
663 /* We suppose we have the reference pictures, since we already
664 * decoded the first field and the second field will not need
665 * any extra reference picture. There is a special case of
666 * P field being the second field of an I field, but ISO/IEC
667 * 13818-2 section 7.6.3.5 specifies that this P field will
668 * not need any reference picture besides the I field. So far
672 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
673 i_previous_coding_type == I_CODING_TYPE &&
674 p_vpar->sequence.p_forward == NULL )
676 /* This is the special case of section 7.6.3.5. Create
677 * a fake reference picture (which will not be used)
678 * but will prevent us from segfaulting in the slice
680 static picture_t fake_picture;
681 fake_picture.p_data = NULL; /* We will use it later */
682 p_vpar->sequence.p_forward = &fake_picture;
688 if( !p_vpar->picture.i_current_structure )
690 /* First field of a frame, or new frame picture. */
693 /* Do we have the reference pictures ? */
694 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
695 p_vpar->picture.b_concealment_mv) &&
696 (p_vpar->sequence.p_backward == NULL)) ||
697 /* p_backward will become p_forward later */
698 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
699 (p_vpar->sequence.p_forward == NULL ||
700 p_vpar->sequence.p_backward == NULL)));
702 /* Compute the number of times the frame will be emitted by the
703 * decoder (number of half-periods). */
704 if( p_vpar->sequence.b_progressive )
706 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
707 + p_vpar->picture.b_top_field_first) * 2;
711 if( p_vpar->picture.b_progressive )
713 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
721 /* Warn synchro we have a new picture (updates pictures index). */
722 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
727 /* Does synchro say we have enough time to decode it ? */
728 b_parsable = vpar_SynchroChoose( p_vpar,
729 p_vpar->picture.i_coding_type, i_structure );
735 /* Update the reference pointers. */
736 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
738 /* Update context. */
739 if( i_structure != FRAME_STRUCTURE )
741 if( (p_vpar->picture.i_current_structure | i_structure)
744 /* The frame is complete. */
745 p_vpar->picture.i_current_structure = 0;
747 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
751 p_vpar->picture.i_current_structure = i_structure;
756 /* Warn Synchro we have trashed a picture. */
757 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
759 p_vpar->picture.p_picture = NULL;
764 /* OK, now we are sure we will decode the picture. */
765 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
767 #define P_picture p_vpar->picture.p_picture
768 p_vpar->picture.b_error = 0;
769 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
771 if( !p_vpar->picture.i_current_structure )
773 /* This is a new frame. Get a structure from the video_output. */
774 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
775 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
776 p_vpar->sequence.i_width,
777 p_vpar->sequence.i_height ) )
780 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
781 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
785 msleep( VPAR_OUTMEM_SLEEP );
788 /* Initialize values. */
789 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
790 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
791 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
792 p_vpar->picture.i_field_width = ( p_vpar->sequence.i_width
793 << ( 1 - p_vpar->picture.b_frame_structure ) );
795 /* FIXME ! remove asap ?? */
796 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
798 /* Update the reference pointers. */
799 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
802 /* Initialize picture data for decoding. */
803 p_vpar->picture.i_current_structure |= i_structure;
804 p_vpar->picture.i_structure = i_structure;
805 p_vpar->picture.b_second_field =
806 (i_structure != p_vpar->picture.i_current_structure);
807 p_vpar->picture.b_current_field =
808 (i_structure == BOTTOM_FIELD );
810 if( !p_vpar->p_config->p_stream_ctrl->b_grayscale )
812 switch( p_vpar->sequence.i_chroma_format )
815 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock422;
818 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock444;
822 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock420;
828 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblockBW;
832 if( p_vpar->sequence.b_mpeg2 )
834 static f_picture_data_t ppf_picture_data[4][4] =
837 NULL, NULL, NULL, NULL
841 #if (VPAR_OPTIM_LEVEL > 1)
842 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
845 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
846 vpar_PictureDataGENERIC
851 #if (VPAR_OPTIM_LEVEL > 1)
852 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
855 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
856 vpar_PictureDataGENERIC
861 #if (VPAR_OPTIM_LEVEL > 0)
862 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
865 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
866 vpar_PictureDataGENERIC
871 ppf_picture_data[p_vpar->picture.i_structure]
872 [p_vpar->picture.i_coding_type]( p_vpar );
876 #if (VPAR_OPTIM_LEVEL > 1)
877 static f_picture_data_t pf_picture_data[5] =
878 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
879 vpar_PictureData1D };
881 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
883 vpar_PictureDataGENERIC( p_vpar );
887 /* Wait for all the macroblocks to be decoded. */
888 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
890 /* Re-spawn decoder threads if the user changed settings. */
891 vpar_SpawnPool( p_vpar );
893 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
898 if( p_vpar->sequence.p_forward != NULL &&
899 p_vpar->sequence.p_forward->p_data == NULL )
901 /* This can only happen with the fake picture created for section
902 * 7.6.3.5. Clean up our mess. */
903 p_vpar->sequence.p_forward = NULL;
906 if( p_vpar->picture.b_error )
909 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
911 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
912 p_vpar->picture.i_structure, 1 );
913 vout_DestroyPicture( p_vpar->p_vout, P_picture );
915 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
917 /* Prepare context for the next picture. */
919 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
920 p_vpar->picture.i_current_structure = 0;
922 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
924 /* Frame completely parsed. */
925 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
926 p_vpar->picture.i_structure, 0 );
927 vout_DisplayPicture( p_vpar->p_vout, P_picture );
929 /* Prepare context for the next picture. */
931 p_vpar->picture.i_current_structure = 0;
936 /*****************************************************************************
937 * ExtensionAndUserData : Parse the extension_and_user_data structure
938 *****************************************************************************/
939 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
941 while( !p_vpar->p_fifo->b_die )
943 NextStartCode( &p_vpar->bit_stream );
944 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
946 case EXTENSION_START_CODE:
947 RemoveBits32( &p_vpar->bit_stream );
948 switch( GetBits( &p_vpar->bit_stream, 4 ) )
950 case SEQUENCE_DISPLAY_EXTENSION_ID:
951 SequenceDisplayExtension( p_vpar );
953 case QUANT_MATRIX_EXTENSION_ID:
954 QuantMatrixExtension( p_vpar );
956 case SEQUENCE_SCALABLE_EXTENSION_ID:
957 SequenceScalableExtension( p_vpar );
959 case PICTURE_DISPLAY_EXTENSION_ID:
960 PictureDisplayExtension( p_vpar );
962 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
963 PictureSpatialScalableExtension( p_vpar );
965 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
966 PictureTemporalScalableExtension( p_vpar );
968 case COPYRIGHT_EXTENSION_ID:
969 CopyrightExtension( p_vpar );
976 case USER_DATA_START_CODE:
977 RemoveBits32( &p_vpar->bit_stream );
978 /* Wait for the next start code */
988 /*****************************************************************************
989 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
990 *****************************************************************************/
992 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
994 /* We don't care sequence_display_extension. */
996 RemoveBits( &p_vpar->bit_stream, 3 );
997 if( GetBits( &p_vpar->bit_stream, 1 ) )
999 /* Two bytes for color_desciption */
1000 RemoveBits( &p_vpar->bit_stream, 16 );
1001 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
1003 /* display_horizontal and vertical_size and a marker_bit */
1004 RemoveBits( &p_vpar->bit_stream, 29 );
1008 /*****************************************************************************
1009 * QuantMatrixExtension : Load quantization matrices for luminance *
1011 *****************************************************************************/
1013 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
1015 if( GetBits( &p_vpar->bit_stream, 1 ) )
1017 /* Load intra_quantiser_matrix for luminance. */
1018 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1022 /* Use the default matrix. */
1023 LinkMatrix( &p_vpar->sequence.intra_quant,
1024 p_vpar->pi_default_intra_quant );
1026 if( GetBits( &p_vpar->bit_stream, 1 ) )
1028 /* Load non_intra_quantiser_matrix for luminance. */
1029 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1033 /* Use the default matrix. */
1034 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1035 p_vpar->pi_default_nonintra_quant );
1037 if( GetBits( &p_vpar->bit_stream, 1 ) )
1039 /* Load intra_quantiser_matrix for chrominance. */
1040 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1044 /* Link the chrominance intra matrix to the luminance one. */
1045 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1046 p_vpar->sequence.intra_quant.pi_matrix );
1048 if( GetBits( &p_vpar->bit_stream, 1 ) )
1050 /* Load non_intra_quantiser_matrix for chrominance. */
1051 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1055 /* Link the chrominance intra matrix to the luminance one. */
1056 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1057 p_vpar->sequence.intra_quant.pi_matrix );
1059 if( GetBits( &p_vpar->bit_stream, 1 ) )
1061 /* Load non_intra_quantiser_matrix for chrominance. */
1062 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1066 /* Link the chrominance nonintra matrix to the luminance one. */
1067 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1068 p_vpar->sequence.nonintra_quant.pi_matrix );
1073 /*****************************************************************************
1074 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1075 * structure to handle scalable coding *
1076 *****************************************************************************/
1078 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1080 /* We don't care about anything scalable except the scalable mode. */
1081 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1082 /* The length of the structure depends on the value of the scalable_mode */
1085 RemoveBits32( &p_vpar->bit_stream );
1086 RemoveBits( &p_vpar->bit_stream, 21 );
1089 RemoveBits( &p_vpar->bit_stream, 12 );
1092 RemoveBits( &p_vpar->bit_stream, 4 );
1096 /*****************************************************************************
1097 * PictureDisplayExtension : Parse the picture_display_extension structure *
1098 *****************************************************************************/
1100 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1102 /* Number of frame center offset */
1104 /* I am not sure it works but it should
1105 (fewer tests than shown in §6.3.12) */
1106 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1107 p_vpar->picture.b_repeat_first_field +
1108 p_vpar->picture.b_top_field_first
1109 : ( p_vpar->picture.b_frame_structure + 1 ) +
1110 p_vpar->picture.b_repeat_first_field;
1111 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1113 RemoveBits( &p_vpar->bit_stream, 17 );
1114 RemoveBits( &p_vpar->bit_stream, 17 );
1119 /*****************************************************************************
1120 * PictureSpatialScalableExtension *
1121 *****************************************************************************/
1123 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1125 /* That's scalable, so we trash it */
1126 RemoveBits32( &p_vpar->bit_stream );
1127 RemoveBits( &p_vpar->bit_stream, 16 );
1131 /*****************************************************************************
1132 * PictureTemporalScalableExtension *
1133 *****************************************************************************/
1135 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1137 /* Scalable again, trashed again */
1138 RemoveBits( &p_vpar->bit_stream, 23 );
1142 /*****************************************************************************
1143 * CopyrightExtension : Keeps some legal informations *
1144 *****************************************************************************/
1146 static void CopyrightExtension( vpar_thread_t * p_vpar )
1148 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1149 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1150 /* A flag that says whether the copyright information is significant */
1151 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1152 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1153 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1155 RemoveBits( &p_vpar->bit_stream, 8 );
1156 /* The copyright_number is split in three parts */
1158 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1159 RemoveBits( &p_vpar->bit_stream, 1 );
1161 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1162 RemoveBits( &p_vpar->bit_stream, 1 );
1163 /* third part and sum */
1164 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1165 ( (u64)i_copyright_nb_2 << 22 ) |
1166 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );