]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* modules/codec/libmpeg2.c: fixed problems with still frames in DVD menus.
[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$
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     mtime_t          i_pts;
58     mtime_t          i_previous_pts;
59     mtime_t          i_current_pts;
60     int              i_current_rate;
61     picture_t *      p_picture_to_destroy;
62     vlc_bool_t       b_garbage_pic;
63     vlc_bool_t       b_after_sequence_header; /* is it the next frame after
64                                                * the sequence header ?    */
65     vlc_bool_t       b_slice_i;             /* intra-slice refresh stream */
66
67     /*
68      * Output properties
69      */
70     vout_synchro_t *p_synchro;
71     int            i_aspect;
72     mtime_t        i_last_frame_pts;
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_mpeg2dec = NULL;
127     p_sys->p_synchro  = NULL;
128     p_sys->p_info     = NULL;
129     p_sys->i_pts      = mdate() + DEFAULT_PTS_DELAY;
130     p_sys->i_current_pts  = 0;
131     p_sys->i_previous_pts = 0;
132     p_sys->p_picture_to_destroy = NULL;
133     p_sys->b_garbage_pic = 0;
134     p_sys->b_slice_i  = 0;
135     p_sys->b_skip     = 0;
136
137 #if defined( __i386__ )
138     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMX )
139     {
140         i_accel |= MPEG2_ACCEL_X86_MMX;
141     }
142
143     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_3DNOW )
144     {
145         i_accel |= MPEG2_ACCEL_X86_3DNOW;
146     }
147
148     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT )
149     {
150         i_accel |= MPEG2_ACCEL_X86_MMXEXT;
151     }
152
153 #elif defined( __powerpc__ ) || defined( SYS_DARWIN )
154     if( p_dec->p_libvlc->i_cpu & CPU_CAPABILITY_ALTIVEC )
155     {
156         i_accel |= MPEG2_ACCEL_PPC_ALTIVEC;
157     }
158
159 #else
160     /* If we do not know this CPU, trust libmpeg2's feature detection */
161     i_accel = MPEG2_ACCEL_DETECT;
162
163 #endif
164
165     /* Set CPU acceleration features */
166     mpeg2_accel( i_accel );
167
168     /* Initialize decoder */
169     p_sys->p_mpeg2dec = mpeg2_init();
170     if( p_sys->p_mpeg2dec == NULL)
171     {
172         msg_Err( p_dec, "mpeg2_init() failed" );
173         free( p_sys );
174         return VLC_EGENERIC;
175     }
176
177     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
178
179     p_dec->pf_decode_video = DecodeBlock;
180
181     return VLC_SUCCESS;
182 }
183
184 /*****************************************************************************
185  * RunDecoder: the libmpeg2 decoder
186  *****************************************************************************/
187 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
188 {
189     decoder_sys_t   *p_sys = p_dec->p_sys;
190     mpeg2_state_t   state;
191     picture_t       *p_pic;
192
193     block_t *p_block;
194
195     if( !pp_block || !*pp_block ) return NULL;
196
197     p_block = *pp_block;
198
199     while( 1 )
200     {
201         state = mpeg2_parse( p_sys->p_mpeg2dec );
202
203         switch( state )
204         {
205         case STATE_BUFFER:
206             if( !p_block->i_buffer )
207             {
208                 block_Release( p_block );
209                 return NULL;
210             }
211
212             if( (p_block->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
213                 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                 if( p_sys->p_info->sequence->pixel_height > 0 )
297                 {
298                     p_sys->i_aspect =
299                         ((uint64_t)p_sys->p_info->sequence->display_width) *
300                         p_sys->p_info->sequence->pixel_width *
301                         VOUT_ASPECT_FACTOR /
302                         p_sys->p_info->sequence->display_height /
303                         p_sys->p_info->sequence->pixel_height;
304                 }
305                 else
306                 {
307                     /* Invalid aspect, assume 4:3.
308                      * This shouldn't happen and if it does it is a bug
309                      * in libmpeg2 (likely triggered by an invalid stream) */
310                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
311                 }
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, p_sys->i_aspect,
317                      (uint32_t)((uint64_t)1001000000 * 27 /
318                          p_sys->p_info->sequence->frame_period / 1001),
319                      (uint32_t)((uint64_t)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 )
328             {
329                 block_Release( p_block );
330                 return NULL;
331             }
332
333             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
334
335             /* This picture will never go through display_picture. */
336             p_pic->date = 0;
337
338             /* For some reason, libmpeg2 will put this pic twice in
339              * discard_picture. This can be considered a bug in libmpeg2. */
340             p_dec->pf_picture_link( p_dec, p_pic );
341
342             if( p_sys->p_synchro )
343             {
344                 vout_SynchroRelease( p_sys->p_synchro );
345             }
346             p_sys->p_synchro = vout_SynchroInit( p_dec,
347                 (uint32_t)((uint64_t)1001000000 * 27 /
348                 p_sys->p_info->sequence->frame_period) );
349             p_sys->b_after_sequence_header = 1;
350         }
351         break;
352
353         case STATE_PICTURE_2ND:
354             vout_SynchroNewPicture( p_sys->p_synchro,
355                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
356                 p_sys->p_info->current_picture->nb_fields,
357                 0, 0, p_sys->i_current_rate );
358
359             if( p_sys->b_skip )
360             {
361                 vout_SynchroTrash( p_sys->p_synchro );
362             }
363             else
364             {
365                 vout_SynchroDecode( p_sys->p_synchro );
366             }
367             break;
368
369         case STATE_PICTURE:
370         {
371             uint8_t *buf[3];
372             mtime_t i_pts;
373             buf[0] = buf[1] = buf[2] = NULL;
374
375             if ( p_sys->b_after_sequence_header &&
376                  ((p_sys->p_info->current_picture->flags &
377                        PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
378             {
379                 /* Intra-slice refresh. Simulate a blank I picture. */
380                 msg_Dbg( p_dec, "intra-slice refresh stream" );
381                 vout_SynchroNewPicture( p_sys->p_synchro,
382                     I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
383                 vout_SynchroDecode( p_sys->p_synchro );
384                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
385                 p_sys->b_slice_i = 1;
386             }
387             p_sys->b_after_sequence_header = 0;
388
389 #ifdef PIC_FLAG_PTS
390             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
391                 ( ( p_sys->p_info->current_picture->pts ==
392                     (uint32_t)p_sys->i_current_pts ) ?
393                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
394
395 #else /* New interface */
396
397             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
398                 ( ( p_sys->p_info->current_picture->tag ==
399                     (uint32_t)p_sys->i_current_pts ) ?
400                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
401 #endif
402
403             /* Hack to handle demuxers which only have DTS timestamps */
404             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
405             {
406                 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
407                     (p_sys->p_info->current_picture->flags &
408                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
409                 {
410                     i_pts = p_block->i_dts;
411                 }
412             }
413             p_block->i_pts = p_block->i_dts = 0;
414             /* End hack */
415
416             vout_SynchroNewPicture( p_sys->p_synchro,
417                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
418                 p_sys->p_info->current_picture->nb_fields, i_pts,
419                 0, p_sys->i_current_rate );
420
421             if ( !(p_sys->b_slice_i
422                    && ((p_sys->p_info->current_picture->flags
423                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
424                    && !vout_SynchroChoose( p_sys->p_synchro,
425                               p_sys->p_info->current_picture->flags
426                                 & PIC_MASK_CODING_TYPE,
427                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
428             {
429                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
430                 p_sys->b_skip = 1;
431                 vout_SynchroTrash( p_sys->p_synchro );
432                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
433             }
434             else
435             {
436                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
437                 p_sys->b_skip = 0;
438                 vout_SynchroDecode( p_sys->p_synchro );
439
440                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
441                 {
442                     block_Release( p_block );
443                     return NULL;
444                 }
445
446                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
447             }
448         }
449         break;
450
451         case STATE_END:
452         case STATE_SLICE:
453             p_pic = NULL;
454             if( p_sys->p_info->display_fbuf
455                 && p_sys->p_info->display_fbuf->id )
456             {
457                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
458
459                 vout_SynchroEnd( p_sys->p_synchro,
460                             p_sys->p_info->display_picture->flags
461                              & PIC_MASK_CODING_TYPE,
462                             p_sys->b_garbage_pic );
463                 p_sys->b_garbage_pic = 0;
464
465                 if ( p_sys->p_picture_to_destroy != p_pic )
466                 {
467                     p_pic->date = vout_SynchroDate( p_sys->p_synchro );
468                 }
469                 else
470                 {
471                     p_sys->p_picture_to_destroy = NULL;
472                     p_pic->date = 0;
473                 }
474             }
475
476             if( p_sys->p_info->discard_fbuf &&
477                 p_sys->p_info->discard_fbuf->id )
478             {
479                 p_dec->pf_picture_unlink( p_dec,
480                                           p_sys->p_info->discard_fbuf->id );
481             }
482
483             /* For still frames */
484             if( state == STATE_END && p_pic ) p_pic->b_force = VLC_TRUE;
485
486             if( p_pic )
487             {
488                 /* Avoid frames with identical timestamps.
489                  * Especially needed for still frames in DVD menus. */
490                 if( p_sys->i_last_frame_pts == p_pic->date ) p_pic->date++;
491                 p_sys->i_last_frame_pts = p_pic->date;
492
493                 return p_pic;
494             }
495
496             break;
497
498         case STATE_INVALID:
499         {
500             uint8_t *buf[3];
501             buf[0] = buf[1] = buf[2] = NULL;
502
503             msg_Warn( p_dec, "invalid picture encountered" );
504             if ( ( p_sys->p_info->current_picture == NULL ) ||
505                ( ( p_sys->p_info->current_picture->flags &
506                    PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
507             {
508                 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
509             }
510             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
511             p_sys->b_skip = 1;
512
513             if( p_sys->p_info->current_fbuf &&
514                 p_sys->p_info->current_fbuf->id )
515             {
516                 p_sys->b_garbage_pic = 1;
517                 p_pic = p_sys->p_info->current_fbuf->id;
518             }
519             else if( !p_sys->p_info->sequence )
520             {
521                 break;
522             }
523             else
524             {
525                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
526                     break;
527                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
528             }
529             p_sys->p_picture_to_destroy = p_pic;
530
531             memset( p_pic->p[0].p_pixels, 0,
532                     p_sys->p_info->sequence->width
533                      * p_sys->p_info->sequence->height );
534             memset( p_pic->p[1].p_pixels, 0x80,
535                     p_sys->p_info->sequence->width
536                      * p_sys->p_info->sequence->height / 4 );
537             memset( p_pic->p[2].p_pixels, 0x80,
538                     p_sys->p_info->sequence->width
539                      * p_sys->p_info->sequence->height / 4 );
540
541             if( p_sys->b_slice_i )
542             {
543                 vout_SynchroNewPicture( p_sys->p_synchro,
544                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
545                 vout_SynchroDecode( p_sys->p_synchro );
546                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
547             }
548             break;
549         }
550
551         default:
552             break;
553         }
554     }
555
556     /* Never reached */
557     return NULL;
558 }
559
560 /*****************************************************************************
561  * CloseDecoder: libmpeg2 decoder destruction
562  *****************************************************************************/
563 static void CloseDecoder( vlc_object_t *p_this )
564 {
565     decoder_t *p_dec = (decoder_t *)p_this;
566     decoder_sys_t *p_sys = p_dec->p_sys;
567
568     if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
569
570     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
571
572     free( p_sys );
573 }
574
575 /*****************************************************************************
576  * GetNewPicture: Get a new picture from the vout and set the buf struct
577  *****************************************************************************/
578 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
579 {
580     decoder_sys_t *p_sys = p_dec->p_sys;
581     picture_t *p_pic;
582
583     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
584     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
585     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
586
587     p_dec->fmt_out.i_codec =
588         ( p_sys->p_info->sequence->chroma_height <
589           p_sys->p_info->sequence->height ) ?
590         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
591
592     /* Get a new picture */
593     p_pic = p_dec->pf_vout_buffer_new( p_dec );
594
595     if( p_pic == NULL ) return NULL;
596
597     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
598         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
599     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
600         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
601     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
602         p_sys->p_info->current_picture->nb_fields : 2;
603
604     p_dec->pf_picture_link( p_dec, p_pic );
605
606     pp_buf[0] = p_pic->p[0].p_pixels;
607     pp_buf[1] = p_pic->p[1].p_pixels;
608     pp_buf[2] = p_pic->p[2].p_pixels;
609
610     return p_pic;
611 }