]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/video.c
* include/vlc_common.h, include/vlc_es.h: new video_palette_t structure.
[vlc] / modules / codec / ffmpeg / video.c
1 /*****************************************************************************
2  * video.c: video decoder using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: video.c,v 1.62 2004/01/25 21:39:37 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
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/decoder.h>
30
31 /* ffmpeg header */
32 #ifdef HAVE_FFMPEG_AVCODEC_H
33 #   include <ffmpeg/avcodec.h>
34 #else
35 #   include <avcodec.h>
36 #endif
37
38 #include "ffmpeg.h"
39
40 /*****************************************************************************
41  * decoder_sys_t : decoder descriptor
42  *****************************************************************************/
43 struct decoder_sys_t
44 {
45     /* Common part between video and audio decoder */
46     int i_cat;
47     int i_codec_id;
48     char *psz_namecodec;
49
50     AVCodecContext      *p_context;
51     AVCodec             *p_codec;
52
53     /* Video decoder specific part */
54     mtime_t input_pts;
55     mtime_t input_dts;
56     mtime_t i_pts;
57
58     AVFrame          *p_ff_pic;
59     BITMAPINFOHEADER *p_format;
60
61     /* for frame skipping algo */
62     int b_hurry_up;
63     int i_frame_skip;
64
65     /* how many decoded frames are late */
66     int     i_late_frames;
67     mtime_t i_late_frames_start;
68
69     /* for direct rendering */
70     int b_direct_rendering;
71
72     vlc_bool_t b_has_b_frames;
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_CopyPicture    ( decoder_t *, picture_t *, AVFrame * );
91 static int  ffmpeg_GetFrameBuf    ( struct AVCodecContext *, AVFrame * );
92 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *, AVFrame * );
93
94 /*****************************************************************************
95  * Local Functions
96  *****************************************************************************/
97 static uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
98 {
99     switch( i_ff_chroma )
100     {
101     case PIX_FMT_YUV420P:
102         return VLC_FOURCC('I','4','2','0');
103     case PIX_FMT_YUV422P:
104         return VLC_FOURCC('I','4','2','2');
105     case PIX_FMT_YUV444P:
106         return VLC_FOURCC('I','4','4','4');
107
108     case PIX_FMT_YUV422:
109         return VLC_FOURCC('Y','U','Y','2');
110
111     case PIX_FMT_RGB555:
112         return VLC_FOURCC('R','V','1','5');
113     case PIX_FMT_RGB565:
114         return VLC_FOURCC('R','V','1','6');
115     case PIX_FMT_RGB24:
116         return VLC_FOURCC('R','V','2','4');
117     case PIX_FMT_RGBA32:
118         return VLC_FOURCC('R','V','3','2');
119     case PIX_FMT_GRAY8:
120         return VLC_FOURCC('G','R','E','Y');
121
122     case PIX_FMT_YUV410P:
123     case PIX_FMT_YUV411P:
124     case PIX_FMT_BGR24:
125     default:
126         return 0;
127     }
128 }
129
130 /* Returns a new picture buffer */
131 static inline picture_t *ffmpeg_NewPictBuf( decoder_t *p_dec,
132                                             AVCodecContext *p_context )
133 {
134     decoder_sys_t *p_sys = p_dec->p_sys;
135     picture_t *p_pic;
136
137     p_dec->fmt_out.video.i_width = p_context->width;
138     p_dec->fmt_out.video.i_height = p_context->height;
139     p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
140
141     if( !p_context->width || !p_context->height )
142     {
143         return NULL; /* invalid display size */
144     }
145
146     if( !p_dec->fmt_out.i_codec )
147     {
148         /* we make conversion if possible*/
149         p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
150     }
151
152     /* If an aspect-ratio was specified in the input format then force it */
153     if( p_dec->fmt_in.video.i_aspect )
154     {
155         p_dec->fmt_out.video.i_aspect = p_dec->fmt_in.video.i_aspect;
156     }
157     else
158     {
159 #if LIBAVCODEC_BUILD >= 4687
160         p_dec->fmt_out.video.i_aspect =
161             VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
162                 p_context->width / p_context->height );
163 #else
164         p_dec->fmt_out.video.i_aspect =
165             VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
166 #endif
167         if( p_dec->fmt_out.video.i_aspect == 0 )
168         {
169             p_dec->fmt_out.video.i_aspect =
170                 VOUT_ASPECT_FACTOR * p_context->width / p_context->height;
171         }
172     }
173
174     p_pic = p_dec->pf_vout_buffer_new( p_dec );
175
176 #ifdef LIBAVCODEC_PP
177     if( p_sys->p_pp && p_sys->b_pp && !p_sys->b_pp_init )
178     {
179         E_(InitPostproc)( p_dec, p_sys->p_pp, p_context->width,
180                           p_context->height, p_context->pix_fmt );
181         p_sys->b_pp_init = VLC_TRUE;
182     }
183 #endif
184
185     return p_pic;
186 }
187
188 /*****************************************************************************
189  * InitVideo: initialize the video decoder
190  *****************************************************************************
191  * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
192  * opened (done after the first decoded frame).
193  *****************************************************************************/
194 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
195                       AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
196 {
197     decoder_sys_t *p_sys;
198     vlc_value_t lockval;
199     vlc_value_t val;
200     int i_tmp;
201
202
203     var_Get( p_dec->p_libvlc, "avcodec", &lockval );
204
205     /* Allocate the memory needed to store the decoder's structure */
206     if( ( p_dec->p_sys = p_sys =
207           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
208     {
209         msg_Err( p_dec, "out of memory" );
210         return VLC_EGENERIC;
211     }
212
213     p_dec->p_sys->p_context = p_context;
214     p_dec->p_sys->p_codec = p_codec;
215     p_dec->p_sys->i_codec_id = i_codec_id;
216     p_dec->p_sys->psz_namecodec = psz_namecodec;
217     p_sys->p_ff_pic = avcodec_alloc_frame();
218
219     /* ***** Fill p_context with init values ***** */
220     p_sys->p_context->width  = p_dec->fmt_in.video.i_width;
221     p_sys->p_context->height = p_dec->fmt_in.video.i_height;
222
223     /*  ***** Get configuration of ffmpeg plugin ***** */
224     i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
225     p_sys->p_context->workaround_bugs  = __MAX( __MIN( i_tmp, 99 ), 0 );
226
227     i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
228     p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
229
230     var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
231     var_Get( p_dec, "grayscale", &val );
232     if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
233
234     /* Decide if we set CODEC_FLAG_TRUNCATED */
235     var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
236     var_Get( p_dec, "ffmpeg-truncated", &val );
237     if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
238
239     /* ***** ffmpeg frame skipping ***** */
240     var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
241     var_Get( p_dec, "ffmpeg-hurry-up", &val );
242     p_sys->b_hurry_up = val.b_bool;
243
244     /* ***** ffmpeg direct rendering ***** */
245     p_sys->b_direct_rendering = 0;
246     var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
247     var_Get( p_dec, "ffmpeg-dr", &val );
248     if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
249         ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
250         /* Apparently direct rendering doesn't work with YUV422P */
251         p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
252         !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
253     {
254         /* Some codecs set pix_fmt only after the 1st frame has been decoded,
255          * so we need to do another check in ffmpeg_GetFrameBuf() */
256         p_sys->b_direct_rendering = 1;
257     }
258
259 #ifdef LIBAVCODEC_PP
260     p_sys->p_pp = NULL;
261     p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
262     p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
263 #endif
264
265     /* ffmpeg doesn't properly release old pictures when frames are skipped */
266     //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
267     if( p_sys->b_direct_rendering )
268     {
269         msg_Dbg( p_dec, "using direct rendering" );
270         p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
271     }
272
273     /* Always use our get_buffer wrapper so we can calculate the
274      * PTS correctly */
275     p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
276     p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
277     p_sys->p_context->opaque = p_dec;
278
279     /* ***** init this codec with special data ***** */
280     if( p_dec->fmt_in.i_extra )
281     {
282         int i_size = p_dec->fmt_in.i_extra;
283
284         if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
285         {
286             uint8_t *p;
287
288             p_sys->p_context->extradata_size = i_size + 12;
289             p = p_sys->p_context->extradata  =
290                 malloc( p_sys->p_context->extradata_size );
291
292             memcpy( &p[0],  "SVQ3", 4 );
293             memset( &p[4], 0, 8 );
294             memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
295         }
296         else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
297                  p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
298                  p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
299         {
300             if( p_dec->fmt_in.i_extra == 8 )
301             {
302                 p_sys->p_context->extradata_size = 8;
303                 p_sys->p_context->extradata = malloc( 8 );
304
305                 memcpy( p_sys->p_context->extradata,
306                         p_dec->fmt_in.p_extra,
307                         p_dec->fmt_in.i_extra );
308                 p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];
309
310                 msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x", p_sys->p_context->sub_id );
311             }
312         }
313         else
314         {
315             p_sys->p_context->extradata_size = i_size;
316             p_sys->p_context->extradata =
317                 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
318             memcpy( p_sys->p_context->extradata,
319                     p_dec->fmt_in.p_extra, i_size );
320             memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
321                     0, FF_INPUT_BUFFER_PADDING_SIZE );
322         }
323     }
324
325     /* ***** misc init ***** */
326     p_sys->input_pts = p_sys->input_dts = 0;
327     p_sys->i_pts = 0;
328     p_sys->b_has_b_frames = VLC_FALSE;
329     p_sys->i_late_frames = 0;
330     p_sys->i_buffer = 0;
331     p_sys->i_buffer_orig = 1;
332     p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );
333
334     /* Set output properties */
335     p_dec->fmt_out.i_cat = VIDEO_ES;
336     p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
337
338     /* Setup palette */
339 #if LIBAVCODEC_BUILD >= 4688
340     if( p_dec->fmt_in.video.p_palette )
341         p_sys->p_context->palctrl =
342             (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
343     else
344         p_sys->p_context->palctrl = &palette_control;
345 #endif
346
347     /* ***** Open the codec ***** */
348     vlc_mutex_lock( lockval.p_address );
349     if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
350     {
351         vlc_mutex_unlock( lockval.p_address );
352         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
353         return VLC_EGENERIC;
354     }
355     vlc_mutex_unlock( lockval.p_address );
356     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
357
358
359     return VLC_SUCCESS;
360 }
361
362 /*****************************************************************************
363  * DecodeVideo: Called to decode one or more frames
364  *****************************************************************************/
365 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
366 {
367     decoder_sys_t *p_sys = p_dec->p_sys;
368     int b_drawpicture;
369     block_t *p_block;
370
371     if( !pp_block || !*pp_block ) return NULL;
372
373     p_block = *pp_block;
374
375     if( p_block->b_discontinuity )
376     {
377         p_sys->i_buffer = 0;
378         p_sys->i_pts = 0; /* To make sure we recover properly */
379
380         block_Release( p_block );
381         return NULL;
382     }
383
384     if( p_sys->i_late_frames > 0 &&
385         mdate() - p_sys->i_late_frames_start > I64C(5000000) )
386     {
387         if( p_sys->i_pts )
388         {
389             msg_Err( p_dec, "more than 5 seconds of late video -> "
390                      "dropping frame (computer too slow ?)" );
391             p_sys->i_pts = 0; /* To make sure we recover properly */
392         }
393         block_Release( p_block );
394         p_sys->i_late_frames--;
395         return NULL;
396     }
397
398     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
399     {
400         p_sys->input_pts = p_block->i_pts;
401         p_sys->input_dts = p_block->i_dts;
402
403         /* Make sure we don't reuse the same timestamps twice */
404         p_block->i_pts = p_block->i_dts = 0;
405     }
406
407     /* TODO implement it in a better way */
408     /* A good idea could be to decode all I pictures and see for the other */
409     if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
410     {
411         b_drawpicture = 0;
412         if( p_sys->i_late_frames < 8 )
413         {
414             p_sys->p_context->hurry_up = 2;
415         }
416         else
417         {
418             /* picture too late, won't decode
419              * but break picture until a new I, and for mpeg4 ...*/
420
421             p_sys->i_late_frames--; /* needed else it will never be decrease */
422             block_Release( p_block );
423             p_sys->i_buffer = 0;
424             return NULL;
425         }
426     }
427     else
428     {
429         b_drawpicture = 1;
430         p_sys->p_context->hurry_up = 0;
431     }
432
433
434     if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
435     {
436         p_sys->p_context->hurry_up = 5;
437     }
438
439     /*
440      * Do the actual decoding now
441      */
442
443     /* Check if post-processing was enabled */
444     p_sys->b_pp = p_sys->b_pp_async;
445
446     /* Don't forget that ffmpeg requires a little more bytes
447      * that the real frame size */
448     if( p_block->i_buffer > 0 )
449     {
450         p_sys->i_buffer = p_block->i_buffer;
451         if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
452             p_sys->i_buffer_orig )
453         {
454             free( p_sys->p_buffer_orig );
455             p_sys->i_buffer_orig =
456                 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
457             p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
458         }
459         p_sys->p_buffer = p_sys->p_buffer_orig;
460         p_sys->i_buffer = p_block->i_buffer;
461         p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
462                                  p_block->i_buffer );
463         memset( p_sys->p_buffer + p_block->i_buffer, 0,
464                 FF_INPUT_BUFFER_PADDING_SIZE );
465
466         p_block->i_buffer = 0;
467     }
468
469     while( p_sys->i_buffer > 0 )
470     {
471         int i_used, b_gotpicture;
472         picture_t *p_pic;
473
474         i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
475                                        &b_gotpicture,
476                                        p_sys->p_buffer, p_sys->i_buffer );
477         if( i_used < 0 )
478         {
479             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
480                       p_sys->i_buffer );
481             block_Release( p_block );
482             return NULL;
483         }
484         else if( i_used > p_sys->i_buffer )
485         {
486             i_used = p_sys->i_buffer;
487         }
488
489         /* Consumed bytes */
490         p_sys->i_buffer -= i_used;
491         p_sys->p_buffer += i_used;
492
493         /* Nothing to display */
494         if( !b_gotpicture )
495         {
496             if( i_used == 0 )
497             {
498                 break;
499             }
500             continue;
501         }
502
503         /* Update frame late count*/
504         if( p_sys->i_pts && p_sys->i_pts <= mdate() )
505         {
506             p_sys->i_late_frames++;
507             if( p_sys->i_late_frames == 1 )
508                 p_sys->i_late_frames_start = mdate();
509         }
510         else
511         {
512             p_sys->i_late_frames = 0;
513         }
514
515         if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
516         {
517             /* Do not display the picture */
518             continue;
519         }
520
521         if( !p_sys->b_direct_rendering || p_sys->b_pp )
522         {
523             /* Get a new picture */
524             p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
525             if( !p_pic )
526             {
527                 block_Release( p_block );
528                 return NULL;
529             }
530
531             /* Fill p_picture_t from AVVideoFrame and do chroma conversion
532              * if needed */
533             ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
534         }
535         else
536         {
537             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
538         }
539
540         /* Set the PTS */
541         if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
542
543         /* Sanity check (seems to be needed for some streams ) */
544         if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
545         {
546             p_sys->b_has_b_frames = VLC_TRUE;
547         }
548
549         /* Send decoded frame to vout */
550         if( p_sys->i_pts )
551         {
552             p_pic->date = p_sys->i_pts;
553
554             /* interpolate the next PTS */
555             if( p_sys->p_context->frame_rate > 0 )
556             {
557                 p_sys->i_pts += I64C(1000000) *
558                     (2 + p_sys->p_ff_pic->repeat_pict) *
559                     p_sys->p_context->frame_rate_base /
560                     (2 * p_sys->p_context->frame_rate);
561             }
562             return p_pic;
563         }
564         else
565         {
566             p_dec->pf_vout_buffer_del( p_dec, p_pic );
567         }
568     }
569
570     block_Release( p_block );
571     return NULL;
572 }
573
574 /*****************************************************************************
575  * EndVideo: decoder destruction
576  *****************************************************************************
577  * This function is called when the thread ends after a sucessful
578  * initialization.
579  *****************************************************************************/
580 void E_(EndVideoDec)( decoder_t *p_dec )
581 {
582     decoder_sys_t *p_sys = p_dec->p_sys;
583
584     if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
585
586 #ifdef LIBAVCODEC_PP
587     E_(ClosePostproc)( p_dec, p_sys->p_pp );
588 #endif
589
590     free( p_sys->p_buffer_orig );
591 }
592
593 /*****************************************************************************
594  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
595  *                     picture_t structure (when not in direct rendering mode).
596  *****************************************************************************/
597 static void ffmpeg_CopyPicture( decoder_t *p_dec,
598                                 picture_t *p_pic, AVFrame *p_ff_pic )
599 {
600     decoder_sys_t *p_sys = p_dec->p_sys;
601
602     if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
603     {
604         int i_plane, i_size, i_line;
605         uint8_t *p_dst, *p_src;
606         int i_src_stride, i_dst_stride;
607
608 #ifdef LIBAVCODEC_PP
609         if( p_sys->p_pp && p_sys->b_pp )
610             E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
611         else
612 #endif
613         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
614         {
615             p_src  = p_ff_pic->data[i_plane];
616             p_dst = p_pic->p[i_plane].p_pixels;
617             i_src_stride = p_ff_pic->linesize[i_plane];
618             i_dst_stride = p_pic->p[i_plane].i_pitch;
619
620             i_size = __MIN( i_src_stride, i_dst_stride );
621             for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
622             {
623                 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
624                 p_src += i_src_stride;
625                 p_dst += i_dst_stride;
626             }
627         }
628     }
629     else
630     {
631         AVPicture dest_pic;
632         int i;
633
634         /* we need to convert to I420 */
635         switch( p_sys->p_context->pix_fmt )
636         {
637         case PIX_FMT_YUV410P:
638         case PIX_FMT_YUV411P:
639         case PIX_FMT_PAL8:
640             for( i = 0; i < p_pic->i_planes; i++ )
641             {
642                 dest_pic.data[i] = p_pic->p[i].p_pixels;
643                 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
644             }
645             img_convert( &dest_pic, PIX_FMT_YUV420P,
646                          (AVPicture *)p_ff_pic,
647                          p_sys->p_context->pix_fmt,
648                          p_sys->p_context->width,
649                          p_sys->p_context->height );
650             break;
651         default:
652             msg_Err( p_dec, "don't know how to convert chroma %i",
653                      p_sys->p_context->pix_fmt );
654             p_dec->b_error = 1;
655             break;
656         }
657     }
658 }
659
660 /*****************************************************************************
661  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
662  *****************************************************************************
663  * It is used for direct rendering as well as to get the right PTS for each
664  * decoded picture (even in indirect rendering mode).
665  *****************************************************************************/
666 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
667                                AVFrame *p_ff_pic )
668 {
669     decoder_t *p_dec = (decoder_t *)p_context->opaque;
670     decoder_sys_t *p_sys = p_dec->p_sys;
671     picture_t *p_pic;
672
673     /* Set picture PTS */
674     if( p_sys->input_pts )
675     {
676         p_ff_pic->pts = p_sys->input_pts;
677     }
678     else if( p_sys->input_dts )
679     {
680         /* Some demuxers only set the dts so let's try to find a useful
681          * timestamp from this */
682         if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
683             !p_ff_pic->reference )
684         {
685             p_ff_pic->pts = p_sys->input_dts;
686         }
687         else p_ff_pic->pts = 0;
688     }
689     else p_ff_pic->pts = 0;
690
691     p_sys->input_pts = p_sys->input_dts = 0;
692
693     /* Not much to do in indirect rendering mode */
694     if( !p_sys->b_direct_rendering || p_sys->b_pp )
695     {
696         return avcodec_default_get_buffer( p_context, p_ff_pic );
697     }
698
699     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
700      * so this check is necessary. */
701     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
702         p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
703     {
704         msg_Dbg( p_dec, "disabling direct rendering" );
705         p_sys->b_direct_rendering = 0;
706         return avcodec_default_get_buffer( p_context, p_ff_pic );
707     }
708
709     /* Get a new picture */
710     //p_sys->p_vout->render.b_allow_modify_pics = 0;
711     p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
712     if( !p_pic )
713     {
714         p_sys->b_direct_rendering = 0;
715         return avcodec_default_get_buffer( p_context, p_ff_pic );
716     }
717     p_sys->p_context->draw_horiz_band = NULL;
718
719     p_ff_pic->opaque = (void*)p_pic;
720     p_ff_pic->type = FF_BUFFER_TYPE_USER;
721     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
722     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
723     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
724     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
725
726     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
727     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
728     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
729     p_ff_pic->linesize[3] = 0;
730
731     if( p_ff_pic->reference != 0 )
732     {
733         p_dec->pf_picture_link( p_dec, p_pic );
734     }
735
736     /* FIXME what is that, should give good value */
737     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
738
739     return 0;
740 }
741
742 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
743                                     AVFrame *p_ff_pic )
744 {
745     decoder_t *p_dec = (decoder_t *)p_context->opaque;
746     picture_t *p_pic;
747
748     if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
749     {
750         avcodec_default_release_buffer( p_context, p_ff_pic );
751         return;
752     }
753
754     p_pic = (picture_t*)p_ff_pic->opaque;
755
756     p_ff_pic->data[0] = NULL;
757     p_ff_pic->data[1] = NULL;
758     p_ff_pic->data[2] = NULL;
759     p_ff_pic->data[3] = NULL;
760
761     if( p_ff_pic->reference != 0 )
762     {
763         p_dec->pf_picture_unlink( p_dec, p_pic );
764     }
765 }