]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
Fixed a potential segfault.
[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.21 2003/06/10 11:12:38 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     vlc_bool_t          b_skip;
56
57     /*
58      * Input properties
59      */
60     decoder_fifo_t   *p_fifo;                  /* stores the PES stream data */
61     pes_packet_t     *p_pes;                  /* current PES we are decoding */
62     mtime_t          i_pts;
63     mtime_t          i_previous_pts;
64     mtime_t          i_current_pts;
65     mtime_t          i_period_remainder;
66     int              i_current_rate;
67     picture_t *      p_picture_to_destroy;
68     vlc_bool_t       b_garbage_pic;
69     vlc_bool_t       b_after_sequence_header; /* is it the next frame after
70                                                * the sequence header ?    */
71     vlc_bool_t       b_slice_i;             /* intra-slice refresh stream */
72
73     /*
74      * Output properties
75      */
76     vout_thread_t *p_vout;
77     vout_synchro_t *p_synchro;
78
79 } dec_thread_t;
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static int  OpenDecoder  ( vlc_object_t * );
85 static int  RunDecoder   ( decoder_fifo_t * );
86 static void CloseDecoder ( dec_thread_t * );
87
88 static picture_t *GetNewPicture( dec_thread_t *, uint8_t ** );
89
90 /*****************************************************************************
91  * Module descriptor
92  *****************************************************************************/
93 vlc_module_begin();
94     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
95     set_capability( "decoder", 40 );
96     set_callbacks( OpenDecoder, NULL );
97     add_shortcut( "libmpeg2" );
98 vlc_module_end();
99
100 /*****************************************************************************
101  * OpenDecoder: probe the decoder and return score
102  *****************************************************************************/
103 static int OpenDecoder( vlc_object_t *p_this )
104 {
105     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
106
107     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
108     {
109         return VLC_EGENERIC;
110     }
111
112     p_fifo->pf_run = RunDecoder;
113     return VLC_SUCCESS;
114 }
115
116 /*****************************************************************************
117  * RunDecoder: the libmpeg2 decoder
118  *****************************************************************************/
119 static int RunDecoder( decoder_fifo_t *p_fifo )
120 {
121     dec_thread_t    *p_dec;
122     data_packet_t   *p_data = NULL;
123     mpeg2_state_t   state;
124     picture_t       *p_pic;
125     int             i_aspect, i_chroma;
126
127     /* Allocate the memory needed to store the thread's structure */
128     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
129         == NULL)
130     {
131         msg_Err( p_fifo, "out of memory" );
132         goto error;
133     }
134
135     /* Initialize the thread properties */
136     memset( p_dec, 0, sizeof(dec_thread_t) );
137     p_dec->p_fifo     = p_fifo;
138     p_dec->p_pes      = NULL;
139     p_dec->p_vout     = NULL;
140     p_dec->p_mpeg2dec = NULL;
141     p_dec->p_synchro  = NULL;
142     p_dec->p_info     = NULL;
143     p_dec->i_pts      = mdate() + DEFAULT_PTS_DELAY;
144     p_dec->i_current_pts  = 0;
145     p_dec->i_previous_pts = 0;
146     p_dec->i_period_remainder = 0;
147     p_dec->p_picture_to_destroy = NULL;
148     p_dec->b_garbage_pic = 0;
149     p_dec->b_slice_i  = 0;
150     p_dec->b_skip     = 0;
151
152     /* Initialize decoder */
153     p_dec->p_mpeg2dec = mpeg2_init();
154     if( p_dec->p_mpeg2dec == NULL)
155         goto error;
156
157     p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
158
159     /* libmpeg2 decoder thread's main loop */
160     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
161     {
162         state = mpeg2_parse( p_dec->p_mpeg2dec );
163
164         switch( state )
165         {
166         case STATE_BUFFER:
167             /* Feed libmpeg2 a data packet at a time */
168             if( p_data == NULL )
169             {
170                 /* Get the next PES */
171                 if( p_dec->p_pes )
172                     input_DeletePES( p_dec->p_fifo->p_packets_mgt,
173                                      p_dec->p_pes );
174
175                 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
176                 if( !p_dec->p_pes )
177                 {
178                     p_dec->p_fifo->b_error = 1;
179                     break;
180                 }
181
182                 if( p_dec->p_pes->b_discontinuity && p_dec->p_synchro )
183                 {
184                     vout_SynchroReset( p_dec->p_synchro );
185                     if ( p_dec->p_info->current_fbuf != NULL
186                           && p_dec->p_info->current_fbuf->id != NULL )
187                     {
188                         p_dec->b_garbage_pic = 1;
189                         p_pic = p_dec->p_info->current_fbuf->id;
190                     }
191                     else
192                     {
193                         uint8_t *buf[3];
194                         buf[0] = buf[1] = buf[2] = NULL;
195                         if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
196                             break;
197                         mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
198                     }
199                     p_dec->p_picture_to_destroy = p_pic;
200
201                     memset( p_pic->p[0].p_pixels, 0,
202                             p_dec->p_info->sequence->width
203                              * p_dec->p_info->sequence->height );
204                     memset( p_pic->p[1].p_pixels, 0x80,
205                             p_dec->p_info->sequence->width
206                              * p_dec->p_info->sequence->height / 4 );
207                     memset( p_pic->p[2].p_pixels, 0x80,
208                             p_dec->p_info->sequence->width
209                              * p_dec->p_info->sequence->height / 4 );
210
211                     if ( p_dec->b_slice_i )
212                     {
213                         vout_SynchroNewPicture( p_dec->p_synchro,
214                             I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
215                         vout_SynchroDecode( p_dec->p_synchro );
216                         vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
217                     }
218                 }
219
220                 if( p_dec->p_pes->i_pts )
221                 {
222                     mpeg2_pts( p_dec->p_mpeg2dec,
223                                (uint32_t)p_dec->p_pes->i_pts );
224                     p_dec->i_previous_pts = p_dec->i_current_pts;
225                     p_dec->i_current_pts = p_dec->p_pes->i_pts;
226                 }
227
228                 p_dec->i_current_rate = p_dec->p_pes->i_rate;
229                 p_data = p_dec->p_pes->p_first;
230             }
231
232             if( p_data != NULL )
233             {
234                 mpeg2_buffer( p_dec->p_mpeg2dec,
235                               p_data->p_payload_start,
236                               p_data->p_payload_end );
237
238                 p_data = p_data->p_next;
239             }
240             break;
241
242         case STATE_SEQUENCE:
243         {
244             /* Initialize video output */
245             uint8_t *buf[3];
246             buf[0] = buf[1] = buf[2] = NULL;
247
248             /* Check whether the input gives a particular aspect ratio */
249             if( p_dec->p_fifo->p_demux_data
250                 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
251             {
252                 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
253                 switch( i_aspect )
254                 {
255                 case AR_3_4_PICTURE:
256                     i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
257                     break;
258                 case AR_16_9_PICTURE:
259                     i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
260                     break;
261                 case AR_221_1_PICTURE:
262                     i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
263                     break;
264                 case AR_SQUARE_PICTURE:
265                 default:
266                     i_aspect = VOUT_ASPECT_FACTOR *
267                                    p_dec->p_info->sequence->width /
268                                    p_dec->p_info->sequence->height;
269                     break;
270                 }
271             }
272             else
273             {
274                 /* Use the value provided in the MPEG sequence header */
275                 i_aspect = ((uint64_t)p_dec->p_info->sequence->display_width) *
276                     p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
277                     p_dec->p_info->sequence->display_height /
278                     p_dec->p_info->sequence->pixel_height;
279             }
280
281             i_chroma = VLC_FOURCC('Y','V','1','2');
282
283             p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
284                                           p_dec->p_info->sequence->width,
285                                           p_dec->p_info->sequence->height,
286                                           i_chroma, i_aspect );
287
288             msg_Dbg( p_dec->p_fifo, "%dx%d, aspect %d, %u.%03u fps",
289                      p_dec->p_info->sequence->width,
290                      p_dec->p_info->sequence->height, i_aspect,
291                      (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period / 1001),
292                      (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period % 1001) );
293
294             mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
295
296             /* Set the first 2 reference frames */
297             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
298
299             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
300             memset( p_pic->p[0].p_pixels, 0,
301                     p_dec->p_info->sequence->width
302                      * p_dec->p_info->sequence->height );
303             memset( p_pic->p[1].p_pixels, 0x80,
304                     p_dec->p_info->sequence->width
305                      * p_dec->p_info->sequence->height / 4 );
306             memset( p_pic->p[2].p_pixels, 0x80,
307                     p_dec->p_info->sequence->width
308                      * p_dec->p_info->sequence->height / 4 );
309             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
310             /* This picture will never go through display_picture. */
311             vout_DatePicture( p_dec->p_vout, p_pic, 0 );
312             vout_DisplayPicture( p_dec->p_vout, p_pic );
313             /* For some reason, libmpeg2 will put this pic twice in
314              * discard_picture. This can be considered a bug in libmpeg2. */
315             vout_LinkPicture( p_dec->p_vout, p_pic );
316
317             if ( p_dec->p_synchro )
318             {
319                 vout_SynchroRelease( p_dec->p_synchro );
320             }
321             p_dec->p_synchro = vout_SynchroInit( p_dec->p_fifo, p_dec->p_vout,
322                 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period) );
323             p_dec->b_after_sequence_header = 1;
324         }
325         break;
326
327         case STATE_PICTURE_2ND:
328             vout_SynchroNewPicture( p_dec->p_synchro,
329                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
330                 p_dec->p_info->current_picture->nb_fields,
331                 0, 0,
332                 p_dec->i_current_rate );
333
334             if ( p_dec->b_skip )
335             {
336                 vout_SynchroTrash( p_dec->p_synchro );
337             }
338             else
339             {
340                 vout_SynchroDecode( p_dec->p_synchro );
341             }
342             break;
343
344         case STATE_PICTURE:
345         {
346             uint8_t *buf[3];
347             buf[0] = buf[1] = buf[2] = NULL;
348
349             if ( p_dec->b_after_sequence_header
350                   && ((p_dec->p_info->current_picture->flags
351                         & PIC_MASK_CODING_TYPE)
352                        == PIC_FLAG_CODING_TYPE_P) )
353             {
354                 /* Intra-slice refresh. Simulate a blank I picture. */
355                 msg_Dbg( p_dec->p_fifo, "intra-slice refresh stream" );
356                 vout_SynchroNewPicture( p_dec->p_synchro,
357                     I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
358                 vout_SynchroDecode( p_dec->p_synchro );
359                 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
360                 p_dec->b_slice_i = 1;
361             }
362             p_dec->b_after_sequence_header = 0;
363
364             vout_SynchroNewPicture( p_dec->p_synchro,
365                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
366                 p_dec->p_info->current_picture->nb_fields,
367                 (p_dec->p_info->current_picture->flags & PIC_FLAG_PTS) ?
368                     ( (p_dec->p_info->current_picture->pts ==
369                                 (uint32_t)p_dec->i_current_pts) ?
370                               p_dec->i_current_pts : p_dec->i_previous_pts ) : 0,
371                 0,
372                 p_dec->i_current_rate );
373
374             if ( !(p_dec->b_slice_i
375                    && ((p_dec->p_info->current_picture->flags
376                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
377                    && !vout_SynchroChoose( p_dec->p_synchro,
378                               p_dec->p_info->current_picture->flags
379                                 & PIC_MASK_CODING_TYPE ) )
380             {
381                 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
382                 p_dec->b_skip = 1;
383                 vout_SynchroTrash( p_dec->p_synchro );
384                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
385             }
386             else
387             {
388                 mpeg2_skip( p_dec->p_mpeg2dec, 0 );
389                 p_dec->b_skip = 0;
390                 vout_SynchroDecode( p_dec->p_synchro );
391                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
392                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
393             }
394         }
395         break;
396
397         case STATE_END:
398         case STATE_SLICE:
399             if( p_dec->p_info->display_fbuf
400                 && p_dec->p_info->display_fbuf->id )
401             {
402                 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
403
404                 vout_SynchroEnd( p_dec->p_synchro,
405                             p_dec->p_info->display_picture->flags
406                              & PIC_MASK_CODING_TYPE,
407                             p_dec->b_garbage_pic );
408                 p_dec->b_garbage_pic = 0;
409                 vout_DisplayPicture( p_dec->p_vout, p_pic );
410
411                 if ( p_dec->p_picture_to_destroy != p_pic )
412                 {
413                     vout_DatePicture( p_dec->p_vout, p_pic,
414                         vout_SynchroDate( p_dec->p_synchro ) );
415                 }
416                 else
417                 {
418                     p_dec->p_picture_to_destroy = NULL;
419                     vout_DatePicture( p_dec->p_vout, p_pic, 0 );
420                 }
421             }
422
423             if( p_dec->p_info->discard_fbuf &&
424                 p_dec->p_info->discard_fbuf->id )
425             {
426                 p_pic = (picture_t *)p_dec->p_info->discard_fbuf->id;
427                 vout_UnlinkPicture( p_dec->p_vout, p_pic );
428             }
429             break;
430
431         case STATE_INVALID:
432         {
433             uint8_t *buf[3];
434             buf[0] = buf[1] = buf[2] = NULL;
435
436             msg_Warn( p_dec->p_fifo, "invalid picture encountered" );
437             if ( ( p_dec->p_info->current_picture == NULL ) || 
438                ( ( p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
439                   != B_CODING_TYPE ) )
440             {
441                 vout_SynchroReset( p_dec->p_synchro );
442             }
443             mpeg2_skip( p_dec->p_mpeg2dec, 1 );
444             p_dec->b_skip = 1;
445
446             if( p_dec->p_info->current_fbuf &&
447                 p_dec->p_info->current_fbuf->id )
448             {
449                 p_dec->b_garbage_pic = 1;
450                 p_pic = p_dec->p_info->current_fbuf->id;
451             }
452             else
453             {
454                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
455                     break;
456                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
457             }
458             p_dec->p_picture_to_destroy = p_pic;
459
460             memset( p_pic->p[0].p_pixels, 0,
461                     p_dec->p_info->sequence->width
462                      * p_dec->p_info->sequence->height );
463             memset( p_pic->p[1].p_pixels, 0x80,
464                     p_dec->p_info->sequence->width
465                      * p_dec->p_info->sequence->height / 4 );
466             memset( p_pic->p[2].p_pixels, 0x80,
467                     p_dec->p_info->sequence->width
468                      * p_dec->p_info->sequence->height / 4 );
469
470             if ( p_dec->b_slice_i )
471             {
472                 vout_SynchroNewPicture( p_dec->p_synchro,
473                             I_CODING_TYPE, 2, 0, 0, p_dec->i_current_rate );
474                 vout_SynchroDecode( p_dec->p_synchro );
475                 vout_SynchroEnd( p_dec->p_synchro, I_CODING_TYPE, 0 );
476             }
477             break;
478         }
479
480         default:
481             break;
482         }
483     }
484
485     /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
486     if( p_dec->p_fifo->b_error )
487     {
488         DecoderError( p_dec->p_fifo );
489     }
490
491     /* End of the libmpeg2 decoder thread */
492     CloseDecoder( p_dec );
493
494     return 0;
495
496  error:
497     DecoderError( p_fifo );
498     if( p_dec )
499     {
500         if( p_dec->p_fifo )
501             p_dec->p_fifo->b_error = 1;
502
503         /* End of the libmpeg2 decoder thread */
504         CloseDecoder( p_dec );
505     }
506
507     return -1;
508 }
509
510 /*****************************************************************************
511  * CloseDecoder: libmpeg2 decoder destruction
512  *****************************************************************************/
513 static void CloseDecoder( dec_thread_t * p_dec )
514 {
515     if( p_dec )
516     {
517         int i_pic;
518
519         if( p_dec->p_pes )
520             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
521
522         if( p_dec->p_synchro )
523             vout_SynchroRelease( p_dec->p_synchro );
524
525         if( p_dec->p_vout )
526         {
527             /* Temporary hack to free the pictures in use by libmpeg2 */
528             for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
529             {
530                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
531                       RESERVED_PICTURE )
532                     vout_DestroyPicture( p_dec->p_vout,
533                                      p_dec->p_vout->render.pp_picture[i_pic] );
534                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
535                     vout_UnlinkPicture( p_dec->p_vout,
536                                      p_dec->p_vout->render.pp_picture[i_pic] );
537             }
538
539             vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
540         }
541
542         if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
543
544         free( p_dec );
545     }
546 }
547
548 /*****************************************************************************
549  * GetNewPicture: Get a new picture from the vout and set the buf struct
550  *****************************************************************************/
551 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
552 {
553     picture_t *p_pic;
554     vlc_bool_t b_progressive = p_dec->p_info->current_picture != NULL ?
555         p_dec->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
556         1;
557     vlc_bool_t b_top_field_first = p_dec->p_info->current_picture != NULL ?
558         p_dec->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST :
559         1;
560     unsigned int i_nb_fields = p_dec->p_info->current_picture != NULL ?
561         p_dec->p_info->current_picture->nb_fields : 2;
562
563     /* Get a new picture */
564     while( !(p_pic = vout_CreatePicture( p_dec->p_vout,
565         b_progressive, b_top_field_first, i_nb_fields )) )
566     {
567         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
568             break;
569
570         msleep( VOUT_OUTMEM_SLEEP );
571     }
572     if( p_pic == NULL )
573         return NULL;
574     vout_LinkPicture( p_dec->p_vout, p_pic );
575
576     pp_buf[0] = p_pic->p[0].p_pixels;
577     pp_buf[1] = p_pic->p[1].p_pixels;
578     pp_buf[2] = p_pic->p[2].p_pixels;
579
580     return p_pic;
581 }