]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/video.c
1349a26c4cd0749030d3f3cfe09043e52cfafb69
[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.43 2003/10/28 14:17:52 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 <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/vout.h>
33 #include <vlc/decoder.h>
34 #include <vlc/input.h>
35
36 #ifdef HAVE_SYS_TIMES_H
37 #   include <sys/times.h>
38 #endif
39
40 /* ffmpeg header */
41 #ifdef HAVE_FFMPEG_AVCODEC_H
42 #   include <ffmpeg/avcodec.h>
43 #else
44 #   include <avcodec.h>
45 #endif
46
47 #include "ffmpeg.h"
48
49 /*****************************************************************************
50  * decoder_sys_t : decoder descriptor
51  *****************************************************************************/
52 struct decoder_sys_t
53 {
54     /* Common part between video and audio decoder */
55     int i_cat;
56     int i_codec_id;
57     char *psz_namecodec;
58
59     AVCodecContext      *p_context;
60     AVCodec             *p_codec;
61
62     /* Video decoder specific part */
63     mtime_t input_pts;
64     mtime_t i_pts;
65
66     AVFrame          *p_ff_pic;
67     BITMAPINFOHEADER *p_format;
68
69     vout_thread_t    *p_vout;
70
71     /* for frame skipping algo */
72     int b_hurry_up;
73     int i_frame_error;
74     int i_frame_skip;
75
76     /* how many decoded frames are late */
77     int     i_late_frames;
78     mtime_t i_late_frames_start;
79
80     /* for direct rendering */
81     int b_direct_rendering;
82
83     vlc_bool_t b_has_b_frames;
84
85     int i_buffer;
86     char *p_buffer;
87
88     /* Postprocessing handle */
89     void *p_pp;
90 };
91
92 /*****************************************************************************
93  * Local prototypes
94  *****************************************************************************/
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 /*****************************************************************************
100  * Local Functions
101  *****************************************************************************/
102 static inline uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
103 {
104     /* FIXME FIXME some of them are wrong */
105     switch( i_ff_chroma )
106     {
107     case PIX_FMT_YUV420P:
108     case PIX_FMT_YUV422:
109         return( VLC_FOURCC('I','4','2','0') );
110     case PIX_FMT_RGB24:
111         return( VLC_FOURCC('R','V','2','4') );
112     case PIX_FMT_YUV422P:
113         return( VLC_FOURCC('I','4','2','2') );
114     case PIX_FMT_YUV444P:
115         return( VLC_FOURCC('I','4','4','4') );
116     case PIX_FMT_YUV410P:
117     case PIX_FMT_YUV411P:
118     case PIX_FMT_BGR24:
119     default:
120         return 0;
121     }
122 }
123
124 /* Return a Vout */
125 static vout_thread_t *ffmpeg_CreateVout( decoder_t  *p_dec,
126                                          AVCodecContext *p_context )
127 {
128     vout_thread_t *p_vout;
129     unsigned int   i_width = p_context->width;
130     unsigned int   i_height = p_context->height;
131     uint32_t       i_chroma = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
132     unsigned int   i_aspect;
133
134     if( !i_width || !i_height )
135     {
136         return( NULL ); /* Can't create a new vout without display size */
137     }
138
139     if( !i_chroma )
140     {
141         /* we make conversion if possible*/
142         i_chroma = VLC_FOURCC('I','4','2','0');
143     }
144
145 #if LIBAVCODEC_BUILD >= 4687
146     i_aspect = VOUT_ASPECT_FACTOR * ( av_q2d(p_context->sample_aspect_ratio) *
147         p_context->width / p_context->height );
148 #else
149     i_aspect = VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
150 #endif
151     if( i_aspect == 0 )
152     {
153         i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
154     }
155
156     /* Spawn a video output if there is none. First we look for our children,
157      * then we look for any other vout that might be available. */
158     p_vout = vout_Request( p_dec, p_dec->p_sys->p_vout,
159                            i_width, i_height, i_chroma, i_aspect );
160
161 #ifdef LIBAVCODEC_PP
162     if( p_dec->p_sys->p_pp )
163         E_(InitPostproc)( p_dec, p_dec->p_sys->p_pp, i_width, i_height,
164                           p_context->pix_fmt );
165 #endif
166
167     return p_vout;
168 }
169
170 /*****************************************************************************
171  * InitVideo: initialize the video decoder
172  *****************************************************************************
173  * the ffmpeg codec will be opened, some memory allocated. The vout is not yet
174  * opened (done after the first decoded frame).
175  *****************************************************************************/
176 int E_(InitVideoDec)( decoder_t *p_dec, AVCodecContext *p_context,
177                       AVCodec *p_codec, int i_codec_id, char *psz_namecodec )
178 {
179     decoder_sys_t *p_sys;
180     vlc_value_t val;
181     int i_tmp;
182
183     /* Allocate the memory needed to store the decoder's structure */
184     if( ( p_dec->p_sys = p_sys =
185           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
186     {
187         msg_Err( p_dec, "out of memory" );
188         return VLC_EGENERIC;
189     }
190
191     p_dec->p_sys->p_context = p_context;
192     p_dec->p_sys->p_codec = p_codec;
193     p_dec->p_sys->i_codec_id = i_codec_id;
194     p_dec->p_sys->psz_namecodec = psz_namecodec;
195     p_sys->p_ff_pic = avcodec_alloc_frame();
196
197     if( ( p_sys->p_format =
198           (BITMAPINFOHEADER *)p_dec->p_fifo->p_bitmapinfoheader ) != NULL )
199     {
200         /* ***** Fill p_context with init values ***** */
201         p_sys->p_context->width  = p_sys->p_format->biWidth;
202         p_sys->p_context->height = p_sys->p_format->biHeight;
203     }
204     else
205     {
206         msg_Warn( p_dec, "display informations missing" );
207         p_sys->p_format = NULL;
208     }
209
210     /*  ***** Get configuration of ffmpeg plugin ***** */
211     i_tmp = config_GetInt( p_dec, "ffmpeg-workaround-bugs" );
212     p_sys->p_context->workaround_bugs  = __MAX( __MIN( i_tmp, 99 ), 0 );
213
214     i_tmp = config_GetInt( p_dec, "ffmpeg-error-resilience" );
215     p_sys->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
216
217     var_Create( p_dec, "grayscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
218     var_Get( p_dec, "grayscale", &val );
219     if( val.b_bool ) p_sys->p_context->flags |= CODEC_FLAG_GRAY;
220
221     /* Decide if we set CODEC_FLAG_TRUNCATED */
222 #if LIBAVCODEC_BUILD >= 4662
223     var_Create( p_dec, "ffmpeg-truncated", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
224     var_Get( p_dec, "ffmpeg-truncated", &val );
225     if( val.i_int > 0 ) p_sys->p_context->flags |= CODEC_FLAG_TRUNCATED;
226 #endif
227
228     /* ***** Open the codec ***** */
229     if( avcodec_open( p_sys->p_context, p_sys->p_codec ) < 0 )
230     {
231         msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
232         return VLC_EGENERIC;
233     }
234     else
235     {
236         msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
237     }
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     if( E_(OpenPostproc)( p_dec, &p_sys->p_pp ) == VLC_SUCCESS )
262     {
263         /* for now we cannot do postproc and dr */
264         p_sys->b_direct_rendering = 0;
265     }
266 #endif
267
268     /* ffmpeg doesn't properly release old pictures when frames are skipped */
269     if( p_sys->b_hurry_up ) p_sys->b_direct_rendering = 0;
270     if( p_sys->b_direct_rendering )
271     {
272         msg_Dbg( p_dec, "using direct rendering" );
273         p_sys->p_context->flags |= CODEC_FLAG_EMU_EDGE;
274     }
275
276     /* Always use our get_buffer wrapper so we can calculate the
277      * PTS correctly */
278     p_sys->p_context->get_buffer = ffmpeg_GetFrameBuf;
279     p_sys->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
280     p_sys->p_context->opaque = p_dec;
281
282     /* ***** init this codec with special data ***** */
283     if( p_sys->p_format && p_sys->p_format->biSize > sizeof(BITMAPINFOHEADER) )
284     {
285         int b_gotpicture;
286         int i_size = p_sys->p_format->biSize - sizeof(BITMAPINFOHEADER);
287
288         if( p_sys->i_codec_id == CODEC_ID_MPEG4 )
289         {
290             uint8_t *p_vol = malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
291
292             memcpy( p_vol, &p_sys->p_format[1], i_size );
293             memset( &p_vol[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
294
295             avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
296                                   &b_gotpicture, p_vol, i_size );
297             free( p_vol );
298         }
299 #if LIBAVCODEC_BUILD >= 4666
300         else if( p_sys->i_codec_id == CODEC_ID_SVQ3 )
301         {
302             uint8_t *p;
303
304             p_sys->p_context->extradata_size = i_size + 12;
305             p = p_sys->p_context->extradata  =
306                 malloc( p_sys->p_context->extradata_size );
307
308             memcpy( &p[0],  "SVQ3", 4 );
309             memset( &p[4], 0, 8 );
310             memcpy( &p[12], &p_sys->p_format[1], i_size );
311         }
312 #endif
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_sys->p_format[1], 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->p_vout = NULL;
327     p_sys->input_pts = 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 = 1;
332     p_sys->p_buffer = malloc( p_sys->i_buffer );
333
334     return VLC_SUCCESS;
335 }
336
337 /*****************************************************************************
338  * DecodeVideo: Called to decode one or more frames
339  *****************************************************************************/
340 int E_(DecodeVideo)( decoder_t *p_dec, block_t *p_block )
341 {
342     decoder_sys_t *p_sys = p_dec->p_sys;
343     int i_buffer, b_drawpicture;
344     char *p_buffer;
345
346     if( p_block->i_pts > 0 )
347     {
348         p_sys->input_pts = p_block->i_pts;
349     }
350
351     /* TODO implement it in a better way */
352     /* A good idea could be to decode all I pictures and see for the other */
353     if( p_sys->b_hurry_up && p_sys->i_late_frames > 4 )
354     {
355         b_drawpicture = 0;
356         if( p_sys->i_late_frames < 8 )
357         {
358             p_sys->p_context->hurry_up = 2;
359         }
360         else
361         {
362             /* picture too late, won't decode
363              * but break picture until a new I, and for mpeg4 ...*/
364
365             p_sys->i_late_frames--; /* needed else it will never be decrease */
366             block_Release( p_block );
367             return VLC_SUCCESS;
368         }
369     }
370     else
371     {
372         b_drawpicture = 1;
373         p_sys->p_context->hurry_up = 0;
374     }
375
376     if( p_sys->i_late_frames > 0 &&
377         mdate() - p_sys->i_late_frames_start > I64C(5000000) )
378     {
379         msg_Err( p_dec, "more than 5 seconds of late video -> "
380                  "dropping frame (computer too slow ?)" );
381         block_Release( p_block );
382         p_sys->i_pts = 0; /* To make sure we recover properly */
383         p_sys->i_late_frames--;
384         return VLC_SUCCESS;
385     }
386
387     if( !p_sys->p_context->width || !p_sys->p_context->height )
388     {
389         p_sys->p_context->hurry_up = 5;
390     }
391
392     /*
393      * Do the actual decoding now
394      */
395
396     /* Don't forget that ffmpeg requires a little more bytes
397      * that the real frame size */
398     i_buffer = p_block->i_buffer;
399     if( i_buffer + FF_INPUT_BUFFER_PADDING_SIZE > p_sys->i_buffer )
400     {
401         free( p_sys->p_buffer );
402         p_sys->i_buffer = i_buffer + FF_INPUT_BUFFER_PADDING_SIZE;
403         p_sys->p_buffer = malloc( p_sys->i_buffer );
404     }
405     p_buffer = p_sys->p_buffer;
406     p_dec->p_vlc->pf_memcpy( p_buffer, p_block->p_buffer, p_block->i_buffer );
407     memset( p_buffer + i_buffer, 0, FF_INPUT_BUFFER_PADDING_SIZE );
408
409     while( i_buffer )
410     {
411         int i_used, b_gotpicture;
412         picture_t *p_pic;
413
414         i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
415                                        &b_gotpicture,
416                                        p_buffer, i_buffer );
417         if( i_used < 0 )
418         {
419             msg_Warn( p_dec, "cannot decode one frame (%d bytes)", i_buffer );
420             p_sys->i_frame_error++;
421             block_Release( p_block );
422             return VLC_SUCCESS;
423         }
424
425         /* Consumed bytes */
426         i_buffer -= i_used;
427         p_buffer += i_used;
428
429         /* Nothing to display */
430         if( !b_gotpicture ) continue;
431
432         /* Update frame late count*/
433         if( p_sys->i_pts && p_sys->i_pts <= mdate() )
434         {
435             p_sys->i_late_frames++;
436             if( p_sys->i_late_frames == 1 )
437                 p_sys->i_late_frames_start = mdate();
438         }
439         else
440         {
441             p_sys->i_late_frames = 0;
442         }
443
444         if( !b_drawpicture || p_sys->p_ff_pic->linesize[0] == 0 )
445         {
446             /* Do not display the picture */
447             continue;
448         }
449
450         if( !p_sys->b_direct_rendering )
451         {
452             p_sys->p_vout = ffmpeg_CreateVout( p_dec, p_sys->p_context );
453             if( !p_sys->p_vout )
454             {
455                 msg_Err( p_dec, "cannot create vout" );
456                 block_Release( p_block );
457                 return VLC_EGENERIC;
458             }
459
460             /* Get a new picture */
461             while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
462             {
463                 if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
464                 {
465                     block_Release( p_block );
466                     return VLC_EGENERIC;
467                 }
468                 msleep( VOUT_OUTMEM_SLEEP );
469             }
470
471             /* Fill p_picture_t from AVVideoFrame and do chroma conversion
472              * if needed */
473             ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
474         }
475         else
476         {
477             p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
478         }
479
480         /* Set the PTS
481          * There is an ugly hack here because some demuxers pass us a dts
482          * instead of a pts so this screw up things for streams with
483          * B frames. */
484         if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
485             p_sys->b_has_b_frames = VLC_TRUE;
486         if( p_sys->p_ff_pic->pts &&
487             ( !p_sys->p_context->has_b_frames || !p_sys->b_has_b_frames ||
488               p_sys->p_ff_pic->pict_type == FF_B_TYPE ) )
489         {
490             p_sys->i_pts = p_sys->p_ff_pic->pts;
491         }
492
493         /* Send decoded frame to vout */
494         if( p_sys->i_pts )
495         {
496             vout_DatePicture( p_sys->p_vout, p_pic, p_sys->i_pts );
497             vout_DisplayPicture( p_sys->p_vout, p_pic );
498
499             /* interpolate the next PTS */
500             if( p_sys->p_context->frame_rate > 0 )
501             {
502                 p_sys->i_pts += I64C(1000000) *
503                     (2 + p_sys->p_ff_pic->repeat_pict) *
504                     p_sys->p_context->frame_rate_base /
505                     (2 * p_sys->p_context->frame_rate);
506             }
507         }
508     }
509
510     block_Release( p_block );
511     return VLC_SUCCESS;
512 }
513
514 /*****************************************************************************
515  * EndVideo: decoder destruction
516  *****************************************************************************
517  * This function is called when the thread ends after a sucessful
518  * initialization.
519  *****************************************************************************/
520 void E_(EndVideoDec)( decoder_t *p_dec )
521 {
522     decoder_sys_t *p_sys = p_dec->p_sys;
523
524     if( p_sys->p_ff_pic ) free( p_sys->p_ff_pic );
525
526 #ifdef LIBAVCODEC_PP
527     E_(ClosePostproc)( p_dec, p_sys->p_pp );
528 #endif
529
530     free( p_sys->p_buffer );
531
532     /* We are about to die. Reattach video output to p_vlc. */
533     vout_Request( p_dec, p_sys->p_vout, 0, 0, 0, 0 );
534 }
535
536 /*****************************************************************************
537  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
538  *                     picture_t structure (when not in direct rendering mode).
539  *****************************************************************************/
540 static void ffmpeg_CopyPicture( decoder_t *p_dec,
541                                 picture_t *p_pic, AVFrame *p_ff_pic )
542 {
543     decoder_sys_t *p_sys = p_dec->p_sys;
544
545     if( ffmpeg_PixFmtToChroma( p_sys->p_context->pix_fmt ) )
546     {
547         int i_plane, i_size, i_line;
548         uint8_t *p_dst, *p_src;
549         int i_src_stride, i_dst_stride;
550
551 #ifdef LIBAVCODEC_PP
552         if( p_sys->p_pp )
553             E_(PostprocPict)( p_dec, p_sys->p_pp, p_pic, p_ff_pic );
554         else
555 #endif
556         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
557         {
558             p_src  = p_ff_pic->data[i_plane];
559             p_dst = p_pic->p[i_plane].p_pixels;
560             i_src_stride = p_ff_pic->linesize[i_plane];
561             i_dst_stride = p_pic->p[i_plane].i_pitch;
562
563             i_size = __MIN( i_src_stride, i_dst_stride );
564             for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
565             {
566                 p_dec->p_vlc->pf_memcpy( p_dst, p_src, i_size );
567                 p_src += i_src_stride;
568                 p_dst += i_dst_stride;
569             }
570         }
571     }
572     else
573     {
574         AVPicture dest_pic;
575         int i;
576
577         /* we need to convert to I420 */
578         switch( p_sys->p_context->pix_fmt )
579         {
580         case( PIX_FMT_YUV410P ):
581         case( PIX_FMT_YUV411P ):
582             for( i = 0; i < p_pic->i_planes; i++ )
583             {
584                 dest_pic.data[i] = p_pic->p[i].p_pixels;
585                 dest_pic.linesize[i] = p_pic->p[i].i_pitch;
586             }
587             img_convert( &dest_pic, PIX_FMT_YUV420P,
588                          (AVPicture *)p_ff_pic,
589                          p_sys->p_context->pix_fmt,
590                          p_sys->p_context->width,
591                          p_sys->p_context->height );
592             break;
593         default:
594             msg_Err( p_dec, "don't know how to convert chroma %i",
595                      p_sys->p_context->pix_fmt );
596             p_dec->p_fifo->b_error = 1;
597             break;
598         }
599     }
600 }
601
602 /*****************************************************************************
603  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
604  *****************************************************************************
605  * It is used for direct rendering as well as to get the right PTS for each
606  * decoded picture (even in indirect rendering mode).
607  *****************************************************************************/
608 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
609                                AVFrame *p_ff_pic )
610 {
611     decoder_t *p_dec = (decoder_t *)p_context->opaque;
612     decoder_sys_t *p_sys = p_dec->p_sys;
613     picture_t *p_pic;
614
615     /* Set picture PTS */
616     p_ff_pic->pts = p_sys->input_pts;
617     p_sys->input_pts = 0;
618
619     /* Not much to do in indirect rendering mode */
620     if( !p_sys->b_direct_rendering )
621     {
622         return avcodec_default_get_buffer( p_context, p_ff_pic );
623     }
624
625     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
626      * so this check is necessary. */
627     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
628         p_sys->p_context->width % 16 || p_sys->p_context->height % 16 )
629     {
630         msg_Dbg( p_dec, "disabling direct rendering" );
631         p_sys->b_direct_rendering = 0;
632         return avcodec_default_get_buffer( p_context, p_ff_pic );
633     }
634
635     /* Check and (re)create our vout if needed */
636     p_sys->p_vout = ffmpeg_CreateVout( p_dec, p_sys->p_context );
637     if( !p_sys->p_vout )
638     {
639         msg_Err( p_dec, "cannot create vout" );
640         p_dec->p_fifo->b_error = 1; /* abort */
641         p_sys->b_direct_rendering = 0;
642         return avcodec_default_get_buffer( p_context, p_ff_pic );
643     }
644
645     p_sys->p_vout->render.b_allow_modify_pics = 0;
646
647     /* Get a new picture */
648     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
649     {
650         if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
651         {
652             p_sys->b_direct_rendering = 0;
653             return avcodec_default_get_buffer( p_context, p_ff_pic );
654         }
655         msleep( VOUT_OUTMEM_SLEEP );
656     }
657     p_sys->p_context->draw_horiz_band = NULL;
658
659     p_ff_pic->opaque = (void*)p_pic;
660     p_ff_pic->type = FF_BUFFER_TYPE_USER;
661     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
662     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
663     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
664     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
665
666     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
667     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
668     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
669     p_ff_pic->linesize[3] = 0;
670
671     if( p_ff_pic->reference != 0 )
672     {
673         vout_LinkPicture( p_sys->p_vout, p_pic );
674     }
675
676     /* FIXME what is that, should give good value */
677     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
678
679     return( 0 );
680 }
681
682 static void  ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
683                                      AVFrame *p_ff_pic )
684 {
685     decoder_t *p_dec = (decoder_t *)p_context->opaque;
686     decoder_sys_t *p_sys = p_dec->p_sys;
687     picture_t *p_pic;
688
689     if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
690     {
691         avcodec_default_release_buffer( p_context, p_ff_pic );
692         return;
693     }
694
695     p_pic = (picture_t*)p_ff_pic->opaque;
696
697     p_ff_pic->data[0] = NULL;
698     p_ff_pic->data[1] = NULL;
699     p_ff_pic->data[2] = NULL;
700     p_ff_pic->data[3] = NULL;
701
702     if( p_ff_pic->reference != 0 )
703     {
704         vout_UnlinkPicture( p_sys->p_vout, p_pic );
705     }
706 }