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