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.43 2004/02/25 17:48:52 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>
33 #include <mpeg2dec/mpeg2.h>
35 #include "vout_synchro.h"
37 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
38 #define AR_SQUARE_PICTURE 1 /* square pixels */
39 #define AR_3_4_PICTURE 2 /* 3:4 picture (TV) */
40 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
41 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
43 /*****************************************************************************
44 * decoder_sys_t : libmpeg2 decoder descriptor
45 *****************************************************************************/
51 mpeg2dec_t *p_mpeg2dec;
52 const mpeg2_info_t *p_info;
58 pes_packet_t *p_pes; /* current PES we are decoding */
60 mtime_t i_previous_pts;
61 mtime_t i_current_pts;
63 picture_t * p_picture_to_destroy;
64 vlc_bool_t b_garbage_pic;
65 vlc_bool_t b_after_sequence_header; /* is it the next frame after
66 * the sequence header ? */
67 vlc_bool_t b_slice_i; /* intra-slice refresh stream */
72 vout_synchro_t *p_synchro;
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_callbacks( OpenDecoder, CloseDecoder );
94 add_shortcut( "libmpeg2" );
97 /*****************************************************************************
98 * OpenDecoder: probe the decoder and return score
99 *****************************************************************************/
100 static int OpenDecoder( vlc_object_t *p_this )
102 decoder_t *p_dec = (decoder_t*)p_this;
103 decoder_sys_t *p_sys;
104 uint32_t i_accel = 0;
106 if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
107 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
108 /* Pinnacle hardware-mpeg1 */
109 p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
111 p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
112 p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
117 /* Allocate the memory needed to store the decoder's structure */
118 if( ( p_dec->p_sys = p_sys =
119 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
121 msg_Err( p_dec, "out of memory" );
125 /* Initialize the thread properties */
126 memset( p_sys, 0, sizeof(decoder_sys_t) );
128 p_sys->p_mpeg2dec = NULL;
129 p_sys->p_synchro = NULL;
130 p_sys->p_info = NULL;
131 p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
132 p_sys->i_current_pts = 0;
133 p_sys->i_previous_pts = 0;
134 p_sys->p_picture_to_destroy = NULL;
135 p_sys->b_garbage_pic = 0;
136 p_sys->b_slice_i = 0;
139 #if defined( __i386__ )
140 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
142 i_accel |= MPEG2_ACCEL_X86_MMX;
145 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
147 i_accel |= MPEG2_ACCEL_X86_3DNOW;
150 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
152 i_accel |= MPEG2_ACCEL_X86_MMXEXT;
155 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
156 if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
158 i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
162 /* If we do not know this CPU, trust libmpeg2's feature detection */
163 i_accel = MPEG2_ACCEL_DETECT;
167 /* Set CPU acceleration features */
168 mpeg2_accel( i_accel );
170 /* Initialize decoder */
171 p_sys->p_mpeg2dec = mpeg2_init();
172 if( p_sys->p_mpeg2dec == NULL)
174 msg_Err( p_dec, "mpeg2_init() failed" );
179 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
181 p_dec->pf_decode_video = DecodeBlock;
186 /*****************************************************************************
187 * RunDecoder: the libmpeg2 decoder
188 *****************************************************************************/
189 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
191 decoder_sys_t *p_sys = p_dec->p_sys;
197 if( !pp_block || !*pp_block ) return NULL;
203 state = mpeg2_parse( p_sys->p_mpeg2dec );
208 if( !p_block->i_buffer )
210 block_Release( p_block );
214 if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
216 p_sys->p_info->sequence &&
217 p_sys->p_info->sequence->width != (unsigned)-1 )
219 vout_SynchroReset( p_sys->p_synchro );
220 if( p_sys->p_info->current_fbuf != NULL
221 && p_sys->p_info->current_fbuf->id != NULL )
223 p_sys->b_garbage_pic = 1;
224 p_pic = p_sys->p_info->current_fbuf->id;
229 buf[0] = buf[1] = buf[2] = NULL;
230 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
232 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
234 p_sys->p_picture_to_destroy = p_pic;
236 if ( p_sys->b_slice_i )
238 vout_SynchroNewPicture( p_sys->p_synchro,
239 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
240 vout_SynchroDecode( p_sys->p_synchro );
241 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
248 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
250 #else /* New interface */
251 mpeg2_tag_picture( p_sys->p_mpeg2dec,
252 (uint32_t)p_block->i_pts, 0/*dts*/ );
254 p_sys->i_previous_pts = p_sys->i_current_pts;
255 p_sys->i_current_pts = p_block->i_pts;
258 p_sys->i_current_rate = p_block->i_rate;
260 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
261 p_block->p_buffer + p_block->i_buffer );
263 p_block->i_buffer = 0;
268 /* Initialize video output */
270 buf[0] = buf[1] = buf[2] = NULL;
272 /* Check whether the input gave a particular aspect ratio */
273 if( p_dec->fmt_in.video.i_aspect )
275 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
276 if( p_sys->i_aspect <= AR_221_1_PICTURE )
277 switch( p_sys->i_aspect )
280 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
282 case AR_16_9_PICTURE:
283 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
285 case AR_221_1_PICTURE:
286 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
288 case AR_SQUARE_PICTURE:
289 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
290 p_sys->p_info->sequence->width /
291 p_sys->p_info->sequence->height;
297 /* Use the value provided in the MPEG sequence header */
299 ((uint64_t)p_sys->p_info->sequence->display_width) *
300 p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
301 p_sys->p_info->sequence->display_height /
302 p_sys->p_info->sequence->pixel_height;
305 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
306 p_sys->p_info->sequence->width,
307 p_sys->p_info->sequence->height, p_sys->i_aspect,
308 (uint32_t)((uint64_t)1001000000 * 27 /
309 p_sys->p_info->sequence->frame_period / 1001),
310 (uint32_t)((uint64_t)1001000000 * 27 /
311 p_sys->p_info->sequence->frame_period % 1001) );
313 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
315 /* Set the first 2 reference frames */
316 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
318 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
320 block_Release( p_block );
324 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
326 /* This picture will never go through display_picture. */
329 /* For some reason, libmpeg2 will put this pic twice in
330 * discard_picture. This can be considered a bug in libmpeg2. */
331 p_dec->pf_picture_link( p_dec, p_pic );
333 if( p_sys->p_synchro )
335 vout_SynchroRelease( p_sys->p_synchro );
337 p_sys->p_synchro = vout_SynchroInit( p_dec,
338 (uint32_t)((uint64_t)1001000000 * 27 /
339 p_sys->p_info->sequence->frame_period) );
340 p_sys->b_after_sequence_header = 1;
344 case STATE_PICTURE_2ND:
345 vout_SynchroNewPicture( p_sys->p_synchro,
346 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
347 p_sys->p_info->current_picture->nb_fields,
348 0, 0, p_sys->i_current_rate );
352 vout_SynchroTrash( p_sys->p_synchro );
356 vout_SynchroDecode( p_sys->p_synchro );
364 buf[0] = buf[1] = buf[2] = NULL;
366 if ( p_sys->b_after_sequence_header &&
367 ((p_sys->p_info->current_picture->flags &
368 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
370 /* Intra-slice refresh. Simulate a blank I picture. */
371 msg_Dbg( p_dec, "intra-slice refresh stream" );
372 vout_SynchroNewPicture( p_sys->p_synchro,
373 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
374 vout_SynchroDecode( p_sys->p_synchro );
375 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
376 p_sys->b_slice_i = 1;
378 p_sys->b_after_sequence_header = 0;
381 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
382 ( ( p_sys->p_info->current_picture->pts ==
383 (uint32_t)p_sys->i_current_pts ) ?
384 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
386 #else /* New interface */
388 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
389 ( ( p_sys->p_info->current_picture->tag ==
390 (uint32_t)p_sys->i_current_pts ) ?
391 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
394 /* Hack to handle demuxers which only have DTS timestamps */
395 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
397 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
398 (p_sys->p_info->current_picture->flags &
399 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
401 i_pts = p_block->i_dts;
404 p_block->i_pts = p_block->i_dts = 0;
407 vout_SynchroNewPicture( p_sys->p_synchro,
408 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
409 p_sys->p_info->current_picture->nb_fields, i_pts,
410 0, p_sys->i_current_rate );
412 if ( !(p_sys->b_slice_i
413 && ((p_sys->p_info->current_picture->flags
414 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
415 && !vout_SynchroChoose( p_sys->p_synchro,
416 p_sys->p_info->current_picture->flags
417 & PIC_MASK_CODING_TYPE,
418 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
420 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
422 vout_SynchroTrash( p_sys->p_synchro );
423 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
427 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
429 vout_SynchroDecode( p_sys->p_synchro );
431 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
433 block_Release( p_block );
437 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
445 if( p_sys->p_info->display_fbuf
446 && p_sys->p_info->display_fbuf->id )
448 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
450 vout_SynchroEnd( p_sys->p_synchro,
451 p_sys->p_info->display_picture->flags
452 & PIC_MASK_CODING_TYPE,
453 p_sys->b_garbage_pic );
454 p_sys->b_garbage_pic = 0;
456 if ( p_sys->p_picture_to_destroy != p_pic )
458 p_pic->date = vout_SynchroDate( p_sys->p_synchro );
462 p_sys->p_picture_to_destroy = NULL;
467 if( p_sys->p_info->discard_fbuf &&
468 p_sys->p_info->discard_fbuf->id )
470 p_dec->pf_picture_unlink( p_dec,
471 p_sys->p_info->discard_fbuf->id );
474 if( p_pic ) return p_pic;
481 buf[0] = buf[1] = buf[2] = NULL;
483 msg_Warn( p_dec, "invalid picture encountered" );
484 if ( ( p_sys->p_info->current_picture == NULL ) ||
485 ( ( p_sys->p_info->current_picture->flags &
486 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
488 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
490 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
493 if( p_sys->p_info->current_fbuf &&
494 p_sys->p_info->current_fbuf->id )
496 p_sys->b_garbage_pic = 1;
497 p_pic = p_sys->p_info->current_fbuf->id;
499 else if( !p_sys->p_info->sequence )
505 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
507 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
509 p_sys->p_picture_to_destroy = p_pic;
511 memset( p_pic->p[0].p_pixels, 0,
512 p_sys->p_info->sequence->width
513 * p_sys->p_info->sequence->height );
514 memset( p_pic->p[1].p_pixels, 0x80,
515 p_sys->p_info->sequence->width
516 * p_sys->p_info->sequence->height / 4 );
517 memset( p_pic->p[2].p_pixels, 0x80,
518 p_sys->p_info->sequence->width
519 * p_sys->p_info->sequence->height / 4 );
521 if( p_sys->b_slice_i )
523 vout_SynchroNewPicture( p_sys->p_synchro,
524 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
525 vout_SynchroDecode( p_sys->p_synchro );
526 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
540 /*****************************************************************************
541 * CloseDecoder: libmpeg2 decoder destruction
542 *****************************************************************************/
543 static void CloseDecoder( vlc_object_t *p_this )
545 decoder_t *p_dec = (decoder_t *)p_this;
546 decoder_sys_t *p_sys = p_dec->p_sys;
548 if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
550 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
555 /*****************************************************************************
556 * GetNewPicture: Get a new picture from the vout and set the buf struct
557 *****************************************************************************/
558 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
560 decoder_sys_t *p_sys = p_dec->p_sys;
563 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
564 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
565 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
567 p_dec->fmt_out.i_codec =
568 ( p_sys->p_info->sequence->chroma_height <
569 p_sys->p_info->sequence->height ) ?
570 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
572 /* Get a new picture */
573 p_pic = p_dec->pf_vout_buffer_new( p_dec );
575 if( p_pic == NULL ) return NULL;
577 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
578 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
579 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
580 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
581 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
582 p_sys->p_info->current_picture->nb_fields : 2;
584 p_dec->pf_picture_link( p_dec, p_pic );
586 pp_buf[0] = p_pic->p[0].p_pixels;
587 pp_buf[1] = p_pic->p[1].p_pixels;
588 pp_buf[2] = p_pic->p[2].p_pixels;