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.39 2003/12/22 14:32:55 sam 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;
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) );
126 p_sys->p_mpeg2dec = NULL;
127 p_sys->p_synchro = NULL;
128 p_sys->p_info = NULL;
129 p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
130 p_sys->i_current_pts = 0;
131 p_sys->i_previous_pts = 0;
132 p_sys->p_picture_to_destroy = NULL;
133 p_sys->b_garbage_pic = 0;
134 p_sys->b_slice_i = 0;
137 /* Initialize decoder */
138 p_sys->p_mpeg2dec = mpeg2_init();
139 if( p_sys->p_mpeg2dec == NULL)
141 msg_Err( p_dec, "mpeg2_init() failed" );
146 p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
148 p_dec->pf_decode_video = DecodeBlock;
153 /*****************************************************************************
154 * RunDecoder: the libmpeg2 decoder
155 *****************************************************************************/
156 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
158 decoder_sys_t *p_sys = p_dec->p_sys;
164 if( !pp_block || !*pp_block ) return NULL;
170 state = mpeg2_parse( p_sys->p_mpeg2dec );
175 if( !p_block->i_buffer )
177 block_Release( p_block );
181 if( p_block->b_discontinuity && p_sys->p_synchro &&
182 p_sys->p_info->sequence &&
183 p_sys->p_info->sequence->width != (unsigned)-1 )
185 vout_SynchroReset( p_sys->p_synchro );
186 if( p_sys->p_info->current_fbuf != NULL
187 && p_sys->p_info->current_fbuf->id != NULL )
189 p_sys->b_garbage_pic = 1;
190 p_pic = p_sys->p_info->current_fbuf->id;
195 buf[0] = buf[1] = buf[2] = NULL;
196 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
198 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
200 p_sys->p_picture_to_destroy = p_pic;
202 if ( p_sys->b_slice_i )
204 vout_SynchroNewPicture( p_sys->p_synchro,
205 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
206 vout_SynchroDecode( p_sys->p_synchro );
207 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
213 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
214 p_sys->i_previous_pts = p_sys->i_current_pts;
215 p_sys->i_current_pts = p_block->i_pts;
218 p_sys->i_current_rate = DEFAULT_RATE;//p_pes->i_rate;
220 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
221 p_block->p_buffer + p_block->i_buffer );
223 p_block->i_buffer = 0;
228 /* Initialize video output */
230 buf[0] = buf[1] = buf[2] = NULL;
232 /* Check whether the input gave a particular aspect ratio */
233 if( p_dec->fmt_in.video.i_aspect )
235 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
236 if( p_sys->i_aspect <= AR_221_1_PICTURE )
237 switch( p_sys->i_aspect )
240 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
242 case AR_16_9_PICTURE:
243 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
245 case AR_221_1_PICTURE:
246 p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
248 case AR_SQUARE_PICTURE:
249 p_sys->i_aspect = VOUT_ASPECT_FACTOR *
250 p_sys->p_info->sequence->width /
251 p_sys->p_info->sequence->height;
257 /* Use the value provided in the MPEG sequence header */
259 ((uint64_t)p_sys->p_info->sequence->display_width) *
260 p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
261 p_sys->p_info->sequence->display_height /
262 p_sys->p_info->sequence->pixel_height;
265 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
266 p_sys->p_info->sequence->width,
267 p_sys->p_info->sequence->height, p_sys->i_aspect,
268 (uint32_t)((uint64_t)1001000000 * 27 /
269 p_sys->p_info->sequence->frame_period / 1001),
270 (uint32_t)((uint64_t)1001000000 * 27 /
271 p_sys->p_info->sequence->frame_period % 1001) );
273 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
275 /* Set the first 2 reference frames */
276 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
278 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
280 block_Release( p_block );
284 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
286 /* This picture will never go through display_picture. */
289 /* For some reason, libmpeg2 will put this pic twice in
290 * discard_picture. This can be considered a bug in libmpeg2. */
291 p_dec->pf_picture_link( p_dec, p_pic );
293 if( p_sys->p_synchro )
295 vout_SynchroRelease( p_sys->p_synchro );
297 p_sys->p_synchro = vout_SynchroInit( p_dec,
298 (uint32_t)((uint64_t)1001000000 * 27 /
299 p_sys->p_info->sequence->frame_period) );
300 p_sys->b_after_sequence_header = 1;
304 case STATE_PICTURE_2ND:
305 vout_SynchroNewPicture( p_sys->p_synchro,
306 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
307 p_sys->p_info->current_picture->nb_fields,
308 0, 0, p_sys->i_current_rate );
312 vout_SynchroTrash( p_sys->p_synchro );
316 vout_SynchroDecode( p_sys->p_synchro );
324 buf[0] = buf[1] = buf[2] = NULL;
326 if ( p_sys->b_after_sequence_header &&
327 ((p_sys->p_info->current_picture->flags &
328 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
330 /* Intra-slice refresh. Simulate a blank I picture. */
331 msg_Dbg( p_dec, "intra-slice refresh stream" );
332 vout_SynchroNewPicture( p_sys->p_synchro,
333 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
334 vout_SynchroDecode( p_sys->p_synchro );
335 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
336 p_sys->b_slice_i = 1;
338 p_sys->b_after_sequence_header = 0;
340 i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
341 ( ( p_sys->p_info->current_picture->pts ==
342 (uint32_t)p_sys->i_current_pts ) ?
343 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
345 /* Hack to handle demuxers which only have DTS timestamps */
346 if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
348 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
349 (p_sys->p_info->current_picture->flags &
350 PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
352 i_pts = p_block->i_dts;
355 p_block->i_pts = p_block->i_dts = 0;
358 vout_SynchroNewPicture( p_sys->p_synchro,
359 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
360 p_sys->p_info->current_picture->nb_fields, i_pts,
361 0, p_sys->i_current_rate );
363 if ( !(p_sys->b_slice_i
364 && ((p_sys->p_info->current_picture->flags
365 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
366 && !vout_SynchroChoose( p_sys->p_synchro,
367 p_sys->p_info->current_picture->flags
368 & PIC_MASK_CODING_TYPE,
369 /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
371 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
373 vout_SynchroTrash( p_sys->p_synchro );
374 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
378 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
380 vout_SynchroDecode( p_sys->p_synchro );
382 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
384 block_Release( p_block );
388 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
396 if( p_sys->p_info->display_fbuf
397 && p_sys->p_info->display_fbuf->id )
399 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
401 vout_SynchroEnd( p_sys->p_synchro,
402 p_sys->p_info->display_picture->flags
403 & PIC_MASK_CODING_TYPE,
404 p_sys->b_garbage_pic );
405 p_sys->b_garbage_pic = 0;
407 if ( p_sys->p_picture_to_destroy != p_pic )
409 p_pic->date = vout_SynchroDate( p_sys->p_synchro );
413 p_sys->p_picture_to_destroy = NULL;
418 if( p_sys->p_info->discard_fbuf &&
419 p_sys->p_info->discard_fbuf->id )
421 p_dec->pf_picture_unlink( p_dec,
422 p_sys->p_info->discard_fbuf->id );
425 if( p_pic ) return p_pic;
432 buf[0] = buf[1] = buf[2] = NULL;
434 msg_Warn( p_dec, "invalid picture encountered" );
435 if ( ( p_sys->p_info->current_picture == NULL ) ||
436 ( ( p_sys->p_info->current_picture->flags &
437 PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
439 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
441 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
444 if( p_sys->p_info->current_fbuf &&
445 p_sys->p_info->current_fbuf->id )
447 p_sys->b_garbage_pic = 1;
448 p_pic = p_sys->p_info->current_fbuf->id;
450 else if( !p_sys->p_info->sequence )
456 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
458 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
460 p_sys->p_picture_to_destroy = p_pic;
462 memset( p_pic->p[0].p_pixels, 0,
463 p_sys->p_info->sequence->width
464 * p_sys->p_info->sequence->height );
465 memset( p_pic->p[1].p_pixels, 0x80,
466 p_sys->p_info->sequence->width
467 * p_sys->p_info->sequence->height / 4 );
468 memset( p_pic->p[2].p_pixels, 0x80,
469 p_sys->p_info->sequence->width
470 * p_sys->p_info->sequence->height / 4 );
472 if( p_sys->b_slice_i )
474 vout_SynchroNewPicture( p_sys->p_synchro,
475 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
476 vout_SynchroDecode( p_sys->p_synchro );
477 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
491 /*****************************************************************************
492 * CloseDecoder: libmpeg2 decoder destruction
493 *****************************************************************************/
494 static void CloseDecoder( vlc_object_t *p_this )
496 decoder_t *p_dec = (decoder_t *)p_this;
497 decoder_sys_t *p_sys = p_dec->p_sys;
499 if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
501 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
506 /*****************************************************************************
507 * GetNewPicture: Get a new picture from the vout and set the buf struct
508 *****************************************************************************/
509 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
511 decoder_sys_t *p_sys = p_dec->p_sys;
514 p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
515 p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
516 p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
518 p_dec->fmt_out.i_codec =
519 ( p_sys->p_info->sequence->chroma_height <
520 p_sys->p_info->sequence->height ) ?
521 VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
523 /* Get a new picture */
524 p_pic = p_dec->pf_vout_buffer_new( p_dec );
526 if( p_pic == NULL ) return NULL;
528 p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
529 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
530 p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
531 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
532 p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
533 p_sys->p_info->current_picture->nb_fields : 2;
535 p_dec->pf_picture_link( p_dec, p_pic );
537 pp_buf[0] = p_pic->p[0].p_pixels;
538 pp_buf[1] = p_pic->p[1].p_pixels;
539 pp_buf[2] = p_pic->p[2].p_pixels;