1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: headers.c,v 1.10 2003/02/04 11:51:21 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 *****************************************************************************/
28 #include <stdlib.h> /* free() */
29 #include <string.h> /* memcpy(), memset() */
33 #include <vlc/decoder.h>
43 static inline void NextStartCode( bit_stream_t * );
44 static void SequenceHeader( vpar_thread_t * p_vpar );
45 static void GroupHeader( vpar_thread_t * p_vpar );
46 static void PictureHeader( vpar_thread_t * p_vpar );
47 static void ExtensionAndUserData( vpar_thread_t * p_vpar );
48 static void QuantMatrixExtension( vpar_thread_t * p_vpar );
49 static void SequenceScalableExtension( vpar_thread_t * p_vpar );
50 static void SequenceDisplayExtension( vpar_thread_t * p_vpar );
51 static void PictureDisplayExtension( vpar_thread_t * p_vpar );
52 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar );
53 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar );
54 static void CopyrightExtension( vpar_thread_t * p_vpar );
61 /*****************************************************************************
62 * pi_default_intra_quant : default quantization matrix
63 *****************************************************************************/
64 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
66 8, 16, 19, 22, 26, 27, 29, 34,
67 16, 16, 22, 24, 27, 29, 34, 37,
68 19, 22, 26, 27, 29, 34, 34, 38,
69 22, 22, 26, 27, 29, 34, 37, 40,
70 22, 26, 27, 29, 32, 35, 40, 48,
71 26, 27, 29, 32, 35, 40, 48, 58,
72 26, 27, 29, 34, 38, 46, 56, 69,
73 27, 29, 35, 38, 46, 56, 69, 83
76 /*****************************************************************************
77 * pi_default_nonintra_quant : default quantization matrix
78 *****************************************************************************/
79 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
81 16, 16, 16, 16, 16, 16, 16, 16,
82 16, 16, 16, 16, 16, 16, 16, 16,
83 16, 16, 16, 16, 16, 16, 16, 16,
84 16, 16, 16, 16, 16, 16, 16, 16,
85 16, 16, 16, 16, 16, 16, 16, 16,
86 16, 16, 16, 16, 16, 16, 16, 16,
87 16, 16, 16, 16, 16, 16, 16, 16,
88 16, 16, 16, 16, 16, 16, 16, 16
91 /*****************************************************************************
92 * pi_scan : zig-zag and alternate scan patterns
93 *****************************************************************************/
94 u8 pi_scan[2][64] ATTR_ALIGN(16) =
96 { /* Zig-Zag pattern */
97 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
98 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
99 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
100 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
102 { /* Alternate scan pattern */
103 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
104 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
105 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
106 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
111 * Local inline functions.
114 /*****************************************************************************
115 * ReferenceUpdate : Update the reference pointers when we have a new picture
116 *****************************************************************************/
117 static void inline ReferenceUpdate( vpar_thread_t * p_vpar,
118 int i_coding_type, picture_t * p_newref )
120 if( i_coding_type != B_CODING_TYPE )
122 if( p_vpar->sequence.p_forward != NULL )
124 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
126 if( p_vpar->sequence.p_backward != NULL )
128 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
129 vpar_SynchroDate( p_vpar ) );
131 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
132 p_vpar->sequence.p_backward = p_newref;
133 if( p_newref != NULL )
135 vout_LinkPicture( p_vpar->p_vout, p_newref );
138 else if( p_newref != NULL )
140 /* Put date immediately. */
141 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
145 /*****************************************************************************
146 * ReferenceReplace : Replace the last reference pointer when we destroy
148 *****************************************************************************/
149 static void inline ReferenceReplace( vpar_thread_t * p_vpar,
150 int i_coding_type, picture_t * p_newref )
152 if( i_coding_type != B_CODING_TYPE )
154 if( p_vpar->sequence.p_backward != NULL )
156 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
158 p_vpar->sequence.p_backward = p_newref;
159 if( p_newref != NULL )
161 vout_LinkPicture( p_vpar->p_vout, p_newref );
166 /*****************************************************************************
167 * LoadMatrix : Load a quantization matrix
168 *****************************************************************************/
169 static inline void LoadMatrix( vpar_thread_t * p_vpar,
170 quant_matrix_t * p_matrix )
174 if( !p_matrix->b_allocated )
176 /* Allocate a piece of memory to load the matrix. */
177 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
179 msg_Err( p_vpar->p_fifo, "out of memory" );
180 p_vpar->p_fifo->b_error = 1;
183 p_matrix->b_allocated = 1;
186 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
188 p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
189 = GetBits( &p_vpar->bit_stream, 8 );
193 /*****************************************************************************
194 * LinkMatrix : Link a quantization matrix to another
195 *****************************************************************************/
196 static inline void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
198 if( p_matrix->b_allocated )
200 /* Deallocate the piece of memory. */
201 free( p_matrix->pi_matrix );
202 p_matrix->b_allocated = 0;
205 p_matrix->pi_matrix = pi_array;
208 /*****************************************************************************
209 * ChromaToFourCC: Return a FourCC value used by the video output.
210 *****************************************************************************/
211 static inline vlc_fourcc_t ChromaToFourCC( int i_chroma )
216 return VLC_FOURCC('I','4','2','0');
219 return VLC_FOURCC('I','4','2','2');
222 return VLC_FOURCC('I','4','4','4');
225 /* This can't happen */
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 NextStartCode( &p_vpar->bit_stream );
257 switch( GetBits32( &p_vpar->bit_stream ) )
259 case SEQUENCE_HEADER_CODE:
260 p_vpar->c_sequences++;
261 SequenceHeader( p_vpar );
264 case GROUP_START_CODE:
265 GroupHeader( p_vpar );
268 case PICTURE_START_CODE:
269 PictureHeader( p_vpar );
272 case SEQUENCE_END_CODE:
273 msg_Dbg( p_vpar->p_fifo, "sequence end code received" );
274 if( p_vpar->sequence.p_backward != NULL )
276 p_vpar->sequence.p_backward->b_force = 1;
278 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
289 * Following functions are local
292 /*****************************************************************************
293 * SequenceHeader : Parse the next sequence header
294 *****************************************************************************/
295 static void SequenceHeader( vpar_thread_t * p_vpar )
298 static int i_frame_rate_table[16] =
309 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
315 p_vpar->sequence.b_after_sequence_header = 1;
316 p_vpar->sequence.i_width = GetBits( &p_vpar->bit_stream, 12 );
317 p_vpar->sequence.i_height = GetBits( &p_vpar->bit_stream, 12 );
318 i_aspect = GetBits( &p_vpar->bit_stream, 4 );
319 p_vpar->sequence.i_frame_rate =
320 i_frame_rate_table[ GetBits( &p_vpar->bit_stream, 4 ) ];
322 /* We don't need bit_rate_value, marker_bit, vbv_buffer_size,
323 * constrained_parameters_flag */
324 RemoveBits( &p_vpar->bit_stream, 30 );
327 * Quantization matrices
329 if( GetBits( &p_vpar->bit_stream, 1 ) ) /* load_intra_quantizer_matrix */
331 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
335 /* Use default matrix. */
336 LinkMatrix( &p_vpar->sequence.intra_quant,
337 p_vpar->pi_default_intra_quant );
340 if( GetBits(&p_vpar->bit_stream, 1) ) /* load_non_intra_quantizer_matrix */
342 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
346 /* Use default matrix. */
347 LinkMatrix( &p_vpar->sequence.nonintra_quant,
348 p_vpar->pi_default_nonintra_quant );
351 /* Unless later overwritten by a matrix extension, we have the same
352 * matrices for luminance and chrominance. */
353 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
354 p_vpar->sequence.intra_quant.pi_matrix );
355 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
356 p_vpar->sequence.nonintra_quant.pi_matrix );
361 NextStartCode( &p_vpar->bit_stream );
362 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
366 /* Turn the MPEG2 flag on */
367 p_vpar->sequence.b_mpeg2 = 1;
369 /* Parse sequence_extension */
370 RemoveBits32( &p_vpar->bit_stream );
371 /* extension_start_code_identifier, profile_and_level_indication */
372 RemoveBits( &p_vpar->bit_stream, 12 );
373 p_vpar->sequence.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
374 p_vpar->sequence.i_chroma_format = GetBits( &p_vpar->bit_stream, 2 );
375 p_vpar->sequence.i_width |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
376 p_vpar->sequence.i_height |= GetBits( &p_vpar->bit_stream, 2 ) << 12;
377 /* bit_rate_extension, marker_bit, vbv_buffer_size_extension,
379 RemoveBits( &p_vpar->bit_stream, 22 );
380 /* frame_rate_extension_n */
381 i_dummy = GetBits( &p_vpar->bit_stream, 2 );
382 /* frame_rate_extension_d */
383 p_vpar->sequence.i_frame_rate *= (i_dummy + 1)
384 / (GetBits( &p_vpar->bit_stream, 5 ) + 1);
388 /* It's an MPEG-1 stream. Put adequate parameters. */
390 static int pi_mpeg1ratio[15] = {
391 10000, 10000, 6735, 7031, 7615, 8055, 8437, 8935,
392 9157, 9815, 10255, 10695, 10950, 11575, 12015
397 i_xyratio = p_vpar->sequence.i_height *
398 pi_mpeg1ratio[i_aspect] / p_vpar->sequence.i_width;
399 if( 7450 < i_xyratio && i_xyratio < 7550 )
403 else if( 5575 < i_xyratio && i_xyratio < 5675 )
407 else if( 4475 < i_xyratio && i_xyratio < 4575 )
413 p_vpar->sequence.b_mpeg2 = 0;
414 p_vpar->sequence.b_progressive = 1;
415 p_vpar->sequence.i_chroma_format = CHROMA_420;
418 /* check whether the input gives a particular aspect ratio */
419 if( p_vpar->p_fifo->p_demux_data
420 && ( *(int*)(p_vpar->p_fifo->p_demux_data) & 0x7 ) )
422 i_aspect = *(int*)(p_vpar->p_fifo->p_demux_data);
425 /* Store calculated aspect ratio */
429 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
432 case AR_16_9_PICTURE:
433 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
436 case AR_221_1_PICTURE:
437 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
440 case AR_SQUARE_PICTURE:
442 p_vpar->sequence.i_aspect = VOUT_ASPECT_FACTOR
443 * p_vpar->sequence.i_width / p_vpar->sequence.i_height;
448 p_vpar->sequence.i_mb_width = (p_vpar->sequence.i_width + 15) / 16;
449 p_vpar->sequence.i_mb_height = (p_vpar->sequence.b_progressive) ?
450 (p_vpar->sequence.i_height + 15) / 16 :
451 2 * ((p_vpar->sequence.i_height + 31) / 32);
452 p_vpar->sequence.i_mb_size = p_vpar->sequence.i_mb_width
453 * p_vpar->sequence.i_mb_height;
454 p_vpar->sequence.i_width = (p_vpar->sequence.i_mb_width * 16);
455 p_vpar->sequence.i_height = (p_vpar->sequence.i_mb_height * 16);
456 p_vpar->sequence.i_size = p_vpar->sequence.i_width
457 * p_vpar->sequence.i_height;
458 switch( p_vpar->sequence.i_chroma_format )
461 p_vpar->sequence.i_chroma_nb_blocks = 2;
462 p_vpar->sequence.b_chroma_h_subsampled = 1;
463 p_vpar->sequence.b_chroma_v_subsampled = 1;
466 p_vpar->sequence.i_chroma_nb_blocks = 4;
467 p_vpar->sequence.b_chroma_h_subsampled = 1;
468 p_vpar->sequence.b_chroma_v_subsampled = 0;
471 p_vpar->sequence.i_chroma_nb_blocks = 6;
472 p_vpar->sequence.b_chroma_h_subsampled = 0;
473 p_vpar->sequence.b_chroma_v_subsampled = 0;
477 /* Extension and User data */
478 ExtensionAndUserData( p_vpar );
480 /* Spawn a video output if there is none. First we look for our children,
481 * then we look for any other vout that might be available. */
483 vout_Request( p_vpar->p_fifo, p_vpar->p_vout,
484 p_vpar->sequence.i_width, p_vpar->sequence.i_height,
485 ChromaToFourCC( p_vpar->sequence.i_chroma_format ),
486 p_vpar->sequence.i_aspect );
488 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
493 if( p_vpar->p_vout == NULL )
495 msg_Err( p_vpar->p_fifo, "cannot open vout, aborting" );
496 p_vpar->p_fifo->b_error = 1;
501 /*****************************************************************************
502 * GroupHeader : Parse the next group of pictures header
503 *****************************************************************************/
504 static void GroupHeader( vpar_thread_t * p_vpar )
506 /* Nothing to do, we don't care. */
507 RemoveBits( &p_vpar->bit_stream, 27 );
508 ExtensionAndUserData( p_vpar );
511 /*****************************************************************************
512 * PictureHeader : Parse the next picture header
513 *****************************************************************************/
514 static void PictureHeader( vpar_thread_t * p_vpar )
516 int i_structure, i_previous_coding_type;
517 vlc_bool_t b_parsable = 0;
519 /* Retrieve the PTS. */
520 CurrentPTS( &p_vpar->bit_stream, &p_vpar->sequence.next_pts,
521 &p_vpar->sequence.next_dts );
523 /* Recover in case of stream discontinuity. */
524 if( p_vpar->sequence.b_expect_discontinuity )
526 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
527 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
528 if( p_vpar->picture.p_picture != NULL )
530 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
531 p_vpar->picture.p_picture = NULL;
533 p_vpar->picture.i_current_structure = 0;
534 p_vpar->sequence.b_expect_discontinuity = 0;
537 /* Parse the picture header. */
538 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
539 i_previous_coding_type = p_vpar->picture.i_coding_type;
540 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
541 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
543 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
544 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
546 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
547 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
550 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
552 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
553 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
557 /* extra_information_picture */
558 while( GetBits( &p_vpar->bit_stream, 1 ) )
560 RemoveBits( &p_vpar->bit_stream, 8 );
564 * Picture Coding Extension
566 NextStartCode( &p_vpar->bit_stream );
567 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
569 /* Parse picture_coding_extension */
570 RemoveBits32( &p_vpar->bit_stream );
571 /* extension_start_code_identifier */
572 RemoveBits( &p_vpar->bit_stream, 4 );
574 /* Pre-substract 1 for later use in MotionDelta(). */
575 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
576 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
577 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
578 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
579 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
580 i_structure = GetBits( &p_vpar->bit_stream, 2 );
581 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
582 p_vpar->picture.b_frame_pred_frame_dct
583 = GetBits( &p_vpar->bit_stream, 1 );
584 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
585 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
586 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
588 p_vpar->picture.pi_scan =
589 p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
590 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
591 /* chroma_420_type (obsolete) */
592 RemoveBits( &p_vpar->bit_stream, 1 );
593 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
595 /* composite_display_flag */
596 if( GetBits( &p_vpar->bit_stream, 1 ) )
598 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
599 * sub_carrier_phase */
600 RemoveBits( &p_vpar->bit_stream, 20 );
605 /* MPEG-1 compatibility flags */
606 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
607 i_structure = FRAME_STRUCTURE;
608 p_vpar->picture.b_top_field_first = 0;
609 p_vpar->picture.b_frame_pred_frame_dct = 1;
610 p_vpar->picture.b_concealment_mv = 0;
611 p_vpar->picture.b_q_scale_type = 0;
612 p_vpar->picture.b_intra_vlc_format = 0;
613 p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
614 p_vpar->picture.b_repeat_first_field = 0;
615 p_vpar->picture.b_progressive = 1;
618 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
620 /* Extension and User data. */
621 ExtensionAndUserData( p_vpar );
623 #define P_picture p_vpar->picture.p_picture
624 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
626 if ( p_vpar->picture.i_coding_type != I_CODING_TYPE
627 && p_vpar->sequence.b_after_sequence_header
628 && p_vpar->sequence.p_backward == NULL )
630 /* Intra-slice refresh. */
631 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
632 1, 0, 0 ) ) == NULL )
634 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
638 msleep( VOUT_OUTMEM_SLEEP );
641 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
642 memset( P_picture->p[0].p_pixels, 0,
643 p_vpar->sequence.i_height * p_vpar->sequence.i_width );
645 memset( P_picture->p[1].p_pixels, 0x80,
646 p_vpar->sequence.i_height * p_vpar->sequence.i_width
647 >> (p_vpar->sequence.b_chroma_h_subsampled
648 + p_vpar->sequence.b_chroma_v_subsampled) );
650 memset( P_picture->p[2].p_pixels, 0x80,
651 p_vpar->sequence.i_height * p_vpar->sequence.i_width
652 >> (p_vpar->sequence.b_chroma_h_subsampled
653 + p_vpar->sequence.b_chroma_v_subsampled) );
655 /* Update the reference pointers. */
656 ReferenceUpdate( p_vpar, I_CODING_TYPE, P_picture );
658 p_vpar->sequence.b_after_sequence_header = 0;
660 if( p_vpar->picture.i_current_structure )
662 if ( (i_structure == FRAME_STRUCTURE ||
663 i_structure == p_vpar->picture.i_current_structure) )
665 /* We don't have the second field of the buffered frame. */
666 if( p_vpar->picture.p_picture != NULL )
668 ReferenceReplace( p_vpar,
669 p_vpar->picture.i_coding_type,
671 vout_DestroyPicture( p_vpar->p_vout,
672 p_vpar->picture.p_picture );
673 p_vpar->picture.p_picture = NULL;
676 p_vpar->picture.i_current_structure = 0;
678 msg_Warn( p_vpar->p_fifo, "odd number of field pictures" );
682 /* Second field of a frame. We will decode it if, and only if we
683 * have decoded the first field. */
684 if( p_vpar->picture.p_picture == NULL )
686 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
687 && p_vpar->sequence.p_backward == NULL) )
689 /* Exceptionnally, parse the picture if it is I. We need
690 * this in case of an odd number of field pictures, if the
691 * previous picture is not intra, we desperately need a
692 * new reference picture. OK, this is kind of kludgy. */
693 p_vpar->picture.i_current_structure = 0;
702 /* We suppose we have the reference pictures, since we already
703 * decoded the first field and the second field will not need
704 * any extra reference picture. There is a special case of
705 * P field being the second field of an I field, but ISO/IEC
706 * 13818-2 section 7.6.3.5 specifies that this P field will
707 * not need any reference picture besides the I field. So far
711 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
712 i_previous_coding_type == I_CODING_TYPE &&
713 p_vpar->sequence.p_forward == NULL )
715 /* This is the special case of section 7.6.3.5. Create
716 * a fake reference picture (which will not be used)
717 * but will prevent us from segfaulting in the slice
719 static picture_t fake_picture;
720 fake_picture.i_planes = 0; /* We will use it later */
721 p_vpar->sequence.p_forward = &fake_picture;
727 if( !p_vpar->picture.i_current_structure )
729 /* First field of a frame, or new frame picture. */
732 /* Do we have the reference pictures ? */
733 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
734 p_vpar->picture.b_concealment_mv) &&
735 (p_vpar->sequence.p_backward == NULL)) ||
736 /* p_backward will become p_forward later */
737 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
738 (p_vpar->sequence.p_forward == NULL ||
739 p_vpar->sequence.p_backward == NULL)));
741 /* Compute the number of times the frame will be emitted by the
742 * decoder (number of half-periods). */
743 if( p_vpar->sequence.b_progressive )
745 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
746 + p_vpar->picture.b_top_field_first) * 2;
750 if( p_vpar->picture.b_progressive )
752 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
760 /* Warn synchro we have a new picture (updates pictures index). */
761 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
766 /* Does synchro say we have enough time to decode it ? */
767 b_parsable = vpar_SynchroChoose( p_vpar,
768 p_vpar->picture.i_coding_type, i_structure );
774 /* Update the reference pointers. */
775 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
777 /* Update context. */
778 if( i_structure != FRAME_STRUCTURE )
780 if( (p_vpar->picture.i_current_structure | i_structure)
783 /* The frame is complete. */
784 p_vpar->picture.i_current_structure = 0;
786 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
790 p_vpar->picture.i_current_structure = i_structure;
795 /* Warn Synchro we have trashed a picture. */
796 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
798 p_vpar->picture.p_picture = NULL;
803 /* OK, now we are sure we will decode the picture. */
804 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
806 p_vpar->picture.b_error = 0;
808 if( !p_vpar->picture.i_current_structure )
810 /* This is a new frame. Get a structure from the video_output. */
811 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
812 p_vpar->picture.b_progressive,
813 p_vpar->picture.b_top_field_first,
814 p_vpar->picture.b_repeat_first_field ) )
817 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
821 msleep( VOUT_OUTMEM_SLEEP );
824 /* Initialize values. */
825 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
826 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
828 /* Update the reference pointers. */
829 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
832 /* Initialize picture data for decoding. */
833 p_vpar->picture.i_current_structure |= i_structure;
834 p_vpar->picture.i_structure = i_structure;
835 p_vpar->picture.b_second_field =
836 (i_structure != p_vpar->picture.i_current_structure);
837 p_vpar->picture.b_current_field =
838 (i_structure == BOTTOM_FIELD );
839 p_vpar->picture.i_lum_stride = p_vpar->picture.p_picture->Y_PITCH
840 << ( 1 - p_vpar->picture.b_frame_structure );
841 p_vpar->picture.i_chrom_stride = p_vpar->picture.p_picture->U_PITCH
842 << ( 1 - p_vpar->picture.b_frame_structure );
843 /* We suppose the pitch is the same for U and V planes. */
844 p_vpar->picture.i_field_width = p_vpar->sequence.i_width
845 << ( 1 - p_vpar->picture.b_frame_structure );
847 if( !p_vpar->p_fifo->p_stream_ctrl->b_grayscale )
849 switch( p_vpar->sequence.i_chroma_format )
852 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock422;
855 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock444;
859 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock420;
865 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblockBW;
869 if( p_vpar->sequence.b_mpeg2 )
871 static f_picture_data_t ppf_picture_data[4][4] =
874 NULL, NULL, NULL, NULL
878 #if (VPAR_OPTIM_LEVEL > 1)
879 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
882 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
883 vpar_PictureDataGENERIC
888 #if (VPAR_OPTIM_LEVEL > 1)
889 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
892 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
893 vpar_PictureDataGENERIC
898 #if (VPAR_OPTIM_LEVEL > 0)
899 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
902 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
903 vpar_PictureDataGENERIC
908 ppf_picture_data[p_vpar->picture.i_structure]
909 [p_vpar->picture.i_coding_type]( p_vpar );
913 #if (VPAR_OPTIM_LEVEL > 1)
914 static f_picture_data_t pf_picture_data[5] =
915 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
916 vpar_PictureData1D };
918 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
920 vpar_PictureDataGENERIC( p_vpar );
924 /* Wait for all the macroblocks to be decoded. */
925 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
927 /* Re-spawn decoder threads if the user changed settings. */
928 vpar_SpawnPool( p_vpar );
930 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
935 if( p_vpar->sequence.p_forward != NULL &&
936 p_vpar->sequence.p_forward->i_planes == 0 )
938 /* This can only happen with the fake picture created for section
939 * 7.6.3.5. Clean up our mess. */
940 p_vpar->sequence.p_forward = NULL;
943 if( p_vpar->picture.b_error )
946 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
948 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
949 p_vpar->picture.i_structure, 1 );
950 vout_DestroyPicture( p_vpar->p_vout, P_picture );
952 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
954 /* Prepare context for the next picture. */
956 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
957 p_vpar->picture.i_current_structure = 0;
959 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
961 /* Frame completely parsed. */
962 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
963 p_vpar->picture.i_structure, 0 );
964 vout_DisplayPicture( p_vpar->p_vout, P_picture );
966 /* Prepare context for the next picture. */
968 p_vpar->picture.i_current_structure = 0;
973 /*****************************************************************************
974 * ExtensionAndUserData : Parse the extension_and_user_data structure
975 *****************************************************************************/
976 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
978 while( !p_vpar->p_fifo->b_die )
980 NextStartCode( &p_vpar->bit_stream );
981 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
983 case EXTENSION_START_CODE:
984 RemoveBits32( &p_vpar->bit_stream );
985 switch( GetBits( &p_vpar->bit_stream, 4 ) )
987 case SEQUENCE_DISPLAY_EXTENSION_ID:
988 SequenceDisplayExtension( p_vpar );
990 case QUANT_MATRIX_EXTENSION_ID:
991 QuantMatrixExtension( p_vpar );
993 case SEQUENCE_SCALABLE_EXTENSION_ID:
994 SequenceScalableExtension( p_vpar );
996 case PICTURE_DISPLAY_EXTENSION_ID:
997 PictureDisplayExtension( p_vpar );
999 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
1000 PictureSpatialScalableExtension( p_vpar );
1002 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
1003 PictureTemporalScalableExtension( p_vpar );
1005 case COPYRIGHT_EXTENSION_ID:
1006 CopyrightExtension( p_vpar );
1013 case USER_DATA_START_CODE:
1014 RemoveBits32( &p_vpar->bit_stream );
1015 /* Wait for the next start code */
1025 /*****************************************************************************
1026 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
1027 *****************************************************************************/
1029 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
1031 /* We don't care sequence_display_extension. */
1033 RemoveBits( &p_vpar->bit_stream, 3 );
1034 if( GetBits( &p_vpar->bit_stream, 1 ) )
1036 /* Two bytes for color_desciption */
1037 RemoveBits( &p_vpar->bit_stream, 16 );
1038 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
1040 /* display_horizontal and vertical_size and a marker_bit */
1041 RemoveBits( &p_vpar->bit_stream, 29 );
1045 /*****************************************************************************
1046 * QuantMatrixExtension : Load quantization matrices for luminance *
1048 *****************************************************************************/
1050 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
1052 if( GetBits( &p_vpar->bit_stream, 1 ) )
1054 /* Load intra_quantiser_matrix for luminance. */
1055 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1059 /* Use the default matrix. */
1060 LinkMatrix( &p_vpar->sequence.intra_quant,
1061 p_vpar->pi_default_intra_quant );
1063 if( GetBits( &p_vpar->bit_stream, 1 ) )
1065 /* Load non_intra_quantiser_matrix for luminance. */
1066 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1070 /* Use the default matrix. */
1071 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1072 p_vpar->pi_default_nonintra_quant );
1074 if( GetBits( &p_vpar->bit_stream, 1 ) )
1076 /* Load intra_quantiser_matrix for chrominance. */
1077 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1081 /* Link the chrominance intra matrix to the luminance one. */
1082 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1083 p_vpar->sequence.intra_quant.pi_matrix );
1085 if( GetBits( &p_vpar->bit_stream, 1 ) )
1087 /* Load non_intra_quantiser_matrix for chrominance. */
1088 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1092 /* Link the chrominance nonintra matrix to the luminance one. */
1093 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1094 p_vpar->sequence.nonintra_quant.pi_matrix );
1099 /*****************************************************************************
1100 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1101 * structure to handle scalable coding *
1102 *****************************************************************************/
1104 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1106 /* We don't care about anything scalable except the scalable mode. */
1107 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1108 /* The length of the structure depends on the value of the scalable_mode */
1111 RemoveBits32( &p_vpar->bit_stream );
1112 RemoveBits( &p_vpar->bit_stream, 21 );
1115 RemoveBits( &p_vpar->bit_stream, 12 );
1118 RemoveBits( &p_vpar->bit_stream, 4 );
1122 /*****************************************************************************
1123 * PictureDisplayExtension : Parse the picture_display_extension structure *
1124 *****************************************************************************/
1126 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1128 /* Number of frame center offset */
1130 /* I am not sure it works but it should
1131 (fewer tests than shown in §6.3.12) */
1132 /* FIXME: is VLC_TRUE always 1 ? */
1133 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1134 p_vpar->picture.b_repeat_first_field +
1135 p_vpar->picture.b_top_field_first
1136 : ( p_vpar->picture.b_frame_structure + 1 ) +
1137 p_vpar->picture.b_repeat_first_field;
1138 for( i_dummy = 0; i_dummy < i_nb && !p_vpar->p_fifo->b_die
1139 && !p_vpar->p_fifo->b_error; i_dummy++ )
1141 RemoveBits( &p_vpar->bit_stream, 17 );
1142 RemoveBits( &p_vpar->bit_stream, 17 );
1147 /*****************************************************************************
1148 * PictureSpatialScalableExtension *
1149 *****************************************************************************/
1151 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1153 /* That's scalable, so we trash it */
1154 RemoveBits32( &p_vpar->bit_stream );
1155 RemoveBits( &p_vpar->bit_stream, 16 );
1159 /*****************************************************************************
1160 * PictureTemporalScalableExtension *
1161 *****************************************************************************/
1163 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1165 /* Scalable again, trashed again */
1166 RemoveBits( &p_vpar->bit_stream, 23 );
1170 /*****************************************************************************
1171 * CopyrightExtension : Keeps some legal informations *
1172 *****************************************************************************/
1174 static void CopyrightExtension( vpar_thread_t * p_vpar )
1176 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1177 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1178 /* A flag that says whether the copyright information is significant */
1179 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1180 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1181 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1183 RemoveBits( &p_vpar->bit_stream, 8 );
1184 /* The copyright_number is split in three parts */
1186 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1187 RemoveBits( &p_vpar->bit_stream, 1 );
1189 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1190 RemoveBits( &p_vpar->bit_stream, 1 );
1191 /* third part and sum */
1192 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1193 ( (u64)i_copyright_nb_2 << 22 ) |
1194 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );