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