1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.4 2001/07/25 15:37:34 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 );
607 p_vpar->picture.i_current_structure = 0;
609 intf_WarnMsg( 2, "Odd number of field pictures." );
612 /* Do we have the reference pictures ? */
613 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
614 (p_vpar->sequence.p_backward == NULL)) ||
615 /* p_backward will become p_forward later */
616 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
617 (p_vpar->sequence.p_forward == NULL ||
618 p_vpar->sequence.p_backward == NULL)));
620 if( p_vpar->picture.i_current_structure )
622 /* Second field of a frame. We will decode it if, and only if we
623 * have decoded the first field. */
626 b_parsable = (p_vpar->picture.p_picture != NULL);
633 /* Compute the number of times the frame will be emitted by the
634 * decoder (number of half-periods). */
635 if( p_vpar->sequence.b_progressive )
637 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
638 + p_vpar->picture.b_top_field_first) * 2;
642 if( p_vpar->picture.b_progressive )
644 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
652 /* Warn synchro we have a new picture (updates pictures index). */
653 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
658 /* Does synchro say we have enough time to decode it ? */
659 b_parsable = vpar_SynchroChoose( p_vpar,
660 p_vpar->picture.i_coding_type, i_structure );
666 /* Update the reference pointers. */
667 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
669 /* Update context. */
670 if( i_structure != FRAME_STRUCTURE )
672 if( (p_vpar->picture.i_current_structure | i_structure)
675 p_vpar->picture.i_current_structure = 0;
679 /* The frame is complete. */
680 p_vpar->picture.i_current_structure = i_structure;
682 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
687 /* Warn Synchro we have trashed a picture. */
688 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
690 p_vpar->picture.p_picture = NULL;
695 /* OK, now we are sure we will decode the picture. */
697 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
700 #define P_picture p_vpar->picture.p_picture
701 p_vpar->picture.b_error = 0;
702 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
704 if( !p_vpar->picture.i_current_structure )
706 /* This is a new frame. Get a structure from the video_output. */
707 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
708 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
709 p_vpar->sequence.i_width,
710 p_vpar->sequence.i_height ) )
713 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
714 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
718 msleep( VPAR_OUTMEM_SLEEP );
721 /* Initialize values. */
722 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
723 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
724 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
725 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
726 << ( 1 - p_vpar->picture.b_frame_structure ) );
727 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
728 << ( 1 - p_vpar->picture.b_frame_structure ));
730 /* FIXME ! remove asap ?? */
731 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
733 /* Update the reference pointers. */
734 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
736 p_vpar->picture.i_current_structure |= i_structure;
737 p_vpar->picture.i_structure = i_structure;
739 /* Initialize picture data for decoding. */
740 if( i_structure == BOTTOM_FIELD )
742 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
743 p_vpar->mb.i_l_y = 1;
744 p_vpar->mb.i_c_y = 1;
749 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
751 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
753 /* Extension and User data. */
754 ExtensionAndUserData( p_vpar );
756 /* This is an MP@ML decoder, please note that neither of the following
757 * assertions can be true :
758 * p_vpar->sequence.i_chroma_format != CHROMA_420
759 * p_vpar->sequence.i_height > 2800
760 * p_vpar->sequence.i_scalable_mode == SC_DP
761 * Be cautious if you try to use the decoder for other profiles and
764 if( p_vpar->sequence.b_mpeg2 )
766 static f_picture_data_t ppf_picture_data[4][4] =
769 NULL, NULL, NULL, NULL
773 #if (VPAR_OPTIM_LEVEL > 1)
774 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
777 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
778 vpar_PictureDataGENERIC
783 #if (VPAR_OPTIM_LEVEL > 1)
784 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
787 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
788 vpar_PictureDataGENERIC
793 #if (VPAR_OPTIM_LEVEL > 0)
794 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
797 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
798 vpar_PictureDataGENERIC
803 ppf_picture_data[p_vpar->picture.i_structure]
804 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
808 #if (VPAR_OPTIM_LEVEL > 0)
809 static f_picture_data_t pf_picture_data[5] =
810 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
811 vpar_PictureData1D };
813 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
815 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
819 /* Wait for all the macroblocks to be decoded. */
820 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
822 /* Re-spawn decoder threads if the user changed settings. */
823 vpar_SpawnPool( p_vpar );
825 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
830 if( p_vpar->picture.b_error )
834 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
837 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
838 p_vpar->picture.i_structure, 1 );
839 vout_DestroyPicture( p_vpar->p_vout, P_picture );
841 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
843 /* Prepare context for the next picture. */
845 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
846 p_vpar->picture.i_current_structure = 0;
848 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
850 /* Frame completely parsed. */
851 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
852 p_vpar->picture.i_structure, 0 );
853 vout_DisplayPicture( p_vpar->p_vout, P_picture );
855 /* Prepare context for the next picture. */
857 p_vpar->picture.i_current_structure = 0;
862 /*****************************************************************************
863 * ExtensionAndUserData : Parse the extension_and_user_data structure
864 *****************************************************************************/
865 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
867 while( !p_vpar->p_fifo->b_die )
869 NextStartCode( &p_vpar->bit_stream );
870 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
872 case EXTENSION_START_CODE:
873 RemoveBits32( &p_vpar->bit_stream );
874 switch( GetBits( &p_vpar->bit_stream, 4 ) )
876 case SEQUENCE_DISPLAY_EXTENSION_ID:
877 SequenceDisplayExtension( p_vpar );
879 case QUANT_MATRIX_EXTENSION_ID:
880 QuantMatrixExtension( p_vpar );
882 case SEQUENCE_SCALABLE_EXTENSION_ID:
883 SequenceScalableExtension( p_vpar );
885 case PICTURE_DISPLAY_EXTENSION_ID:
886 PictureDisplayExtension( p_vpar );
888 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
889 PictureSpatialScalableExtension( p_vpar );
891 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
892 PictureTemporalScalableExtension( p_vpar );
894 case COPYRIGHT_EXTENSION_ID:
895 CopyrightExtension( p_vpar );
902 case USER_DATA_START_CODE:
903 RemoveBits32( &p_vpar->bit_stream );
904 /* Wait for the next start code */
914 /*****************************************************************************
915 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
916 *****************************************************************************/
918 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
920 /* We don't care sequence_display_extension. */
922 RemoveBits( &p_vpar->bit_stream, 3 );
923 if( GetBits( &p_vpar->bit_stream, 1 ) )
925 /* Two bytes for color_desciption */
926 RemoveBits( &p_vpar->bit_stream, 16 );
927 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
929 /* display_horizontal and vertical_size and a marker_bit */
930 RemoveBits( &p_vpar->bit_stream, 29 );
934 /*****************************************************************************
935 * QuantMatrixExtension : Load quantization matrices for luminance *
937 *****************************************************************************/
939 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
941 if( GetBits( &p_vpar->bit_stream, 1 ) )
943 /* Load intra_quantiser_matrix for luminance. */
944 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
948 /* Use the default matrix. */
949 LinkMatrix( &p_vpar->sequence.intra_quant,
950 p_vpar->pi_default_intra_quant );
952 if( GetBits( &p_vpar->bit_stream, 1 ) )
954 /* Load non_intra_quantiser_matrix for luminance. */
955 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
959 /* Use the default matrix. */
960 LinkMatrix( &p_vpar->sequence.nonintra_quant,
961 p_vpar->pi_default_nonintra_quant );
963 if( GetBits( &p_vpar->bit_stream, 1 ) )
965 /* Load intra_quantiser_matrix for chrominance. */
966 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
970 /* Link the chrominance intra matrix to the luminance one. */
971 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
972 p_vpar->sequence.intra_quant.pi_matrix );
974 if( GetBits( &p_vpar->bit_stream, 1 ) )
976 /* Load non_intra_quantiser_matrix for chrominance. */
977 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
981 /* Link the chrominance intra matrix to the luminance one. */
982 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
983 p_vpar->sequence.intra_quant.pi_matrix );
985 if( GetBits( &p_vpar->bit_stream, 1 ) )
987 /* Load non_intra_quantiser_matrix for chrominance. */
988 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
992 /* Link the chrominance nonintra matrix to the luminance one. */
993 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
994 p_vpar->sequence.nonintra_quant.pi_matrix );
999 /*****************************************************************************
1000 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1001 * structure to handle scalable coding *
1002 *****************************************************************************/
1004 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1006 /* We don't care about anything scalable except the scalable mode. */
1007 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1008 /* The length of the structure depends on the value of the scalable_mode */
1011 RemoveBits32( &p_vpar->bit_stream );
1012 RemoveBits( &p_vpar->bit_stream, 21 );
1015 RemoveBits( &p_vpar->bit_stream, 12 );
1018 RemoveBits( &p_vpar->bit_stream, 4 );
1022 /*****************************************************************************
1023 * PictureDisplayExtension : Parse the picture_display_extension structure *
1024 *****************************************************************************/
1026 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1028 /* Number of frame center offset */
1030 /* I am not sure it works but it should
1031 (fewer tests than shown in §6.3.12) */
1032 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1033 p_vpar->picture.b_repeat_first_field +
1034 p_vpar->picture.b_top_field_first
1035 : ( p_vpar->picture.b_frame_structure + 1 ) +
1036 p_vpar->picture.b_repeat_first_field;
1037 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1039 RemoveBits( &p_vpar->bit_stream, 17 );
1040 RemoveBits( &p_vpar->bit_stream, 17 );
1045 /*****************************************************************************
1046 * PictureSpatialScalableExtension *
1047 *****************************************************************************/
1049 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1051 /* That's scalable, so we trash it */
1052 RemoveBits32( &p_vpar->bit_stream );
1053 RemoveBits( &p_vpar->bit_stream, 16 );
1057 /*****************************************************************************
1058 * PictureTemporalScalableExtension *
1059 *****************************************************************************/
1061 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1063 /* Scalable again, trashed again */
1064 RemoveBits( &p_vpar->bit_stream, 23 );
1068 /*****************************************************************************
1069 * CopyrightExtension : Keeps some legal informations *
1070 *****************************************************************************/
1072 static void CopyrightExtension( vpar_thread_t * p_vpar )
1074 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1075 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1076 /* A flag that says whether the copyright information is significant */
1077 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1078 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1079 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1081 RemoveBits( &p_vpar->bit_stream, 8 );
1082 /* The copyright_number is split in three parts */
1084 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1085 RemoveBits( &p_vpar->bit_stream, 1 );
1087 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1088 RemoveBits( &p_vpar->bit_stream, 1 );
1089 /* third part and sum */
1090 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1091 ( (u64)i_copyright_nb_2 << 22 ) |
1092 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );