]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/video.c
* all: removed block_t->b_discontinuity,b_frame_* and added i_flags
[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.67 2004/02/25 17:48:52 fenrir 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     p_sys->p_context->bits_per_sample = p_dec->fmt_in.video.i_bits_per_pixel;
223
224     /*  ***** Get configuration of ffmpeg plugin ***** */
225     i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
226     p_sys->p_context->workaround_bugs  = __MAX( __MIN( i_tmp, 99 ), 0 );
227
228     i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
229     p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
230
231     var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
232     var_Get( p_dec, "grayscale", &val );
233     if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
234
235     /* Decide if we set CODEC_FLAG_TRUNCATED */
236     var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
237     var_Get( p_dec, "ffmpeg-truncated", &val );
238     if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
239
240     /* ***** ffmpeg frame skipping ***** */
241     var_Create( p_dec, "ffmpeg-hurry-up", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
242     var_Get( p_dec, "ffmpeg-hurry-up", &val );
243     p_sys->b_hurry_up = val.b_bool;
244
245     /* ***** ffmpeg direct rendering ***** */
246     p_sys->b_direct_rendering = 0;
247     var_Create( p_dec, "ffmpeg-dr", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248     var_Get( p_dec, "ffmpeg-dr", &val );
249     if( val.b_bool && (p_sys->p_codec->capabilities & CODEC_CAP_DR1) &&
250         ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) &&
251         /* Apparently direct rendering doesn't work with YUV422P */
252         p_sys->p_context->pix_fmt != PIX_FMT_YUV422P &&
253         !(p_sys->p_context->width % 16) && !(p_sys->p_context->height % 16) )
254     {
255         /* Some codecs set pix_fmt only after the 1st frame has been decoded,
256          * so we need to do another check in ffmpeg_GetFrameBuf() */
257         p_sys->b_direct_rendering = 1;
258     }
259
260 #ifdef LIBAVCODEC_PP
261     p_sys->p_pp = NULL;
262     p_sys->b_pp = p_sys->b_pp_async = p_sys->b_pp_init = VLC_FALSE;
263     p_sys->p_pp = E_(OpenPostproc)( p_dec, &p_sys->b_pp_async );
264 #endif
265
266     /* ffmpeg doesn't properly release old pictures when frames are skipped */
267     //if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
268     if( p_sys->b_direct_rendering )
269     {
270         msg_Dbg( p_dec, "using direct rendering" );
271         p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
272     }
273
274     /* Always use our get_buffer wrapper so we can calculate the
275      * PTS correctly */
276     p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
277     p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
278     p_sys->p_context->opaque = p_dec;
279
280     /* ***** init this codec with special data ***** */
281     if( p_dec->fmt_in.i_extra )
282     {
283         int i_size = p_dec->fmt_in.i_extra;
284
285         if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
286         {
287             uint8_t *p;
288
289             p_sys->p_context->extradata_size = i_size + 12;
290             p = p_sys->p_context->extradata  =
291                 malloc( p_sys->p_context->extradata_size );
292
293             memcpy( &p[0],  "SVQ3", 4 );
294             memset( &p[4], 0, 8 );
295             memcpy( &p[12], p_dec->fmt_in.p_extra, i_size );
296         }
297         else if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '0' ) ||
298                  p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '1', '3' ) ||
299                  p_dec->fmt_in.i_codec == VLC_FOURCC( 'R', 'V', '2', '0' ) )
300         {
301             if( p_dec->fmt_in.i_extra == 8 )
302             {
303                 p_sys->p_context->extradata_size = 8;
304                 p_sys->p_context->extradata = malloc( 8 );
305
306                 memcpy( p_sys->p_context->extradata,
307                         p_dec->fmt_in.p_extra,
308                         p_dec->fmt_in.i_extra );
309                 p_sys->p_context->sub_id= ((uint32_t*)p_dec->fmt_in.p_extra)[1];
310
311                 msg_Warn( p_dec, "using extra data for RV codec sub_id=%08x", p_sys->p_context->sub_id );
312             }
313         }
314         else
315         {
316             p_sys->p_context->extradata_size = i_size;
317             p_sys->p_context->extradata =
318                 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
319             memcpy( p_sys->p_context->extradata,
320                     p_dec->fmt_in.p_extra, i_size );
321             memset( &((uint8_t*)p_sys->p_context->extradata)[i_size],
322                     0, FF_INPUT_BUFFER_PADDING_SIZE );
323         }
324     }
325
326     /* ***** misc init ***** */
327     p_sys->input_pts = p_sys->input_dts = 0;
328     p_sys->i_pts = 0;
329     p_sys->b_has_b_frames = VLC_FALSE;
330     p_sys->i_late_frames = 0;
331     p_sys->i_buffer = 0;
332     p_sys->i_buffer_orig = 1;
333     p_sys->p_buffer_orig = p_sys->p_buffer = malloc( p_sys->i_buffer );
334
335     /* Set output properties */
336     p_dec->fmt_out.i_cat = VIDEO_ES;
337     p_dec->fmt_out.i_codec = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
338
339     /* Setup palette */
340 #if LIBAVCODEC_BUILD >= 4688
341     if( p_dec->fmt_in.video.p_palette )
342         p_sys->p_context->palctrl =
343             (AVPaletteControl *)p_dec->fmt_in.video.p_palette;
344     else
345         p_sys->p_context->palctrl = &palette_control;
346 #endif
347
348     /* ***** Open the codec ***** */
349     vlc_mutex_lock( lockval.p_address );
350     if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
351     {
352         vlc_mutex_unlock( lockval.p_address );
353         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
354         return VLC_EGENERIC;
355     }
356     vlc_mutex_unlock( lockval.p_address );
357     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
358
359
360     return VLC_SUCCESS;
361 }
362
363 /*****************************************************************************
364  * DecodeVideo: Called to decode one or more frames
365  *****************************************************************************/
366 picture_t *E_(DecodeVideo)( decoder_t *p_dec, block_t **pp_block )
367 {
368     decoder_sys_t *p_sys = p_dec->p_sys;
369     int b_drawpicture;
370     block_t *p_block;
371
372     if( !pp_block || !*pp_block ) return NULL;
373
374     p_block = *pp_block;
375
376     if( p_block->i_flags&BLOCK_FLAG_DISCONTINUITY )
377     {
378         p_sys->i_buffer = 0;
379         p_sys->i_pts = 0; /* To make sure we recover properly */
380
381         block_Release( p_block );
382         return NULL;
383     }
384
385     if( p_sys->i_late_frames > 0 &&
386         mdate() - p_sys->i_late_frames_start > I64C(5000000) )
387     {
388         if( p_sys->i_pts )
389         {
390             msg_Err( p_dec, "more than 5 seconds of late video -> "
391                      "dropping frame (computer too slow ?)" );
392             p_sys->i_pts = 0; /* To make sure we recover properly */
393         }
394         block_Release( p_block );
395         p_sys->i_late_frames--;
396         return NULL;
397     }
398
399     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
400     {
401         p_sys->input_pts = p_block->i_pts;
402         p_sys->input_dts = p_block->i_dts;
403
404         /* Make sure we don't reuse the same timestamps twice */
405         p_block->i_pts = p_block->i_dts = 0;
406     }
407
408     /* TODO implement it in a better way */
409     /* A good idea could be to decode all I pictures and see for the other */
410     if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
411     {
412         b_drawpicture = 0;
413         if( p_sys->i_late_frames < 8 )
414         {
415             p_sys->p_context->hurry_up = 2;
416         }
417         else
418         {
419             /* picture too late, won't decode
420              * but break picture until a new I, and for mpeg4 ...*/
421
422             p_sys->i_late_frames--; /* needed else it will never be decrease */
423             block_Release( p_block );
424             p_sys->i_buffer = 0;
425             return NULL;
426         }
427     }
428     else
429     {
430         b_drawpicture = 1;
431         p_sys->p_context->hurry_up = 0;
432     }
433
434
435     if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
436     {
437         p_sys->p_context->hurry_up = 5;
438     }
439
440     /*
441      * Do the actual decoding now
442      */
443
444     /* Check if post-processing was enabled */
445     p_sys->b_pp = p_sys->b_pp_async;
446
447     /* Don't forget that ffmpeg requires a little more bytes
448      * that the real frame size */
449     if( p_block->i_buffer > 0 )
450     {
451         p_sys->i_buffer = p_block->i_buffer;
452         if( p_sys->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE >
453             p_sys->i_buffer_orig )
454         {
455             free( p_sys->p_buffer_orig );
456             p_sys->i_buffer_orig =
457                 p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
458             p_sys->p_buffer_orig = malloc( p_sys->i_buffer_orig );
459         }
460         p_sys->p_buffer = p_sys->p_buffer_orig;
461         p_sys->i_buffer = p_block->i_buffer;
462         p_dec->p_vlc->pf_memcpy( p_sys->p_buffer, p_block->p_buffer,
463                                  p_block->i_buffer );
464         memset( p_sys->p_buffer + p_block->i_buffer, 0,
465                 FF_INPUT_BUFFER_PADDING_SIZE );
466
467         p_block->i_buffer = 0;
468     }
469
470     while( p_sys->i_buffer > 0 )
471     {
472         int i_used, b_gotpicture;
473         picture_t *p_pic;
474
475         i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
476                                        &b_gotpicture,
477                                        p_sys->p_buffer, p_sys->i_buffer );
478         if( i_used < 0 )
479         {
480             msg_Warn( p_dec, "cannot decode one frame (%d bytes)",
481                       p_sys->i_buffer );
482             block_Release( p_block );
483             return NULL;
484         }
485         else if( i_used > p_sys->i_buffer )
486         {
487             i_used = p_sys->i_buffer;
488         }
489
490         /* Consumed bytes */
491         p_sys->i_buffer -= i_used;
492         p_sys->p_buffer += i_used;
493
494         /* Nothing to display */
495         if( !b_gotpicture )
496         {
497             if( i_used == 0 )
498             {
499                 break;
500             }
501             continue;
502         }
503
504         /* Update frame late count*/
505         if( p_sys->i_pts && p_sys->i_pts <= mdate() )
506         {
507             p_sys->i_late_frames++;
508             if( p_sys->i_late_frames == 1 )
509                 p_sys->i_late_frames_start = mdate();
510         }
511         else
512         {
513             p_sys->i_late_frames = 0;
514         }
515
516         if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
517         {
518             /* Do not display the picture */
519             continue;
520         }
521
522         if( !p_sys->p_ff_pic->opaque )
523         {
524             /* Get a new picture */
525             p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
526             if( !p_pic )
527             {
528                 block_Release( p_block );
529                 return NULL;
530             }
531
532             /* Fill p_picture_t from AVVideoFrame and do chroma conversion
533              * if needed */
534             ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
535         }
536         else
537         {
538             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
539         }
540
541         /* Set the PTS */
542         if( p_sys->p_ff_pic->pts ) p_sys->i_pts = p_sys->p_ff_pic->pts;
543
544         /* Sanity check (seems to be needed for some streams ) */
545         if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
546         {
547             p_sys->b_has_b_frames = VLC_TRUE;
548         }
549
550         /* Send decoded frame to vout */
551         if( p_sys->i_pts )
552         {
553             p_pic->date = p_sys->i_pts;
554
555             /* interpolate the next PTS */
556             if( p_sys->p_context->frame_rate > 0 )
557             {
558                 p_sys->i_pts += I64C(1000000) *
559                     (2 + p_sys->p_ff_pic->repeat_pict) *
560                     p_sys->p_context->frame_rate_base /
561                     (2 * p_sys->p_context->frame_rate);
562             }
563             return p_pic;
564         }
565         else
566         {
567             p_dec->pf_vout_buffer_del( p_dec, p_pic );
568         }
569     }
570
571     block_Release( p_block );
572     return NULL;
573 }
574
575 /*****************************************************************************
576  * EndVideo: decoder destruction
577  *****************************************************************************
578  * This function is called when the thread ends after a sucessful
579  * initialization.
580  *****************************************************************************/
581 void E_(EndVideoDec)( decoder_t *p_dec )
582 {
583     decoder_sys_t *p_sys = p_dec->p_sys;
584
585     if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
586
587 #ifdef LIBAVCODEC_PP
588     E_(ClosePostproc)( p_dec, p_sys->p_pp );
589 #endif
590
591     free( p_sys->p_buffer_orig );
592 }
593
594 /*****************************************************************************
595  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
596  *                     picture_t structure (when not in direct rendering mode).
597  *****************************************************************************/
598 static void ffmpeg_CopyPicture( decoder_t *p_dec,
599                                 picture_t *p_pic, AVFrame *p_ff_pic )
600 {
601     decoder_sys_t *p_sys = p_dec->p_sys;
602
603     if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
604     {
605         int i_plane, i_size, i_line;
606         uint8_t *p_dst, *p_src;
607         int i_src_stride, i_dst_stride;
608
609 #ifdef LIBAVCODEC_PP
610         if( p_sys->p_pp && p_sys->b_pp )
611             E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
612         else
613 #endif
614         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
615         {
616             p_src  = p_ff_pic->data[i_plane];
617             p_dst = p_pic->p[i_plane].p_pixels;
618             i_src_stride = p_ff_pic->linesize[i_plane];
619             i_dst_stride = p_pic->p[i_plane].i_pitch;
620
621             i_size = __MIN( i_src_stride, i_dst_stride );
622             for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
623             {
624                 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
625                 p_src += i_src_stride;
626                 p_dst += i_dst_stride;
627             }
628         }
629     }
630     else
631     {
632         AVPicture dest_pic;
633         int i;
634
635         /* we need to convert to I420 */
636         switch( p_sys->p_context->pix_fmt )
637         {
638         case PIX_FMT_YUV410P:
639         case PIX_FMT_YUV411P:
640         case PIX_FMT_PAL8:
641             for( i = 0; i < p_pic->i_planes; i++ )
642             {
643                 dest_pic.data[i] = p_pic->p[i].p_pixels;
644                 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
645             }
646             img_convert( &dest_pic, PIX_FMT_YUV420P,
647                          (AVPicture *)p_ff_pic,
648                          p_sys->p_context->pix_fmt,
649                          p_sys->p_context->width,
650                          p_sys->p_context->height );
651             break;
652         default:
653             msg_Err( p_dec, "don't know how to convert chroma %i",
654                      p_sys->p_context->pix_fmt );
655             p_dec->b_error = 1;
656             break;
657         }
658     }
659 }
660
661 /*****************************************************************************
662  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
663  *****************************************************************************
664  * It is used for direct rendering as well as to get the right PTS for each
665  * decoded picture (even in indirect rendering mode).
666  *****************************************************************************/
667 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
668                                AVFrame *p_ff_pic )
669 {
670     decoder_t *p_dec = (decoder_t *)p_context->opaque;
671     decoder_sys_t *p_sys = p_dec->p_sys;
672     picture_t *p_pic;
673
674     /* Set picture PTS */
675     if( p_sys->input_pts )
676     {
677         p_ff_pic->pts = p_sys->input_pts;
678     }
679     else if( p_sys->input_dts )
680     {
681         /* Some demuxers only set the dts so let's try to find a useful
682          * timestamp from this */
683         if( !p_context->has_b_frames || !p_sys->b_has_b_frames ||
684             !p_ff_pic->reference )
685         {
686             p_ff_pic->pts = p_sys->input_dts;
687         }
688         else p_ff_pic->pts = 0;
689     }
690     else p_ff_pic->pts = 0;
691
692     p_sys->input_pts = p_sys->input_dts = 0;
693     p_ff_pic->opaque = 0;
694
695     /* Not much to do in indirect rendering mode */
696     if( !p_sys->b_direct_rendering || p_sys->b_pp )
697     {
698         return avcodec_default_get_buffer( p_context, p_ff_pic );
699     }
700
701     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
702      * so this check is necessary. */
703     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
704         p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
705     {
706         msg_Dbg( p_dec, "disabling direct rendering" );
707         p_sys->b_direct_rendering = 0;
708         return avcodec_default_get_buffer( p_context, p_ff_pic );
709     }
710
711     /* Get a new picture */
712     //p_sys->p_vout->render.b_allow_modify_pics = 0;
713     p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
714     if( !p_pic )
715     {
716         p_sys->b_direct_rendering = 0;
717         return avcodec_default_get_buffer( p_context, p_ff_pic );
718     }
719     p_sys->p_context->draw_horiz_band = NULL;
720
721     p_ff_pic->opaque = (void*)p_pic;
722     p_ff_pic->type = FF_BUFFER_TYPE_USER;
723     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
724     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
725     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
726     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
727
728     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
729     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
730     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
731     p_ff_pic->linesize[3] = 0;
732
733     if( p_ff_pic->reference != 0 )
734     {
735         p_dec->pf_picture_link( p_dec, p_pic );
736     }
737
738     /* FIXME what is that, should give good value */
739     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
740
741     return 0;
742 }
743
744 static void ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
745                                     AVFrame *p_ff_pic )
746 {
747     decoder_t *p_dec = (decoder_t *)p_context->opaque;
748     picture_t *p_pic;
749
750     if( !p_ff_pic->opaque )
751     {
752         avcodec_default_release_buffer( p_context, p_ff_pic );
753         return;
754     }
755
756     p_pic = (picture_t*)p_ff_pic->opaque;
757
758     p_ff_pic->data[0] = NULL;
759     p_ff_pic->data[1] = NULL;
760     p_ff_pic->data[2] = NULL;
761     p_ff_pic->data[3] = NULL;
762
763     if( p_ff_pic->reference != 0 )
764     {
765         p_dec->pf_picture_unlink( p_dec, p_pic );
766     }
767 }