]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* modules/codec/libmpeg2.c: compiles with the latest libmpeg2 cvs.
[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.40 2003/12/22 16:40:04 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/vout.h>
30 #include <vlc/decoder.h>
31
32 #include <mpeg2dec/mpeg2.h>
33
34 #include "vout_synchro.h"
35
36 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
37 #define AR_SQUARE_PICTURE       1                           /* square pixels */
38 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
39 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
40 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
41
42 /*****************************************************************************
43  * decoder_sys_t : libmpeg2 decoder descriptor
44  *****************************************************************************/
45 struct decoder_sys_t
46 {
47     /*
48      * libmpeg2 properties
49      */
50     mpeg2dec_t          *p_mpeg2dec;
51     const mpeg2_info_t  *p_info;
52     vlc_bool_t          b_skip;
53
54     /*
55      * Input properties
56      */
57     pes_packet_t     *p_pes;                  /* current PES we are decoding */
58     mtime_t          i_pts;
59     mtime_t          i_previous_pts;
60     mtime_t          i_current_pts;
61     int              i_current_rate;
62     picture_t *      p_picture_to_destroy;
63     vlc_bool_t       b_garbage_pic;
64     vlc_bool_t       b_after_sequence_header; /* is it the next frame after
65                                                * the sequence header ?    */
66     vlc_bool_t       b_slice_i;             /* intra-slice refresh stream */
67
68     /*
69      * Output properties
70      */
71     vout_synchro_t *p_synchro;
72     int i_aspect;
73
74 };
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 static int  OpenDecoder( vlc_object_t * );
80 static void CloseDecoder( vlc_object_t * );
81
82 static picture_t *DecodeBlock( decoder_t *, block_t ** );
83
84 static picture_t *GetNewPicture( decoder_t *, uint8_t ** );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 vlc_module_begin();
90     set_description( _("MPEG I/II video decoder (using libmpeg2)") );
91     set_capability( "decoder", 150 );
92     set_callbacks( OpenDecoder, CloseDecoder );
93     add_shortcut( "libmpeg2" );
94 vlc_module_end();
95
96 /*****************************************************************************
97  * OpenDecoder: probe the decoder and return score
98  *****************************************************************************/
99 static int OpenDecoder( vlc_object_t *p_this )
100 {
101     decoder_t *p_dec = (decoder_t*)p_this;
102     decoder_sys_t *p_sys;
103
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_pes      = NULL;
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     /* Initialize decoder */
138     p_sys->p_mpeg2dec = mpeg2_init();
139     if( p_sys->p_mpeg2dec == NULL)
140     {
141         msg_Err( p_dec, "mpeg2_init() failed" );
142         free( p_sys );
143         return VLC_EGENERIC;
144     }
145
146     p_sys->p_info = mpeg2_info( p_sys->p_mpeg2dec );
147
148     p_dec->pf_decode_video = DecodeBlock;
149
150     return VLC_SUCCESS;
151 }
152
153 /*****************************************************************************
154  * RunDecoder: the libmpeg2 decoder
155  *****************************************************************************/
156 static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
157 {
158     decoder_sys_t   *p_sys = p_dec->p_sys;
159     mpeg2_state_t   state;
160     picture_t       *p_pic;
161
162     block_t *p_block;
163
164     if( !pp_block || !*pp_block ) return NULL;
165
166     p_block = *pp_block;
167
168     while( 1 )
169     {
170         state = mpeg2_parse( p_sys->p_mpeg2dec );
171
172         switch( state )
173         {
174         case STATE_BUFFER:
175             if( !p_block->i_buffer )
176             {
177                 block_Release( p_block );
178                 return NULL;
179             }
180
181             if( p_block->b_discontinuity && p_sys->p_synchro &&
182                 p_sys->p_info->sequence &&
183                 p_sys->p_info->sequence->width != (unsigned)-1 )
184             {
185                 vout_SynchroReset( p_sys->p_synchro );
186                 if( p_sys->p_info->current_fbuf != NULL
187                     && p_sys->p_info->current_fbuf->id != NULL )
188                 {
189                     p_sys->b_garbage_pic = 1;
190                     p_pic = p_sys->p_info->current_fbuf->id;
191                 }
192                 else
193                 {
194                     uint8_t *buf[3];
195                     buf[0] = buf[1] = buf[2] = NULL;
196                     if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
197                         break;
198                     mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
199                 }
200                 p_sys->p_picture_to_destroy = p_pic;
201
202                 if ( p_sys->b_slice_i )
203                 {
204                     vout_SynchroNewPicture( p_sys->p_synchro,
205                         I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
206                     vout_SynchroDecode( p_sys->p_synchro );
207                     vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
208                 }
209             }
210
211             if( p_block->i_pts )
212             {
213 #ifdef PIC_FLAG_PTS
214                 mpeg2_pts( p_sys->p_mpeg2dec, (uint32_t)p_block->i_pts );
215
216 #else /* New interface */
217                 mpeg2_tag_picture( p_sys->p_mpeg2dec,
218                                    (uint32_t)p_block->i_pts, 0/*dts*/ );
219 #endif
220                 p_sys->i_previous_pts = p_sys->i_current_pts;
221                 p_sys->i_current_pts = p_block->i_pts;
222             }
223
224             p_sys->i_current_rate = DEFAULT_RATE;//p_pes->i_rate;
225
226             mpeg2_buffer( p_sys->p_mpeg2dec, p_block->p_buffer,
227                           p_block->p_buffer + p_block->i_buffer );
228
229             p_block->i_buffer = 0;
230             break;
231
232         case STATE_SEQUENCE:
233         {
234             /* Initialize video output */
235             uint8_t *buf[3];
236             buf[0] = buf[1] = buf[2] = NULL;
237
238             /* Check whether the input gave a particular aspect ratio */
239             if( p_dec->fmt_in.video.i_aspect )
240             {
241                 p_sys->i_aspect = p_dec->fmt_in.video.i_aspect;
242                 if( p_sys->i_aspect <= AR_221_1_PICTURE )
243                 switch( p_sys->i_aspect )
244                 {
245                 case AR_3_4_PICTURE:
246                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
247                     break;
248                 case AR_16_9_PICTURE:
249                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
250                     break;
251                 case AR_221_1_PICTURE:
252                     p_sys->i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
253                     break;
254                 case AR_SQUARE_PICTURE:
255                     p_sys->i_aspect = VOUT_ASPECT_FACTOR *
256                                    p_sys->p_info->sequence->width /
257                                    p_sys->p_info->sequence->height;
258                     break;
259                 }
260             }
261             else
262             {
263                 /* Use the value provided in the MPEG sequence header */
264                 p_sys->i_aspect =
265                     ((uint64_t)p_sys->p_info->sequence->display_width) *
266                     p_sys->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
267                     p_sys->p_info->sequence->display_height /
268                     p_sys->p_info->sequence->pixel_height;
269             }
270
271             msg_Dbg( p_dec, "%dx%d, aspect %d, %u.%03u fps",
272                      p_sys->p_info->sequence->width,
273                      p_sys->p_info->sequence->height, p_sys->i_aspect,
274                      (uint32_t)((uint64_t)1001000000 * 27 /
275                          p_sys->p_info->sequence->frame_period / 1001),
276                      (uint32_t)((uint64_t)1001000000 * 27 /
277                          p_sys->p_info->sequence->frame_period % 1001) );
278
279             mpeg2_custom_fbuf( p_sys->p_mpeg2dec, 1 );
280
281             /* Set the first 2 reference frames */
282             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
283
284             if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
285             {
286                 block_Release( p_block );
287                 return NULL;
288             }
289
290             mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
291
292             /* This picture will never go through display_picture. */
293             p_pic->date = 0;
294
295             /* For some reason, libmpeg2 will put this pic twice in
296              * discard_picture. This can be considered a bug in libmpeg2. */
297             p_dec->pf_picture_link( p_dec, p_pic );
298
299             if( p_sys->p_synchro )
300             {
301                 vout_SynchroRelease( p_sys->p_synchro );
302             }
303             p_sys->p_synchro = vout_SynchroInit( p_dec,
304                 (uint32_t)((uint64_t)1001000000 * 27 /
305                 p_sys->p_info->sequence->frame_period) );
306             p_sys->b_after_sequence_header = 1;
307         }
308         break;
309
310         case STATE_PICTURE_2ND:
311             vout_SynchroNewPicture( p_sys->p_synchro,
312                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
313                 p_sys->p_info->current_picture->nb_fields,
314                 0, 0, p_sys->i_current_rate );
315
316             if( p_sys->b_skip )
317             {
318                 vout_SynchroTrash( p_sys->p_synchro );
319             }
320             else
321             {
322                 vout_SynchroDecode( p_sys->p_synchro );
323             }
324             break;
325
326         case STATE_PICTURE:
327         {
328             uint8_t *buf[3];
329             mtime_t i_pts;
330             buf[0] = buf[1] = buf[2] = NULL;
331
332             if ( p_sys->b_after_sequence_header &&
333                  ((p_sys->p_info->current_picture->flags &
334                        PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_P) )
335             {
336                 /* Intra-slice refresh. Simulate a blank I picture. */
337                 msg_Dbg( p_dec, "intra-slice refresh stream" );
338                 vout_SynchroNewPicture( p_sys->p_synchro,
339                     I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
340                 vout_SynchroDecode( p_sys->p_synchro );
341                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
342                 p_sys->b_slice_i = 1;
343             }
344             p_sys->b_after_sequence_header = 0;
345
346 #ifdef PIC_FLAG_PTS
347             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_PTS ?
348                 ( ( p_sys->p_info->current_picture->pts ==
349                     (uint32_t)p_sys->i_current_pts ) ?
350                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
351
352 #else /* New interface */
353
354             i_pts = p_sys->p_info->current_picture->flags & PIC_FLAG_TAGS ?
355                 ( ( p_sys->p_info->current_picture->tag ==
356                     (uint32_t)p_sys->i_current_pts ) ?
357                   p_sys->i_current_pts : p_sys->i_previous_pts ) : 0;
358 #endif
359
360             /* Hack to handle demuxers which only have DTS timestamps */
361             if( !i_pts && !p_block->i_pts && p_block->i_dts > 0 )
362             {
363                 if( p_sys->p_info->sequence->flags & SEQ_FLAG_LOW_DELAY ||
364                     (p_sys->p_info->current_picture->flags &
365                       PIC_MASK_CODING_TYPE) == PIC_FLAG_CODING_TYPE_B )
366                 {
367                     i_pts = p_block->i_dts;
368                 }
369             }
370             p_block->i_pts = p_block->i_dts = 0;
371             /* End hack */
372
373             vout_SynchroNewPicture( p_sys->p_synchro,
374                 p_sys->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
375                 p_sys->p_info->current_picture->nb_fields, i_pts,
376                 0, p_sys->i_current_rate );
377
378             if ( !(p_sys->b_slice_i
379                    && ((p_sys->p_info->current_picture->flags
380                          & PIC_MASK_CODING_TYPE) == P_CODING_TYPE))
381                    && !vout_SynchroChoose( p_sys->p_synchro,
382                               p_sys->p_info->current_picture->flags
383                                 & PIC_MASK_CODING_TYPE,
384                               /*p_sys->p_vout->render_time*/ 0 /*FIXME*/ ) )
385             {
386                 mpeg2_skip( p_sys->p_mpeg2dec, 1 );
387                 p_sys->b_skip = 1;
388                 vout_SynchroTrash( p_sys->p_synchro );
389                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, NULL );
390             }
391             else
392             {
393                 mpeg2_skip( p_sys->p_mpeg2dec, 0 );
394                 p_sys->b_skip = 0;
395                 vout_SynchroDecode( p_sys->p_synchro );
396
397                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
398                 {
399                     block_Release( p_block );
400                     return NULL;
401                 }
402
403                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
404             }
405         }
406         break;
407
408         case STATE_END:
409         case STATE_SLICE:
410             p_pic = NULL;
411             if( p_sys->p_info->display_fbuf
412                 && p_sys->p_info->display_fbuf->id )
413             {
414                 p_pic = (picture_t *)p_sys->p_info->display_fbuf->id;
415
416                 vout_SynchroEnd( p_sys->p_synchro,
417                             p_sys->p_info->display_picture->flags
418                              & PIC_MASK_CODING_TYPE,
419                             p_sys->b_garbage_pic );
420                 p_sys->b_garbage_pic = 0;
421
422                 if ( p_sys->p_picture_to_destroy != p_pic )
423                 {
424                     p_pic->date = vout_SynchroDate( p_sys->p_synchro );
425                 }
426                 else
427                 {
428                     p_sys->p_picture_to_destroy = NULL;
429                     p_pic->date = 0;
430                 }
431             }
432
433             if( p_sys->p_info->discard_fbuf &&
434                 p_sys->p_info->discard_fbuf->id )
435             {
436                 p_dec->pf_picture_unlink( p_dec,
437                                           p_sys->p_info->discard_fbuf->id );
438             }
439
440             if( p_pic ) return p_pic;
441
442             break;
443
444         case STATE_INVALID:
445         {
446             uint8_t *buf[3];
447             buf[0] = buf[1] = buf[2] = NULL;
448
449             msg_Warn( p_dec, "invalid picture encountered" );
450             if ( ( p_sys->p_info->current_picture == NULL ) ||
451                ( ( p_sys->p_info->current_picture->flags &
452                    PIC_MASK_CODING_TYPE) != B_CODING_TYPE ) )
453             {
454                 if( p_sys->p_synchro ) vout_SynchroReset( p_sys->p_synchro );
455             }
456             mpeg2_skip( p_sys->p_mpeg2dec, 1 );
457             p_sys->b_skip = 1;
458
459             if( p_sys->p_info->current_fbuf &&
460                 p_sys->p_info->current_fbuf->id )
461             {
462                 p_sys->b_garbage_pic = 1;
463                 p_pic = p_sys->p_info->current_fbuf->id;
464             }
465             else if( !p_sys->p_info->sequence )
466             {
467                 break;
468             }
469             else
470             {
471                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL )
472                     break;
473                 mpeg2_set_buf( p_sys->p_mpeg2dec, buf, p_pic );
474             }
475             p_sys->p_picture_to_destroy = p_pic;
476
477             memset( p_pic->p[0].p_pixels, 0,
478                     p_sys->p_info->sequence->width
479                      * p_sys->p_info->sequence->height );
480             memset( p_pic->p[1].p_pixels, 0x80,
481                     p_sys->p_info->sequence->width
482                      * p_sys->p_info->sequence->height / 4 );
483             memset( p_pic->p[2].p_pixels, 0x80,
484                     p_sys->p_info->sequence->width
485                      * p_sys->p_info->sequence->height / 4 );
486
487             if( p_sys->b_slice_i )
488             {
489                 vout_SynchroNewPicture( p_sys->p_synchro,
490                             I_CODING_TYPE, 2, 0, 0, p_sys->i_current_rate );
491                 vout_SynchroDecode( p_sys->p_synchro );
492                 vout_SynchroEnd( p_sys->p_synchro, I_CODING_TYPE, 0 );
493             }
494             break;
495         }
496
497         default:
498             break;
499         }
500     }
501
502     /* Never reached */
503     return NULL;
504 }
505
506 /*****************************************************************************
507  * CloseDecoder: libmpeg2 decoder destruction
508  *****************************************************************************/
509 static void CloseDecoder( vlc_object_t *p_this )
510 {
511     decoder_t *p_dec = (decoder_t *)p_this;
512     decoder_sys_t *p_sys = p_dec->p_sys;
513
514     if( p_sys->p_synchro ) vout_SynchroRelease( p_sys->p_synchro );
515
516     if( p_sys->p_mpeg2dec ) mpeg2_close( p_sys->p_mpeg2dec );
517
518     free( p_sys );
519 }
520
521 /*****************************************************************************
522  * GetNewPicture: Get a new picture from the vout and set the buf struct
523  *****************************************************************************/
524 static picture_t *GetNewPicture( decoder_t *p_dec, uint8_t **pp_buf )
525 {
526     decoder_sys_t *p_sys = p_dec->p_sys;
527     picture_t *p_pic;
528
529     p_dec->fmt_out.video.i_width = p_sys->p_info->sequence->width;
530     p_dec->fmt_out.video.i_height = p_sys->p_info->sequence->height;
531     p_dec->fmt_out.video.i_aspect = p_sys->i_aspect;
532
533     p_dec->fmt_out.i_codec =
534         ( p_sys->p_info->sequence->chroma_height <
535           p_sys->p_info->sequence->height ) ?
536         VLC_FOURCC('I','4','2','0') : VLC_FOURCC('I','4','2','2');
537
538     /* Get a new picture */
539     p_pic = p_dec->pf_vout_buffer_new( p_dec );
540
541     if( p_pic == NULL ) return NULL;
542
543     p_pic->b_progressive = p_sys->p_info->current_picture != NULL ?
544         p_sys->p_info->current_picture->flags & PIC_FLAG_PROGRESSIVE_FRAME : 1;
545     p_pic->b_top_field_first = p_sys->p_info->current_picture != NULL ?
546         p_sys->p_info->current_picture->flags & PIC_FLAG_TOP_FIELD_FIRST : 1;
547     p_pic->i_nb_fields = p_sys->p_info->current_picture != NULL ?
548         p_sys->p_info->current_picture->nb_fields : 2;
549
550     p_dec->pf_picture_link( p_dec, p_pic );
551
552     pp_buf[0] = p_pic->p[0].p_pixels;
553     pp_buf[1] = p_pic->p[1].p_pixels;
554     pp_buf[2] = p_pic->p[2].p_pixels;
555
556     return p_pic;
557 }