]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* po/fr.po: small update.
[vlc] / modules / codec / libmpeg2.c
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.8 2003/04/07 17:35:01 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                    /* memcpy(), memset() */
34
35 #include <mpeg2dec/mpeg2.h>
36
37 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
38 #define AR_SQUARE_PICTURE       1                           /* square pixels */
39 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
40 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
41 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
42
43 /*****************************************************************************
44  * dec_thread_t : libmpeg2 decoder thread descriptor
45  *****************************************************************************/
46 typedef struct dec_thread_t
47 {
48     /*
49      * libmpeg2 properties
50      */
51     mpeg2dec_t          *p_mpeg2dec;
52     const mpeg2_info_t  *p_info;
53
54     /*
55      * Input properties
56      */
57     decoder_fifo_t   *p_fifo;                  /* stores the PES stream data */
58     pes_packet_t     *p_pes;                  /* current PES we are decoding */
59     mtime_t          i_pts;
60     mtime_t          i_previous_pts;
61     mtime_t          i_current_pts;
62     mtime_t          i_period_remainder;
63
64     /*
65      * Output properties
66      */
67     vout_thread_t *p_vout;
68
69 } dec_thread_t;
70
71 /*****************************************************************************
72  * Local prototypes
73  *****************************************************************************/
74 static int  OpenDecoder  ( vlc_object_t * );
75 static int  RunDecoder   ( decoder_fifo_t * );
76 static void CloseDecoder ( dec_thread_t * );
77
78 static picture_t *GetNewPicture( dec_thread_t *, uint8_t ** );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 vlc_module_begin();
84     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
85     set_capability( "decoder", 40 );
86     set_callbacks( OpenDecoder, NULL );
87     add_shortcut( "libmpeg2" );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * OpenDecoder: probe the decoder and return score
92  *****************************************************************************/
93 static int OpenDecoder( vlc_object_t *p_this )
94 {
95     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
96
97     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
98     {
99         return VLC_EGENERIC;
100     }
101
102     p_fifo->pf_run = RunDecoder;
103     return VLC_SUCCESS;
104 }
105 /*****************************************************************************
106  * RunDecoder: the libmpeg2 decoder
107  *****************************************************************************/
108 static int RunDecoder( decoder_fifo_t *p_fifo )
109 {
110     dec_thread_t    *p_dec;
111     data_packet_t   *p_data = NULL;
112     mpeg2_state_t   state;
113     picture_t       *p_pic;
114     int             i_aspect, i_chroma;
115
116     /* Allocate the memory needed to store the thread's structure */
117     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
118         == NULL)
119     {
120         msg_Err( p_fifo, "out of memory" );
121         goto error;
122     }
123
124     /* Initialize the thread properties */
125     memset( p_dec, 0, sizeof(dec_thread_t) );
126     p_dec->p_fifo     = p_fifo;
127     p_dec->p_pes      = NULL;
128     p_dec->p_vout     = NULL;
129     p_dec->p_mpeg2dec = NULL;
130     p_dec->p_info     = NULL;
131     p_dec->i_pts      = mdate() + DEFAULT_PTS_DELAY;
132     p_dec->i_current_pts  = 0;
133     p_dec->i_previous_pts = 0;
134     p_dec->i_period_remainder = 0;
135
136     /* Initialize decoder */
137     p_dec->p_mpeg2dec = mpeg2_init();
138     if( p_dec->p_mpeg2dec == NULL)
139         goto error;
140
141     p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
142
143     /* libmpeg2 decoder thread's main loop */
144     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
145     {
146         state = mpeg2_parse( p_dec->p_mpeg2dec );
147
148         switch( state )
149         {
150         case STATE_BUFFER:
151             /* Feed libmpeg2 a data packet at a time */
152             if( p_data == NULL )
153             {
154                 /* Get the next PES */
155                 if( p_dec->p_pes )
156                     input_DeletePES( p_dec->p_fifo->p_packets_mgt,
157                                      p_dec->p_pes );
158
159                 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
160                 if( !p_dec->p_pes )
161                 {
162                     p_dec->p_fifo->b_error = 1;
163                     break;
164                 }
165
166                 if( p_dec->p_pes->i_pts )
167                 {
168                     mpeg2_pts( p_dec->p_mpeg2dec,
169                                (uint32_t)p_dec->p_pes->i_pts );
170                     p_dec->i_previous_pts = p_dec->i_current_pts;
171                     p_dec->i_current_pts = p_dec->p_pes->i_pts;
172                 }
173                 p_data = p_dec->p_pes->p_first;
174             }
175
176             if( p_data != NULL )
177             {
178                 mpeg2_buffer( p_dec->p_mpeg2dec,
179                               p_data->p_payload_start,
180                               p_data->p_payload_end );
181
182                 p_data = p_data->p_next;
183             }
184             break;
185
186         case STATE_SEQUENCE:
187         {
188             /* Initialize video output */
189             uint8_t *buf[3];
190
191             /* Check whether the input gives a particular aspect ratio */
192             if( p_dec->p_fifo->p_demux_data
193                 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
194             {
195                 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
196                 switch( i_aspect )
197                 {
198                 case AR_3_4_PICTURE:
199                     i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
200                     break;
201                 case AR_16_9_PICTURE:
202                     i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
203                     break;
204                 case AR_221_1_PICTURE:
205                     i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
206                     break;
207                 case AR_SQUARE_PICTURE:
208                 default:
209                     i_aspect = VOUT_ASPECT_FACTOR *
210                                    p_dec->p_info->sequence->width /
211                                    p_dec->p_info->sequence->height;
212                     break;
213                 }
214             }
215             else
216             {
217                 /* Use the value provided in the MPEG sequence header */
218                 i_aspect = ((uint64_t)p_dec->p_info->sequence->width) *
219                     p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
220                     p_dec->p_info->sequence->height /
221                     p_dec->p_info->sequence->pixel_height;
222             }
223
224             i_chroma = VLC_FOURCC('Y','V','1','2');
225
226             p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
227                                           p_dec->p_info->sequence->width,
228                                           p_dec->p_info->sequence->height,
229                                           i_chroma, i_aspect );
230
231             mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
232
233             /* Set the first 2 reference frames */
234             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
235             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
236             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
237             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
238         }
239         break;
240
241         case STATE_PICTURE:
242         {
243             uint8_t *buf[3];
244
245             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
246             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
247
248             /* Store the date for the picture */
249             if( p_dec->p_info->current_picture->flags & PIC_FLAG_PTS )
250             {
251                 p_pic->date = ( p_dec->p_info->current_picture->pts ==
252                                 (uint32_t)p_dec->i_current_pts ) ?
253                               p_dec->i_current_pts : p_dec->i_previous_pts;
254             }
255         }
256         break;
257
258         case STATE_END:
259         case STATE_SLICE:
260             if( p_dec->p_info->display_fbuf
261                 && p_dec->p_info->display_fbuf->id )
262             {
263                 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
264
265                 /* Date the new picture */
266                 if( p_dec->p_info->display_picture->flags & PIC_FLAG_PTS )
267                 {
268                     p_dec->i_pts = p_pic->date;
269                     p_dec->i_period_remainder = 0;
270                 }
271                 else
272                 {
273                     p_dec->i_pts += ( (p_dec->p_info->sequence->frame_period +
274                                        p_dec->i_period_remainder) / 27 );
275                     p_dec->i_period_remainder =
276                         p_dec->p_info->sequence->frame_period +
277                         p_dec->i_period_remainder -
278                         ( p_dec->p_info->sequence->frame_period +
279                           p_dec->i_period_remainder ) / 27 * 27;
280                 }
281                 vout_DatePicture( p_dec->p_vout, p_pic, p_dec->i_pts );
282
283                 vout_DisplayPicture( p_dec->p_vout, p_pic );
284
285                 /* Handle pulldown by adding some delay to the pts of the next
286                  * picture. */
287                 if( p_dec->p_info->display_picture->nb_fields > 2 )
288                 {
289                     int i_repeat_fields =
290                         p_dec->p_info->display_picture->nb_fields - 2;
291
292                     p_dec->i_pts += ( (p_dec->p_info->sequence->frame_period +
293                                        p_dec->i_period_remainder)
294                                       / 27 / 2 * i_repeat_fields );
295                     p_dec->i_period_remainder =
296                         p_dec->p_info->sequence->frame_period +
297                         p_dec->i_period_remainder -
298                         ( p_dec->p_info->sequence->frame_period +
299                           p_dec->i_period_remainder ) / 27 / 2 * 27 * 2;
300                 }
301             }
302             break;
303
304         default:
305             break;
306         }
307     }
308
309     /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
310     if( p_dec->p_fifo->b_error )
311     {
312         DecoderError( p_dec->p_fifo );
313     }
314
315     /* End of the libmpeg2 decoder thread */
316     CloseDecoder( p_dec );
317
318     return 0;
319
320  error:
321     DecoderError( p_fifo );
322     if( p_dec )
323     {
324         if( p_dec->p_fifo )
325             p_dec->p_fifo->b_error = 1;
326
327         /* End of the libmpeg2 decoder thread */
328         CloseDecoder( p_dec );
329     }
330
331     return -1;
332 }
333
334 /*****************************************************************************
335  * CloseDecoder: libmpeg2 decoder destruction
336  *****************************************************************************/
337 static void CloseDecoder( dec_thread_t * p_dec )
338 {
339     if( p_dec )
340     {
341         int i_pic;
342
343         if( p_dec->p_pes )
344             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
345
346         if( p_dec->p_vout )
347         {
348             /* Temporary hack to free the pictures in use by libmpeg2 */
349             for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
350             {
351                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
352                       RESERVED_PICTURE )
353                     vout_DestroyPicture( p_dec->p_vout,
354                                      p_dec->p_vout->render.pp_picture[i_pic] );
355             }
356
357             vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
358         }
359
360         if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
361
362         free( p_dec );
363     }
364 }
365
366 /*****************************************************************************
367  * GetNewPicture: Get a new picture from the vout and set the buf struct
368  *****************************************************************************/
369 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
370 {
371     picture_t *p_pic;
372
373     /* Get a new picture */
374     while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
375     {
376         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
377             break;
378
379         msleep( VOUT_OUTMEM_SLEEP );
380     }
381     if( p_pic == NULL )
382         return NULL;
383
384     pp_buf[0] = p_pic->p[0].p_pixels;
385     pp_buf[1] = p_pic->p[1].p_pixels;
386     pp_buf[2] = p_pic->p[2].p_pixels;
387
388     return p_pic;
389 }