1 /*****************************************************************************
2 * vpar_headers.c : headers parsing
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: headers.c,v 1.5 2002/11/20 13:37:35 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 *****************************************************************************/
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 );
60 /*****************************************************************************
61 * pi_default_intra_quant : default quantization matrix
62 *****************************************************************************/
63 u8 pi_default_intra_quant[] ATTR_ALIGN(16) =
65 8, 16, 19, 22, 26, 27, 29, 34,
66 16, 16, 22, 24, 27, 29, 34, 37,
67 19, 22, 26, 27, 29, 34, 34, 38,
68 22, 22, 26, 27, 29, 34, 37, 40,
69 22, 26, 27, 29, 32, 35, 40, 48,
70 26, 27, 29, 32, 35, 40, 48, 58,
71 26, 27, 29, 34, 38, 46, 56, 69,
72 27, 29, 35, 38, 46, 56, 69, 83
75 /*****************************************************************************
76 * pi_default_nonintra_quant : default quantization matrix
77 *****************************************************************************/
78 u8 pi_default_nonintra_quant[] ATTR_ALIGN(16) =
80 16, 16, 16, 16, 16, 16, 16, 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
90 /*****************************************************************************
91 * pi_scan : zig-zag and alternate scan patterns
92 *****************************************************************************/
93 u8 pi_scan[2][64] ATTR_ALIGN(16) =
95 { /* Zig-Zag pattern */
96 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,
97 12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,
98 35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,
99 58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63
101 { /* Alternate scan pattern */
102 0,8,16,24,1,9,2,10,17,25,32,40,48,56,57,49,
103 41,33,26,18,3,11,4,12,19,27,34,42,50,58,35,43,
104 51,59,20,28,5,13,6,14,21,29,36,44,52,60,37,45,
105 53,61,22,30,7,15,23,31,38,46,54,62,39,47,55,63
110 * Local inline functions.
113 /*****************************************************************************
114 * ReferenceUpdate : Update the reference pointers when we have a new picture
115 *****************************************************************************/
116 static void inline ReferenceUpdate( vpar_thread_t * p_vpar,
117 int i_coding_type, picture_t * p_newref )
119 if( i_coding_type != B_CODING_TYPE )
121 if( p_vpar->sequence.p_forward != NULL )
123 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_forward );
125 if( p_vpar->sequence.p_backward != NULL )
127 vout_DatePicture( p_vpar->p_vout, p_vpar->sequence.p_backward,
128 vpar_SynchroDate( p_vpar ) );
130 p_vpar->sequence.p_forward = p_vpar->sequence.p_backward;
131 p_vpar->sequence.p_backward = p_newref;
132 if( p_newref != NULL )
134 vout_LinkPicture( p_vpar->p_vout, p_newref );
137 else if( p_newref != NULL )
139 /* Put date immediately. */
140 vout_DatePicture( p_vpar->p_vout, p_newref, vpar_SynchroDate(p_vpar) );
144 /*****************************************************************************
145 * ReferenceReplace : Replace the last reference pointer when we destroy
147 *****************************************************************************/
148 static void inline ReferenceReplace( vpar_thread_t * p_vpar,
149 int i_coding_type, picture_t * p_newref )
151 if( i_coding_type != B_CODING_TYPE )
153 if( p_vpar->sequence.p_backward != NULL )
155 vout_UnlinkPicture( p_vpar->p_vout, p_vpar->sequence.p_backward );
157 p_vpar->sequence.p_backward = p_newref;
158 if( p_newref != NULL )
160 vout_LinkPicture( p_vpar->p_vout, p_newref );
165 /*****************************************************************************
166 * LoadMatrix : Load a quantization matrix
167 *****************************************************************************/
168 static inline void LoadMatrix( vpar_thread_t * p_vpar,
169 quant_matrix_t * p_matrix )
173 if( !p_matrix->b_allocated )
175 /* Allocate a piece of memory to load the matrix. */
176 if( (p_matrix->pi_matrix = (u8 *)malloc( 64*sizeof(u8) )) == NULL )
178 msg_Err( p_vpar->p_fifo, "out of memory" );
179 p_vpar->p_fifo->b_error = 1;
182 p_matrix->b_allocated = 1;
185 for( i_dummy = 0; i_dummy < 64; i_dummy++ )
187 p_matrix->pi_matrix[p_vpar->ppi_scan[0][i_dummy]]
188 = GetBits( &p_vpar->bit_stream, 8 );
192 /*****************************************************************************
193 * LinkMatrix : Link a quantization matrix to another
194 *****************************************************************************/
195 static inline void LinkMatrix( quant_matrix_t * p_matrix, u8 * pi_array )
197 if( p_matrix->b_allocated )
199 /* Deallocate the piece of memory. */
200 free( p_matrix->pi_matrix );
201 p_matrix->b_allocated = 0;
204 p_matrix->pi_matrix = pi_array;
207 /*****************************************************************************
208 * ChromaToFourCC: Return a FourCC value used by the video output.
209 *****************************************************************************/
210 static inline vlc_fourcc_t ChromaToFourCC( int i_chroma )
215 return VLC_FOURCC('I','4','2','0');
218 return VLC_FOURCC('I','4','2','2');
221 return VLC_FOURCC('I','4','4','4');
224 /* This can't happen */
230 * Exported functions.
233 /*****************************************************************************
234 * vpar_NextSequenceHeader : Find the next sequence header
235 *****************************************************************************/
236 int vpar_NextSequenceHeader( vpar_thread_t * p_vpar )
238 while( !p_vpar->p_fifo->b_die )
240 NextStartCode( &p_vpar->bit_stream );
241 if( ShowBits( &p_vpar->bit_stream, 32 ) == SEQUENCE_HEADER_CODE )
245 RemoveBits( &p_vpar->bit_stream, 8 );
250 /*****************************************************************************
251 * vpar_ParseHeader : Parse the next header
252 *****************************************************************************/
253 int vpar_ParseHeader( vpar_thread_t * p_vpar )
255 NextStartCode( &p_vpar->bit_stream );
256 switch( GetBits32( &p_vpar->bit_stream ) )
258 case SEQUENCE_HEADER_CODE:
259 p_vpar->c_sequences++;
260 SequenceHeader( p_vpar );
263 case GROUP_START_CODE:
264 GroupHeader( p_vpar );
267 case PICTURE_START_CODE:
268 PictureHeader( p_vpar );
271 case SEQUENCE_END_CODE:
272 msg_Dbg( p_vpar->p_fifo, "sequence end code received" );
273 if( p_vpar->sequence.p_backward != NULL )
275 p_vpar->sequence.p_backward->b_force = 1;
277 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
288 * Following functions are local
291 /*****************************************************************************
292 * SequenceHeader : Parse the next sequence header
293 *****************************************************************************/
294 static void SequenceHeader( vpar_thread_t * p_vpar )
297 static int i_frame_rate_table[16] =
308 RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED, RESERVED
314 vout_thread_t *p_vout;
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. */
482 p_vout = vlc_object_find( p_vpar->p_fifo, VLC_OBJECT_VOUT, FIND_CHILD );
485 p_vout = vlc_object_find( p_vpar->p_fifo, VLC_OBJECT_VOUT,
491 if( p_vout->render.i_width != p_vpar->sequence.i_width
492 || p_vout->render.i_height != p_vpar->sequence.i_height
493 || p_vout->render.i_chroma != ChromaToFourCC( p_vpar->sequence.i_chroma_format )
494 || p_vout->render.i_aspect != p_vpar->sequence.i_aspect )
496 /* We are not interested in this format, close this vout */
497 vlc_object_detach( p_vout );
498 vlc_object_release( p_vout );
499 vout_DestroyThread( p_vout );
504 /* This video output is cool! Hijack it. */
505 if( p_vout != p_vpar->p_vout )
507 vlc_object_detach( p_vout );
508 vlc_object_attach( p_vout, p_vpar->p_fifo );
510 vlc_object_release( p_vout );
514 p_vpar->p_vout = p_vout;
516 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
521 if( p_vpar->p_vout == NULL )
523 msg_Dbg( p_vpar->p_fifo, "no vout present, spawning one" );
525 p_vpar->p_vout = vout_CreateThread( p_vpar->p_fifo,
526 p_vpar->sequence.i_width,
527 p_vpar->sequence.i_height,
528 ChromaToFourCC( p_vpar->sequence.i_chroma_format ),
529 p_vpar->sequence.i_aspect );
531 /* Everything failed */
532 if( p_vpar->p_vout == NULL )
534 msg_Err( p_vpar->p_fifo, "cannot open vout, aborting" );
535 p_vpar->p_fifo->b_error = 1;
541 /*****************************************************************************
542 * GroupHeader : Parse the next group of pictures header
543 *****************************************************************************/
544 static void GroupHeader( vpar_thread_t * p_vpar )
546 /* Nothing to do, we don't care. */
547 RemoveBits( &p_vpar->bit_stream, 27 );
548 ExtensionAndUserData( p_vpar );
551 /*****************************************************************************
552 * PictureHeader : Parse the next picture header
553 *****************************************************************************/
554 static void PictureHeader( vpar_thread_t * p_vpar )
556 int i_structure, i_previous_coding_type;
557 vlc_bool_t b_parsable = 0;
559 /* Retrieve the PTS. */
560 CurrentPTS( &p_vpar->bit_stream, &p_vpar->sequence.next_pts,
561 &p_vpar->sequence.next_dts );
563 /* Recover in case of stream discontinuity. */
564 if( p_vpar->sequence.b_expect_discontinuity )
566 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
567 ReferenceUpdate( p_vpar, I_CODING_TYPE, NULL );
568 if( p_vpar->picture.p_picture != NULL )
570 vout_DestroyPicture( p_vpar->p_vout, p_vpar->picture.p_picture );
571 p_vpar->picture.p_picture = NULL;
573 p_vpar->picture.i_current_structure = 0;
574 p_vpar->sequence.b_expect_discontinuity = 0;
577 /* Parse the picture header. */
578 RemoveBits( &p_vpar->bit_stream, 10 ); /* temporal_reference */
579 i_previous_coding_type = p_vpar->picture.i_coding_type;
580 p_vpar->picture.i_coding_type = GetBits( &p_vpar->bit_stream, 3 );
581 RemoveBits( &p_vpar->bit_stream, 16 ); /* vbv_delay */
583 if( p_vpar->picture.i_coding_type == P_CODING_TYPE
584 || p_vpar->picture.i_coding_type == B_CODING_TYPE )
586 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 1 );
587 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 3 )
590 if( p_vpar->picture.i_coding_type == B_CODING_TYPE )
592 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 1 );
593 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 3 )
597 /* extra_information_picture */
598 while( GetBits( &p_vpar->bit_stream, 1 ) )
600 RemoveBits( &p_vpar->bit_stream, 8 );
604 * Picture Coding Extension
606 NextStartCode( &p_vpar->bit_stream );
607 if( ShowBits( &p_vpar->bit_stream, 32 ) == EXTENSION_START_CODE )
609 /* Parse picture_coding_extension */
610 RemoveBits32( &p_vpar->bit_stream );
611 /* extension_start_code_identifier */
612 RemoveBits( &p_vpar->bit_stream, 4 );
614 /* Pre-substract 1 for later use in MotionDelta(). */
615 p_vpar->picture.ppi_f_code[0][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
616 p_vpar->picture.ppi_f_code[0][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
617 p_vpar->picture.ppi_f_code[1][0] = GetBits( &p_vpar->bit_stream, 4 ) -1;
618 p_vpar->picture.ppi_f_code[1][1] = GetBits( &p_vpar->bit_stream, 4 ) -1;
619 p_vpar->picture.i_intra_dc_precision = GetBits( &p_vpar->bit_stream, 2 );
620 i_structure = GetBits( &p_vpar->bit_stream, 2 );
621 p_vpar->picture.b_top_field_first = GetBits( &p_vpar->bit_stream, 1 );
622 p_vpar->picture.b_frame_pred_frame_dct
623 = GetBits( &p_vpar->bit_stream, 1 );
624 p_vpar->picture.b_concealment_mv = GetBits( &p_vpar->bit_stream, 1 );
625 p_vpar->picture.b_q_scale_type = GetBits( &p_vpar->bit_stream, 1 );
626 p_vpar->picture.b_intra_vlc_format = GetBits( &p_vpar->bit_stream, 1 );
628 p_vpar->picture.pi_scan =
629 p_vpar->ppi_scan[ GetBits( &p_vpar->bit_stream, 1 ) ];
630 p_vpar->picture.b_repeat_first_field = GetBits( &p_vpar->bit_stream, 1 );
631 /* chroma_420_type (obsolete) */
632 RemoveBits( &p_vpar->bit_stream, 1 );
633 p_vpar->picture.b_progressive = GetBits( &p_vpar->bit_stream, 1 );
635 /* composite_display_flag */
636 if( GetBits( &p_vpar->bit_stream, 1 ) )
638 /* v_axis, field_sequence, sub_carrier, burst_amplitude,
639 * sub_carrier_phase */
640 RemoveBits( &p_vpar->bit_stream, 20 );
645 /* MPEG-1 compatibility flags */
646 p_vpar->picture.i_intra_dc_precision = 0; /* 8 bits */
647 i_structure = FRAME_STRUCTURE;
648 p_vpar->picture.b_top_field_first = 0;
649 p_vpar->picture.b_frame_pred_frame_dct = 1;
650 p_vpar->picture.b_concealment_mv = 0;
651 p_vpar->picture.b_q_scale_type = 0;
652 p_vpar->picture.b_intra_vlc_format = 0;
653 p_vpar->picture.pi_scan = p_vpar->ppi_scan[0];
654 p_vpar->picture.b_repeat_first_field = 0;
655 p_vpar->picture.b_progressive = 1;
658 /* Extension and User data. */
659 ExtensionAndUserData( p_vpar );
661 p_vpar->pc_pictures[p_vpar->picture.i_coding_type]++;
663 if( p_vpar->picture.i_current_structure )
665 if ( (i_structure == FRAME_STRUCTURE ||
666 i_structure == p_vpar->picture.i_current_structure) )
668 /* We don't have the second field of the buffered frame. */
669 if( p_vpar->picture.p_picture != NULL )
671 ReferenceReplace( p_vpar,
672 p_vpar->picture.i_coding_type,
674 vout_DestroyPicture( p_vpar->p_vout,
675 p_vpar->picture.p_picture );
676 p_vpar->picture.p_picture = NULL;
679 p_vpar->picture.i_current_structure = 0;
681 msg_Warn( p_vpar->p_fifo, "odd number of field pictures" );
685 /* Second field of a frame. We will decode it if, and only if we
686 * have decoded the first field. */
687 if( p_vpar->picture.p_picture == NULL )
689 if( (p_vpar->picture.i_coding_type == I_CODING_TYPE
690 && p_vpar->sequence.p_backward == NULL) )
692 /* Exceptionnally, parse the picture if it is I. We need
693 * this in case of an odd number of field pictures, if the
694 * previous picture is not intra, we desperately need a
695 * new reference picture. OK, this is kind of kludgy. */
696 p_vpar->picture.i_current_structure = 0;
705 /* We suppose we have the reference pictures, since we already
706 * decoded the first field and the second field will not need
707 * any extra reference picture. There is a special case of
708 * P field being the second field of an I field, but ISO/IEC
709 * 13818-2 section 7.6.3.5 specifies that this P field will
710 * not need any reference picture besides the I field. So far
714 if( p_vpar->picture.i_coding_type == P_CODING_TYPE &&
715 i_previous_coding_type == I_CODING_TYPE &&
716 p_vpar->sequence.p_forward == NULL )
718 /* This is the special case of section 7.6.3.5. Create
719 * a fake reference picture (which will not be used)
720 * but will prevent us from segfaulting in the slice
722 static picture_t fake_picture;
723 fake_picture.i_planes = 0; /* We will use it later */
724 p_vpar->sequence.p_forward = &fake_picture;
730 if( !p_vpar->picture.i_current_structure )
732 /* First field of a frame, or new frame picture. */
735 /* Do we have the reference pictures ? */
736 b_parsable = !(((p_vpar->picture.i_coding_type == P_CODING_TYPE ||
737 p_vpar->picture.b_concealment_mv) &&
738 (p_vpar->sequence.p_backward == NULL)) ||
739 /* p_backward will become p_forward later */
740 ((p_vpar->picture.i_coding_type == B_CODING_TYPE) &&
741 (p_vpar->sequence.p_forward == NULL ||
742 p_vpar->sequence.p_backward == NULL)));
744 /* Compute the number of times the frame will be emitted by the
745 * decoder (number of half-periods). */
746 if( p_vpar->sequence.b_progressive )
748 i_repeat_field = (1 + p_vpar->picture.b_repeat_first_field
749 + p_vpar->picture.b_top_field_first) * 2;
753 if( p_vpar->picture.b_progressive )
755 i_repeat_field = 2 + p_vpar->picture.b_repeat_first_field;
763 /* Warn synchro we have a new picture (updates pictures index). */
764 vpar_SynchroNewPicture( p_vpar, p_vpar->picture.i_coding_type,
769 /* Does synchro say we have enough time to decode it ? */
770 b_parsable = vpar_SynchroChoose( p_vpar,
771 p_vpar->picture.i_coding_type, i_structure );
777 /* Update the reference pointers. */
778 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, NULL );
780 /* Update context. */
781 if( i_structure != FRAME_STRUCTURE )
783 if( (p_vpar->picture.i_current_structure | i_structure)
786 /* The frame is complete. */
787 p_vpar->picture.i_current_structure = 0;
789 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
793 p_vpar->picture.i_current_structure = i_structure;
798 /* Warn Synchro we have trashed a picture. */
799 vpar_SynchroTrash( p_vpar, p_vpar->picture.i_coding_type, i_structure );
801 p_vpar->picture.p_picture = NULL;
806 /* OK, now we are sure we will decode the picture. */
807 p_vpar->pc_decoded_pictures[p_vpar->picture.i_coding_type]++;
809 #define P_picture p_vpar->picture.p_picture
810 p_vpar->picture.b_error = 0;
811 p_vpar->picture.b_frame_structure = (i_structure == FRAME_STRUCTURE);
813 if( !p_vpar->picture.i_current_structure )
815 /* This is a new frame. Get a structure from the video_output. */
816 while( ( P_picture = vout_CreatePicture( p_vpar->p_vout,
817 p_vpar->picture.b_progressive,
818 p_vpar->picture.b_top_field_first,
819 p_vpar->picture.b_repeat_first_field ) )
822 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
826 msleep( VOUT_OUTMEM_SLEEP );
829 /* Initialize values. */
830 vpar_SynchroDecode( p_vpar, p_vpar->picture.i_coding_type, i_structure );
831 P_picture->i_matrix_coefficients = p_vpar->sequence.i_matrix_coefficients;
833 /* Update the reference pointers. */
834 ReferenceUpdate( p_vpar, p_vpar->picture.i_coding_type, P_picture );
837 /* Initialize picture data for decoding. */
838 p_vpar->picture.i_current_structure |= i_structure;
839 p_vpar->picture.i_structure = i_structure;
840 p_vpar->picture.b_second_field =
841 (i_structure != p_vpar->picture.i_current_structure);
842 p_vpar->picture.b_current_field =
843 (i_structure == BOTTOM_FIELD );
844 p_vpar->picture.i_lum_stride = p_vpar->picture.p_picture->Y_PITCH
845 << ( 1 - p_vpar->picture.b_frame_structure );
846 p_vpar->picture.i_chrom_stride = p_vpar->picture.p_picture->U_PITCH
847 << ( 1 - p_vpar->picture.b_frame_structure );
848 /* We suppose the pitch is the same for U and V planes. */
849 p_vpar->picture.i_field_width = p_vpar->sequence.i_width
850 << ( 1 - p_vpar->picture.b_frame_structure );
852 if( !p_vpar->p_fifo->p_stream_ctrl->b_grayscale )
854 switch( p_vpar->sequence.i_chroma_format )
857 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock422;
860 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock444;
864 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblock420;
870 p_vpar->pool.pf_vdec_decode = vdec_DecodeMacroblockBW;
874 if( p_vpar->sequence.b_mpeg2 )
876 static f_picture_data_t ppf_picture_data[4][4] =
879 NULL, NULL, NULL, NULL
883 #if (VPAR_OPTIM_LEVEL > 1)
884 NULL, vpar_PictureData2IT, vpar_PictureData2PT,
887 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
888 vpar_PictureDataGENERIC
893 #if (VPAR_OPTIM_LEVEL > 1)
894 NULL, vpar_PictureData2IB, vpar_PictureData2PB,
897 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
898 vpar_PictureDataGENERIC
903 #if (VPAR_OPTIM_LEVEL > 0)
904 NULL, vpar_PictureData2IF, vpar_PictureData2PF,
907 NULL, vpar_PictureDataGENERIC, vpar_PictureDataGENERIC,
908 vpar_PictureDataGENERIC
913 ppf_picture_data[p_vpar->picture.i_structure]
914 [p_vpar->picture.i_coding_type]( p_vpar );
918 #if (VPAR_OPTIM_LEVEL > 1)
919 static f_picture_data_t pf_picture_data[5] =
920 { NULL, vpar_PictureData1I, vpar_PictureData1P, vpar_PictureData1B,
921 vpar_PictureData1D };
923 pf_picture_data[p_vpar->picture.i_coding_type]( p_vpar );
925 vpar_PictureDataGENERIC( p_vpar );
929 /* Wait for all the macroblocks to be decoded. */
930 p_vpar->pool.pf_wait_pool( &p_vpar->pool );
932 /* Re-spawn decoder threads if the user changed settings. */
933 vpar_SpawnPool( p_vpar );
935 if( p_vpar->p_fifo->b_die || p_vpar->p_fifo->b_error )
940 if( p_vpar->sequence.p_forward != NULL &&
941 p_vpar->sequence.p_forward->i_planes == 0 )
943 /* This can only happen with the fake picture created for section
944 * 7.6.3.5. Clean up our mess. */
945 p_vpar->sequence.p_forward = NULL;
948 if( p_vpar->picture.b_error )
951 p_vpar->pc_malformed_pictures[p_vpar->picture.i_coding_type]++;
953 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
954 p_vpar->picture.i_structure, 1 );
955 vout_DestroyPicture( p_vpar->p_vout, P_picture );
957 ReferenceReplace( p_vpar, p_vpar->picture.i_coding_type, NULL );
959 /* Prepare context for the next picture. */
961 if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
962 p_vpar->picture.i_current_structure = 0;
964 else if( p_vpar->picture.i_current_structure == FRAME_STRUCTURE )
966 /* Frame completely parsed. */
967 vpar_SynchroEnd( p_vpar, p_vpar->picture.i_coding_type,
968 p_vpar->picture.i_structure, 0 );
969 vout_DisplayPicture( p_vpar->p_vout, P_picture );
971 /* Prepare context for the next picture. */
973 p_vpar->picture.i_current_structure = 0;
978 /*****************************************************************************
979 * ExtensionAndUserData : Parse the extension_and_user_data structure
980 *****************************************************************************/
981 static void ExtensionAndUserData( vpar_thread_t * p_vpar )
983 while( !p_vpar->p_fifo->b_die )
985 NextStartCode( &p_vpar->bit_stream );
986 switch( ShowBits( &p_vpar->bit_stream, 32 ) )
988 case EXTENSION_START_CODE:
989 RemoveBits32( &p_vpar->bit_stream );
990 switch( GetBits( &p_vpar->bit_stream, 4 ) )
992 case SEQUENCE_DISPLAY_EXTENSION_ID:
993 SequenceDisplayExtension( p_vpar );
995 case QUANT_MATRIX_EXTENSION_ID:
996 QuantMatrixExtension( p_vpar );
998 case SEQUENCE_SCALABLE_EXTENSION_ID:
999 SequenceScalableExtension( p_vpar );
1001 case PICTURE_DISPLAY_EXTENSION_ID:
1002 PictureDisplayExtension( p_vpar );
1004 case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
1005 PictureSpatialScalableExtension( p_vpar );
1007 case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
1008 PictureTemporalScalableExtension( p_vpar );
1010 case COPYRIGHT_EXTENSION_ID:
1011 CopyrightExtension( p_vpar );
1018 case USER_DATA_START_CODE:
1019 RemoveBits32( &p_vpar->bit_stream );
1020 /* Wait for the next start code */
1030 /*****************************************************************************
1031 * SequenceDisplayExtension : Parse the sequence_display_extension structure *
1032 *****************************************************************************/
1034 static void SequenceDisplayExtension( vpar_thread_t * p_vpar )
1036 /* We don't care sequence_display_extension. */
1038 RemoveBits( &p_vpar->bit_stream, 3 );
1039 if( GetBits( &p_vpar->bit_stream, 1 ) )
1041 /* Two bytes for color_desciption */
1042 RemoveBits( &p_vpar->bit_stream, 16 );
1043 p_vpar->sequence.i_matrix_coefficients = GetBits( &p_vpar->bit_stream, 8 );
1045 /* display_horizontal and vertical_size and a marker_bit */
1046 RemoveBits( &p_vpar->bit_stream, 29 );
1050 /*****************************************************************************
1051 * QuantMatrixExtension : Load quantization matrices for luminance *
1053 *****************************************************************************/
1055 static void QuantMatrixExtension( vpar_thread_t * p_vpar )
1057 if( GetBits( &p_vpar->bit_stream, 1 ) )
1059 /* Load intra_quantiser_matrix for luminance. */
1060 LoadMatrix( p_vpar, &p_vpar->sequence.intra_quant );
1064 /* Use the default matrix. */
1065 LinkMatrix( &p_vpar->sequence.intra_quant,
1066 p_vpar->pi_default_intra_quant );
1068 if( GetBits( &p_vpar->bit_stream, 1 ) )
1070 /* Load non_intra_quantiser_matrix for luminance. */
1071 LoadMatrix( p_vpar, &p_vpar->sequence.nonintra_quant );
1075 /* Use the default matrix. */
1076 LinkMatrix( &p_vpar->sequence.nonintra_quant,
1077 p_vpar->pi_default_nonintra_quant );
1079 if( GetBits( &p_vpar->bit_stream, 1 ) )
1081 /* Load intra_quantiser_matrix for chrominance. */
1082 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_intra_quant );
1086 /* Link the chrominance intra matrix to the luminance one. */
1087 LinkMatrix( &p_vpar->sequence.chroma_intra_quant,
1088 p_vpar->sequence.intra_quant.pi_matrix );
1090 if( GetBits( &p_vpar->bit_stream, 1 ) )
1092 /* Load non_intra_quantiser_matrix for chrominance. */
1093 LoadMatrix( p_vpar, &p_vpar->sequence.chroma_nonintra_quant );
1097 /* Link the chrominance nonintra matrix to the luminance one. */
1098 LinkMatrix( &p_vpar->sequence.chroma_nonintra_quant,
1099 p_vpar->sequence.nonintra_quant.pi_matrix );
1104 /*****************************************************************************
1105 * SequenceScalableExtension : Parse the sequence_scalable_extension *
1106 * structure to handle scalable coding *
1107 *****************************************************************************/
1109 static void SequenceScalableExtension( vpar_thread_t * p_vpar )
1111 /* We don't care about anything scalable except the scalable mode. */
1112 switch( p_vpar->sequence.i_scalable_mode = GetBits( &p_vpar->bit_stream, 2 ) )
1113 /* The length of the structure depends on the value of the scalable_mode */
1116 RemoveBits32( &p_vpar->bit_stream );
1117 RemoveBits( &p_vpar->bit_stream, 21 );
1120 RemoveBits( &p_vpar->bit_stream, 12 );
1123 RemoveBits( &p_vpar->bit_stream, 4 );
1127 /*****************************************************************************
1128 * PictureDisplayExtension : Parse the picture_display_extension structure *
1129 *****************************************************************************/
1131 static void PictureDisplayExtension( vpar_thread_t * p_vpar )
1133 /* Number of frame center offset */
1135 /* I am not sure it works but it should
1136 (fewer tests than shown in §6.3.12) */
1137 i_nb = p_vpar->sequence.b_progressive ? p_vpar->sequence.b_progressive +
1138 p_vpar->picture.b_repeat_first_field +
1139 p_vpar->picture.b_top_field_first
1140 : ( p_vpar->picture.b_frame_structure + 1 ) +
1141 p_vpar->picture.b_repeat_first_field;
1142 for( i_dummy = 0; i_dummy < i_nb; i_dummy++ )
1144 RemoveBits( &p_vpar->bit_stream, 17 );
1145 RemoveBits( &p_vpar->bit_stream, 17 );
1150 /*****************************************************************************
1151 * PictureSpatialScalableExtension *
1152 *****************************************************************************/
1154 static void PictureSpatialScalableExtension( vpar_thread_t * p_vpar )
1156 /* That's scalable, so we trash it */
1157 RemoveBits32( &p_vpar->bit_stream );
1158 RemoveBits( &p_vpar->bit_stream, 16 );
1162 /*****************************************************************************
1163 * PictureTemporalScalableExtension *
1164 *****************************************************************************/
1166 static void PictureTemporalScalableExtension( vpar_thread_t * p_vpar )
1168 /* Scalable again, trashed again */
1169 RemoveBits( &p_vpar->bit_stream, 23 );
1173 /*****************************************************************************
1174 * CopyrightExtension : Keeps some legal informations *
1175 *****************************************************************************/
1177 static void CopyrightExtension( vpar_thread_t * p_vpar )
1179 u32 i_copyright_nb_1, i_copyright_nb_2; /* local integers */
1180 p_vpar->sequence.b_copyright_flag = GetBits( &p_vpar->bit_stream, 1 );
1181 /* A flag that says whether the copyright information is significant */
1182 p_vpar->sequence.i_copyright_id = GetBits( &p_vpar->bit_stream, 8 );
1183 /* An identifier compliant with ISO/CEI JTC 1/SC 29 */
1184 p_vpar->sequence.b_original = GetBits( &p_vpar->bit_stream, 1 );
1186 RemoveBits( &p_vpar->bit_stream, 8 );
1187 /* The copyright_number is split in three parts */
1189 i_copyright_nb_1 = GetBits( &p_vpar->bit_stream, 20 );
1190 RemoveBits( &p_vpar->bit_stream, 1 );
1192 i_copyright_nb_2 = GetBits( &p_vpar->bit_stream, 22 );
1193 RemoveBits( &p_vpar->bit_stream, 1 );
1194 /* third part and sum */
1195 p_vpar->sequence.i_copyright_nb = ( (u64)i_copyright_nb_1 << 44 ) |
1196 ( (u64)i_copyright_nb_2 << 22 ) |
1197 ( (u64)GetBits( &p_vpar->bit_stream, 22 ) );