1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
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
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
34 #include <vlc_codec.h>
36 #include <mpeg2dec/mpeg2.h>
38 #include <vlc_codec_synchro.h>
40 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
41 #define AR_SQUARE_PICTURE 1 /* square pixels */
42 #define AR_4_3_PICTURE 2 /* 4:3 picture (TV) */
43 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
44 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
46 /*****************************************************************************
47 * decoder_sys_t : libmpeg2 decoder descriptor
48 *****************************************************************************/
54 mpeg2dec_t *p_mpeg2dec;
55 const mpeg2_info_t *p_info;
61 mtime_t i_previous_pts;
62 mtime_t i_current_pts;
63 mtime_t i_previous_dts;
64 mtime_t i_current_dts;
66 picture_t * p_picture_to_destroy;
68 bool b_after_sequence_header; /* is it the next frame after
69 * the sequence header ? */
70 bool b_slice_i; /* intra-slice refresh stream */
78 decoder_synchro_t *p_synchro;
82 mtime_t i_last_frame_pts;
86 /*****************************************************************************
88 *****************************************************************************/
89 static int OpenDecoder( vlc_object_t * );
90 static void CloseDecoder( vlc_object_t * );
92 static picture_t *DecodeBlock( decoder_t *, block_t ** );
94 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
95 static void GetAR( decoder_t *p_dec );
97 /*****************************************************************************
99 *****************************************************************************/
101 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
102 set_capability( "decoder", 150 );
103 set_category( CAT_INPUT );
104 set_subcategory( SUBCAT_INPUT_VCODEC );
105 set_callbacks( OpenDecoder, CloseDecoder );
106 add_shortcut( "libmpeg2" );
109 /*****************************************************************************
110 * OpenDecoder: probe the decoder and return score
111 *****************************************************************************/
112 static int OpenDecoder( vlc_object_t *p_this )
114 decoder_t *p_dec = (decoder_t*)p_this;
115 decoder_sys_t *p_sys;
116 uint32_t i_accel = 0;
118 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
119 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
120 /* Pinnacle hardware-mpeg1 */
121 p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
122 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','2','v') &&
123 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') &&
124 p_dec->fmt_in.i_codec != VLC_FOURCC('h','d','v','2') )
129 /* Allocate the memory needed to store the decoder's structure */
130 if( ( p_dec->p_sys = p_sys =
131 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
133 msg_Err( p_dec, "out of memory" );
137 /* Initialize the thread properties */
138 memset( p_sys, 0, sizeof(decoder_sys_t) );
139 p_sys->p_mpeg2dec = NULL;
140 p_sys->p_synchro = NULL;
141 p_sys->p_info = NULL;
142 p_sys->i_current_pts = 0;
143 p_sys->i_previous_pts = 0;
144 p_sys->i_current_dts = 0;
145 p_sys->i_previous_dts = 0;
146 p_sys->p_picture_to_destroy = NULL;
147 p_sys->b_garbage_pic = 0;
148 p_sys->b_slice_i = 0;
149 p_sys->b_second_field = 0;
151 p_sys->b_preroll = false;
153 #if defined( __i386__ ) || defined( __x86_64__ )
154 if( vlc_CPU() & CPU_CAPABILITY_MMX )
156 i_accel |= MPEG2_ACCEL_X86_MMX;
159 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
161 i_accel |= MPEG2_ACCEL_X86_3DNOW;
164 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
166 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
169 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
170 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
172 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
176 /* If we do not know this CPU, trust libmpeg2's feature detection */
177 i_accel = MPEG2_ACCEL_DETECT;
181 /* Set CPU acceleration features */
182 mpeg2_accel( i_accel );
184 /* Initialize decoder */
185 p_sys->p_mpeg2dec = mpeg2_init();
186 if( p_sys->p_mpeg2dec == NULL)
188 msg_Err( p_dec, "mpeg2_init() failed" );
193 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
195 p_dec->pf_decode_video = DecodeBlock;
200 /*****************************************************************************
201 * RunDecoder: the libmpeg2 decoder
202 *****************************************************************************/
203 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
205 decoder_sys_t *p_sys = p_dec->p_sys;
211 if( !pp_block || !*pp_block ) return NULL;
217 state = mpeg2_parse( p_sys->p_mpeg2dec );
222 if( !p_block->i_buffer )
224 block_Release( p_block );
228 if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
229 | BLOCK_FLAG_CORRUPTED)) &&
231 p_sys->p_info->sequence &&
232 p_sys->p_info->sequence->width != (unsigned)-1 )
234 decoder_SynchroReset( p_sys->p_synchro );
235 if( p_sys->p_info->current_fbuf != NULL
236 && p_sys->p_info->current_fbuf->id != NULL )
238 p_sys->b_garbage_pic = 1;
239 p_pic = p_sys->p_info->current_fbuf->id;
244 buf[0] = buf[1] = buf[2] = NULL;
245 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
247 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
248 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
250 p_sys->p_picture_to_destroy = p_pic;
252 if ( p_sys->b_slice_i )
254 decoder_SynchroNewPicture( p_sys->p_synchro,
255 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
256 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
257 decoder_SynchroDecode( p_sys->p_synchro );
258 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
262 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
264 p_sys->b_preroll = true;
266 else if( p_sys->b_preroll )
268 p_sys->b_preroll = false;
270 decoder_SynchroReset( p_sys->p_synchro );
276 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
278 #else /* New interface */
279 if( p_block->i_pts || p_block->i_dts )
281 mpeg2_tag_picture( p_sys->p_mpeg2dec,
282 (uint32_t)p_block->i_pts,
283 (uint32_t)p_block->i_dts );
285 p_sys->i_previous_pts = p_sys->i_current_pts;
286 p_sys->i_current_pts = p_block->i_pts;
287 p_sys->i_previous_dts = p_sys->i_current_dts;
288 p_sys->i_current_dts = p_block->i_dts;
291 p_sys->i_current_rate = p_block->i_rate;
293 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
294 p_block->p_buffer + p_block->i_buffer );
296 p_block->i_buffer = 0;
299 #ifdef STATE_SEQUENCE_MODIFIED
300 /* FIXME - the above ifdef is always FALSE, even with libmpeg2-0.5.0
301 * Need to improve the implementation to avoid invalid/unref pictures */
303 case STATE_SEQUENCE_MODIFIED:
310 /* Initialize video output */
312 buf[0] = buf[1] = buf[2] = NULL;
316 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
318 /* Set the first 2 reference frames */
319 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
321 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
323 block_Release( p_block );
327 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
328 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
330 /* This picture will never go through display_picture. */
333 /* For some reason, libmpeg2 will put this pic twice in
334 * discard_picture. This can be considered a bug in libmpeg2. */
335 p_dec->pf_picture_link( p_dec, p_pic );
337 if( p_sys->p_synchro )
339 decoder_SynchroRelease( p_sys->p_synchro );
341 p_sys->p_synchro = decoder_SynchroInit( p_dec,
342 (uint32_t)((uint64_t)1001000000 * 27 /
343 p_sys->p_info->sequence->frame_period) );
344 p_sys->b_after_sequence_header = 1;
348 case STATE_PICTURE_2ND:
349 p_sys->b_second_field = 1;
355 mtime_t i_pts, i_dts;
356 buf[0] = buf[1] = buf[2] = NULL;
358 if ( p_sys->b_after_sequence_header &&
359 ((p_sys->p_info->current_picture->flags &
360 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
362 /* Intra-slice refresh. Simulate a blank I picture. */
363 msg_Dbg( p_dec, "intra-slice refresh stream" );
364 decoder_SynchroNewPicture( p_sys->p_synchro,
365 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
366 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
367 decoder_SynchroDecode( p_sys->p_synchro );
368 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
369 p_sys->b_slice_i = 1;
371 p_sys->b_after_sequence_header = 0;
374 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
375 ( ( p_sys->p_info->current_picture->pts ==
376 (uint32_t)p_sys->i_current_pts ) ?
377 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
380 /* Hack to handle demuxers which only have DTS timestamps */
381 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
383 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
384 (p_sys->p_info->current_picture->flags &
385 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
387 i_pts = p_block->i_dts;
390 p_block->i_pts = p_block->i_dts = 0;
393 #else /* New interface */
395 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
396 ( ( p_sys->p_info->current_picture->tag ==
397 (uint32_t)p_sys->i_current_pts ) ?
398 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
399 i_dts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
400 ( ( p_sys->p_info->current_picture->tag2 ==
401 (uint32_t)p_sys->i_current_dts ) ?
402 p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
405 /* If nb_fields == 1, it is a field picture, and it will be
406 * followed by another field picture for which we won't call
407 * decoder_SynchroNewPicture() because this would have other
408 * problems, so we take it into account here.
409 * This kind of sucks, but I didn't think better. --Meuuh
411 decoder_SynchroNewPicture( p_sys->p_synchro,
412 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
413 p_sys->p_info->current_picture->nb_fields == 1 ? 2 :
414 p_sys->p_info->current_picture->nb_fields, i_pts, i_dts,
415 p_sys->i_current_rate,
416 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
418 if( !p_dec->b_pace_control && !p_sys->b_preroll &&
420 && ((p_sys->p_info->current_picture->flags
421 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
422 && !decoder_SynchroChoose( p_sys->p_synchro,
423 p_sys->p_info->current_picture->flags
424 & PIC_MASK_CODING_TYPE,
425 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
426 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
428 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
430 decoder_SynchroTrash( p_sys->p_synchro );
431 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
435 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
437 decoder_SynchroDecode( p_sys->p_synchro );
439 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
441 block_Release( p_block );
445 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
446 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
454 if( p_sys->p_info->display_fbuf
455 && p_sys->p_info->display_fbuf->id )
457 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
459 decoder_SynchroEnd( p_sys->p_synchro,
460 p_sys->p_info->display_picture->flags
461 & PIC_MASK_CODING_TYPE,
462 p_sys->b_garbage_pic );
463 p_sys->b_garbage_pic = 0;
465 if ( p_sys->p_picture_to_destroy != p_pic )
467 p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
471 p_sys->p_picture_to_destroy = NULL;
476 if( p_sys->p_info->discard_fbuf &&
477 p_sys->p_info->discard_fbuf->id )
479 p_dec->pf_picture_unlink( p_dec,
480 p_sys->p_info->discard_fbuf->id );
483 /* For still frames */
484 if( state == STATE_END && p_pic ) p_pic->b_force = true;
488 /* Avoid frames with identical timestamps.
489 * Especially needed for still frames in DVD menus. */
490 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
491 p_sys->i_last_frame_pts = p_pic->date;
501 buf[0] = buf[1] = buf[2] = NULL;
503 msg_Warn( p_dec, "invalid picture encountered" );
504 if ( ( p_sys->p_info->current_picture == NULL ) ||
505 ( ( p_sys->p_info->current_picture->flags &
506 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
508 if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
510 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
513 if( p_sys->p_info->current_fbuf &&
514 p_sys->p_info->current_fbuf->id )
516 p_sys->b_garbage_pic = 1;
517 p_pic = p_sys->p_info->current_fbuf->id;
519 else if( !p_sys->p_info->sequence )
525 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
527 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
528 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
530 p_sys->p_picture_to_destroy = p_pic;
532 memset( p_pic->p[0].p_pixels, 0,
533 p_sys->p_info->sequence->width
534 * p_sys->p_info->sequence->height );
535 memset( p_pic->p[1].p_pixels, 0x80,
536 p_sys->p_info->sequence->width
537 * p_sys->p_info->sequence->height / 4 );
538 memset( p_pic->p[2].p_pixels, 0x80,
539 p_sys->p_info->sequence->width
540 * p_sys->p_info->sequence->height / 4 );
542 if( p_sys->b_slice_i )
544 decoder_SynchroNewPicture( p_sys->p_synchro,
545 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
546 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
547 decoder_SynchroDecode( p_sys->p_synchro );
548 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
562 /*****************************************************************************
563 * CloseDecoder: libmpeg2 decoder destruction
564 *****************************************************************************/
565 static void CloseDecoder( vlc_object_t *p_this )
567 decoder_t *p_dec = (decoder_t *)p_this;
568 decoder_sys_t *p_sys = p_dec->p_sys;
570 if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
572 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
577 /*****************************************************************************
578 * GetNewPicture: Get a new picture from the vout and set the buf struct
579 *****************************************************************************/
580 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
582 decoder_sys_t *p_sys = p_dec->p_sys;
585 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
586 p_dec->fmt_out.video.i_visible_width =
587 p_sys->p_info->sequence->picture_width;
588 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
589 p_dec->fmt_out.video.i_visible_height =
590 p_sys->p_info->sequence->picture_height;
591 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
592 p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
593 p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
595 if( p_sys->p_info->sequence->frame_period > 0 )
597 p_dec->fmt_out.video.i_frame_rate =
598 (uint32_t)( (uint64_t)1001000000 * 27 /
599 p_sys->p_info->sequence->frame_period );
600 p_dec->fmt_out.video.i_frame_rate_base = 1001;
603 p_dec->fmt_out.i_codec =
604 ( p_sys->p_info->sequence->chroma_height <
605 p_sys->p_info->sequence->height ) ?
606 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
608 /* Get a new picture */
609 p_pic = p_dec->pf_vout_buffer_new( p_dec );
611 if( p_pic == NULL ) return NULL;
613 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
614 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
615 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
616 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
617 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
618 p_sys->p_info->current_picture->nb_fields : 2;
620 p_dec->pf_picture_link( p_dec, p_pic );
622 pp_buf[0] = p_pic->p[0].p_pixels;
623 pp_buf[1] = p_pic->p[1].p_pixels;
624 pp_buf[2] = p_pic->p[2].p_pixels;
629 /*****************************************************************************
630 * GetAR: Get aspect ratio
631 *****************************************************************************/
632 static void GetAR( decoder_t *p_dec )
634 decoder_sys_t *p_sys = p_dec->p_sys;
636 /* Check whether the input gave a particular aspect ratio */
637 if( p_dec->fmt_in.video.i_aspect )
639 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
640 if( p_sys->i_aspect <= AR_221_1_PICTURE )
641 switch( p_sys->i_aspect )
644 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
645 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
646 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
648 case AR_16_9_PICTURE:
649 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
650 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 16;
651 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 9;
653 case AR_221_1_PICTURE:
654 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
655 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 221;
656 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 100;
658 case AR_SQUARE_PICTURE:
659 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
660 p_sys->p_info->sequence->picture_width /
661 p_sys->p_info->sequence->picture_height;
662 p_sys->i_sar_num = p_sys->i_sar_den = 1;
668 /* Use the value provided in the MPEG sequence header */
669 if( p_sys->p_info->sequence->pixel_height > 0 )
672 ((uint64_t)p_sys->p_info->sequence->picture_width) *
673 p_sys->p_info->sequence->pixel_width *
675 p_sys->p_info->sequence->picture_height /
676 p_sys->p_info->sequence->pixel_height;
677 p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
678 p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
682 /* Invalid aspect, assume 4:3.
683 * This shouldn't happen and if it does it is a bug
684 * in libmpeg2 (likely triggered by an invalid stream) */
685 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
686 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
687 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
691 msg_Dbg( p_dec, "%dx%d (display %d,%d), aspect %d, sar %i:%i, %u.%03u fps",
692 p_sys->p_info->sequence->picture_width,
693 p_sys->p_info->sequence->picture_height,
694 p_sys->p_info->sequence->display_width,
695 p_sys->p_info->sequence->display_height,
696 p_sys->i_aspect, p_sys->i_sar_num, p_sys->i_sar_den,
697 (uint32_t)((uint64_t)1001000000 * 27 /
698 p_sys->p_info->sequence->frame_period / 1001),
699 (uint32_t)((uint64_t)1001000000 * 27 /
700 p_sys->p_info->sequence->frame_period % 1001) );