]> git.sesse.net Git - vlc/blob - modules/codec/libmpeg2.c
* modules/codec/libmpeg2.c: Fixed synchro with field pictures.
[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.18 2003/05/24 13:05:55 massiot Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
31
32 #include <stdlib.h>                                      /* malloc(), free() */
33 #include <string.h>                                    /* memcpy(), memset() */
34
35 #include <mpeg2dec/mpeg2.h>
36
37 #include "vout_synchro.h"
38
39 /* Aspect ratio (ISO/IEC 13818-2 section 6.3.3, table 6-3) */
40 #define AR_SQUARE_PICTURE       1                           /* square pixels */
41 #define AR_3_4_PICTURE          2                        /* 3:4 picture (TV) */
42 #define AR_16_9_PICTURE         3              /* 16:9 picture (wide screen) */
43 #define AR_221_1_PICTURE        4                  /* 2.21:1 picture (movie) */
44
45 /*****************************************************************************
46  * dec_thread_t : libmpeg2 decoder thread descriptor
47  *****************************************************************************/
48 typedef struct dec_thread_t
49 {
50     /*
51      * libmpeg2 properties
52      */
53     mpeg2dec_t          *p_mpeg2dec;
54     const mpeg2_info_t  *p_info;
55     vlc_bool_t          b_skip;
56
57     /*
58      * Input properties
59      */
60     decoder_fifo_t   *p_fifo;                  /* stores the PES stream data */
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     mtime_t          i_period_remainder;
66     int              i_current_rate;
67     picture_t *      p_picture_to_destroy;
68
69     /*
70      * Output properties
71      */
72     vout_thread_t *p_vout;
73     vout_synchro_t *p_synchro;
74
75 } dec_thread_t;
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int  OpenDecoder  ( vlc_object_t * );
81 static int  RunDecoder   ( decoder_fifo_t * );
82 static void CloseDecoder ( dec_thread_t * );
83
84 static picture_t *GetNewPicture( dec_thread_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", 40 );
92     set_callbacks( OpenDecoder, NULL );
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_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
102
103     if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','v') )
104     {
105         return VLC_EGENERIC;
106     }
107
108     p_fifo->pf_run = RunDecoder;
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * RunDecoder: the libmpeg2 decoder
114  *****************************************************************************/
115 static int RunDecoder( decoder_fifo_t *p_fifo )
116 {
117     dec_thread_t    *p_dec;
118     data_packet_t   *p_data = NULL;
119     mpeg2_state_t   state;
120     picture_t       *p_pic;
121     int             i_aspect, i_chroma;
122
123     /* Allocate the memory needed to store the thread's structure */
124     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
125         == NULL)
126     {
127         msg_Err( p_fifo, "out of memory" );
128         goto error;
129     }
130
131     /* Initialize the thread properties */
132     memset( p_dec, 0, sizeof(dec_thread_t) );
133     p_dec->p_fifo     = p_fifo;
134     p_dec->p_pes      = NULL;
135     p_dec->p_vout     = NULL;
136     p_dec->p_mpeg2dec = NULL;
137     p_dec->p_synchro  = NULL;
138     p_dec->p_info     = NULL;
139     p_dec->i_pts      = mdate() + DEFAULT_PTS_DELAY;
140     p_dec->i_current_pts  = 0;
141     p_dec->i_previous_pts = 0;
142     p_dec->i_period_remainder = 0;
143     p_dec->p_picture_to_destroy = NULL;
144     p_dec->b_skip     = 0;
145
146     /* Initialize decoder */
147     p_dec->p_mpeg2dec = mpeg2_init();
148     if( p_dec->p_mpeg2dec == NULL)
149         goto error;
150
151     p_dec->p_info = mpeg2_info( p_dec->p_mpeg2dec );
152
153     /* libmpeg2 decoder thread's main loop */
154     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
155     {
156         state = mpeg2_parse( p_dec->p_mpeg2dec );
157
158         switch( state )
159         {
160         case STATE_BUFFER:
161             /* Feed libmpeg2 a data packet at a time */
162             if( p_data == NULL )
163             {
164                 /* Get the next PES */
165                 if( p_dec->p_pes )
166                     input_DeletePES( p_dec->p_fifo->p_packets_mgt,
167                                      p_dec->p_pes );
168
169                 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
170                 if( !p_dec->p_pes )
171                 {
172                     p_dec->p_fifo->b_error = 1;
173                     break;
174                 }
175
176                 if( p_dec->p_pes->b_discontinuity && p_dec->p_synchro )
177                 {
178                     vout_SynchroReset( p_dec->p_synchro );
179                     if ( p_dec->p_info->current_fbuf != NULL )
180                         p_dec->p_picture_to_destroy
181                             = p_dec->p_info->current_fbuf->id;
182                 }
183
184                 if( p_dec->p_pes->i_pts )
185                 {
186                     mpeg2_pts( p_dec->p_mpeg2dec,
187                                (uint32_t)p_dec->p_pes->i_pts );
188                     p_dec->i_previous_pts = p_dec->i_current_pts;
189                     p_dec->i_current_pts = p_dec->p_pes->i_pts;
190                 }
191
192                 p_dec->i_current_rate = p_dec->p_pes->i_rate;
193                 p_data = p_dec->p_pes->p_first;
194             }
195
196             if( p_data != NULL )
197             {
198                 mpeg2_buffer( p_dec->p_mpeg2dec,
199                               p_data->p_payload_start,
200                               p_data->p_payload_end );
201
202                 p_data = p_data->p_next;
203             }
204             break;
205
206         case STATE_SEQUENCE:
207         {
208             /* Initialize video output */
209             uint8_t *buf[3];
210             buf[0] = buf[1] = buf[2] = NULL;
211
212             /* Check whether the input gives a particular aspect ratio */
213             if( p_dec->p_fifo->p_demux_data
214                 && ( *(int*)(p_dec->p_fifo->p_demux_data) & 0x7 ) )
215             {
216                 i_aspect = *(int*)(p_dec->p_fifo->p_demux_data);
217                 switch( i_aspect )
218                 {
219                 case AR_3_4_PICTURE:
220                     i_aspect = VOUT_ASPECT_FACTOR * 4 / 3;
221                     break;
222                 case AR_16_9_PICTURE:
223                     i_aspect = VOUT_ASPECT_FACTOR * 16 / 9;
224                     break;
225                 case AR_221_1_PICTURE:
226                     i_aspect = VOUT_ASPECT_FACTOR * 221 / 100;
227                     break;
228                 case AR_SQUARE_PICTURE:
229                 default:
230                     i_aspect = VOUT_ASPECT_FACTOR *
231                                    p_dec->p_info->sequence->width /
232                                    p_dec->p_info->sequence->height;
233                     break;
234                 }
235             }
236             else
237             {
238                 /* Use the value provided in the MPEG sequence header */
239                 i_aspect = ((uint64_t)p_dec->p_info->sequence->display_width) *
240                     p_dec->p_info->sequence->pixel_width * VOUT_ASPECT_FACTOR /
241                     p_dec->p_info->sequence->display_height /
242                     p_dec->p_info->sequence->pixel_height;
243             }
244
245             i_chroma = VLC_FOURCC('Y','V','1','2');
246
247             p_dec->p_vout = vout_Request( p_dec->p_fifo, p_dec->p_vout,
248                                           p_dec->p_info->sequence->width,
249                                           p_dec->p_info->sequence->height,
250                                           i_chroma, i_aspect );
251
252             msg_Dbg( p_dec->p_fifo, "%dx%d, aspect %d, %u.%03u fps",
253                      p_dec->p_info->sequence->width,
254                      p_dec->p_info->sequence->height, i_aspect,
255                      (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period / 1001),
256                      (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period % 1001) );
257
258             mpeg2_custom_fbuf( p_dec->p_mpeg2dec, 1 );
259
260             /* Set the first 2 reference frames */
261             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
262             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
263
264             if ( p_dec->p_synchro )
265             {
266                 vout_SynchroRelease( p_dec->p_synchro );
267             }
268             p_dec->p_synchro = vout_SynchroInit( p_dec->p_fifo, p_dec->p_vout,
269                 (u32)((u64)1001000000 * 27 / p_dec->p_info->sequence->frame_period) );
270         }
271         break;
272
273         case STATE_PICTURE_2ND:
274             vout_SynchroNewPicture( p_dec->p_synchro,
275                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
276                 p_dec->p_info->current_picture->nb_fields,
277                 0, 0,
278                 p_dec->i_current_rate );
279
280             if ( p_dec->b_skip )
281             {
282                 vout_SynchroTrash( p_dec->p_synchro );
283             }
284             else
285             {
286                 vout_SynchroDecode( p_dec->p_synchro );
287             }
288             break;
289
290         case STATE_PICTURE:
291         {
292             uint8_t *buf[3];
293             buf[0] = buf[1] = buf[2] = NULL;
294
295             vout_SynchroNewPicture( p_dec->p_synchro,
296                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE,
297                 p_dec->p_info->current_picture->nb_fields,
298                 (p_dec->p_info->current_picture->flags & PIC_FLAG_PTS) ?
299                     ( (p_dec->p_info->current_picture->pts ==
300                                 (uint32_t)p_dec->i_current_pts) ?
301                               p_dec->i_current_pts : p_dec->i_previous_pts ) : 0,
302                 0,
303                 p_dec->i_current_rate );
304
305             if ( !vout_SynchroChoose( p_dec->p_synchro,
306                 p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE ) )
307             {
308                 mpeg2_skip( p_dec->p_mpeg2dec, 1 );
309                 p_dec->b_skip = 1;
310                 vout_SynchroTrash( p_dec->p_synchro );
311                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
312             }
313             else
314             {
315                 mpeg2_skip( p_dec->p_mpeg2dec, 0 );
316                 p_dec->b_skip = 0;
317                 vout_SynchroDecode( p_dec->p_synchro );
318                 if( (p_pic = GetNewPicture( p_dec, buf )) == NULL ) break;
319                 mpeg2_set_buf( p_dec->p_mpeg2dec, buf, p_pic );
320             }
321         }
322         break;
323
324         case STATE_END:
325         case STATE_SLICE:
326             if( p_dec->p_info->display_fbuf
327                 && p_dec->p_info->display_fbuf->id )
328             {
329                 p_pic = (picture_t *)p_dec->p_info->display_fbuf->id;
330
331                 if ( p_pic != NULL )
332                 {
333                     if ( p_dec->p_picture_to_destroy != p_pic )
334                     {
335                         vout_SynchroEnd( p_dec->p_synchro,
336                             p_dec->p_info->display_picture->flags
337                              & PIC_MASK_CODING_TYPE,
338                             0 );
339                         vout_DatePicture( p_dec->p_vout, p_pic,
340                             vout_SynchroDate( p_dec->p_synchro ) );
341                         vout_DisplayPicture( p_dec->p_vout, p_pic );
342                     }
343                     else
344                     {
345                         p_dec->p_picture_to_destroy = NULL;
346                         vout_SynchroEnd( p_dec->p_synchro,
347                             p_dec->p_info->display_picture->flags
348                              & PIC_MASK_CODING_TYPE,
349                             1 );
350                         vout_DatePicture( p_dec->p_vout, p_pic, 0 );
351                         vout_DisplayPicture( p_dec->p_vout, p_pic );
352                     }
353                 }
354             }
355
356             if( p_dec->p_info->discard_fbuf &&
357                 p_dec->p_info->discard_fbuf->id )
358             {
359                 p_pic = (picture_t *)p_dec->p_info->discard_fbuf->id;
360                 vout_UnlinkPicture( p_dec->p_vout, p_pic );
361             }
362             break;
363
364         case STATE_INVALID:
365         {
366             uint8_t *buf[3];
367             buf[0] = buf[1] = buf[2] = NULL;
368
369             msg_Warn( p_dec->p_fifo, "invalid picture encountered" );
370             if ( (p_dec->p_info->current_picture->flags & PIC_MASK_CODING_TYPE)
371                   != B_CODING_TYPE )
372             {
373                 vout_SynchroReset( p_dec->p_synchro );
374             }
375             mpeg2_skip( p_dec->p_mpeg2dec, 1 );
376             p_dec->b_skip = 1;
377
378             if( p_dec->p_info->current_fbuf &&
379                 p_dec->p_info->current_fbuf->id )
380             {
381                 p_pic = (picture_t *)p_dec->p_info->current_fbuf->id;
382                 vout_UnlinkPicture( p_dec->p_vout, p_pic );
383                 vout_DestroyPicture( p_dec->p_vout, p_pic );
384             }
385             mpeg2_set_buf( p_dec->p_mpeg2dec, buf, NULL );
386             break;
387         }
388
389         default:
390             break;
391         }
392     }
393
394     /* If b_error is set, the libmpeg2 decoder thread enters the error loop */
395     if( p_dec->p_fifo->b_error )
396     {
397         DecoderError( p_dec->p_fifo );
398     }
399
400     /* End of the libmpeg2 decoder thread */
401     CloseDecoder( p_dec );
402
403     return 0;
404
405  error:
406     DecoderError( p_fifo );
407     if( p_dec )
408     {
409         if( p_dec->p_fifo )
410             p_dec->p_fifo->b_error = 1;
411
412         /* End of the libmpeg2 decoder thread */
413         CloseDecoder( p_dec );
414     }
415
416     return -1;
417 }
418
419 /*****************************************************************************
420  * CloseDecoder: libmpeg2 decoder destruction
421  *****************************************************************************/
422 static void CloseDecoder( dec_thread_t * p_dec )
423 {
424     if( p_dec )
425     {
426         int i_pic;
427
428         if( p_dec->p_pes )
429             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
430
431         if( p_dec->p_synchro )
432             vout_SynchroRelease( p_dec->p_synchro );
433
434         if( p_dec->p_vout )
435         {
436             /* Temporary hack to free the pictures in use by libmpeg2 */
437             for( i_pic = 0; i_pic < p_dec->p_vout->render.i_pictures; i_pic++ )
438             {
439                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_status ==
440                       RESERVED_PICTURE )
441                     vout_DestroyPicture( p_dec->p_vout,
442                                      p_dec->p_vout->render.pp_picture[i_pic] );
443                 if( p_dec->p_vout->render.pp_picture[i_pic]->i_refcount > 0 )
444                     vout_UnlinkPicture( p_dec->p_vout,
445                                      p_dec->p_vout->render.pp_picture[i_pic] );
446             }
447
448             vout_Request( p_dec->p_fifo, p_dec->p_vout, 0, 0, 0, 0 );
449         }
450
451         if( p_dec->p_mpeg2dec ) mpeg2_close( p_dec->p_mpeg2dec );
452
453         free( p_dec );
454     }
455 }
456
457 /*****************************************************************************
458  * GetNewPicture: Get a new picture from the vout and set the buf struct
459  *****************************************************************************/
460 static picture_t *GetNewPicture( dec_thread_t *p_dec, uint8_t **pp_buf )
461 {
462     picture_t *p_pic;
463
464     /* Get a new picture */
465     while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
466     {
467         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
468             break;
469
470         msleep( VOUT_OUTMEM_SLEEP );
471     }
472     if( p_pic == NULL )
473         return NULL;
474     vout_LinkPicture( p_dec->p_vout, p_pic );
475
476     pp_buf[0] = p_pic->p[0].p_pixels;
477     pp_buf[1] = p_pic->p[1].p_pixels;
478     pp_buf[2] = p_pic->p[2].p_pixels;
479
480     return p_pic;
481 }