1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.6 2001/07/26 11:36:52 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 "vpar_pool.h"
48 #include "video_parser.h"
53 static __inline__ void NextStartCode( bit_stream_t * );
54 static void SequenceHeader( vpar_thread_t * p_vpar );
55 static void GroupHeader( vpar_thread_t * p_vpar );
56 static void PictureHeader( vpar_thread_t * p_vpar );
57 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
58 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
59 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
60 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
61 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
62 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
63 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
64 static void CopyrightExtension( vpar_thread_t * p_vpar );
70 /*****************************************************************************
71 * pi_default_intra_quant : default quantization matrix
72 *****************************************************************************/
73 u8 pi_default_intra_quant[] =
75 8, 16, 19, 22, 26, 27, 29, 34,
76 16, 16, 22, 24, 27, 29, 34, 37,
77 19, 22, 26, 27, 29, 34, 34, 38,
78 22, 22, 26, 27, 29, 34, 37, 40,
79 22, 26, 27, 29, 32, 35, 40, 48,
80 26, 27, 29, 32, 35, 40, 48, 58,
81 26, 27, 29, 34, 38, 46, 56, 69,
82 27, 29, 35, 38, 46, 56, 69, 83
85 /*****************************************************************************
86 * pi_default_nonintra_quant : default quantization matrix
87 *****************************************************************************/
88 u8 pi_default_nonintra_quant[] =
90 16, 16, 16, 16, 16, 16, 16, 16,
91 16, 16, 16, 16, 16, 16, 16, 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
100 /*****************************************************************************
101 * pi_scan : zig-zag and alternate scan patterns
102 *****************************************************************************/
105 { /* Zig-Zag pattern */
106 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
107 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
108 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
109 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
111 { /* Alternate scan pattern */
112 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
113 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
114 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
115 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
120 * Local inline functions.
123 /*****************************************************************************
124 * ReferenceUpdate : Update the reference pointers when we have a new picture
125 *****************************************************************************/
126 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
128 picture_t * p_newref )
130 if( i_coding_type != B_CODING_TYPE )
132 if( p_vpar->sequence.p_forward != NULL )
134 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
136 if( p_vpar->sequence.p_backward != NULL )
138 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
139 vpar_SynchroDate( p_vpar ) );
141 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
142 p_vpar->sequence.p_backward = p_newref;
143 if( p_newref != NULL )
145 vout_LinkPicture( p_vpar->p_vout, p_newref );
148 else if( p_newref != NULL )
150 /* Put date immediately. */
151 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
155 /*****************************************************************************
156 * ReferenceReplace : Replace the last reference pointer when we destroy
158 *****************************************************************************/
159 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
161 picture_t * p_newref )
163 if( i_coding_type != B_CODING_TYPE )
165 if( p_vpar->sequence.p_backward != NULL )
167 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
169 p_vpar->sequence.p_backward = p_newref;
170 if( p_newref != NULL )
172 vout_LinkPicture( p_vpar->p_vout, p_newref );
177 /*****************************************************************************
178 * LoadMatrix : Load a quantization matrix
179 *****************************************************************************/
180 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
181 quant_matrix_t * p_matrix )
185 if( !p_matrix->b_allocated )
187 /* Allocate a piece of memory to load the matrix. */
188 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
190 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
191 p_vpar->p_fifo->b_error = 1;
194 p_matrix->b_allocated = 1;
197 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
199 p_matrix->pi_matrix[p_vpar->ppi_scan[SCAN_ZIGZAG][i_dummy]]
200 = GetBits( &p_vpar->bit_stream, 8 );
204 /* Discrete Fourier Transform requires the quantization matrices to
205 * be normalized before using them. */
206 vdec_NormQuantMatrix( p_matrix->pi_matrix );
210 /*****************************************************************************
211 * LinkMatrix : Link a quantization matrix to another
212 *****************************************************************************/
213 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
215 if( p_matrix->b_allocated )
217 /* Deallocate the piece of memory. */
218 free( p_matrix->pi_matrix );
219 p_matrix->b_allocated = 0;
222 p_matrix->pi_matrix = pi_array;
226 * Exported functions.
229 /*****************************************************************************
230 * vpar_NextSequenceHeader : Find the next sequence header
231 *****************************************************************************/
232 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
234 while( !p_vpar->p_fifo->b_die )
236 NextStartCode( &p_vpar->bit_stream );
237 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
241 RemoveBits( &p_vpar->bit_stream, 8 );
246 /*****************************************************************************
247 * vpar_ParseHeader : Parse the next header
248 *****************************************************************************/
249 int vpar_ParseHeader( vpar_thread_t * p_vpar )
251 while( !p_vpar->p_fifo->b_die )
253 NextStartCode( &p_vpar->bit_stream );
254 switch( GetBits32( &p_vpar->bit_stream ) )
256 case SEQUENCE_HEADER_CODE:
258 p_vpar->c_sequences++;
260 SequenceHeader( p_vpar );
264 case GROUP_START_CODE:
265 GroupHeader( p_vpar );
269 case PICTURE_START_CODE:
270 PictureHeader( p_vpar );
274 case SEQUENCE_END_CODE:
275 intf_DbgMsg("vpar debug: sequence end code received");
288 * Following functions are local
291 /*****************************************************************************
292 * SequenceHeader : Parse the next sequence header
293 *****************************************************************************/
294 static void SequenceHeader( vpar_thread_t * p_vpar )
297 static int i_frame_rate_table[16] =
308 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
312 int i_height_save, i_width_save;
314 i_height_save = p_vpar->sequence.i_height;
315 i_width_save = p_vpar->sequence.i_width;
317 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
318 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
319 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
320 p_vpar->sequence.i_frame_rate =
321 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
323 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
324 * constrained_parameters_flag */
325 RemoveBits( &p_vpar->bit_stream, 30 );
328 * Quantization matrices
330 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
332 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
336 /* Use default matrix. */
337 LinkMatrix( &p_vpar->sequence.intra_quant,
338 p_vpar->pi_default_intra_quant );
341 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
343 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
347 /* Use default matrix. */
348 LinkMatrix( &p_vpar->sequence.nonintra_quant,
349 p_vpar->pi_default_nonintra_quant );
352 /* Unless later overwritten by a matrix extension, we have the same
353 * matrices for luminance and chrominance. */
354 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
355 p_vpar->sequence.intra_quant.pi_matrix );
356 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
357 p_vpar->sequence.nonintra_quant.pi_matrix );
362 NextStartCode( &p_vpar->bit_stream );
363 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
367 /* Turn the MPEG2 flag on */
368 p_vpar->sequence.b_mpeg2 = 1;
370 /* Parse sequence_extension */
371 RemoveBits32( &p_vpar->bit_stream );
372 /* extension_start_code_identifier, profile_and_level_indication */
373 RemoveBits( &p_vpar->bit_stream, 12 );
374 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
375 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
376 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
377 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
378 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
380 RemoveBits( &p_vpar->bit_stream, 22 );
381 /* frame_rate_extension_n */
382 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
383 /* frame_rate_extension_d */
384 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
385 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
389 /* It's an MPEG-1 stream. Put adequate parameters. */
391 p_vpar->sequence.b_mpeg2 = 0;
392 p_vpar->sequence.b_progressive = 1;
393 p_vpar->sequence.i_chroma_format = CHROMA_420;
397 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
398 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
399 (p_vpar->sequence.i_height + 15) / 16 :
400 2 * ((p_vpar->sequence.i_height + 31) / 32);
401 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
402 * p_vpar->sequence.i_mb_height;
403 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
404 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
405 p_vpar->sequence.i_size = p_vpar->sequence.i_width
406 * p_vpar->sequence.i_height;
408 /* Update chromatic information. */
409 switch( p_vpar->sequence.i_chroma_format )
412 p_vpar->sequence.i_chroma_nb_blocks = 2;
413 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
414 p_vpar->sequence.i_chroma_mb_width = 8;
415 p_vpar->sequence.i_chroma_mb_height = 8;
419 p_vpar->sequence.i_chroma_nb_blocks = 4;
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 = 16;
426 p_vpar->sequence.i_chroma_nb_blocks = 8;
427 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
428 p_vpar->sequence.i_chroma_mb_width = 16;
429 p_vpar->sequence.i_chroma_mb_height = 16;
432 /* Reset scalable_mode. */
433 p_vpar->sequence.i_scalable_mode = SC_NONE;
436 if( p_vpar->sequence.i_width != i_width_save
437 || p_vpar->sequence.i_height != i_height_save )
439 /* FIXME: What do we do in case of a size change ?? */
443 /* Extension and User data */
444 ExtensionAndUserData( p_vpar );
446 /* XXX: The vout request and fifo opening will eventually be here */
448 /* Spawn a video output if there is none */
449 vlc_mutex_lock( &p_vout_bank->lock );
451 if( p_vout_bank->i_count == 0 )
453 intf_WarnMsg( 1, "vpar: no vout present, spawning one" );
455 p_vpar->p_vout = vout_CreateThread( NULL );
457 /* Everything failed */
458 if( p_vpar->p_vout == NULL )
460 intf_ErrMsg( "vpar error: can't open vout, aborting" );
461 vlc_mutex_unlock( &p_vout_bank->lock );
463 /* XXX ! XXX ! XXX ! what to do here ? */
467 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
468 p_vout_bank->i_count++;
472 /* Take the first video output FIXME: take the best one */
473 p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
476 vlc_mutex_unlock( &p_vout_bank->lock );
479 /*****************************************************************************
480 * GroupHeader : Parse the next group of pictures header
481 *****************************************************************************/
482 static void GroupHeader( vpar_thread_t * p_vpar )
484 /* Nothing to do, we don't care. */
485 RemoveBits( &p_vpar->bit_stream, 27 );
486 ExtensionAndUserData( p_vpar );
489 /*****************************************************************************
490 * PictureHeader : Parse the next picture header
491 *****************************************************************************/
492 static void PictureHeader( vpar_thread_t * p_vpar )
496 boolean_t b_parsable;
501 /* Recover in case of stream discontinuity. */
502 if( p_vpar->sequence.b_expect_discontinuity )
504 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
505 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
506 if( p_vpar->picture.p_picture != NULL )
508 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
509 p_vpar->picture.p_picture = NULL;
511 p_vpar->picture.i_current_structure = 0;
512 p_vpar->sequence.b_expect_discontinuity = 0;
515 /* Parse the picture header. */
516 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
517 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
518 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
520 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
521 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
523 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
524 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
526 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
528 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
529 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
532 /* extra_information_picture */
533 while( GetBits( &p_vpar->bit_stream, 1 ) )
535 RemoveBits( &p_vpar->bit_stream, 8 );
539 * Picture Coding Extension
541 NextStartCode( &p_vpar->bit_stream );
542 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
544 /* Parse picture_coding_extension */
545 RemoveBits32( &p_vpar->bit_stream );
546 /* extension_start_code_identifier */
547 RemoveBits( &p_vpar->bit_stream, 4 );
549 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
550 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
551 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
552 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
553 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
554 i_structure = GetBits( &p_vpar->bit_stream, 2 );
555 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
556 p_vpar->picture.b_frame_pred_frame_dct
557 = GetBits( &p_vpar->bit_stream, 1 );
558 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
559 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
560 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
561 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
562 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
563 /* chroma_420_type (obsolete) */
564 RemoveBits( &p_vpar->bit_stream, 1 );
565 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
567 /* composite_display_flag */
568 if( GetBits( &p_vpar->bit_stream, 1 ) )
570 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
571 * sub_carrier_phase */
572 RemoveBits( &p_vpar->bit_stream, 20 );
577 /* MPEG-1 compatibility flags */
578 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
579 i_structure = FRAME_STRUCTURE;
580 p_vpar->picture.b_top_field_first = 0;
581 p_vpar->picture.b_frame_pred_frame_dct = 1;
582 p_vpar->picture.b_concealment_mv = 0;
583 p_vpar->picture.b_q_scale_type = 0;
584 p_vpar->picture.b_intra_vlc_format = 0;
585 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
586 p_vpar->picture.b_repeat_first_field = 0;
587 p_vpar->picture.b_progressive = 1;
591 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
594 if( p_vpar->picture.i_current_structure &&
595 (i_structure == FRAME_STRUCTURE ||
596 i_structure == p_vpar->picture.i_current_structure) )
598 /* We don't have the second field of the buffered frame. */
599 if( p_vpar->picture.p_picture != NULL )
601 ReferenceReplace( p_vpar,
602 p_vpar->picture.i_coding_type,
604 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
605 p_vpar->picture.p_picture = NULL;
608 p_vpar->picture.i_current_structure = 0;
610 intf_WarnMsg( 2, "Odd number of field pictures." );
613 /* Do we have the reference pictures ? */
614 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
615 (p_vpar->sequence.p_backward == NULL)) ||
616 /* p_backward will become p_forward later */
617 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
618 (p_vpar->sequence.p_forward == NULL ||
619 p_vpar->sequence.p_backward == NULL)));
621 if( p_vpar->picture.i_current_structure )
623 /* Second field of a frame. We will decode it if, and only if we
624 * have decoded the first field. */
627 if( p_vpar->picture.p_picture == NULL )
629 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
630 && p_vpar->sequence.p_backward == NULL) )
632 /* Exceptionnally, parse the picture if it is I. We need
633 * this in case of an odd number of field pictures, if the
634 * previous picture is not intra, we desperately need a
635 * new reference picture. OK, this is kind of kludgy. */
636 p_vpar->picture.i_current_structure = 0;
649 /* Compute the number of times the frame will be emitted by the
650 * decoder (number of half-periods). */
651 if( p_vpar->sequence.b_progressive )
653 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
654 + p_vpar->picture.b_top_field_first) * 2;
658 if( p_vpar->picture.b_progressive )
660 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
668 /* Warn synchro we have a new picture (updates pictures index). */
669 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
674 /* Does synchro say we have enough time to decode it ? */
675 b_parsable = vpar_SynchroChoose( p_vpar,
676 p_vpar->picture.i_coding_type, i_structure );
682 /* Update the reference pointers. */
683 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
685 /* Update context. */
686 if( i_structure != FRAME_STRUCTURE )
688 if( (p_vpar->picture.i_current_structure | i_structure)
691 /* The frame is complete. */
692 p_vpar->picture.i_current_structure = 0;
694 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
698 p_vpar->picture.i_current_structure = i_structure;
703 /* Warn Synchro we have trashed a picture. */
704 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
706 p_vpar->picture.p_picture = NULL;
711 /* OK, now we are sure we will decode the picture. */
713 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
716 #define P_picture p_vpar->picture.p_picture
717 p_vpar->picture.b_error = 0;
718 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
720 if( !p_vpar->picture.i_current_structure )
722 /* This is a new frame. Get a structure from the video_output. */
723 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
724 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
725 p_vpar->sequence.i_width,
726 p_vpar->sequence.i_height ) )
729 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
730 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
734 msleep( VPAR_OUTMEM_SLEEP );
737 /* Initialize values. */
738 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
739 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
740 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
741 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
742 << ( 1 - p_vpar->picture.b_frame_structure ) );
743 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
744 << ( 1 - p_vpar->picture.b_frame_structure ));
746 /* FIXME ! remove asap ?? */
747 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
749 /* Update the reference pointers. */
750 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
752 p_vpar->picture.i_current_structure |= i_structure;
753 p_vpar->picture.i_structure = i_structure;
755 /* Initialize picture data for decoding. */
756 if( i_structure == BOTTOM_FIELD )
758 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
759 p_vpar->mb.i_l_y = 1;
760 p_vpar->mb.i_c_y = 1;
765 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
767 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
769 /* Extension and User data. */
770 ExtensionAndUserData( p_vpar );
772 /* This is an MP@ML decoder, please note that neither of the following
773 * assertions can be true :
774 * p_vpar->sequence.i_chroma_format != CHROMA_420
775 * p_vpar->sequence.i_height > 2800
776 * p_vpar->sequence.i_scalable_mode == SC_DP
777 * Be cautious if you try to use the decoder for other profiles and
780 if( p_vpar->sequence.b_mpeg2 )
782 static f_picture_data_t ppf_picture_data[4][4] =
785 NULL, NULL, NULL, NULL
789 #if (VPAR_OPTIM_LEVEL > 1)
790 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
793 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
794 vpar_PictureDataGENERIC
799 #if (VPAR_OPTIM_LEVEL > 1)
800 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
803 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
804 vpar_PictureDataGENERIC
809 #if (VPAR_OPTIM_LEVEL > 0)
810 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
813 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
814 vpar_PictureDataGENERIC
819 ppf_picture_data[p_vpar->picture.i_structure]
820 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
824 #if (VPAR_OPTIM_LEVEL > 0)
825 static f_picture_data_t pf_picture_data[5] =
826 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
827 vpar_PictureData1D };
829 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
831 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
835 /* Wait for all the macroblocks to be decoded. */
836 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
838 /* Re-spawn decoder threads if the user changed settings. */
839 vpar_SpawnPool( p_vpar );
841 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
846 if( p_vpar->picture.b_error )
850 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
853 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
854 p_vpar->picture.i_structure, 1 );
855 vout_DestroyPicture( p_vpar->p_vout, P_picture );
857 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
859 /* Prepare context for the next picture. */
861 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
862 p_vpar->picture.i_current_structure = 0;
864 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
866 /* Frame completely parsed. */
867 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
868 p_vpar->picture.i_structure, 0 );
869 vout_DisplayPicture( p_vpar->p_vout, P_picture );
871 /* Prepare context for the next picture. */
873 p_vpar->picture.i_current_structure = 0;
878 /*****************************************************************************
879 * ExtensionAndUserData : Parse the extension_and_user_data structure
880 *****************************************************************************/
881 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
883 while( !p_vpar->p_fifo->b_die )
885 NextStartCode( &p_vpar->bit_stream );
886 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
888 case EXTENSION_START_CODE:
889 RemoveBits32( &p_vpar->bit_stream );
890 switch( GetBits( &p_vpar->bit_stream, 4 ) )
892 case SEQUENCE_DISPLAY_EXTENSION_ID:
893 SequenceDisplayExtension( p_vpar );
895 case QUANT_MATRIX_EXTENSION_ID:
896 QuantMatrixExtension( p_vpar );
898 case SEQUENCE_SCALABLE_EXTENSION_ID:
899 SequenceScalableExtension( p_vpar );
901 case PICTURE_DISPLAY_EXTENSION_ID:
902 PictureDisplayExtension( p_vpar );
904 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
905 PictureSpatialScalableExtension( p_vpar );
907 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
908 PictureTemporalScalableExtension( p_vpar );
910 case COPYRIGHT_EXTENSION_ID:
911 CopyrightExtension( p_vpar );
918 case USER_DATA_START_CODE:
919 RemoveBits32( &p_vpar->bit_stream );
920 /* Wait for the next start code */
930 /*****************************************************************************
931 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
932 *****************************************************************************/
934 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
936 /* We don't care sequence_display_extension. */
938 RemoveBits( &p_vpar->bit_stream, 3 );
939 if( GetBits( &p_vpar->bit_stream, 1 ) )
941 /* Two bytes for color_desciption */
942 RemoveBits( &p_vpar->bit_stream, 16 );
943 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
945 /* display_horizontal and vertical_size and a marker_bit */
946 RemoveBits( &p_vpar->bit_stream, 29 );
950 /*****************************************************************************
951 * QuantMatrixExtension : Load quantization matrices for luminance *
953 *****************************************************************************/
955 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
957 if( GetBits( &p_vpar->bit_stream, 1 ) )
959 /* Load intra_quantiser_matrix for luminance. */
960 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
964 /* Use the default matrix. */
965 LinkMatrix( &p_vpar->sequence.intra_quant,
966 p_vpar->pi_default_intra_quant );
968 if( GetBits( &p_vpar->bit_stream, 1 ) )
970 /* Load non_intra_quantiser_matrix for luminance. */
971 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
975 /* Use the default matrix. */
976 LinkMatrix( &p_vpar->sequence.nonintra_quant,
977 p_vpar->pi_default_nonintra_quant );
979 if( GetBits( &p_vpar->bit_stream, 1 ) )
981 /* Load intra_quantiser_matrix for chrominance. */
982 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
986 /* Link the chrominance intra matrix to the luminance one. */
987 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
988 p_vpar->sequence.intra_quant.pi_matrix );
990 if( GetBits( &p_vpar->bit_stream, 1 ) )
992 /* Load non_intra_quantiser_matrix for chrominance. */
993 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
997 /* Link the chrominance intra matrix to the luminance one. */
998 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
999 p_vpar->sequence.intra_quant.pi_matrix );
1001 if( GetBits( &p_vpar->bit_stream, 1 ) )
1003 /* Load non_intra_quantiser_matrix for chrominance. */
1004 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1008 /* Link the chrominance nonintra matrix to the luminance one. */
1009 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1010 p_vpar->sequence.nonintra_quant.pi_matrix );
1015 /*****************************************************************************
1016 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1017 * structure to handle scalable coding *
1018 *****************************************************************************/
1020 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1022 /* We don't care about anything scalable except the scalable mode. */
1023 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1024 /* The length of the structure depends on the value of the scalable_mode */
1027 RemoveBits32( &p_vpar->bit_stream );
1028 RemoveBits( &p_vpar->bit_stream, 21 );
1031 RemoveBits( &p_vpar->bit_stream, 12 );
1034 RemoveBits( &p_vpar->bit_stream, 4 );
1038 /*****************************************************************************
1039 * PictureDisplayExtension : Parse the picture_display_extension structure *
1040 *****************************************************************************/
1042 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1044 /* Number of frame center offset */
1046 /* I am not sure it works but it should
1047 (fewer tests than shown in §6.3.12) */
1048 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1049 p_vpar->picture.b_repeat_first_field +
1050 p_vpar->picture.b_top_field_first
1051 : ( p_vpar->picture.b_frame_structure + 1 ) +
1052 p_vpar->picture.b_repeat_first_field;
1053 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1055 RemoveBits( &p_vpar->bit_stream, 17 );
1056 RemoveBits( &p_vpar->bit_stream, 17 );
1061 /*****************************************************************************
1062 * PictureSpatialScalableExtension *
1063 *****************************************************************************/
1065 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1067 /* That's scalable, so we trash it */
1068 RemoveBits32( &p_vpar->bit_stream );
1069 RemoveBits( &p_vpar->bit_stream, 16 );
1073 /*****************************************************************************
1074 * PictureTemporalScalableExtension *
1075 *****************************************************************************/
1077 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1079 /* Scalable again, trashed again */
1080 RemoveBits( &p_vpar->bit_stream, 23 );
1084 /*****************************************************************************
1085 * CopyrightExtension : Keeps some legal informations *
1086 *****************************************************************************/
1088 static void CopyrightExtension( vpar_thread_t * p_vpar )
1090 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1091 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1092 /* A flag that says whether the copyright information is significant */
1093 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1094 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1095 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1097 RemoveBits( &p_vpar->bit_stream, 8 );
1098 /* The copyright_number is split in three parts */
1100 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1101 RemoveBits( &p_vpar->bit_stream, 1 );
1103 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1104 RemoveBits( &p_vpar->bit_stream, 1 );
1105 /* third part and sum */
1106 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1107 ( (u64)i_copyright_nb_2 << 22 ) |
1108 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );