1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.83 2001/05/01 04:18:18 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Stéphane Borel <stef@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <stdlib.h> /* free() */
31 #include <string.h> /* memcpy(), memset() */
40 #include "stream_control.h"
41 #include "input_ext-dec.h"
44 #include "video_output.h"
46 #include "video_decoder.h"
47 #include "vdec_motion.h"
48 #include "../video_decoder/vdec_idct.h"
50 #include "vpar_blocks.h"
51 #include "../video_decoder/vpar_headers.h"
52 #include "../video_decoder/vpar_synchro.h"
53 #include "../video_decoder/video_parser.h"
54 #include "../video_decoder/video_fifo.h"
56 #include "main.h" /* XXX REMOVE THIS */
61 static __inline__ void NextStartCode( bit_stream_t * );
62 static void SequenceHeader( vpar_thread_t * p_vpar );
63 static void GroupHeader( vpar_thread_t * p_vpar );
64 static void PictureHeader( vpar_thread_t * p_vpar );
65 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
66 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
67 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
68 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
69 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
70 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
71 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
72 static void CopyrightExtension( vpar_thread_t * p_vpar );
78 /*****************************************************************************
79 * pi_default_intra_quant : default quantization matrix
80 *****************************************************************************/
81 u8 pi_default_intra_quant[] =
83 8, 16, 19, 22, 26, 27, 29, 34,
84 16, 16, 22, 24, 27, 29, 34, 37,
85 19, 22, 26, 27, 29, 34, 34, 38,
86 22, 22, 26, 27, 29, 34, 37, 40,
87 22, 26, 27, 29, 32, 35, 40, 48,
88 26, 27, 29, 32, 35, 40, 48, 58,
89 26, 27, 29, 34, 38, 46, 56, 69,
90 27, 29, 35, 38, 46, 56, 69, 83
93 /*****************************************************************************
94 * pi_default_nonintra_quant : default quantization matrix
95 *****************************************************************************/
96 u8 pi_default_nonintra_quant[] =
98 16, 16, 16, 16, 16, 16, 16, 16,
99 16, 16, 16, 16, 16, 16, 16, 16,
100 16, 16, 16, 16, 16, 16, 16, 16,
101 16, 16, 16, 16, 16, 16, 16, 16,
102 16, 16, 16, 16, 16, 16, 16, 16,
103 16, 16, 16, 16, 16, 16, 16, 16,
104 16, 16, 16, 16, 16, 16, 16, 16,
105 16, 16, 16, 16, 16, 16, 16, 16
108 /*****************************************************************************
109 * pi_scan : zig-zag and alternate scan patterns
110 *****************************************************************************/
113 { /* Zig-Zag pattern */
114 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
115 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
116 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
117 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
119 { /* Alternate scan pattern */
120 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
121 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
122 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
123 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
128 * Local inline functions.
131 /*****************************************************************************
132 * ReferenceUpdate : Update the reference pointers when we have a new picture
133 *****************************************************************************/
134 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
136 picture_t * p_newref )
138 if( i_coding_type != B_CODING_TYPE )
140 if( p_vpar->sequence.p_forward != NULL )
142 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
144 if( p_vpar->sequence.p_backward != NULL )
146 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
147 vpar_SynchroDate( p_vpar ) );
149 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
150 p_vpar->sequence.p_backward = p_newref;
151 if( p_newref != NULL )
153 vout_LinkPicture( p_vpar->p_vout, p_newref );
156 else if( p_newref != NULL )
158 /* Put date immediately. */
159 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
163 /*****************************************************************************
164 * ReferenceReplace : Replace the last reference pointer when we destroy
166 *****************************************************************************/
167 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
169 picture_t * p_newref )
171 if( i_coding_type != B_CODING_TYPE )
173 if( p_vpar->sequence.p_backward != NULL )
175 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
177 p_vpar->sequence.p_backward = p_newref;
178 if( p_newref != NULL )
180 vout_LinkPicture( p_vpar->p_vout, p_newref );
185 /*****************************************************************************
186 * LoadMatrix : Load a quantization matrix
187 *****************************************************************************/
188 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
189 quant_matrix_t * p_matrix )
193 if( !p_matrix->b_allocated )
195 /* Allocate a piece of memory to load the matrix. */
196 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
198 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
199 p_vpar->p_fifo->b_error = 1;
202 p_matrix->b_allocated = 1;
205 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
207 p_matrix->pi_matrix[p_vpar->ppi_scan[SCAN_ZIGZAG][i_dummy]]
208 = GetBits( &p_vpar->bit_stream, 8 );
212 /* Discrete Fourier Transform requires the quantization matrices to
213 * be normalized before using them. */
214 vdec_NormQuantMatrix( p_matrix->pi_matrix );
218 /*****************************************************************************
219 * LinkMatrix : Link a quantization matrix to another
220 *****************************************************************************/
221 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
223 if( p_matrix->b_allocated )
225 /* Deallocate the piece of memory. */
226 free( p_matrix->pi_matrix );
227 p_matrix->b_allocated = 0;
230 p_matrix->pi_matrix = pi_array;
234 * Exported functions.
237 /*****************************************************************************
238 * vpar_NextSequenceHeader : Find the next sequence header
239 *****************************************************************************/
240 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
242 while( !p_vpar->p_fifo->b_die )
244 NextStartCode( &p_vpar->bit_stream );
245 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
249 RemoveBits( &p_vpar->bit_stream, 8 );
254 /*****************************************************************************
255 * vpar_ParseHeader : Parse the next header
256 *****************************************************************************/
257 int vpar_ParseHeader( vpar_thread_t * p_vpar )
259 while( !p_vpar->p_fifo->b_die )
261 NextStartCode( &p_vpar->bit_stream );
262 switch( GetBits32( &p_vpar->bit_stream ) )
264 case SEQUENCE_HEADER_CODE:
266 p_vpar->c_sequences++;
268 SequenceHeader( p_vpar );
272 case GROUP_START_CODE:
273 GroupHeader( p_vpar );
277 case PICTURE_START_CODE:
278 PictureHeader( p_vpar );
282 case SEQUENCE_END_CODE:
283 intf_DbgMsg("vpar debug: sequence end code received");
296 * Following functions are local
299 /*****************************************************************************
300 * SequenceHeader : Parse the next sequence header
301 *****************************************************************************/
302 static void SequenceHeader( vpar_thread_t * p_vpar )
305 static int i_frame_rate_table[16] =
316 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
320 int i_height_save, i_width_save;
322 i_height_save = p_vpar->sequence.i_height;
323 i_width_save = p_vpar->sequence.i_width;
325 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
326 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
327 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
328 p_vpar->sequence.i_frame_rate =
329 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
331 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
332 * constrained_parameters_flag */
333 RemoveBits( &p_vpar->bit_stream, 30 );
336 * Quantization matrices
338 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
340 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
344 /* Use default matrix. */
345 LinkMatrix( &p_vpar->sequence.intra_quant,
346 p_vpar->pi_default_intra_quant );
349 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
351 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
355 /* Use default matrix. */
356 LinkMatrix( &p_vpar->sequence.nonintra_quant,
357 p_vpar->pi_default_nonintra_quant );
360 /* Unless later overwritten by a matrix extension, we have the same
361 * matrices for luminance and chrominance. */
362 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
363 p_vpar->sequence.intra_quant.pi_matrix );
364 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
365 p_vpar->sequence.nonintra_quant.pi_matrix );
370 NextStartCode( &p_vpar->bit_stream );
371 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
375 /* Turn the MPEG2 flag on */
376 p_vpar->sequence.b_mpeg2 = 1;
378 /* Parse sequence_extension */
379 RemoveBits32( &p_vpar->bit_stream );
380 /* extension_start_code_identifier, profile_and_level_indication */
381 RemoveBits( &p_vpar->bit_stream, 12 );
382 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
383 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
384 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
385 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
386 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
388 RemoveBits( &p_vpar->bit_stream, 22 );
389 /* frame_rate_extension_n */
390 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
391 /* frame_rate_extension_d */
392 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
393 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
397 /* It's an MPEG-1 stream. Put adequate parameters. */
399 p_vpar->sequence.b_mpeg2 = 0;
400 p_vpar->sequence.b_progressive = 1;
401 p_vpar->sequence.i_chroma_format = CHROMA_420;
405 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
406 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
407 (p_vpar->sequence.i_height + 15) / 16 :
408 2 * ((p_vpar->sequence.i_height + 31) / 32);
409 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
410 * p_vpar->sequence.i_mb_height;
411 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
412 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
413 p_vpar->sequence.i_size = p_vpar->sequence.i_width
414 * p_vpar->sequence.i_height;
416 /* Update chromatic information. */
417 switch( p_vpar->sequence.i_chroma_format )
420 p_vpar->sequence.i_chroma_nb_blocks = 2;
421 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
422 p_vpar->sequence.i_chroma_mb_width = 8;
423 p_vpar->sequence.i_chroma_mb_height = 8;
427 p_vpar->sequence.i_chroma_nb_blocks = 4;
428 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
429 p_vpar->sequence.i_chroma_mb_width = 8;
430 p_vpar->sequence.i_chroma_mb_height = 16;
434 p_vpar->sequence.i_chroma_nb_blocks = 8;
435 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
436 p_vpar->sequence.i_chroma_mb_width = 16;
437 p_vpar->sequence.i_chroma_mb_height = 16;
440 /* Reset scalable_mode. */
441 p_vpar->sequence.i_scalable_mode = SC_NONE;
444 if( p_vpar->sequence.i_width != i_width_save
445 || p_vpar->sequence.i_height != i_height_save )
447 /* FIXME: What do we do in case of a size change ?? */
451 /* Extension and User data */
452 ExtensionAndUserData( p_vpar );
454 /* XXX: The vout request and fifo opening will eventually be here */
455 if( p_main->p_vout == NULL )
457 intf_Msg( "vpar: no vout present, spawning one" );
458 p_main->p_vout = p_vpar->p_vout = vout_CreateThread( NULL );
462 /*****************************************************************************
463 * GroupHeader : Parse the next group of pictures header
464 *****************************************************************************/
465 static void GroupHeader( vpar_thread_t * p_vpar )
467 /* Nothing to do, we don't care. */
468 RemoveBits( &p_vpar->bit_stream, 27 );
469 ExtensionAndUserData( p_vpar );
472 /*****************************************************************************
473 * PictureHeader : Parse the next picture header
474 *****************************************************************************/
475 static void PictureHeader( vpar_thread_t * p_vpar )
479 boolean_t b_parsable;
484 /* Recover in case of stream discontinuity. */
485 if( p_vpar->sequence.b_expect_discontinuity )
487 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
488 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
489 if( p_vpar->picture.p_picture != NULL )
494 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
496 vpar_DestroyMacroblock( &p_vpar->vfifo,
497 p_vpar->picture.pp_mb[i_mb] );
500 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
502 p_vpar->sequence.b_expect_discontinuity = 0;
505 /* Parse the picture header. */
506 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
507 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
508 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
510 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
511 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
513 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
514 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
516 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
518 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
519 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
522 /* extra_information_picture */
523 while( GetBits( &p_vpar->bit_stream, 1 ) )
525 RemoveBits( &p_vpar->bit_stream, 8 );
529 * Picture Coding Extension
531 NextStartCode( &p_vpar->bit_stream );
532 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
534 /* Parse picture_coding_extension */
535 RemoveBits32( &p_vpar->bit_stream );
536 /* extension_start_code_identifier */
537 RemoveBits( &p_vpar->bit_stream, 4 );
539 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
540 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
541 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
542 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
543 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
544 i_structure = GetBits( &p_vpar->bit_stream, 2 );
545 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
546 p_vpar->picture.b_frame_pred_frame_dct
547 = GetBits( &p_vpar->bit_stream, 1 );
548 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
549 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
550 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
551 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
552 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
553 /* chroma_420_type (obsolete) */
554 RemoveBits( &p_vpar->bit_stream, 1 );
555 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
557 /* composite_display_flag */
558 if( GetBits( &p_vpar->bit_stream, 1 ) )
560 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
561 * sub_carrier_phase */
562 RemoveBits( &p_vpar->bit_stream, 20 );
567 /* MPEG-1 compatibility flags */
568 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
569 i_structure = FRAME_STRUCTURE;
570 p_vpar->picture.b_top_field_first = 0;
571 p_vpar->picture.b_frame_pred_frame_dct = 1;
572 p_vpar->picture.b_concealment_mv = 0;
573 p_vpar->picture.b_q_scale_type = 0;
574 p_vpar->picture.b_intra_vlc_format = 0;
575 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
576 p_vpar->picture.b_repeat_first_field = 0;
577 p_vpar->picture.b_progressive = 1;
581 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
584 if( p_vpar->picture.i_current_structure &&
585 (i_structure == FRAME_STRUCTURE ||
586 i_structure == p_vpar->picture.i_current_structure) )
588 /* We don't have the second field of the buffered frame. */
589 if( p_vpar->picture.p_picture != NULL )
591 ReferenceReplace( p_vpar,
592 p_vpar->picture.i_coding_type,
596 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
598 vpar_DestroyMacroblock( &p_vpar->vfifo,
599 p_vpar->picture.pp_mb[i_mb] );
602 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
605 p_vpar->picture.i_current_structure = 0;
607 intf_WarnMsg( 2, "Odd number of field pictures." );
610 /* Do we have the reference pictures ? */
611 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
612 (p_vpar->sequence.p_backward == NULL)) ||
613 /* p_backward will become p_forward later */
614 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
615 (p_vpar->sequence.p_forward == NULL ||
616 p_vpar->sequence.p_backward == NULL)));
618 if( p_vpar->picture.i_current_structure )
620 /* Second field of a frame. We will decode it if, and only if we
621 * have decoded the first field. */
624 b_parsable = (p_vpar->picture.p_picture != NULL);
631 /* Compute the number of times the frame will be emitted by the
632 * decoder (number of half-periods). */
633 if( p_vpar->sequence.b_progressive )
635 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
636 + p_vpar->picture.b_top_field_first) * 2;
640 if( p_vpar->picture.b_progressive )
642 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
650 /* Warn synchro we have a new picture (updates pictures index). */
651 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
656 /* Does synchro say we have enough time to decode it ? */
657 b_parsable = vpar_SynchroChoose( p_vpar,
658 p_vpar->picture.i_coding_type, i_structure );
664 /* Update the reference pointers. */
665 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
667 /* Update context. */
668 if( i_structure != FRAME_STRUCTURE )
670 if( (p_vpar->picture.i_current_structure | i_structure)
673 p_vpar->picture.i_current_structure = 0;
677 /* The frame is complete. */
678 p_vpar->picture.i_current_structure = i_structure;
680 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
685 /* Warn Synchro we have trashed a picture. */
686 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
688 p_vpar->picture.p_picture = NULL;
693 /* OK, now we are sure we will decode the picture. */
695 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
698 #define P_picture p_vpar->picture.p_picture
699 p_vpar->picture.b_error = 0;
700 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
702 if( !p_vpar->picture.i_current_structure )
704 /* This is a new frame. Get a structure from the video_output. */
705 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
706 /* XXX */ 99+p_vpar->sequence.i_chroma_format,
707 p_vpar->sequence.i_width,
708 p_vpar->sequence.i_height ) )
711 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
712 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
716 msleep( VPAR_OUTMEM_SLEEP );
719 /* Initialize values. */
720 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
721 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
722 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
723 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
724 << ( 1 - p_vpar->picture.b_frame_structure ) );
725 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
726 << ( 1 - p_vpar->picture.b_frame_structure ));
728 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
730 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
732 /* FIXME ! remove asap ?? */
733 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
735 /* Update the reference pointers. */
736 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
739 /* Link referenced pictures for the decoder
740 * They are unlinked in vpar_ReleaseMacroblock() &
741 * vpar_DestroyMacroblock() */
742 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
743 p_vpar->picture.i_coding_type == B_CODING_TYPE )
745 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
747 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
749 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
753 p_vpar->picture.i_current_structure |= i_structure;
754 p_vpar->picture.i_structure = i_structure;
756 /* Initialize picture data for decoding. */
757 if( i_structure == BOTTOM_FIELD )
759 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
760 p_vpar->mb.i_l_y = 1;
761 p_vpar->mb.i_c_y = 1;
766 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
768 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
770 /* Extension and User data. */
771 ExtensionAndUserData( p_vpar );
773 /* This is an MP@ML decoder, please note that neither of the following
774 * assertions can be true :
775 * p_vpar->sequence.i_chroma_format != CHROMA_420
776 * p_vpar->sequence.i_height > 2800
777 * p_vpar->sequence.i_scalable_mode == SC_DP
778 * Be cautious if you try to use the decoder for other profiles and
781 if( p_vpar->sequence.b_mpeg2 )
783 static f_picture_data_t ppf_picture_data[4][4] =
786 NULL, NULL, NULL, NULL
790 #if (VPAR_OPTIM_LEVEL > 1)
791 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
794 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
795 vpar_PictureDataGENERIC
800 #if (VPAR_OPTIM_LEVEL > 1)
801 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
804 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
805 vpar_PictureDataGENERIC
810 #if (VPAR_OPTIM_LEVEL > 0)
811 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
814 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
815 vpar_PictureDataGENERIC
820 ppf_picture_data[p_vpar->picture.i_structure]
821 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
825 #if (VPAR_OPTIM_LEVEL > 0)
826 static f_picture_data_t pf_picture_data[5] =
827 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
828 vpar_PictureData1D };
830 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
832 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
836 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
841 if( p_vpar->picture.b_error )
845 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
847 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
852 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
855 if( P_picture->i_deccount != 1 )
857 vpar_SynchroEnd( p_vpar, 1 );
858 vout_DestroyPicture( p_vpar->p_vout, P_picture );
861 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
863 /* Prepare context for the next picture. */
865 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
866 p_vpar->picture.i_current_structure = 0;
868 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
870 /* Frame completely parsed. */
872 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
874 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
877 /* Send signal to the video_decoder. */
878 vlc_mutex_lock( &p_vpar->vfifo.lock );
879 vlc_cond_signal( &p_vpar->vfifo.wait );
880 vlc_mutex_unlock( &p_vpar->vfifo.lock );
883 /* Prepare context for the next picture. */
885 p_vpar->picture.i_current_structure = 0;
890 /*****************************************************************************
891 * ExtensionAndUserData : Parse the extension_and_user_data structure
892 *****************************************************************************/
893 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
895 while( !p_vpar->p_fifo->b_die )
897 NextStartCode( &p_vpar->bit_stream );
898 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
900 case EXTENSION_START_CODE:
901 RemoveBits32( &p_vpar->bit_stream );
902 switch( GetBits( &p_vpar->bit_stream, 4 ) )
904 case SEQUENCE_DISPLAY_EXTENSION_ID:
905 SequenceDisplayExtension( p_vpar );
907 case QUANT_MATRIX_EXTENSION_ID:
908 QuantMatrixExtension( p_vpar );
910 case SEQUENCE_SCALABLE_EXTENSION_ID:
911 SequenceScalableExtension( p_vpar );
913 case PICTURE_DISPLAY_EXTENSION_ID:
914 PictureDisplayExtension( p_vpar );
916 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
917 PictureSpatialScalableExtension( p_vpar );
919 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
920 PictureTemporalScalableExtension( p_vpar );
922 case COPYRIGHT_EXTENSION_ID:
923 CopyrightExtension( p_vpar );
930 case USER_DATA_START_CODE:
931 RemoveBits32( &p_vpar->bit_stream );
932 /* Wait for the next start code */
942 /*****************************************************************************
943 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
944 *****************************************************************************/
946 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
948 /* We don't care sequence_display_extension. */
950 RemoveBits( &p_vpar->bit_stream, 3 );
951 if( GetBits( &p_vpar->bit_stream, 1 ) )
953 /* Two bytes for color_desciption */
954 RemoveBits( &p_vpar->bit_stream, 16 );
955 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
957 /* display_horizontal and vertical_size and a marker_bit */
958 RemoveBits( &p_vpar->bit_stream, 29 );
962 /*****************************************************************************
963 * QuantMatrixExtension : Load quantization matrices for luminance *
965 *****************************************************************************/
967 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
969 if( GetBits( &p_vpar->bit_stream, 1 ) )
971 /* Load intra_quantiser_matrix for luminance. */
972 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
976 /* Use the default matrix. */
977 LinkMatrix( &p_vpar->sequence.intra_quant,
978 p_vpar->pi_default_intra_quant );
980 if( GetBits( &p_vpar->bit_stream, 1 ) )
982 /* Load non_intra_quantiser_matrix for luminance. */
983 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
987 /* Use the default matrix. */
988 LinkMatrix( &p_vpar->sequence.nonintra_quant,
989 p_vpar->pi_default_nonintra_quant );
991 if( GetBits( &p_vpar->bit_stream, 1 ) )
993 /* Load intra_quantiser_matrix for chrominance. */
994 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
998 /* Link the chrominance intra matrix to the luminance one. */
999 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1000 p_vpar->sequence.intra_quant.pi_matrix );
1002 if( GetBits( &p_vpar->bit_stream, 1 ) )
1004 /* Load non_intra_quantiser_matrix for chrominance. */
1005 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1009 /* Link the chrominance intra matrix to the luminance one. */
1010 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1011 p_vpar->sequence.intra_quant.pi_matrix );
1013 if( GetBits( &p_vpar->bit_stream, 1 ) )
1015 /* Load non_intra_quantiser_matrix for chrominance. */
1016 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1020 /* Link the chrominance nonintra matrix to the luminance one. */
1021 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1022 p_vpar->sequence.nonintra_quant.pi_matrix );
1027 /*****************************************************************************
1028 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1029 * structure to handle scalable coding *
1030 *****************************************************************************/
1032 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1034 /* We don't care about anything scalable except the scalable mode. */
1035 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1036 /* The length of the structure depends on the value of the scalable_mode */
1039 RemoveBits32( &p_vpar->bit_stream );
1040 RemoveBits( &p_vpar->bit_stream, 21 );
1043 RemoveBits( &p_vpar->bit_stream, 12 );
1046 RemoveBits( &p_vpar->bit_stream, 4 );
1050 /*****************************************************************************
1051 * PictureDisplayExtension : Parse the picture_display_extension structure *
1052 *****************************************************************************/
1054 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1056 /* Number of frame center offset */
1058 /* I am not sure it works but it should
1059 (fewer tests than shown in §6.3.12) */
1060 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1061 p_vpar->picture.b_repeat_first_field +
1062 p_vpar->picture.b_top_field_first
1063 : ( p_vpar->picture.b_frame_structure + 1 ) +
1064 p_vpar->picture.b_repeat_first_field;
1065 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1067 RemoveBits( &p_vpar->bit_stream, 17 );
1068 RemoveBits( &p_vpar->bit_stream, 17 );
1073 /*****************************************************************************
1074 * PictureSpatialScalableExtension *
1075 *****************************************************************************/
1077 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1079 /* That's scalable, so we trash it */
1080 RemoveBits32( &p_vpar->bit_stream );
1081 RemoveBits( &p_vpar->bit_stream, 16 );
1085 /*****************************************************************************
1086 * PictureTemporalScalableExtension *
1087 *****************************************************************************/
1089 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1091 /* Scalable again, trashed again */
1092 RemoveBits( &p_vpar->bit_stream, 23 );
1096 /*****************************************************************************
1097 * CopyrightExtension : Keeps some legal informations *
1098 *****************************************************************************/
1100 static void CopyrightExtension( vpar_thread_t * p_vpar )
1102 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1103 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1104 /* A flag that says whether the copyright information is significant */
1105 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1106 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1107 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1109 RemoveBits( &p_vpar->bit_stream, 8 );
1110 /* The copyright_number is split in three parts */
1112 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1113 RemoveBits( &p_vpar->bit_stream, 1 );
1115 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1116 RemoveBits( &p_vpar->bit_stream, 1 );
1117 /* third part and sum */
1118 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1119 ( (u64)i_copyright_nb_2 << 22 ) |
1120 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );