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