]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/video.c
a45e3ac0e6bca50d14bb7944f4a92abd9e5c6e1b
[vlc] / modules / codec / ffmpeg / video.c
1 /*****************************************************************************
2  * video.c: video decoder using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_codec.h>
30 #include <vlc_vout.h>
31 #include <vlc_input.h>                  /* hmmm, just for INPUT_RATE_DEFAULT */
32
33 /* ffmpeg header */
34 #ifdef HAVE_FFMPEG_AVCODEC_H
35 #   include <ffmpeg/avcodec.h>
36 #else
37 #   include <avcodec.h>
38 #endif
39
40 #include "ffmpeg.h"
41
42 /*****************************************************************************
43  * decoder_sys_t : decoder descriptor
44  *****************************************************************************/
45 struct decoder_sys_t
46 {
47     FFMPEG_COMMON_MEMBERS
48
49     /* Video decoder specific part */
50     mtime_t input_pts;
51     mtime_t input_dts;
52     mtime_t i_pts;
53
54     AVFrame          *p_ff_pic;
55     BITMAPINFOHEADER *p_format;
56
57     /* for frame skipping algo */
58     int b_hurry_up;
59     enum AVDiscard i_skip_frame;
60     enum AVDiscard i_skip_idct;
61
62     /* how many decoded frames are late */
63     int     i_late_frames;
64     mtime_t i_late_frames_start;
65
66     /* for direct rendering */
67     int b_direct_rendering;
68
69     vlc_bool_t b_has_b_frames;
70
71     /* Hack to force display of still pictures */
72     vlc_bool_t b_first_frame;
73
74     int i_buffer_orig, i_buffer;
75     char *p_buffer_orig, *p_buffer;
76
77     /* Postprocessing handle */
78     void *p_pp;
79     vlc_bool_t b_pp;
80     vlc_bool_t b_pp_async;
81     vlc_bool_t b_pp_init;
82 };
83
84 /* FIXME (dummy palette for now) */
85 static AVPaletteControl palette_control;
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static void ffmpeg_InitCodec      ( decoder_t * );
91 static void ffmpeg_CopyPicture    ( decoder_t *, picture_t *, AVFrame * );
92 static int  ffmpeg_GetFrameBuf    ( struct AVCodecContext *, AVFrame * );
93 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
94
95 static uint32_t ffmpeg_CodecTag( vlc_fourcc_t fcc )
96 {
97     uint8_t *p = (uint8_t*)&fcc;
98     return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
99 }
100
101 /*****************************************************************************
102  * Local Functions
103  *****************************************************************************/
104 static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
105 {
106     switch( i_ff_chroma )
107     {
108     case PIX_FMT_YUV420P:
109     case PIX_FMT_YUVJ420P: /* Hacky but better then chroma conversion */
110         return VLC_FOURCC('I','4','2','0');
111     case PIX_FMT_YUV422P:
112     case PIX_FMT_YUVJ422P: /* Hacky but better then chroma conversion */
113         return VLC_FOURCC('I','4','2','2');
114     case PIX_FMT_YUV444P:
115     case PIX_FMT_YUVJ444P: /* Hacky but better then chroma conversion */
116         return VLC_FOURCC('I','4','4','4');
117
118     case PIX_FMT_YUV422:
119         return VLC_FOURCC('Y','U','Y','2');
120
121     case PIX_FMT_RGB555:
122         return VLC_FOURCC('R','V','1','5');
123     case PIX_FMT_RGB565:
124         return VLC_FOURCC('R','V','1','6');
125     case PIX_FMT_RGB24:
126         return VLC_FOURCC('R','V','2','4');
127     case PIX_FMT_RGBA32:
128         return VLC_FOURCC('R','V','3','2');
129     case PIX_FMT_GRAY8:
130         return VLC_FOURCC('G','R','E','Y');
131
132     case PIX_FMT_YUV410P:
133     case PIX_FMT_YUV411P:
134     case PIX_FMT_BGR24:
135     default:
136         return 0;
137     }
138 }
139
140 /* Returns a new picture buffer */
141 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
142                                             AVCodecContext *p_context )
143 {
144     decoder_sys_t *p_sys = p_dec->p_sys;
145     picture_t *p_pic;
146
147     p_dec->fmt_out.video.i_width = p_context->width;
148     p_dec->fmt_out.video.i_height = p_context->height;
149     p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
150
151     if( !p_context->width || !p_context->height )
152     {
153         return NULL; /* invalid display size */
154     }
155
156     if( !p_dec->fmt_out.i_codec )
157     {
158         /* we make conversion if possible*/
159         p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
160     }
161
162     /* If an aspect-ratio was specified in the input format then force it */
163     if( p_dec->fmt_in.video.i_aspect )
164     {
165         p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
166     }
167     else
168     {
169         p_dec->fmt_out.video.i_aspect =
170             VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
171                 p_context->width / p_context->height );
172         p_dec->fmt_out.video.i_sar_num = p_context->sample_aspect_ratio.num;
173         p_dec->fmt_out.video.i_sar_den = p_context->sample_aspect_ratio.den;
174
175         if( p_dec->fmt_out.video.i_aspect == 0 )
176         {
177             p_dec->fmt_out.video.i_aspect =
178                 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
179         }
180     }
181
182     if( p_dec->fmt_out.video.i_frame_rate > 0 &&
183         p_dec->fmt_out.video.i_frame_rate_base > 0 )
184     {
185         p_dec->fmt_out.video.i_frame_rate =
186             p_dec->fmt_in.video.i_frame_rate;
187         p_dec->fmt_out.video.i_frame_rate_base =
188             p_dec->fmt_in.video.i_frame_rate_base;
189     }
190     else if( p_context->time_base.num > 0 && p_context->time_base.den > 0 )
191     {
192         p_dec->fmt_out.video.i_frame_rate = p_context->time_base.den;
193         p_dec->fmt_out.video.i_frame_rate_base = p_context->time_base.num;
194     }
195
196     p_pic = p_dec->pf_vout_buffer_new( p_dec );
197
198     if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
199     {
200         E_(InitPostproc)( p_dec, p_sys->p_pp, p_context->width,
201                           p_context->height, p_context->pix_fmt );
202         p_sys->b_pp_init = VLC_TRUE;
203     }
204
205     return p_pic;
206 }
207
208 /*****************************************************************************
209  * InitVideo: initialize the video decoder
210  *****************************************************************************
211  * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
212  * opened (done after the first decoded frame).
213  *****************************************************************************/
214 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
215                       AVCodec *p_codec, int i_codec_id, const char *psz_namecodec )
216 {
217     decoder_sys_t *p_sys;
218     vlc_value_t val;
219
220     /* Allocate the memory needed to store the decoder's structure */
221     if( ( p_dec->p_sys = p_sys =
222           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
223     {
224         msg_Err( p_dec, "out of memory" );
225         return VLC_ENOMEM;
226     }
227     memset( p_sys, 0, sizeof(decoder_sys_t) );
228
229     p_dec->p_sys->p_context = p_context;
230     p_dec->p_sys->p_codec = p_codec;
231     p_dec->p_sys->i_codec_id = i_codec_id;
232     p_dec->p_sys->psz_namecodec = psz_namecodec;
233     p_sys->p_ff_pic = avcodec_alloc_frame();
234
235     /* ***** Fill p_context with init values ***** */
236     p_sys->p_context->codec_tag = ffmpeg_CodecTag( p_dec->fmt_in.i_codec );
237     p_sys->p_context->width  = p_dec->fmt_in.video.i_width;
238     p_sys->p_context->height = p_dec->fmt_in.video.i_height;
239     p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
240
241     /*  ***** Get configuration of ffmpeg plugin ***** */
242     p_sys->p_context->workaround_bugs =
243         config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
244     p_sys->p_context->error_resilience =
245         config_GetInt( p_dec, "ffmpeg-error-resilience" );
246
247     var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248     var_Get( p_dec, "grayscale", &val );
249     if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
250
251     var_Create( p_dec, "ffmpeg-vismv", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
252     var_Get( p_dec, "ffmpeg-vismv", &val );
253     if( val.i_int ) p_sys->p_context->debug_mv = val.i_int;
254
255     var_Create( p_dec, "ffmpeg-lowres", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
256     var_Get( p_dec, "ffmpeg-lowres", &val );
257     if( val.i_int > 0 && val.i_int <= 2 ) p_sys->p_context->lowres = val.i_int;
258
259     var_Create( p_dec, "ffmpeg-skiploopfilter",
260                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
261     var_Get( p_dec, "ffmpeg-skiploopfilter", &val );
262     if( val.i_int > 0 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONREF;
263     if( val.i_int > 1 ) p_sys->p_context->skip_loop_filter = AVDISCARD_BIDIR;
264     if( val.i_int > 2 ) p_sys->p_context->skip_loop_filter = AVDISCARD_NONKEY;
265     if( val.i_int > 3 ) p_sys->p_context->skip_loop_filter = AVDISCARD_ALL;
266
267     /* ***** ffmpeg frame skipping ***** */
268     var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
269     var_Get( p_dec, "ffmpeg-hurry-up", &val );
270     p_sys->b_hurry_up = val.b_bool;
271
272     var_Create( p_dec, "ffmpeg-skip-frame", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
273     var_Get( p_dec, "ffmpeg-skip-frame", &val );
274     switch( val.i_int )
275     {
276         case -1:
277             p_sys->p_context->skip_frame = AVDISCARD_NONE;
278             break;
279         case 0:
280             p_sys->p_context->skip_frame = AVDISCARD_DEFAULT;
281             break;
282         case 1:
283             p_sys->p_context->skip_frame = AVDISCARD_BIDIR;
284             break;
285         case 2:
286             p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
287             break;
288         case 3:
289             p_sys->p_context->skip_frame = AVDISCARD_ALL;
290             break;
291         default:
292             p_sys->p_context->skip_frame = AVDISCARD_NONE;
293             break;
294     }
295     p_sys->i_skip_frame = p_sys->p_context->skip_frame;
296
297     var_Create( p_dec, "ffmpeg-skip-idct",  VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
298     var_Get( p_dec, "ffmpeg-skip-idct", &val );
299     switch( val.i_int )
300     {
301         case -1:
302             p_sys->p_context->skip_idct = AVDISCARD_NONE;
303             break;
304         case 0:
305             p_sys->p_context->skip_idct = AVDISCARD_DEFAULT;
306             break;
307         case 1:
308             p_sys->p_context->skip_idct = AVDISCARD_BIDIR;
309             break;
310         case 2:
311             p_sys->p_context->skip_idct = AVDISCARD_NONKEY;
312             break;
313         case 3:
314             p_sys->p_context->skip_idct = AVDISCARD_ALL;
315             break;
316         default:
317             p_sys->p_context->skip_idct = AVDISCARD_NONE;
318             break;
319     }
320     p_sys->i_skip_idct = p_sys->p_context->skip_idct;
321
322     /* ***** ffmpeg direct rendering ***** */
323     p_sys->b_direct_rendering = 0;
324     var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
325     var_Get( p_dec, "ffmpeg-dr", &val );
326     if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
327         /* Apparently direct rendering doesn't work with YUV422P */
328         p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
329         /* H264 uses too many reference frames */
330         p_sys->i_codec_id != CODEC_ID_H264 &&
331         !p_sys->p_context->debug_mv )
332     {
333         /* Some codecs set pix_fmt only after the 1st frame has been decoded,
334          * so we need to do another check in ffmpeg_GetFrameBuf() */
335         p_sys->b_direct_rendering = 1;
336     }
337
338     p_sys->p_pp = NULL;
339     p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
340     p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
341
342     /* ffmpeg doesn't properly release old pictures when frames are skipped */
343     //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
344     if( p_sys->b_direct_rendering )
345     {
346         msg_Dbg( p_dec, "using direct rendering" );
347         p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
348     }
349
350     /* Always use our get_buffer wrapper so we can calculate the
351      * PTS correctly */
352     p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
353     p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
354     p_sys->p_context->opaque = p_dec;
355
356     /* ***** init this codec with special data ***** */
357     ffmpeg_InitCodec( p_dec );
358
359     /* ***** misc init ***** */
360     p_sys->input_pts = p_sys->input_dts = 0;
361     p_sys->i_pts = 0;
362     p_sys->b_has_b_frames = VLC_FALSE;
363     p_sys->b_first_frame = VLC_TRUE;
364     p_sys->i_late_frames = 0;
365     p_sys->i_buffer = 0;
366     p_sys->i_buffer_orig = 1;
367     p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer_orig );
368
369     /* Set output properties */
370     p_dec->fmt_out.i_cat = VIDEO_ES;
371     p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
372
373     /* Setup palette */
374     if( p_dec->fmt_in.video.p_palette )
375         p_sys->p_context->palctrl =
376             (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
377     else
378         p_sys->p_context->palctrl = &palette_control;
379
380     /* ***** Open the codec ***** */
381     vlc_mutex_t *lock = var_AcquireMutex( "avcodec" );
382     if( lock == NULL )
383     {
384         free( p_sys );
385         return VLC_ENOMEM;
386     }
387
388     if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
389     {
390         vlc_mutex_unlock( lock );
391         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
392         free( p_sys );
393         return VLC_EGENERIC;
394     }
395     vlc_mutex_unlock( lock );
396     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
397
398
399     return VLC_SUCCESS;
400 }
401
402 /*****************************************************************************
403  * DecodeVideo: Called to decode one or more frames
404  *****************************************************************************/
405 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
406 {
407     decoder_sys_t *p_sys = p_dec->p_sys;
408     int b_drawpicture;
409     int b_null_size = VLC_FALSE;
410     block_t *p_block;
411
412     if( !pp_block || !*pp_block ) return NULL;
413
414     if( !p_sys->p_context->extradata_size && p_dec->fmt_in.i_extra )
415         ffmpeg_InitCodec( p_dec );
416
417     p_block = *pp_block;
418
419     if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
420     {
421         p_sys->i_buffer = 0;
422         p_sys->i_pts = 0; /* To make sure we recover properly */
423
424         p_sys->input_pts = p_sys->input_dts = 0;
425         p_sys->i_late_frames = 0;
426
427         block_Release( p_block );
428
429         //if( p_block->i_flags & BLOCK_FLAG_CORRUPTED )
430             //avcodec_flush_buffers( p_sys->p_context );
431         return NULL;
432     }
433
434     if( p_block->i_flags & BLOCK_FLAG_PREROLL )
435     {
436         /* Do not care about late frames when prerolling
437          * TODO avoid decoding of non reference frame
438          * (ie all B except for H264 where it depends only on nal_ref_idc) */
439         p_sys->i_late_frames = 0;
440     }
441
442     if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
443         (mdate() - p_sys->i_late_frames_start > I64C(5000000)) )
444     {
445         if( p_sys->i_pts )
446         {
447             msg_Err( p_dec, "more than 5 seconds of late video -> "
448                      "dropping frame (computer too slow ?)" );
449             p_sys->i_pts = 0; /* To make sure we recover properly */
450         }
451         block_Release( p_block );
452         p_sys->i_late_frames--;
453         return NULL;
454     }
455
456     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
457     {
458         p_sys->input_pts = p_block->i_pts;
459         p_sys->input_dts = p_block->i_dts;
460
461         /* Make sure we don't reuse the same timestamps twice */
462         p_block->i_pts = p_block->i_dts = 0;
463     }
464
465     /* A good idea could be to decode all I pictures and see for the other */
466     if( !p_dec->b_pace_control &&
467         p_sys->b_hurry_up &&
468         (p_sys->i_late_frames > 4) )
469     {
470         b_drawpicture = 0;
471         if( p_sys->i_late_frames < 8 )
472         {
473             p_sys->p_context->skip_frame = AVDISCARD_NONKEY;
474         }
475         else
476         {
477             /* picture too late, won't decode
478              * but break picture until a new I, and for mpeg4 ...*/
479             p_sys->i_late_frames--; /* needed else it will never be decrease */
480             block_Release( p_block );
481             p_sys->i_buffer = 0;
482             return NULL;
483         }
484     }
485     else
486     {
487         if( p_sys->b_hurry_up )
488             p_sys->p_context->skip_frame = p_sys->i_skip_frame;
489         if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
490             b_drawpicture = 1;
491         else
492             b_drawpicture = 0;
493     }
494
495     if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
496     {
497         if( p_sys->b_hurry_up )
498             p_sys->p_context->skip_frame = p_sys->i_skip_frame;
499         b_null_size = VLC_TRUE;
500     }
501
502     /*
503      * Do the actual decoding now
504      */
505
506     /* Check if post-processing was enabled */
507     p_sys->b_pp = p_sys->b_pp_async;
508
509     /* Don't forget that ffmpeg requires a little more bytes
510      * that the real frame size */
511     if( p_block->i_buffer > 0 )
512     {
513         p_sys->i_buffer = p_block->i_buffer;
514         if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
515             p_sys->i_buffer_orig )
516         {
517             free( p_sys->p_buffer_orig );
518             p_sys->i_buffer_orig =
519                 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
520             p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
521         }
522         p_sys->p_buffer = p_sys->p_buffer_orig;
523         p_sys->i_buffer = p_block->i_buffer;
524         p_dec->p_libvlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
525                                  p_block->i_buffer );
526         memset( p_sys->p_buffer + p_block->i_buffer, 0,
527                 FF_INPUT_BUFFER_PADDING_SIZE );
528
529         p_block->i_buffer = 0;
530     }
531
532     while( p_sys->i_buffer > 0 )
533     {
534         int i_used, b_gotpicture;
535         picture_t *p_pic;
536
537         i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
538                                        &b_gotpicture,
539                                        (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
540         if( b_null_size && p_sys->p_context->width > 0 &&
541             p_sys->p_context->height > 0 )
542         {
543             /* Reparse it to not drop the I frame */
544             b_null_size = VLC_FALSE;
545             if( p_sys->b_hurry_up )
546                 p_sys->p_context->skip_frame = p_sys->i_skip_frame;
547             i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
548                                            &b_gotpicture,
549                                            (uint8_t*)p_sys->p_buffer, p_sys->i_buffer );
550         }
551
552         if( i_used < 0 )
553         {
554             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
555                       p_sys->i_buffer );
556             block_Release( p_block );
557             return NULL;
558         }
559         else if( i_used > p_sys->i_buffer )
560         {
561             i_used = p_sys->i_buffer;
562         }
563
564         /* Consumed bytes */
565         p_sys->i_buffer -= i_used;
566         p_sys->p_buffer += i_used;
567
568         /* Nothing to display */
569         if( !b_gotpicture )
570         {
571             if( i_used == 0 ) break;
572             continue;
573         }
574
575         /* Update frame late count (except when doing preroll) */
576         if( p_sys->i_pts && decoder_GetDisplayDate(p_sys->i_pts) <= mdate() &&
577             !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
578         {
579             p_sys->i_late_frames++;
580             if( p_sys->i_late_frames == 1 )
581                 p_sys->i_late_frames_start = mdate();
582         }
583         else
584         {
585             p_sys->i_late_frames = 0;
586         }
587
588         if( !b_drawpicture || !p_sys->p_ff_pic->linesize[0] )
589         {
590             /* Do not display the picture */
591             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
592             if( !b_drawpicture && p_pic )
593                 p_dec->pf_vout_buffer_del( p_dec, p_pic );
594             continue;
595         }
596
597         if( !p_sys->p_ff_pic->opaque )
598         {
599             /* Get a new picture */
600             p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
601             if( !p_pic )
602             {
603                 block_Release( p_block );
604                 return NULL;
605             }
606
607             /* Fill p_picture_t from AVVideoFrame and do chroma conversion
608              * if needed */
609             ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
610         }
611         else
612         {
613             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
614         }
615
616         /* Set the PTS */
617         if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
618
619         /* Sanity check (seems to be needed for some streams) */
620         if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
621         {
622             p_sys->b_has_b_frames = VLC_TRUE;
623         }
624
625         if( !p_dec->fmt_in.video.i_aspect )
626         {
627             /* Fetch again the aspect ratio in case it changed */
628             p_dec->fmt_out.video.i_aspect =
629                 VOUT_ASPECT_FACTOR
630                     * ( av_q2d(p_sys->p_context->sample_aspect_ratio)
631                     * p_sys->p_context->width / p_sys->p_context->height );
632             p_dec->fmt_out.video.i_sar_num
633                 = p_sys->p_context->sample_aspect_ratio.num;
634             p_dec->fmt_out.video.i_sar_den
635                 = p_sys->p_context->sample_aspect_ratio.den;
636
637             if( p_dec->fmt_out.video.i_aspect == 0 )
638             {
639                 p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR
640                     * p_sys->p_context->width / p_sys->p_context->height;
641             }
642         }
643
644         /* Send decoded frame to vout */
645         if( p_sys->i_pts )
646         {
647             p_pic->date = p_sys->i_pts;
648
649             /* interpolate the next PTS */
650             if( p_dec->fmt_in.video.i_frame_rate > 0 &&
651                 p_dec->fmt_in.video.i_frame_rate_base > 0 )
652             {
653                 p_sys->i_pts += I64C(1000000) *
654                     (2 + p_sys->p_ff_pic->repeat_pict) *
655                     p_dec->fmt_in.video.i_frame_rate_base *
656                     p_block->i_rate / INPUT_RATE_DEFAULT /
657                     (2 * p_dec->fmt_in.video.i_frame_rate);
658             }
659             else if( p_sys->p_context->time_base.den > 0 )
660             {
661                 p_sys->i_pts += I64C(1000000) *
662                     (2 + p_sys->p_ff_pic->repeat_pict) *
663                     p_sys->p_context->time_base.num *
664                     p_block->i_rate / INPUT_RATE_DEFAULT /
665                     (2 * p_sys->p_context->time_base.den);
666             }
667
668             if( p_sys->b_first_frame )
669             {
670                 /* Hack to force display of still pictures */
671                 p_sys->b_first_frame = VLC_FALSE;
672                 p_pic->b_force = VLC_TRUE;
673             }
674
675             p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
676             p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
677             p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;
678
679             return p_pic;
680         }
681         else
682         {
683             p_dec->pf_vout_buffer_del( p_dec, p_pic );
684         }
685     }
686
687     block_Release( p_block );
688     return NULL;
689 }
690
691 /*****************************************************************************
692  * EndVideo: decoder destruction
693  *****************************************************************************
694  * This function is called when the thread ends after a successful
695  * initialization.
696  *****************************************************************************/
697 void E_(EndVideoDec)( decoder_t *p_dec )
698 {
699     decoder_sys_t *p_sys = p_dec->p_sys;
700
701     if( p_sys->p_ff_pic ) av_free( p_sys->p_ff_pic );
702     E_(ClosePostproc)( p_dec, p_sys->p_pp );
703     free( p_sys->p_buffer_orig );
704 }
705
706 /*****************************************************************************
707  * ffmpeg_InitCodec: setup codec extra initialization data for ffmpeg
708  *****************************************************************************/
709 static void ffmpeg_InitCodec( decoder_t *p_dec )
710 {
711     decoder_sys_t *p_sys = p_dec->p_sys;
712     int i_size = p_dec->fmt_in.i_extra;
713
714     if( !i_size ) return;
715
716     if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
717     {
718         uint8_t *p;
719
720         p_sys->p_context->extradata_size = i_size + 12;
721         p = p_sys->p_context->extradata  =
722             malloc( p_sys->p_context->extradata_size );
723
724         memcpy( &p[0],  "SVQ3", 4 );
725         memset( &p[4], 0, 8 );
726         memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
727
728         /* Now remove all atoms before the SMI one */
729         if( p_sys->p_context->extradata_size > 0x5a &&
730             strncmp( (char*)&p[0x56], "SMI ", 4 ) )
731         {
732             uint8_t *psz = &p[0x52];
733
734             while( psz < &p[p_sys->p_context->extradata_size - 8] )
735             {
736                 int i_size = GetDWBE( psz );
737                 if( i_size <= 1 )
738                 {
739                     /* FIXME handle 1 as long size */
740                     break;
741                 }
742                 if( !strncmp( (char*)&psz[4], "SMI ", 4 ) )
743                 {
744                     memmove( &p[0x52], psz,
745                              &p[p_sys->p_context->extradata_size] - psz );
746                     break;
747                 }
748
749                 psz += i_size;
750             }
751         }
752     }
753     else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
754              p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
755              p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
756     {
757         if( p_dec->fmt_in.i_extra == 8 )
758         {
759             p_sys->p_context->extradata_size = 8;
760             p_sys->p_context->extradata = malloc( 8 );
761
762             memcpy( p_sys->p_context->extradata,
763                     p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
764             p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];
765
766             msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x",
767                       p_sys->p_context->sub_id );
768         }
769     }
770     else
771     {
772         p_sys->p_context->extradata_size = i_size;
773         p_sys->p_context->extradata =
774             malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
775         memcpy( p_sys->p_context->extradata,
776                 p_dec->fmt_in.p_extra, i_size );
777         memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
778                 0, FF_INPUT_BUFFER_PADDING_SIZE );
779     }
780 }
781
782 /*****************************************************************************
783  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
784  *                     picture_t structure (when not in direct rendering mode).
785  *****************************************************************************/
786 static void ffmpeg_CopyPicture( decoder_t *p_dec,
787                                 picture_t *p_pic, AVFrame *p_ff_pic )
788 {
789     decoder_sys_t *p_sys = p_dec->p_sys;
790
791     if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
792     {
793         int i_plane, i_size, i_line;
794         uint8_t *p_dst, *p_src;
795         int i_src_stride, i_dst_stride;
796
797         if( p_sys->p_pp && p_sys->b_pp )
798             E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
799         else
800         {
801             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
802             {
803                 p_src  = p_ff_pic->data[i_plane];
804                 p_dst = p_pic->p[i_plane].p_pixels;
805                 i_src_stride = p_ff_pic->linesize[i_plane];
806                 i_dst_stride = p_pic->p[i_plane].i_pitch;
807
808                 i_size = __MIN( i_src_stride, i_dst_stride );
809                 for( i_line = 0; i_line < p_pic->p[i_plane].i_visible_lines;
810                      i_line++ )
811                 {
812                     p_dec->p_libvlc->pf_memcpy( p_dst, p_src, i_size );
813                     p_src += i_src_stride;
814                     p_dst += i_dst_stride;
815                 }
816             }
817         }
818     }
819     else
820     {
821         AVPicture dest_pic;
822         int i;
823
824         /* we need to convert to I420 */
825         switch( p_sys->p_context->pix_fmt )
826         {
827         case PIX_FMT_YUV410P:
828         case PIX_FMT_YUV411P:
829         case PIX_FMT_BGR24:
830         case PIX_FMT_PAL8:
831             for( i = 0; i < p_pic->i_planes; i++ )
832             {
833                 dest_pic.data[i] = p_pic->p[i].p_pixels;
834                 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
835             }
836 #if !defined(HAVE_FFMPEG_SWSCALE_H) && !defined(HAVE_LIBSWSCALE_TREE)
837             img_convert( &dest_pic, PIX_FMT_YUV420P,
838                          (AVPicture *)p_ff_pic,
839                          p_sys->p_context->pix_fmt,
840                          p_sys->p_context->width,
841                          p_sys->p_context->height );
842 #endif
843             break;
844         default:
845             msg_Err( p_dec, "don't know how to convert chroma %i",
846                      p_sys->p_context->pix_fmt );
847             p_dec->b_error = 1;
848             break;
849         }
850     }
851 }
852
853 /*****************************************************************************
854  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
855  *****************************************************************************
856  * It is used for direct rendering as well as to get the right PTS for each
857  * decoded picture (even in indirect rendering mode).
858  *****************************************************************************/
859 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
860                                AVFrame *p_ff_pic )
861 {
862     decoder_t *p_dec = (decoder_t *)p_context->opaque;
863     decoder_sys_t *p_sys = p_dec->p_sys;
864     picture_t *p_pic;
865
866     /* Set picture PTS */
867     if( p_sys->input_pts )
868     {
869         p_ff_pic->pts = p_sys->input_pts;
870     }
871     else if( p_sys->input_dts )
872     {
873         /* Some demuxers only set the dts so let's try to find a useful
874          * timestamp from this */
875         if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
876             !p_ff_pic->reference || !p_sys->i_pts )
877         {
878             p_ff_pic->pts = p_sys->input_dts;
879         }
880         else p_ff_pic->pts = 0;
881     }
882     else p_ff_pic->pts = 0;
883
884     if( p_sys->i_pts ) /* make sure 1st frame has a pts > 0 */
885     {
886         p_sys->input_pts = p_sys->input_dts = 0;
887     }
888
889     p_ff_pic->opaque = 0;
890
891     /* Not much to do in indirect rendering mode */
892     if( !p_sys->b_direct_rendering || p_sys->b_pp )
893     {
894         return avcodec_default_get_buffer( p_context, p_ff_pic );
895     }
896
897     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
898      * so this check is necessary. */
899     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
900         p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
901     {
902         msg_Dbg( p_dec, "disabling direct rendering" );
903         p_sys->b_direct_rendering = 0;
904         return avcodec_default_get_buffer( p_context, p_ff_pic );
905     }
906
907     /* Get a new picture */
908     //p_sys->p_vout->render.b_allow_modify_pics = 0;
909     p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
910     if( !p_pic )
911     {
912         p_sys->b_direct_rendering = 0;
913         return avcodec_default_get_buffer( p_context, p_ff_pic );
914     }
915     p_sys->p_context->draw_horiz_band = NULL;
916
917     p_ff_pic->opaque = (void*)p_pic;
918     p_ff_pic->type = FF_BUFFER_TYPE_USER;
919     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
920     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
921     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
922     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
923
924     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
925     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
926     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
927     p_ff_pic->linesize[3] = 0;
928
929     if( p_ff_pic->reference != 0 ||
930         p_sys->i_codec_id == CODEC_ID_H264 /* Bug in libavcodec */ )
931     {
932         p_dec->pf_picture_link( p_dec, p_pic );
933     }
934
935     /* FIXME what is that, should give good value */
936     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
937
938     return 0;
939 }
940
941 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
942                                     AVFrame *p_ff_pic )
943 {
944     decoder_t *p_dec = (decoder_t *)p_context->opaque;
945     picture_t *p_pic;
946
947     if( !p_ff_pic->opaque )
948     {
949         avcodec_default_release_buffer( p_context, p_ff_pic );
950         return;
951     }
952
953     p_pic = (picture_t*)p_ff_pic->opaque;
954
955     p_ff_pic->data[0] = NULL;
956     p_ff_pic->data[1] = NULL;
957     p_ff_pic->data[2] = NULL;
958     p_ff_pic->data[3] = NULL;
959
960     if( p_ff_pic->reference != 0 ||
961         p_dec->p_sys->i_codec_id == CODEC_ID_H264 /* Bug in libavcodec */ )
962     {
963         p_dec->pf_picture_unlink( p_dec, p_pic );
964     }
965 }