]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* modules/codec/libmpeg2.c: modified last change to use a 4:3 aspect ratio when libmp...
[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     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->i_flags&BLOCK_FLAG_DISCONTINUITY) &&
214                 p_sys->p_synchro &&
215                 p_sys->p_info->sequence &&
216                 p_sys->p_info->sequence->width != (unsigned)-1 )
217             {
218                 vout_SynchroReset( p_sys->p_synchro );
219                 if( p_sys->p_info->current_fbuf != NULL
220                     && p_sys->p_info->current_fbuf->id != NULL )
221                 {
222                     p_sys->b_garbage_pic = 1;
223                     p_pic = p_sys->p_info->current_fbuf->id;
224                 }
225                 else
226                 {
227                     uint8_t *buf[3];
228                     buf[0] = buf[1] = buf[2] = NULL;
229                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
230                         break;
231                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
232                 }
233                 p_sys->p_picture_to_destroy = p_pic;
234
235                 if ( p_sys->b_slice_i )
236                 {
237                     vout_SynchroNewPicture( p_sys->p_synchro,
238                         I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
239                     vout_SynchroDecode( p_sys->p_synchro );
240                     vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
241                 }
242             }
243
244             if( p_block->i_pts )
245             {
246 #ifdef PIC_FLAG_PTS
247                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
248
249 #else /* New interface */
250                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
251                                    (uint32_t)p_block->i_pts, 0/*dts*/ );
252 #endif
253                 p_sys->i_previous_pts = p_sys->i_current_pts;
254                 p_sys->i_current_pts = p_block->i_pts;
255             }
256
257             p_sys->i_current_rate = p_block->i_rate;
258
259             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
260                           p_block->p_buffer + p_block->i_buffer );
261
262             p_block->i_buffer = 0;
263             break;
264
265         case STATE_SEQUENCE:
266         {
267             /* Initialize video output */
268             uint8_t *buf[3];
269             buf[0] = buf[1] = buf[2] = NULL;
270
271             /* Check whether the input gave a particular aspect ratio */
272             if( p_dec->fmt_in.video.i_aspect )
273             {
274                 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
275                 if( p_sys->i_aspect <= AR_221_1_PICTURE )
276                 switch( p_sys->i_aspect )
277                 {
278                 case AR_3_4_PICTURE:
279                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
280                     break;
281                 case AR_16_9_PICTURE:
282                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
283                     break;
284                 case AR_221_1_PICTURE:
285                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
286                     break;
287                 case AR_SQUARE_PICTURE:
288                     p_sys->i_aspect = VOUT_ASPECT_FACTOR *
289                                    p_sys->p_info->sequence->width /
290                                    p_sys->p_info->sequence->height;
291                     break;
292                 }
293             }
294             else
295             {
296                 /* Use the value provided in the MPEG sequence header */
297                 if( p_sys->p_info->sequence->pixel_height > 0 )
298                 {
299                     p_sys->i_aspect =
300                         ((uint64_t)p_sys->p_info->sequence->display_width) *
301                         p_sys->p_info->sequence->pixel_width *
302                         VOUT_ASPECT_FACTOR /
303                         p_sys->p_info->sequence->display_height /
304                         p_sys->p_info->sequence->pixel_height;
305                 }
306                 else
307                 {
308                     /* Invalid aspect, assume 4:3.
309                      * This shouldn't happen and if it does it is a bug
310                      * in libmpeg2 (likely triggered by an invalid stream) */
311                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
312                 }
313             }
314
315             msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
316                      p_sys->p_info->sequence->width,
317                      p_sys->p_info->sequence->height, p_sys->i_aspect,
318                      (uint32_t)((uint64_t)1001000000 * 27 /
319                          p_sys->p_info->sequence->frame_period / 1001),
320                      (uint32_t)((uint64_t)1001000000 * 27 /
321                          p_sys->p_info->sequence->frame_period % 1001) );
322
323             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
324
325             /* Set the first 2 reference frames */
326             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
327
328             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
329             {
330                 block_Release( p_block );
331                 return NULL;
332             }
333
334             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
335
336             /* This picture will never go through display_picture. */
337             p_pic->date = 0;
338
339             /* For some reason, libmpeg2 will put this pic twice in
340              * discard_picture. This can be considered a bug in libmpeg2. */
341             p_dec->pf_picture_link( p_dec, p_pic );
342
343             if( p_sys->p_synchro )
344             {
345                 vout_SynchroRelease( p_sys->p_synchro );
346             }
347             p_sys->p_synchro = vout_SynchroInit( p_dec,
348                 (uint32_t)((uint64_t)1001000000 * 27 /
349                 p_sys->p_info->sequence->frame_period) );
350             p_sys->b_after_sequence_header = 1;
351         }
352         break;
353
354         case STATE_PICTURE_2ND:
355             vout_SynchroNewPicture( p_sys->p_synchro,
356                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
357                 p_sys->p_info->current_picture->nb_fields,
358                 0, 0, p_sys->i_current_rate );
359
360             if( p_sys->b_skip )
361             {
362                 vout_SynchroTrash( p_sys->p_synchro );
363             }
364             else
365             {
366                 vout_SynchroDecode( p_sys->p_synchro );
367             }
368             break;
369
370         case STATE_PICTURE:
371         {
372             uint8_t *buf[3];
373             mtime_t i_pts;
374             buf[0] = buf[1] = buf[2] = NULL;
375
376             if ( p_sys->b_after_sequence_header &&
377                  ((p_sys->p_info->current_picture->flags &
378                        PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
379             {
380                 /* Intra-slice refresh. Simulate a blank I picture. */
381                 msg_Dbg( p_dec, "intra-slice refresh stream" );
382                 vout_SynchroNewPicture( p_sys->p_synchro,
383                     I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
384                 vout_SynchroDecode( p_sys->p_synchro );
385                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
386                 p_sys->b_slice_i = 1;
387             }
388             p_sys->b_after_sequence_header = 0;
389
390 #ifdef PIC_FLAG_PTS
391             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
392                 ( ( p_sys->p_info->current_picture->pts ==
393                     (uint32_t)p_sys->i_current_pts ) ?
394                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
395
396 #else /* New interface */
397
398             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
399                 ( ( p_sys->p_info->current_picture->tag ==
400                     (uint32_t)p_sys->i_current_pts ) ?
401                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
402 #endif
403
404             /* Hack to handle demuxers which only have DTS timestamps */
405             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
406             {
407                 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
408                     (p_sys->p_info->current_picture->flags &
409                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
410                 {
411                     i_pts = p_block->i_dts;
412                 }
413             }
414             p_block->i_pts = p_block->i_dts = 0;
415             /* End hack */
416
417             vout_SynchroNewPicture( p_sys->p_synchro,
418                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
419                 p_sys->p_info->current_picture->nb_fields, i_pts,
420                 0, p_sys->i_current_rate );
421
422             if ( !(p_sys->b_slice_i
423                    && ((p_sys->p_info->current_picture->flags
424                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
425                    && !vout_SynchroChoose( p_sys->p_synchro,
426                               p_sys->p_info->current_picture->flags
427                                 & PIC_MASK_CODING_TYPE,
428                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
429             {
430                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
431                 p_sys->b_skip = 1;
432                 vout_SynchroTrash( p_sys->p_synchro );
433                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
434             }
435             else
436             {
437                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
438                 p_sys->b_skip = 0;
439                 vout_SynchroDecode( p_sys->p_synchro );
440
441                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
442                 {
443                     block_Release( p_block );
444                     return NULL;
445                 }
446
447                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
448             }
449         }
450         break;
451
452         case STATE_END:
453         case STATE_SLICE:
454             p_pic = NULL;
455             if( p_sys->p_info->display_fbuf
456                 && p_sys->p_info->display_fbuf->id )
457             {
458                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
459
460                 vout_SynchroEnd( p_sys->p_synchro,
461                             p_sys->p_info->display_picture->flags
462                              & PIC_MASK_CODING_TYPE,
463                             p_sys->b_garbage_pic );
464                 p_sys->b_garbage_pic = 0;
465
466                 if ( p_sys->p_picture_to_destroy != p_pic )
467                 {
468                     p_pic->date = vout_SynchroDate( p_sys->p_synchro );
469                 }
470                 else
471                 {
472                     p_sys->p_picture_to_destroy = NULL;
473                     p_pic->date = 0;
474                 }
475             }
476
477             if( p_sys->p_info->discard_fbuf &&
478                 p_sys->p_info->discard_fbuf->id )
479             {
480                 p_dec->pf_picture_unlink( p_dec,
481                                           p_sys->p_info->discard_fbuf->id );
482             }
483
484             if( p_pic ) return p_pic;
485
486             break;
487
488         case STATE_INVALID:
489         {
490             uint8_t *buf[3];
491             buf[0] = buf[1] = buf[2] = NULL;
492
493             msg_Warn( p_dec, "invalid picture encountered" );
494             if ( ( p_sys->p_info->current_picture == NULL ) ||
495                ( ( p_sys->p_info->current_picture->flags &
496                    PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
497             {
498                 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
499             }
500             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
501             p_sys->b_skip = 1;
502
503             if( p_sys->p_info->current_fbuf &&
504                 p_sys->p_info->current_fbuf->id )
505             {
506                 p_sys->b_garbage_pic = 1;
507                 p_pic = p_sys->p_info->current_fbuf->id;
508             }
509             else if( !p_sys->p_info->sequence )
510             {
511                 break;
512             }
513             else
514             {
515                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
516                     break;
517                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
518             }
519             p_sys->p_picture_to_destroy = p_pic;
520
521             memset( p_pic->p[0].p_pixels, 0,
522                     p_sys->p_info->sequence->width
523                      * p_sys->p_info->sequence->height );
524             memset( p_pic->p[1].p_pixels, 0x80,
525                     p_sys->p_info->sequence->width
526                      * p_sys->p_info->sequence->height / 4 );
527             memset( p_pic->p[2].p_pixels, 0x80,
528                     p_sys->p_info->sequence->width
529                      * p_sys->p_info->sequence->height / 4 );
530
531             if( p_sys->b_slice_i )
532             {
533                 vout_SynchroNewPicture( p_sys->p_synchro,
534                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
535                 vout_SynchroDecode( p_sys->p_synchro );
536                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
537             }
538             break;
539         }
540
541         default:
542             break;
543         }
544     }
545
546     /* Never reached */
547     return NULL;
548 }
549
550 /*****************************************************************************
551  * CloseDecoder: libmpeg2 decoder destruction
552  *****************************************************************************/
553 static void CloseDecoder( vlc_object_t *p_this )
554 {
555     decoder_t *p_dec = (decoder_t *)p_this;
556     decoder_sys_t *p_sys = p_dec->p_sys;
557
558     if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
559
560     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
561
562     free( p_sys );
563 }
564
565 /*****************************************************************************
566  * GetNewPicture: Get a new picture from the vout and set the buf struct
567  *****************************************************************************/
568 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
569 {
570     decoder_sys_t *p_sys = p_dec->p_sys;
571     picture_t *p_pic;
572
573     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
574     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
575     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
576
577     p_dec->fmt_out.i_codec =
578         ( p_sys->p_info->sequence->chroma_height <
579           p_sys->p_info->sequence->height ) ?
580         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
581
582     /* Get a new picture */
583     p_pic = p_dec->pf_vout_buffer_new( p_dec );
584
585     if( p_pic == NULL ) return NULL;
586
587     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
588         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
589     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
590         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
591     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
592         p_sys->p_info->current_picture->nb_fields : 2;
593
594     p_dec->pf_picture_link( p_dec, p_pic );
595
596     pp_buf[0] = p_pic->p[0].p_pixels;
597     pp_buf[1] = p_pic->p[1].p_pixels;
598     pp_buf[2] = p_pic->p[2].p_pixels;
599
600     return p_pic;
601 }