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.22 2003/06/10 23:01:40 massiot Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
32 #include <stdlib.h> /* malloc(), free() */
33 #include <string.h> /* memcpy(), memset() */
35 #include <mpeg2dec/mpeg2.h>
37 #include "vout_synchro.h"
39 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
40 #define AR_SQUARE_PICTURE 1 /* square pixels */
41 #define AR_3_4_PICTURE 2 /* 3:4 picture (TV) */
42 #define AR_16_9_PICTURE 3 /* 16:9 picture (wide screen) */
43 #define AR_221_1_PICTURE 4 /* 2.21:1 picture (movie) */
45 /*****************************************************************************
46 * dec_thread_t : libmpeg2 decoder thread descriptor
47 *****************************************************************************/
48 typedef struct dec_thread_t
53 mpeg2dec_t *p_mpeg2dec;
54 const mpeg2_info_t *p_info;
60 decoder_fifo_t *p_fifo; /* stores the PES stream data */
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 RunDecoder ( decoder_fifo_t * );
86 static void CloseDecoder ( dec_thread_t * );
88 static picture_t *GetNewPicture( dec_thread_t *, uint8_t ** );
90 /*****************************************************************************
92 *****************************************************************************/
94 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
95 set_capability( "decoder", 40 );
96 set_callbacks( OpenDecoder, NULL );
97 add_shortcut( "libmpeg2" );
100 /*****************************************************************************
101 * OpenDecoder: probe the decoder and return score
102 *****************************************************************************/
103 static int OpenDecoder( vlc_object_t *p_this )
105 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
107 if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
112 p_fifo->pf_run = RunDecoder;
116 /*****************************************************************************
117 * RunDecoder: the libmpeg2 decoder
118 *****************************************************************************/
119 static int RunDecoder( decoder_fifo_t *p_fifo )
122 data_packet_t *p_data = NULL;
125 int i_aspect, i_chroma;
127 /* Allocate the memory needed to store the thread's structure */
128 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
131 msg_Err( p_fifo, "out of memory" );
135 /* Initialize the thread properties */
136 memset( p_dec, 0, sizeof(dec_thread_t) );
137 p_dec->p_fifo = p_fifo;
139 p_dec->p_vout = NULL;
140 p_dec->p_mpeg2dec = NULL;
141 p_dec->p_synchro = NULL;
142 p_dec->p_info = NULL;
143 p_dec->i_pts = mdate() + DEFAULT_PTS_DELAY;
144 p_dec->i_current_pts = 0;
145 p_dec->i_previous_pts = 0;
146 p_dec->i_period_remainder = 0;
147 p_dec->p_picture_to_destroy = NULL;
148 p_dec->b_garbage_pic = 0;
149 p_dec->b_slice_i = 0;
152 /* Initialize decoder */
153 p_dec->p_mpeg2dec = mpeg2_init();
154 if( p_dec->p_mpeg2dec == NULL)
157 p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
159 /* libmpeg2 decoder thread's main loop */
160 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
162 state = mpeg2_parse( p_dec->p_mpeg2dec );
167 /* Feed libmpeg2 a data packet at a time */
170 /* Get the next PES */
172 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
175 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
178 p_dec->p_fifo->b_error = 1;
182 if( p_dec->p_pes->b_discontinuity && p_dec->p_synchro
183 && p_dec->p_info->sequence->width != (unsigned)-1 )
185 vout_SynchroReset( p_dec->p_synchro );
186 if ( p_dec->p_info->current_fbuf != NULL
187 && p_dec->p_info->current_fbuf->id != NULL )
189 p_dec->b_garbage_pic = 1;
190 p_pic = p_dec->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_dec->p_mpeg2dec, buf, p_pic );
200 p_dec->p_picture_to_destroy = p_pic;
202 memset( p_pic->p[0].p_pixels, 0,
203 p_dec->p_info->sequence->width
204 * p_dec->p_info->sequence->height );
205 memset( p_pic->p[1].p_pixels, 0x80,
206 p_dec->p_info->sequence->width
207 * p_dec->p_info->sequence->height / 4 );
208 memset( p_pic->p[2].p_pixels, 0x80,
209 p_dec->p_info->sequence->width
210 * p_dec->p_info->sequence->height / 4 );
212 if ( p_dec->b_slice_i )
214 vout_SynchroNewPicture( p_dec->p_synchro,
215 I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
216 vout_SynchroDecode( p_dec->p_synchro );
217 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
221 if( p_dec->p_pes->i_pts )
223 mpeg2_pts( p_dec->p_mpeg2dec,
224 (uint32_t)p_dec->p_pes->i_pts );
225 p_dec->i_previous_pts = p_dec->i_current_pts;
226 p_dec->i_current_pts = p_dec->p_pes->i_pts;
229 p_dec->i_current_rate = p_dec->p_pes->i_rate;
230 p_data = p_dec->p_pes->p_first;
235 mpeg2_buffer( p_dec->p_mpeg2dec,
236 p_data->p_payload_start,
237 p_data->p_payload_end );
239 p_data = p_data->p_next;
245 /* Initialize video output */
247 buf[0] = buf[1] = buf[2] = NULL;
249 /* Check whether the input gives a particular aspect ratio */
250 if( p_dec->p_fifo->p_demux_data
251 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
253 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
257 i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
259 case AR_16_9_PICTURE:
260 i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
262 case AR_221_1_PICTURE:
263 i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
265 case AR_SQUARE_PICTURE:
267 i_aspect = VOUT_ASPECT_FACTOR *
268 p_dec->p_info->sequence->width /
269 p_dec->p_info->sequence->height;
275 /* Use the value provided in the MPEG sequence header */
276 i_aspect = ((uint64_t)p_dec->p_info->sequence->display_width) *
277 p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
278 p_dec->p_info->sequence->display_height /
279 p_dec->p_info->sequence->pixel_height;
282 i_chroma = VLC_FOURCC('Y','V','1','2');
284 p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
285 p_dec->p_info->sequence->width,
286 p_dec->p_info->sequence->height,
287 i_chroma, i_aspect );
289 msg_Dbg( p_dec->p_fifo, "%dx%d, aspect %d, %u.%03u fps",
290 p_dec->p_info->sequence->width,
291 p_dec->p_info->sequence->height, i_aspect,
292 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period / 1001),
293 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period % 1001) );
295 mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
297 /* Set the first 2 reference frames */
298 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
300 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
301 memset( p_pic->p[0].p_pixels, 0,
302 p_dec->p_info->sequence->width
303 * p_dec->p_info->sequence->height );
304 memset( p_pic->p[1].p_pixels, 0x80,
305 p_dec->p_info->sequence->width
306 * p_dec->p_info->sequence->height / 4 );
307 memset( p_pic->p[2].p_pixels, 0x80,
308 p_dec->p_info->sequence->width
309 * p_dec->p_info->sequence->height / 4 );
310 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
311 /* This picture will never go through display_picture. */
312 vout_DatePicture( p_dec->p_vout, p_pic, 0 );
313 vout_DisplayPicture( p_dec->p_vout, p_pic );
314 /* For some reason, libmpeg2 will put this pic twice in
315 * discard_picture. This can be considered a bug in libmpeg2. */
316 vout_LinkPicture( p_dec->p_vout, p_pic );
318 if ( p_dec->p_synchro )
320 vout_SynchroRelease( p_dec->p_synchro );
322 p_dec->p_synchro = vout_SynchroInit( p_dec->p_fifo, p_dec->p_vout,
323 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period) );
324 p_dec->b_after_sequence_header = 1;
328 case STATE_PICTURE_2ND:
329 vout_SynchroNewPicture( p_dec->p_synchro,
330 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
331 p_dec->p_info->current_picture->nb_fields,
333 p_dec->i_current_rate );
337 vout_SynchroTrash( p_dec->p_synchro );
341 vout_SynchroDecode( p_dec->p_synchro );
348 buf[0] = buf[1] = buf[2] = NULL;
350 if ( p_dec->b_after_sequence_header
351 && ((p_dec->p_info->current_picture->flags
352 & PIC_MASK_CODING_TYPE)
353 == PIC_FLAG_CODING_TYPE_P) )
355 /* Intra-slice refresh. Simulate a blank I picture. */
356 msg_Dbg( p_dec->p_fifo, "intra-slice refresh stream" );
357 vout_SynchroNewPicture( p_dec->p_synchro,
358 I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
359 vout_SynchroDecode( p_dec->p_synchro );
360 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
361 p_dec->b_slice_i = 1;
363 p_dec->b_after_sequence_header = 0;
365 vout_SynchroNewPicture( p_dec->p_synchro,
366 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
367 p_dec->p_info->current_picture->nb_fields,
368 (p_dec->p_info->current_picture->flags & PIC_FLAG_PTS) ?
369 ( (p_dec->p_info->current_picture->pts ==
370 (uint32_t)p_dec->i_current_pts) ?
371 p_dec->i_current_pts : p_dec->i_previous_pts ) : 0,
373 p_dec->i_current_rate );
375 if ( !(p_dec->b_slice_i
376 && ((p_dec->p_info->current_picture->flags
377 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
378 && !vout_SynchroChoose( p_dec->p_synchro,
379 p_dec->p_info->current_picture->flags
380 & PIC_MASK_CODING_TYPE ) )
382 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
384 vout_SynchroTrash( p_dec->p_synchro );
385 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
389 mpeg2_skip( p_dec->p_mpeg2dec, 0 );
391 vout_SynchroDecode( p_dec->p_synchro );
392 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
393 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
400 if( p_dec->p_info->display_fbuf
401 && p_dec->p_info->display_fbuf->id )
403 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
405 vout_SynchroEnd( p_dec->p_synchro,
406 p_dec->p_info->display_picture->flags
407 & PIC_MASK_CODING_TYPE,
408 p_dec->b_garbage_pic );
409 p_dec->b_garbage_pic = 0;
410 vout_DisplayPicture( p_dec->p_vout, p_pic );
412 if ( p_dec->p_picture_to_destroy != p_pic )
414 vout_DatePicture( p_dec->p_vout, p_pic,
415 vout_SynchroDate( p_dec->p_synchro ) );
419 p_dec->p_picture_to_destroy = NULL;
420 vout_DatePicture( p_dec->p_vout, p_pic, 0 );
424 if( p_dec->p_info->discard_fbuf &&
425 p_dec->p_info->discard_fbuf->id )
427 p_pic = (picture_t *)p_dec->p_info->discard_fbuf->id;
428 vout_UnlinkPicture( p_dec->p_vout, p_pic );
435 buf[0] = buf[1] = buf[2] = NULL;
437 msg_Warn( p_dec->p_fifo, "invalid picture encountered" );
438 if ( ( p_dec->p_info->current_picture == NULL ) ||
439 ( ( p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
442 vout_SynchroReset( p_dec->p_synchro );
444 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
447 if( p_dec->p_info->current_fbuf &&
448 p_dec->p_info->current_fbuf->id )
450 p_dec->b_garbage_pic = 1;
451 p_pic = p_dec->p_info->current_fbuf->id;
455 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
457 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
459 p_dec->p_picture_to_destroy = p_pic;
461 memset( p_pic->p[0].p_pixels, 0,
462 p_dec->p_info->sequence->width
463 * p_dec->p_info->sequence->height );
464 memset( p_pic->p[1].p_pixels, 0x80,
465 p_dec->p_info->sequence->width
466 * p_dec->p_info->sequence->height / 4 );
467 memset( p_pic->p[2].p_pixels, 0x80,
468 p_dec->p_info->sequence->width
469 * p_dec->p_info->sequence->height / 4 );
471 if ( p_dec->b_slice_i )
473 vout_SynchroNewPicture( p_dec->p_synchro,
474 I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
475 vout_SynchroDecode( p_dec->p_synchro );
476 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
486 /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
487 if( p_dec->p_fifo->b_error )
489 DecoderError( p_dec->p_fifo );
492 /* End of the libmpeg2 decoder thread */
493 CloseDecoder( p_dec );
498 DecoderError( p_fifo );
502 p_dec->p_fifo->b_error = 1;
504 /* End of the libmpeg2 decoder thread */
505 CloseDecoder( p_dec );
511 /*****************************************************************************
512 * CloseDecoder: libmpeg2 decoder destruction
513 *****************************************************************************/
514 static void CloseDecoder( dec_thread_t * p_dec )
521 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
523 if( p_dec->p_synchro )
524 vout_SynchroRelease( p_dec->p_synchro );
528 /* Temporary hack to free the pictures in use by libmpeg2 */
529 for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
531 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
533 vout_DestroyPicture( p_dec->p_vout,
534 p_dec->p_vout->render.pp_picture[i_pic] );
535 if( p_dec->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
536 vout_UnlinkPicture( p_dec->p_vout,
537 p_dec->p_vout->render.pp_picture[i_pic] );
540 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
543 if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
549 /*****************************************************************************
550 * GetNewPicture: Get a new picture from the vout and set the buf struct
551 *****************************************************************************/
552 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
555 vlc_bool_t b_progressive = p_dec->p_info->current_picture != NULL ?
556 p_dec->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
558 vlc_bool_t b_top_field_first = p_dec->p_info->current_picture != NULL ?
559 p_dec->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST :
561 unsigned int i_nb_fields = p_dec->p_info->current_picture != NULL ?
562 p_dec->p_info->current_picture->nb_fields : 2;
564 /* Get a new picture */
565 while( !(p_pic = vout_CreatePicture( p_dec->p_vout,
566 b_progressive, b_top_field_first, i_nb_fields )) )
568 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
571 msleep( VOUT_OUTMEM_SLEEP );
575 vout_LinkPicture( p_dec->p_vout, p_pic );
577 pp_buf[0] = p_pic->p[0].p_pixels;
578 pp_buf[1] = p_pic->p[1].p_pixels;
579 pp_buf[2] = p_pic->p[2].p_pixels;