1 /*****************************************************************************
\r
2 * libmpeg2.c: mpeg2 video decoder module making use of libmpeg2.
\r
3 *****************************************************************************
\r
4 * Copyright (C) 1999-2001 VideoLAN
\r
5 * $Id: libmpeg2.c,v 1.2 2003/02/25 21:09:34 gbazin Exp $
\r
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
\r
9 * This program is free software; you can redistribute it and/or modify
\r
10 * it under the terms of the GNU General Public License as published by
\r
11 * the Free Software Foundation; either version 2 of the License, or
\r
12 * (at your option) any later version.
\r
14 * This program is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU General Public License for more details.
\r
19 * You should have received a copy of the GNU General Public License
\r
20 * along with this program; if not, write to the Free Software
\r
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
\r
22 *****************************************************************************/
\r
24 /*****************************************************************************
\r
26 *****************************************************************************/
\r
27 #include <vlc/vlc.h>
\r
28 #include <vlc/vout.h>
\r
29 #include <vlc/input.h>
\r
30 #include <vlc/decoder.h>
\r
32 #include <stdlib.h> /* malloc(), free() */
\r
33 #include <string.h> /* memcpy(), memset() */
\r
35 #include <mpeg2dec/mpeg2.h>
\r
37 /*****************************************************************************
\r
38 * dec_thread_t : libmpeg2 decoder thread descriptor
\r
39 *****************************************************************************/
\r
40 typedef struct dec_thread_t
\r
43 * libmpeg2 properties
\r
45 mpeg2dec_t *p_mpeg2dec;
\r
46 const mpeg2_info_t *p_info;
\r
51 decoder_fifo_t *p_fifo; /* stores the PES stream data */
\r
52 pes_packet_t *p_pes; /* current PES we are decoding */
\r
54 mtime_t i_previous_pts;
\r
55 mtime_t i_current_pts;
\r
60 vout_thread_t *p_vout;
\r
64 /*****************************************************************************
\r
66 *****************************************************************************/
\r
67 static int OpenDecoder ( vlc_object_t * );
\r
68 static int RunDecoder ( decoder_fifo_t * );
\r
69 static void CloseDecoder ( dec_thread_t * );
\r
71 /*****************************************************************************
\r
73 *****************************************************************************/
\r
75 set_description( _("libmpeg2 decoder module") );
\r
76 set_capability( "decoder", 40 );
\r
77 set_callbacks( OpenDecoder, NULL );
\r
78 add_shortcut( "libmpeg2" );
\r
81 /*****************************************************************************
\r
82 * OpenDecoder: probe the decoder and return score
\r
83 *****************************************************************************/
\r
84 static int OpenDecoder( vlc_object_t *p_this )
\r
86 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
\r
88 if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
\r
90 return VLC_EGENERIC;
\r
93 p_fifo->pf_run = RunDecoder;
\r
96 /*****************************************************************************
\r
97 * RunDecoder: the libmpeg2 decoder
\r
98 *****************************************************************************/
\r
99 static int RunDecoder( decoder_fifo_t *p_fifo )
\r
101 dec_thread_t *p_dec;
\r
102 data_packet_t *p_data = NULL;
\r
103 mpeg2_state_t state;
\r
105 int i_aspect, i_chroma;
\r
107 /* Allocate the memory needed to store the thread's structure */
\r
108 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
\r
111 msg_Err( p_fifo, "out of memory" );
\r
115 /* Initialize the thread properties */
\r
116 memset( p_dec, 0, sizeof(dec_thread_t) );
\r
117 p_dec->p_fifo = p_fifo;
\r
118 p_dec->p_pes = NULL;
\r
119 p_dec->p_vout = NULL;
\r
120 p_dec->p_mpeg2dec = NULL;
\r
121 p_dec->p_info = NULL;
\r
123 p_dec->i_current_pts = 0;
\r
124 p_dec->i_previous_pts = 0;
\r
126 /* Initialize decoder */
\r
127 p_dec->p_mpeg2dec = mpeg2_init();
\r
128 if( p_dec->p_mpeg2dec == NULL)
\r
131 p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
\r
133 /* libmpeg2 decoder thread's main loop */
\r
134 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
\r
136 state = mpeg2_parse( p_dec->p_mpeg2dec );
\r
141 /* Feed libmpeg2 a data packet at a time */
\r
142 if( p_data == NULL )
\r
144 /* Get the next PES */
\r
146 input_DeletePES( p_dec->p_fifo->p_packets_mgt,
\r
149 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
\r
150 if( !p_dec->p_pes )
\r
152 p_dec->p_fifo->b_error = 1;
\r
156 if( p_dec->p_pes->i_pts )
\r
158 mpeg2_pts( p_dec->p_mpeg2dec,
\r
159 (uint32_t)p_dec->p_pes->i_pts );
\r
160 p_dec->i_previous_pts = p_dec->i_current_pts;
\r
161 p_dec->i_current_pts = p_dec->p_pes->i_pts;
\r
163 p_data = p_dec->p_pes->p_first;
\r
166 if( p_data != NULL )
\r
168 mpeg2_buffer( p_dec->p_mpeg2dec,
\r
169 p_data->p_payload_start,
\r
170 p_data->p_payload_end );
\r
172 p_data = p_data->p_next;
\r
176 case STATE_SEQUENCE:
\r
177 /* Initialize video output */
\r
178 i_aspect = p_dec->p_info->sequence->width *
\r
179 p_dec->p_info->sequence->pixel_width /
\r
180 p_dec->p_info->sequence->height * VOUT_ASPECT_FACTOR /
\r
181 p_dec->p_info->sequence->pixel_height;
\r
183 i_chroma = VLC_FOURCC('Y','V','1','2');
\r
185 p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
\r
186 p_dec->p_info->sequence->width,
\r
187 p_dec->p_info->sequence->height,
\r
188 i_chroma, i_aspect );
\r
191 case STATE_PICTURE:
\r
195 /* Get a new picture */
\r
196 while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
\r
198 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
\r
201 msleep( VOUT_OUTMEM_SLEEP );
\r
203 if( p_pic == NULL )
\r
206 buf[0] = p_pic->p[0].p_pixels;
\r
207 buf[1] = p_pic->p[1].p_pixels;
\r
208 buf[2] = p_pic->p[2].p_pixels;
\r
209 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
\r
215 msg_Err( p_dec->p_fifo, "STATE_END" );
\r
219 msg_Err( p_dec->p_fifo, "STATE_SLICE: %i",
\r
220 p_dec->p_info->display_fbuf ?
\r
221 p_dec->p_info->display_fbuf->id : -1 );
\r
223 if( p_dec->p_info->display_fbuf
\r
224 && p_dec->p_info->display_fbuf->id )
\r
226 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
\r
228 if( p_dec->p_info->display_picture->flags & PIC_FLAG_PTS )
\r
230 p_dec->i_pts = ( p_dec->p_info->display_picture->pts ==
\r
231 (uint32_t)p_dec->i_current_pts ) ?
\r
232 p_dec->i_current_pts : p_dec->i_previous_pts;
\r
236 p_dec->i_pts += (p_dec->p_info->sequence->frame_period/27);
\r
239 vout_DatePicture( p_dec->p_vout, p_pic, p_dec->i_pts );
\r
241 vout_DisplayPicture( p_dec->p_vout, p_pic );
\r
250 /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
\r
251 if( p_dec->p_fifo->b_error )
\r
253 DecoderError( p_dec->p_fifo );
\r
256 /* End of the libmpeg2 decoder thread */
\r
257 CloseDecoder( p_dec );
\r
262 DecoderError( p_fifo );
\r
265 if( p_dec->p_fifo )
\r
266 p_dec->p_fifo->b_error = 1;
\r
268 /* End of the libmpeg2 decoder thread */
\r
269 CloseDecoder( p_dec );
\r
275 /*****************************************************************************
\r
276 * CloseDecoder: libmpeg2 decoder destruction
\r
277 *****************************************************************************/
\r
278 static void CloseDecoder( dec_thread_t * p_dec )
\r
283 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
\r
285 vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
\r
287 if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
\r