1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
30 #include <vlc/decoder.h>
32 #include <mpeg2dec/mpeg2.h>
34 #include "vout_synchro.h"
36 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
37 #define AR_SQUARE_PICTURE 1 /* square pixels */
38 #define AR_3_4_PICTURE 2 /* 3:4 picture (TV) */
39 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
40 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
42 /*****************************************************************************
43 * decoder_sys_t : libmpeg2 decoder descriptor
44 *****************************************************************************/
50 mpeg2dec_t *p_mpeg2dec;
51 const mpeg2_info_t *p_info;
57 mtime_t i_previous_pts;
58 mtime_t i_current_pts;
59 mtime_t i_previous_dts;
60 mtime_t i_current_dts;
62 picture_t * p_picture_to_destroy;
63 vlc_bool_t b_garbage_pic;
64 vlc_bool_t b_after_sequence_header; /* is it the next frame after
65 * the sequence header ? */
66 vlc_bool_t b_slice_i; /* intra-slice refresh stream */
71 vout_synchro_t *p_synchro;
73 mtime_t i_last_frame_pts;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int OpenDecoder( vlc_object_t * );
81 static void CloseDecoder( vlc_object_t * );
83 static picture_t *DecodeBlock( decoder_t *, block_t ** );
85 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
87 /*****************************************************************************
89 *****************************************************************************/
91 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
92 set_capability( "decoder", 150 );
93 set_category( CAT_INPUT );
94 set_subcategory( SUBCAT_INPUT_VCODEC );
95 set_callbacks( OpenDecoder, CloseDecoder );
96 add_shortcut( "libmpeg2" );
99 /*****************************************************************************
100 * OpenDecoder: probe the decoder and return score
101 *****************************************************************************/
102 static int OpenDecoder( vlc_object_t *p_this )
104 decoder_t *p_dec = (decoder_t*)p_this;
105 decoder_sys_t *p_sys;
106 uint32_t i_accel = 0;
108 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
109 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
110 /* Pinnacle hardware-mpeg1 */
111 p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
113 p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
114 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
119 /* Allocate the memory needed to store the decoder's structure */
120 if( ( p_dec->p_sys = p_sys =
121 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
123 msg_Err( p_dec, "out of memory" );
127 /* Initialize the thread properties */
128 memset( p_sys, 0, sizeof(decoder_sys_t) );
129 p_sys->p_mpeg2dec = NULL;
130 p_sys->p_synchro = NULL;
131 p_sys->p_info = NULL;
132 p_sys->i_current_pts = 0;
133 p_sys->i_previous_pts = 0;
134 p_sys->i_current_dts = 0;
135 p_sys->i_previous_dts = 0;
136 p_sys->p_picture_to_destroy = NULL;
137 p_sys->b_garbage_pic = 0;
138 p_sys->b_slice_i = 0;
141 #if defined( __i386__ )
142 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
144 i_accel |= MPEG2_ACCEL_X86_MMX;
147 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
149 i_accel |= MPEG2_ACCEL_X86_3DNOW;
152 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
154 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
157 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
158 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
160 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
164 /* If we do not know this CPU, trust libmpeg2's feature detection */
165 i_accel = MPEG2_ACCEL_DETECT;
169 /* Set CPU acceleration features */
170 mpeg2_accel( i_accel );
172 /* Initialize decoder */
173 p_sys->p_mpeg2dec = mpeg2_init();
174 if( p_sys->p_mpeg2dec == NULL)
176 msg_Err( p_dec, "mpeg2_init() failed" );
181 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
183 p_dec->pf_decode_video = DecodeBlock;
188 /*****************************************************************************
189 * RunDecoder: the libmpeg2 decoder
190 *****************************************************************************/
191 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
193 decoder_sys_t *p_sys = p_dec->p_sys;
199 if( !pp_block || !*pp_block ) return NULL;
205 state = mpeg2_parse( p_sys->p_mpeg2dec );
210 if( !p_block->i_buffer )
212 block_Release( p_block );
216 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
218 p_sys->p_info->sequence &&
219 p_sys->p_info->sequence->width != (unsigned)-1 )
221 vout_SynchroReset( p_sys->p_synchro );
222 if( p_sys->p_info->current_fbuf != NULL
223 && p_sys->p_info->current_fbuf->id != NULL )
225 p_sys->b_garbage_pic = 1;
226 p_pic = p_sys->p_info->current_fbuf->id;
231 buf[0] = buf[1] = buf[2] = NULL;
232 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
234 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
236 p_sys->p_picture_to_destroy = p_pic;
238 if ( p_sys->b_slice_i )
240 vout_SynchroNewPicture( p_sys->p_synchro,
241 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
242 vout_SynchroDecode( p_sys->p_synchro );
243 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
250 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
252 #else /* New interface */
253 if( p_block->i_pts || p_block->i_dts )
255 mpeg2_tag_picture( p_sys->p_mpeg2dec,
256 (uint32_t)p_block->i_pts,
257 (uint32_t)p_block->i_dts );
259 p_sys->i_previous_pts = p_sys->i_current_pts;
260 p_sys->i_current_pts = p_block->i_pts;
261 p_sys->i_previous_dts = p_sys->i_current_dts;
262 p_sys->i_current_dts = p_block->i_dts;
265 p_sys->i_current_rate = p_block->i_rate;
267 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
268 p_block->p_buffer + p_block->i_buffer );
270 p_block->i_buffer = 0;
275 /* Initialize video output */
277 buf[0] = buf[1] = buf[2] = NULL;
279 /* Check whether the input gave a particular aspect ratio */
280 if( p_dec->fmt_in.video.i_aspect )
282 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
283 if( p_sys->i_aspect <= AR_221_1_PICTURE )
284 switch( p_sys->i_aspect )
287 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
289 case AR_16_9_PICTURE:
290 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
292 case AR_221_1_PICTURE:
293 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
295 case AR_SQUARE_PICTURE:
296 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
297 p_sys->p_info->sequence->width /
298 p_sys->p_info->sequence->height;
304 /* Use the value provided in the MPEG sequence header */
305 if( p_sys->p_info->sequence->pixel_height > 0 )
308 ((uint64_t)p_sys->p_info->sequence->display_width) *
309 p_sys->p_info->sequence->pixel_width *
311 p_sys->p_info->sequence->display_height /
312 p_sys->p_info->sequence->pixel_height;
316 /* Invalid aspect, assume 4:3.
317 * This shouldn't happen and if it does it is a bug
318 * in libmpeg2 (likely triggered by an invalid stream) */
319 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
323 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
324 p_sys->p_info->sequence->width,
325 p_sys->p_info->sequence->height, p_sys->i_aspect,
326 (uint32_t)((uint64_t)1001000000 * 27 /
327 p_sys->p_info->sequence->frame_period / 1001),
328 (uint32_t)((uint64_t)1001000000 * 27 /
329 p_sys->p_info->sequence->frame_period % 1001) );
331 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
333 /* Set the first 2 reference frames */
334 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
336 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
338 block_Release( p_block );
342 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
344 /* This picture will never go through display_picture. */
347 /* For some reason, libmpeg2 will put this pic twice in
348 * discard_picture. This can be considered a bug in libmpeg2. */
349 p_dec->pf_picture_link( p_dec, p_pic );
351 if( p_sys->p_synchro )
353 vout_SynchroRelease( p_sys->p_synchro );
355 p_sys->p_synchro = vout_SynchroInit( p_dec,
356 (uint32_t)((uint64_t)1001000000 * 27 /
357 p_sys->p_info->sequence->frame_period) );
358 p_sys->b_after_sequence_header = 1;
362 case STATE_PICTURE_2ND:
363 vout_SynchroNewPicture( p_sys->p_synchro,
364 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
365 p_sys->p_info->current_picture->nb_fields,
366 0, 0, p_sys->i_current_rate );
370 vout_SynchroTrash( p_sys->p_synchro );
374 vout_SynchroDecode( p_sys->p_synchro );
381 mtime_t i_pts, i_dts;
382 buf[0] = buf[1] = buf[2] = NULL;
384 if ( p_sys->b_after_sequence_header &&
385 ((p_sys->p_info->current_picture->flags &
386 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
388 /* Intra-slice refresh. Simulate a blank I picture. */
389 msg_Dbg( p_dec, "intra-slice refresh stream" );
390 vout_SynchroNewPicture( p_sys->p_synchro,
391 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
392 vout_SynchroDecode( p_sys->p_synchro );
393 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
394 p_sys->b_slice_i = 1;
396 p_sys->b_after_sequence_header = 0;
399 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
400 ( ( p_sys->p_info->current_picture->pts ==
401 (uint32_t)p_sys->i_current_pts ) ?
402 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
405 /* Hack to handle demuxers which only have DTS timestamps */
406 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
408 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
409 (p_sys->p_info->current_picture->flags &
410 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
412 i_pts = p_block->i_dts;
415 p_block->i_pts = p_block->i_dts = 0;
418 #else /* New interface */
420 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
421 ( ( p_sys->p_info->current_picture->tag ==
422 (uint32_t)p_sys->i_current_pts ) ?
423 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
424 i_dts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
425 ( ( p_sys->p_info->current_picture->tag2 ==
426 (uint32_t)p_sys->i_current_dts ) ?
427 p_sys->i_current_dts : p_sys->i_previous_dts ) : 0;
430 vout_SynchroNewPicture( p_sys->p_synchro,
431 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
432 p_sys->p_info->current_picture->nb_fields, i_pts, i_dts,
433 p_sys->i_current_rate );
435 if( !p_dec->b_pace_control &&
437 && ((p_sys->p_info->current_picture->flags
438 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
439 && !vout_SynchroChoose( p_sys->p_synchro,
440 p_sys->p_info->current_picture->flags
441 & PIC_MASK_CODING_TYPE,
442 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
444 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
446 vout_SynchroTrash( p_sys->p_synchro );
447 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
451 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
453 vout_SynchroDecode( p_sys->p_synchro );
455 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
457 block_Release( p_block );
461 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
469 if( p_sys->p_info->display_fbuf
470 && p_sys->p_info->display_fbuf->id )
472 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
474 vout_SynchroEnd( p_sys->p_synchro,
475 p_sys->p_info->display_picture->flags
476 & PIC_MASK_CODING_TYPE,
477 p_sys->b_garbage_pic );
478 p_sys->b_garbage_pic = 0;
480 if ( p_sys->p_picture_to_destroy != p_pic )
482 p_pic->date = vout_SynchroDate( p_sys->p_synchro );
486 p_sys->p_picture_to_destroy = NULL;
491 if( p_sys->p_info->discard_fbuf &&
492 p_sys->p_info->discard_fbuf->id )
494 p_dec->pf_picture_unlink( p_dec,
495 p_sys->p_info->discard_fbuf->id );
498 /* For still frames */
499 if( state == STATE_END && p_pic ) p_pic->b_force = VLC_TRUE;
503 /* Avoid frames with identical timestamps.
504 * Especially needed for still frames in DVD menus. */
505 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
506 p_sys->i_last_frame_pts = p_pic->date;
516 buf[0] = buf[1] = buf[2] = NULL;
518 msg_Warn( p_dec, "invalid picture encountered" );
519 if ( ( p_sys->p_info->current_picture == NULL ) ||
520 ( ( p_sys->p_info->current_picture->flags &
521 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
523 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
525 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
528 if( p_sys->p_info->current_fbuf &&
529 p_sys->p_info->current_fbuf->id )
531 p_sys->b_garbage_pic = 1;
532 p_pic = p_sys->p_info->current_fbuf->id;
534 else if( !p_sys->p_info->sequence )
540 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
542 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
544 p_sys->p_picture_to_destroy = p_pic;
546 memset( p_pic->p[0].p_pixels, 0,
547 p_sys->p_info->sequence->width
548 * p_sys->p_info->sequence->height );
549 memset( p_pic->p[1].p_pixels, 0x80,
550 p_sys->p_info->sequence->width
551 * p_sys->p_info->sequence->height / 4 );
552 memset( p_pic->p[2].p_pixels, 0x80,
553 p_sys->p_info->sequence->width
554 * p_sys->p_info->sequence->height / 4 );
556 if( p_sys->b_slice_i )
558 vout_SynchroNewPicture( p_sys->p_synchro,
559 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
560 vout_SynchroDecode( p_sys->p_synchro );
561 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
575 /*****************************************************************************
576 * CloseDecoder: libmpeg2 decoder destruction
577 *****************************************************************************/
578 static void CloseDecoder( vlc_object_t *p_this )
580 decoder_t *p_dec = (decoder_t *)p_this;
581 decoder_sys_t *p_sys = p_dec->p_sys;
583 if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
585 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
590 /*****************************************************************************
591 * GetNewPicture: Get a new picture from the vout and set the buf struct
592 *****************************************************************************/
593 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
595 decoder_sys_t *p_sys = p_dec->p_sys;
598 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
599 p_dec->fmt_out.video.i_visible_width =
600 p_sys->p_info->sequence->picture_width;
601 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
602 p_dec->fmt_out.video.i_visible_height =
603 p_sys->p_info->sequence->picture_height;
604 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
606 if( p_sys->p_info->sequence->frame_period > 0 )
608 p_dec->fmt_out.video.i_frame_rate =
609 (uint32_t)( (uint64_t)1001000000 * 27 /
610 p_sys->p_info->sequence->frame_period );
611 p_dec->fmt_out.video.i_frame_rate_base = 1001;
614 p_dec->fmt_out.i_codec =
615 ( p_sys->p_info->sequence->chroma_height <
616 p_sys->p_info->sequence->height ) ?
617 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
619 /* Get a new picture */
620 p_pic = p_dec->pf_vout_buffer_new( p_dec );
622 if( p_pic == NULL ) return NULL;
624 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
625 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
626 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
627 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
628 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
629 p_sys->p_info->current_picture->nb_fields : 2;
631 p_dec->pf_picture_link( p_dec, p_pic );
633 pp_buf[0] = p_pic->p[0].p_pixels;
634 pp_buf[1] = p_pic->p[1].p_pixels;
635 pp_buf[2] = p_pic->p[2].p_pixels;