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.9 2003/04/14 22:22:32 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;
59 decoder_fifo_t *p_fifo; /* stores the PES stream data */
60 pes_packet_t *p_pes; /* current PES we are decoding */
62 mtime_t i_previous_pts;
63 mtime_t i_current_pts;
64 mtime_t i_period_remainder;
66 picture_t * p_picture_to_destroy;
71 vout_thread_t *p_vout;
72 vout_synchro_t *p_synchro;
76 /*****************************************************************************
78 *****************************************************************************/
79 static int OpenDecoder ( vlc_object_t * );
80 static int RunDecoder ( decoder_fifo_t * );
81 static void CloseDecoder ( dec_thread_t * );
83 static picture_t *GetNewPicture( dec_thread_t *, uint8_t ** );
85 /*****************************************************************************
87 *****************************************************************************/
89 set_description( _("MPEG I/II video decoder (using libmpeg2)") );
90 set_capability( "decoder", 40 );
91 set_callbacks( OpenDecoder, NULL );
92 add_shortcut( "libmpeg2" );
95 /*****************************************************************************
96 * OpenDecoder: probe the decoder and return score
97 *****************************************************************************/
98 static int OpenDecoder( vlc_object_t *p_this )
100 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
102 if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
107 p_fifo->pf_run = RunDecoder;
111 /*****************************************************************************
112 * RunDecoder: the libmpeg2 decoder
113 *****************************************************************************/
114 static int RunDecoder( decoder_fifo_t *p_fifo )
117 data_packet_t *p_data = NULL;
120 int i_aspect, i_chroma;
122 /* Allocate the memory needed to store the thread's structure */
123 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
126 msg_Err( p_fifo, "out of memory" );
130 /* Initialize the thread properties */
131 memset( p_dec, 0, sizeof(dec_thread_t) );
132 p_dec->p_fifo = p_fifo;
134 p_dec->p_vout = NULL;
135 p_dec->p_mpeg2dec = NULL;
136 p_dec->p_info = NULL;
137 p_dec->i_pts = mdate() + DEFAULT_PTS_DELAY;
138 p_dec->i_current_pts = 0;
139 p_dec->i_previous_pts = 0;
140 p_dec->i_period_remainder = 0;
141 p_dec->p_picture_to_destroy = NULL;
143 /* Initialize decoder */
144 p_dec->p_mpeg2dec = mpeg2_init();
145 if( p_dec->p_mpeg2dec == NULL)
148 p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
150 /* libmpeg2 decoder thread's main loop */
151 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
153 state = mpeg2_parse( p_dec->p_mpeg2dec );
158 /* Feed libmpeg2 a data packet at a time */
161 /* Get the next PES */
163 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
166 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
169 p_dec->p_fifo->b_error = 1;
173 if( p_dec->p_pes->b_discontinuity )
175 vout_SynchroReset( p_dec->p_synchro );
176 if ( p_dec->p_info->current_fbuf != NULL )
177 p_dec->p_picture_to_destroy
178 = p_dec->p_info->current_fbuf->id;
181 if( p_dec->p_pes->i_pts )
183 mpeg2_pts( p_dec->p_mpeg2dec,
184 (uint32_t)p_dec->p_pes->i_pts );
185 p_dec->i_previous_pts = p_dec->i_current_pts;
186 p_dec->i_current_pts = p_dec->p_pes->i_pts;
189 p_dec->i_current_rate = p_dec->p_pes->i_rate;
190 p_data = p_dec->p_pes->p_first;
195 mpeg2_buffer( p_dec->p_mpeg2dec,
196 p_data->p_payload_start,
197 p_data->p_payload_end );
199 p_data = p_data->p_next;
205 /* Initialize video output */
207 buf[0] = buf[1] = buf[2] = NULL;
209 /* Check whether the input gives a particular aspect ratio */
210 if( p_dec->p_fifo->p_demux_data
211 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
213 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
217 i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
219 case AR_16_9_PICTURE:
220 i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
222 case AR_221_1_PICTURE:
223 i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
225 case AR_SQUARE_PICTURE:
227 i_aspect = VOUT_ASPECT_FACTOR *
228 p_dec->p_info->sequence->width /
229 p_dec->p_info->sequence->height;
235 /* Use the value provided in the MPEG sequence header */
236 i_aspect = ((uint64_t)p_dec->p_info->sequence->width) *
237 p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
238 p_dec->p_info->sequence->height /
239 p_dec->p_info->sequence->pixel_height;
242 i_chroma = VLC_FOURCC('Y','V','1','2');
244 p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
245 p_dec->p_info->sequence->width,
246 p_dec->p_info->sequence->height,
247 i_chroma, i_aspect );
249 mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
251 /* Set the first 2 reference frames */
252 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
253 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
255 p_dec->p_synchro = vout_SynchroInit( p_dec->p_fifo, p_dec->p_vout,
256 1000000 * 27 / p_dec->p_info->sequence->frame_period * 1001 );
263 buf[0] = buf[1] = buf[2] = NULL;
265 vout_SynchroNewPicture( p_dec->p_synchro,
266 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
267 p_dec->p_info->current_picture->nb_fields,
268 (p_dec->p_info->current_picture->flags & PIC_FLAG_PTS) ?
269 ( (p_dec->p_info->current_picture->pts ==
270 (uint32_t)p_dec->i_current_pts) ?
271 p_dec->i_current_pts : p_dec->i_previous_pts ) : 0,
273 p_dec->i_current_rate );
275 if ( !vout_SynchroChoose( p_dec->p_synchro,
276 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE ) )
278 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
279 vout_SynchroTrash( p_dec->p_synchro );
280 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
284 mpeg2_skip( p_dec->p_mpeg2dec, 0 );
285 vout_SynchroDecode( p_dec->p_synchro );
286 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
287 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
293 if( p_dec->p_info->display_fbuf
294 && p_dec->p_info->display_fbuf->id )
296 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
300 if ( p_dec->p_picture_to_destroy != p_pic )
302 vout_SynchroEnd( p_dec->p_synchro,
303 p_dec->p_info->display_picture->flags
304 & PIC_MASK_CODING_TYPE,
306 vout_DatePicture( p_dec->p_vout, p_pic,
307 vout_SynchroDate( p_dec->p_synchro ) );
308 vout_DisplayPicture( p_dec->p_vout, p_pic );
312 p_dec->p_picture_to_destroy = NULL;
313 vout_SynchroEnd( p_dec->p_synchro,
314 p_dec->p_info->display_picture->flags
315 & PIC_MASK_CODING_TYPE,
317 vout_DestroyPicture( p_dec->p_vout, p_pic );
320 if( p_dec->p_info->discard_fbuf &&
321 p_dec->p_info->discard_fbuf->id )
323 vout_UnlinkPicture( p_dec->p_vout, p_pic );
329 msg_Warn( p_dec->p_fifo, "Received STATE_INVALID" );
337 /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
338 if( p_dec->p_fifo->b_error )
340 DecoderError( p_dec->p_fifo );
343 /* End of the libmpeg2 decoder thread */
344 CloseDecoder( p_dec );
349 DecoderError( p_fifo );
353 p_dec->p_fifo->b_error = 1;
355 /* End of the libmpeg2 decoder thread */
356 CloseDecoder( p_dec );
362 /*****************************************************************************
363 * CloseDecoder: libmpeg2 decoder destruction
364 *****************************************************************************/
365 static void CloseDecoder( dec_thread_t * p_dec )
372 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
376 /* Temporary hack to free the pictures in use by libmpeg2 */
377 for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
379 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
381 vout_DestroyPicture( p_dec->p_vout,
382 p_dec->p_vout->render.pp_picture[i_pic] );
385 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
388 if( p_dec->p_synchro )
389 vout_SynchroRelease( p_dec->p_synchro );
391 if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
397 /*****************************************************************************
398 * GetNewPicture: Get a new picture from the vout and set the buf struct
399 *****************************************************************************/
400 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
404 /* Get a new picture */
405 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
407 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
410 msleep( VOUT_OUTMEM_SLEEP );
414 vout_LinkPicture( p_dec->p_vout, p_pic );
416 pp_buf[0] = p_pic->p[0].p_pixels;
417 pp_buf[1] = p_pic->p[1].p_pixels;
418 pp_buf[2] = p_pic->p[2].p_pixels;