1 /*****************************************************************************
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: libmpeg2.c,v 1.44 2004/02/25 18:22:54 fenrir Exp $
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;
57 pes_packet_t *p_pes; /* current PES we are decoding */
59 mtime_t i_previous_pts;
60 mtime_t i_current_pts;
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;
76 /*****************************************************************************
78 *****************************************************************************/
79 static int OpenDecoder( vlc_object_t * );
80 static void CloseDecoder( vlc_object_t * );
82 static picture_t *DecodeBlock( decoder_t *, block_t ** );
84 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
86 /*****************************************************************************
88 *****************************************************************************/
90 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
91 set_capability( "decoder", 150 );
92 set_callbacks( OpenDecoder, CloseDecoder );
93 add_shortcut( "libmpeg2" );
96 /*****************************************************************************
97 * OpenDecoder: probe the decoder and return score
98 *****************************************************************************/
99 static int OpenDecoder( vlc_object_t *p_this )
101 decoder_t *p_dec = (decoder_t*)p_this;
102 decoder_sys_t *p_sys;
103 uint32_t i_accel = 0;
105 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
106 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
107 /* Pinnacle hardware-mpeg1 */
108 p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
110 p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
111 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
116 /* Allocate the memory needed to store the decoder's structure */
117 if( ( p_dec->p_sys = p_sys =
118 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
120 msg_Err( p_dec, "out of memory" );
124 /* Initialize the thread properties */
125 memset( p_sys, 0, sizeof(decoder_sys_t) );
127 p_sys->p_mpeg2dec = NULL;
128 p_sys->p_synchro = NULL;
129 p_sys->p_info = NULL;
130 p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
131 p_sys->i_current_pts = 0;
132 p_sys->i_previous_pts = 0;
133 p_sys->p_picture_to_destroy = NULL;
134 p_sys->b_garbage_pic = 0;
135 p_sys->b_slice_i = 0;
138 #if defined( __i386__ )
139 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
141 i_accel |= MPEG2_ACCEL_X86_MMX;
144 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
146 i_accel |= MPEG2_ACCEL_X86_3DNOW;
149 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
151 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
154 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
155 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
157 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
161 /* If we do not know this CPU, trust libmpeg2's feature detection */
162 i_accel = MPEG2_ACCEL_DETECT;
166 /* Set CPU acceleration features */
167 mpeg2_accel( i_accel );
169 /* Initialize decoder */
170 p_sys->p_mpeg2dec = mpeg2_init();
171 if( p_sys->p_mpeg2dec == NULL)
173 msg_Err( p_dec, "mpeg2_init() failed" );
178 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
180 p_dec->pf_decode_video = DecodeBlock;
185 /*****************************************************************************
186 * RunDecoder: the libmpeg2 decoder
187 *****************************************************************************/
188 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
190 decoder_sys_t *p_sys = p_dec->p_sys;
196 if( !pp_block || !*pp_block ) return NULL;
202 state = mpeg2_parse( p_sys->p_mpeg2dec );
207 if( !p_block->i_buffer )
209 block_Release( p_block );
213 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
215 p_sys->p_info->sequence &&
216 p_sys->p_info->sequence->width != (unsigned)-1 )
218 vout_SynchroReset( p_sys->p_synchro );
219 if( p_sys->p_info->current_fbuf != NULL
220 && p_sys->p_info->current_fbuf->id != NULL )
222 p_sys->b_garbage_pic = 1;
223 p_pic = p_sys->p_info->current_fbuf->id;
228 buf[0] = buf[1] = buf[2] = NULL;
229 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
231 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
233 p_sys->p_picture_to_destroy = p_pic;
235 if ( p_sys->b_slice_i )
237 vout_SynchroNewPicture( p_sys->p_synchro,
238 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
239 vout_SynchroDecode( p_sys->p_synchro );
240 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
247 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
249 #else /* New interface */
250 mpeg2_tag_picture( p_sys->p_mpeg2dec,
251 (uint32_t)p_block->i_pts, 0/*dts*/ );
253 p_sys->i_previous_pts = p_sys->i_current_pts;
254 p_sys->i_current_pts = p_block->i_pts;
257 p_sys->i_current_rate = p_block->i_rate;
259 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
260 p_block->p_buffer + p_block->i_buffer );
262 p_block->i_buffer = 0;
267 /* Initialize video output */
269 buf[0] = buf[1] = buf[2] = NULL;
271 /* Check whether the input gave a particular aspect ratio */
272 if( p_dec->fmt_in.video.i_aspect )
274 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
275 if( p_sys->i_aspect <= AR_221_1_PICTURE )
276 switch( p_sys->i_aspect )
279 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
281 case AR_16_9_PICTURE:
282 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
284 case AR_221_1_PICTURE:
285 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
287 case AR_SQUARE_PICTURE:
288 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
289 p_sys->p_info->sequence->width /
290 p_sys->p_info->sequence->height;
296 /* Use the value provided in the MPEG sequence header */
298 ((uint64_t)p_sys->p_info->sequence->display_width) *
299 p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
300 p_sys->p_info->sequence->display_height /
301 p_sys->p_info->sequence->pixel_height;
304 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
305 p_sys->p_info->sequence->width,
306 p_sys->p_info->sequence->height, p_sys->i_aspect,
307 (uint32_t)((uint64_t)1001000000 * 27 /
308 p_sys->p_info->sequence->frame_period / 1001),
309 (uint32_t)((uint64_t)1001000000 * 27 /
310 p_sys->p_info->sequence->frame_period % 1001) );
312 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
314 /* Set the first 2 reference frames */
315 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
317 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
319 block_Release( p_block );
323 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
325 /* This picture will never go through display_picture. */
328 /* For some reason, libmpeg2 will put this pic twice in
329 * discard_picture. This can be considered a bug in libmpeg2. */
330 p_dec->pf_picture_link( p_dec, p_pic );
332 if( p_sys->p_synchro )
334 vout_SynchroRelease( p_sys->p_synchro );
336 p_sys->p_synchro = vout_SynchroInit( p_dec,
337 (uint32_t)((uint64_t)1001000000 * 27 /
338 p_sys->p_info->sequence->frame_period) );
339 p_sys->b_after_sequence_header = 1;
343 case STATE_PICTURE_2ND:
344 vout_SynchroNewPicture( p_sys->p_synchro,
345 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
346 p_sys->p_info->current_picture->nb_fields,
347 0, 0, p_sys->i_current_rate );
351 vout_SynchroTrash( p_sys->p_synchro );
355 vout_SynchroDecode( p_sys->p_synchro );
363 buf[0] = buf[1] = buf[2] = NULL;
365 if ( p_sys->b_after_sequence_header &&
366 ((p_sys->p_info->current_picture->flags &
367 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
369 /* Intra-slice refresh. Simulate a blank I picture. */
370 msg_Dbg( p_dec, "intra-slice refresh stream" );
371 vout_SynchroNewPicture( p_sys->p_synchro,
372 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
373 vout_SynchroDecode( p_sys->p_synchro );
374 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
375 p_sys->b_slice_i = 1;
377 p_sys->b_after_sequence_header = 0;
380 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
381 ( ( p_sys->p_info->current_picture->pts ==
382 (uint32_t)p_sys->i_current_pts ) ?
383 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
385 #else /* New interface */
387 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
388 ( ( p_sys->p_info->current_picture->tag ==
389 (uint32_t)p_sys->i_current_pts ) ?
390 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
393 /* Hack to handle demuxers which only have DTS timestamps */
394 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
396 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
397 (p_sys->p_info->current_picture->flags &
398 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
400 i_pts = p_block->i_dts;
403 p_block->i_pts = p_block->i_dts = 0;
406 vout_SynchroNewPicture( p_sys->p_synchro,
407 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
408 p_sys->p_info->current_picture->nb_fields, i_pts,
409 0, p_sys->i_current_rate );
411 if ( !(p_sys->b_slice_i
412 && ((p_sys->p_info->current_picture->flags
413 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
414 && !vout_SynchroChoose( p_sys->p_synchro,
415 p_sys->p_info->current_picture->flags
416 & PIC_MASK_CODING_TYPE,
417 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
419 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
421 vout_SynchroTrash( p_sys->p_synchro );
422 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
426 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
428 vout_SynchroDecode( p_sys->p_synchro );
430 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
432 block_Release( p_block );
436 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
444 if( p_sys->p_info->display_fbuf
445 && p_sys->p_info->display_fbuf->id )
447 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
449 vout_SynchroEnd( p_sys->p_synchro,
450 p_sys->p_info->display_picture->flags
451 & PIC_MASK_CODING_TYPE,
452 p_sys->b_garbage_pic );
453 p_sys->b_garbage_pic = 0;
455 if ( p_sys->p_picture_to_destroy != p_pic )
457 p_pic->date = vout_SynchroDate( p_sys->p_synchro );
461 p_sys->p_picture_to_destroy = NULL;
466 if( p_sys->p_info->discard_fbuf &&
467 p_sys->p_info->discard_fbuf->id )
469 p_dec->pf_picture_unlink( p_dec,
470 p_sys->p_info->discard_fbuf->id );
473 if( p_pic ) return p_pic;
480 buf[0] = buf[1] = buf[2] = NULL;
482 msg_Warn( p_dec, "invalid picture encountered" );
483 if ( ( p_sys->p_info->current_picture == NULL ) ||
484 ( ( p_sys->p_info->current_picture->flags &
485 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
487 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
489 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
492 if( p_sys->p_info->current_fbuf &&
493 p_sys->p_info->current_fbuf->id )
495 p_sys->b_garbage_pic = 1;
496 p_pic = p_sys->p_info->current_fbuf->id;
498 else if( !p_sys->p_info->sequence )
504 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
506 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
508 p_sys->p_picture_to_destroy = p_pic;
510 memset( p_pic->p[0].p_pixels, 0,
511 p_sys->p_info->sequence->width
512 * p_sys->p_info->sequence->height );
513 memset( p_pic->p[1].p_pixels, 0x80,
514 p_sys->p_info->sequence->width
515 * p_sys->p_info->sequence->height / 4 );
516 memset( p_pic->p[2].p_pixels, 0x80,
517 p_sys->p_info->sequence->width
518 * p_sys->p_info->sequence->height / 4 );
520 if( p_sys->b_slice_i )
522 vout_SynchroNewPicture( p_sys->p_synchro,
523 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
524 vout_SynchroDecode( p_sys->p_synchro );
525 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
539 /*****************************************************************************
540 * CloseDecoder: libmpeg2 decoder destruction
541 *****************************************************************************/
542 static void CloseDecoder( vlc_object_t *p_this )
544 decoder_t *p_dec = (decoder_t *)p_this;
545 decoder_sys_t *p_sys = p_dec->p_sys;
547 if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
549 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
554 /*****************************************************************************
555 * GetNewPicture: Get a new picture from the vout and set the buf struct
556 *****************************************************************************/
557 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
559 decoder_sys_t *p_sys = p_dec->p_sys;
562 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
563 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
564 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
566 p_dec->fmt_out.i_codec =
567 ( p_sys->p_info->sequence->chroma_height <
568 p_sys->p_info->sequence->height ) ?
569 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
571 /* Get a new picture */
572 p_pic = p_dec->pf_vout_buffer_new( p_dec );
574 if( p_pic == NULL ) return NULL;
576 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
577 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
578 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
579 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
580 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
581 p_sys->p_info->current_picture->nb_fields : 2;
583 p_dec->pf_picture_link( p_dec, p_pic );
585 pp_buf[0] = p_pic->p[0].p_pixels;
586 pp_buf[1] = p_pic->p[1].p_pixels;
587 pp_buf[2] = p_pic->p[2].p_pixels;