]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/video.c
* modules/video_output/x11/xcommon.c: fixed a bug with fullscreen and sawfish.
[vlc] / modules / codec / ffmpeg / video.c
1 /*****************************************************************************
2  * video.c: video decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: video.c,v 1.40 2003/08/13 18:39:53 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32 #include <vlc/aout.h>
33 #include <vlc/decoder.h>
34 #include <vlc/input.h>
35
36 #include <string.h>
37
38 #ifdef HAVE_SYS_TIMES_H
39 #   include <sys/times.h>
40 #endif
41
42 /* ffmpeg header */
43 #ifdef HAVE_FFMPEG_AVCODEC_H
44 #   include <ffmpeg/avcodec.h>
45 #else
46 #   include <avcodec.h>
47 #endif
48
49 #include "ffmpeg.h"
50
51 #ifdef LIBAVCODEC_PP
52 #   ifdef HAVE_POSTPROC_POSTPROCESS_H
53 #       include <postproc/postprocess.h>
54 #   else
55 #       include <libpostproc/postprocess.h>
56 #   endif
57 #else
58 #   include "postprocessing/postprocessing.h"
59 #endif
60
61 #include "video.h"
62
63
64 /*****************************************************************************
65  * Local prototypes
66  *****************************************************************************/
67 static void ffmpeg_CopyPicture( picture_t *, AVFrame *, vdec_thread_t * );
68
69 static int  ffmpeg_GetFrameBuf      ( struct AVCodecContext *, AVFrame * );
70 static void ffmpeg_ReleaseFrameBuf  ( struct AVCodecContext *, AVFrame * );
71
72 /*****************************************************************************
73  * Local Functions
74  *****************************************************************************/
75 static inline uint32_t ffmpeg_PixFmtToChroma( int i_ff_chroma )
76 {
77     /* FIXME FIXME some of them are wrong */
78     switch( i_ff_chroma )
79     {
80         case PIX_FMT_YUV420P:
81         case PIX_FMT_YUV422:
82             return( VLC_FOURCC('I','4','2','0') );
83         case PIX_FMT_RGB24:
84             return( VLC_FOURCC('R','V','2','4') );
85
86         case PIX_FMT_YUV422P:
87             return( VLC_FOURCC('I','4','2','2') );
88         case PIX_FMT_YUV444P:
89             return( VLC_FOURCC('I','4','4','4') );
90         case PIX_FMT_YUV410P:
91         case PIX_FMT_YUV411P:
92         case PIX_FMT_BGR24:
93         default:
94             return( 0 );
95     }
96 }
97
98 /* Return a Vout */
99 static vout_thread_t *ffmpeg_CreateVout( vdec_thread_t  *p_vdec,
100                                          AVCodecContext *p_context )
101 {
102     vout_thread_t *p_vout;
103     unsigned int   i_width = p_context->width;
104     unsigned int   i_height = p_context->height;
105     uint32_t       i_chroma = ffmpeg_PixFmtToChroma( p_context->pix_fmt );
106     unsigned int   i_aspect;
107
108     if( !i_width || !i_height )
109     {
110         return( NULL ); /* Can't create a new vout without display size */
111     }
112
113     if( !i_chroma )
114     {
115         /* we make conversion if possible*/
116         i_chroma = VLC_FOURCC('I','4','2','0');
117     }
118
119     i_aspect = VOUT_ASPECT_FACTOR * p_context->aspect_ratio;
120     if( i_aspect == 0 )
121     {
122         i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
123     }
124
125     /* Spawn a video output if there is none. First we look for our children,
126      * then we look for any other vout that might be available. */
127     p_vout = vout_Request( p_vdec->p_fifo, p_vdec->p_vout,
128                            i_width, i_height, i_chroma, i_aspect );
129 #ifdef LIBAVCODEC_PP
130     if( p_vdec->pp_mode && !p_vdec->pp_context )
131     {
132         int32_t i_cpu = p_vdec->p_fifo->p_libvlc->i_cpu;
133         int i_flags = 0;
134
135         if( i_cpu & CPU_CAPABILITY_MMX )
136         {
137             i_flags |= PP_CPU_CAPS_MMX;
138         }
139         if( i_cpu & CPU_CAPABILITY_MMXEXT )
140         {
141             i_flags |= PP_CPU_CAPS_MMX2;
142         }
143         if( i_cpu & CPU_CAPABILITY_3DNOW )
144         {
145             i_flags |= PP_CPU_CAPS_3DNOW;
146         }
147
148         switch( p_context->pix_fmt )
149         {
150             case PIX_FMT_YUV444P:
151                 i_flags |= PP_FORMAT_444;
152                 break;
153             case PIX_FMT_YUV422P:
154                 i_flags |= PP_FORMAT_422;
155                 break;
156             case PIX_FMT_YUV411P:
157                 i_flags |= PP_FORMAT_411;
158                 break;
159             default:
160                 i_flags |= PP_FORMAT_420;
161                 break;
162         }
163
164         p_vdec->pp_context = pp_get_context( i_width, i_height, i_flags );
165     }
166 #endif
167
168     return p_vout;
169 }
170
171 /*****************************************************************************
172  *
173  * Functions that initialize, decode and end the decoding process
174  *
175  * Functions exported for ffmpeg.c
176  *   * E_( InitThread_Video )
177  *   * E_( DecodeThread )
178  *   * E_( EndThread_Video )
179  *****************************************************************************/
180
181 /*****************************************************************************
182  * InitThread: initialize vdec output thread
183  *****************************************************************************
184  * This function is called from decoder_Run and performs the second step
185  * of the initialization. It returns 0 on success. Note that the thread's
186  * flag are not modified inside this function.
187  *
188  * ffmpeg codec will be open, some memory allocated. But Vout is not yet
189  * open (done after the first decoded frame)
190  *****************************************************************************/
191 static inline void SetDWBE( void *data, uint32_t dw )
192 {
193     uint8_t *p = data;
194
195     p[0] = (dw >> 24 )&0xff;
196     p[1] = (dw >> 16 )&0xff;
197     p[2] = (dw >>  8 )&0xff;
198     p[3] = (dw )&0xff;
199 }
200
201 int E_( InitThread_Video )( vdec_thread_t *p_vdec )
202 {
203     int i_tmp;
204     int i_truncated;
205
206     p_vdec->p_ff_pic = avcodec_alloc_frame();
207
208     if( ( p_vdec->p_format =
209           (BITMAPINFOHEADER *)p_vdec->p_fifo->p_bitmapinfoheader ) != NULL )
210     {
211         /* ***** Fill p_context with init values ***** */
212         p_vdec->p_context->width  = p_vdec->p_format->biWidth;
213         p_vdec->p_context->height = p_vdec->p_format->biHeight;
214     }
215     else
216     {
217         msg_Warn( p_vdec->p_fifo, "display informations missing" );
218         p_vdec->p_format = NULL;
219     }
220
221     /*  ***** Get configuration of ffmpeg plugin ***** */
222     i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
223     p_vdec->p_context->workaround_bugs  = __MAX( __MIN( i_tmp, 99 ), 0 );
224
225     i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
226     p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
227
228     if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
229     {
230         p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
231     }
232
233     p_vdec->b_hurry_up = config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up");
234
235     /* CODEC_FLAG_TRUNCATED */
236
237     /* FIXME search real LIBAVCODEC_BUILD */
238 #if LIBAVCODEC_BUILD >= 4662
239     i_truncated = config_GetInt( p_vdec->p_fifo, "ffmpeg-truncated" );
240     if( i_truncated == 1 )
241 #if 0
242         ||
243         ( i_truncated == -1 && ( p_vdec->p_context->width == 0 || p_vdec->p_context->height == 0 ) ) )
244 #endif
245     {
246         p_vdec->p_context->flags |= CODEC_FLAG_TRUNCATED;
247     }
248 #endif
249
250     /* ***** Open the codec ***** */
251     if( avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0 )
252     {
253         msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
254                                  p_vdec->psz_namecodec );
255         return( VLC_EGENERIC );
256     }
257     else
258     {
259         msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
260                                  p_vdec->psz_namecodec );
261     }
262
263     p_vdec->b_direct_rendering = 0;
264     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr" ) &&
265         p_vdec->p_codec->capabilities & CODEC_CAP_DR1 &&
266         ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) &&
267         /* Apparently direct rendering doesn't work with YUV422P */
268         p_vdec->p_context->pix_fmt != PIX_FMT_YUV422P &&
269         !(p_vdec->p_context->width % 16) && !(p_vdec->p_context->height % 16) )
270     {
271         /* Some codecs set pix_fmt only after the 1st frame has been decoded,
272          * so we need to do another check in ffmpeg_GetFrameBuf() */
273         p_vdec->b_direct_rendering = 1;
274     }
275
276     /* ***** Load post processing ***** */
277 #ifdef LIBAVCODEC_PP
278     p_vdec->pp_context = NULL;
279     p_vdec->pp_mode    = NULL;
280
281     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ) > 0 )
282     {
283         int  i_quality = config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" );
284         char *psz_name = config_GetPsz( p_vdec->p_fifo, "ffmpeg-pp-name" );
285
286         if( !psz_name )
287         {
288             psz_name = strdup( "default" );
289         }
290         else if( *psz_name == '\0' )
291         {
292             free( psz_name );
293             psz_name = strdup( "default" );
294         }
295
296         p_vdec->pp_mode =
297             pp_get_mode_by_name_and_quality( psz_name, i_quality );
298
299         if( !p_vdec->pp_mode )
300         {
301             msg_Err( p_vdec->p_fifo, "failed geting mode for postproc" );
302         }
303         else
304         {
305             msg_Info( p_vdec->p_fifo, "postproc activated" );
306         }
307         free( psz_name );
308
309         /* for now we cannot do postproc and dr */
310         p_vdec->b_direct_rendering = 0;
311     }
312     else
313     {
314         msg_Dbg( p_vdec->p_fifo, "no postproc" );
315     }
316 #endif
317
318     /* ffmpeg doesn't properly release old pictures when frames are skipped */
319     if( p_vdec->b_hurry_up ) p_vdec->b_direct_rendering = 0;
320
321     /* Always use our get_buffer wrapper so we can calculate the
322      * PTS correctly */
323     p_vdec->p_context->get_buffer = ffmpeg_GetFrameBuf;
324     p_vdec->p_context->release_buffer = ffmpeg_ReleaseFrameBuf;
325     p_vdec->p_context->opaque = p_vdec;
326
327     if( p_vdec->b_direct_rendering )
328     {
329         msg_Dbg( p_vdec->p_fifo, "using direct rendering" );
330         p_vdec->p_context->flags |= CODEC_FLAG_EMU_EDGE;
331     }
332
333     /* ***** init this codec with special data ***** */
334     if( p_vdec->p_format &&
335             p_vdec->p_format->biSize > sizeof(BITMAPINFOHEADER) )
336     {
337         int b_gotpicture;
338         int i_size = p_vdec->p_format->biSize - sizeof(BITMAPINFOHEADER);
339
340         if( p_vdec->i_codec_id == CODEC_ID_MPEG4 )
341         {
342             uint8_t *p_vol = malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
343
344             memcpy( p_vol, &p_vdec->p_format[1], i_size );
345             memset( &p_vol[i_size], 0, FF_INPUT_BUFFER_PADDING_SIZE );
346
347             avcodec_decode_video( p_vdec->p_context, p_vdec->p_ff_pic,
348                                   &b_gotpicture,
349                                   p_vol,
350                                   i_size );
351             free( p_vol );
352         }
353 #if LIBAVCODEC_BUILD >= 4666
354         else if( p_vdec->i_codec_id == CODEC_ID_SVQ3 )
355         {
356             uint8_t *p;
357
358             p_vdec->p_context->extradata_size = i_size + 12;
359             p = p_vdec->p_context->extradata  =
360                 malloc( p_vdec->p_context->extradata_size );
361
362             memcpy( &p[0],  "SVQ3", 4 );
363             memset( &p[4], 0, 8 );
364             memcpy( &p[12], &p_vdec->p_format[1], i_size );
365         }
366 #endif
367         else
368         {
369             p_vdec->p_context->extradata_size = i_size;
370             p_vdec->p_context->extradata =
371                 malloc( i_size + FF_INPUT_BUFFER_PADDING_SIZE );
372             memcpy( p_vdec->p_context->extradata,
373                     &p_vdec->p_format[1], i_size );
374             memset( &((uint8_t*)p_vdec->p_context->extradata)[i_size],
375                     0, FF_INPUT_BUFFER_PADDING_SIZE );
376         }
377     }
378     p_vdec->p_vout = NULL;
379
380     p_vdec->input_pts_previous = 0;
381     p_vdec->input_pts = 0;
382
383     p_vdec->b_has_b_frames = VLC_FALSE;
384
385     return( VLC_SUCCESS );
386 }
387
388 /*****************************************************************************
389  * DecodeThread: Called to decode one frame
390  *****************************************************************************
391  * We have to get a frame stored in a pes, give it to ffmpeg decoder and send
392  * the image to the output.
393  *****************************************************************************/
394 void  E_( DecodeThread_Video )( vdec_thread_t *p_vdec )
395 {
396     pes_packet_t    *p_pes;
397     int     i_frame_size;
398     int     i_used;
399     int     b_drawpicture;
400     int     b_gotpicture;
401     picture_t *p_pic;                                         /* vlc picture */
402
403     /* TODO implement it in a better way */
404     /* A good idea could be to decode all I pictures and see for the other */
405     if( p_vdec->b_hurry_up && p_vdec->i_frame_late > 4 )
406     {
407         b_drawpicture = 0;
408         if( p_vdec->i_frame_late < 8 )
409         {
410             p_vdec->p_context->hurry_up = 2;
411         }
412         else
413         {
414             /* too much late picture, won't decode
415                but break picture until a new I, and for mpeg4 ...*/
416             p_vdec->i_frame_late--; /* needed else it will never be decrease */
417             input_ExtractPES( p_vdec->p_fifo, NULL );
418             return;
419         }
420     }
421     else
422     {
423         b_drawpicture = 1;
424         p_vdec->p_context->hurry_up = 0;
425     }
426
427     if( p_vdec->i_frame_late > 0 &&
428         mdate() - p_vdec->i_frame_late_start > (mtime_t)5000000 )
429     {
430         msg_Err( p_vdec->p_fifo, "more than 5 seconds of late video -> "
431                  "dropping (to slow computer ?)" );
432         do
433         {
434             input_ExtractPES( p_vdec->p_fifo, &p_pes );
435             if( !p_pes )
436             {
437                 p_vdec->p_fifo->b_error = 1;
438                 return;
439             }
440
441             if( p_pes->i_pts > 0 )
442             {
443                 p_vdec->input_pts_previous = p_vdec->input_pts;
444                 p_vdec->input_pts = p_pes->i_pts;
445             }
446             input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
447
448         } while( p_vdec->input_pts <= 0 || p_vdec->input_pts < mdate() );
449     }
450
451     if( !p_vdec->p_context->width || !p_vdec->p_context->height )
452     {
453         p_vdec->p_context->hurry_up = 5;
454     }
455
456     do
457     {
458         input_ExtractPES( p_vdec->p_fifo, &p_pes );
459         if( !p_pes )
460         {
461             p_vdec->p_fifo->b_error = 1;
462             return;
463         }
464
465         if( p_pes->i_pts > 0 )
466         {
467             p_vdec->input_pts_previous = p_vdec->input_pts;
468             p_vdec->input_pts = p_pes->i_pts;
469         }
470
471         i_frame_size = p_pes->i_pes_size;
472
473         if( i_frame_size > 0 )
474         {
475             uint8_t *p_last;
476             int i_need;
477             /* XXX Don't forget that ffmpeg required a little more bytes
478              * that the real frame size */
479             i_need = i_frame_size + FF_INPUT_BUFFER_PADDING_SIZE + p_vdec->i_buffer;
480             if( p_vdec->i_buffer_size < i_need)
481             {
482                 p_last = p_vdec->p_buffer;
483                 p_vdec->p_buffer = malloc( i_need );
484                 p_vdec->i_buffer_size = i_need;
485                 if( p_vdec->i_buffer > 0 )
486                 {
487                     memcpy( p_vdec->p_buffer, p_last, p_vdec->i_buffer );
488                 }
489                 FREE( p_last );
490             }
491             i_frame_size =
492                 E_( GetPESData )( p_vdec->p_buffer + p_vdec->i_buffer,
493                                   i_frame_size , p_pes );
494             memset( p_vdec->p_buffer + p_vdec->i_buffer + i_frame_size,
495                     0, FF_INPUT_BUFFER_PADDING_SIZE );
496         }
497
498         input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
499
500     } while( i_frame_size <= 0 );
501
502     i_frame_size += p_vdec->i_buffer;
503
504 usenextdata:
505     i_used = avcodec_decode_video( p_vdec->p_context,
506                                    p_vdec->p_ff_pic,
507                                    &b_gotpicture,
508                                    p_vdec->p_buffer,
509                                    i_frame_size );
510
511     if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error ) return;
512
513 #if 0
514     msg_Dbg( p_vdec->p_fifo,
515              "used:%d framesize:%d (%s picture)",
516              i_used, i_frame_size, b_gotpicture ? "got":"no got" );
517 #endif
518     if( i_used < 0 )
519     {
520         msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
521                                   i_frame_size );
522         p_vdec->i_frame_error++;
523         p_vdec->i_buffer = 0;
524         return;
525     }
526     else if( i_used < i_frame_size )
527     {
528         memmove( p_vdec->p_buffer,
529                  p_vdec->p_buffer + i_used,
530                  p_vdec->i_buffer_size - i_used );
531
532         p_vdec->i_buffer = i_frame_size - i_used;
533     }
534     else
535     {
536         p_vdec->i_buffer = 0;
537     }
538
539     /* consumed bytes */
540     i_frame_size -= i_used;
541
542    /* Update frame late count*/
543     if( p_vdec->pts <= mdate() )
544     {
545         p_vdec->i_frame_late++;
546         if( p_vdec->i_frame_late == 1 )
547         {
548             p_vdec->i_frame_late_start = mdate();
549         }
550     }
551     else
552     {
553         p_vdec->i_frame_late = 0;
554     }
555
556     if( !b_gotpicture || p_vdec->p_ff_pic->linesize[0] == 0 || !b_drawpicture )
557     {
558         return;
559     }
560
561     if( !p_vdec->b_direct_rendering )
562     {
563         p_vdec->p_vout = ffmpeg_CreateVout( p_vdec, p_vdec->p_context );
564         if( !p_vdec->p_vout )
565         {
566             msg_Err( p_vdec->p_fifo, "cannot create vout" );
567             p_vdec->p_fifo->b_error = 1; /* abort */
568             return;
569         }
570
571         /* Get a new picture */
572         while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
573         {
574             if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
575             {
576                 return;
577             }
578             msleep( VOUT_OUTMEM_SLEEP );
579         }
580
581         /* fill p_picture_t from AVVideoFrame and do chroma conversion
582          * if needed */
583         ffmpeg_CopyPicture( p_pic, p_vdec->p_ff_pic, p_vdec );
584     }
585     else
586     {
587         p_pic = (picture_t *)p_vdec->p_ff_pic->opaque;
588     }
589
590     /* Set the PTS
591      * There is an ugly hack here because some demuxers pass us a dts instead
592      * of a pts so this screw up things for streams with B frames. */
593     if( p_vdec->p_ff_pic->pict_type == FF_B_TYPE )
594         p_vdec->b_has_b_frames = VLC_TRUE;
595     if( p_vdec->p_ff_pic->pts &&
596         ( !p_vdec->p_context->has_b_frames || !p_vdec->b_has_b_frames ||
597           p_vdec->p_ff_pic->pict_type == FF_B_TYPE ) )
598     {
599         p_vdec->pts = p_vdec->p_ff_pic->pts;
600     }
601
602     if( p_vdec->pts <= 0 )
603     {
604         p_vdec->pts = mdate() + DEFAULT_PTS_DELAY;  // FIXME
605     }
606
607     /* Send decoded frame to vout */
608     vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->pts );
609     vout_DisplayPicture( p_vdec->p_vout, p_pic );
610
611     /* interpolate the next PTS */
612     if( p_vdec->p_context->frame_rate > 0 )
613     {
614         p_vdec->pts += I64C(1000000) * (2 + p_vdec->p_ff_pic->repeat_pict) *
615                        p_vdec->p_context->frame_rate_base /
616                        (2 * p_vdec->p_context->frame_rate);
617     }
618
619     if( i_frame_size > 0 )
620     {
621         goto usenextdata; /* try to use all data */
622     }
623 }
624
625 /*****************************************************************************
626  * EndThread: thread destruction
627  *****************************************************************************
628  * This function is called when the thread ends after a sucessful
629  * initialization.
630  *****************************************************************************/
631 void E_( EndThread_Video )( vdec_thread_t *p_vdec )
632 {
633
634 #ifdef LIBAVCODEC_PP
635     if( p_vdec->pp_mode )
636     {
637         pp_free_mode( p_vdec->pp_mode );
638         if( p_vdec->pp_context )
639         {
640             pp_free_context( p_vdec->pp_context );
641         }
642     }
643 #endif
644
645     if( p_vdec->p_ff_pic )
646     {
647         free( p_vdec->p_ff_pic );
648     }
649
650     /* We are about to die. Reattach video output to p_vlc. */
651     vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
652 }
653
654 /*****************************************************************************
655  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
656  *                     picture_t structure (when not in direct rendering mode).
657  *****************************************************************************/
658 static void ffmpeg_CopyPicture( picture_t    *p_pic,
659                                 AVFrame *p_ff_pic,
660                                 vdec_thread_t *p_vdec )
661 {
662     int i_plane;
663     int i_size;
664     int i_line;
665
666     uint8_t *p_dst;
667     uint8_t *p_src;
668     int i_src_stride;
669     int i_dst_stride;
670
671     if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
672     {
673 #ifdef LIBAVCODEC_PP
674         if( p_vdec->pp_mode && p_vdec->pp_context )
675         {
676             uint8_t *src[3], *dst[3];
677             int     i_src_stride[3], i_dst_stride[3];
678
679             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
680             {
681                 src[i_plane] = p_ff_pic->data[i_plane];
682                 dst[i_plane] = p_pic->p[i_plane].p_pixels;
683
684                 i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
685                 i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
686             }
687             pp_postprocess( src, i_src_stride,
688                             dst, i_dst_stride,
689                             p_vdec->p_context->width,
690                             p_vdec->p_context->height,
691                             p_ff_pic->qscale_table, p_ff_pic->qstride,
692                             p_vdec->pp_mode, p_vdec->pp_context,
693                             p_ff_pic->pict_type );
694         }
695         else
696         {
697 #endif
698             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
699             {
700                 p_src  = p_ff_pic->data[i_plane];
701                 p_dst = p_pic->p[i_plane].p_pixels;
702                 i_src_stride = p_ff_pic->linesize[i_plane];
703                 i_dst_stride = p_pic->p[i_plane].i_pitch;
704
705                 i_size = __MIN( i_src_stride, i_dst_stride );
706                 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
707                 {
708                     p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
709                     p_src += i_src_stride;
710                     p_dst += i_dst_stride;
711                 }
712             }
713 #ifdef LIBAVCODEC_PP
714         }
715 #endif
716     }
717     else
718     {
719         /* we need to convert to I420 */
720         switch( p_vdec->p_context->pix_fmt )
721         {
722             AVPicture dest_pic;
723             int i;
724
725             case( PIX_FMT_YUV410P ):
726             case( PIX_FMT_YUV411P ):
727                 for( i = 0; i < p_pic->i_planes; i++ )
728                 {
729                     dest_pic.data[i] = p_pic->p[i].p_pixels;
730                     dest_pic.linesize[i] = p_pic->p[i].i_pitch;
731                 }
732                 img_convert( &dest_pic, PIX_FMT_YUV420P,
733                              (AVPicture *)p_ff_pic,
734                              p_vdec->p_context->pix_fmt,
735                              p_vdec->p_context->width,
736                              p_vdec->p_context->height );
737                 break;
738             default:
739                 msg_Err( p_vdec->p_fifo, "don't know how to convert chroma %i",
740                          p_vdec->p_context->pix_fmt );
741                 p_vdec->p_fifo->b_error = 1;
742                 break;
743         }
744     }
745 }
746
747 /*****************************************************************************
748  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
749  *****************************************************************************
750  * It is used for direct rendering as well as to get the right PTS for each
751  * decoded picture (even in indirect rendering mode).
752  *****************************************************************************/
753 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
754                                AVFrame *p_ff_pic )
755 {
756     vdec_thread_t *p_vdec = (vdec_thread_t *)p_context->opaque;
757     picture_t *p_pic;
758
759     /* Set picture PTS */
760     if( p_context->flags & CODEC_FLAG_TRUNCATED )
761     {
762         p_ff_pic->pts = p_vdec->input_pts_previous;
763         p_vdec->input_pts_previous = 0;
764     }
765     else
766     {
767         p_ff_pic->pts = p_vdec->input_pts;
768         p_vdec->input_pts = 0;
769     }
770
771     /* Not much to do in indirect rendering mode */
772     if( !p_vdec->b_direct_rendering )
773     {
774         return avcodec_default_get_buffer( p_context, p_ff_pic );
775     }
776
777     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
778      * so this check is necessary. */
779     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
780         p_vdec->p_context->width % 16 || p_vdec->p_context->height % 16 )
781     {
782         msg_Dbg( p_vdec->p_fifo, "disabling direct rendering" );
783         p_vdec->b_direct_rendering = 0;
784         return avcodec_default_get_buffer( p_context, p_ff_pic );
785     }
786
787     /* Check and (re)create our vout if needed */
788     p_vdec->p_vout = ffmpeg_CreateVout( p_vdec, p_vdec->p_context );
789     if( !p_vdec->p_vout )
790     {
791         msg_Err( p_vdec->p_fifo, "cannot create vout" );
792         p_vdec->p_fifo->b_error = 1; /* abort */
793         p_vdec->b_direct_rendering = 0;
794         return avcodec_default_get_buffer( p_context, p_ff_pic );
795     }
796
797     p_vdec->p_vout->render.b_allow_modify_pics = 0;
798
799     /* Get a new picture */
800     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
801     {
802         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
803         {
804             p_vdec->b_direct_rendering = 0;
805             return avcodec_default_get_buffer( p_context, p_ff_pic );
806         }
807         msleep( VOUT_OUTMEM_SLEEP );
808     }
809     p_vdec->p_context->draw_horiz_band = NULL;
810
811     p_ff_pic->opaque = (void*)p_pic;
812     p_ff_pic->type = FF_BUFFER_TYPE_USER;
813     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
814     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
815     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
816     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
817
818     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
819     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
820     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
821     p_ff_pic->linesize[3] = 0;
822
823     if( p_ff_pic->reference != 0 )
824     {
825         vout_LinkPicture( p_vdec->p_vout, p_pic );
826     }
827     /* FIXME what is that, should give good value */
828     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
829
830     return( 0 );
831 }
832
833 static void  ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
834                                      AVFrame *p_ff_pic )
835 {
836     vdec_thread_t *p_vdec = (vdec_thread_t *)p_context->opaque;
837     picture_t *p_pic;
838
839     if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
840     {
841         avcodec_default_release_buffer( p_context, p_ff_pic );
842         return;
843     }
844
845     p_pic = (picture_t*)p_ff_pic->opaque;
846
847     p_ff_pic->data[0] = NULL;
848     p_ff_pic->data[1] = NULL;
849     p_ff_pic->data[2] = NULL;
850     p_ff_pic->data[3] = NULL;
851
852     if( p_ff_pic->reference != 0 )
853     {
854         vout_UnlinkPicture( p_vdec->p_vout, p_pic );
855     }
856 }