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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
37 #include <mpeg2dec/mpeg2.h>
39 #include <vlc_codec_synchro.h>
41 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
42 #define AR_SQUARE_PICTURE 1 /* square pixels */
43 #define AR_4_3_PICTURE 2 /* 4:3 picture (TV) */
44 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
45 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
47 /*****************************************************************************
48 * decoder_sys_t : libmpeg2 decoder descriptor
49 *****************************************************************************/
55 mpeg2dec_t *p_mpeg2dec;
56 const mpeg2_info_t *p_info;
62 mtime_t i_previous_pts;
63 mtime_t i_current_pts;
64 mtime_t i_previous_dts;
65 mtime_t i_current_dts;
67 picture_t * p_picture_to_destroy;
69 bool b_after_sequence_header; /* is it the next frame after
70 * the sequence header ? */
71 bool b_slice_i; /* intra-slice refresh stream */
79 decoder_synchro_t *p_synchro;
83 mtime_t i_last_frame_pts;
87 /*****************************************************************************
89 *****************************************************************************/
90 static int OpenDecoder( vlc_object_t * );
91 static void CloseDecoder( vlc_object_t * );
93 static picture_t *DecodeBlock( decoder_t *, block_t ** );
95 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
96 static void GetAR( decoder_t *p_dec );
98 /*****************************************************************************
100 *****************************************************************************/
102 set_description( N_("MPEG I/II video decoder (using libmpeg2)") );
103 set_capability( "decoder", 150 );
104 set_category( CAT_INPUT );
105 set_subcategory( SUBCAT_INPUT_VCODEC );
106 set_callbacks( OpenDecoder, CloseDecoder );
107 add_shortcut( "libmpeg2" );
110 /*****************************************************************************
111 * OpenDecoder: probe the decoder and return score
112 *****************************************************************************/
113 static int OpenDecoder( vlc_object_t *p_this )
115 decoder_t *p_dec = (decoder_t*)p_this;
116 decoder_sys_t *p_sys;
117 uint32_t i_accel = 0;
119 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
120 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
121 /* Pinnacle hardware-mpeg1 */
122 p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
123 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','2','v') &&
124 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') &&
125 p_dec->fmt_in.i_codec != VLC_FOURCC('h','d','v','2') )
130 /* Allocate the memory needed to store the decoder's structure */
131 if( ( p_dec->p_sys = p_sys =
132 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
135 /* Initialize the thread properties */
136 memset( p_sys, 0, sizeof(decoder_sys_t) );
137 p_sys->p_mpeg2dec = NULL;
138 p_sys->p_synchro = NULL;
139 p_sys->p_info = NULL;
140 p_sys->i_current_pts = 0;
141 p_sys->i_previous_pts = 0;
142 p_sys->i_current_dts = 0;
143 p_sys->i_previous_dts = 0;
144 p_sys->p_picture_to_destroy = NULL;
145 p_sys->b_garbage_pic = 0;
146 p_sys->b_slice_i = 0;
147 p_sys->b_second_field = 0;
149 p_sys->b_preroll = false;
151 #if defined( __i386__ ) || defined( __x86_64__ )
152 if( vlc_CPU() & CPU_CAPABILITY_MMX )
154 i_accel |= MPEG2_ACCEL_X86_MMX;
157 if( vlc_CPU() & CPU_CAPABILITY_3DNOW )
159 i_accel |= MPEG2_ACCEL_X86_3DNOW;
162 if( vlc_CPU() & CPU_CAPABILITY_MMXEXT )
164 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
167 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
168 if( vlc_CPU() & CPU_CAPABILITY_ALTIVEC )
170 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
174 /* If we do not know this CPU, trust libmpeg2's feature detection */
175 i_accel = MPEG2_ACCEL_DETECT;
179 /* Set CPU acceleration features */
180 mpeg2_accel( i_accel );
182 /* Initialize decoder */
183 p_sys->p_mpeg2dec = mpeg2_init();
184 if( p_sys->p_mpeg2dec == NULL)
186 msg_Err( p_dec, "mpeg2_init() failed" );
191 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
193 p_dec->pf_decode_video = DecodeBlock;
198 /*****************************************************************************
199 * RunDecoder: the libmpeg2 decoder
200 *****************************************************************************/
201 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
203 decoder_sys_t *p_sys = p_dec->p_sys;
209 if( !pp_block || !*pp_block ) return NULL;
215 state = mpeg2_parse( p_sys->p_mpeg2dec );
220 if( !p_block->i_buffer )
222 block_Release( p_block );
226 if( (p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY
227 | BLOCK_FLAG_CORRUPTED)) &&
229 p_sys->p_info->sequence &&
230 p_sys->p_info->sequence->width != (unsigned)-1 )
232 decoder_SynchroReset( p_sys->p_synchro );
233 if( p_sys->p_info->current_fbuf != NULL
234 && p_sys->p_info->current_fbuf->id != NULL )
236 p_sys->b_garbage_pic = 1;
237 p_pic = p_sys->p_info->current_fbuf->id;
242 buf[0] = buf[1] = buf[2] = NULL;
243 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
245 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
246 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
248 p_sys->p_picture_to_destroy = p_pic;
250 if ( p_sys->b_slice_i )
252 decoder_SynchroNewPicture( p_sys->p_synchro,
253 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
254 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
255 decoder_SynchroDecode( p_sys->p_synchro );
256 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
260 if( p_block->i_flags & BLOCK_FLAG_PREROLL )
262 p_sys->b_preroll = true;
264 else if( p_sys->b_preroll )
266 p_sys->b_preroll = false;
268 decoder_SynchroReset( p_sys->p_synchro );
274 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
276 #else /* New interface */
277 if( p_block->i_pts || p_block->i_dts )
279 mpeg2_tag_picture( p_sys->p_mpeg2dec,
280 (uint32_t)p_block->i_pts,
281 (uint32_t)p_block->i_dts );
283 p_sys->i_previous_pts = p_sys->i_current_pts;
284 p_sys->i_current_pts = p_block->i_pts;
285 p_sys->i_previous_dts = p_sys->i_current_dts;
286 p_sys->i_current_dts = p_block->i_dts;
289 p_sys->i_current_rate = p_block->i_rate;
291 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
292 p_block->p_buffer + p_block->i_buffer );
294 p_block->i_buffer = 0;
297 #ifdef STATE_SEQUENCE_MODIFIED
298 /* FIXME - the above ifdef is always FALSE, even with libmpeg2-0.5.0
299 * Need to improve the implementation to avoid invalid/unref pictures */
301 case STATE_SEQUENCE_MODIFIED:
308 /* Initialize video output */
310 buf[0] = buf[1] = buf[2] = NULL;
314 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
316 /* Set the first 2 reference frames */
317 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
319 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
321 block_Release( p_block );
325 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
326 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
328 /* This picture will never go through display_picture. */
331 /* For some reason, libmpeg2 will put this pic twice in
332 * discard_picture. This can be considered a bug in libmpeg2. */
333 p_dec->pf_picture_link( p_dec, p_pic );
335 if( p_sys->p_synchro )
337 decoder_SynchroRelease( p_sys->p_synchro );
339 p_sys->p_synchro = decoder_SynchroInit( p_dec,
340 (uint32_t)((uint64_t)1001000000 * 27 /
341 p_sys->p_info->sequence->frame_period) );
342 p_sys->b_after_sequence_header = 1;
346 case STATE_PICTURE_2ND:
347 p_sys->b_second_field = 1;
353 mtime_t i_pts, i_dts;
354 buf[0] = buf[1] = buf[2] = NULL;
356 if ( p_sys->b_after_sequence_header &&
357 ((p_sys->p_info->current_picture->flags &
358 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
360 /* Intra-slice refresh. Simulate a blank I picture. */
361 msg_Dbg( p_dec, "intra-slice refresh stream" );
362 decoder_SynchroNewPicture( p_sys->p_synchro,
363 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
364 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
365 decoder_SynchroDecode( p_sys->p_synchro );
366 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
367 p_sys->b_slice_i = 1;
369 p_sys->b_after_sequence_header = 0;
372 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
373 ( ( p_sys->p_info->current_picture->pts ==
374 (uint32_t)p_sys->i_current_pts ) ?
375 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
378 /* Hack to handle demuxers which only have DTS timestamps */
379 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
381 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
382 (p_sys->p_info->current_picture->flags &
383 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
385 i_pts = p_block->i_dts;
388 p_block->i_pts = p_block->i_dts = 0;
391 #else /* New interface */
393 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
394 ( ( p_sys->p_info->current_picture->tag ==
395 (uint32_t)p_sys->i_current_pts ) ?
396 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
397 i_dts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
398 ( ( p_sys->p_info->current_picture->tag2 ==
399 (uint32_t)p_sys->i_current_dts ) ?
400 p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
403 /* If nb_fields == 1, it is a field picture, and it will be
404 * followed by another field picture for which we won't call
405 * decoder_SynchroNewPicture() because this would have other
406 * problems, so we take it into account here.
407 * This kind of sucks, but I didn't think better. --Meuuh
409 decoder_SynchroNewPicture( p_sys->p_synchro,
410 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
411 p_sys->p_info->current_picture->nb_fields == 1 ? 2 :
412 p_sys->p_info->current_picture->nb_fields, i_pts, i_dts,
413 p_sys->i_current_rate,
414 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
416 if( !p_dec->b_pace_control && !p_sys->b_preroll &&
418 && ((p_sys->p_info->current_picture->flags
419 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
420 && !decoder_SynchroChoose( p_sys->p_synchro,
421 p_sys->p_info->current_picture->flags
422 & PIC_MASK_CODING_TYPE,
423 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/,
424 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ) )
426 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
428 decoder_SynchroTrash( p_sys->p_synchro );
429 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
433 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
435 decoder_SynchroDecode( p_sys->p_synchro );
437 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
439 block_Release( p_block );
443 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
444 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
452 if( p_sys->p_info->display_fbuf
453 && p_sys->p_info->display_fbuf->id )
455 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
457 decoder_SynchroEnd( p_sys->p_synchro,
458 p_sys->p_info->display_picture->flags
459 & PIC_MASK_CODING_TYPE,
460 p_sys->b_garbage_pic );
461 p_sys->b_garbage_pic = 0;
463 if ( p_sys->p_picture_to_destroy != p_pic )
465 p_pic->date = decoder_SynchroDate( p_sys->p_synchro );
469 p_sys->p_picture_to_destroy = NULL;
474 if( p_sys->p_info->discard_fbuf &&
475 p_sys->p_info->discard_fbuf->id )
477 p_dec->pf_picture_unlink( p_dec,
478 p_sys->p_info->discard_fbuf->id );
481 /* For still frames */
482 if( state == STATE_END && p_pic ) p_pic->b_force = true;
486 /* Avoid frames with identical timestamps.
487 * Especially needed for still frames in DVD menus. */
488 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
489 p_sys->i_last_frame_pts = p_pic->date;
499 buf[0] = buf[1] = buf[2] = NULL;
501 msg_Warn( p_dec, "invalid picture encountered" );
502 if ( ( p_sys->p_info->current_picture == NULL ) ||
503 ( ( p_sys->p_info->current_picture->flags &
504 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
506 if( p_sys->p_synchro ) decoder_SynchroReset( p_sys->p_synchro );
508 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
511 if( p_sys->p_info->current_fbuf &&
512 p_sys->p_info->current_fbuf->id )
514 p_sys->b_garbage_pic = 1;
515 p_pic = p_sys->p_info->current_fbuf->id;
517 else if( !p_sys->p_info->sequence )
523 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
525 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
526 mpeg2_stride( p_sys->p_mpeg2dec, p_pic->p[Y_PLANE].i_pitch );
528 p_sys->p_picture_to_destroy = p_pic;
530 memset( p_pic->p[0].p_pixels, 0,
531 p_sys->p_info->sequence->width
532 * p_sys->p_info->sequence->height );
533 memset( p_pic->p[1].p_pixels, 0x80,
534 p_sys->p_info->sequence->width
535 * p_sys->p_info->sequence->height / 4 );
536 memset( p_pic->p[2].p_pixels, 0x80,
537 p_sys->p_info->sequence->width
538 * p_sys->p_info->sequence->height / 4 );
540 if( p_sys->b_slice_i )
542 decoder_SynchroNewPicture( p_sys->p_synchro,
543 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate,
544 p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY );
545 decoder_SynchroDecode( p_sys->p_synchro );
546 decoder_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
560 /*****************************************************************************
561 * CloseDecoder: libmpeg2 decoder destruction
562 *****************************************************************************/
563 static void CloseDecoder( vlc_object_t *p_this )
565 decoder_t *p_dec = (decoder_t *)p_this;
566 decoder_sys_t *p_sys = p_dec->p_sys;
568 if( p_sys->p_synchro ) decoder_SynchroRelease( p_sys->p_synchro );
570 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
575 /*****************************************************************************
576 * GetNewPicture: Get a new picture from the vout and set the buf struct
577 *****************************************************************************/
578 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
580 decoder_sys_t *p_sys = p_dec->p_sys;
583 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
584 p_dec->fmt_out.video.i_visible_width =
585 p_sys->p_info->sequence->picture_width;
586 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
587 p_dec->fmt_out.video.i_visible_height =
588 p_sys->p_info->sequence->picture_height;
589 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
590 p_dec->fmt_out.video.i_sar_num = p_sys->i_sar_num;
591 p_dec->fmt_out.video.i_sar_den = p_sys->i_sar_den;
593 if( p_sys->p_info->sequence->frame_period > 0 )
595 p_dec->fmt_out.video.i_frame_rate =
596 (uint32_t)( (uint64_t)1001000000 * 27 /
597 p_sys->p_info->sequence->frame_period );
598 p_dec->fmt_out.video.i_frame_rate_base = 1001;
601 p_dec->fmt_out.i_codec =
602 ( p_sys->p_info->sequence->chroma_height <
603 p_sys->p_info->sequence->height ) ?
604 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
606 /* Get a new picture */
607 p_pic = p_dec->pf_vout_buffer_new( p_dec );
609 if( p_pic == NULL ) return NULL;
611 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
612 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
613 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
614 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
615 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
616 p_sys->p_info->current_picture->nb_fields : 2;
618 p_dec->pf_picture_link( p_dec, p_pic );
620 pp_buf[0] = p_pic->p[0].p_pixels;
621 pp_buf[1] = p_pic->p[1].p_pixels;
622 pp_buf[2] = p_pic->p[2].p_pixels;
627 /*****************************************************************************
628 * GetAR: Get aspect ratio
629 *****************************************************************************/
630 static void GetAR( decoder_t *p_dec )
632 decoder_sys_t *p_sys = p_dec->p_sys;
634 /* Check whether the input gave a particular aspect ratio */
635 if( p_dec->fmt_in.video.i_aspect )
637 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
638 if( p_sys->i_aspect <= AR_221_1_PICTURE )
639 switch( p_sys->i_aspect )
642 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
643 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
644 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
646 case AR_16_9_PICTURE:
647 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
648 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 16;
649 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 9;
651 case AR_221_1_PICTURE:
652 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
653 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 221;
654 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 100;
656 case AR_SQUARE_PICTURE:
657 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
658 p_sys->p_info->sequence->picture_width /
659 p_sys->p_info->sequence->picture_height;
660 p_sys->i_sar_num = p_sys->i_sar_den = 1;
666 /* Use the value provided in the MPEG sequence header */
667 if( p_sys->p_info->sequence->pixel_height > 0 )
670 ((uint64_t)p_sys->p_info->sequence->picture_width) *
671 p_sys->p_info->sequence->pixel_width *
673 p_sys->p_info->sequence->picture_height /
674 p_sys->p_info->sequence->pixel_height;
675 p_sys->i_sar_num = p_sys->p_info->sequence->pixel_width;
676 p_sys->i_sar_den = p_sys->p_info->sequence->pixel_height;
680 /* Invalid aspect, assume 4:3.
681 * This shouldn't happen and if it does it is a bug
682 * in libmpeg2 (likely triggered by an invalid stream) */
683 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
684 p_sys->i_sar_num = p_sys->p_info->sequence->picture_height * 4;
685 p_sys->i_sar_den = p_sys->p_info->sequence->picture_width * 3;
689 msg_Dbg( p_dec, "%dx%d (display %d,%d), aspect %d, sar %i:%i, %u.%03u fps",
690 p_sys->p_info->sequence->picture_width,
691 p_sys->p_info->sequence->picture_height,
692 p_sys->p_info->sequence->display_width,
693 p_sys->p_info->sequence->display_height,
694 p_sys->i_aspect, p_sys->i_sar_num, p_sys->i_sar_den,
695 (uint32_t)((uint64_t)1001000000 * 27 /
696 p_sys->p_info->sequence->frame_period / 1001),
697 (uint32_t)((uint64_t)1001000000 * 27 /
698 p_sys->p_info->sequence->frame_period % 1001) );