1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: vpar_headers.c,v 1.81 2001/04/06 09:15:48 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"
59 static __inline__ void NextStartCode( bit_stream_t * );
60 static void SequenceHeader( vpar_thread_t * p_vpar );
61 static void GroupHeader( vpar_thread_t * p_vpar );
62 static void PictureHeader( vpar_thread_t * p_vpar );
63 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
64 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
65 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
66 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
67 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
68 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
69 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
70 static void CopyrightExtension( vpar_thread_t * p_vpar );
76 /*****************************************************************************
77 * pi_default_intra_quant : default quantization matrix
78 *****************************************************************************/
79 u8 pi_default_intra_quant[] =
81 8, 16, 19, 22, 26, 27, 29, 34,
82 16, 16, 22, 24, 27, 29, 34, 37,
83 19, 22, 26, 27, 29, 34, 34, 38,
84 22, 22, 26, 27, 29, 34, 37, 40,
85 22, 26, 27, 29, 32, 35, 40, 48,
86 26, 27, 29, 32, 35, 40, 48, 58,
87 26, 27, 29, 34, 38, 46, 56, 69,
88 27, 29, 35, 38, 46, 56, 69, 83
91 /*****************************************************************************
92 * pi_default_nonintra_quant : default quantization matrix
93 *****************************************************************************/
94 u8 pi_default_nonintra_quant[] =
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,
103 16, 16, 16, 16, 16, 16, 16, 16
106 /*****************************************************************************
107 * pi_scan : zig-zag and alternate scan patterns
108 *****************************************************************************/
111 { /* Zig-Zag pattern */
112 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
113 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
114 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
115 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
117 { /* Alternate scan pattern */
118 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
119 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
120 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
121 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
126 * Local inline functions.
129 /*****************************************************************************
130 * ReferenceUpdate : Update the reference pointers when we have a new picture
131 *****************************************************************************/
132 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
134 picture_t * p_newref )
136 if( i_coding_type != B_CODING_TYPE )
138 if( p_vpar->sequence.p_forward != NULL )
140 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
142 if( p_vpar->sequence.p_backward != NULL )
144 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
145 vpar_SynchroDate( p_vpar ) );
147 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
148 p_vpar->sequence.p_backward = p_newref;
149 if( p_newref != NULL )
151 vout_LinkPicture( p_vpar->p_vout, p_newref );
154 else if( p_newref != NULL )
156 /* Put date immediately. */
157 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
161 /*****************************************************************************
162 * ReferenceReplace : Replace the last reference pointer when we destroy
164 *****************************************************************************/
165 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
167 picture_t * p_newref )
169 if( i_coding_type != B_CODING_TYPE )
171 if( p_vpar->sequence.p_backward != NULL )
173 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
175 p_vpar->sequence.p_backward = p_newref;
176 if( p_newref != NULL )
178 vout_LinkPicture( p_vpar->p_vout, p_newref );
183 /*****************************************************************************
184 * LoadMatrix : Load a quantization matrix
185 *****************************************************************************/
186 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar,
187 quant_matrix_t * p_matrix )
191 if( !p_matrix->b_allocated )
193 /* Allocate a piece of memory to load the matrix. */
194 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
196 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()" );
197 p_vpar->p_fifo->b_error = 1;
200 p_matrix->b_allocated = 1;
203 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
205 p_matrix->pi_matrix[p_vpar->ppi_scan[SCAN_ZIGZAG][i_dummy]]
206 = GetBits( &p_vpar->bit_stream, 8 );
210 /* Discrete Fourier Transform requires the quantization matrices to
211 * be normalized before using them. */
212 vdec_NormQuantMatrix( p_matrix->pi_matrix );
216 /*****************************************************************************
217 * LinkMatrix : Link a quantization matrix to another
218 *****************************************************************************/
219 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
221 if( p_matrix->b_allocated )
223 /* Deallocate the piece of memory. */
224 free( p_matrix->pi_matrix );
225 p_matrix->b_allocated = 0;
228 p_matrix->pi_matrix = pi_array;
232 * Exported functions.
235 /*****************************************************************************
236 * vpar_NextSequenceHeader : Find the next sequence header
237 *****************************************************************************/
238 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
240 while( !p_vpar->p_fifo->b_die )
242 NextStartCode( &p_vpar->bit_stream );
243 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
247 RemoveBits( &p_vpar->bit_stream, 8 );
252 /*****************************************************************************
253 * vpar_ParseHeader : Parse the next header
254 *****************************************************************************/
255 int vpar_ParseHeader( vpar_thread_t * p_vpar )
257 while( !p_vpar->p_fifo->b_die )
259 NextStartCode( &p_vpar->bit_stream );
260 switch( GetBits32( &p_vpar->bit_stream ) )
262 case SEQUENCE_HEADER_CODE:
264 p_vpar->c_sequences++;
266 SequenceHeader( p_vpar );
270 case GROUP_START_CODE:
271 GroupHeader( p_vpar );
275 case PICTURE_START_CODE:
276 PictureHeader( p_vpar );
280 case SEQUENCE_END_CODE:
281 intf_DbgMsg("vpar debug: sequence end code received");
294 * Following functions are local
297 /*****************************************************************************
298 * SequenceHeader : Parse the next sequence header
299 *****************************************************************************/
300 static void SequenceHeader( vpar_thread_t * p_vpar )
303 static int i_frame_rate_table[16] =
314 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
318 int i_height_save, i_width_save;
320 i_height_save = p_vpar->sequence.i_height;
321 i_width_save = p_vpar->sequence.i_width;
323 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
324 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
325 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
326 p_vpar->sequence.i_frame_rate =
327 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
329 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
330 * constrained_parameters_flag */
331 RemoveBits( &p_vpar->bit_stream, 30 );
334 * Quantization matrices
336 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
338 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
342 /* Use default matrix. */
343 LinkMatrix( &p_vpar->sequence.intra_quant,
344 p_vpar->pi_default_intra_quant );
347 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
349 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
353 /* Use default matrix. */
354 LinkMatrix( &p_vpar->sequence.nonintra_quant,
355 p_vpar->pi_default_nonintra_quant );
358 /* Unless later overwritten by a matrix extension, we have the same
359 * matrices for luminance and chrominance. */
360 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
361 p_vpar->sequence.intra_quant.pi_matrix );
362 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
363 p_vpar->sequence.nonintra_quant.pi_matrix );
368 NextStartCode( &p_vpar->bit_stream );
369 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
373 /* Turn the MPEG2 flag on */
374 p_vpar->sequence.b_mpeg2 = 1;
376 /* Parse sequence_extension */
377 RemoveBits32( &p_vpar->bit_stream );
378 /* extension_start_code_identifier, profile_and_level_indication */
379 RemoveBits( &p_vpar->bit_stream, 12 );
380 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
381 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
382 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
383 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
384 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
385 RemoveBits( &p_vpar->bit_stream, 22 );
386 /* frame_rate_extension_n */
387 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
388 /* frame_rate_extension_d */
389 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
390 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
394 /* It's an MPEG-1 stream. Put adequate parameters. */
396 p_vpar->sequence.b_mpeg2 = 0;
397 p_vpar->sequence.b_progressive = 1;
398 p_vpar->sequence.i_chroma_format = CHROMA_420;
402 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
403 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
404 (p_vpar->sequence.i_height + 15) / 16 :
405 2 * ((p_vpar->sequence.i_height + 31) / 32);
406 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
407 * p_vpar->sequence.i_mb_height;
408 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
409 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
410 p_vpar->sequence.i_size = p_vpar->sequence.i_width
411 * p_vpar->sequence.i_height;
413 /* Update chromatic information. */
414 switch( p_vpar->sequence.i_chroma_format )
417 p_vpar->sequence.i_chroma_nb_blocks = 2;
418 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
419 p_vpar->sequence.i_chroma_mb_width = 8;
420 p_vpar->sequence.i_chroma_mb_height = 8;
424 p_vpar->sequence.i_chroma_nb_blocks = 4;
425 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
426 p_vpar->sequence.i_chroma_mb_width = 8;
427 p_vpar->sequence.i_chroma_mb_height = 16;
431 p_vpar->sequence.i_chroma_nb_blocks = 8;
432 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
433 p_vpar->sequence.i_chroma_mb_width = 16;
434 p_vpar->sequence.i_chroma_mb_height = 16;
437 /* Reset scalable_mode. */
438 p_vpar->sequence.i_scalable_mode = SC_NONE;
441 if( p_vpar->sequence.i_width != i_width_save
442 || p_vpar->sequence.i_height != i_height_save )
444 /* FIXME: What do we do in case of a size change ?? */
448 /* Extension and User data */
449 ExtensionAndUserData( p_vpar );
452 /*****************************************************************************
453 * GroupHeader : Parse the next group of pictures header
454 *****************************************************************************/
455 static void GroupHeader( vpar_thread_t * p_vpar )
457 /* Nothing to do, we don't care. */
458 RemoveBits( &p_vpar->bit_stream, 27 );
459 ExtensionAndUserData( p_vpar );
462 /*****************************************************************************
463 * PictureHeader : Parse the next picture header
464 *****************************************************************************/
465 static void PictureHeader( vpar_thread_t * p_vpar )
469 boolean_t b_parsable;
474 /* Recover in case of stream discontinuity. */
475 if( p_vpar->sequence.b_expect_discontinuity )
477 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
478 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
479 if( p_vpar->picture.p_picture != NULL )
484 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
486 vpar_DestroyMacroblock( &p_vpar->vfifo,
487 p_vpar->picture.pp_mb[i_mb] );
490 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
492 p_vpar->sequence.b_expect_discontinuity = 0;
495 /* Parse the picture header. */
496 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
497 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
498 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
500 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
501 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
503 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
504 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
506 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
508 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
509 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
512 /* extra_information_picture */
513 while( GetBits( &p_vpar->bit_stream, 1 ) )
515 RemoveBits( &p_vpar->bit_stream, 8 );
519 * Picture Coding Extension
521 NextStartCode( &p_vpar->bit_stream );
522 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
524 /* Parse picture_coding_extension */
525 RemoveBits32( &p_vpar->bit_stream );
526 /* extension_start_code_identifier */
527 RemoveBits( &p_vpar->bit_stream, 4 );
529 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
530 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
531 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
532 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
533 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
534 i_structure = GetBits( &p_vpar->bit_stream, 2 );
535 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
536 p_vpar->picture.b_frame_pred_frame_dct
537 = GetBits( &p_vpar->bit_stream, 1 );
538 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
539 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
540 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
541 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
542 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
543 /* chroma_420_type (obsolete) */
544 RemoveBits( &p_vpar->bit_stream, 1 );
545 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
547 /* composite_display_flag */
548 if( GetBits( &p_vpar->bit_stream, 1 ) )
550 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
551 * sub_carrier_phase */
552 RemoveBits( &p_vpar->bit_stream, 20 );
557 /* MPEG-1 compatibility flags */
558 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
559 i_structure = FRAME_STRUCTURE;
560 p_vpar->picture.b_top_field_first = 0;
561 p_vpar->picture.b_frame_pred_frame_dct = 1;
562 p_vpar->picture.b_concealment_mv = 0;
563 p_vpar->picture.b_q_scale_type = 0;
564 p_vpar->picture.b_intra_vlc_format = 0;
565 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
566 p_vpar->picture.b_repeat_first_field = 0;
567 p_vpar->picture.b_progressive = 1;
571 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
574 if( p_vpar->picture.i_current_structure &&
575 (i_structure == FRAME_STRUCTURE ||
576 i_structure == p_vpar->picture.i_current_structure) )
578 /* We don't have the second field of the buffered frame. */
579 if( p_vpar->picture.p_picture != NULL )
581 ReferenceReplace( p_vpar,
582 p_vpar->picture.i_coding_type,
586 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
588 vpar_DestroyMacroblock( &p_vpar->vfifo,
589 p_vpar->picture.pp_mb[i_mb] );
592 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
595 p_vpar->picture.i_current_structure = 0;
597 intf_WarnMsg( 2, "Odd number of field pictures." );
600 /* Do we have the reference pictures ? */
601 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
602 (p_vpar->sequence.p_backward == NULL)) ||
603 /* p_backward will become p_forward later */
604 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
605 (p_vpar->sequence.p_forward == NULL ||
606 p_vpar->sequence.p_backward == NULL)));
608 if( p_vpar->picture.i_current_structure )
610 /* Second field of a frame. We will decode it if, and only if we
611 * have decoded the first field. */
614 b_parsable = (p_vpar->picture.p_picture != NULL);
621 /* Compute the number of times the frame will be emitted by the
622 * decoder (number of half-periods). */
623 if( p_vpar->sequence.b_progressive )
625 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
626 + p_vpar->picture.b_top_field_first) * 2;
630 if( p_vpar->picture.b_progressive )
632 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
640 /* Warn synchro we have a new picture (updates pictures index). */
641 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
646 /* Does synchro say we have enough time to decode it ? */
647 b_parsable = vpar_SynchroChoose( p_vpar,
648 p_vpar->picture.i_coding_type, i_structure );
654 /* Update the reference pointers. */
655 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
657 /* Update context. */
658 if( i_structure != FRAME_STRUCTURE )
660 if( (p_vpar->picture.i_current_structure | i_structure)
663 p_vpar->picture.i_current_structure = 0;
667 /* The frame is complete. */
668 p_vpar->picture.i_current_structure = i_structure;
670 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
675 /* Warn Synchro we have trashed a picture. */
676 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
678 p_vpar->picture.p_picture = NULL;
683 /* OK, now we are sure we will decode the picture. */
685 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
688 #define P_picture p_vpar->picture.p_picture
689 p_vpar->picture.b_error = 0;
690 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
692 if( !p_vpar->picture.i_current_structure )
694 /* This is a new frame. Get a structure from the video_output. */
695 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
696 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
697 p_vpar->sequence.i_width,
698 p_vpar->sequence.i_height ) )
701 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying");
702 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
706 msleep( VPAR_OUTMEM_SLEEP );
709 /* Initialize values. */
710 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
711 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
712 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
713 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
714 << ( 1 - p_vpar->picture.b_frame_structure ) );
715 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
716 << ( 1 - p_vpar->picture.b_frame_structure ));
718 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
720 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
722 /* FIXME ! remove asap ?? */
723 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
725 /* Update the reference pointers. */
726 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
729 /* Link referenced pictures for the decoder
730 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
731 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
732 p_vpar->picture.i_coding_type == B_CODING_TYPE )
734 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
736 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
738 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
742 p_vpar->picture.i_current_structure |= i_structure;
743 p_vpar->picture.i_structure = i_structure;
745 /* Initialize picture data for decoding. */
746 if( i_structure == BOTTOM_FIELD )
748 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
749 p_vpar->mb.i_l_y = 1;
750 p_vpar->mb.i_c_y = 1;
755 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
757 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
759 /* Extension and User data. */
760 ExtensionAndUserData( p_vpar );
762 /* This is an MP@ML decoder, please note that neither of the following
763 * assertions can be true :
764 * p_vpar->sequence.i_chroma_format != CHROMA_420
765 * p_vpar->sequence.i_height > 2800
766 * p_vpar->sequence.i_scalable_mode == SC_DP
767 * Be cautious if you try to use the decoder for other profiles and
770 if( p_vpar->sequence.b_mpeg2 )
772 static f_picture_data_t ppf_picture_data[4][4] =
775 NULL, NULL, NULL, NULL
779 #if (VPAR_OPTIM_LEVEL > 1)
780 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
783 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
784 vpar_PictureDataGENERIC
789 #if (VPAR_OPTIM_LEVEL > 1)
790 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
793 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
794 vpar_PictureDataGENERIC
799 #if (VPAR_OPTIM_LEVEL > 0)
800 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
803 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
804 vpar_PictureDataGENERIC
809 ppf_picture_data[p_vpar->picture.i_structure]
810 [p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
814 #if (VPAR_OPTIM_LEVEL > 0)
815 static f_picture_data_t pf_picture_data[5] =
816 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
817 vpar_PictureData1D };
819 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar, i_mb_base );
821 vpar_PictureDataGENERIC( p_vpar, i_mb_base );
825 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
830 if( p_vpar->picture.b_error )
834 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
836 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
841 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
844 if( P_picture->i_deccount != 1 )
846 vpar_SynchroEnd( p_vpar, 1 );
847 vout_DestroyPicture( p_vpar->p_vout, P_picture );
850 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
852 /* Prepare context for the next picture. */
854 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
855 p_vpar->picture.i_current_structure = 0;
857 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
859 /* Frame completely parsed. */
861 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
863 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
866 /* Send signal to the video_decoder. */
867 vlc_mutex_lock( &p_vpar->vfifo.lock );
868 vlc_cond_signal( &p_vpar->vfifo.wait );
869 vlc_mutex_unlock( &p_vpar->vfifo.lock );
872 /* Prepare context for the next picture. */
874 p_vpar->picture.i_current_structure = 0;
879 /*****************************************************************************
880 * ExtensionAndUserData : Parse the extension_and_user_data structure
881 *****************************************************************************/
882 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
884 while( !p_vpar->p_fifo->b_die )
886 NextStartCode( &p_vpar->bit_stream );
887 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
889 case EXTENSION_START_CODE:
890 RemoveBits32( &p_vpar->bit_stream );
891 switch( GetBits( &p_vpar->bit_stream, 4 ) )
893 case SEQUENCE_DISPLAY_EXTENSION_ID:
894 SequenceDisplayExtension( p_vpar );
896 case QUANT_MATRIX_EXTENSION_ID:
897 QuantMatrixExtension( p_vpar );
899 case SEQUENCE_SCALABLE_EXTENSION_ID:
900 SequenceScalableExtension( p_vpar );
902 case PICTURE_DISPLAY_EXTENSION_ID:
903 PictureDisplayExtension( p_vpar );
905 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
906 PictureSpatialScalableExtension( p_vpar );
908 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
909 PictureTemporalScalableExtension( p_vpar );
911 case COPYRIGHT_EXTENSION_ID:
912 CopyrightExtension( p_vpar );
919 case USER_DATA_START_CODE:
920 RemoveBits32( &p_vpar->bit_stream );
921 /* Wait for the next start code */
931 /*****************************************************************************
932 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
933 *****************************************************************************/
935 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
937 /* We don't care sequence_display_extension. */
939 RemoveBits( &p_vpar->bit_stream, 3 );
940 if( GetBits( &p_vpar->bit_stream, 1 ) )
942 /* Two bytes for color_desciption */
943 RemoveBits( &p_vpar->bit_stream, 16 );
944 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
946 /* display_horizontal and vertical_size and a marker_bit */
947 RemoveBits( &p_vpar->bit_stream, 29 );
951 /*****************************************************************************
952 * QuantMatrixExtension : Load quantization matrices for luminance *
954 *****************************************************************************/
956 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
958 if( GetBits( &p_vpar->bit_stream, 1 ) )
960 /* Load intra_quantiser_matrix for luminance. */
961 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
965 /* Use the default matrix. */
966 LinkMatrix( &p_vpar->sequence.intra_quant,
967 p_vpar->pi_default_intra_quant );
969 if( GetBits( &p_vpar->bit_stream, 1 ) )
971 /* Load non_intra_quantiser_matrix for luminance. */
972 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
976 /* Use the default matrix. */
977 LinkMatrix( &p_vpar->sequence.nonintra_quant,
978 p_vpar->pi_default_nonintra_quant );
980 if( GetBits( &p_vpar->bit_stream, 1 ) )
982 /* Load intra_quantiser_matrix for chrominance. */
983 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
987 /* Link the chrominance intra matrix to the luminance one. */
988 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
989 p_vpar->sequence.intra_quant.pi_matrix );
991 if( GetBits( &p_vpar->bit_stream, 1 ) )
993 /* Load non_intra_quantiser_matrix for chrominance. */
994 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_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 nonintra matrix to the luminance one. */
1010 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1011 p_vpar->sequence.nonintra_quant.pi_matrix );
1016 /*****************************************************************************
1017 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1018 * structure to handle scalable coding *
1019 *****************************************************************************/
1021 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1023 /* We don't care about anything scalable except the scalable mode. */
1024 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1025 /* The length of the structure depends on the value of the scalable_mode */
1028 RemoveBits32( &p_vpar->bit_stream );
1029 RemoveBits( &p_vpar->bit_stream, 21 );
1032 RemoveBits( &p_vpar->bit_stream, 12 );
1035 RemoveBits( &p_vpar->bit_stream, 4 );
1039 /*****************************************************************************
1040 * PictureDisplayExtension : Parse the picture_display_extension structure *
1041 *****************************************************************************/
1043 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1045 /* Number of frame center offset */
1047 /* I am not sure it works but it should
1048 (fewer tests than shown in §6.3.12) */
1049 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1050 p_vpar->picture.b_repeat_first_field +
1051 p_vpar->picture.b_top_field_first
1052 : ( p_vpar->picture.b_frame_structure + 1 ) +
1053 p_vpar->picture.b_repeat_first_field;
1054 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1056 RemoveBits( &p_vpar->bit_stream, 17 );
1057 RemoveBits( &p_vpar->bit_stream, 17 );
1062 /*****************************************************************************
1063 * PictureSpatialScalableExtension *
1064 *****************************************************************************/
1066 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1068 /* That's scalable, so we trash it */
1069 RemoveBits32( &p_vpar->bit_stream );
1070 RemoveBits( &p_vpar->bit_stream, 16 );
1074 /*****************************************************************************
1075 * PictureTemporalScalableExtension *
1076 *****************************************************************************/
1078 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1080 /* Scalable again, trashed again */
1081 RemoveBits( &p_vpar->bit_stream, 23 );
1085 /*****************************************************************************
1086 * CopyrightExtension : Keeps some legal informations *
1087 *****************************************************************************/
1089 static void CopyrightExtension( vpar_thread_t * p_vpar )
1091 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1092 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1093 /* A flag that says whether the copyright information is significant */
1094 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1095 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1096 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1098 RemoveBits( &p_vpar->bit_stream, 8 );
1099 /* The copyright_number is split in three parts */
1101 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1102 RemoveBits( &p_vpar->bit_stream, 1 );
1104 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1105 RemoveBits( &p_vpar->bit_stream, 1 );
1106 /* third part and sum */
1107 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1108 ( (u64)i_copyright_nb_2 << 22 ) |
1109 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );