* modules/codec/ffmpeg/video.c: temporary hack to work around demuxers that set the...
[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.38 2003/08/09 19:49:13 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     return( VLC_SUCCESS );
384 }
385
386 /*****************************************************************************
387  * DecodeThread: Called to decode one frame
388  *****************************************************************************
389  * We have to get a frame stored in a pes, give it to ffmpeg decoder and send
390  * the image to the output.
391  *****************************************************************************/
392 void  E_( DecodeThread_Video )( vdec_thread_t *p_vdec )
393 {
394     pes_packet_t    *p_pes;
395     int     i_frame_size;
396     int     i_used;
397     int     b_drawpicture;
398     int     b_gotpicture;
399     picture_t *p_pic;                                         /* vlc picture */
400
401     /* TODO implement it in a better way */
402     /* A good idea could be to decode all I pictures and see for the other */
403     if( p_vdec->b_hurry_up && p_vdec->i_frame_late > 4 )
404     {
405         b_drawpicture = 0;
406         if( p_vdec->i_frame_late < 8 )
407         {
408             p_vdec->p_context->hurry_up = 2;
409         }
410         else
411         {
412             /* too much late picture, won't decode
413                but break picture until a new I, and for mpeg4 ...*/
414             p_vdec->i_frame_late--; /* needed else it will never be decrease */
415             input_ExtractPES( p_vdec->p_fifo, NULL );
416             return;
417         }
418     }
419     else
420     {
421         b_drawpicture = 1;
422         p_vdec->p_context->hurry_up = 0;
423     }
424
425     if( p_vdec->i_frame_late > 0 &&
426         mdate() - p_vdec->i_frame_late_start > (mtime_t)5000000 )
427     {
428         msg_Err( p_vdec->p_fifo, "more than 5 seconds of late video -> "
429                  "dropping (to slow computer ?)" );
430         do
431         {
432             input_ExtractPES( p_vdec->p_fifo, &p_pes );
433             if( !p_pes )
434             {
435                 p_vdec->p_fifo->b_error = 1;
436                 return;
437             }
438
439             if( p_pes->i_pts > 0 )
440             {
441                 p_vdec->input_pts_previous = p_vdec->input_pts;
442                 p_vdec->input_pts = p_pes->i_pts;
443             }
444             input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
445
446         } while( p_vdec->input_pts <= 0 || p_vdec->input_pts < mdate() );
447     }
448
449     if( !p_vdec->p_context->width || !p_vdec->p_context->height )
450     {
451         p_vdec->p_context->hurry_up = 5;
452     }
453
454     do
455     {
456         input_ExtractPES( p_vdec->p_fifo, &p_pes );
457         if( !p_pes )
458         {
459             p_vdec->p_fifo->b_error = 1;
460             return;
461         }
462
463         if( p_pes->i_pts > 0 )
464         {
465             p_vdec->input_pts_previous = p_vdec->input_pts;
466             p_vdec->input_pts = p_pes->i_pts;
467         }
468
469         i_frame_size = p_pes->i_pes_size;
470
471         if( i_frame_size > 0 )
472         {
473             uint8_t *p_last;
474             int i_need;
475             /* XXX Don't forget that ffmpeg required a little more bytes
476              * that the real frame size */
477             i_need = i_frame_size + FF_INPUT_BUFFER_PADDING_SIZE + p_vdec->i_buffer;
478             if( p_vdec->i_buffer_size < i_need)
479             {
480                 p_last = p_vdec->p_buffer;
481                 p_vdec->p_buffer = malloc( i_need );
482                 p_vdec->i_buffer_size = i_need;
483                 if( p_vdec->i_buffer > 0 )
484                 {
485                     memcpy( p_vdec->p_buffer, p_last, p_vdec->i_buffer );
486                 }
487                 FREE( p_last );
488             }
489             i_frame_size =
490                 E_( GetPESData )( p_vdec->p_buffer + p_vdec->i_buffer,
491                                   i_frame_size , p_pes );
492             memset( p_vdec->p_buffer + p_vdec->i_buffer + i_frame_size,
493                     0, FF_INPUT_BUFFER_PADDING_SIZE );
494         }
495
496         input_DeletePES( p_vdec->p_fifo->p_packets_mgt, p_pes );
497
498     } while( i_frame_size <= 0 );
499
500     i_frame_size += p_vdec->i_buffer;
501
502 usenextdata:
503     i_used = avcodec_decode_video( p_vdec->p_context,
504                                    p_vdec->p_ff_pic,
505                                    &b_gotpicture,
506                                    p_vdec->p_buffer,
507                                    i_frame_size );
508
509     if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error ) return;
510
511 #if 0
512     msg_Dbg( p_vdec->p_fifo,
513              "used:%d framesize:%d (%s picture)",
514              i_used, i_frame_size, b_gotpicture ? "got":"no got" );
515 #endif
516     if( i_used < 0 )
517     {
518         msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
519                                   i_frame_size );
520         p_vdec->i_frame_error++;
521         p_vdec->i_buffer = 0;
522         return;
523     }
524     else if( i_used < i_frame_size )
525     {
526         memmove( p_vdec->p_buffer,
527                  p_vdec->p_buffer + i_used,
528                  p_vdec->i_buffer_size - i_used );
529
530         p_vdec->i_buffer = i_frame_size - i_used;
531     }
532     else
533     {
534         p_vdec->i_buffer = 0;
535     }
536
537     /* consumed bytes */
538     i_frame_size -= i_used;
539
540    /* Update frame late count*/
541     if( p_vdec->pts <= mdate() )
542     {
543         p_vdec->i_frame_late++;
544         if( p_vdec->i_frame_late == 1 )
545         {
546             p_vdec->i_frame_late_start = mdate();
547         }
548     }
549     else
550     {
551         p_vdec->i_frame_late = 0;
552     }
553
554     if( !b_gotpicture || p_vdec->p_ff_pic->linesize[0] == 0 || !b_drawpicture )
555     {
556         return;
557     }
558
559     if( !p_vdec->b_direct_rendering )
560     {
561         p_vdec->p_vout = ffmpeg_CreateVout( p_vdec, p_vdec->p_context );
562         if( !p_vdec->p_vout )
563         {
564             msg_Err( p_vdec->p_fifo, "cannot create vout" );
565             p_vdec->p_fifo->b_error = 1; /* abort */
566             return;
567         }
568
569         /* Get a new picture */
570         while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
571         {
572             if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
573             {
574                 return;
575             }
576             msleep( VOUT_OUTMEM_SLEEP );
577         }
578
579         /* fill p_picture_t from AVVideoFrame and do chroma conversion
580          * if needed */
581         ffmpeg_CopyPicture( p_pic, p_vdec->p_ff_pic, p_vdec );
582     }
583     else
584     {
585         p_pic = (picture_t *)p_vdec->p_ff_pic->opaque;
586     }
587
588     /* Set the PTS
589      * There is an ugly hack here because some demuxers pass us a dts instead
590      * of a pts so this screw up things for streams with B frames. */
591     if( p_vdec->p_ff_pic->pts &&
592         ( !p_vdec->p_context->has_b_frames ||
593           p_vdec->p_ff_pic->pict_type == FF_B_TYPE ) )
594     {
595         p_vdec->pts = p_vdec->p_ff_pic->pts;
596     }
597
598     if( p_vdec->pts <= 0 )
599     {
600         p_vdec->pts = mdate() + DEFAULT_PTS_DELAY;  // FIXME
601     }
602
603     /* Send decoded frame to vout */
604     vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->pts );
605     vout_DisplayPicture( p_vdec->p_vout, p_pic );
606
607     /* interpolate the next PTS */
608     if( p_vdec->p_context->frame_rate > 0 )
609     {
610         p_vdec->pts += I64C(1000000) * (2 + p_vdec->p_ff_pic->repeat_pict) *
611                        p_vdec->p_context->frame_rate_base /
612                        (2 * p_vdec->p_context->frame_rate);
613     }
614
615     if( i_frame_size > 0 )
616     {
617         goto usenextdata; /* try to use all data */
618     }
619 }
620
621 /*****************************************************************************
622  * EndThread: thread destruction
623  *****************************************************************************
624  * This function is called when the thread ends after a sucessful
625  * initialization.
626  *****************************************************************************/
627 void E_( EndThread_Video )( vdec_thread_t *p_vdec )
628 {
629
630 #ifdef LIBAVCODEC_PP
631     if( p_vdec->pp_mode )
632     {
633         pp_free_mode( p_vdec->pp_mode );
634         if( p_vdec->pp_context )
635         {
636             pp_free_context( p_vdec->pp_context );
637         }
638     }
639 #endif
640
641     if( p_vdec->p_ff_pic )
642     {
643         free( p_vdec->p_ff_pic );
644     }
645
646     /* We are about to die. Reattach video output to p_vlc. */
647     vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
648 }
649
650 /*****************************************************************************
651  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
652  *                     picture_t structure (when not in direct rendering mode).
653  *****************************************************************************/
654 static void ffmpeg_CopyPicture( picture_t    *p_pic,
655                                 AVFrame *p_ff_pic,
656                                 vdec_thread_t *p_vdec )
657 {
658     int i_plane;
659     int i_size;
660     int i_line;
661
662     uint8_t *p_dst;
663     uint8_t *p_src;
664     int i_src_stride;
665     int i_dst_stride;
666
667     if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
668     {
669 #ifdef LIBAVCODEC_PP
670         if( p_vdec->pp_mode && p_vdec->pp_context )
671         {
672             uint8_t *src[3], *dst[3];
673             int     i_src_stride[3], i_dst_stride[3];
674
675             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
676             {
677                 src[i_plane] = p_ff_pic->data[i_plane];
678                 dst[i_plane] = p_pic->p[i_plane].p_pixels;
679
680                 i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
681                 i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
682             }
683             pp_postprocess( src, i_src_stride,
684                             dst, i_dst_stride,
685                             p_vdec->p_context->width,
686                             p_vdec->p_context->height,
687                             p_ff_pic->qscale_table, p_ff_pic->qstride,
688                             p_vdec->pp_mode, p_vdec->pp_context,
689                             p_ff_pic->pict_type );
690         }
691         else
692         {
693 #endif
694             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
695             {
696                 p_src  = p_ff_pic->data[i_plane];
697                 p_dst = p_pic->p[i_plane].p_pixels;
698                 i_src_stride = p_ff_pic->linesize[i_plane];
699                 i_dst_stride = p_pic->p[i_plane].i_pitch;
700
701                 i_size = __MIN( i_src_stride, i_dst_stride );
702                 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
703                 {
704                     p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
705                     p_src += i_src_stride;
706                     p_dst += i_dst_stride;
707                 }
708             }
709 #ifdef LIBAVCODEC_PP
710         }
711 #endif
712     }
713     else
714     {
715         /* we need to convert to I420 */
716         switch( p_vdec->p_context->pix_fmt )
717         {
718             AVPicture dest_pic;
719             int i;
720
721             case( PIX_FMT_YUV410P ):
722             case( PIX_FMT_YUV411P ):
723                 for( i = 0; i < p_pic->i_planes; i++ )
724                 {
725                     dest_pic.data[i] = p_pic->p[i].p_pixels;
726                     dest_pic.linesize[i] = p_pic->p[i].i_pitch;
727                 }
728                 img_convert( &dest_pic, PIX_FMT_YUV420P,
729                              (AVPicture *)p_ff_pic,
730                              p_vdec->p_context->pix_fmt,
731                              p_vdec->p_context->width,
732                              p_vdec->p_context->height );
733                 break;
734             default:
735                 msg_Err( p_vdec->p_fifo, "don't know how to convert chroma %i",
736                          p_vdec->p_context->pix_fmt );
737                 p_vdec->p_fifo->b_error = 1;
738                 break;
739         }
740     }
741 }
742
743 /*****************************************************************************
744  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
745  *****************************************************************************
746  * It is used for direct rendering as well as to get the right PTS for each
747  * decoded picture (even in indirect rendering mode).
748  *****************************************************************************/
749 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
750                                AVFrame *p_ff_pic )
751 {
752     vdec_thread_t *p_vdec = (vdec_thread_t *)p_context->opaque;
753     picture_t *p_pic;
754
755     /* Set picture PTS */
756     if( p_context->flags & CODEC_FLAG_TRUNCATED )
757     {
758         p_ff_pic->pts = p_vdec->input_pts_previous;
759         p_vdec->input_pts_previous = 0;
760     }
761     else
762     {
763         p_ff_pic->pts = p_vdec->input_pts;
764         p_vdec->input_pts = 0;
765     }
766
767     /* Not much to do in indirect rendering mode */
768     if( !p_vdec->b_direct_rendering )
769     {
770         return avcodec_default_get_buffer( p_context, p_ff_pic );
771     }
772
773     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
774      * so this check is necessary. */
775     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
776         p_vdec->p_context->width % 16 || p_vdec->p_context->height % 16 )
777     {
778         msg_Dbg( p_vdec->p_fifo, "disabling direct rendering" );
779         p_vdec->b_direct_rendering = 0;
780         return avcodec_default_get_buffer( p_context, p_ff_pic );
781     }
782
783     /* Check and (re)create our vout if needed */
784     p_vdec->p_vout = ffmpeg_CreateVout( p_vdec, p_vdec->p_context );
785     if( !p_vdec->p_vout )
786     {
787         msg_Err( p_vdec->p_fifo, "cannot create vout" );
788         p_vdec->p_fifo->b_error = 1; /* abort */
789         p_vdec->b_direct_rendering = 0;
790         return avcodec_default_get_buffer( p_context, p_ff_pic );
791     }
792
793     p_vdec->p_vout->render.b_allow_modify_pics = 0;
794
795     /* Get a new picture */
796     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
797     {
798         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
799         {
800             p_vdec->b_direct_rendering = 0;
801             return avcodec_default_get_buffer( p_context, p_ff_pic );
802         }
803         msleep( VOUT_OUTMEM_SLEEP );
804     }
805     p_vdec->p_context->draw_horiz_band = NULL;
806
807     p_ff_pic->opaque = (void*)p_pic;
808     p_ff_pic->type = FF_BUFFER_TYPE_USER;
809     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
810     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
811     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
812     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
813
814     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
815     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
816     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
817     p_ff_pic->linesize[3] = 0;
818
819     if( p_ff_pic->reference != 0 )
820     {
821         vout_LinkPicture( p_vdec->p_vout, p_pic );
822     }
823     /* FIXME what is that, should give good value */
824     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
825
826     return( 0 );
827 }
828
829 static void  ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
830                                      AVFrame *p_ff_pic )
831 {
832     vdec_thread_t *p_vdec = (vdec_thread_t *)p_context->opaque;
833     picture_t *p_pic;
834
835     if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
836     {
837         avcodec_default_release_buffer( p_context, p_ff_pic );
838         return;
839     }
840
841     p_pic = (picture_t*)p_ff_pic->opaque;
842
843     p_ff_pic->data[0] = NULL;
844     p_ff_pic->data[1] = NULL;
845     p_ff_pic->data[2] = NULL;
846     p_ff_pic->data[3] = NULL;
847
848     if( p_ff_pic->reference != 0 )
849     {
850         vout_UnlinkPicture( p_vdec->p_vout, p_pic );
851     }
852 }