]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* ALL: Introduction of a new api for decoders.
[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.26 2003/09/02 20:19:25 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/input.h>
31 #include <vlc/decoder.h>
32
33 #include <stdlib.h>                                      /* malloc(), free() */
34 #include <string.h>                                    /* memcpy(), memset() */
35
36 #include <mpeg2dec/mpeg2.h>
37
38 #include "vout_synchro.h"
39
40 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
41 #define AR_SQUARE_PICTURE       1                           /* square pixels */
42 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
43 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
44 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
45
46 /*****************************************************************************
47  * decoder_sys_t : libmpeg2 decoder descriptor
48  *****************************************************************************/
49 struct decoder_sys_t
50 {
51     /*
52      * libmpeg2 properties
53      */
54     mpeg2dec_t          *p_mpeg2dec;
55     const mpeg2_info_t  *p_info;
56     vlc_bool_t          b_skip;
57
58     /*
59      * Input properties
60      */
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 };
80
81 /*****************************************************************************
82  * Local prototypes
83  *****************************************************************************/
84 static int  OpenDecoder  ( vlc_object_t * );
85 static int  InitDecoder  ( decoder_t * );
86 static int  RunDecoder   ( decoder_t *, block_t * );
87 static int  EndDecoder   ( decoder_t * );
88
89 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
90
91 /*****************************************************************************
92  * Module descriptor
93  *****************************************************************************/
94 vlc_module_begin();
95     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
96     set_capability( "decoder", 150 );
97     set_callbacks( OpenDecoder, NULL );
98     add_shortcut( "libmpeg2" );
99 vlc_module_end();
100
101 /*****************************************************************************
102  * OpenDecoder: probe the decoder and return score
103  *****************************************************************************/
104 static int OpenDecoder( vlc_object_t *p_this )
105 {
106     decoder_t *p_dec = (decoder_t*)p_this;
107
108     if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') &&
109         p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','1') &&
110         p_dec->p_fifo->i_fourcc != VLC_FOURCC('m','p','g','2') )
111     {
112         return VLC_EGENERIC;
113     }
114
115     p_dec->pf_init = InitDecoder;
116     p_dec->pf_decode = RunDecoder;
117     p_dec->pf_end = EndDecoder;
118
119     return VLC_SUCCESS;
120 }
121
122 /*****************************************************************************
123  * InitDecoder: Initalize the decoder
124  *****************************************************************************/
125 static int InitDecoder( decoder_t *p_dec )
126 {
127     /* Allocate the memory needed to store the decoder's structure */
128     if( ( p_dec->p_sys =
129           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
130     {
131         msg_Err( p_dec, "out of memory" );
132         return VLC_EGENERIC;
133     }
134
135     /* Initialize the thread properties */
136     memset( p_dec->p_sys, 0, sizeof(decoder_sys_t) );
137     p_dec->p_sys->p_pes      = NULL;
138     p_dec->p_sys->p_vout     = NULL;
139     p_dec->p_sys->p_mpeg2dec = NULL;
140     p_dec->p_sys->p_synchro  = NULL;
141     p_dec->p_sys->p_info     = NULL;
142     p_dec->p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
143     p_dec->p_sys->i_current_pts  = 0;
144     p_dec->p_sys->i_previous_pts = 0;
145     p_dec->p_sys->i_period_remainder = 0;
146     p_dec->p_sys->p_picture_to_destroy = NULL;
147     p_dec->p_sys->b_garbage_pic = 0;
148     p_dec->p_sys->b_slice_i  = 0;
149     p_dec->p_sys->b_skip     = 0;
150
151     /* Initialize decoder */
152     p_dec->p_sys->p_mpeg2dec = mpeg2_init();
153     if( p_dec->p_sys->p_mpeg2dec == NULL)
154     {
155         msg_Err( p_dec, "mpeg2_init() failed" );
156         free( p_dec->p_sys );
157         return VLC_EGENERIC;
158     }
159
160     p_dec->p_sys->p_info = mpeg2_info( p_dec->p_sys->p_mpeg2dec );
161
162     return VLC_SUCCESS;
163 }
164
165 /*****************************************************************************
166  * RunDecoder: the libmpeg2 decoder
167  *****************************************************************************/
168 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
169 {
170     decoder_sys_t   *p_sys = p_dec->p_sys;
171     mpeg2_state_t   state;
172     picture_t       *p_pic;
173     int             i_aspect;
174
175     vlc_bool_t      b_need_more_data = VLC_FALSE;
176
177     while( 1 )
178     {
179         state = mpeg2_parse( p_sys->p_mpeg2dec );
180
181         switch( state )
182         {
183         case STATE_BUFFER:
184                 if( !p_block->i_buffer || b_need_more_data )
185                 {
186                     block_Release( p_block );
187                     return VLC_SUCCESS;
188                 }
189
190 #if 0
191                 if( p_pes->b_discontinuity && p_sys->p_synchro 
192                      && p_sys->p_info->sequence->width != (unsigned)-1 )
193                 {
194                     vout_SynchroReset( p_sys->p_synchro );
195                     if ( p_sys->p_info->current_fbuf != NULL
196                           && p_sys->p_info->current_fbuf->id != NULL )
197                     {
198                         p_sys->b_garbage_pic = 1;
199                         p_pic = p_sys->p_info->current_fbuf->id;
200                     }
201                     else
202                     {
203                         uint8_t *buf[3];
204                         buf[0] = buf[1] = buf[2] = NULL;
205                         if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
206                             break;
207                         mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
208                     }
209                     p_sys->p_picture_to_destroy = p_pic;
210
211                     memset( p_pic->p[0].p_pixels, 0,
212                             p_sys->p_info->sequence->width
213                              * p_sys->p_info->sequence->height );
214                     memset( p_pic->p[1].p_pixels, 0x80,
215                             p_sys->p_info->sequence->width
216                              * p_sys->p_info->sequence->height / 4 );
217                     memset( p_pic->p[2].p_pixels, 0x80,
218                             p_sys->p_info->sequence->width
219                              * p_sys->p_info->sequence->height / 4 );
220
221                     if ( p_sys->b_slice_i )
222                     {
223                         vout_SynchroNewPicture( p_sys->p_synchro,
224                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
225                         vout_SynchroDecode( p_sys->p_synchro );
226                         vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
227                     }
228                 }
229 #endif
230
231             if( p_block->i_pts )
232             {
233                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
234                 p_sys->i_previous_pts = p_sys->i_current_pts;
235                 p_sys->i_current_pts = p_block->i_pts;
236             }
237
238             p_sys->i_current_rate = DEFAULT_RATE;//p_pes->i_rate;
239
240             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
241                           p_block->p_buffer + p_block->i_buffer );
242
243             b_need_more_data = VLC_TRUE;
244             break;
245
246         case STATE_SEQUENCE:
247         {
248             /* Initialize video output */
249             uint8_t *buf[3];
250             buf[0] = buf[1] = buf[2] = NULL;
251
252             /* Check whether the input gives a particular aspect ratio */
253             if( p_dec->p_fifo->p_demux_data
254                 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
255             {
256                 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
257                 switch( i_aspect )
258                 {
259                 case AR_3_4_PICTURE:
260                     i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
261                     break;
262                 case AR_16_9_PICTURE:
263                     i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
264                     break;
265                 case AR_221_1_PICTURE:
266                     i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
267                     break;
268                 case AR_SQUARE_PICTURE:
269                 default:
270                     i_aspect = VOUT_ASPECT_FACTOR *
271                                    p_sys->p_info->sequence->width /
272                                    p_sys->p_info->sequence->height;
273                     break;
274                 }
275             }
276             else
277             {
278                 /* Use the value provided in the MPEG sequence header */
279                 i_aspect = ((uint64_t)p_sys->p_info->sequence->display_width) *
280                     p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
281                     p_sys->p_info->sequence->display_height /
282                     p_sys->p_info->sequence->pixel_height;
283             }
284
285             if ( p_dec->p_sys->p_vout != NULL )
286             {
287               int i_pic;
288                 /* Temporary hack to free the pictures in use by libmpeg2 */
289                 for ( i_pic = 0; i_pic < p_dec->p_sys->p_vout->render.i_pictures; i_pic++ )
290                 {
291                     if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
292                           RESERVED_PICTURE )
293                         vout_DestroyPicture( p_dec->p_sys->p_vout,
294                                          p_dec->p_sys->p_vout->render.pp_picture[i_pic] );
295                     if( p_dec->p_sys->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
296                         vout_UnlinkPicture( p_dec->p_sys->p_vout,
297                                          p_dec->p_sys->p_vout->render.pp_picture[i_pic] );
298                 }
299             }
300
301             p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
302                                           p_sys->p_info->sequence->width,
303                                           p_sys->p_info->sequence->height,
304                                           VLC_FOURCC('Y','V','1','2'),
305                                           i_aspect );
306
307             if(p_sys->p_vout == NULL )
308             {
309                 msg_Err( p_dec, "cannot create vout" );
310                 block_Release( p_block );
311                 return -1;
312             } 
313
314             msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
315                      p_sys->p_info->sequence->width,
316                      p_sys->p_info->sequence->height, i_aspect,
317                      (uint32_t)((u64)1001000000 * 27 /
318                      p_sys->p_info->sequence->frame_period / 1001),
319                      (uint32_t)((u64)1001000000 * 27 /
320                      p_sys->p_info->sequence->frame_period % 1001) );
321
322             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
323
324             /* Set the first 2 reference frames */
325             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
326
327             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
328             memset( p_pic->p[0].p_pixels, 0,
329                     p_sys->p_info->sequence->width
330                      * p_sys->p_info->sequence->height );
331             memset( p_pic->p[1].p_pixels, 0x80,
332                     p_sys->p_info->sequence->width
333                      * p_sys->p_info->sequence->height / 4 );
334             memset( p_pic->p[2].p_pixels, 0x80,
335                     p_sys->p_info->sequence->width
336                      * p_sys->p_info->sequence->height / 4 );
337             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
338             /* This picture will never go through display_picture. */
339             vout_DatePicture( p_sys->p_vout, p_pic, 0 );
340             vout_DisplayPicture( p_sys->p_vout, p_pic );
341             /* For some reason, libmpeg2 will put this pic twice in
342              * discard_picture. This can be considered a bug in libmpeg2. */
343             vout_LinkPicture( p_sys->p_vout, p_pic );
344
345             if ( p_sys->p_synchro )
346             {
347                 vout_SynchroRelease( p_sys->p_synchro );
348             }
349             p_sys->p_synchro = vout_SynchroInit( p_dec, p_sys->p_vout,
350                 (uint32_t)((uint64_t)1001000000 * 27 /
351                 p_sys->p_info->sequence->frame_period) );
352             p_sys->b_after_sequence_header = 1;
353         }
354         break;
355
356         case STATE_PICTURE_2ND:
357             vout_SynchroNewPicture( p_sys->p_synchro,
358                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
359                 p_sys->p_info->current_picture->nb_fields,
360                 0, 0,
361                 p_sys->i_current_rate );
362
363             if ( p_sys->b_skip )
364             {
365                 vout_SynchroTrash( p_sys->p_synchro );
366             }
367             else
368             {
369                 vout_SynchroDecode( p_sys->p_synchro );
370             }
371             break;
372
373         case STATE_PICTURE:
374         {
375             uint8_t *buf[3];
376             buf[0] = buf[1] = buf[2] = NULL;
377
378             if ( p_sys->b_after_sequence_header
379                   && ((p_sys->p_info->current_picture->flags
380                         & PIC_MASK_CODING_TYPE)
381                        == PIC_FLAG_CODING_TYPE_P) )
382             {
383                 /* Intra-slice refresh. Simulate a blank I picture. */
384                 msg_Dbg( p_dec, "intra-slice refresh stream" );
385                 vout_SynchroNewPicture( p_sys->p_synchro,
386                     I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
387                 vout_SynchroDecode( p_sys->p_synchro );
388                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
389                 p_sys->b_slice_i = 1;
390             }
391             p_sys->b_after_sequence_header = 0;
392
393             vout_SynchroNewPicture( p_sys->p_synchro,
394                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
395                 p_sys->p_info->current_picture->nb_fields,
396                 (p_sys->p_info->current_picture->flags & PIC_FLAG_PTS) ?
397                     ( ( p_sys->p_info->current_picture->pts ==
398                         (uint32_t)p_sys->i_current_pts ) ?
399                       p_sys->i_current_pts : p_sys->i_previous_pts ) : 0,
400                 0, p_sys->i_current_rate );
401
402             if ( !(p_sys->b_slice_i
403                    && ((p_sys->p_info->current_picture->flags
404                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
405                    && !vout_SynchroChoose( p_sys->p_synchro,
406                               p_sys->p_info->current_picture->flags
407                                 & PIC_MASK_CODING_TYPE ) )
408             {
409                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
410                 p_sys->b_skip = 1;
411                 vout_SynchroTrash( p_sys->p_synchro );
412                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
413             }
414             else
415             {
416                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
417                 p_sys->b_skip = 0;
418                 vout_SynchroDecode( p_sys->p_synchro );
419                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
420                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
421             }
422         }
423         break;
424
425         case STATE_END:
426         case STATE_SLICE:
427             if( p_sys->p_info->display_fbuf
428                 && p_sys->p_info->display_fbuf->id )
429             {
430                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
431
432                 vout_SynchroEnd( p_sys->p_synchro,
433                             p_sys->p_info->display_picture->flags
434                              & PIC_MASK_CODING_TYPE,
435                             p_sys->b_garbage_pic );
436                 p_sys->b_garbage_pic = 0;
437                 vout_DisplayPicture( p_sys->p_vout, p_pic );
438
439                 if ( p_sys->p_picture_to_destroy != p_pic )
440                 {
441                     vout_DatePicture( p_sys->p_vout, p_pic,
442                         vout_SynchroDate( p_sys->p_synchro ) );
443                 }
444                 else
445                 {
446                     p_sys->p_picture_to_destroy = NULL;
447                     vout_DatePicture( p_sys->p_vout, p_pic, 0 );
448                 }
449             }
450
451             if( p_sys->p_info->discard_fbuf &&
452                 p_sys->p_info->discard_fbuf->id )
453             {
454                 p_pic = (picture_t *)p_sys->p_info->discard_fbuf->id;
455                 vout_UnlinkPicture( p_sys->p_vout, p_pic );
456             }
457             break;
458
459         case STATE_INVALID:
460         {
461             uint8_t *buf[3];
462             buf[0] = buf[1] = buf[2] = NULL;
463
464             msg_Warn( p_dec, "invalid picture encountered" );
465             if ( ( p_sys->p_info->current_picture == NULL ) || 
466                ( ( p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
467                   != B_CODING_TYPE ) )
468             {
469                 vout_SynchroReset( p_sys->p_synchro );
470             }
471             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
472             p_sys->b_skip = 1;
473
474             if( p_sys->p_info->current_fbuf &&
475                 p_sys->p_info->current_fbuf->id )
476             {
477                 p_sys->b_garbage_pic = 1;
478                 p_pic = p_sys->p_info->current_fbuf->id;
479             }
480             else
481             {
482                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
483                     break;
484                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
485             }
486             p_sys->p_picture_to_destroy = p_pic;
487
488             memset( p_pic->p[0].p_pixels, 0,
489                     p_sys->p_info->sequence->width
490                      * p_sys->p_info->sequence->height );
491             memset( p_pic->p[1].p_pixels, 0x80,
492                     p_sys->p_info->sequence->width
493                      * p_sys->p_info->sequence->height / 4 );
494             memset( p_pic->p[2].p_pixels, 0x80,
495                     p_sys->p_info->sequence->width
496                      * p_sys->p_info->sequence->height / 4 );
497
498             if ( p_sys->b_slice_i )
499             {
500                 vout_SynchroNewPicture( p_sys->p_synchro,
501                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
502                 vout_SynchroDecode( p_sys->p_synchro );
503                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
504             }
505             break;
506         }
507
508         default:
509             break;
510         }
511     }
512
513     block_Release( p_block );
514     return VLC_EGENERIC;
515 }
516
517 /*****************************************************************************
518  * EndDecoder: libmpeg2 decoder destruction
519  *****************************************************************************/
520 static int EndDecoder( decoder_t * p_dec )
521 {
522     decoder_sys_t *p_sys = p_dec->p_sys;
523
524     if( p_sys )
525     {
526         int i_pic;
527
528         if( p_sys->p_synchro )
529             vout_SynchroRelease( p_sys->p_synchro );
530
531         if( p_sys->p_vout )
532         {
533             /* Temporary hack to free the pictures in use by libmpeg2 */
534             for( i_pic = 0; i_pic < p_sys->p_vout->render.i_pictures; i_pic++ )
535             {
536                 if( p_sys->p_vout->render.pp_picture[i_pic]->i_status ==
537                       RESERVED_PICTURE )
538                     vout_DestroyPicture( p_sys->p_vout,
539                                      p_sys->p_vout->render.pp_picture[i_pic] );
540                 if( p_sys->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
541                     vout_UnlinkPicture( p_sys->p_vout,
542                                      p_sys->p_vout->render.pp_picture[i_pic] );
543             }
544
545             vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
546         }
547
548         if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
549
550         free( p_sys );
551     }
552
553     return VLC_SUCCESS;
554 }
555
556 /*****************************************************************************
557  * GetNewPicture: Get a new picture from the vout and set the buf struct
558  *****************************************************************************/
559 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
560 {
561     decoder_sys_t *p_sys = p_dec->p_sys;
562
563     picture_t *p_pic;
564     vlc_bool_t b_progressive = p_sys->p_info->current_picture != NULL ?
565         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME :
566         1;
567     vlc_bool_t b_top_field_first = p_sys->p_info->current_picture != NULL ?
568         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST :
569         1;
570     unsigned int i_nb_fields = p_sys->p_info->current_picture != NULL ?
571         p_sys->p_info->current_picture->nb_fields : 2;
572
573     /* Get a new picture */
574     while( !(p_pic = vout_CreatePicture( p_sys->p_vout,
575         b_progressive, b_top_field_first, i_nb_fields )) )
576     {
577         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
578             break;
579
580         msleep( VOUT_OUTMEM_SLEEP );
581     }
582     if( p_pic == NULL )
583         return NULL;
584     vout_LinkPicture( p_sys->p_vout, p_pic );
585
586     pp_buf[0] = p_pic->p[0].p_pixels;
587     pp_buf[1] = p_pic->p[1].p_pixels;
588     pp_buf[2] = p_pic->p[2].p_pixels;
589
590     return p_pic;
591 }