1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.2 2001/07/17 09:48:08 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 "stream_control.h"
41 #include "input_ext-dec.h"
44 #include "video_output.h"
46 #include "vdec_ext-plugins.h"
47 #include "video_decoder.h"
49 #include "vpar_blocks.h"
50 #include "vpar_headers.h"
51 #include "vpar_synchro.h"
52 #include "video_parser.h"
53 #include "video_fifo.h"
58 static __inline__ void NextStartCode( bit_stream_t * );
59 static void SequenceHeader( vpar_thread_t * p_vpar );
60 static void GroupHeader( vpar_thread_t * p_vpar );
61 static void PictureHeader( vpar_thread_t * p_vpar );
62 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
63 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
64 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
65 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
66 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
67 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
68 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
69 static void CopyrightExtension( vpar_thread_t * p_vpar );
75 /*****************************************************************************
76 * pi_default_intra_quant : default quantization matrix
77 *****************************************************************************/
78 u8 pi_default_intra_quant[] =
80 8, 16, 19, 22, 26, 27, 29, 34,
81 16, 16, 22, 24, 27, 29, 34, 37,
82 19, 22, 26, 27, 29, 34, 34, 38,
83 22, 22, 26, 27, 29, 34, 37, 40,
84 22, 26, 27, 29, 32, 35, 40, 48,
85 26, 27, 29, 32, 35, 40, 48, 58,
86 26, 27, 29, 34, 38, 46, 56, 69,
87 27, 29, 35, 38, 46, 56, 69, 83
90 /*****************************************************************************
91 * pi_default_nonintra_quant : default quantization matrix
92 *****************************************************************************/
93 u8 pi_default_nonintra_quant[] =
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,
100 16, 16, 16, 16, 16, 16, 16, 16,
101 16, 16, 16, 16, 16, 16, 16, 16,
102 16, 16, 16, 16, 16, 16, 16, 16
105 /*****************************************************************************
106 * pi_scan : zig-zag and alternate scan patterns
107 *****************************************************************************/
110 { /* Zig-Zag pattern */
111 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
112 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
113 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
114 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
116 { /* Alternate scan pattern */
117 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
118 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
119 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
120 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
125 * Local inline functions.
128 /*****************************************************************************
129 * ReferenceUpdate : Update the reference pointers when we have a new picture
130 *****************************************************************************/
131 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
133 picture_t * p_newref )
135 if( i_coding_type != B_CODING_TYPE )
137 if( p_vpar->sequence.p_forward != NULL )
139 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
141 if( p_vpar->sequence.p_backward != NULL )
143 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
144 vpar_SynchroDate( p_vpar ) );
146 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
147 p_vpar->sequence.p_backward = p_newref;
148 if( p_newref != NULL )
150 vout_LinkPicture( p_vpar->p_vout, p_newref );
153 else if( p_newref != NULL )
155 /* Put date immediately. */
156 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
160 /*****************************************************************************
161 * ReferenceReplace : Replace the last reference pointer when we destroy
163 *****************************************************************************/
164 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
166 picture_t * p_newref )
168 if( i_coding_type != B_CODING_TYPE )
170 if( p_vpar->sequence.p_backward != NULL )
172 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
174 p_vpar->sequence.p_backward = p_newref;
175 if( p_newref != NULL )
177 vout_LinkPicture( p_vpar->p_vout, p_newref );
182 /*****************************************************************************
183 * LoadMatrix : Load a quantization matrix
184 *****************************************************************************/
185 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
186 quant_matrix_t * p_matrix )
190 if( !p_matrix->b_allocated )
192 /* Allocate a piece of memory to load the matrix. */
193 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
195 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
196 p_vpar->p_fifo->b_error = 1;
199 p_matrix->b_allocated = 1;
202 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
204 p_matrix->pi_matrix[p_vpar->ppi_scan[SCAN_ZIGZAG][i_dummy]]
205 = GetBits( &p_vpar->bit_stream, 8 );
209 /* Discrete Fourier Transform requires the quantization matrices to
210 * be normalized before using them. */
211 vdec_NormQuantMatrix( p_matrix->pi_matrix );
215 /*****************************************************************************
216 * LinkMatrix : Link a quantization matrix to another
217 *****************************************************************************/
218 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
220 if( p_matrix->b_allocated )
222 /* Deallocate the piece of memory. */
223 free( p_matrix->pi_matrix );
224 p_matrix->b_allocated = 0;
227 p_matrix->pi_matrix = pi_array;
231 * Exported functions.
234 /*****************************************************************************
235 * vpar_NextSequenceHeader : Find the next sequence header
236 *****************************************************************************/
237 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
239 while( !p_vpar->p_fifo->b_die )
241 NextStartCode( &p_vpar->bit_stream );
242 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
246 RemoveBits( &p_vpar->bit_stream, 8 );
251 /*****************************************************************************
252 * vpar_ParseHeader : Parse the next header
253 *****************************************************************************/
254 int vpar_ParseHeader( vpar_thread_t * p_vpar )
256 while( !p_vpar->p_fifo->b_die )
258 NextStartCode( &p_vpar->bit_stream );
259 switch( GetBits32( &p_vpar->bit_stream ) )
261 case SEQUENCE_HEADER_CODE:
263 p_vpar->c_sequences++;
265 SequenceHeader( p_vpar );
269 case GROUP_START_CODE:
270 GroupHeader( p_vpar );
274 case PICTURE_START_CODE:
275 PictureHeader( p_vpar );
279 case SEQUENCE_END_CODE:
280 intf_DbgMsg("vpar debug: sequence end code received");
293 * Following functions are local
296 /*****************************************************************************
297 * SequenceHeader : Parse the next sequence header
298 *****************************************************************************/
299 static void SequenceHeader( vpar_thread_t * p_vpar )
302 static int i_frame_rate_table[16] =
313 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
317 int i_height_save, i_width_save;
319 i_height_save = p_vpar->sequence.i_height;
320 i_width_save = p_vpar->sequence.i_width;
322 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
323 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
324 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
325 p_vpar->sequence.i_frame_rate =
326 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
328 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
329 * constrained_parameters_flag */
330 RemoveBits( &p_vpar->bit_stream, 30 );
333 * Quantization matrices
335 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
337 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
341 /* Use default matrix. */
342 LinkMatrix( &p_vpar->sequence.intra_quant,
343 p_vpar->pi_default_intra_quant );
346 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
348 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
352 /* Use default matrix. */
353 LinkMatrix( &p_vpar->sequence.nonintra_quant,
354 p_vpar->pi_default_nonintra_quant );
357 /* Unless later overwritten by a matrix extension, we have the same
358 * matrices for luminance and chrominance. */
359 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
360 p_vpar->sequence.intra_quant.pi_matrix );
361 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
362 p_vpar->sequence.nonintra_quant.pi_matrix );
367 NextStartCode( &p_vpar->bit_stream );
368 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
372 /* Turn the MPEG2 flag on */
373 p_vpar->sequence.b_mpeg2 = 1;
375 /* Parse sequence_extension */
376 RemoveBits32( &p_vpar->bit_stream );
377 /* extension_start_code_identifier, profile_and_level_indication */
378 RemoveBits( &p_vpar->bit_stream, 12 );
379 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
380 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
381 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
382 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
383 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
385 RemoveBits( &p_vpar->bit_stream, 22 );
386 /* frame_rate_extension_n */
387 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
388 /* frame_rate_extension_d */
389 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
390 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
394 /* It's an MPEG-1 stream. Put adequate parameters. */
396 p_vpar->sequence.b_mpeg2 = 0;
397 p_vpar->sequence.b_progressive = 1;
398 p_vpar->sequence.i_chroma_format = CHROMA_420;
402 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
403 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
404 (p_vpar->sequence.i_height + 15) / 16 :
405 2 * ((p_vpar->sequence.i_height + 31) / 32);
406 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
407 * p_vpar->sequence.i_mb_height;
408 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
409 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
410 p_vpar->sequence.i_size = p_vpar->sequence.i_width
411 * p_vpar->sequence.i_height;
413 /* Update chromatic information. */
414 switch( p_vpar->sequence.i_chroma_format )
417 p_vpar->sequence.i_chroma_nb_blocks = 2;
418 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
419 p_vpar->sequence.i_chroma_mb_width = 8;
420 p_vpar->sequence.i_chroma_mb_height = 8;
424 p_vpar->sequence.i_chroma_nb_blocks = 4;
425 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
426 p_vpar->sequence.i_chroma_mb_width = 8;
427 p_vpar->sequence.i_chroma_mb_height = 16;
431 p_vpar->sequence.i_chroma_nb_blocks = 8;
432 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
433 p_vpar->sequence.i_chroma_mb_width = 16;
434 p_vpar->sequence.i_chroma_mb_height = 16;
437 /* Reset scalable_mode. */
438 p_vpar->sequence.i_scalable_mode = SC_NONE;
441 if( p_vpar->sequence.i_width != i_width_save
442 || p_vpar->sequence.i_height != i_height_save )
444 /* FIXME: What do we do in case of a size change ?? */
448 /* Extension and User data */
449 ExtensionAndUserData( p_vpar );
451 /* XXX: The vout request and fifo opening will eventually be here */
453 /* Spawn an audio output if there is none */
454 vlc_mutex_lock( &p_vout_bank->lock );
456 if( p_vout_bank->i_count == 0 )
458 intf_WarnMsg( 1, "vpar: no vout present, spawning one" );
460 p_vpar->p_vout = vout_CreateThread( NULL );
462 /* Everything failed */
463 if( p_vpar->p_vout == NULL )
465 intf_ErrMsg( "vpar error: can't open vout, aborting" );
466 vlc_mutex_unlock( &p_vout_bank->lock );
468 /* XXX ! XXX ! XXX ! what to do here ? */
472 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
473 p_vout_bank->i_count++;
477 /* Take the first video output FIXME: take the best one */
478 p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
481 vlc_mutex_unlock( &p_vout_bank->lock );
484 /*****************************************************************************
485 * GroupHeader : Parse the next group of pictures header
486 *****************************************************************************/
487 static void GroupHeader( vpar_thread_t * p_vpar )
489 /* Nothing to do, we don't care. */
490 RemoveBits( &p_vpar->bit_stream, 27 );
491 ExtensionAndUserData( p_vpar );
494 /*****************************************************************************
495 * PictureHeader : Parse the next picture header
496 *****************************************************************************/
497 static void PictureHeader( vpar_thread_t * p_vpar )
501 boolean_t b_parsable;
506 /* Recover in case of stream discontinuity. */
507 if( p_vpar->sequence.b_expect_discontinuity )
509 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
510 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
511 if( p_vpar->picture.p_picture != NULL )
516 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
518 vpar_DestroyMacroblock( &p_vpar->vfifo,
519 p_vpar->picture.pp_mb[i_mb] );
522 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
524 p_vpar->sequence.b_expect_discontinuity = 0;
527 /* Parse the picture header. */
528 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
529 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
530 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
532 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
533 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
535 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
536 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
538 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
540 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
541 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
544 /* extra_information_picture */
545 while( GetBits( &p_vpar->bit_stream, 1 ) )
547 RemoveBits( &p_vpar->bit_stream, 8 );
551 * Picture Coding Extension
553 NextStartCode( &p_vpar->bit_stream );
554 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
556 /* Parse picture_coding_extension */
557 RemoveBits32( &p_vpar->bit_stream );
558 /* extension_start_code_identifier */
559 RemoveBits( &p_vpar->bit_stream, 4 );
561 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
562 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
563 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
564 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
565 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
566 i_structure = GetBits( &p_vpar->bit_stream, 2 );
567 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
568 p_vpar->picture.b_frame_pred_frame_dct
569 = GetBits( &p_vpar->bit_stream, 1 );
570 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
571 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
572 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
573 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
574 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
575 /* chroma_420_type (obsolete) */
576 RemoveBits( &p_vpar->bit_stream, 1 );
577 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
579 /* composite_display_flag */
580 if( GetBits( &p_vpar->bit_stream, 1 ) )
582 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
583 * sub_carrier_phase */
584 RemoveBits( &p_vpar->bit_stream, 20 );
589 /* MPEG-1 compatibility flags */
590 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
591 i_structure = FRAME_STRUCTURE;
592 p_vpar->picture.b_top_field_first = 0;
593 p_vpar->picture.b_frame_pred_frame_dct = 1;
594 p_vpar->picture.b_concealment_mv = 0;
595 p_vpar->picture.b_q_scale_type = 0;
596 p_vpar->picture.b_intra_vlc_format = 0;
597 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
598 p_vpar->picture.b_repeat_first_field = 0;
599 p_vpar->picture.b_progressive = 1;
603 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
606 if( p_vpar->picture.i_current_structure &&
607 (i_structure == FRAME_STRUCTURE ||
608 i_structure == p_vpar->picture.i_current_structure) )
610 /* We don't have the second field of the buffered frame. */
611 if( p_vpar->picture.p_picture != NULL )
613 ReferenceReplace( p_vpar,
614 p_vpar->picture.i_coding_type,
618 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
620 vpar_DestroyMacroblock( &p_vpar->vfifo,
621 p_vpar->picture.pp_mb[i_mb] );
624 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
627 p_vpar->picture.i_current_structure = 0;
629 intf_WarnMsg( 2, "Odd number of field pictures." );
632 /* Do we have the reference pictures ? */
633 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
634 (p_vpar->sequence.p_backward == NULL)) ||
635 /* p_backward will become p_forward later */
636 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
637 (p_vpar->sequence.p_forward == NULL ||
638 p_vpar->sequence.p_backward == NULL)));
640 if( p_vpar->picture.i_current_structure )
642 /* Second field of a frame. We will decode it if, and only if we
643 * have decoded the first field. */
646 b_parsable = (p_vpar->picture.p_picture != NULL);
653 /* Compute the number of times the frame will be emitted by the
654 * decoder (number of half-periods). */
655 if( p_vpar->sequence.b_progressive )
657 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
658 + p_vpar->picture.b_top_field_first) * 2;
662 if( p_vpar->picture.b_progressive )
664 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
672 /* Warn synchro we have a new picture (updates pictures index). */
673 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
678 /* Does synchro say we have enough time to decode it ? */
679 b_parsable = vpar_SynchroChoose( p_vpar,
680 p_vpar->picture.i_coding_type, i_structure );
686 /* Update the reference pointers. */
687 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
689 /* Update context. */
690 if( i_structure != FRAME_STRUCTURE )
692 if( (p_vpar->picture.i_current_structure | i_structure)
695 p_vpar->picture.i_current_structure = 0;
699 /* The frame is complete. */
700 p_vpar->picture.i_current_structure = i_structure;
702 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
707 /* Warn Synchro we have trashed a picture. */
708 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
710 p_vpar->picture.p_picture = NULL;
715 /* OK, now we are sure we will decode the picture. */
717 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
720 #define P_picture p_vpar->picture.p_picture
721 p_vpar->picture.b_error = 0;
722 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
724 if( !p_vpar->picture.i_current_structure )
726 /* This is a new frame. Get a structure from the video_output. */
727 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
728 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
729 p_vpar->sequence.i_width,
730 p_vpar->sequence.i_height ) )
733 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
734 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
738 msleep( VPAR_OUTMEM_SLEEP );
741 /* Initialize values. */
742 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
743 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
744 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
745 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
746 << ( 1 - p_vpar->picture.b_frame_structure ) );
747 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
748 << ( 1 - p_vpar->picture.b_frame_structure ));
750 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
752 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
754 /* FIXME ! remove asap ?? */
755 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
757 /* Update the reference pointers. */
758 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
761 /* Link referenced pictures for the decoder
762 * They are unlinked in vpar_ReleaseMacroblock() &
763 * vpar_DestroyMacroblock() */
764 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
765 p_vpar->picture.i_coding_type == B_CODING_TYPE )
767 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
769 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
771 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
775 p_vpar->picture.i_current_structure |= i_structure;
776 p_vpar->picture.i_structure = i_structure;
778 /* Initialize picture data for decoding. */
779 if( i_structure == BOTTOM_FIELD )
781 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
782 p_vpar->mb.i_l_y = 1;
783 p_vpar->mb.i_c_y = 1;
788 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
790 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
792 /* Extension and User data. */
793 ExtensionAndUserData( p_vpar );
795 /* This is an MP@ML decoder, please note that neither of the following
796 * assertions can be true :
797 * p_vpar->sequence.i_chroma_format != CHROMA_420
798 * p_vpar->sequence.i_height > 2800
799 * p_vpar->sequence.i_scalable_mode == SC_DP
800 * Be cautious if you try to use the decoder for other profiles and
803 if( p_vpar->sequence.b_mpeg2 )
805 static f_picture_data_t ppf_picture_data[4][4] =
808 NULL, NULL, NULL, NULL
812 #if (VPAR_OPTIM_LEVEL > 1)
813 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
816 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
817 vpar_PictureDataGENERIC
822 #if (VPAR_OPTIM_LEVEL > 1)
823 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
826 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
827 vpar_PictureDataGENERIC
832 #if (VPAR_OPTIM_LEVEL > 0)
833 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
836 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
837 vpar_PictureDataGENERIC
842 ppf_picture_data[p_vpar->picture.i_structure]
843 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
847 #if (VPAR_OPTIM_LEVEL > 0)
848 static f_picture_data_t pf_picture_data[5] =
849 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
850 vpar_PictureData1D };
852 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
854 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
858 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
863 if( p_vpar->picture.b_error )
867 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
869 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
874 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
877 if( P_picture->i_deccount != 1 )
879 vpar_SynchroEnd( p_vpar, 1 );
880 vout_DestroyPicture( p_vpar->p_vout, P_picture );
883 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
885 /* Prepare context for the next picture. */
887 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
888 p_vpar->picture.i_current_structure = 0;
890 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
892 /* Frame completely parsed. */
894 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
896 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
899 /* Send signal to the video_decoder. */
900 vlc_mutex_lock( &p_vpar->vfifo.lock );
901 vlc_cond_signal( &p_vpar->vfifo.wait );
902 vlc_mutex_unlock( &p_vpar->vfifo.lock );
905 /* Prepare context for the next picture. */
907 p_vpar->picture.i_current_structure = 0;
912 /*****************************************************************************
913 * ExtensionAndUserData : Parse the extension_and_user_data structure
914 *****************************************************************************/
915 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
917 while( !p_vpar->p_fifo->b_die )
919 NextStartCode( &p_vpar->bit_stream );
920 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
922 case EXTENSION_START_CODE:
923 RemoveBits32( &p_vpar->bit_stream );
924 switch( GetBits( &p_vpar->bit_stream, 4 ) )
926 case SEQUENCE_DISPLAY_EXTENSION_ID:
927 SequenceDisplayExtension( p_vpar );
929 case QUANT_MATRIX_EXTENSION_ID:
930 QuantMatrixExtension( p_vpar );
932 case SEQUENCE_SCALABLE_EXTENSION_ID:
933 SequenceScalableExtension( p_vpar );
935 case PICTURE_DISPLAY_EXTENSION_ID:
936 PictureDisplayExtension( p_vpar );
938 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
939 PictureSpatialScalableExtension( p_vpar );
941 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
942 PictureTemporalScalableExtension( p_vpar );
944 case COPYRIGHT_EXTENSION_ID:
945 CopyrightExtension( p_vpar );
952 case USER_DATA_START_CODE:
953 RemoveBits32( &p_vpar->bit_stream );
954 /* Wait for the next start code */
964 /*****************************************************************************
965 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
966 *****************************************************************************/
968 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
970 /* We don't care sequence_display_extension. */
972 RemoveBits( &p_vpar->bit_stream, 3 );
973 if( GetBits( &p_vpar->bit_stream, 1 ) )
975 /* Two bytes for color_desciption */
976 RemoveBits( &p_vpar->bit_stream, 16 );
977 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
979 /* display_horizontal and vertical_size and a marker_bit */
980 RemoveBits( &p_vpar->bit_stream, 29 );
984 /*****************************************************************************
985 * QuantMatrixExtension : Load quantization matrices for luminance *
987 *****************************************************************************/
989 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
991 if( GetBits( &p_vpar->bit_stream, 1 ) )
993 /* Load intra_quantiser_matrix for luminance. */
994 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
998 /* Use the default matrix. */
999 LinkMatrix( &p_vpar->sequence.intra_quant,
1000 p_vpar->pi_default_intra_quant );
1002 if( GetBits( &p_vpar->bit_stream, 1 ) )
1004 /* Load non_intra_quantiser_matrix for luminance. */
1005 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1009 /* Use the default matrix. */
1010 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1011 p_vpar->pi_default_nonintra_quant );
1013 if( GetBits( &p_vpar->bit_stream, 1 ) )
1015 /* Load intra_quantiser_matrix for chrominance. */
1016 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1020 /* Link the chrominance intra matrix to the luminance one. */
1021 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1022 p_vpar->sequence.intra_quant.pi_matrix );
1024 if( GetBits( &p_vpar->bit_stream, 1 ) )
1026 /* Load non_intra_quantiser_matrix for chrominance. */
1027 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1031 /* Link the chrominance intra matrix to the luminance one. */
1032 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1033 p_vpar->sequence.intra_quant.pi_matrix );
1035 if( GetBits( &p_vpar->bit_stream, 1 ) )
1037 /* Load non_intra_quantiser_matrix for chrominance. */
1038 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1042 /* Link the chrominance nonintra matrix to the luminance one. */
1043 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1044 p_vpar->sequence.nonintra_quant.pi_matrix );
1049 /*****************************************************************************
1050 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1051 * structure to handle scalable coding *
1052 *****************************************************************************/
1054 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1056 /* We don't care about anything scalable except the scalable mode. */
1057 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1058 /* The length of the structure depends on the value of the scalable_mode */
1061 RemoveBits32( &p_vpar->bit_stream );
1062 RemoveBits( &p_vpar->bit_stream, 21 );
1065 RemoveBits( &p_vpar->bit_stream, 12 );
1068 RemoveBits( &p_vpar->bit_stream, 4 );
1072 /*****************************************************************************
1073 * PictureDisplayExtension : Parse the picture_display_extension structure *
1074 *****************************************************************************/
1076 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1078 /* Number of frame center offset */
1080 /* I am not sure it works but it should
1081 (fewer tests than shown in §6.3.12) */
1082 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1083 p_vpar->picture.b_repeat_first_field +
1084 p_vpar->picture.b_top_field_first
1085 : ( p_vpar->picture.b_frame_structure + 1 ) +
1086 p_vpar->picture.b_repeat_first_field;
1087 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1089 RemoveBits( &p_vpar->bit_stream, 17 );
1090 RemoveBits( &p_vpar->bit_stream, 17 );
1095 /*****************************************************************************
1096 * PictureSpatialScalableExtension *
1097 *****************************************************************************/
1099 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1101 /* That's scalable, so we trash it */
1102 RemoveBits32( &p_vpar->bit_stream );
1103 RemoveBits( &p_vpar->bit_stream, 16 );
1107 /*****************************************************************************
1108 * PictureTemporalScalableExtension *
1109 *****************************************************************************/
1111 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1113 /* Scalable again, trashed again */
1114 RemoveBits( &p_vpar->bit_stream, 23 );
1118 /*****************************************************************************
1119 * CopyrightExtension : Keeps some legal informations *
1120 *****************************************************************************/
1122 static void CopyrightExtension( vpar_thread_t * p_vpar )
1124 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1125 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1126 /* A flag that says whether the copyright information is significant */
1127 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1128 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1129 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1131 RemoveBits( &p_vpar->bit_stream, 8 );
1132 /* The copyright_number is split in three parts */
1134 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1135 RemoveBits( &p_vpar->bit_stream, 1 );
1137 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1138 RemoveBits( &p_vpar->bit_stream, 1 );
1139 /* third part and sum */
1140 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1141 ( (u64)i_copyright_nb_2 << 22 ) |
1142 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );