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