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