* configure.ac.in, modules/codec/Modules.am, modules/codec/libmpeg2.c:
[vlc.git] / modules / codec / libmpeg2.c
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.1 2003/02/25 17:15:32 gbazin Exp $\r
6  *\r
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>\r
8  *\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
13  * \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
18  *\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
23 \r
24 /*****************************************************************************\r
25  * Preamble\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
31 \r
32 #include <stdlib.h>                                      /* malloc(), free() */\r
33 #include <string.h>                                    /* memcpy(), memset() */\r
34 \r
35 #include <mpeg2dec/mpeg2.h>\r
36 \r
37 /*****************************************************************************\r
38  * dec_thread_t : libmpeg2 decoder thread descriptor\r
39  *****************************************************************************/\r
40 typedef struct dec_thread_t\r
41 {\r
42     /*\r
43      * libmpeg2 properties\r
44      */\r
45     mpeg2dec_t          *p_mpeg2dec;\r
46     const mpeg2_info_t  *p_info;\r
47 \r
48     /*\r
49      * Input properties\r
50      */\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
53     mtime_t          i_pts;\r
54 \r
55     /*\r
56      * Output properties\r
57      */\r
58     vout_thread_t *p_vout;\r
59 \r
60 } dec_thread_t;\r
61 \r
62 /*****************************************************************************\r
63  * Local prototypes\r
64  *****************************************************************************/\r
65 static int  OpenDecoder  ( vlc_object_t * );\r
66 static int  RunDecoder   ( decoder_fifo_t * );\r
67 static void CloseDecoder ( dec_thread_t * );\r
68 \r
69 /*****************************************************************************\r
70  * Module descriptor\r
71  *****************************************************************************/\r
72 vlc_module_begin();\r
73     set_description( _("libmpeg2 decoder module") );\r
74     set_capability( "decoder", 40 );\r
75     set_callbacks( OpenDecoder, NULL );\r
76     add_shortcut( "libmpeg2" );\r
77 vlc_module_end();\r
78 \r
79 /*****************************************************************************\r
80  * OpenDecoder: probe the decoder and return score\r
81  *****************************************************************************/\r
82 static int OpenDecoder( vlc_object_t *p_this )\r
83 {\r
84     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;\r
85 \r
86     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )\r
87     {\r
88         return VLC_EGENERIC;\r
89     }\r
90 \r
91     p_fifo->pf_run = RunDecoder;\r
92     return VLC_SUCCESS;\r
93 }\r
94 /*****************************************************************************\r
95  * RunDecoder: the libmpeg2 decoder\r
96  *****************************************************************************/\r
97 static int RunDecoder( decoder_fifo_t *p_fifo )\r
98 {\r
99     dec_thread_t    *p_dec;\r
100     data_packet_t   *p_data = NULL;\r
101     mpeg2_state_t   state;\r
102     picture_t       *p_pic;\r
103     int             i_aspect, i_chroma;\r
104 \r
105     /* Allocate the memory needed to store the thread's structure */\r
106     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )\r
107         == NULL)\r
108     {\r
109         msg_Err( p_fifo, "out of memory" );\r
110         goto error;\r
111     }\r
112 \r
113     /* Initialize the thread properties */\r
114     memset( p_dec, 0, sizeof(dec_thread_t) );\r
115     p_dec->p_fifo     = p_fifo;\r
116     p_dec->p_pes      = NULL;\r
117     p_dec->p_vout     = NULL;\r
118     p_dec->p_mpeg2dec = NULL;\r
119     p_dec->p_info     = NULL;\r
120     p_dec->i_pts      = 0;\r
121 \r
122     /* Initialize decoder */\r
123     p_dec->p_mpeg2dec = mpeg2_init();\r
124     if( p_dec->p_mpeg2dec == NULL)\r
125         goto error;\r
126 \r
127     p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );\r
128 \r
129     /* libmpeg2 decoder thread's main loop */\r
130     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )\r
131     {\r
132         state = mpeg2_parse( p_dec->p_mpeg2dec );\r
133 \r
134         switch( state )\r
135         {\r
136         case STATE_BUFFER:\r
137             /* Feed libmpeg2 a data packet at a time */\r
138             if( p_data == NULL )\r
139             {\r
140                 /* Get the next PES */\r
141                 if( p_dec->p_pes )\r
142                     input_DeletePES( p_dec->p_fifo->p_packets_mgt,\r
143                                      p_dec->p_pes );\r
144 \r
145                 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );\r
146                 if( !p_dec->p_pes )\r
147                 {\r
148                     p_dec->p_fifo->b_error = 1;\r
149                     break;\r
150                 }\r
151 \r
152                 if( p_dec->p_pes->i_pts )\r
153                 {\r
154                     mpeg2_pts( p_dec->p_mpeg2dec,\r
155                                p_dec->p_pes->i_pts * 9 / 100 );\r
156                 }\r
157                 p_data = p_dec->p_pes->p_first;\r
158             }\r
159 \r
160             if( p_data != NULL )\r
161             {\r
162                 mpeg2_buffer( p_dec->p_mpeg2dec,\r
163                               p_data->p_payload_start,\r
164                               p_data->p_payload_end );\r
165 \r
166                 p_data = p_data->p_next;\r
167             }\r
168             break;\r
169 \r
170         case STATE_SEQUENCE:\r
171             /* Initialize video output */\r
172             i_aspect = p_dec->p_info->sequence->width *\r
173                 p_dec->p_info->sequence->pixel_width /\r
174                 p_dec->p_info->sequence->height * VOUT_ASPECT_FACTOR /\r
175                 p_dec->p_info->sequence->pixel_height;\r
176 \r
177             i_chroma = VLC_FOURCC('Y','V','1','2');\r
178 \r
179             p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,\r
180                                           p_dec->p_info->sequence->width,\r
181                                           p_dec->p_info->sequence->height,\r
182                                           i_chroma, i_aspect );\r
183             break;\r
184 \r
185         case STATE_PICTURE:\r
186           {\r
187             uint8_t * buf[3];\r
188 \r
189             /* Get a new picture */\r
190             while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )\r
191             {\r
192                 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )\r
193                     break;\r
194 \r
195                 msleep( VOUT_OUTMEM_SLEEP );\r
196             }\r
197             if( p_pic == NULL )\r
198                 break;\r
199 \r
200             buf[0] = p_pic->p[0].p_pixels;\r
201             buf[1] = p_pic->p[1].p_pixels;\r
202             buf[2] = p_pic->p[2].p_pixels;\r
203             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );\r
204           }\r
205           break;\r
206 \r
207         case STATE_END:\r
208             msg_Err( p_dec->p_fifo, "STATE_END" );\r
209         case STATE_SLICE:\r
210 #if 0\r
211             msg_Err( p_dec->p_fifo, "STATE_SLICE: %i",\r
212                      p_dec->p_info->display_fbuf ?\r
213                      p_dec->p_info->display_fbuf->id : -1 );\r
214 #endif\r
215             if( p_dec->p_info->display_fbuf\r
216                 && p_dec->p_info->display_fbuf->id )\r
217             {\r
218                 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;\r
219 \r
220                 if( p_dec->p_info->display_picture->flags & PIC_FLAG_PTS )\r
221                 {\r
222                     p_dec->i_pts =\r
223                         ((mtime_t)p_dec->p_info->display_picture->pts)\r
224                         * 100 / 9;\r
225                 }\r
226                 else\r
227                 {\r
228                     p_dec->i_pts += (p_dec->p_info->sequence->frame_period/27);\r
229                 }\r
230 \r
231                 vout_DatePicture( p_dec->p_vout, p_pic, p_dec->i_pts );\r
232 \r
233                 vout_DisplayPicture( p_dec->p_vout, p_pic );\r
234             }\r
235             break;\r
236 \r
237         default:\r
238             break;\r
239         }\r
240     }\r
241 \r
242     /* If b_error is set, the libmpeg2 decoder thread enters the error loop */\r
243     if( p_dec->p_fifo->b_error )\r
244     {\r
245         DecoderError( p_dec->p_fifo );\r
246     }\r
247 \r
248     /* End of the libmpeg2 decoder thread */\r
249     CloseDecoder( p_dec );\r
250 \r
251     return 0;\r
252 \r
253  error:\r
254     DecoderError( p_fifo );\r
255     if( p_dec )\r
256     {\r
257         if( p_dec->p_fifo )\r
258             p_dec->p_fifo->b_error = 1;\r
259 \r
260         /* End of the libmpeg2 decoder thread */\r
261         CloseDecoder( p_dec );\r
262     }\r
263 \r
264     return -1;\r
265 }\r
266 \r
267 /*****************************************************************************\r
268  * CloseDecoder: libmpeg2 decoder destruction\r
269  *****************************************************************************/\r
270 static void CloseDecoder( dec_thread_t * p_dec )\r
271 {\r
272     if( p_dec )\r
273     {\r
274         if( p_dec->p_pes )\r
275             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );\r
276 \r
277         vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );\r
278 \r
279         if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );\r
280 \r
281         free( p_dec );\r
282     }\r
283 }\r