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