]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* modules/codec/libmpeg2.c: fixed pts handling.
[vlc] / 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.2 2003/02/25 21:09:34 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     mtime_t          i_previous_pts;\r
55     mtime_t          i_current_pts;\r
56 \r
57     /*\r
58      * Output properties\r
59      */\r
60     vout_thread_t *p_vout;\r
61 \r
62 } dec_thread_t;\r
63 \r
64 /*****************************************************************************\r
65  * Local prototypes\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
70 \r
71 /*****************************************************************************\r
72  * Module descriptor\r
73  *****************************************************************************/\r
74 vlc_module_begin();\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
79 vlc_module_end();\r
80 \r
81 /*****************************************************************************\r
82  * OpenDecoder: probe the decoder and return score\r
83  *****************************************************************************/\r
84 static int OpenDecoder( vlc_object_t *p_this )\r
85 {\r
86     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;\r
87 \r
88     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )\r
89     {\r
90         return VLC_EGENERIC;\r
91     }\r
92 \r
93     p_fifo->pf_run = RunDecoder;\r
94     return VLC_SUCCESS;\r
95 }\r
96 /*****************************************************************************\r
97  * RunDecoder: the libmpeg2 decoder\r
98  *****************************************************************************/\r
99 static int RunDecoder( decoder_fifo_t *p_fifo )\r
100 {\r
101     dec_thread_t    *p_dec;\r
102     data_packet_t   *p_data = NULL;\r
103     mpeg2_state_t   state;\r
104     picture_t       *p_pic;\r
105     int             i_aspect, i_chroma;\r
106 \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
109         == NULL)\r
110     {\r
111         msg_Err( p_fifo, "out of memory" );\r
112         goto error;\r
113     }\r
114 \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
122     p_dec->i_pts      = 0;\r
123     p_dec->i_current_pts  = 0;\r
124     p_dec->i_previous_pts = 0;\r
125 \r
126     /* Initialize decoder */\r
127     p_dec->p_mpeg2dec = mpeg2_init();\r
128     if( p_dec->p_mpeg2dec == NULL)\r
129         goto error;\r
130 \r
131     p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );\r
132 \r
133     /* libmpeg2 decoder thread's main loop */\r
134     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )\r
135     {\r
136         state = mpeg2_parse( p_dec->p_mpeg2dec );\r
137 \r
138         switch( state )\r
139         {\r
140         case STATE_BUFFER:\r
141             /* Feed libmpeg2 a data packet at a time */\r
142             if( p_data == NULL )\r
143             {\r
144                 /* Get the next PES */\r
145                 if( p_dec->p_pes )\r
146                     input_DeletePES( p_dec->p_fifo->p_packets_mgt,\r
147                                      p_dec->p_pes );\r
148 \r
149                 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );\r
150                 if( !p_dec->p_pes )\r
151                 {\r
152                     p_dec->p_fifo->b_error = 1;\r
153                     break;\r
154                 }\r
155 \r
156                 if( p_dec->p_pes->i_pts )\r
157                 {\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
162                 }\r
163                 p_data = p_dec->p_pes->p_first;\r
164             }\r
165 \r
166             if( p_data != NULL )\r
167             {\r
168                 mpeg2_buffer( p_dec->p_mpeg2dec,\r
169                               p_data->p_payload_start,\r
170                               p_data->p_payload_end );\r
171 \r
172                 p_data = p_data->p_next;\r
173             }\r
174             break;\r
175 \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
182 \r
183             i_chroma = VLC_FOURCC('Y','V','1','2');\r
184 \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
189             break;\r
190 \r
191         case STATE_PICTURE:\r
192           {\r
193             uint8_t * buf[3];\r
194 \r
195             /* Get a new picture */\r
196             while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )\r
197             {\r
198                 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )\r
199                     break;\r
200 \r
201                 msleep( VOUT_OUTMEM_SLEEP );\r
202             }\r
203             if( p_pic == NULL )\r
204                 break;\r
205 \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
210           }\r
211           break;\r
212 \r
213         case STATE_END:\r
214 #if 0\r
215             msg_Err( p_dec->p_fifo, "STATE_END" );\r
216 #endif\r
217         case STATE_SLICE:\r
218 #if 0\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
222 #endif\r
223             if( p_dec->p_info->display_fbuf\r
224                 && p_dec->p_info->display_fbuf->id )\r
225             {\r
226                 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;\r
227 \r
228                 if( p_dec->p_info->display_picture->flags & PIC_FLAG_PTS )\r
229                 {\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
233                 }\r
234                 else\r
235                 {\r
236                     p_dec->i_pts += (p_dec->p_info->sequence->frame_period/27);\r
237                 }\r
238 \r
239                 vout_DatePicture( p_dec->p_vout, p_pic, p_dec->i_pts );\r
240 \r
241                 vout_DisplayPicture( p_dec->p_vout, p_pic );\r
242             }\r
243             break;\r
244 \r
245         default:\r
246             break;\r
247         }\r
248     }\r
249 \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
252     {\r
253         DecoderError( p_dec->p_fifo );\r
254     }\r
255 \r
256     /* End of the libmpeg2 decoder thread */\r
257     CloseDecoder( p_dec );\r
258 \r
259     return 0;\r
260 \r
261  error:\r
262     DecoderError( p_fifo );\r
263     if( p_dec )\r
264     {\r
265         if( p_dec->p_fifo )\r
266             p_dec->p_fifo->b_error = 1;\r
267 \r
268         /* End of the libmpeg2 decoder thread */\r
269         CloseDecoder( p_dec );\r
270     }\r
271 \r
272     return -1;\r
273 }\r
274 \r
275 /*****************************************************************************\r
276  * CloseDecoder: libmpeg2 decoder destruction\r
277  *****************************************************************************/\r
278 static void CloseDecoder( dec_thread_t * p_dec )\r
279 {\r
280     if( p_dec )\r
281     {\r
282         if( p_dec->p_pes )\r
283             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );\r
284 \r
285         vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );\r
286 \r
287         if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );\r
288 \r
289         free( p_dec );\r
290     }\r
291 }\r