1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.80 2001/03/02 13:20:29 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() */
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
43 #include "video_output.h"
45 #include "video_decoder.h"
46 #include "vdec_motion.h"
47 #include "../video_decoder/vdec_idct.h"
49 #include "vpar_blocks.h"
50 #include "../video_decoder/vpar_headers.h"
51 #include "../video_decoder/vpar_synchro.h"
52 #include "../video_decoder/video_parser.h"
53 #include "../video_decoder/video_fifo.h"
58 static __inline__ void NextStartCode( bit_stream_t * );
59 static void SequenceHeader( vpar_thread_t * p_vpar );
60 static void GroupHeader( vpar_thread_t * p_vpar );
61 static void PictureHeader( vpar_thread_t * p_vpar );
62 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
63 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
64 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
65 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
66 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
67 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
68 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
69 static void CopyrightExtension( vpar_thread_t * p_vpar );
75 /*****************************************************************************
76 * pi_default_intra_quant : default quantization matrix
77 *****************************************************************************/
78 u8 pi_default_intra_quant[] =
80 8, 16, 19, 22, 26, 27, 29, 34,
81 16, 16, 22, 24, 27, 29, 34, 37,
82 19, 22, 26, 27, 29, 34, 34, 38,
83 22, 22, 26, 27, 29, 34, 37, 40,
84 22, 26, 27, 29, 32, 35, 40, 48,
85 26, 27, 29, 32, 35, 40, 48, 58,
86 26, 27, 29, 34, 38, 46, 56, 69,
87 27, 29, 35, 38, 46, 56, 69, 83
90 /*****************************************************************************
91 * pi_default_nonintra_quant : default quantization matrix
92 *****************************************************************************/
93 u8 pi_default_nonintra_quant[] =
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,
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
105 /*****************************************************************************
106 * pi_scan : zig-zag and alternate scan patterns
107 *****************************************************************************/
110 { /* Zig-Zag pattern */
111 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
112 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
113 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
114 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
116 { /* Alternate scan pattern */
117 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
118 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
119 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
120 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
125 * Local inline functions.
128 /*****************************************************************************
129 * ReferenceUpdate : Update the reference pointers when we have a new picture
130 *****************************************************************************/
131 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
133 picture_t * p_newref )
135 if( i_coding_type != B_CODING_TYPE )
137 if( p_vpar->sequence.p_forward != NULL )
139 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
141 if( p_vpar->sequence.p_backward != NULL )
143 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
144 vpar_SynchroDate( p_vpar ) );
146 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
147 p_vpar->sequence.p_backward = p_newref;
148 if( p_newref != NULL )
150 vout_LinkPicture( p_vpar->p_vout, p_newref );
153 else if( p_newref != NULL )
155 /* Put date immediately. */
156 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
160 /*****************************************************************************
161 * ReferenceReplace : Replace the last reference pointer when we destroy
163 *****************************************************************************/
164 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
166 picture_t * p_newref )
168 if( i_coding_type != B_CODING_TYPE )
170 if( p_vpar->sequence.p_backward != NULL )
172 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
174 p_vpar->sequence.p_backward = p_newref;
175 if( p_newref != NULL )
177 vout_LinkPicture( p_vpar->p_vout, p_newref );
182 /*****************************************************************************
183 * LoadMatrix : Load a quantization matrix
184 *****************************************************************************/
185 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
186 quant_matrix_t * p_matrix )
190 if( !p_matrix->b_allocated )
192 /* Allocate a piece of memory to load the matrix. */
193 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
195 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
196 p_vpar->p_fifo->b_error = 1;
199 p_matrix->b_allocated = 1;
202 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
204 p_matrix->pi_matrix[p_vpar->ppi_scan[SCAN_ZIGZAG][i_dummy]]
205 = GetBits( &p_vpar->bit_stream, 8 );
209 /* Discrete Fourier Transform requires the quantization matrices to
210 * be normalized before using them. */
211 vdec_NormQuantMatrix( p_matrix->pi_matrix );
215 /*****************************************************************************
216 * LinkMatrix : Link a quantization matrix to another
217 *****************************************************************************/
218 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
220 if( p_matrix->b_allocated )
222 /* Deallocate the piece of memory. */
223 free( p_matrix->pi_matrix );
224 p_matrix->b_allocated = 0;
227 p_matrix->pi_matrix = pi_array;
231 * Exported functions.
234 /*****************************************************************************
235 * vpar_NextSequenceHeader : Find the next sequence header
236 *****************************************************************************/
237 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
239 while( !p_vpar->p_fifo->b_die )
241 NextStartCode( &p_vpar->bit_stream );
242 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
246 RemoveBits( &p_vpar->bit_stream, 8 );
251 /*****************************************************************************
252 * vpar_ParseHeader : Parse the next header
253 *****************************************************************************/
254 int vpar_ParseHeader( vpar_thread_t * p_vpar )
256 while( !p_vpar->p_fifo->b_die )
258 NextStartCode( &p_vpar->bit_stream );
259 switch( GetBits32( &p_vpar->bit_stream ) )
261 case SEQUENCE_HEADER_CODE:
263 p_vpar->c_sequences++;
265 SequenceHeader( p_vpar );
269 case GROUP_START_CODE:
270 GroupHeader( p_vpar );
274 case PICTURE_START_CODE:
275 PictureHeader( p_vpar );
279 case SEQUENCE_END_CODE:
280 intf_DbgMsg("vpar debug: sequence end code received");
292 * Following functions are local
295 /*****************************************************************************
296 * SequenceHeader : Parse the next sequence header
297 *****************************************************************************/
298 static void SequenceHeader( vpar_thread_t * p_vpar )
301 static int i_frame_rate_table[16] =
312 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
316 int i_height_save, i_width_save;
318 i_height_save = p_vpar->sequence.i_height;
319 i_width_save = p_vpar->sequence.i_width;
321 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
322 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
323 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
324 p_vpar->sequence.i_frame_rate =
325 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
327 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
328 * constrained_parameters_flag */
329 RemoveBits( &p_vpar->bit_stream, 30 );
332 * Quantization matrices
334 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
336 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
340 /* Use default matrix. */
341 LinkMatrix( &p_vpar->sequence.intra_quant,
342 p_vpar->pi_default_intra_quant );
345 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
347 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
351 /* Use default matrix. */
352 LinkMatrix( &p_vpar->sequence.nonintra_quant,
353 p_vpar->pi_default_nonintra_quant );
356 /* Unless later overwritten by a matrix extension, we have the same
357 * matrices for luminance and chrominance. */
358 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
359 p_vpar->sequence.intra_quant.pi_matrix );
360 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
361 p_vpar->sequence.nonintra_quant.pi_matrix );
366 NextStartCode( &p_vpar->bit_stream );
367 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
371 /* Turn the MPEG2 flag on */
372 p_vpar->sequence.b_mpeg2 = 1;
374 /* Parse sequence_extension */
375 RemoveBits32( &p_vpar->bit_stream );
376 /* extension_start_code_identifier, profile_and_level_indication */
377 RemoveBits( &p_vpar->bit_stream, 12 );
378 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
379 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
380 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
381 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
382 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
383 RemoveBits( &p_vpar->bit_stream, 22 );
384 /* frame_rate_extension_n */
385 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
386 /* frame_rate_extension_d */
387 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
388 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
392 /* It's an MPEG-1 stream. Put adequate parameters. */
394 p_vpar->sequence.b_mpeg2 = 0;
395 p_vpar->sequence.b_progressive = 1;
396 p_vpar->sequence.i_chroma_format = CHROMA_420;
400 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
401 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
402 (p_vpar->sequence.i_height + 15) / 16 :
403 2 * ((p_vpar->sequence.i_height + 31) / 32);
404 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
405 * p_vpar->sequence.i_mb_height;
406 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
407 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
408 p_vpar->sequence.i_size = p_vpar->sequence.i_width
409 * p_vpar->sequence.i_height;
411 /* Update chromatic information. */
412 switch( p_vpar->sequence.i_chroma_format )
415 p_vpar->sequence.i_chroma_nb_blocks = 2;
416 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
417 p_vpar->sequence.i_chroma_mb_width = 8;
418 p_vpar->sequence.i_chroma_mb_height = 8;
422 p_vpar->sequence.i_chroma_nb_blocks = 4;
423 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
424 p_vpar->sequence.i_chroma_mb_width = 8;
425 p_vpar->sequence.i_chroma_mb_height = 16;
429 p_vpar->sequence.i_chroma_nb_blocks = 8;
430 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
431 p_vpar->sequence.i_chroma_mb_width = 16;
432 p_vpar->sequence.i_chroma_mb_height = 16;
435 /* Reset scalable_mode. */
436 p_vpar->sequence.i_scalable_mode = SC_NONE;
439 if( p_vpar->sequence.i_width != i_width_save
440 || p_vpar->sequence.i_height != i_height_save )
442 /* FIXME: What do we do in case of a size change ?? */
446 /* Extension and User data */
447 ExtensionAndUserData( p_vpar );
450 /*****************************************************************************
451 * GroupHeader : Parse the next group of pictures header
452 *****************************************************************************/
453 static void GroupHeader( vpar_thread_t * p_vpar )
455 /* Nothing to do, we don't care. */
456 RemoveBits( &p_vpar->bit_stream, 27 );
457 ExtensionAndUserData( p_vpar );
460 /*****************************************************************************
461 * PictureHeader : Parse the next picture header
462 *****************************************************************************/
463 static void PictureHeader( vpar_thread_t * p_vpar )
467 boolean_t b_parsable;
472 /* Recover in case of stream discontinuity. */
473 if( p_vpar->sequence.b_expect_discontinuity )
475 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
476 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
477 if( p_vpar->picture.p_picture != NULL )
482 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
484 vpar_DestroyMacroblock( &p_vpar->vfifo,
485 p_vpar->picture.pp_mb[i_mb] );
488 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
490 p_vpar->sequence.b_expect_discontinuity = 0;
493 /* Parse the picture header. */
494 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
495 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
496 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
498 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
499 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
501 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
502 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
504 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
506 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
507 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
510 /* extra_information_picture */
511 while( GetBits( &p_vpar->bit_stream, 1 ) )
513 RemoveBits( &p_vpar->bit_stream, 8 );
517 * Picture Coding Extension
519 NextStartCode( &p_vpar->bit_stream );
520 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
522 /* Parse picture_coding_extension */
523 RemoveBits32( &p_vpar->bit_stream );
524 /* extension_start_code_identifier */
525 RemoveBits( &p_vpar->bit_stream, 4 );
527 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
528 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
529 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
530 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
531 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
532 i_structure = GetBits( &p_vpar->bit_stream, 2 );
533 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
534 p_vpar->picture.b_frame_pred_frame_dct
535 = GetBits( &p_vpar->bit_stream, 1 );
536 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
537 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
538 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
539 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
540 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
541 /* chroma_420_type (obsolete) */
542 RemoveBits( &p_vpar->bit_stream, 1 );
543 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
545 /* composite_display_flag */
546 if( GetBits( &p_vpar->bit_stream, 1 ) )
548 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
549 * sub_carrier_phase */
550 RemoveBits( &p_vpar->bit_stream, 20 );
555 /* MPEG-1 compatibility flags */
556 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
557 i_structure = FRAME_STRUCTURE;
558 p_vpar->picture.b_top_field_first = 0;
559 p_vpar->picture.b_frame_pred_frame_dct = 1;
560 p_vpar->picture.b_concealment_mv = 0;
561 p_vpar->picture.b_q_scale_type = 0;
562 p_vpar->picture.b_intra_vlc_format = 0;
563 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
564 p_vpar->picture.b_repeat_first_field = 0;
565 p_vpar->picture.b_progressive = 1;
569 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
572 if( p_vpar->picture.i_current_structure &&
573 (i_structure == FRAME_STRUCTURE ||
574 i_structure == p_vpar->picture.i_current_structure) )
576 /* We don't have the second field of the buffered frame. */
577 if( p_vpar->picture.p_picture != NULL )
579 ReferenceReplace( p_vpar,
580 p_vpar->picture.i_coding_type,
584 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
586 vpar_DestroyMacroblock( &p_vpar->vfifo,
587 p_vpar->picture.pp_mb[i_mb] );
590 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
593 p_vpar->picture.i_current_structure = 0;
595 intf_WarnMsg( 2, "Odd number of field pictures." );
598 /* Do we have the reference pictures ? */
599 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
600 (p_vpar->sequence.p_backward == NULL)) ||
601 /* p_backward will become p_forward later */
602 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
603 (p_vpar->sequence.p_forward == NULL ||
604 p_vpar->sequence.p_backward == NULL)));
606 if( p_vpar->picture.i_current_structure )
608 /* Second field of a frame. We will decode it if, and only if we
609 * have decoded the first field. */
612 b_parsable = (p_vpar->picture.p_picture != NULL);
619 /* Compute the number of times the frame will be emitted by the
620 * decoder (number of half-periods). */
621 if( p_vpar->sequence.b_progressive )
623 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
624 + p_vpar->picture.b_top_field_first) * 2;
628 if( p_vpar->picture.b_progressive )
630 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
638 /* Warn synchro we have a new picture (updates pictures index). */
639 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
644 /* Does synchro say we have enough time to decode it ? */
645 b_parsable = vpar_SynchroChoose( p_vpar,
646 p_vpar->picture.i_coding_type, i_structure );
652 /* Update the reference pointers. */
653 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
655 /* Update context. */
656 if( i_structure != FRAME_STRUCTURE )
658 if( (p_vpar->picture.i_current_structure | i_structure)
661 p_vpar->picture.i_current_structure = 0;
665 /* The frame is complete. */
666 p_vpar->picture.i_current_structure = i_structure;
668 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
673 /* Warn Synchro we have trashed a picture. */
674 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
676 p_vpar->picture.p_picture = NULL;
681 /* OK, now we are sure we will decode the picture. */
683 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
686 #define P_picture p_vpar->picture.p_picture
687 p_vpar->picture.b_error = 0;
688 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
690 if( !p_vpar->picture.i_current_structure )
692 /* This is a new frame. Get a structure from the video_output. */
693 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
694 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
695 p_vpar->sequence.i_width,
696 p_vpar->sequence.i_height ) )
699 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
700 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
704 msleep( VPAR_OUTMEM_SLEEP );
707 /* Initialize values. */
708 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
709 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
710 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
711 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
712 << ( 1 - p_vpar->picture.b_frame_structure ) );
713 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
714 << ( 1 - p_vpar->picture.b_frame_structure ));
716 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
718 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
720 /* FIXME ! remove asap ?? */
721 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
723 /* Update the reference pointers. */
724 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
727 /* Link referenced pictures for the decoder
728 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
729 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
730 p_vpar->picture.i_coding_type == B_CODING_TYPE )
732 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
734 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
736 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
740 p_vpar->picture.i_current_structure |= i_structure;
741 p_vpar->picture.i_structure = i_structure;
743 /* Initialize picture data for decoding. */
744 if( i_structure == BOTTOM_FIELD )
746 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
747 p_vpar->mb.i_l_y = 1;
748 p_vpar->mb.i_c_y = 1;
753 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
755 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
757 /* Extension and User data. */
758 ExtensionAndUserData( p_vpar );
760 /* This is an MP@ML decoder, please note that neither of the following
761 * assertions can be true :
762 * p_vpar->sequence.i_chroma_format != CHROMA_420
763 * p_vpar->sequence.i_height > 2800
764 * p_vpar->sequence.i_scalable_mode == SC_DP
765 * Be cautious if you try to use the decoder for other profiles and
768 if( p_vpar->sequence.b_mpeg2 )
770 static f_picture_data_t ppf_picture_data[4][4] =
773 NULL, NULL, NULL, NULL
777 #if (VPAR_OPTIM_LEVEL > 1)
778 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
781 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
782 vpar_PictureDataGENERIC
787 #if (VPAR_OPTIM_LEVEL > 1)
788 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
791 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
792 vpar_PictureDataGENERIC
797 #if (VPAR_OPTIM_LEVEL > 0)
798 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
801 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
802 vpar_PictureDataGENERIC
807 ppf_picture_data[p_vpar->picture.i_structure]
808 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
812 #if (VPAR_OPTIM_LEVEL > 0)
813 static f_picture_data_t pf_picture_data[5] =
814 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
815 vpar_PictureData1D };
817 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
819 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
823 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
828 if( p_vpar->picture.b_error )
832 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
834 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
839 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
842 if( P_picture->i_deccount != 1 )
844 vpar_SynchroEnd( p_vpar, 1 );
845 vout_DestroyPicture( p_vpar->p_vout, P_picture );
848 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
850 /* Prepare context for the next picture. */
852 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
853 p_vpar->picture.i_current_structure = 0;
855 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
857 /* Frame completely parsed. */
859 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
861 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
864 /* Send signal to the video_decoder. */
865 vlc_mutex_lock( &p_vpar->vfifo.lock );
866 vlc_cond_signal( &p_vpar->vfifo.wait );
867 vlc_mutex_unlock( &p_vpar->vfifo.lock );
870 /* Prepare context for the next picture. */
872 p_vpar->picture.i_current_structure = 0;
877 /*****************************************************************************
878 * ExtensionAndUserData : Parse the extension_and_user_data structure
879 *****************************************************************************/
880 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
882 while( !p_vpar->p_fifo->b_die )
884 NextStartCode( &p_vpar->bit_stream );
885 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
887 case EXTENSION_START_CODE:
888 RemoveBits32( &p_vpar->bit_stream );
889 switch( GetBits( &p_vpar->bit_stream, 4 ) )
891 case SEQUENCE_DISPLAY_EXTENSION_ID:
892 SequenceDisplayExtension( p_vpar );
894 case QUANT_MATRIX_EXTENSION_ID:
895 QuantMatrixExtension( p_vpar );
897 case SEQUENCE_SCALABLE_EXTENSION_ID:
898 SequenceScalableExtension( p_vpar );
900 case PICTURE_DISPLAY_EXTENSION_ID:
901 PictureDisplayExtension( p_vpar );
903 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
904 PictureSpatialScalableExtension( p_vpar );
906 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
907 PictureTemporalScalableExtension( p_vpar );
909 case COPYRIGHT_EXTENSION_ID:
910 CopyrightExtension( p_vpar );
916 case USER_DATA_START_CODE:
917 RemoveBits32( &p_vpar->bit_stream );
918 /* Wait for the next start code */
928 /*****************************************************************************
929 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
930 *****************************************************************************/
932 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
934 /* We don't care sequence_display_extension. */
936 RemoveBits( &p_vpar->bit_stream, 3 );
937 if( GetBits( &p_vpar->bit_stream, 1 ) )
939 /* Two bytes for color_desciption */
940 RemoveBits( &p_vpar->bit_stream, 16 );
941 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
943 /* display_horizontal and vertical_size and a marker_bit */
944 RemoveBits( &p_vpar->bit_stream, 29 );
948 /*****************************************************************************
949 * QuantMatrixExtension : Load quantization matrices for luminance *
951 *****************************************************************************/
953 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
955 if( GetBits( &p_vpar->bit_stream, 1 ) )
957 /* Load intra_quantiser_matrix for luminance. */
958 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
962 /* Use the default matrix. */
963 LinkMatrix( &p_vpar->sequence.intra_quant,
964 p_vpar->pi_default_intra_quant );
966 if( GetBits( &p_vpar->bit_stream, 1 ) )
968 /* Load non_intra_quantiser_matrix for luminance. */
969 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
973 /* Use the default matrix. */
974 LinkMatrix( &p_vpar->sequence.nonintra_quant,
975 p_vpar->pi_default_nonintra_quant );
977 if( GetBits( &p_vpar->bit_stream, 1 ) )
979 /* Load intra_quantiser_matrix for chrominance. */
980 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
984 /* Link the chrominance intra matrix to the luminance one. */
985 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
986 p_vpar->sequence.intra_quant.pi_matrix );
988 if( GetBits( &p_vpar->bit_stream, 1 ) )
990 /* Load non_intra_quantiser_matrix for chrominance. */
991 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
995 /* Link the chrominance intra matrix to the luminance one. */
996 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
997 p_vpar->sequence.intra_quant.pi_matrix );
999 if( GetBits( &p_vpar->bit_stream, 1 ) )
1001 /* Load non_intra_quantiser_matrix for chrominance. */
1002 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1006 /* Link the chrominance nonintra matrix to the luminance one. */
1007 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1008 p_vpar->sequence.nonintra_quant.pi_matrix );
1013 /*****************************************************************************
1014 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1015 * structure to handle scalable coding *
1016 *****************************************************************************/
1018 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1020 /* We don't care about anything scalable except the scalable mode. */
1021 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1022 /* The length of the structure depends on the value of the scalable_mode */
1025 RemoveBits32( &p_vpar->bit_stream );
1026 RemoveBits( &p_vpar->bit_stream, 21 );
1029 RemoveBits( &p_vpar->bit_stream, 12 );
1032 RemoveBits( &p_vpar->bit_stream, 4 );
1036 /*****************************************************************************
1037 * PictureDisplayExtension : Parse the picture_display_extension structure *
1038 *****************************************************************************/
1040 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1042 /* Number of frame center offset */
1044 /* I am not sure it works but it should
1045 (fewer tests than shown in §6.3.12) */
1046 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1047 p_vpar->picture.b_repeat_first_field +
1048 p_vpar->picture.b_top_field_first
1049 : ( p_vpar->picture.b_frame_structure + 1 ) +
1050 p_vpar->picture.b_repeat_first_field;
1051 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1053 RemoveBits( &p_vpar->bit_stream, 17 );
1054 RemoveBits( &p_vpar->bit_stream, 17 );
1059 /*****************************************************************************
1060 * PictureSpatialScalableExtension *
1061 *****************************************************************************/
1063 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1065 /* That's scalable, so we trash it */
1066 RemoveBits32( &p_vpar->bit_stream );
1067 RemoveBits( &p_vpar->bit_stream, 16 );
1071 /*****************************************************************************
1072 * PictureTemporalScalableExtension *
1073 *****************************************************************************/
1075 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1077 /* Scalable again, trashed again */
1078 RemoveBits( &p_vpar->bit_stream, 23 );
1082 /*****************************************************************************
1083 * CopyrightExtension : Keeps some legal informations *
1084 *****************************************************************************/
1086 static void CopyrightExtension( vpar_thread_t * p_vpar )
1088 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1089 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1090 /* A flag that says whether the copyright information is significant */
1091 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1092 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1093 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1095 RemoveBits( &p_vpar->bit_stream, 8 );
1096 /* The copyright_number is split in three parts */
1098 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1099 RemoveBits( &p_vpar->bit_stream, 1 );
1101 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1102 RemoveBits( &p_vpar->bit_stream, 1 );
1103 /* third part and sum */
1104 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1105 ( (u64)i_copyright_nb_2 << 22 ) |
1106 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );