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.25 2003/08/18 13:16:43 zorglub 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", 150 );
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;
128 /* Allocate the memory needed to store the thread's structure */
129 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
132 msg_Err( p_fifo, "out of memory" );
136 /* Initialize the thread properties */
137 memset( p_dec, 0, sizeof(dec_thread_t) );
138 p_dec->p_fifo = p_fifo;
140 p_dec->p_vout = NULL;
141 p_dec->p_mpeg2dec = NULL;
142 p_dec->p_synchro = NULL;
143 p_dec->p_info = NULL;
144 p_dec->i_pts = mdate() + DEFAULT_PTS_DELAY;
145 p_dec->i_current_pts = 0;
146 p_dec->i_previous_pts = 0;
147 p_dec->i_period_remainder = 0;
148 p_dec->p_picture_to_destroy = NULL;
149 p_dec->b_garbage_pic = 0;
150 p_dec->b_slice_i = 0;
153 /* Initialize decoder */
154 p_dec->p_mpeg2dec = mpeg2_init();
155 if( p_dec->p_mpeg2dec == NULL)
158 p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
160 /* libmpeg2 decoder thread's main loop */
161 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
163 state = mpeg2_parse( p_dec->p_mpeg2dec );
168 /* Feed libmpeg2 a data packet at a time */
171 /* Get the next PES */
173 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
176 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
179 p_dec->p_fifo->b_error = 1;
183 if( p_dec->p_pes->b_discontinuity && p_dec->p_synchro
184 && p_dec->p_info->sequence->width != (unsigned)-1 )
186 vout_SynchroReset( p_dec->p_synchro );
187 if ( p_dec->p_info->current_fbuf != NULL
188 && p_dec->p_info->current_fbuf->id != NULL )
190 p_dec->b_garbage_pic = 1;
191 p_pic = p_dec->p_info->current_fbuf->id;
196 buf[0] = buf[1] = buf[2] = NULL;
197 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
199 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
201 p_dec->p_picture_to_destroy = p_pic;
203 memset( p_pic->p[0].p_pixels, 0,
204 p_dec->p_info->sequence->width
205 * p_dec->p_info->sequence->height );
206 memset( p_pic->p[1].p_pixels, 0x80,
207 p_dec->p_info->sequence->width
208 * p_dec->p_info->sequence->height / 4 );
209 memset( p_pic->p[2].p_pixels, 0x80,
210 p_dec->p_info->sequence->width
211 * p_dec->p_info->sequence->height / 4 );
213 if ( p_dec->b_slice_i )
215 vout_SynchroNewPicture( p_dec->p_synchro,
216 I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
217 vout_SynchroDecode( p_dec->p_synchro );
218 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
222 if( p_dec->p_pes->i_pts )
224 mpeg2_pts( p_dec->p_mpeg2dec,
225 (uint32_t)p_dec->p_pes->i_pts );
226 p_dec->i_previous_pts = p_dec->i_current_pts;
227 p_dec->i_current_pts = p_dec->p_pes->i_pts;
230 p_dec->i_current_rate = p_dec->p_pes->i_rate;
231 p_data = p_dec->p_pes->p_first;
236 mpeg2_buffer( p_dec->p_mpeg2dec,
237 p_data->p_payload_start,
238 p_data->p_payload_end );
240 p_data = p_data->p_next;
246 /* Initialize video output */
248 buf[0] = buf[1] = buf[2] = NULL;
250 /* Check whether the input gives a particular aspect ratio */
251 if( p_dec->p_fifo->p_demux_data
252 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
254 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
258 i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
260 case AR_16_9_PICTURE:
261 i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
263 case AR_221_1_PICTURE:
264 i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
266 case AR_SQUARE_PICTURE:
268 i_aspect = VOUT_ASPECT_FACTOR *
269 p_dec->p_info->sequence->width /
270 p_dec->p_info->sequence->height;
276 /* Use the value provided in the MPEG sequence header */
277 i_aspect = ((uint64_t)p_dec->p_info->sequence->display_width) *
278 p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
279 p_dec->p_info->sequence->display_height /
280 p_dec->p_info->sequence->pixel_height;
283 if ( p_dec->p_vout != NULL )
285 /* Temporary hack to free the pictures in use by libmpeg2 */
286 for ( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
288 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
290 vout_DestroyPicture( p_dec->p_vout,
291 p_dec->p_vout->render.pp_picture[i_pic] );
292 if( p_dec->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
293 vout_UnlinkPicture( p_dec->p_vout,
294 p_dec->p_vout->render.pp_picture[i_pic] );
298 p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
299 p_dec->p_info->sequence->width,
300 p_dec->p_info->sequence->height,
301 VLC_FOURCC('Y','V','1','2'), i_aspect );
303 if(p_dec->p_vout == NULL )
305 msg_Err( p_dec->p_fifo , "cannot create vout" );
309 msg_Dbg( p_dec->p_fifo, "%dx%d, aspect %d, %u.%03u fps",
310 p_dec->p_info->sequence->width,
311 p_dec->p_info->sequence->height, i_aspect,
312 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period / 1001),
313 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period % 1001) );
315 mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
317 /* Set the first 2 reference frames */
318 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
320 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
321 memset( p_pic->p[0].p_pixels, 0,
322 p_dec->p_info->sequence->width
323 * p_dec->p_info->sequence->height );
324 memset( p_pic->p[1].p_pixels, 0x80,
325 p_dec->p_info->sequence->width
326 * p_dec->p_info->sequence->height / 4 );
327 memset( p_pic->p[2].p_pixels, 0x80,
328 p_dec->p_info->sequence->width
329 * p_dec->p_info->sequence->height / 4 );
330 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
331 /* This picture will never go through display_picture. */
332 vout_DatePicture( p_dec->p_vout, p_pic, 0 );
333 vout_DisplayPicture( p_dec->p_vout, p_pic );
334 /* For some reason, libmpeg2 will put this pic twice in
335 * discard_picture. This can be considered a bug in libmpeg2. */
336 vout_LinkPicture( p_dec->p_vout, p_pic );
338 if ( p_dec->p_synchro )
340 vout_SynchroRelease( p_dec->p_synchro );
342 p_dec->p_synchro = vout_SynchroInit( p_dec->p_fifo, p_dec->p_vout,
343 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period) );
344 p_dec->b_after_sequence_header = 1;
348 case STATE_PICTURE_2ND:
349 vout_SynchroNewPicture( p_dec->p_synchro,
350 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
351 p_dec->p_info->current_picture->nb_fields,
353 p_dec->i_current_rate );
357 vout_SynchroTrash( p_dec->p_synchro );
361 vout_SynchroDecode( p_dec->p_synchro );
368 buf[0] = buf[1] = buf[2] = NULL;
370 if ( p_dec->b_after_sequence_header
371 && ((p_dec->p_info->current_picture->flags
372 & PIC_MASK_CODING_TYPE)
373 == PIC_FLAG_CODING_TYPE_P) )
375 /* Intra-slice refresh. Simulate a blank I picture. */
376 msg_Dbg( p_dec->p_fifo, "intra-slice refresh stream" );
377 vout_SynchroNewPicture( p_dec->p_synchro,
378 I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
379 vout_SynchroDecode( p_dec->p_synchro );
380 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
381 p_dec->b_slice_i = 1;
383 p_dec->b_after_sequence_header = 0;
385 vout_SynchroNewPicture( p_dec->p_synchro,
386 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
387 p_dec->p_info->current_picture->nb_fields,
388 (p_dec->p_info->current_picture->flags & PIC_FLAG_PTS) ?
389 ( (p_dec->p_info->current_picture->pts ==
390 (uint32_t)p_dec->i_current_pts) ?
391 p_dec->i_current_pts : p_dec->i_previous_pts ) : 0,
393 p_dec->i_current_rate );
395 if ( !(p_dec->b_slice_i
396 && ((p_dec->p_info->current_picture->flags
397 & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
398 && !vout_SynchroChoose( p_dec->p_synchro,
399 p_dec->p_info->current_picture->flags
400 & PIC_MASK_CODING_TYPE ) )
402 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
404 vout_SynchroTrash( p_dec->p_synchro );
405 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
409 mpeg2_skip( p_dec->p_mpeg2dec, 0 );
411 vout_SynchroDecode( p_dec->p_synchro );
412 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
413 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
420 if( p_dec->p_info->display_fbuf
421 && p_dec->p_info->display_fbuf->id )
423 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
425 vout_SynchroEnd( p_dec->p_synchro,
426 p_dec->p_info->display_picture->flags
427 & PIC_MASK_CODING_TYPE,
428 p_dec->b_garbage_pic );
429 p_dec->b_garbage_pic = 0;
430 vout_DisplayPicture( p_dec->p_vout, p_pic );
432 if ( p_dec->p_picture_to_destroy != p_pic )
434 vout_DatePicture( p_dec->p_vout, p_pic,
435 vout_SynchroDate( p_dec->p_synchro ) );
439 p_dec->p_picture_to_destroy = NULL;
440 vout_DatePicture( p_dec->p_vout, p_pic, 0 );
444 if( p_dec->p_info->discard_fbuf &&
445 p_dec->p_info->discard_fbuf->id )
447 p_pic = (picture_t *)p_dec->p_info->discard_fbuf->id;
448 vout_UnlinkPicture( p_dec->p_vout, p_pic );
455 buf[0] = buf[1] = buf[2] = NULL;
457 msg_Warn( p_dec->p_fifo, "invalid picture encountered" );
458 if ( ( p_dec->p_info->current_picture == NULL ) ||
459 ( ( p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
462 vout_SynchroReset( p_dec->p_synchro );
464 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
467 if( p_dec->p_info->current_fbuf &&
468 p_dec->p_info->current_fbuf->id )
470 p_dec->b_garbage_pic = 1;
471 p_pic = p_dec->p_info->current_fbuf->id;
475 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
477 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
479 p_dec->p_picture_to_destroy = p_pic;
481 memset( p_pic->p[0].p_pixels, 0,
482 p_dec->p_info->sequence->width
483 * p_dec->p_info->sequence->height );
484 memset( p_pic->p[1].p_pixels, 0x80,
485 p_dec->p_info->sequence->width
486 * p_dec->p_info->sequence->height / 4 );
487 memset( p_pic->p[2].p_pixels, 0x80,
488 p_dec->p_info->sequence->width
489 * p_dec->p_info->sequence->height / 4 );
491 if ( p_dec->b_slice_i )
493 vout_SynchroNewPicture( p_dec->p_synchro,
494 I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
495 vout_SynchroDecode( p_dec->p_synchro );
496 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
506 /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
507 if( p_dec->p_fifo->b_error )
509 DecoderError( p_dec->p_fifo );
512 /* End of the libmpeg2 decoder thread */
513 CloseDecoder( p_dec );
518 DecoderError( p_fifo );
522 p_dec->p_fifo->b_error = 1;
524 /* End of the libmpeg2 decoder thread */
525 CloseDecoder( p_dec );
531 /*****************************************************************************
532 * CloseDecoder: libmpeg2 decoder destruction
533 *****************************************************************************/
534 static void CloseDecoder( dec_thread_t * p_dec )
541 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
543 if( p_dec->p_synchro )
544 vout_SynchroRelease( p_dec->p_synchro );
548 /* Temporary hack to free the pictures in use by libmpeg2 */
549 for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
551 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
553 vout_DestroyPicture( p_dec->p_vout,
554 p_dec->p_vout->render.pp_picture[i_pic] );
555 if( p_dec->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
556 vout_UnlinkPicture( p_dec->p_vout,
557 p_dec->p_vout->render.pp_picture[i_pic] );
560 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
563 if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
569 /*****************************************************************************
570 * GetNewPicture: Get a new picture from the vout and set the buf struct
571 *****************************************************************************/
572 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
575 vlc_bool_t b_progressive = p_dec->p_info->current_picture != NULL ?
576 p_dec->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
578 vlc_bool_t b_top_field_first = p_dec->p_info->current_picture != NULL ?
579 p_dec->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST :
581 unsigned int i_nb_fields = p_dec->p_info->current_picture != NULL ?
582 p_dec->p_info->current_picture->nb_fields : 2;
584 /* Get a new picture */
585 while( !(p_pic = vout_CreatePicture( p_dec->p_vout,
586 b_progressive, b_top_field_first, i_nb_fields )) )
588 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
591 msleep( VOUT_OUTMEM_SLEEP );
595 vout_LinkPicture( p_dec->p_vout, p_pic );
597 pp_buf[0] = p_pic->p[0].p_pixels;
598 pp_buf[1] = p_pic->p[1].p_pixels;
599 pp_buf[2] = p_pic->p[2].p_pixels;