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