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