1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.7 2001/08/22 17:21:46 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[] ATTR_ALIGN(16) =
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[] ATTR_ALIGN(16) =
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 *****************************************************************************/
103 u8 pi_scan[2][64] ATTR_ALIGN(16) =
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[0][i_dummy]]
200 = GetBits( &p_vpar->bit_stream, 8 );
204 /*****************************************************************************
205 * LinkMatrix : Link a quantization matrix to another
206 *****************************************************************************/
207 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
209 if( p_matrix->b_allocated )
211 /* Deallocate the piece of memory. */
212 free( p_matrix->pi_matrix );
213 p_matrix->b_allocated = 0;
216 p_matrix->pi_matrix = pi_array;
220 * Exported functions.
223 /*****************************************************************************
224 * vpar_NextSequenceHeader : Find the next sequence header
225 *****************************************************************************/
226 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
228 while( !p_vpar->p_fifo->b_die )
230 NextStartCode( &p_vpar->bit_stream );
231 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
235 RemoveBits( &p_vpar->bit_stream, 8 );
240 /*****************************************************************************
241 * vpar_ParseHeader : Parse the next header
242 *****************************************************************************/
243 int vpar_ParseHeader( vpar_thread_t * p_vpar )
245 while( !p_vpar->p_fifo->b_die )
247 NextStartCode( &p_vpar->bit_stream );
248 switch( GetBits32( &p_vpar->bit_stream ) )
250 case SEQUENCE_HEADER_CODE:
252 p_vpar->c_sequences++;
254 SequenceHeader( p_vpar );
258 case GROUP_START_CODE:
259 GroupHeader( p_vpar );
263 case PICTURE_START_CODE:
264 PictureHeader( p_vpar );
268 case SEQUENCE_END_CODE:
269 intf_DbgMsg("vpar debug: sequence end code received");
282 * Following functions are local
285 /*****************************************************************************
286 * SequenceHeader : Parse the next sequence header
287 *****************************************************************************/
288 static void SequenceHeader( vpar_thread_t * p_vpar )
291 static int i_frame_rate_table[16] =
302 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
306 int i_height_save, i_width_save;
308 i_height_save = p_vpar->sequence.i_height;
309 i_width_save = p_vpar->sequence.i_width;
311 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
312 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
313 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
314 p_vpar->sequence.i_frame_rate =
315 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
317 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
318 * constrained_parameters_flag */
319 RemoveBits( &p_vpar->bit_stream, 30 );
322 * Quantization matrices
324 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
326 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
330 /* Use default matrix. */
331 LinkMatrix( &p_vpar->sequence.intra_quant,
332 p_vpar->pi_default_intra_quant );
335 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
337 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
341 /* Use default matrix. */
342 LinkMatrix( &p_vpar->sequence.nonintra_quant,
343 p_vpar->pi_default_nonintra_quant );
346 /* Unless later overwritten by a matrix extension, we have the same
347 * matrices for luminance and chrominance. */
348 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
349 p_vpar->sequence.intra_quant.pi_matrix );
350 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
351 p_vpar->sequence.nonintra_quant.pi_matrix );
356 NextStartCode( &p_vpar->bit_stream );
357 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
361 /* Turn the MPEG2 flag on */
362 p_vpar->sequence.b_mpeg2 = 1;
364 /* Parse sequence_extension */
365 RemoveBits32( &p_vpar->bit_stream );
366 /* extension_start_code_identifier, profile_and_level_indication */
367 RemoveBits( &p_vpar->bit_stream, 12 );
368 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
369 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
370 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
371 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
372 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
374 RemoveBits( &p_vpar->bit_stream, 22 );
375 /* frame_rate_extension_n */
376 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
377 /* frame_rate_extension_d */
378 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
379 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
383 /* It's an MPEG-1 stream. Put adequate parameters. */
385 p_vpar->sequence.b_mpeg2 = 0;
386 p_vpar->sequence.b_progressive = 1;
387 p_vpar->sequence.i_chroma_format = CHROMA_420;
391 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
392 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
393 (p_vpar->sequence.i_height + 15) / 16 :
394 2 * ((p_vpar->sequence.i_height + 31) / 32);
395 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
396 * p_vpar->sequence.i_mb_height;
397 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
398 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
399 p_vpar->sequence.i_size = p_vpar->sequence.i_width
400 * p_vpar->sequence.i_height;
403 if( p_vpar->sequence.i_width != i_width_save
404 || p_vpar->sequence.i_height != i_height_save )
406 /* FIXME: Warn the video output */
410 /* Extension and User data */
411 ExtensionAndUserData( p_vpar );
413 /* XXX: The vout request and fifo opening will eventually be here */
415 /* Spawn a video output if there is none */
416 vlc_mutex_lock( &p_vout_bank->lock );
418 if( p_vout_bank->i_count == 0 )
420 intf_WarnMsg( 1, "vpar: no vout present, spawning one" );
422 p_vpar->p_vout = vout_CreateThread( NULL );
424 /* Everything failed */
425 if( p_vpar->p_vout == NULL )
427 intf_ErrMsg( "vpar error: can't open vout, aborting" );
428 vlc_mutex_unlock( &p_vout_bank->lock );
430 /* XXX ! XXX ! XXX ! what to do here ? */
434 p_vout_bank->pp_vout[ p_vout_bank->i_count ] = p_vpar->p_vout;
435 p_vout_bank->i_count++;
439 /* Take the first video output FIXME: take the best one */
440 p_vpar->p_vout = p_vout_bank->pp_vout[ 0 ];
443 vlc_mutex_unlock( &p_vout_bank->lock );
446 /*****************************************************************************
447 * GroupHeader : Parse the next group of pictures header
448 *****************************************************************************/
449 static void GroupHeader( vpar_thread_t * p_vpar )
451 /* Nothing to do, we don't care. */
452 RemoveBits( &p_vpar->bit_stream, 27 );
453 ExtensionAndUserData( p_vpar );
456 /*****************************************************************************
457 * PictureHeader : Parse the next picture header
458 *****************************************************************************/
459 static void PictureHeader( vpar_thread_t * p_vpar )
462 boolean_t b_parsable;
467 /* Recover in case of stream discontinuity. */
468 if( p_vpar->sequence.b_expect_discontinuity )
470 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
471 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
472 if( p_vpar->picture.p_picture != NULL )
474 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
475 p_vpar->picture.p_picture = NULL;
477 p_vpar->picture.i_current_structure = 0;
478 p_vpar->sequence.b_expect_discontinuity = 0;
481 /* Parse the picture header. */
482 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
483 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
484 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
486 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
487 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
489 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
490 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
493 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
495 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
496 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
500 /* extra_information_picture */
501 while( GetBits( &p_vpar->bit_stream, 1 ) )
503 RemoveBits( &p_vpar->bit_stream, 8 );
507 * Picture Coding Extension
509 NextStartCode( &p_vpar->bit_stream );
510 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
512 /* Parse picture_coding_extension */
513 RemoveBits32( &p_vpar->bit_stream );
514 /* extension_start_code_identifier */
515 RemoveBits( &p_vpar->bit_stream, 4 );
517 /* Pre-substract 1 for later use in MotionDelta(). */
518 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
519 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
520 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
521 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
522 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
523 i_structure = GetBits( &p_vpar->bit_stream, 2 );
524 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
525 p_vpar->picture.b_frame_pred_frame_dct
526 = GetBits( &p_vpar->bit_stream, 1 );
527 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
528 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
529 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
531 p_vpar->picture.pi_scan =
532 p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
533 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
534 /* chroma_420_type (obsolete) */
535 RemoveBits( &p_vpar->bit_stream, 1 );
536 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
538 /* composite_display_flag */
539 if( GetBits( &p_vpar->bit_stream, 1 ) )
541 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
542 * sub_carrier_phase */
543 RemoveBits( &p_vpar->bit_stream, 20 );
548 /* MPEG-1 compatibility flags */
549 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
550 i_structure = FRAME_STRUCTURE;
551 p_vpar->picture.b_top_field_first = 0;
552 p_vpar->picture.b_frame_pred_frame_dct = 1;
553 p_vpar->picture.b_concealment_mv = 0;
554 p_vpar->picture.b_q_scale_type = 0;
555 p_vpar->picture.b_intra_vlc_format = 0;
556 p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
557 p_vpar->picture.b_repeat_first_field = 0;
558 p_vpar->picture.b_progressive = 1;
561 /* Extension and User data. */
562 ExtensionAndUserData( p_vpar );
565 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
568 if( p_vpar->picture.i_current_structure &&
569 (i_structure == FRAME_STRUCTURE ||
570 i_structure == p_vpar->picture.i_current_structure) )
572 /* We don't have the second field of the buffered frame. */
573 if( p_vpar->picture.p_picture != NULL )
575 ReferenceReplace( p_vpar,
576 p_vpar->picture.i_coding_type,
578 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
579 p_vpar->picture.p_picture = NULL;
582 p_vpar->picture.i_current_structure = 0;
584 intf_WarnMsg( 2, "Odd number of field pictures." );
587 /* Do we have the reference pictures ? */
588 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
589 p_vpar->picture.b_concealment_mv) &&
590 (p_vpar->sequence.p_backward == NULL)) ||
591 /* p_backward will become p_forward later */
592 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
593 (p_vpar->sequence.p_forward == NULL ||
594 p_vpar->sequence.p_backward == NULL)));
596 if( p_vpar->picture.i_current_structure )
598 /* Second field of a frame. We will decode it if, and only if we
599 * have decoded the first field. */
602 if( p_vpar->picture.p_picture == NULL )
604 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
605 && p_vpar->sequence.p_backward == NULL) )
607 /* Exceptionnally, parse the picture if it is I. We need
608 * this in case of an odd number of field pictures, if the
609 * previous picture is not intra, we desperately need a
610 * new reference picture. OK, this is kind of kludgy. */
611 p_vpar->picture.i_current_structure = 0;
624 /* Compute the number of times the frame will be emitted by the
625 * decoder (number of half-periods). */
626 if( p_vpar->sequence.b_progressive )
628 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
629 + p_vpar->picture.b_top_field_first) * 2;
633 if( p_vpar->picture.b_progressive )
635 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
643 /* Warn synchro we have a new picture (updates pictures index). */
644 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
649 /* Does synchro say we have enough time to decode it ? */
650 b_parsable = vpar_SynchroChoose( p_vpar,
651 p_vpar->picture.i_coding_type, i_structure );
657 /* Update the reference pointers. */
658 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
660 /* Update context. */
661 if( i_structure != FRAME_STRUCTURE )
663 if( (p_vpar->picture.i_current_structure | i_structure)
666 /* The frame is complete. */
667 p_vpar->picture.i_current_structure = 0;
669 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
673 p_vpar->picture.i_current_structure = i_structure;
678 /* Warn Synchro we have trashed a picture. */
679 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
681 p_vpar->picture.p_picture = NULL;
686 /* OK, now we are sure we will decode the picture. */
688 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
691 #define P_picture p_vpar->picture.p_picture
692 p_vpar->picture.b_error = 0;
693 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
695 if( !p_vpar->picture.i_current_structure )
697 /* This is a new frame. Get a structure from the video_output. */
698 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
699 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
700 p_vpar->sequence.i_width,
701 p_vpar->sequence.i_height ) )
704 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
705 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
709 msleep( VPAR_OUTMEM_SLEEP );
712 /* Initialize values. */
713 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
714 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
715 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
716 p_vpar->picture.i_field_width = ( p_vpar->sequence.i_width
717 << ( 1 - p_vpar->picture.b_frame_structure ) );
719 /* FIXME ! remove asap ?? */
720 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
722 /* Update the reference pointers. */
723 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
726 /* Initialize picture data for decoding. */
727 p_vpar->picture.i_current_structure |= i_structure;
728 p_vpar->picture.i_structure = i_structure;
729 p_vpar->picture.b_second_field =
730 (i_structure != p_vpar->picture.i_current_structure);
731 p_vpar->picture.b_current_field =
732 (i_structure == BOTTOM_FIELD );
734 if( p_vpar->sequence.b_mpeg2 )
736 static f_picture_data_t ppf_picture_data[4][4] =
739 NULL, NULL, NULL, NULL
743 #if (VPAR_OPTIM_LEVEL > 1)
744 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
747 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
748 vpar_PictureDataGENERIC
753 #if (VPAR_OPTIM_LEVEL > 1)
754 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
757 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
758 vpar_PictureDataGENERIC
763 #if (VPAR_OPTIM_LEVEL > 0)
764 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
767 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
768 vpar_PictureDataGENERIC
773 ppf_picture_data[p_vpar->picture.i_structure]
774 [p_vpar->picture.i_coding_type]( p_vpar );
778 #if (VPAR_OPTIM_LEVEL > 0)
779 static f_picture_data_t pf_picture_data[5] =
780 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
781 vpar_PictureData1D };
783 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
785 vpar_PictureDataGENERIC( p_vpar );
789 /* Wait for all the macroblocks to be decoded. */
790 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
792 /* Re-spawn decoder threads if the user changed settings. */
793 vpar_SpawnPool( p_vpar );
795 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
800 if( p_vpar->picture.b_error )
804 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
807 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
808 p_vpar->picture.i_structure, 1 );
809 vout_DestroyPicture( p_vpar->p_vout, P_picture );
811 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
813 /* Prepare context for the next picture. */
815 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
816 p_vpar->picture.i_current_structure = 0;
818 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
820 /* Frame completely parsed. */
821 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
822 p_vpar->picture.i_structure, 0 );
823 vout_DisplayPicture( p_vpar->p_vout, P_picture );
825 /* Prepare context for the next picture. */
827 p_vpar->picture.i_current_structure = 0;
832 /*****************************************************************************
833 * ExtensionAndUserData : Parse the extension_and_user_data structure
834 *****************************************************************************/
835 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
837 while( !p_vpar->p_fifo->b_die )
839 NextStartCode( &p_vpar->bit_stream );
840 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
842 case EXTENSION_START_CODE:
843 RemoveBits32( &p_vpar->bit_stream );
844 switch( GetBits( &p_vpar->bit_stream, 4 ) )
846 case SEQUENCE_DISPLAY_EXTENSION_ID:
847 SequenceDisplayExtension( p_vpar );
849 case QUANT_MATRIX_EXTENSION_ID:
850 QuantMatrixExtension( p_vpar );
852 case SEQUENCE_SCALABLE_EXTENSION_ID:
853 SequenceScalableExtension( p_vpar );
855 case PICTURE_DISPLAY_EXTENSION_ID:
856 PictureDisplayExtension( p_vpar );
858 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
859 PictureSpatialScalableExtension( p_vpar );
861 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
862 PictureTemporalScalableExtension( p_vpar );
864 case COPYRIGHT_EXTENSION_ID:
865 CopyrightExtension( p_vpar );
872 case USER_DATA_START_CODE:
873 RemoveBits32( &p_vpar->bit_stream );
874 /* Wait for the next start code */
884 /*****************************************************************************
885 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
886 *****************************************************************************/
888 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
890 /* We don't care sequence_display_extension. */
892 RemoveBits( &p_vpar->bit_stream, 3 );
893 if( GetBits( &p_vpar->bit_stream, 1 ) )
895 /* Two bytes for color_desciption */
896 RemoveBits( &p_vpar->bit_stream, 16 );
897 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
899 /* display_horizontal and vertical_size and a marker_bit */
900 RemoveBits( &p_vpar->bit_stream, 29 );
904 /*****************************************************************************
905 * QuantMatrixExtension : Load quantization matrices for luminance *
907 *****************************************************************************/
909 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
911 if( GetBits( &p_vpar->bit_stream, 1 ) )
913 /* Load intra_quantiser_matrix for luminance. */
914 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
918 /* Use the default matrix. */
919 LinkMatrix( &p_vpar->sequence.intra_quant,
920 p_vpar->pi_default_intra_quant );
922 if( GetBits( &p_vpar->bit_stream, 1 ) )
924 /* Load non_intra_quantiser_matrix for luminance. */
925 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
929 /* Use the default matrix. */
930 LinkMatrix( &p_vpar->sequence.nonintra_quant,
931 p_vpar->pi_default_nonintra_quant );
933 if( GetBits( &p_vpar->bit_stream, 1 ) )
935 /* Load intra_quantiser_matrix for chrominance. */
936 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
940 /* Link the chrominance intra matrix to the luminance one. */
941 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
942 p_vpar->sequence.intra_quant.pi_matrix );
944 if( GetBits( &p_vpar->bit_stream, 1 ) )
946 /* Load non_intra_quantiser_matrix for chrominance. */
947 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
951 /* Link the chrominance intra matrix to the luminance one. */
952 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
953 p_vpar->sequence.intra_quant.pi_matrix );
955 if( GetBits( &p_vpar->bit_stream, 1 ) )
957 /* Load non_intra_quantiser_matrix for chrominance. */
958 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
962 /* Link the chrominance nonintra matrix to the luminance one. */
963 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
964 p_vpar->sequence.nonintra_quant.pi_matrix );
969 /*****************************************************************************
970 * SequenceScalableExtension : Parse the sequence_scalable_extension *
971 * structure to handle scalable coding *
972 *****************************************************************************/
974 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
976 /* We don't care about anything scalable except the scalable mode. */
977 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
978 /* The length of the structure depends on the value of the scalable_mode */
981 RemoveBits32( &p_vpar->bit_stream );
982 RemoveBits( &p_vpar->bit_stream, 21 );
985 RemoveBits( &p_vpar->bit_stream, 12 );
988 RemoveBits( &p_vpar->bit_stream, 4 );
992 /*****************************************************************************
993 * PictureDisplayExtension : Parse the picture_display_extension structure *
994 *****************************************************************************/
996 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
998 /* Number of frame center offset */
1000 /* I am not sure it works but it should
1001 (fewer tests than shown in §6.3.12) */
1002 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1003 p_vpar->picture.b_repeat_first_field +
1004 p_vpar->picture.b_top_field_first
1005 : ( p_vpar->picture.b_frame_structure + 1 ) +
1006 p_vpar->picture.b_repeat_first_field;
1007 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1009 RemoveBits( &p_vpar->bit_stream, 17 );
1010 RemoveBits( &p_vpar->bit_stream, 17 );
1015 /*****************************************************************************
1016 * PictureSpatialScalableExtension *
1017 *****************************************************************************/
1019 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1021 /* That's scalable, so we trash it */
1022 RemoveBits32( &p_vpar->bit_stream );
1023 RemoveBits( &p_vpar->bit_stream, 16 );
1027 /*****************************************************************************
1028 * PictureTemporalScalableExtension *
1029 *****************************************************************************/
1031 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1033 /* Scalable again, trashed again */
1034 RemoveBits( &p_vpar->bit_stream, 23 );
1038 /*****************************************************************************
1039 * CopyrightExtension : Keeps some legal informations *
1040 *****************************************************************************/
1042 static void CopyrightExtension( vpar_thread_t * p_vpar )
1044 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1045 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1046 /* A flag that says whether the copyright information is significant */
1047 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1048 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1049 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1051 RemoveBits( &p_vpar->bit_stream, 8 );
1052 /* The copyright_number is split in three parts */
1054 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1055 RemoveBits( &p_vpar->bit_stream, 1 );
1057 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1058 RemoveBits( &p_vpar->bit_stream, 1 );
1059 /* third part and sum */
1060 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1061 ( (u64)i_copyright_nb_2 << 22 ) |
1062 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );