1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.87 2001/05/07 03:14:10 stef 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 "video_decoder.h"
47 #include "vdec_motion.h"
49 #include "vpar_blocks.h"
50 #include "vpar_headers.h"
51 #include "vpar_synchro.h"
52 #include "video_parser.h"
53 #include "video_fifo.h"
55 #include "main.h" /* XXX REMOVE THIS */
60 static __inline__ void NextStartCode( bit_stream_t * );
61 static void SequenceHeader( vpar_thread_t * p_vpar );
62 static void GroupHeader( vpar_thread_t * p_vpar );
63 static void PictureHeader( vpar_thread_t * p_vpar );
64 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
65 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
66 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
67 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
68 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
69 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
70 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
71 static void CopyrightExtension( vpar_thread_t * p_vpar );
77 /*****************************************************************************
78 * pi_default_intra_quant : default quantization matrix
79 *****************************************************************************/
80 u8 pi_default_intra_quant[] =
82 8, 16, 19, 22, 26, 27, 29, 34,
83 16, 16, 22, 24, 27, 29, 34, 37,
84 19, 22, 26, 27, 29, 34, 34, 38,
85 22, 22, 26, 27, 29, 34, 37, 40,
86 22, 26, 27, 29, 32, 35, 40, 48,
87 26, 27, 29, 32, 35, 40, 48, 58,
88 26, 27, 29, 34, 38, 46, 56, 69,
89 27, 29, 35, 38, 46, 56, 69, 83
92 /*****************************************************************************
93 * pi_default_nonintra_quant : default quantization matrix
94 *****************************************************************************/
95 u8 pi_default_nonintra_quant[] =
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,
103 16, 16, 16, 16, 16, 16, 16, 16,
104 16, 16, 16, 16, 16, 16, 16, 16
107 /*****************************************************************************
108 * pi_scan : zig-zag and alternate scan patterns
109 *****************************************************************************/
112 { /* Zig-Zag pattern */
113 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
114 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
115 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
116 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
118 { /* Alternate scan pattern */
119 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
120 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
121 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
122 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
127 * Local inline functions.
130 /*****************************************************************************
131 * ReferenceUpdate : Update the reference pointers when we have a new picture
132 *****************************************************************************/
133 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
135 picture_t * p_newref )
137 if( i_coding_type != B_CODING_TYPE )
139 if( p_vpar->sequence.p_forward != NULL )
141 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
143 if( p_vpar->sequence.p_backward != NULL )
145 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
146 vpar_SynchroDate( p_vpar ) );
148 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
149 p_vpar->sequence.p_backward = p_newref;
150 if( p_newref != NULL )
152 vout_LinkPicture( p_vpar->p_vout, p_newref );
155 else if( p_newref != NULL )
157 /* Put date immediately. */
158 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
162 /*****************************************************************************
163 * ReferenceReplace : Replace the last reference pointer when we destroy
165 *****************************************************************************/
166 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
168 picture_t * p_newref )
170 if( i_coding_type != B_CODING_TYPE )
172 if( p_vpar->sequence.p_backward != NULL )
174 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
176 p_vpar->sequence.p_backward = p_newref;
177 if( p_newref != NULL )
179 vout_LinkPicture( p_vpar->p_vout, p_newref );
184 /*****************************************************************************
185 * LoadMatrix : Load a quantization matrix
186 *****************************************************************************/
187 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
188 quant_matrix_t * p_matrix )
192 if( !p_matrix->b_allocated )
194 /* Allocate a piece of memory to load the matrix. */
195 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
197 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
198 p_vpar->p_fifo->b_error = 1;
201 p_matrix->b_allocated = 1;
204 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
206 p_matrix->pi_matrix[p_vpar->ppi_scan[SCAN_ZIGZAG][i_dummy]]
207 = GetBits( &p_vpar->bit_stream, 8 );
211 /* Discrete Fourier Transform requires the quantization matrices to
212 * be normalized before using them. */
213 vdec_NormQuantMatrix( p_matrix->pi_matrix );
217 /*****************************************************************************
218 * LinkMatrix : Link a quantization matrix to another
219 *****************************************************************************/
220 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
222 if( p_matrix->b_allocated )
224 /* Deallocate the piece of memory. */
225 free( p_matrix->pi_matrix );
226 p_matrix->b_allocated = 0;
229 p_matrix->pi_matrix = pi_array;
233 * Exported functions.
236 /*****************************************************************************
237 * vpar_NextSequenceHeader : Find the next sequence header
238 *****************************************************************************/
239 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
241 while( !p_vpar->p_fifo->b_die )
243 NextStartCode( &p_vpar->bit_stream );
244 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
248 RemoveBits( &p_vpar->bit_stream, 8 );
253 /*****************************************************************************
254 * vpar_ParseHeader : Parse the next header
255 *****************************************************************************/
256 int vpar_ParseHeader( vpar_thread_t * p_vpar )
258 while( !p_vpar->p_fifo->b_die )
260 NextStartCode( &p_vpar->bit_stream );
261 switch( GetBits32( &p_vpar->bit_stream ) )
263 case SEQUENCE_HEADER_CODE:
265 p_vpar->c_sequences++;
267 SequenceHeader( p_vpar );
271 case GROUP_START_CODE:
272 GroupHeader( p_vpar );
276 case PICTURE_START_CODE:
277 PictureHeader( p_vpar );
281 case SEQUENCE_END_CODE:
282 intf_DbgMsg("vpar debug: sequence end code received");
295 * Following functions are local
298 /*****************************************************************************
299 * SequenceHeader : Parse the next sequence header
300 *****************************************************************************/
301 static void SequenceHeader( vpar_thread_t * p_vpar )
304 static int i_frame_rate_table[16] =
315 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
319 int i_height_save, i_width_save;
321 i_height_save = p_vpar->sequence.i_height;
322 i_width_save = p_vpar->sequence.i_width;
324 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
325 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
326 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
327 p_vpar->sequence.i_frame_rate =
328 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
330 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
331 * constrained_parameters_flag */
332 RemoveBits( &p_vpar->bit_stream, 30 );
335 * Quantization matrices
337 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
339 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
343 /* Use default matrix. */
344 LinkMatrix( &p_vpar->sequence.intra_quant,
345 p_vpar->pi_default_intra_quant );
348 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
350 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
354 /* Use default matrix. */
355 LinkMatrix( &p_vpar->sequence.nonintra_quant,
356 p_vpar->pi_default_nonintra_quant );
359 /* Unless later overwritten by a matrix extension, we have the same
360 * matrices for luminance and chrominance. */
361 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
362 p_vpar->sequence.intra_quant.pi_matrix );
363 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
364 p_vpar->sequence.nonintra_quant.pi_matrix );
369 NextStartCode( &p_vpar->bit_stream );
370 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
374 /* Turn the MPEG2 flag on */
375 p_vpar->sequence.b_mpeg2 = 1;
377 /* Parse sequence_extension */
378 RemoveBits32( &p_vpar->bit_stream );
379 /* extension_start_code_identifier, profile_and_level_indication */
380 RemoveBits( &p_vpar->bit_stream, 12 );
381 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
382 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
383 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
384 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
385 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
387 RemoveBits( &p_vpar->bit_stream, 22 );
388 /* frame_rate_extension_n */
389 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
390 /* frame_rate_extension_d */
391 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
392 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
396 /* It's an MPEG-1 stream. Put adequate parameters. */
398 p_vpar->sequence.b_mpeg2 = 0;
399 p_vpar->sequence.b_progressive = 1;
400 p_vpar->sequence.i_chroma_format = CHROMA_420;
404 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
405 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
406 (p_vpar->sequence.i_height + 15) / 16 :
407 2 * ((p_vpar->sequence.i_height + 31) / 32);
408 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
409 * p_vpar->sequence.i_mb_height;
410 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
411 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
412 p_vpar->sequence.i_size = p_vpar->sequence.i_width
413 * p_vpar->sequence.i_height;
415 /* Update chromatic information. */
416 switch( p_vpar->sequence.i_chroma_format )
419 p_vpar->sequence.i_chroma_nb_blocks = 2;
420 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
421 p_vpar->sequence.i_chroma_mb_width = 8;
422 p_vpar->sequence.i_chroma_mb_height = 8;
426 p_vpar->sequence.i_chroma_nb_blocks = 4;
427 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
428 p_vpar->sequence.i_chroma_mb_width = 8;
429 p_vpar->sequence.i_chroma_mb_height = 16;
433 p_vpar->sequence.i_chroma_nb_blocks = 8;
434 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
435 p_vpar->sequence.i_chroma_mb_width = 16;
436 p_vpar->sequence.i_chroma_mb_height = 16;
439 /* Reset scalable_mode. */
440 p_vpar->sequence.i_scalable_mode = SC_NONE;
443 if( p_vpar->sequence.i_width != i_width_save
444 || p_vpar->sequence.i_height != i_height_save )
446 /* FIXME: What do we do in case of a size change ?? */
450 /* Extension and User data */
451 ExtensionAndUserData( p_vpar );
453 /* XXX: The vout request and fifo opening will eventually be here */
455 /* Spawn an audio output if there is none */
456 vlc_mutex_lock( &p_vout_bank->lock );
458 if( p_vout_bank->i_count == 0 )
460 intf_WarnMsg( 1, "vpar: no vout present, spawning one" );
462 p_vpar->p_vout = vout_CreateThread( NULL );
464 /* Everything failed */
465 if( p_vpar->p_vout == NULL )
467 intf_ErrMsg( "vpar error: can't open vout, aborting" );
468 vlc_mutex_unlock( &p_vout_bank->lock );
470 /* XXX ! XXX ! XXX ! what to do here ? */
474 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
475 p_vout_bank->i_count++;
479 /* Take the first video output FIXME: take the best one */
480 p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
483 vlc_mutex_unlock( &p_vout_bank->lock );
486 /*****************************************************************************
487 * GroupHeader : Parse the next group of pictures header
488 *****************************************************************************/
489 static void GroupHeader( vpar_thread_t * p_vpar )
491 /* Nothing to do, we don't care. */
492 RemoveBits( &p_vpar->bit_stream, 27 );
493 ExtensionAndUserData( p_vpar );
496 /*****************************************************************************
497 * PictureHeader : Parse the next picture header
498 *****************************************************************************/
499 static void PictureHeader( vpar_thread_t * p_vpar )
503 boolean_t b_parsable;
508 /* Recover in case of stream discontinuity. */
509 if( p_vpar->sequence.b_expect_discontinuity )
511 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
512 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
513 if( p_vpar->picture.p_picture != NULL )
518 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
520 vpar_DestroyMacroblock( &p_vpar->vfifo,
521 p_vpar->picture.pp_mb[i_mb] );
524 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
526 p_vpar->sequence.b_expect_discontinuity = 0;
529 /* Parse the picture header. */
530 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
531 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
532 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
534 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
535 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
537 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
538 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
540 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
542 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
543 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
546 /* extra_information_picture */
547 while( GetBits( &p_vpar->bit_stream, 1 ) )
549 RemoveBits( &p_vpar->bit_stream, 8 );
553 * Picture Coding Extension
555 NextStartCode( &p_vpar->bit_stream );
556 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
558 /* Parse picture_coding_extension */
559 RemoveBits32( &p_vpar->bit_stream );
560 /* extension_start_code_identifier */
561 RemoveBits( &p_vpar->bit_stream, 4 );
563 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
564 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
565 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
566 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
567 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
568 i_structure = GetBits( &p_vpar->bit_stream, 2 );
569 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
570 p_vpar->picture.b_frame_pred_frame_dct
571 = GetBits( &p_vpar->bit_stream, 1 );
572 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
573 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
574 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
575 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
576 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
577 /* chroma_420_type (obsolete) */
578 RemoveBits( &p_vpar->bit_stream, 1 );
579 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
581 /* composite_display_flag */
582 if( GetBits( &p_vpar->bit_stream, 1 ) )
584 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
585 * sub_carrier_phase */
586 RemoveBits( &p_vpar->bit_stream, 20 );
591 /* MPEG-1 compatibility flags */
592 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
593 i_structure = FRAME_STRUCTURE;
594 p_vpar->picture.b_top_field_first = 0;
595 p_vpar->picture.b_frame_pred_frame_dct = 1;
596 p_vpar->picture.b_concealment_mv = 0;
597 p_vpar->picture.b_q_scale_type = 0;
598 p_vpar->picture.b_intra_vlc_format = 0;
599 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
600 p_vpar->picture.b_repeat_first_field = 0;
601 p_vpar->picture.b_progressive = 1;
605 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
608 if( p_vpar->picture.i_current_structure &&
609 (i_structure == FRAME_STRUCTURE ||
610 i_structure == p_vpar->picture.i_current_structure) )
612 /* We don't have the second field of the buffered frame. */
613 if( p_vpar->picture.p_picture != NULL )
615 ReferenceReplace( p_vpar,
616 p_vpar->picture.i_coding_type,
620 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
622 vpar_DestroyMacroblock( &p_vpar->vfifo,
623 p_vpar->picture.pp_mb[i_mb] );
626 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
629 p_vpar->picture.i_current_structure = 0;
631 intf_WarnMsg( 2, "Odd number of field pictures." );
634 /* Do we have the reference pictures ? */
635 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
636 (p_vpar->sequence.p_backward == NULL)) ||
637 /* p_backward will become p_forward later */
638 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
639 (p_vpar->sequence.p_forward == NULL ||
640 p_vpar->sequence.p_backward == NULL)));
642 if( p_vpar->picture.i_current_structure )
644 /* Second field of a frame. We will decode it if, and only if we
645 * have decoded the first field. */
648 b_parsable = (p_vpar->picture.p_picture != NULL);
655 /* Compute the number of times the frame will be emitted by the
656 * decoder (number of half-periods). */
657 if( p_vpar->sequence.b_progressive )
659 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
660 + p_vpar->picture.b_top_field_first) * 2;
664 if( p_vpar->picture.b_progressive )
666 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
674 /* Warn synchro we have a new picture (updates pictures index). */
675 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
680 /* Does synchro say we have enough time to decode it ? */
681 b_parsable = vpar_SynchroChoose( p_vpar,
682 p_vpar->picture.i_coding_type, i_structure );
688 /* Update the reference pointers. */
689 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
691 /* Update context. */
692 if( i_structure != FRAME_STRUCTURE )
694 if( (p_vpar->picture.i_current_structure | i_structure)
697 p_vpar->picture.i_current_structure = 0;
701 /* The frame is complete. */
702 p_vpar->picture.i_current_structure = i_structure;
704 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
709 /* Warn Synchro we have trashed a picture. */
710 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
712 p_vpar->picture.p_picture = NULL;
717 /* OK, now we are sure we will decode the picture. */
719 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
722 #define P_picture p_vpar->picture.p_picture
723 p_vpar->picture.b_error = 0;
724 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
726 if( !p_vpar->picture.i_current_structure )
728 /* This is a new frame. Get a structure from the video_output. */
729 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
730 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
731 p_vpar->sequence.i_width,
732 p_vpar->sequence.i_height ) )
735 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
736 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
740 msleep( VPAR_OUTMEM_SLEEP );
743 /* Initialize values. */
744 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
745 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
746 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
747 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
748 << ( 1 - p_vpar->picture.b_frame_structure ) );
749 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
750 << ( 1 - p_vpar->picture.b_frame_structure ));
752 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
754 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
756 /* FIXME ! remove asap ?? */
757 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
759 /* Update the reference pointers. */
760 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
763 /* Link referenced pictures for the decoder
764 * They are unlinked in vpar_ReleaseMacroblock() &
765 * vpar_DestroyMacroblock() */
766 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
767 p_vpar->picture.i_coding_type == B_CODING_TYPE )
769 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
771 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
773 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
777 p_vpar->picture.i_current_structure |= i_structure;
778 p_vpar->picture.i_structure = i_structure;
780 /* Initialize picture data for decoding. */
781 if( i_structure == BOTTOM_FIELD )
783 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
784 p_vpar->mb.i_l_y = 1;
785 p_vpar->mb.i_c_y = 1;
790 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
792 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
794 /* Extension and User data. */
795 ExtensionAndUserData( p_vpar );
797 /* This is an MP@ML decoder, please note that neither of the following
798 * assertions can be true :
799 * p_vpar->sequence.i_chroma_format != CHROMA_420
800 * p_vpar->sequence.i_height > 2800
801 * p_vpar->sequence.i_scalable_mode == SC_DP
802 * Be cautious if you try to use the decoder for other profiles and
805 if( p_vpar->sequence.b_mpeg2 )
807 static f_picture_data_t ppf_picture_data[4][4] =
810 NULL, NULL, NULL, NULL
814 #if (VPAR_OPTIM_LEVEL > 1)
815 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
818 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
819 vpar_PictureDataGENERIC
824 #if (VPAR_OPTIM_LEVEL > 1)
825 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
828 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
829 vpar_PictureDataGENERIC
834 #if (VPAR_OPTIM_LEVEL > 0)
835 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
838 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
839 vpar_PictureDataGENERIC
844 ppf_picture_data[p_vpar->picture.i_structure]
845 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
849 #if (VPAR_OPTIM_LEVEL > 0)
850 static f_picture_data_t pf_picture_data[5] =
851 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
852 vpar_PictureData1D };
854 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
856 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
860 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
865 if( p_vpar->picture.b_error )
869 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
871 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
876 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
879 if( P_picture->i_deccount != 1 )
881 vpar_SynchroEnd( p_vpar, 1 );
882 vout_DestroyPicture( p_vpar->p_vout, P_picture );
885 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
887 /* Prepare context for the next picture. */
889 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
890 p_vpar->picture.i_current_structure = 0;
892 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
894 /* Frame completely parsed. */
896 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
898 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
901 /* Send signal to the video_decoder. */
902 vlc_mutex_lock( &p_vpar->vfifo.lock );
903 vlc_cond_signal( &p_vpar->vfifo.wait );
904 vlc_mutex_unlock( &p_vpar->vfifo.lock );
907 /* Prepare context for the next picture. */
909 p_vpar->picture.i_current_structure = 0;
914 /*****************************************************************************
915 * ExtensionAndUserData : Parse the extension_and_user_data structure
916 *****************************************************************************/
917 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
919 while( !p_vpar->p_fifo->b_die )
921 NextStartCode( &p_vpar->bit_stream );
922 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
924 case EXTENSION_START_CODE:
925 RemoveBits32( &p_vpar->bit_stream );
926 switch( GetBits( &p_vpar->bit_stream, 4 ) )
928 case SEQUENCE_DISPLAY_EXTENSION_ID:
929 SequenceDisplayExtension( p_vpar );
931 case QUANT_MATRIX_EXTENSION_ID:
932 QuantMatrixExtension( p_vpar );
934 case SEQUENCE_SCALABLE_EXTENSION_ID:
935 SequenceScalableExtension( p_vpar );
937 case PICTURE_DISPLAY_EXTENSION_ID:
938 PictureDisplayExtension( p_vpar );
940 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
941 PictureSpatialScalableExtension( p_vpar );
943 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
944 PictureTemporalScalableExtension( p_vpar );
946 case COPYRIGHT_EXTENSION_ID:
947 CopyrightExtension( p_vpar );
954 case USER_DATA_START_CODE:
955 RemoveBits32( &p_vpar->bit_stream );
956 /* Wait for the next start code */
966 /*****************************************************************************
967 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
968 *****************************************************************************/
970 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
972 /* We don't care sequence_display_extension. */
974 RemoveBits( &p_vpar->bit_stream, 3 );
975 if( GetBits( &p_vpar->bit_stream, 1 ) )
977 /* Two bytes for color_desciption */
978 RemoveBits( &p_vpar->bit_stream, 16 );
979 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
981 /* display_horizontal and vertical_size and a marker_bit */
982 RemoveBits( &p_vpar->bit_stream, 29 );
986 /*****************************************************************************
987 * QuantMatrixExtension : Load quantization matrices for luminance *
989 *****************************************************************************/
991 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
993 if( GetBits( &p_vpar->bit_stream, 1 ) )
995 /* Load intra_quantiser_matrix for luminance. */
996 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1000 /* Use the default matrix. */
1001 LinkMatrix( &p_vpar->sequence.intra_quant,
1002 p_vpar->pi_default_intra_quant );
1004 if( GetBits( &p_vpar->bit_stream, 1 ) )
1006 /* Load non_intra_quantiser_matrix for luminance. */
1007 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1011 /* Use the default matrix. */
1012 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1013 p_vpar->pi_default_nonintra_quant );
1015 if( GetBits( &p_vpar->bit_stream, 1 ) )
1017 /* Load intra_quantiser_matrix for chrominance. */
1018 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1022 /* Link the chrominance intra matrix to the luminance one. */
1023 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1024 p_vpar->sequence.intra_quant.pi_matrix );
1026 if( GetBits( &p_vpar->bit_stream, 1 ) )
1028 /* Load non_intra_quantiser_matrix for chrominance. */
1029 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1033 /* Link the chrominance intra matrix to the luminance one. */
1034 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1035 p_vpar->sequence.intra_quant.pi_matrix );
1037 if( GetBits( &p_vpar->bit_stream, 1 ) )
1039 /* Load non_intra_quantiser_matrix for chrominance. */
1040 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1044 /* Link the chrominance nonintra matrix to the luminance one. */
1045 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1046 p_vpar->sequence.nonintra_quant.pi_matrix );
1051 /*****************************************************************************
1052 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1053 * structure to handle scalable coding *
1054 *****************************************************************************/
1056 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1058 /* We don't care about anything scalable except the scalable mode. */
1059 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1060 /* The length of the structure depends on the value of the scalable_mode */
1063 RemoveBits32( &p_vpar->bit_stream );
1064 RemoveBits( &p_vpar->bit_stream, 21 );
1067 RemoveBits( &p_vpar->bit_stream, 12 );
1070 RemoveBits( &p_vpar->bit_stream, 4 );
1074 /*****************************************************************************
1075 * PictureDisplayExtension : Parse the picture_display_extension structure *
1076 *****************************************************************************/
1078 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1080 /* Number of frame center offset */
1082 /* I am not sure it works but it should
1083 (fewer tests than shown in §6.3.12) */
1084 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1085 p_vpar->picture.b_repeat_first_field +
1086 p_vpar->picture.b_top_field_first
1087 : ( p_vpar->picture.b_frame_structure + 1 ) +
1088 p_vpar->picture.b_repeat_first_field;
1089 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1091 RemoveBits( &p_vpar->bit_stream, 17 );
1092 RemoveBits( &p_vpar->bit_stream, 17 );
1097 /*****************************************************************************
1098 * PictureSpatialScalableExtension *
1099 *****************************************************************************/
1101 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1103 /* That's scalable, so we trash it */
1104 RemoveBits32( &p_vpar->bit_stream );
1105 RemoveBits( &p_vpar->bit_stream, 16 );
1109 /*****************************************************************************
1110 * PictureTemporalScalableExtension *
1111 *****************************************************************************/
1113 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1115 /* Scalable again, trashed again */
1116 RemoveBits( &p_vpar->bit_stream, 23 );
1120 /*****************************************************************************
1121 * CopyrightExtension : Keeps some legal informations *
1122 *****************************************************************************/
1124 static void CopyrightExtension( vpar_thread_t * p_vpar )
1126 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1127 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1128 /* A flag that says whether the copyright information is significant */
1129 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1130 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1131 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1133 RemoveBits( &p_vpar->bit_stream, 8 );
1134 /* The copyright_number is split in three parts */
1136 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1137 RemoveBits( &p_vpar->bit_stream, 1 );
1139 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1140 RemoveBits( &p_vpar->bit_stream, 1 );
1141 /* third part and sum */
1142 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1143 ( (u64)i_copyright_nb_2 << 22 ) |
1144 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );