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