1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Stéphane Borel <stef@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <stdlib.h> /* free() */
30 #include <sys/types.h> /* on BSD, uio.h needs types.h */
31 #include <sys/uio.h> /* "input.h" */
42 #include "decoder_fifo.h"
44 #include "video_output.h"
46 #include "vdec_idct.h"
47 #include "video_decoder.h"
48 #include "vdec_motion.h"
50 #include "vpar_blocks.h"
51 #include "vpar_headers.h"
52 #include "vpar_synchro.h"
53 #include "video_parser.h"
54 #include "video_fifo.h"
59 static __inline__ void NextStartCode( vpar_thread_t * p_vpar );
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 *****************************************************************************/
80 int pi_default_intra_quant[] =
82 8, 16, 19, 22, 26, 27, 29, 34,
83 16, 16, 22, 24, 27, 29, 34, 37,
84 19, 22, 26, 27, 29, 34, 34, 38,
85 22, 22, 26, 27, 29, 34, 37, 40,
86 22, 26, 27, 29, 32, 35, 40, 48,
87 26, 27, 29, 32, 35, 40, 48, 58,
88 26, 27, 29, 34, 38, 46, 56, 69,
89 27, 29, 35, 38, 46, 56, 69, 83
92 int pi_default_intra_quant[] =
94 2048, 5681, 6355, 6623, 6656, 5431, 4018, 2401,
95 5681, 7880, 10207, 10021, 9587, 8091, 6534, 3625,
96 6355, 10207, 11363, 10619, 9700, 8935, 6155, 3507,
97 6623, 9186, 10226, 9557, 8730, 8041, 6028, 3322,
98 5632, 9232, 9031, 8730, 8192, 7040, 5542, 3390,
99 5230, 7533, 7621, 7568, 7040, 6321, 5225, 3219,
100 3602, 5189, 5250, 5539, 5265, 5007, 4199, 2638,
101 1907, 2841, 3230, 3156, 3249, 3108, 2638, 1617
105 /*****************************************************************************
106 * pi_default_nonintra_quant : default quantization matrix
107 *****************************************************************************/
109 int pi_default_nonintra_quant[] =
111 16, 16, 16, 16, 16, 16, 16, 16,
112 16, 16, 16, 16, 16, 16, 16, 16,
113 16, 16, 16, 16, 16, 16, 16, 16,
114 16, 16, 16, 16, 16, 16, 16, 16,
115 16, 16, 16, 16, 16, 16, 16, 16,
116 16, 16, 16, 16, 16, 16, 16, 16,
117 16, 16, 16, 16, 16, 16, 16, 16,
118 16, 16, 16, 16, 16, 16, 16, 16
121 int pi_default_nonintra_quanit[] =
123 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
124 5680, 7888, 7424, 6688, 5680, 4464, 3072, 1568,
125 5344, 7424, 6992, 6288, 5344, 4208, 2896, 1472,
126 4816, 6688, 6288, 5664, 4816, 3792, 2608, 1328,
127 4096, 5680, 5344, 4816, 4096, 3216, 2224, 1136,
128 3216, 4464, 4208, 3792, 3216, 2528, 1744, 880,
129 2224, 3072, 2896, 2608, 2224, 1744, 1200, 608,
130 1136, 1568, 1472, 1328, 1136, 880, 608, 304
134 /*****************************************************************************
135 * pi_scan : zig-zag and alternate scan patterns
136 *****************************************************************************/
139 { /* Zig-Zag pattern */
140 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
141 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
142 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
143 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
145 { /* Alternate scan pattern */
146 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
147 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
148 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
149 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
154 * Local inline functions.
157 /*****************************************************************************
158 * ReferenceUpdate : Update the reference pointers when we have a new picture
159 *****************************************************************************/
160 static void __inline__ ReferenceUpdate( vpar_thread_t * p_vpar,
162 picture_t * p_newref )
164 if( i_coding_type != B_CODING_TYPE )
166 if( p_vpar->sequence.p_forward != NULL )
168 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
170 if( p_vpar->sequence.p_backward != NULL )
172 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
173 vpar_SynchroDate( p_vpar ) );
175 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
176 p_vpar->sequence.p_backward = p_newref;
177 if( p_newref != NULL )
179 vout_LinkPicture( p_vpar->p_vout, p_newref );
182 else if( p_newref != NULL )
184 /* Put date immediately. */
185 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
189 /*****************************************************************************
190 * ReferenceReplace : Replace the last reference pointer when we destroy
192 *****************************************************************************/
193 static void __inline__ ReferenceReplace( vpar_thread_t * p_vpar,
195 picture_t * p_newref )
197 if( i_coding_type != B_CODING_TYPE )
199 if( p_vpar->sequence.p_backward != NULL )
201 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
203 p_vpar->sequence.p_backward = p_newref;
204 if( p_newref != NULL )
206 vout_LinkPicture( p_vpar->p_vout, p_newref );
211 /*****************************************************************************
212 * LoadMatrix : Load a quantization matrix
213 *****************************************************************************/
214 static __inline__ void LoadMatrix( vpar_thread_t * p_vpar, quant_matrix_t * p_matrix )
218 if( !p_matrix->b_allocated )
220 /* Allocate a piece of memory to load the matrix. */
221 if( (p_matrix->pi_matrix = (int *)malloc( 64*sizeof(int) )) == NULL )
223 intf_ErrMsg( "vpar error: allocation error in LoadMatrix()\n" );
227 p_matrix->b_allocated = 1;
230 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
232 p_matrix->pi_matrix[pi_scan[SCAN_ZIGZAG][i_dummy]]
233 = GetBits( &p_vpar->bit_stream, 8 );
237 /* Discrete Fourier Transform requires the quantization matrices to
238 * be normalized before using them. */
239 vdec_NormQuantMatrix( p_matrix->pi_matrix );
243 /*****************************************************************************
244 * LinkMatrix : Link a quantization matrix to another
245 *****************************************************************************/
246 static __inline__ void LinkMatrix( quant_matrix_t * p_matrix, int * pi_array )
248 if( p_matrix->b_allocated )
250 /* Deallocate the piece of memory. */
251 free( p_matrix->pi_matrix );
252 p_matrix->b_allocated = 0;
255 p_matrix->pi_matrix = pi_array;
259 * Exported functions.
262 /*****************************************************************************
263 * vpar_NextSequenceHeader : Find the next sequence header
264 *****************************************************************************/
265 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
267 while( !p_vpar->b_die )
269 NextStartCode( p_vpar );
270 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
274 RemoveBits( &p_vpar->bit_stream, 8 );
279 /*****************************************************************************
280 * vpar_ParseHeader : Parse the next header
281 *****************************************************************************/
282 int vpar_ParseHeader( vpar_thread_t * p_vpar )
284 while( !p_vpar->b_die )
286 NextStartCode( p_vpar );
287 switch( GetBits32( &p_vpar->bit_stream ) )
289 case SEQUENCE_HEADER_CODE:
291 p_vpar->c_sequences++;
293 SequenceHeader( p_vpar );
297 case GROUP_START_CODE:
298 GroupHeader( p_vpar );
302 case PICTURE_START_CODE:
303 PictureHeader( p_vpar );
307 case SEQUENCE_END_CODE:
308 intf_DbgMsg("vpar debug: sequence end code received\n");
320 * Following functions are local
323 /*****************************************************************************
324 * SequenceHeader : Parse the next sequence header
325 *****************************************************************************/
326 static void SequenceHeader( vpar_thread_t * p_vpar )
329 static int i_frame_rate_table[16] =
340 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
344 int i_height_save, i_width_save;
346 i_height_save = p_vpar->sequence.i_height;
347 i_width_save = p_vpar->sequence.i_width;
349 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
350 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
351 p_vpar->sequence.i_aspect_ratio = GetBits( &p_vpar->bit_stream, 4 );
352 p_vpar->sequence.i_frame_rate =
353 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
355 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
356 * constrained_parameters_flag */
357 RemoveBits( &p_vpar->bit_stream, 30 );
360 * Quantization matrices
362 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
364 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
368 /* Use default matrix. */
369 LinkMatrix( &p_vpar->sequence.intra_quant, pi_default_intra_quant );
372 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_non_intra_quantizer_matrix */
374 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
378 /* Use default matrix. */
379 LinkMatrix( &p_vpar->sequence.nonintra_quant, pi_default_nonintra_quant );
382 /* Unless later overwritten by a matrix extension, we have the same
383 * matrices for luminance and chrominance. */
384 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
385 p_vpar->sequence.intra_quant.pi_matrix );
386 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
387 p_vpar->sequence.nonintra_quant.pi_matrix );
392 NextStartCode( p_vpar );
393 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
397 /* Turn the MPEG2 flag on */
398 p_vpar->sequence.b_mpeg2 = 1;
400 /* Parse sequence_extension */
401 RemoveBits32( &p_vpar->bit_stream );
402 /* extension_start_code_identifier, profile_and_level_indication */
403 RemoveBits( &p_vpar->bit_stream, 12 );
404 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
405 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
406 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
407 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
408 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension, low_delay */
409 RemoveBits( &p_vpar->bit_stream, 22 );
410 /* frame_rate_extension_n */
411 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
412 /* frame_rate_extension_d */
413 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
414 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
418 /* It's an MPEG-1 stream. Put adequate parameters. */
420 p_vpar->sequence.b_mpeg2 = 0;
421 p_vpar->sequence.b_progressive = 1;
422 p_vpar->sequence.i_chroma_format = CHROMA_420;
426 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
427 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
428 (p_vpar->sequence.i_height + 15) / 16 :
429 2 * ((p_vpar->sequence.i_height + 31) / 32);
430 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
431 * p_vpar->sequence.i_mb_height;
432 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
433 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
434 p_vpar->sequence.i_size = p_vpar->sequence.i_width
435 * p_vpar->sequence.i_height;
437 /* Update chromatic information. */
438 switch( p_vpar->sequence.i_chroma_format )
441 p_vpar->sequence.i_chroma_nb_blocks = 2;
442 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
443 p_vpar->sequence.i_chroma_mb_width = 8;
444 p_vpar->sequence.i_chroma_mb_height = 8;
448 p_vpar->sequence.i_chroma_nb_blocks = 4;
449 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width >> 1;
450 p_vpar->sequence.i_chroma_mb_width = 8;
451 p_vpar->sequence.i_chroma_mb_height = 16;
455 p_vpar->sequence.i_chroma_nb_blocks = 8;
456 p_vpar->sequence.i_chroma_width = p_vpar->sequence.i_width;
457 p_vpar->sequence.i_chroma_mb_width = 16;
458 p_vpar->sequence.i_chroma_mb_height = 16;
461 /* Reset scalable_mode. */
462 p_vpar->sequence.i_scalable_mode = SC_NONE;
465 if( p_vpar->sequence.i_width != i_width_save
466 || p_vpar->sequence.i_height != i_height_save )
468 /* FIXME: What do we do in case of a size change ?? */
472 /* Extension and User data */
473 ExtensionAndUserData( p_vpar );
476 /*****************************************************************************
477 * GroupHeader : Parse the next group of pictures header
478 *****************************************************************************/
479 static void GroupHeader( vpar_thread_t * p_vpar )
481 /* Nothing to do, we don't care. */
482 RemoveBits( &p_vpar->bit_stream, 27 );
483 ExtensionAndUserData( p_vpar );
486 /*****************************************************************************
487 * PictureHeader : Parse the next picture header
488 *****************************************************************************/
489 static void PictureHeader( vpar_thread_t * p_vpar )
493 boolean_t b_parsable;
498 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
499 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
500 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
502 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
503 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
505 p_vpar->picture.pb_full_pel_vector[0] = GetBits( &p_vpar->bit_stream, 1 );
506 p_vpar->picture.i_forward_f_code = GetBits( &p_vpar->bit_stream, 3 );
508 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
510 p_vpar->picture.pb_full_pel_vector[1] = GetBits( &p_vpar->bit_stream, 1 );
511 p_vpar->picture.i_backward_f_code = GetBits( &p_vpar->bit_stream, 3 );
514 /* extra_information_picture */
515 while( GetBits( &p_vpar->bit_stream, 1 ) )
517 RemoveBits( &p_vpar->bit_stream, 8 );
521 * Picture Coding Extension
523 NextStartCode( p_vpar );
524 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
526 /* Parse picture_coding_extension */
527 RemoveBits32( &p_vpar->bit_stream );
528 /* extension_start_code_identifier */
529 RemoveBits( &p_vpar->bit_stream, 4 );
531 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 );
532 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 );
533 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 );
534 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 );
535 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
536 i_structure = GetBits( &p_vpar->bit_stream, 2 );
537 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
538 p_vpar->picture.b_frame_pred_frame_dct
539 = GetBits( &p_vpar->bit_stream, 1 );
540 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
541 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
542 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
543 p_vpar->picture.b_alternate_scan = GetBits( &p_vpar->bit_stream, 1 );
544 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
545 /* repeat_first_field (ISO/IEC 13818-2 6.3.10 is necessary to know
546 * the length of the picture_display_extension structure.
547 * chroma_420_type (obsolete) */
548 RemoveBits( &p_vpar->bit_stream, 1 );
549 p_vpar->picture.b_progressive_frame = GetBits( &p_vpar->bit_stream, 1 );
551 /* composite_display_flag */
552 if( GetBits( &p_vpar->bit_stream, 1 ) )
554 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
555 * sub_carrier_phase */
556 RemoveBits( &p_vpar->bit_stream, 20 );
561 /* MPEG-1 compatibility flags */
562 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
563 i_structure = FRAME_STRUCTURE;
564 p_vpar->picture.b_frame_pred_frame_dct = 1;
565 p_vpar->picture.b_concealment_mv = 0;
566 p_vpar->picture.b_q_scale_type = 0;
567 p_vpar->picture.b_intra_vlc_format = 0;
568 p_vpar->picture.b_alternate_scan = 0; /* zigzag */
569 p_vpar->picture.b_repeat_first_field = 0;
570 p_vpar->picture.b_progressive_frame = 1;
574 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
577 if( p_vpar->picture.i_current_structure &&
578 (i_structure == FRAME_STRUCTURE ||
579 i_structure == p_vpar->picture.i_current_structure) )
581 /* We don't have the second field of the buffered frame. */
582 if( p_vpar->picture.p_picture != NULL )
584 ReferenceReplace( p_vpar,
585 p_vpar->picture.i_coding_type,
589 for( i_mb = 0; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
591 vpar_DestroyMacroblock( &p_vpar->vfifo,
592 p_vpar->picture.pp_mb[i_mb] );
595 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
598 p_vpar->picture.i_current_structure = 0;
600 intf_DbgMsg("vpar debug: odd number of field picture.\n");
603 /* Do we have the reference pictures ? */
604 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE) &&
605 (p_vpar->sequence.p_backward == NULL)) ||
606 /* p_backward will become p_forward later */
607 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
608 (p_vpar->sequence.p_forward == NULL ||
609 p_vpar->sequence.p_backward == NULL)));
613 if( p_vpar->picture.i_current_structure )
615 /* Second field of a frame. We will decode it if, and only if we
616 * have decoded the first field. */
617 b_parsable = (p_vpar->picture.p_picture != NULL);
621 /* Does synchro say we have enough time to decode it ? */
622 b_parsable = vpar_SynchroChoose( p_vpar,
623 p_vpar->picture.i_coding_type, i_structure );
629 /* Update the reference pointers. */
630 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
632 /* Update context. */
633 if( i_structure != FRAME_STRUCTURE )
635 if( (p_vpar->picture.i_current_structure | i_structure)
638 p_vpar->picture.i_current_structure = 0;
642 /* The frame is complete. */
643 p_vpar->picture.i_current_structure = i_structure;
644 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
649 /* Warn Synchro we have trashed a picture. */
650 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
652 p_vpar->picture.p_picture = NULL;
657 /* OK, now we are sure we will decode the picture. */
659 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
662 #define P_picture p_vpar->picture.p_picture
663 p_vpar->picture.b_error = 0;
664 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
666 if( !p_vpar->picture.i_current_structure )
668 /* This is a new frame. Get a structure from the video_output. */
669 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
670 99+p_vpar->sequence.i_chroma_format, /*XXX??*/
671 p_vpar->sequence.i_width,
672 p_vpar->sequence.i_height ) )
675 intf_DbgMsg("vpar debug: allocation error in vout_CreatePicture, delaying\n");
676 if( p_vpar->b_die || p_vpar->b_error )
680 msleep( VPAR_OUTMEM_SLEEP );
683 /* Initialize values. */
684 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
685 P_picture->i_aspect_ratio = p_vpar->sequence.i_aspect_ratio;
686 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
687 p_vpar->picture.i_l_stride = ( p_vpar->sequence.i_width
688 << ( 1 - p_vpar->picture.b_frame_structure ) );
689 p_vpar->picture.i_c_stride = ( p_vpar->sequence.i_chroma_width
690 << ( 1 - p_vpar->picture.b_frame_structure ));
692 P_picture->i_deccount = p_vpar->sequence.i_mb_size;
693 vlc_mutex_init( &p_vpar->picture.p_picture->lock_deccount );
695 memset( p_vpar->picture.pp_mb, 0, MAX_MB*sizeof(macroblock_t *) );
697 /* FIXME ! remove asap ?? */
698 //memset( P_picture->p_data, 0, (p_vpar->sequence.i_mb_size*384));
700 /* Update the reference pointers. */
701 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
704 /* Link referenced pictures for the decoder
705 * They are unlinked in vpar_ReleaseMacroblock() & vpar_DestroyMacroblock() */
706 if( p_vpar->picture.i_coding_type == P_CODING_TYPE ||
707 p_vpar->picture.i_coding_type == B_CODING_TYPE )
709 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
711 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
713 vout_LinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
717 p_vpar->picture.i_current_structure |= i_structure;
718 p_vpar->picture.i_structure = i_structure;
720 /* Initialize picture data for decoding. */
721 if( i_structure == BOTTOM_FIELD )
723 i_mb_base = p_vpar->sequence.i_mb_size >> 1;
724 p_vpar->mb.i_l_y = 1;
725 p_vpar->mb.i_c_y = 1;
730 p_vpar->mb.i_l_y = p_vpar->mb.i_c_y = 0;
732 p_vpar->mb.i_l_x = p_vpar->mb.i_c_x = 0;
734 /* Extension and User data. */
735 ExtensionAndUserData( p_vpar );
737 vpar_PictureData( p_vpar, i_mb_base );
739 if( p_vpar->b_die || p_vpar->b_error )
744 if( p_vpar->picture.b_error )
747 //fprintf(stderr, "Image trashee\n");
749 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
751 vpar_DestroyMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
756 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
759 if( P_picture->i_deccount != 1 )
761 vpar_SynchroEnd( p_vpar, 1 );
762 vout_DestroyPicture( p_vpar->p_vout, P_picture );
765 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
767 /* Prepare context for the next picture. */
769 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
770 p_vpar->picture.i_current_structure = 0;
772 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
774 //fprintf(stderr, "Image parsee (%d)\n", p_vpar->picture.i_coding_type);
775 /* Frame completely parsed. */
777 for( i_mb = 1; p_vpar->picture.pp_mb[i_mb] != NULL; i_mb++ )
779 vpar_DecodeMacroblock( &p_vpar->vfifo, p_vpar->picture.pp_mb[i_mb] );
782 /* Send signal to the video_decoder. */
783 vlc_mutex_lock( &p_vpar->vfifo.lock );
784 vlc_cond_signal( &p_vpar->vfifo.wait );
785 vlc_mutex_unlock( &p_vpar->vfifo.lock );
788 /* Prepare context for the next picture. */
790 p_vpar->picture.i_current_structure = 0;
795 /*****************************************************************************
796 * ExtensionAndUserData : Parse the extension_and_user_data structure
797 *****************************************************************************/
798 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
800 while( !p_vpar->b_die )
802 NextStartCode( p_vpar );
803 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
805 case EXTENSION_START_CODE:
806 RemoveBits32( &p_vpar->bit_stream );
807 switch( GetBits( &p_vpar->bit_stream, 4 ) )
809 case SEQUENCE_DISPLAY_EXTENSION_ID:
810 SequenceDisplayExtension( p_vpar );
812 case QUANT_MATRIX_EXTENSION_ID:
813 QuantMatrixExtension( p_vpar );
815 case SEQUENCE_SCALABLE_EXTENSION_ID:
816 SequenceScalableExtension( p_vpar );
818 case PICTURE_DISPLAY_EXTENSION_ID:
819 PictureDisplayExtension( p_vpar );
821 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
822 PictureSpatialScalableExtension( p_vpar );
824 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
825 PictureTemporalScalableExtension( p_vpar );
827 case COPYRIGHT_EXTENSION_ID:
828 CopyrightExtension( p_vpar );
834 case USER_DATA_START_CODE:
835 RemoveBits32( &p_vpar->bit_stream );
836 /* Wait for the next start code */
846 /*****************************************************************************
847 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
848 *****************************************************************************/
850 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
852 /* We don't care sequence_display_extension. */
854 RemoveBits( &p_vpar->bit_stream, 3 );
855 if( GetBits( &p_vpar->bit_stream, 1 ) )
857 /* Two bytes for color_desciption */
858 RemoveBits( &p_vpar->bit_stream, 16 );
859 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
861 /* display_horizontal and vertical_size and a marker_bit */
862 RemoveBits( &p_vpar->bit_stream, 29 );
866 /*****************************************************************************
867 * QuantMatrixExtension : Load quantization matrices for luminance *
869 *****************************************************************************/
871 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
873 if( GetBits( &p_vpar->bit_stream, 1 ) )
875 /* Load intra_quantiser_matrix for luminance. */
876 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
880 /* Use the default matrix. */
881 LinkMatrix( &p_vpar->sequence.intra_quant,
882 pi_default_intra_quant );
884 if( GetBits( &p_vpar->bit_stream, 1 ) )
886 /* Load non_intra_quantiser_matrix for luminance. */
887 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
891 /* Use the default matrix. */
892 LinkMatrix( &p_vpar->sequence.nonintra_quant,
893 pi_default_nonintra_quant );
895 if( GetBits( &p_vpar->bit_stream, 1 ) )
897 /* Load intra_quantiser_matrix for chrominance. */
898 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
902 /* Link the chrominance intra matrix to the luminance one. */
903 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
904 p_vpar->sequence.intra_quant.pi_matrix );
906 if( GetBits( &p_vpar->bit_stream, 1 ) )
908 /* Load non_intra_quantiser_matrix for chrominance. */
909 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
913 /* Link the chrominance intra matrix to the luminance one. */
914 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
915 p_vpar->sequence.intra_quant.pi_matrix );
917 if( GetBits( &p_vpar->bit_stream, 1 ) )
919 /* Load non_intra_quantiser_matrix for chrominance. */
920 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
924 /* Link the chrominance nonintra matrix to the luminance one. */
925 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
926 p_vpar->sequence.nonintra_quant.pi_matrix );
931 /*****************************************************************************
932 * SequenceScalableExtension : Parse the sequence_scalable_extension *
933 * structure to handle scalable coding *
934 *****************************************************************************/
936 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
938 /* We don't care about anything scalable except the scalable mode. */
939 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
940 /* The length of the structure depends on the value of the scalable_mode */
943 RemoveBits32( &p_vpar->bit_stream );
944 RemoveBits( &p_vpar->bit_stream, 21 );
947 RemoveBits( &p_vpar->bit_stream, 12 );
950 RemoveBits( &p_vpar->bit_stream, 4 );
954 /*****************************************************************************
955 * PictureDisplayExtension : Parse the picture_display_extension structure *
956 *****************************************************************************/
958 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
960 /* Number of frame center offset */
962 /* I am not sure it works but it should
963 (fewer tests than shown in §6.3.12) */
964 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
965 p_vpar->picture.b_repeat_first_field +
966 p_vpar->picture.b_top_field_first
967 : ( p_vpar->picture.b_frame_structure + 1 ) +
968 p_vpar->picture.b_repeat_first_field;
969 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
971 RemoveBits( &p_vpar->bit_stream, 17 );
972 RemoveBits( &p_vpar->bit_stream, 17 );
977 /*****************************************************************************
978 * PictureSpatialScalableExtension *
979 *****************************************************************************/
981 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
983 /* That's scalable, so we trash it */
984 RemoveBits32( &p_vpar->bit_stream );
985 RemoveBits( &p_vpar->bit_stream, 16 );
989 /*****************************************************************************
990 * PictureTemporalScalableExtension *
991 *****************************************************************************/
993 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
995 /* Scalable again, trashed again */
996 RemoveBits( &p_vpar->bit_stream, 23 );
1000 /*****************************************************************************
1001 * CopyrightExtension : Keeps some legal informations *
1002 *****************************************************************************/
1004 static void CopyrightExtension( vpar_thread_t * p_vpar )
1006 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1007 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1008 /* A flag that says whether the copyright information is significant */
1009 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1010 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1011 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1013 RemoveBits( &p_vpar->bit_stream, 8 );
1014 /* The copyright_number is split in three parts */
1016 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1017 RemoveBits( &p_vpar->bit_stream, 1 );
1019 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1020 RemoveBits( &p_vpar->bit_stream, 1 );
1021 /* third part and sum */
1022 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1023 ( (u64)i_copyright_nb_2 << 22 ) |
1024 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );