]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
d040cc0be67a7bac3e57b5f40ebdaead6a5157c8
[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.42 2004/01/16 09:39:57 sam 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/decoder.h>
31
32 #include <mpeg2dec/mpeg2.h>
33
34 #include "vout_synchro.h"
35
36 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
37 #define AR_SQUARE_PICTURE       1                           /* square pixels */
38 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
39 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
40 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
41
42 /*****************************************************************************
43  * decoder_sys_t : libmpeg2 decoder descriptor
44  *****************************************************************************/
45 struct decoder_sys_t
46 {
47     /*
48      * libmpeg2 properties
49      */
50     mpeg2dec_t          *p_mpeg2dec;
51     const mpeg2_info_t  *p_info;
52     vlc_bool_t          b_skip;
53
54     /*
55      * Input properties
56      */
57     pes_packet_t     *p_pes;                  /* current PES we are decoding */
58     mtime_t          i_pts;
59     mtime_t          i_previous_pts;
60     mtime_t          i_current_pts;
61     int              i_current_rate;
62     picture_t *      p_picture_to_destroy;
63     vlc_bool_t       b_garbage_pic;
64     vlc_bool_t       b_after_sequence_header; /* is it the next frame after
65                                                * the sequence header ?    */
66     vlc_bool_t       b_slice_i;             /* intra-slice refresh stream */
67
68     /*
69      * Output properties
70      */
71     vout_synchro_t *p_synchro;
72     int i_aspect;
73
74 };
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 static int  OpenDecoder( vlc_object_t * );
80 static void CloseDecoder( vlc_object_t * );
81
82 static picture_t *DecodeBlock( decoder_t *, block_t ** );
83
84 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 vlc_module_begin();
90     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
91     set_capability( "decoder", 150 );
92     set_callbacks( OpenDecoder, CloseDecoder );
93     add_shortcut( "libmpeg2" );
94 vlc_module_end();
95
96 /*****************************************************************************
97  * OpenDecoder: probe the decoder and return score
98  *****************************************************************************/
99 static int OpenDecoder( vlc_object_t *p_this )
100 {
101     decoder_t *p_dec = (decoder_t*)p_this;
102     decoder_sys_t *p_sys;
103     uint32_t i_accel = 0;
104
105     if( p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','v') &&
106         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','1') &&
107         /* Pinnacle hardware-mpeg1 */
108         p_dec->fmt_in.i_codec != VLC_FOURCC('P','I','M','1') &&
109         /* ATI Video */
110         p_dec->fmt_in.i_codec != VLC_FOURCC('V','C','R','2') &&
111         p_dec->fmt_in.i_codec != VLC_FOURCC('m','p','g','2') )
112     {
113         return VLC_EGENERIC;
114     }
115
116     /* Allocate the memory needed to store the decoder's structure */
117     if( ( p_dec->p_sys = p_sys =
118           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
119     {
120         msg_Err( p_dec, "out of memory" );
121         return VLC_EGENERIC;
122     }
123
124     /* Initialize the thread properties */
125     memset( p_sys, 0, sizeof(decoder_sys_t) );
126     p_sys->p_pes      = NULL;
127     p_sys->p_mpeg2dec = NULL;
128     p_sys->p_synchro  = NULL;
129     p_sys->p_info     = NULL;
130     p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
131     p_sys->i_current_pts  = 0;
132     p_sys->i_previous_pts = 0;
133     p_sys->p_picture_to_destroy = NULL;
134     p_sys->b_garbage_pic = 0;
135     p_sys->b_slice_i  = 0;
136     p_sys->b_skip     = 0;
137
138 #if defined( __i386__ )
139     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
140     {
141         i_accel |= MPEG2_ACCEL_X86_MMX;
142     }
143
144     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
145     {
146         i_accel |= MPEG2_ACCEL_X86_3DNOW;
147     }
148
149     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
150     {
151         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
152     }
153
154 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
155     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
156     {
157         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
158     }
159
160 #else
161     /* If we do not know this CPU, trust libmpeg2's feature detection */
162     i_accel = MPEG2_ACCEL_DETECT;
163
164 #endif
165
166     /* Set CPU acceleration features */
167     mpeg2_accel( i_accel );
168
169     /* Initialize decoder */
170     p_sys->p_mpeg2dec = mpeg2_init();
171     if( p_sys->p_mpeg2dec == NULL)
172     {
173         msg_Err( p_dec, "mpeg2_init() failed" );
174         free( p_sys );
175         return VLC_EGENERIC;
176     }
177
178     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
179
180     p_dec->pf_decode_video = DecodeBlock;
181
182     return VLC_SUCCESS;
183 }
184
185 /*****************************************************************************
186  * RunDecoder: the libmpeg2 decoder
187  *****************************************************************************/
188 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
189 {
190     decoder_sys_t   *p_sys = p_dec->p_sys;
191     mpeg2_state_t   state;
192     picture_t       *p_pic;
193
194     block_t *p_block;
195
196     if( !pp_block || !*pp_block ) return NULL;
197
198     p_block = *pp_block;
199
200     while( 1 )
201     {
202         state = mpeg2_parse( p_sys->p_mpeg2dec );
203
204         switch( state )
205         {
206         case STATE_BUFFER:
207             if( !p_block->i_buffer )
208             {
209                 block_Release( p_block );
210                 return NULL;
211             }
212
213             if( p_block->b_discontinuity && p_sys->p_synchro &&
214                 p_sys->p_info->sequence &&
215                 p_sys->p_info->sequence->width != (unsigned)-1 )
216             {
217                 vout_SynchroReset( p_sys->p_synchro );
218                 if( p_sys->p_info->current_fbuf != NULL
219                     && p_sys->p_info->current_fbuf->id != NULL )
220                 {
221                     p_sys->b_garbage_pic = 1;
222                     p_pic = p_sys->p_info->current_fbuf->id;
223                 }
224                 else
225                 {
226                     uint8_t *buf[3];
227                     buf[0] = buf[1] = buf[2] = NULL;
228                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
229                         break;
230                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
231                 }
232                 p_sys->p_picture_to_destroy = p_pic;
233
234                 if ( p_sys->b_slice_i )
235                 {
236                     vout_SynchroNewPicture( p_sys->p_synchro,
237                         I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
238                     vout_SynchroDecode( p_sys->p_synchro );
239                     vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
240                 }
241             }
242
243             if( p_block->i_pts )
244             {
245 #ifdef PIC_FLAG_PTS
246                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
247
248 #else /* New interface */
249                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
250                                    (uint32_t)p_block->i_pts, 0/*dts*/ );
251 #endif
252                 p_sys->i_previous_pts = p_sys->i_current_pts;
253                 p_sys->i_current_pts = p_block->i_pts;
254             }
255
256             p_sys->i_current_rate = p_block->i_rate;
257
258             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
259                           p_block->p_buffer + p_block->i_buffer );
260
261             p_block->i_buffer = 0;
262             break;
263
264         case STATE_SEQUENCE:
265         {
266             /* Initialize video output */
267             uint8_t *buf[3];
268             buf[0] = buf[1] = buf[2] = NULL;
269
270             /* Check whether the input gave a particular aspect ratio */
271             if( p_dec->fmt_in.video.i_aspect )
272             {
273                 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
274                 if( p_sys->i_aspect <= AR_221_1_PICTURE )
275                 switch( p_sys->i_aspect )
276                 {
277                 case AR_3_4_PICTURE:
278                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
279                     break;
280                 case AR_16_9_PICTURE:
281                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
282                     break;
283                 case AR_221_1_PICTURE:
284                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
285                     break;
286                 case AR_SQUARE_PICTURE:
287                     p_sys->i_aspect = VOUT_ASPECT_FACTOR *
288                                    p_sys->p_info->sequence->width /
289                                    p_sys->p_info->sequence->height;
290                     break;
291                 }
292             }
293             else
294             {
295                 /* Use the value provided in the MPEG sequence header */
296                 p_sys->i_aspect =
297                     ((uint64_t)p_sys->p_info->sequence->display_width) *
298                     p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
299                     p_sys->p_info->sequence->display_height /
300                     p_sys->p_info->sequence->pixel_height;
301             }
302
303             msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
304                      p_sys->p_info->sequence->width,
305                      p_sys->p_info->sequence->height, p_sys->i_aspect,
306                      (uint32_t)((uint64_t)1001000000 * 27 /
307                          p_sys->p_info->sequence->frame_period / 1001),
308                      (uint32_t)((uint64_t)1001000000 * 27 /
309                          p_sys->p_info->sequence->frame_period % 1001) );
310
311             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
312
313             /* Set the first 2 reference frames */
314             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
315
316             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
317             {
318                 block_Release( p_block );
319                 return NULL;
320             }
321
322             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
323
324             /* This picture will never go through display_picture. */
325             p_pic->date = 0;
326
327             /* For some reason, libmpeg2 will put this pic twice in
328              * discard_picture. This can be considered a bug in libmpeg2. */
329             p_dec->pf_picture_link( p_dec, p_pic );
330
331             if( p_sys->p_synchro )
332             {
333                 vout_SynchroRelease( p_sys->p_synchro );
334             }
335             p_sys->p_synchro = vout_SynchroInit( p_dec,
336                 (uint32_t)((uint64_t)1001000000 * 27 /
337                 p_sys->p_info->sequence->frame_period) );
338             p_sys->b_after_sequence_header = 1;
339         }
340         break;
341
342         case STATE_PICTURE_2ND:
343             vout_SynchroNewPicture( p_sys->p_synchro,
344                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
345                 p_sys->p_info->current_picture->nb_fields,
346                 0, 0, p_sys->i_current_rate );
347
348             if( p_sys->b_skip )
349             {
350                 vout_SynchroTrash( p_sys->p_synchro );
351             }
352             else
353             {
354                 vout_SynchroDecode( p_sys->p_synchro );
355             }
356             break;
357
358         case STATE_PICTURE:
359         {
360             uint8_t *buf[3];
361             mtime_t i_pts;
362             buf[0] = buf[1] = buf[2] = NULL;
363
364             if ( p_sys->b_after_sequence_header &&
365                  ((p_sys->p_info->current_picture->flags &
366                        PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
367             {
368                 /* Intra-slice refresh. Simulate a blank I picture. */
369                 msg_Dbg( p_dec, "intra-slice refresh stream" );
370                 vout_SynchroNewPicture( p_sys->p_synchro,
371                     I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
372                 vout_SynchroDecode( p_sys->p_synchro );
373                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
374                 p_sys->b_slice_i = 1;
375             }
376             p_sys->b_after_sequence_header = 0;
377
378 #ifdef PIC_FLAG_PTS
379             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
380                 ( ( p_sys->p_info->current_picture->pts ==
381                     (uint32_t)p_sys->i_current_pts ) ?
382                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
383
384 #else /* New interface */
385
386             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
387                 ( ( p_sys->p_info->current_picture->tag ==
388                     (uint32_t)p_sys->i_current_pts ) ?
389                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
390 #endif
391
392             /* Hack to handle demuxers which only have DTS timestamps */
393             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
394             {
395                 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
396                     (p_sys->p_info->current_picture->flags &
397                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
398                 {
399                     i_pts = p_block->i_dts;
400                 }
401             }
402             p_block->i_pts = p_block->i_dts = 0;
403             /* End hack */
404
405             vout_SynchroNewPicture( p_sys->p_synchro,
406                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
407                 p_sys->p_info->current_picture->nb_fields, i_pts,
408                 0, p_sys->i_current_rate );
409
410             if ( !(p_sys->b_slice_i
411                    && ((p_sys->p_info->current_picture->flags
412                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
413                    && !vout_SynchroChoose( p_sys->p_synchro,
414                               p_sys->p_info->current_picture->flags
415                                 & PIC_MASK_CODING_TYPE,
416                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
417             {
418                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
419                 p_sys->b_skip = 1;
420                 vout_SynchroTrash( p_sys->p_synchro );
421                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
422             }
423             else
424             {
425                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
426                 p_sys->b_skip = 0;
427                 vout_SynchroDecode( p_sys->p_synchro );
428
429                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
430                 {
431                     block_Release( p_block );
432                     return NULL;
433                 }
434
435                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
436             }
437         }
438         break;
439
440         case STATE_END:
441         case STATE_SLICE:
442             p_pic = NULL;
443             if( p_sys->p_info->display_fbuf
444                 && p_sys->p_info->display_fbuf->id )
445             {
446                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
447
448                 vout_SynchroEnd( p_sys->p_synchro,
449                             p_sys->p_info->display_picture->flags
450                              & PIC_MASK_CODING_TYPE,
451                             p_sys->b_garbage_pic );
452                 p_sys->b_garbage_pic = 0;
453
454                 if ( p_sys->p_picture_to_destroy != p_pic )
455                 {
456                     p_pic->date = vout_SynchroDate( p_sys->p_synchro );
457                 }
458                 else
459                 {
460                     p_sys->p_picture_to_destroy = NULL;
461                     p_pic->date = 0;
462                 }
463             }
464
465             if( p_sys->p_info->discard_fbuf &&
466                 p_sys->p_info->discard_fbuf->id )
467             {
468                 p_dec->pf_picture_unlink( p_dec,
469                                           p_sys->p_info->discard_fbuf->id );
470             }
471
472             if( p_pic ) return p_pic;
473
474             break;
475
476         case STATE_INVALID:
477         {
478             uint8_t *buf[3];
479             buf[0] = buf[1] = buf[2] = NULL;
480
481             msg_Warn( p_dec, "invalid picture encountered" );
482             if ( ( p_sys->p_info->current_picture == NULL ) ||
483                ( ( p_sys->p_info->current_picture->flags &
484                    PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
485             {
486                 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
487             }
488             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
489             p_sys->b_skip = 1;
490
491             if( p_sys->p_info->current_fbuf &&
492                 p_sys->p_info->current_fbuf->id )
493             {
494                 p_sys->b_garbage_pic = 1;
495                 p_pic = p_sys->p_info->current_fbuf->id;
496             }
497             else if( !p_sys->p_info->sequence )
498             {
499                 break;
500             }
501             else
502             {
503                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
504                     break;
505                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
506             }
507             p_sys->p_picture_to_destroy = p_pic;
508
509             memset( p_pic->p[0].p_pixels, 0,
510                     p_sys->p_info->sequence->width
511                      * p_sys->p_info->sequence->height );
512             memset( p_pic->p[1].p_pixels, 0x80,
513                     p_sys->p_info->sequence->width
514                      * p_sys->p_info->sequence->height / 4 );
515             memset( p_pic->p[2].p_pixels, 0x80,
516                     p_sys->p_info->sequence->width
517                      * p_sys->p_info->sequence->height / 4 );
518
519             if( p_sys->b_slice_i )
520             {
521                 vout_SynchroNewPicture( p_sys->p_synchro,
522                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
523                 vout_SynchroDecode( p_sys->p_synchro );
524                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
525             }
526             break;
527         }
528
529         default:
530             break;
531         }
532     }
533
534     /* Never reached */
535     return NULL;
536 }
537
538 /*****************************************************************************
539  * CloseDecoder: libmpeg2 decoder destruction
540  *****************************************************************************/
541 static void CloseDecoder( vlc_object_t *p_this )
542 {
543     decoder_t *p_dec = (decoder_t *)p_this;
544     decoder_sys_t *p_sys = p_dec->p_sys;
545
546     if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
547
548     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
549
550     free( p_sys );
551 }
552
553 /*****************************************************************************
554  * GetNewPicture: Get a new picture from the vout and set the buf struct
555  *****************************************************************************/
556 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
557 {
558     decoder_sys_t *p_sys = p_dec->p_sys;
559     picture_t *p_pic;
560
561     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
562     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
563     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
564
565     p_dec->fmt_out.i_codec =
566         ( p_sys->p_info->sequence->chroma_height <
567           p_sys->p_info->sequence->height ) ?
568         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
569
570     /* Get a new picture */
571     p_pic = p_dec->pf_vout_buffer_new( p_dec );
572
573     if( p_pic == NULL ) return NULL;
574
575     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
576         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
577     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
578         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
579     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
580         p_sys->p_info->current_picture->nb_fields : 2;
581
582     p_dec->pf_picture_link( p_dec, p_pic );
583
584     pp_buf[0] = p_pic->p[0].p_pixels;
585     pp_buf[1] = p_pic->p[1].p_pixels;
586     pp_buf[2] = p_pic->p[2].p_pixels;
587
588     return p_pic;
589 }