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