]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* modules/codec/libmpeg2.c: fixed the "main: cannot delete object with
[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.17 2003/05/04 22:33:35 massiot 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 #include "vout_synchro.h"
38
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) */
44
45 /*****************************************************************************
46  * dec_thread_t : libmpeg2 decoder thread descriptor
47  *****************************************************************************/
48 typedef struct dec_thread_t
49 {
50     /*
51      * libmpeg2 properties
52      */
53     mpeg2dec_t          *p_mpeg2dec;
54     const mpeg2_info_t  *p_info;
55
56     /*
57      * Input properties
58      */
59     decoder_fifo_t   *p_fifo;                  /* stores the PES stream data */
60     pes_packet_t     *p_pes;                  /* current PES we are decoding */
61     mtime_t          i_pts;
62     mtime_t          i_previous_pts;
63     mtime_t          i_current_pts;
64     mtime_t          i_period_remainder;
65     int              i_current_rate;
66     picture_t *      p_picture_to_destroy;
67
68     /*
69      * Output properties
70      */
71     vout_thread_t *p_vout;
72     vout_synchro_t *p_synchro;
73
74 } dec_thread_t;
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 static int  OpenDecoder  ( vlc_object_t * );
80 static int  RunDecoder   ( decoder_fifo_t * );
81 static void CloseDecoder ( dec_thread_t * );
82
83 static picture_t *GetNewPicture( dec_thread_t *, uint8_t ** );
84
85 /*****************************************************************************
86  * Module descriptor
87  *****************************************************************************/
88 vlc_module_begin();
89     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
90     set_capability( "decoder", 40 );
91     set_callbacks( OpenDecoder, NULL );
92     add_shortcut( "libmpeg2" );
93 vlc_module_end();
94
95 /*****************************************************************************
96  * OpenDecoder: probe the decoder and return score
97  *****************************************************************************/
98 static int OpenDecoder( vlc_object_t *p_this )
99 {
100     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
101
102     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
103     {
104         return VLC_EGENERIC;
105     }
106
107     p_fifo->pf_run = RunDecoder;
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * RunDecoder: the libmpeg2 decoder
113  *****************************************************************************/
114 static int RunDecoder( decoder_fifo_t *p_fifo )
115 {
116     dec_thread_t    *p_dec;
117     data_packet_t   *p_data = NULL;
118     mpeg2_state_t   state;
119     picture_t       *p_pic;
120     int             i_aspect, i_chroma;
121
122     /* Allocate the memory needed to store the thread's structure */
123     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
124         == NULL)
125     {
126         msg_Err( p_fifo, "out of memory" );
127         goto error;
128     }
129
130     /* Initialize the thread properties */
131     memset( p_dec, 0, sizeof(dec_thread_t) );
132     p_dec->p_fifo     = p_fifo;
133     p_dec->p_pes      = NULL;
134     p_dec->p_vout     = NULL;
135     p_dec->p_mpeg2dec = NULL;
136     p_dec->p_synchro  = NULL;
137     p_dec->p_info     = NULL;
138     p_dec->i_pts      = mdate() + DEFAULT_PTS_DELAY;
139     p_dec->i_current_pts  = 0;
140     p_dec->i_previous_pts = 0;
141     p_dec->i_period_remainder = 0;
142     p_dec->p_picture_to_destroy = NULL;
143
144     /* Initialize decoder */
145     p_dec->p_mpeg2dec = mpeg2_init();
146     if( p_dec->p_mpeg2dec == NULL)
147         goto error;
148
149     p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
150
151     /* libmpeg2 decoder thread's main loop */
152     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
153     {
154         state = mpeg2_parse( p_dec->p_mpeg2dec );
155
156         switch( state )
157         {
158         case STATE_BUFFER:
159             /* Feed libmpeg2 a data packet at a time */
160             if( p_data == NULL )
161             {
162                 /* Get the next PES */
163                 if( p_dec->p_pes )
164                     input_DeletePES( p_dec->p_fifo->p_packets_mgt,
165                                      p_dec->p_pes );
166
167                 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
168                 if( !p_dec->p_pes )
169                 {
170                     p_dec->p_fifo->b_error = 1;
171                     break;
172                 }
173
174                 if( p_dec->p_pes->b_discontinuity && p_dec->p_synchro )
175                 {
176                     vout_SynchroReset( p_dec->p_synchro );
177                     if ( p_dec->p_info->current_fbuf != NULL )
178                         p_dec->p_picture_to_destroy
179                             = p_dec->p_info->current_fbuf->id;
180                 }
181
182                 if( p_dec->p_pes->i_pts )
183                 {
184                     mpeg2_pts( p_dec->p_mpeg2dec,
185                                (uint32_t)p_dec->p_pes->i_pts );
186                     p_dec->i_previous_pts = p_dec->i_current_pts;
187                     p_dec->i_current_pts = p_dec->p_pes->i_pts;
188                 }
189
190                 p_dec->i_current_rate = p_dec->p_pes->i_rate;
191                 p_data = p_dec->p_pes->p_first;
192             }
193
194             if( p_data != NULL )
195             {
196                 mpeg2_buffer( p_dec->p_mpeg2dec,
197                               p_data->p_payload_start,
198                               p_data->p_payload_end );
199
200                 p_data = p_data->p_next;
201             }
202             break;
203
204         case STATE_SEQUENCE:
205         {
206             /* Initialize video output */
207             uint8_t *buf[3];
208             buf[0] = buf[1] = buf[2] = NULL;
209
210             /* Check whether the input gives a particular aspect ratio */
211             if( p_dec->p_fifo->p_demux_data
212                 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
213             {
214                 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
215                 switch( i_aspect )
216                 {
217                 case AR_3_4_PICTURE:
218                     i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
219                     break;
220                 case AR_16_9_PICTURE:
221                     i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
222                     break;
223                 case AR_221_1_PICTURE:
224                     i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
225                     break;
226                 case AR_SQUARE_PICTURE:
227                 default:
228                     i_aspect = VOUT_ASPECT_FACTOR *
229                                    p_dec->p_info->sequence->width /
230                                    p_dec->p_info->sequence->height;
231                     break;
232                 }
233             }
234             else
235             {
236                 /* Use the value provided in the MPEG sequence header */
237                 i_aspect = ((uint64_t)p_dec->p_info->sequence->display_width) *
238                     p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
239                     p_dec->p_info->sequence->display_height /
240                     p_dec->p_info->sequence->pixel_height;
241             }
242
243             i_chroma = VLC_FOURCC('Y','V','1','2');
244
245             p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
246                                           p_dec->p_info->sequence->width,
247                                           p_dec->p_info->sequence->height,
248                                           i_chroma, i_aspect );
249
250             msg_Dbg( p_dec->p_fifo, "%dx%d, aspect %d, %u.%03u fps",
251                      p_dec->p_info->sequence->width,
252                      p_dec->p_info->sequence->height, i_aspect,
253                      (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period / 1001),
254                      (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period % 1001) );
255
256             mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
257
258             /* Set the first 2 reference frames */
259             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
260             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
261
262             if ( p_dec->p_synchro )
263             {
264                 vout_SynchroRelease( p_dec->p_synchro );
265             }
266             p_dec->p_synchro = vout_SynchroInit( p_dec->p_fifo, p_dec->p_vout,
267                 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period) );
268         }
269         break;
270
271         case STATE_PICTURE:
272         {
273             uint8_t *buf[3];
274             buf[0] = buf[1] = buf[2] = NULL;
275
276             vout_SynchroNewPicture( p_dec->p_synchro,
277                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
278                 p_dec->p_info->current_picture->nb_fields,
279                 (p_dec->p_info->current_picture->flags & PIC_FLAG_PTS) ?
280                     ( (p_dec->p_info->current_picture->pts ==
281                                 (uint32_t)p_dec->i_current_pts) ?
282                               p_dec->i_current_pts : p_dec->i_previous_pts ) : 0,
283                 0,
284                 p_dec->i_current_rate );
285
286             if ( !vout_SynchroChoose( p_dec->p_synchro,
287                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE ) )
288             {
289                 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
290                 vout_SynchroTrash( p_dec->p_synchro );
291                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
292             }
293             else
294             {
295                 mpeg2_skip( p_dec->p_mpeg2dec, 0 );
296                 vout_SynchroDecode( p_dec->p_synchro );
297                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
298                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
299             }
300         }
301         break;
302
303         case STATE_END:
304         case STATE_SLICE:
305             if( p_dec->p_info->display_fbuf
306                 && p_dec->p_info->display_fbuf->id )
307             {
308                 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
309
310                 if ( p_pic != NULL )
311                 {
312                     if ( p_dec->p_picture_to_destroy != p_pic )
313                     {
314                         vout_SynchroEnd( p_dec->p_synchro,
315                             p_dec->p_info->display_picture->flags
316                              & PIC_MASK_CODING_TYPE,
317                             0 );
318                         vout_DatePicture( p_dec->p_vout, p_pic,
319                             vout_SynchroDate( p_dec->p_synchro ) );
320                         vout_DisplayPicture( p_dec->p_vout, p_pic );
321                     }
322                     else
323                     {
324                         p_dec->p_picture_to_destroy = NULL;
325                         vout_SynchroEnd( p_dec->p_synchro,
326                             p_dec->p_info->display_picture->flags
327                              & PIC_MASK_CODING_TYPE,
328                             1 );
329                         vout_DatePicture( p_dec->p_vout, p_pic, 0 );
330                         vout_DisplayPicture( p_dec->p_vout, p_pic );
331                     }
332                 }
333             }
334
335             if( p_dec->p_info->discard_fbuf &&
336                 p_dec->p_info->discard_fbuf->id )
337             {
338                 p_pic = (picture_t *)p_dec->p_info->discard_fbuf->id;
339                 vout_UnlinkPicture( p_dec->p_vout, p_pic );
340             }
341             break;
342
343         case STATE_INVALID:
344         {
345             uint8_t *buf[3];
346             buf[0] = buf[1] = buf[2] = NULL;
347
348             msg_Warn( p_dec->p_fifo, "invalid picture encountered" );
349             if ( (p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
350                   != B_CODING_TYPE )
351             {
352                 vout_SynchroReset( p_dec->p_synchro );
353             }
354             mpeg2_skip( p_dec->p_mpeg2dec, 1 );
355
356             if( p_dec->p_info->current_fbuf &&
357                 p_dec->p_info->current_fbuf->id )
358             {
359                 p_pic = (picture_t *)p_dec->p_info->current_fbuf->id;
360                 vout_UnlinkPicture( p_dec->p_vout, p_pic );
361                 vout_DestroyPicture( p_dec->p_vout, p_pic );
362             }
363             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
364             break;
365         }
366
367         default:
368             break;
369         }
370     }
371
372     /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
373     if( p_dec->p_fifo->b_error )
374     {
375         DecoderError( p_dec->p_fifo );
376     }
377
378     /* End of the libmpeg2 decoder thread */
379     CloseDecoder( p_dec );
380
381     return 0;
382
383  error:
384     DecoderError( p_fifo );
385     if( p_dec )
386     {
387         if( p_dec->p_fifo )
388             p_dec->p_fifo->b_error = 1;
389
390         /* End of the libmpeg2 decoder thread */
391         CloseDecoder( p_dec );
392     }
393
394     return -1;
395 }
396
397 /*****************************************************************************
398  * CloseDecoder: libmpeg2 decoder destruction
399  *****************************************************************************/
400 static void CloseDecoder( dec_thread_t * p_dec )
401 {
402     if( p_dec )
403     {
404         int i_pic;
405
406         if( p_dec->p_pes )
407             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
408
409         if( p_dec->p_synchro )
410             vout_SynchroRelease( p_dec->p_synchro );
411
412         if( p_dec->p_vout )
413         {
414             /* Temporary hack to free the pictures in use by libmpeg2 */
415             for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
416             {
417                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
418                       RESERVED_PICTURE )
419                     vout_DestroyPicture( p_dec->p_vout,
420                                      p_dec->p_vout->render.pp_picture[i_pic] );
421                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
422                     vout_UnlinkPicture( p_dec->p_vout,
423                                      p_dec->p_vout->render.pp_picture[i_pic] );
424             }
425
426             vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
427         }
428
429         if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
430
431         free( p_dec );
432     }
433 }
434
435 /*****************************************************************************
436  * GetNewPicture: Get a new picture from the vout and set the buf struct
437  *****************************************************************************/
438 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
439 {
440     picture_t *p_pic;
441
442     /* Get a new picture */
443     while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
444     {
445         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
446             break;
447
448         msleep( VOUT_OUTMEM_SLEEP );
449     }
450     if( p_pic == NULL )
451         return NULL;
452     vout_LinkPicture( p_dec->p_vout, p_pic );
453
454     pp_buf[0] = p_pic->p[0].p_pixels;
455     pp_buf[1] = p_pic->p[1].p_pixels;
456     pp_buf[2] = p_pic->p[2].p_pixels;
457
458     return p_pic;
459 }