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.27 2003/09/03 10:23:17 gbazin 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/input.h>
31 #include <vlc/decoder.h>
33 #include <stdlib.h> /* malloc(), free() */
34 #include <string.h> /* memcpy(), memset() */
36 #include <mpeg2dec/mpeg2.h>
38 #include "vout_synchro.h"
40 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
41 #define AR_SQUARE_PICTURE 1 /* square pixels */
42 #define AR_3_4_PICTURE 2 /* 3:4 picture (TV) */
43 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
44 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
46 /*****************************************************************************
47 * decoder_sys_t : libmpeg2 decoder descriptor
48 *****************************************************************************/
54 mpeg2dec_t *p_mpeg2dec;
55 const mpeg2_info_t *p_info;
61 pes_packet_t *p_pes; /* current PES we are decoding */
63 mtime_t i_previous_pts;
64 mtime_t i_current_pts;
65 mtime_t i_period_remainder;
67 picture_t * p_picture_to_destroy;
68 vlc_bool_t b_garbage_pic;
69 vlc_bool_t b_after_sequence_header; /* is it the next frame after
70 * the sequence header ? */
71 vlc_bool_t b_slice_i; /* intra-slice refresh stream */
76 vout_thread_t *p_vout;
77 vout_synchro_t *p_synchro;
81 /*****************************************************************************
83 *****************************************************************************/
84 static int OpenDecoder ( vlc_object_t * );
85 static int InitDecoder ( decoder_t * );
86 static int RunDecoder ( decoder_t *, block_t * );
87 static int EndDecoder ( decoder_t * );
89 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
91 /*****************************************************************************
93 *****************************************************************************/
95 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
96 set_capability( "decoder", 150 );
97 set_callbacks( OpenDecoder, NULL );
98 add_shortcut( "libmpeg2" );
101 /*****************************************************************************
102 * OpenDecoder: probe the decoder and return score
103 *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
106 decoder_t *p_dec = (decoder_t*)p_this;
108 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') &&
109 p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','1') &&
110 p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','2') )
115 p_dec->pf_init = InitDecoder;
116 p_dec->pf_decode = RunDecoder;
117 p_dec->pf_end = EndDecoder;
122 /*****************************************************************************
123 * InitDecoder: Initalize the decoder
124 *****************************************************************************/
125 static int InitDecoder( decoder_t *p_dec )
127 /* Allocate the memory needed to store the decoder's structure */
129 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
131 msg_Err( p_dec, "out of memory" );
135 /* Initialize the thread properties */
136 memset( p_dec->p_sys, 0, sizeof(decoder_sys_t) );
137 p_dec->p_sys->p_pes = NULL;
138 p_dec->p_sys->p_vout = NULL;
139 p_dec->p_sys->p_mpeg2dec = NULL;
140 p_dec->p_sys->p_synchro = NULL;
141 p_dec->p_sys->p_info = NULL;
142 p_dec->p_sys->i_pts = mdate() + DEFAULT_PTS_DELAY;
143 p_dec->p_sys->i_current_pts = 0;
144 p_dec->p_sys->i_previous_pts = 0;
145 p_dec->p_sys->i_period_remainder = 0;
146 p_dec->p_sys->p_picture_to_destroy = NULL;
147 p_dec->p_sys->b_garbage_pic = 0;
148 p_dec->p_sys->b_slice_i = 0;
149 p_dec->p_sys->b_skip = 0;
151 /* Initialize decoder */
152 p_dec->p_sys->p_mpeg2dec = mpeg2_init();
153 if( p_dec->p_sys->p_mpeg2dec == NULL)
155 msg_Err( p_dec, "mpeg2_init() failed" );
156 free( p_dec->p_sys );
160 p_dec->p_sys->p_info = mpeg2_info( p_dec->p_sys->p_mpeg2dec );
165 /*****************************************************************************
166 * RunDecoder: the libmpeg2 decoder
167 *****************************************************************************/
168 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
170 decoder_sys_t *p_sys = p_dec->p_sys;
175 vlc_bool_t b_need_more_data = VLC_FALSE;
179 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
181 block_Release( p_block );
185 state = mpeg2_parse( p_sys->p_mpeg2dec );
190 if( !p_block->i_buffer || b_need_more_data )
192 block_Release( p_block );
197 if( p_pes->b_discontinuity && p_sys->p_synchro
198 && p_sys->p_info->sequence->width != (unsigned)-1 )
200 vout_SynchroReset( p_sys->p_synchro );
201 if ( p_sys->p_info->current_fbuf != NULL
202 && p_sys->p_info->current_fbuf->id != NULL )
204 p_sys->b_garbage_pic = 1;
205 p_pic = p_sys->p_info->current_fbuf->id;
210 buf[0] = buf[1] = buf[2] = NULL;
211 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
213 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
215 p_sys->p_picture_to_destroy = p_pic;
217 memset( p_pic->p[0].p_pixels, 0,
218 p_sys->p_info->sequence->width
219 * p_sys->p_info->sequence->height );
220 memset( p_pic->p[1].p_pixels, 0x80,
221 p_sys->p_info->sequence->width
222 * p_sys->p_info->sequence->height / 4 );
223 memset( p_pic->p[2].p_pixels, 0x80,
224 p_sys->p_info->sequence->width
225 * p_sys->p_info->sequence->height / 4 );
227 if ( p_sys->b_slice_i )
229 vout_SynchroNewPicture( p_sys->p_synchro,
230 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
231 vout_SynchroDecode( p_sys->p_synchro );
232 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
239 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
240 p_sys->i_previous_pts = p_sys->i_current_pts;
241 p_sys->i_current_pts = p_block->i_pts;
244 p_sys->i_current_rate = DEFAULT_RATE;//p_pes->i_rate;
246 mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
247 p_block->p_buffer + p_block->i_buffer );
249 b_need_more_data = VLC_TRUE;
254 /* Initialize video output */
256 buf[0] = buf[1] = buf[2] = NULL;
258 /* Check whether the input gives a particular aspect ratio */
259 if( p_dec->p_fifo->p_demux_data
260 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
262 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
266 i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
268 case AR_16_9_PICTURE:
269 i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
271 case AR_221_1_PICTURE:
272 i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
274 case AR_SQUARE_PICTURE:
276 i_aspect = VOUT_ASPECT_FACTOR *
277 p_sys->p_info->sequence->width /
278 p_sys->p_info->sequence->height;
284 /* Use the value provided in the MPEG sequence header */
285 i_aspect = ((uint64_t)p_sys->p_info->sequence->display_width) *
286 p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
287 p_sys->p_info->sequence->display_height /
288 p_sys->p_info->sequence->pixel_height;
291 if ( p_dec->p_sys->p_vout != NULL )
294 /* Temporary hack to free the pictures in use by libmpeg2 */
295 for ( i_pic = 0; i_pic < p_dec->p_sys->p_vout->render.i_pictures; i_pic++ )
297 if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
299 vout_DestroyPicture( p_dec->p_sys->p_vout,
300 p_dec->p_sys->p_vout->render.pp_picture[i_pic] );
301 if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
302 vout_UnlinkPicture( p_dec->p_sys->p_vout,
303 p_dec->p_sys->p_vout->render.pp_picture[i_pic] );
307 p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
308 p_sys->p_info->sequence->width,
309 p_sys->p_info->sequence->height,
310 VLC_FOURCC('Y','V','1','2'),
313 if(p_sys->p_vout == NULL )
315 msg_Err( p_dec, "cannot create vout" );
316 block_Release( p_block );
320 msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
321 p_sys->p_info->sequence->width,
322 p_sys->p_info->sequence->height, i_aspect,
323 (uint32_t)((u64)1001000000 * 27 /
324 p_sys->p_info->sequence->frame_period / 1001),
325 (uint32_t)((u64)1001000000 * 27 /
326 p_sys->p_info->sequence->frame_period % 1001) );
328 mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
330 /* Set the first 2 reference frames */
331 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
333 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
334 memset( p_pic->p[0].p_pixels, 0,
335 p_sys->p_info->sequence->width
336 * p_sys->p_info->sequence->height );
337 memset( p_pic->p[1].p_pixels, 0x80,
338 p_sys->p_info->sequence->width
339 * p_sys->p_info->sequence->height / 4 );
340 memset( p_pic->p[2].p_pixels, 0x80,
341 p_sys->p_info->sequence->width
342 * p_sys->p_info->sequence->height / 4 );
343 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
344 /* This picture will never go through display_picture. */
345 vout_DatePicture( p_sys->p_vout, p_pic, 0 );
346 vout_DisplayPicture( p_sys->p_vout, p_pic );
347 /* For some reason, libmpeg2 will put this pic twice in
348 * discard_picture. This can be considered a bug in libmpeg2. */
349 vout_LinkPicture( p_sys->p_vout, p_pic );
351 if ( p_sys->p_synchro )
353 vout_SynchroRelease( p_sys->p_synchro );
355 p_sys->p_synchro = vout_SynchroInit( p_dec, p_sys->p_vout,
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,
367 p_sys->i_current_rate );
371 vout_SynchroTrash( p_sys->p_synchro );
375 vout_SynchroDecode( p_sys->p_synchro );
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)
387 == PIC_FLAG_CODING_TYPE_P) )
389 /* Intra-slice refresh. Simulate a blank I picture. */
390 msg_Dbg( p_dec, "intra-slice refresh stream" );
391 vout_SynchroNewPicture( p_sys->p_synchro,
392 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
393 vout_SynchroDecode( p_sys->p_synchro );
394 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
395 p_sys->b_slice_i = 1;
397 p_sys->b_after_sequence_header = 0;
399 vout_SynchroNewPicture( p_sys->p_synchro,
400 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
401 p_sys->p_info->current_picture->nb_fields,
402 (p_sys->p_info->current_picture->flags & PIC_FLAG_PTS) ?
403 ( ( p_sys->p_info->current_picture->pts ==
404 (uint32_t)p_sys->i_current_pts ) ?
405 p_sys->i_current_pts : p_sys->i_previous_pts ) : 0,
406 0, p_sys->i_current_rate );
408 if ( !(p_sys->b_slice_i
409 && ((p_sys->p_info->current_picture->flags
410 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
411 && !vout_SynchroChoose( p_sys->p_synchro,
412 p_sys->p_info->current_picture->flags
413 & PIC_MASK_CODING_TYPE ) )
415 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
417 vout_SynchroTrash( p_sys->p_synchro );
418 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
422 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
424 vout_SynchroDecode( p_sys->p_synchro );
425 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
426 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
433 if( p_sys->p_info->display_fbuf
434 && p_sys->p_info->display_fbuf->id )
436 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
438 vout_SynchroEnd( p_sys->p_synchro,
439 p_sys->p_info->display_picture->flags
440 & PIC_MASK_CODING_TYPE,
441 p_sys->b_garbage_pic );
442 p_sys->b_garbage_pic = 0;
443 vout_DisplayPicture( p_sys->p_vout, p_pic );
445 if ( p_sys->p_picture_to_destroy != p_pic )
447 vout_DatePicture( p_sys->p_vout, p_pic,
448 vout_SynchroDate( p_sys->p_synchro ) );
452 p_sys->p_picture_to_destroy = NULL;
453 vout_DatePicture( p_sys->p_vout, p_pic, 0 );
457 if( p_sys->p_info->discard_fbuf &&
458 p_sys->p_info->discard_fbuf->id )
460 p_pic = (picture_t *)p_sys->p_info->discard_fbuf->id;
461 vout_UnlinkPicture( p_sys->p_vout, p_pic );
468 buf[0] = buf[1] = buf[2] = NULL;
470 msg_Warn( p_dec, "invalid picture encountered" );
471 if ( ( p_sys->p_info->current_picture == NULL ) ||
472 ( ( p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
475 vout_SynchroReset( p_sys->p_synchro );
477 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
480 if( p_sys->p_info->current_fbuf &&
481 p_sys->p_info->current_fbuf->id )
483 p_sys->b_garbage_pic = 1;
484 p_pic = p_sys->p_info->current_fbuf->id;
488 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
490 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
492 p_sys->p_picture_to_destroy = p_pic;
494 memset( p_pic->p[0].p_pixels, 0,
495 p_sys->p_info->sequence->width
496 * p_sys->p_info->sequence->height );
497 memset( p_pic->p[1].p_pixels, 0x80,
498 p_sys->p_info->sequence->width
499 * p_sys->p_info->sequence->height / 4 );
500 memset( p_pic->p[2].p_pixels, 0x80,
501 p_sys->p_info->sequence->width
502 * p_sys->p_info->sequence->height / 4 );
504 if ( p_sys->b_slice_i )
506 vout_SynchroNewPicture( p_sys->p_synchro,
507 I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
508 vout_SynchroDecode( p_sys->p_synchro );
509 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
519 block_Release( p_block );
523 /*****************************************************************************
524 * EndDecoder: libmpeg2 decoder destruction
525 *****************************************************************************/
526 static int EndDecoder( decoder_t * p_dec )
528 decoder_sys_t *p_sys = p_dec->p_sys;
534 if( p_sys->p_synchro )
535 vout_SynchroRelease( p_sys->p_synchro );
539 /* Temporary hack to free the pictures in use by libmpeg2 */
540 for( i_pic = 0; i_pic < p_sys->p_vout->render.i_pictures; i_pic++ )
542 if( p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
544 vout_DestroyPicture( p_sys->p_vout,
545 p_sys->p_vout->render.pp_picture[i_pic] );
546 if( p_sys->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
547 vout_UnlinkPicture( p_sys->p_vout,
548 p_sys->p_vout->render.pp_picture[i_pic] );
551 vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
554 if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
562 /*****************************************************************************
563 * GetNewPicture: Get a new picture from the vout and set the buf struct
564 *****************************************************************************/
565 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
567 decoder_sys_t *p_sys = p_dec->p_sys;
570 vlc_bool_t b_progressive = p_sys->p_info->current_picture != NULL ?
571 p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
573 vlc_bool_t b_top_field_first = p_sys->p_info->current_picture != NULL ?
574 p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST :
576 unsigned int i_nb_fields = p_sys->p_info->current_picture != NULL ?
577 p_sys->p_info->current_picture->nb_fields : 2;
579 /* Get a new picture */
580 while( !(p_pic = vout_CreatePicture( p_sys->p_vout,
581 b_progressive, b_top_field_first, i_nb_fields )) )
583 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
586 msleep( VOUT_OUTMEM_SLEEP );
590 vout_LinkPicture( p_sys->p_vout, p_pic );
592 pp_buf[0] = p_pic->p[0].p_pixels;
593 pp_buf[1] = p_pic->p[1].p_pixels;
594 pp_buf[2] = p_pic->p[2].p_pixels;