demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / codec / libmpeg2.c
blob2cbc96cf084c4f9370a82e61df5e251ccc95fa08
1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Christophe Massiot <massiot@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * NOTA BENE: this module requires the linking against a library which is
27 * known to require licensing under the GNU General Public License version 2
28 * (or later). Therefore, the result of compiling this module will normally
29 * be subject to the terms of that later license.
30 *****************************************************************************/
33 /*****************************************************************************
34 * Preamble
35 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39 #include <assert.h>
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_codec.h>
44 #include <vlc_block_helper.h>
45 #include <vlc_cpu.h>
46 #include "cc.h"
47 #include "synchro.h"
49 #include <mpeg2.h>
51 /*****************************************************************************
52 * decoder_sys_t : libmpeg2 decoder descriptor
53 *****************************************************************************/
54 #define DPB_COUNT (3+1)
55 typedef struct
57 picture_t *p_picture;
58 bool b_linked;
59 bool b_displayed;
60 } picture_dpb_t;
62 struct decoder_sys_t
65 * libmpeg2 properties
67 mpeg2dec_t *p_mpeg2dec;
68 const mpeg2_info_t *p_info;
69 bool b_skip;
72 * Input properties
74 mtime_t i_previous_pts;
75 mtime_t i_current_pts;
76 mtime_t i_previous_dts;
77 mtime_t i_current_dts;
78 bool b_garbage_pic;
79 bool b_after_sequence_header; /* is it the next frame after
80 * the sequence header ? */
81 bool b_slice_i; /* intra-slice refresh stream */
82 bool b_second_field;
84 bool b_preroll;
86 /* */
87 picture_dpb_t p_dpb[DPB_COUNT];
90 * Output properties
92 decoder_synchro_t *p_synchro;
93 int i_sar_num;
94 int i_sar_den;
95 mtime_t i_last_frame_pts;
97 /* Closed captioning support */
98 uint32_t i_cc_flags;
99 mtime_t i_cc_pts;
100 mtime_t i_cc_dts;
101 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
102 cc_data_t cc;
103 #endif
104 uint8_t *p_gop_user_data;
105 uint32_t i_gop_user_data;
108 /*****************************************************************************
109 * Local prototypes
110 *****************************************************************************/
111 static int OpenDecoder( vlc_object_t * );
112 static void CloseDecoder( vlc_object_t * );
114 static int DecodeVideo( decoder_t *, block_t *);
115 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
116 static void SendCc( decoder_t *p_dec );
117 #endif
119 static picture_t *GetNewPicture( decoder_t * );
120 static void PutPicture( decoder_t *, picture_t * );
122 static void GetAR( decoder_t *p_dec );
124 static void Reset( decoder_t *p_dec );
126 /* */
127 static void DpbInit( decoder_t * );
128 static void DpbClean( decoder_t * );
129 static picture_t *DpbNewPicture( decoder_t * );
130 static void DpbUnlinkPicture( decoder_t *, picture_t * );
131 static int DpbDisplayPicture( decoder_t *, picture_t * );
133 /*****************************************************************************
134 * Module descriptor
135 *****************************************************************************/
136 vlc_module_begin ()
137 set_description( N_("MPEG I/II video decoder (using libmpeg2)") )
138 set_capability( "video decoder", 50 )
139 set_category( CAT_INPUT )
140 set_subcategory( SUBCAT_INPUT_VCODEC )
141 set_callbacks( OpenDecoder, CloseDecoder )
142 add_shortcut( "libmpeg2" )
143 vlc_module_end ()
145 /*****************************************************************************
146 * OpenDecoder: probe the decoder and return score
147 *****************************************************************************/
148 static int OpenDecoder( vlc_object_t *p_this )
150 decoder_t *p_dec = (decoder_t*)p_this;
151 decoder_sys_t *p_sys;
152 uint32_t i_accel = 0;
154 if( p_dec->fmt_in.i_codec != VLC_CODEC_MPGV )
155 return VLC_EGENERIC;
157 /* Select onl recognized original format (standard mpeg video) */
158 switch( p_dec->fmt_in.i_original_fourcc )
160 case VLC_FOURCC('m','p','g','1'):
161 case VLC_FOURCC('m','p','g','2'):
162 case VLC_FOURCC('m','p','g','v'):
163 case VLC_FOURCC('P','I','M','1'):
164 case VLC_FOURCC('h','d','v','2'):
165 break;
166 default:
167 if( p_dec->fmt_in.i_original_fourcc )
168 return VLC_EGENERIC;
169 break;
172 /* Allocate the memory needed to store the decoder's structure */
173 if( ( p_dec->p_sys = p_sys = calloc( 1, sizeof(*p_sys)) ) == NULL )
174 return VLC_ENOMEM;
176 /* Initialize the thread properties */
177 p_sys->p_mpeg2dec = NULL;
178 p_sys->p_synchro = NULL;
179 p_sys->p_info = NULL;
180 p_sys->i_current_pts = 0;
181 p_sys->i_previous_pts = 0;
182 p_sys->i_current_dts = 0;
183 p_sys->i_previous_dts = 0;
184 p_sys->i_sar_num = 0;
185 p_sys->i_sar_den = 0;
186 p_sys->b_garbage_pic = false;
187 p_sys->b_slice_i = false;
188 p_sys->b_second_field = false;
189 p_sys->b_skip = false;
190 p_sys->b_preroll = false;
191 DpbInit( p_dec );
193 p_sys->i_cc_pts = 0;
194 p_sys->i_cc_dts = 0;
195 p_sys->i_cc_flags = 0;
196 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
197 cc_Init( &p_sys->cc );
198 #endif
199 p_sys->p_gop_user_data = NULL;
200 p_sys->i_gop_user_data = 0;
202 #if defined( __i386__ ) || defined( __x86_64__ )
203 if( vlc_CPU_MMX() )
204 i_accel |= MPEG2_ACCEL_X86_MMX;
205 if( vlc_CPU_3dNOW() )
206 i_accel |= MPEG2_ACCEL_X86_3DNOW;
207 if( vlc_CPU_MMXEXT() )
208 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
209 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
210 if( vlc_CPU_ALTIVEC() )
211 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
213 #elif defined(__arm__)
214 # ifdef MPEG2_ACCEL_ARM
215 i_accel |= MPEG2_ACCEL_ARM;
216 # endif
217 # ifdef MPEG2_ACCEL_ARM_NEON
218 if( vlc_CPU_ARM_NEON() )
219 i_accel |= MPEG2_ACCEL_ARM_NEON;
220 # endif
222 /* TODO: sparc */
223 #else
224 /* If we do not know this CPU, trust libmpeg2's feature detection */
225 i_accel = MPEG2_ACCEL_DETECT;
227 #endif
229 /* Set CPU acceleration features */
230 mpeg2_accel( i_accel );
232 /* Initialize decoder */
233 p_sys->p_mpeg2dec = mpeg2_init();
234 if( p_sys->p_mpeg2dec == NULL)
236 msg_Err( p_dec, "mpeg2_init() failed" );
237 free( p_sys );
238 return VLC_EGENERIC;
241 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
243 p_dec->pf_decode = DecodeVideo;
244 p_dec->pf_flush = Reset;
245 p_dec->fmt_out.i_codec = 0;
247 return VLC_SUCCESS;
250 /*****************************************************************************
251 * RunDecoder: the libmpeg2 decoder
252 *****************************************************************************/
253 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
255 decoder_sys_t *p_sys = p_dec->p_sys;
256 mpeg2_state_t state;
258 block_t *p_block;
260 if( !pp_block || !*pp_block )
261 return NULL;
263 p_block = *pp_block;
264 if( p_block->i_flags & (BLOCK_FLAG_CORRUPTED) )
265 Reset( p_dec );
267 while( 1 )
269 state = mpeg2_parse( p_sys->p_mpeg2dec );
271 switch( state )
273 case STATE_SEQUENCE:
275 /* */
276 DpbClean( p_dec );
278 /* */
279 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
281 if( p_sys->p_synchro )
282 decoder_SynchroRelease( p_sys->p_synchro );
284 if( p_sys->p_info->sequence->frame_period <= 0 )
285 p_sys->p_synchro = NULL;
286 else
287 p_sys->p_synchro =
288 decoder_SynchroInit( p_dec, (uint32_t)(UINT64_C(1001000000) *
289 27 / p_sys->p_info->sequence->frame_period) );
290 p_sys->b_after_sequence_header = true;
292 /* Set the first 2 reference frames */
293 GetAR( p_dec );
294 for( int i = 0; i < 2; i++ )
296 picture_t *p_picture = DpbNewPicture( p_dec );
297 if( !p_picture )
299 Reset( p_dec );
300 block_Release( p_block );
301 return NULL;
303 PutPicture( p_dec, p_picture );
305 break;
308 case STATE_GOP:
309 /* There can be userdata in a GOP. It needs to be remembered for the next picture. */
310 if( p_sys->p_info->user_data_len > 2 )
312 free( p_sys->p_gop_user_data );
313 p_sys->p_gop_user_data = calloc( p_sys->p_info->user_data_len, sizeof(uint8_t) );
314 if( p_sys->p_gop_user_data )
316 p_sys->i_gop_user_data = p_sys->p_info->user_data_len;
317 memcpy( p_sys->p_gop_user_data, p_sys->p_info->user_data, p_sys->p_info->user_data_len );
320 break;
322 case STATE_PICTURE:
324 const mpeg2_info_t *p_info = p_sys->p_info;
325 const mpeg2_picture_t *p_current = p_info->current_picture;
327 mtime_t i_pts, i_dts;
329 if( p_sys->b_after_sequence_header &&
330 (p_current->flags &
331 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
333 /* Intra-slice refresh. Simulate a blank I picture. */
334 msg_Dbg( p_dec, "intra-slice refresh stream" );
335 decoder_SynchroNewPicture( p_sys->p_synchro,
336 I_CODING_TYPE, 2, 0, 0,
337 p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
338 decoder_SynchroDecode( p_sys->p_synchro );
339 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
340 p_sys->b_slice_i = true;
342 p_sys->b_after_sequence_header = false;
344 #ifdef PIC_FLAG_PTS
345 i_pts = p_current->flags & PIC_FLAG_PTS ?
346 ( ( p_current->pts ==
347 (uint32_t)p_sys->i_current_pts ) ?
348 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
349 i_dts = 0;
351 /* Hack to handle demuxers which only have DTS timestamps */
352 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
354 if( p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
355 (p_current->flags &
356 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
358 i_pts = p_block->i_dts;
361 p_block->i_pts = p_block->i_dts = 0;
362 /* End hack */
364 #else /* New interface */
366 i_pts = p_current->flags & PIC_FLAG_TAGS ?
367 ( ( p_current->tag == (uint32_t)p_sys->i_current_pts ) ?
368 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
369 i_dts = p_current->flags & PIC_FLAG_TAGS ?
370 ( ( p_current->tag2 == (uint32_t)p_sys->i_current_dts ) ?
371 p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
372 #endif
374 /* If nb_fields == 1, it is a field picture, and it will be
375 * followed by another field picture for which we won't call
376 * decoder_SynchroNewPicture() because this would have other
377 * problems, so we take it into account here.
378 * This kind of sucks, but I didn't think better. --Meuuh
380 decoder_SynchroNewPicture( p_sys->p_synchro,
381 p_current->flags & PIC_MASK_CODING_TYPE,
382 p_current->nb_fields == 1 ? 2 :
383 p_current->nb_fields, i_pts, i_dts,
384 p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
387 picture_t *p_pic;
389 if( p_dec->b_frame_drop_allowed && !p_sys->b_preroll &&
390 !(p_sys->b_slice_i
391 && ((p_current->flags
392 & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P))
393 && !decoder_SynchroChoose( p_sys->p_synchro,
394 p_current->flags
395 & PIC_MASK_CODING_TYPE,
396 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
397 p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
398 p_pic = NULL;
399 else
401 p_pic = DpbNewPicture( p_dec );
402 if( !p_pic )
404 Reset( p_dec );
406 p_pic = DpbNewPicture( p_dec );
407 if( !p_pic )
409 mpeg2_reset( p_sys->p_mpeg2dec, 1 );
410 block_Release( p_block );
411 return NULL;
416 mpeg2_skip( p_sys->p_mpeg2dec, p_pic == NULL );
417 p_sys->b_skip = p_pic == NULL;
418 if( p_pic != NULL )
419 decoder_SynchroDecode( p_sys->p_synchro );
420 else
421 decoder_SynchroTrash( p_sys->p_synchro );
423 PutPicture( p_dec, p_pic );
425 if( p_info->user_data_len > 2 || p_sys->i_gop_user_data > 2 )
427 p_sys->i_cc_pts = i_pts;
428 p_sys->i_cc_dts = i_dts;
429 if( (p_current->flags
430 & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P )
431 p_sys->i_cc_flags = BLOCK_FLAG_TYPE_P;
432 else if( (p_current->flags
433 & PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
434 p_sys->i_cc_flags = BLOCK_FLAG_TYPE_B;
435 else p_sys->i_cc_flags = BLOCK_FLAG_TYPE_I;
436 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
437 bool b_top_field_first = p_sys->p_info->current_picture->flags
438 & PIC_FLAG_TOP_FIELD_FIRST;
439 #endif
440 if( p_sys->i_gop_user_data > 2 )
442 /* We now have picture info for any cached user_data out of the gop */
443 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
444 cc_ProbeAndExtract( &p_sys->cc, b_top_field_first,
445 &p_sys->p_gop_user_data[0], p_sys->i_gop_user_data );
446 #endif
447 p_sys->i_gop_user_data = 0;
450 /* Extract the CC from the user_data of the picture */
451 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
452 if( p_info->user_data_len > 2 )
453 cc_ProbeAndExtract( &p_sys->cc, b_top_field_first,
454 &p_info->user_data[0], p_info->user_data_len );
456 if( p_sys->cc.i_data )
457 SendCc( p_dec );
458 #endif
461 break;
464 case STATE_BUFFER:
465 if( !p_block->i_buffer )
467 block_Release( p_block );
468 return NULL;
471 if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
472 | BLOCK_FLAG_CORRUPTED)) &&
473 p_sys->p_synchro &&
474 p_sys->p_info->sequence &&
475 p_sys->p_info->sequence->width != (unsigned)-1 )
477 decoder_SynchroReset( p_sys->p_synchro );
478 if( p_sys->p_info->current_fbuf != NULL &&
479 p_sys->p_info->current_fbuf->id != NULL )
481 p_sys->b_garbage_pic = true;
483 if( p_sys->b_slice_i )
485 decoder_SynchroNewPicture( p_sys->p_synchro,
486 I_CODING_TYPE, 2, 0, 0,
487 p_sys->p_info->sequence->flags &
488 SEQ_FLAG_LOW_DELAY );
489 decoder_SynchroDecode( p_sys->p_synchro );
490 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
494 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
496 p_sys->b_preroll = true;
498 else if( p_sys->b_preroll )
500 p_sys->b_preroll = false;
501 if( p_sys->p_synchro )
502 decoder_SynchroReset( p_sys->p_synchro );
505 #ifdef PIC_FLAG_PTS
506 if( p_block->i_pts )
508 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
510 #else /* New interface */
511 if( p_block->i_pts || p_block->i_dts )
513 mpeg2_tag_picture( p_sys->p_mpeg2dec,
514 (uint32_t)p_block->i_pts,
515 (uint32_t)p_block->i_dts );
516 #endif
517 p_sys->i_previous_pts = p_sys->i_current_pts;
518 p_sys->i_current_pts = p_block->i_pts;
519 p_sys->i_previous_dts = p_sys->i_current_dts;
520 p_sys->i_current_dts = p_block->i_dts;
523 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
524 p_block->p_buffer + p_block->i_buffer );
526 p_block->i_buffer = 0;
527 break;
529 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
531 case STATE_SEQUENCE_MODIFIED:
532 GetAR( p_dec );
533 break;
534 #endif
535 case STATE_PICTURE_2ND:
536 p_sys->b_second_field = true;
537 break;
540 case STATE_INVALID_END:
541 case STATE_END:
542 case STATE_SLICE:
544 picture_t *p_pic = NULL;
546 if( p_sys->p_info->display_fbuf &&
547 p_sys->p_info->display_fbuf->id )
549 p_pic = p_sys->p_info->display_fbuf->id;
550 if( DpbDisplayPicture( p_dec, p_pic ) )
551 p_pic = NULL;
553 decoder_SynchroEnd( p_sys->p_synchro,
554 p_sys->p_info->display_picture->flags & PIC_MASK_CODING_TYPE,
555 p_sys->b_garbage_pic );
557 if( p_pic )
559 p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
560 if( p_sys->b_garbage_pic )
561 p_pic->date = 0; /* ??? */
562 p_sys->b_garbage_pic = false;
566 if( p_sys->p_info->discard_fbuf &&
567 p_sys->p_info->discard_fbuf->id )
569 DpbUnlinkPicture( p_dec, p_sys->p_info->discard_fbuf->id );
572 if( p_pic )
574 if( state == STATE_END )
575 p_pic->b_force = true; /* For still frames */
577 /* Avoid frames with identical timestamps.
578 * Especially needed for still frames in DVD menus. */
579 if( p_sys->i_last_frame_pts == p_pic->date )
580 p_pic->date++;
581 p_sys->i_last_frame_pts = p_pic->date;
582 return p_pic;
584 break;
587 case STATE_INVALID:
589 msg_Err( p_dec, "invalid picture encountered" );
590 /* I don't think we have anything to do, but well without
591 * docs ... */
592 break;
595 default:
596 break;
600 /* Never reached */
601 return NULL;
604 static int DecodeVideo( decoder_t *p_dec, block_t *p_block)
606 if( p_block == NULL ) /* No Drain */
607 return VLCDEC_SUCCESS;
609 block_t **pp_block = &p_block;
610 picture_t *p_pic;
611 while( ( p_pic = DecodeBlock( p_dec, pp_block ) ) != NULL )
612 decoder_QueueVideo( p_dec, p_pic );
613 return VLCDEC_SUCCESS;
616 /*****************************************************************************
617 * CloseDecoder: libmpeg2 decoder destruction
618 *****************************************************************************/
619 static void CloseDecoder( vlc_object_t *p_this )
621 decoder_t *p_dec = (decoder_t *)p_this;
622 decoder_sys_t *p_sys = p_dec->p_sys;
624 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
625 cc_Flush( &p_sys->cc );
626 #endif
628 DpbClean( p_dec );
630 free( p_sys->p_gop_user_data );
632 if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
634 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
636 free( p_sys );
639 /*****************************************************************************
640 * Reset: reset the decoder state
641 *****************************************************************************/
642 static void Reset( decoder_t *p_dec )
644 decoder_sys_t *p_sys = p_dec->p_sys;
646 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
647 cc_Flush( &p_sys->cc );
648 #endif
649 mpeg2_reset( p_sys->p_mpeg2dec, 0 );
650 DpbClean( p_dec );
653 /*****************************************************************************
654 * GetNewPicture: Get a new picture from the vout and set the buf struct
655 *****************************************************************************/
656 static picture_t *GetNewPicture( decoder_t *p_dec )
658 decoder_sys_t *p_sys = p_dec->p_sys;
659 picture_t *p_pic;
661 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
662 p_dec->fmt_out.video.i_visible_width =
663 p_sys->p_info->sequence->picture_width;
664 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
665 p_dec->fmt_out.video.i_visible_height =
666 p_sys->p_info->sequence->picture_height;
667 p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
668 p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
670 if( p_sys->p_info->sequence->frame_period > 0 )
672 p_dec->fmt_out.video.i_frame_rate =
673 (uint32_t)( (uint64_t)1001000000 * 27 /
674 p_sys->p_info->sequence->frame_period );
675 p_dec->fmt_out.video.i_frame_rate_base = 1001;
678 p_dec->fmt_out.i_codec =
679 ( p_sys->p_info->sequence->chroma_height <
680 p_sys->p_info->sequence->height ) ?
681 VLC_CODEC_I420 : VLC_CODEC_I422;
683 /* Get a new picture */
684 if( decoder_UpdateVideoFormat( p_dec ) )
685 return NULL;
686 p_pic = decoder_NewPicture( p_dec );
688 if( p_pic == NULL )
689 return NULL;
691 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
692 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
693 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
694 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
695 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
696 p_sys->p_info->current_picture->nb_fields : 2;
698 return p_pic;
701 #if MPEG2_RELEASE >= MPEG2_VERSION (0, 5, 0)
702 /*****************************************************************************
703 * SendCc: Sends the Closed Captions for the CC decoder.
704 *****************************************************************************/
705 static void SendCc( decoder_t *p_dec )
707 decoder_sys_t *p_sys = p_dec->p_sys;
708 block_t *p_cc = NULL;
710 if( !p_sys->cc.b_reorder && p_sys->cc.i_data <= 0 )
711 return;
713 p_cc = block_Alloc( p_sys->cc.i_data);
714 if( p_cc )
716 memcpy( p_cc->p_buffer, p_sys->cc.p_data, p_sys->cc.i_data );
717 p_cc->i_dts =
718 p_cc->i_pts = p_sys->cc.b_reorder ? p_sys->i_cc_pts : p_sys->i_cc_dts;
719 p_cc->i_flags = p_sys->i_cc_flags & BLOCK_FLAG_TYPE_MASK;
720 decoder_QueueCc( p_dec, p_cc, p_sys->cc.pb_present, p_sys->cc.b_reorder ? 0 : -1 );
722 cc_Flush( &p_sys->cc );
723 return;
725 #endif
727 /*****************************************************************************
728 * GetAR: Get aspect ratio
729 *****************************************************************************/
730 static void GetAR( decoder_t *p_dec )
732 decoder_sys_t *p_sys = p_dec->p_sys;
733 int i_old_sar_num = p_sys->i_sar_num;
734 int i_old_sar_den = p_sys->i_sar_den;
736 /* Check whether the input gave a particular aspect ratio */
737 if( p_dec->fmt_in.video.i_sar_num > 0 &&
738 p_dec->fmt_in.video.i_sar_den > 0 )
740 p_sys->i_sar_num = p_dec->fmt_in.video.i_sar_num;
741 p_sys->i_sar_den = p_dec->fmt_in.video.i_sar_den;
743 /* Use the value provided in the MPEG sequence header */
744 else if( p_sys->p_info->sequence->pixel_height > 0 )
746 p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
747 p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
749 else
751 /* Invalid aspect, assume 4:3.
752 * This shouldn't happen and if it does it is a bug
753 * in libmpeg2 (likely triggered by an invalid stream) */
754 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
755 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
758 if( p_sys->i_sar_num == i_old_sar_num &&
759 p_sys->i_sar_den == i_old_sar_den )
760 return;
762 if( p_sys->p_info->sequence->frame_period > 0 )
763 msg_Dbg( p_dec,
764 "%dx%d (display %d,%d), sar %i:%i, %u.%03u fps",
765 p_sys->p_info->sequence->picture_width,
766 p_sys->p_info->sequence->picture_height,
767 p_sys->p_info->sequence->display_width,
768 p_sys->p_info->sequence->display_height,
769 p_sys->i_sar_num, p_sys->i_sar_den,
770 (uint32_t)((uint64_t)1001000000 * 27 /
771 p_sys->p_info->sequence->frame_period / 1001),
772 (uint32_t)((uint64_t)1001000000 * 27 /
773 p_sys->p_info->sequence->frame_period % 1001) );
774 else
775 msg_Dbg( p_dec, "bad frame period" );
778 /*****************************************************************************
779 * PutPicture: Put a picture_t in mpeg2 context
780 *****************************************************************************/
781 static void PutPicture( decoder_t *p_dec, picture_t *p_picture )
783 decoder_sys_t *p_sys = p_dec->p_sys;
785 /* */
786 uint8_t *pp_buf[3];
787 for( int j = 0; j < 3; j++ )
788 pp_buf[j] = p_picture ? p_picture->p[j].p_pixels : NULL;
789 mpeg2_set_buf( p_sys->p_mpeg2dec, pp_buf, p_picture );
791 /* Completly broken API, why the hell does it suppose
792 * the stride of the chroma planes ! */
793 if( p_picture )
794 mpeg2_stride( p_sys->p_mpeg2dec, p_picture->p[Y_PLANE].i_pitch );
799 * Initialize a virtual Decoded Picture Buffer to workaround
800 * libmpeg2 deficient API
802 static void DpbInit( decoder_t *p_dec )
804 decoder_sys_t *p_sys = p_dec->p_sys;
806 for( int i = 0; i < DPB_COUNT; i++ )
807 p_sys->p_dpb[i].p_picture = NULL;
810 * Empty and reset the current DPB
812 static void DpbClean( decoder_t *p_dec )
814 decoder_sys_t *p_sys = p_dec->p_sys;
816 for( int i = 0; i < DPB_COUNT; i++ )
818 picture_dpb_t *p = &p_sys->p_dpb[i];
819 if( !p->p_picture )
820 continue;
821 if( p->b_linked )
822 picture_Release( p->p_picture );
823 if( !p->b_displayed )
824 picture_Release( p->p_picture );
826 p->p_picture = NULL;
830 * Retreive a picture and reserve a place in the DPB
832 static picture_t *DpbNewPicture( decoder_t *p_dec )
834 decoder_sys_t *p_sys = p_dec->p_sys;
836 picture_dpb_t *p;
837 int i;
839 for( i = 0; i < DPB_COUNT; i++ )
841 p = &p_sys->p_dpb[i];
842 if( !p->p_picture )
843 break;
845 if( i >= DPB_COUNT )
847 msg_Err( p_dec, "Leaking picture" );
848 return NULL;
851 p->p_picture = GetNewPicture( p_dec );
852 if( p->p_picture )
854 picture_Hold( p->p_picture );
855 p->b_linked = true;
856 p->b_displayed = false;
858 p->p_picture->date = 0;
860 return p->p_picture;
862 static picture_dpb_t *DpbFindPicture( decoder_t *p_dec, picture_t *p_picture )
864 decoder_sys_t *p_sys = p_dec->p_sys;
866 for( int i = 0; i < DPB_COUNT; i++ )
868 picture_dpb_t *p = &p_sys->p_dpb[i];
869 if( p->p_picture == p_picture )
870 return p;
872 return NULL;
875 * Unlink the provided picture and ensure that the decoder
876 * does not own it anymore.
878 static void DpbUnlinkPicture( decoder_t *p_dec, picture_t *p_picture )
880 picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
882 /* XXX it is needed to workaround libmpeg2 bugs */
883 if( !p || !p->b_linked )
885 msg_Err( p_dec, "DpbUnlinkPicture called on an invalid picture" );
886 return;
889 assert( p && p->b_linked );
891 picture_Release( p->p_picture );
892 p->b_linked = false;
894 if( !p->b_displayed )
895 picture_Release( p->p_picture );
896 p->p_picture = NULL;
899 * Mark the provided picture as displayed.
901 static int DpbDisplayPicture( decoder_t *p_dec, picture_t *p_picture )
903 picture_dpb_t *p = DpbFindPicture( p_dec, p_picture );
905 /* XXX it is needed to workaround libmpeg2 bugs */
906 if( !p || p->b_displayed || !p->b_linked )
908 msg_Err( p_dec, "DpbDisplayPicture called on an invalid picture" );
909 return VLC_EGENERIC;
912 assert( p && !p->b_displayed && p->b_linked );
914 p->b_displayed = true;
915 return VLC_SUCCESS;