1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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;
58 mtime_t i_previous_pts;
59 mtime_t i_current_pts;
61 picture_t * p_picture_to_destroy;
62 vlc_bool_t b_garbage_pic;
63 vlc_bool_t b_after_sequence_header; /* is it the next frame after
64 * the sequence header ? */
65 vlc_bool_t b_slice_i; /* intra-slice refresh stream */
70 vout_synchro_t *p_synchro;
75 /*****************************************************************************
77 *****************************************************************************/
78 static int OpenDecoder( vlc_object_t * );
79 static void CloseDecoder( vlc_object_t * );
81 static picture_t *DecodeBlock( decoder_t *, block_t ** );
83 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
85 /*****************************************************************************
87 *****************************************************************************/
89 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
90 set_capability( "decoder", 150 );
91 set_callbacks( OpenDecoder, CloseDecoder );
92 add_shortcut( "libmpeg2" );
95 /*****************************************************************************
96 * OpenDecoder: probe the decoder and return score
97 *****************************************************************************/
98 static int OpenDecoder( vlc_object_t *p_this )
100 decoder_t *p_dec = (decoder_t*)p_this;
101 decoder_sys_t *p_sys;
102 uint32_t i_accel = 0;
104 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
105 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
106 /* Pinnacle hardware-mpeg1 */
107 p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
109 p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
110 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
115 /* Allocate the memory needed to store the decoder's structure */
116 if( ( p_dec->p_sys = p_sys =
117 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
119 msg_Err( p_dec, "out of memory" );
123 /* Initialize the thread properties */
124 memset( p_sys, 0, sizeof(decoder_sys_t) );
125 p_sys->p_mpeg2dec = NULL;
126 p_sys->p_synchro = NULL;
127 p_sys->p_info = NULL;
128 p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
129 p_sys->i_current_pts = 0;
130 p_sys->i_previous_pts = 0;
131 p_sys->p_picture_to_destroy = NULL;
132 p_sys->b_garbage_pic = 0;
133 p_sys->b_slice_i = 0;
136 #if defined( __i386__ )
137 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
139 i_accel |= MPEG2_ACCEL_X86_MMX;
142 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
144 i_accel |= MPEG2_ACCEL_X86_3DNOW;
147 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
149 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
152 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
153 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
155 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
159 /* If we do not know this CPU, trust libmpeg2's feature detection */
160 i_accel = MPEG2_ACCEL_DETECT;
164 /* Set CPU acceleration features */
165 mpeg2_accel( i_accel );
167 /* Initialize decoder */
168 p_sys->p_mpeg2dec = mpeg2_init();
169 if( p_sys->p_mpeg2dec == NULL)
171 msg_Err( p_dec, "mpeg2_init() failed" );
176 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
178 p_dec->pf_decode_video = DecodeBlock;
183 /*****************************************************************************
184 * RunDecoder: the libmpeg2 decoder
185 *****************************************************************************/
186 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
188 decoder_sys_t *p_sys = p_dec->p_sys;
194 if( !pp_block || !*pp_block ) return NULL;
200 state = mpeg2_parse( p_sys->p_mpeg2dec );
205 if( !p_block->i_buffer )
207 block_Release( p_block );
211 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
213 p_sys->p_info->sequence &&
214 p_sys->p_info->sequence->width != (unsigned)-1 )
216 vout_SynchroReset( p_sys->p_synchro );
217 if( p_sys->p_info->current_fbuf != NULL
218 && p_sys->p_info->current_fbuf->id != NULL )
220 p_sys->b_garbage_pic = 1;
221 p_pic = p_sys->p_info->current_fbuf->id;
226 buf[0] = buf[1] = buf[2] = NULL;
227 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
229 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
231 p_sys->p_picture_to_destroy = p_pic;
233 if ( p_sys->b_slice_i )
235 vout_SynchroNewPicture( p_sys->p_synchro,
236 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
237 vout_SynchroDecode( p_sys->p_synchro );
238 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
245 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
247 #else /* New interface */
248 mpeg2_tag_picture( p_sys->p_mpeg2dec,
249 (uint32_t)p_block->i_pts, 0/*dts*/ );
251 p_sys->i_previous_pts = p_sys->i_current_pts;
252 p_sys->i_current_pts = p_block->i_pts;
255 p_sys->i_current_rate = p_block->i_rate;
257 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
258 p_block->p_buffer + p_block->i_buffer );
260 p_block->i_buffer = 0;
265 /* Initialize video output */
267 buf[0] = buf[1] = buf[2] = NULL;
269 /* Check whether the input gave a particular aspect ratio */
270 if( p_dec->fmt_in.video.i_aspect )
272 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
273 if( p_sys->i_aspect <= AR_221_1_PICTURE )
274 switch( p_sys->i_aspect )
277 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
279 case AR_16_9_PICTURE:
280 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
282 case AR_221_1_PICTURE:
283 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
285 case AR_SQUARE_PICTURE:
286 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
287 p_sys->p_info->sequence->width /
288 p_sys->p_info->sequence->height;
294 /* Use the value provided in the MPEG sequence header */
295 if( p_sys->p_info->sequence->pixel_height > 0 )
298 ((uint64_t)p_sys->p_info->sequence->display_width) *
299 p_sys->p_info->sequence->pixel_width *
301 p_sys->p_info->sequence->display_height /
302 p_sys->p_info->sequence->pixel_height;
306 /* Invalid aspect, assume 4:3.
307 * This shouldn't happen and if it does it is a bug
308 * in libmpeg2 (likely triggered by an invalid stream) */
309 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
313 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
314 p_sys->p_info->sequence->width,
315 p_sys->p_info->sequence->height, p_sys->i_aspect,
316 (uint32_t)((uint64_t)1001000000 * 27 /
317 p_sys->p_info->sequence->frame_period / 1001),
318 (uint32_t)((uint64_t)1001000000 * 27 /
319 p_sys->p_info->sequence->frame_period % 1001) );
321 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
323 /* Set the first 2 reference frames */
324 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
326 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
328 block_Release( p_block );
332 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
334 /* This picture will never go through display_picture. */
337 /* For some reason, libmpeg2 will put this pic twice in
338 * discard_picture. This can be considered a bug in libmpeg2. */
339 p_dec->pf_picture_link( p_dec, p_pic );
341 if( p_sys->p_synchro )
343 vout_SynchroRelease( p_sys->p_synchro );
345 p_sys->p_synchro = vout_SynchroInit( p_dec,
346 (uint32_t)((uint64_t)1001000000 * 27 /
347 p_sys->p_info->sequence->frame_period) );
348 p_sys->b_after_sequence_header = 1;
352 case STATE_PICTURE_2ND:
353 vout_SynchroNewPicture( p_sys->p_synchro,
354 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
355 p_sys->p_info->current_picture->nb_fields,
356 0, 0, p_sys->i_current_rate );
360 vout_SynchroTrash( p_sys->p_synchro );
364 vout_SynchroDecode( p_sys->p_synchro );
372 buf[0] = buf[1] = buf[2] = NULL;
374 if ( p_sys->b_after_sequence_header &&
375 ((p_sys->p_info->current_picture->flags &
376 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
378 /* Intra-slice refresh. Simulate a blank I picture. */
379 msg_Dbg( p_dec, "intra-slice refresh stream" );
380 vout_SynchroNewPicture( p_sys->p_synchro,
381 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
382 vout_SynchroDecode( p_sys->p_synchro );
383 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
384 p_sys->b_slice_i = 1;
386 p_sys->b_after_sequence_header = 0;
389 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
390 ( ( p_sys->p_info->current_picture->pts ==
391 (uint32_t)p_sys->i_current_pts ) ?
392 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
394 #else /* New interface */
396 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
397 ( ( p_sys->p_info->current_picture->tag ==
398 (uint32_t)p_sys->i_current_pts ) ?
399 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
402 /* Hack to handle demuxers which only have DTS timestamps */
403 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
405 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
406 (p_sys->p_info->current_picture->flags &
407 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
409 i_pts = p_block->i_dts;
412 p_block->i_pts = p_block->i_dts = 0;
415 vout_SynchroNewPicture( p_sys->p_synchro,
416 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
417 p_sys->p_info->current_picture->nb_fields, i_pts,
418 0, p_sys->i_current_rate );
420 if ( !(p_sys->b_slice_i
421 && ((p_sys->p_info->current_picture->flags
422 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
423 && !vout_SynchroChoose( p_sys->p_synchro,
424 p_sys->p_info->current_picture->flags
425 & PIC_MASK_CODING_TYPE,
426 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
428 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
430 vout_SynchroTrash( p_sys->p_synchro );
431 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
435 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
437 vout_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 );
453 if( p_sys->p_info->display_fbuf
454 && p_sys->p_info->display_fbuf->id )
456 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
458 vout_SynchroEnd( p_sys->p_synchro,
459 p_sys->p_info->display_picture->flags
460 & PIC_MASK_CODING_TYPE,
461 p_sys->b_garbage_pic );
462 p_sys->b_garbage_pic = 0;
464 if ( p_sys->p_picture_to_destroy != p_pic )
466 p_pic->date = vout_SynchroDate( p_sys->p_synchro );
470 p_sys->p_picture_to_destroy = NULL;
475 if( p_sys->p_info->discard_fbuf &&
476 p_sys->p_info->discard_fbuf->id )
478 p_dec->pf_picture_unlink( p_dec,
479 p_sys->p_info->discard_fbuf->id );
482 if( p_pic ) return p_pic;
489 buf[0] = buf[1] = buf[2] = NULL;
491 msg_Warn( p_dec, "invalid picture encountered" );
492 if ( ( p_sys->p_info->current_picture == NULL ) ||
493 ( ( p_sys->p_info->current_picture->flags &
494 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
496 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
498 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
501 if( p_sys->p_info->current_fbuf &&
502 p_sys->p_info->current_fbuf->id )
504 p_sys->b_garbage_pic = 1;
505 p_pic = p_sys->p_info->current_fbuf->id;
507 else if( !p_sys->p_info->sequence )
513 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
515 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
517 p_sys->p_picture_to_destroy = p_pic;
519 memset( p_pic->p[0].p_pixels, 0,
520 p_sys->p_info->sequence->width
521 * p_sys->p_info->sequence->height );
522 memset( p_pic->p[1].p_pixels, 0x80,
523 p_sys->p_info->sequence->width
524 * p_sys->p_info->sequence->height / 4 );
525 memset( p_pic->p[2].p_pixels, 0x80,
526 p_sys->p_info->sequence->width
527 * p_sys->p_info->sequence->height / 4 );
529 if( p_sys->b_slice_i )
531 vout_SynchroNewPicture( p_sys->p_synchro,
532 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
533 vout_SynchroDecode( p_sys->p_synchro );
534 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
548 /*****************************************************************************
549 * CloseDecoder: libmpeg2 decoder destruction
550 *****************************************************************************/
551 static void CloseDecoder( vlc_object_t *p_this )
553 decoder_t *p_dec = (decoder_t *)p_this;
554 decoder_sys_t *p_sys = p_dec->p_sys;
556 if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
558 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
563 /*****************************************************************************
564 * GetNewPicture: Get a new picture from the vout and set the buf struct
565 *****************************************************************************/
566 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
568 decoder_sys_t *p_sys = p_dec->p_sys;
571 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
572 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
573 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
575 p_dec->fmt_out.i_codec =
576 ( p_sys->p_info->sequence->chroma_height <
577 p_sys->p_info->sequence->height ) ?
578 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
580 /* Get a new picture */
581 p_pic = p_dec->pf_vout_buffer_new( p_dec );
583 if( p_pic == NULL ) return NULL;
585 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
586 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
587 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
588 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
589 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
590 p_sys->p_info->current_picture->nb_fields : 2;
592 p_dec->pf_picture_link( p_dec, p_pic );
594 pp_buf[0] = p_pic->p[0].p_pixels;
595 pp_buf[1] = p_pic->p[1].p_pixels;
596 pp_buf[2] = p_pic->p[2].p_pixels;