ab793e3306619fd3e45cd40953e54dd8755ad0a3
[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.37 2003/08/08 17:08:32 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     if( p_vdec->p_ff_pic->pts )
590     {
591         p_vdec->pts = p_vdec->p_ff_pic->pts;
592     }
593
594     if( p_vdec->pts <= 0 )
595     {
596         p_vdec->pts = mdate() + DEFAULT_PTS_DELAY;  // FIXME
597     }
598
599     /* Send decoded frame to vout */
600     vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->pts );
601     vout_DisplayPicture( p_vdec->p_vout, p_pic );
602
603     /* interpolate the next PTS */
604     if( p_vdec->p_context->frame_rate > 0 )
605     {
606         p_vdec->pts += I64C(1000000) * (2 + p_vdec->p_ff_pic->repeat_pict) *
607                        p_vdec->p_context->frame_rate_base /
608                        (2 * p_vdec->p_context->frame_rate);
609     }
610
611     if( i_frame_size > 0 )
612     {
613         goto usenextdata; /* try to use all data */
614     }
615 }
616
617 /*****************************************************************************
618  * EndThread: thread destruction
619  *****************************************************************************
620  * This function is called when the thread ends after a sucessful
621  * initialization.
622  *****************************************************************************/
623 void E_( EndThread_Video )( vdec_thread_t *p_vdec )
624 {
625
626 #ifdef LIBAVCODEC_PP
627     if( p_vdec->pp_mode )
628     {
629         pp_free_mode( p_vdec->pp_mode );
630         if( p_vdec->pp_context )
631         {
632             pp_free_context( p_vdec->pp_context );
633         }
634     }
635 #endif
636
637     if( p_vdec->p_ff_pic )
638     {
639         free( p_vdec->p_ff_pic );
640     }
641
642     /* We are about to die. Reattach video output to p_vlc. */
643     vout_Request( p_vdec->p_fifo, p_vdec->p_vout, 0, 0, 0, 0 );
644 }
645
646 /*****************************************************************************
647  * ffmpeg_CopyPicture: copy a picture from ffmpeg internal buffers to a
648  *                     picture_t structure (when not in direct rendering mode).
649  *****************************************************************************/
650 static void ffmpeg_CopyPicture( picture_t    *p_pic,
651                                 AVFrame *p_ff_pic,
652                                 vdec_thread_t *p_vdec )
653 {
654     int i_plane;
655     int i_size;
656     int i_line;
657
658     uint8_t *p_dst;
659     uint8_t *p_src;
660     int i_src_stride;
661     int i_dst_stride;
662
663     if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
664     {
665 #ifdef LIBAVCODEC_PP
666         if( p_vdec->pp_mode && p_vdec->pp_context )
667         {
668             uint8_t *src[3], *dst[3];
669             int     i_src_stride[3], i_dst_stride[3];
670
671             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
672             {
673                 src[i_plane] = p_ff_pic->data[i_plane];
674                 dst[i_plane] = p_pic->p[i_plane].p_pixels;
675
676                 i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
677                 i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
678             }
679             pp_postprocess( src, i_src_stride,
680                             dst, i_dst_stride,
681                             p_vdec->p_context->width,
682                             p_vdec->p_context->height,
683                             p_ff_pic->qscale_table, p_ff_pic->qstride,
684                             p_vdec->pp_mode, p_vdec->pp_context,
685                             p_ff_pic->pict_type );
686         }
687         else
688         {
689 #endif
690             for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
691             {
692                 p_src  = p_ff_pic->data[i_plane];
693                 p_dst = p_pic->p[i_plane].p_pixels;
694                 i_src_stride = p_ff_pic->linesize[i_plane];
695                 i_dst_stride = p_pic->p[i_plane].i_pitch;
696
697                 i_size = __MIN( i_src_stride, i_dst_stride );
698                 for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
699                 {
700                     p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
701                     p_src += i_src_stride;
702                     p_dst += i_dst_stride;
703                 }
704             }
705 #ifdef LIBAVCODEC_PP
706         }
707 #endif
708     }
709     else
710     {
711         /* we need to convert to I420 */
712         switch( p_vdec->p_context->pix_fmt )
713         {
714             AVPicture dest_pic;
715             int i;
716
717             case( PIX_FMT_YUV410P ):
718             case( PIX_FMT_YUV411P ):
719                 for( i = 0; i < p_pic->i_planes; i++ )
720                 {
721                     dest_pic.data[i] = p_pic->p[i].p_pixels;
722                     dest_pic.linesize[i] = p_pic->p[i].i_pitch;
723                 }
724                 img_convert( &dest_pic, PIX_FMT_YUV420P,
725                              (AVPicture *)p_ff_pic,
726                              p_vdec->p_context->pix_fmt,
727                              p_vdec->p_context->width,
728                              p_vdec->p_context->height );
729                 break;
730             default:
731                 msg_Err( p_vdec->p_fifo, "don't know how to convert chroma %i",
732                          p_vdec->p_context->pix_fmt );
733                 p_vdec->p_fifo->b_error = 1;
734                 break;
735         }
736     }
737 }
738
739 /*****************************************************************************
740  * ffmpeg_GetFrameBuf: callback used by ffmpeg to get a frame buffer.
741  *****************************************************************************
742  * It is used for direct rendering as well as to get the right PTS for each
743  * decoded picture (even in indirect rendering mode).
744  *****************************************************************************/
745 static int ffmpeg_GetFrameBuf( struct AVCodecContext *p_context,
746                                AVFrame *p_ff_pic )
747 {
748     vdec_thread_t *p_vdec = (vdec_thread_t *)p_context->opaque;
749     picture_t *p_pic;
750
751     /* Set picture PTS */
752     if( p_context->flags & CODEC_FLAG_TRUNCATED )
753     {
754         p_ff_pic->pts = p_vdec->input_pts_previous;
755         p_vdec->input_pts_previous = 0;
756     }
757     else
758     {
759         p_ff_pic->pts = p_vdec->input_pts;
760         p_vdec->input_pts = 0;
761     }
762
763     /* Not much to do in indirect rendering mode */
764     if( !p_vdec->b_direct_rendering )
765     {
766         return avcodec_default_get_buffer( p_context, p_ff_pic );
767     }
768
769     /* Some codecs set pix_fmt only after the 1st frame has been decoded,
770      * so this check is necessary. */
771     if( !ffmpeg_PixFmtToChroma( p_context->pix_fmt ) ||
772         p_vdec->p_context->width % 16 || p_vdec->p_context->height % 16 )
773     {
774         msg_Dbg( p_vdec->p_fifo, "disabling direct rendering" );
775         p_vdec->b_direct_rendering = 0;
776         return avcodec_default_get_buffer( p_context, p_ff_pic );
777     }
778
779     /* Check and (re)create our vout if needed */
780     p_vdec->p_vout = ffmpeg_CreateVout( p_vdec, p_vdec->p_context );
781     if( !p_vdec->p_vout )
782     {
783         msg_Err( p_vdec->p_fifo, "cannot create vout" );
784         p_vdec->p_fifo->b_error = 1; /* abort */
785         p_vdec->b_direct_rendering = 0;
786         return avcodec_default_get_buffer( p_context, p_ff_pic );
787     }
788
789     p_vdec->p_vout->render.b_allow_modify_pics = 0;
790
791     /* Get a new picture */
792     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
793     {
794         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
795         {
796             p_vdec->b_direct_rendering = 0;
797             return avcodec_default_get_buffer( p_context, p_ff_pic );
798         }
799         msleep( VOUT_OUTMEM_SLEEP );
800     }
801     p_vdec->p_context->draw_horiz_band = NULL;
802
803     p_ff_pic->opaque = (void*)p_pic;
804     p_ff_pic->type = FF_BUFFER_TYPE_USER;
805     p_ff_pic->data[0] = p_pic->p[0].p_pixels;
806     p_ff_pic->data[1] = p_pic->p[1].p_pixels;
807     p_ff_pic->data[2] = p_pic->p[2].p_pixels;
808     p_ff_pic->data[3] = NULL; /* alpha channel but I'm not sure */
809
810     p_ff_pic->linesize[0] = p_pic->p[0].i_pitch;
811     p_ff_pic->linesize[1] = p_pic->p[1].i_pitch;
812     p_ff_pic->linesize[2] = p_pic->p[2].i_pitch;
813     p_ff_pic->linesize[3] = 0;
814
815     if( p_ff_pic->reference != 0 )
816     {
817         vout_LinkPicture( p_vdec->p_vout, p_pic );
818     }
819     /* FIXME what is that, should give good value */
820     p_ff_pic->age = 256*256*256*64; // FIXME FIXME from ffmpeg
821
822     return( 0 );
823 }
824
825 static void  ffmpeg_ReleaseFrameBuf( struct AVCodecContext *p_context,
826                                      AVFrame *p_ff_pic )
827 {
828     vdec_thread_t *p_vdec = (vdec_thread_t *)p_context->opaque;
829     picture_t *p_pic;
830
831     if( p_ff_pic->type != FF_BUFFER_TYPE_USER )
832     {
833         avcodec_default_release_buffer( p_context, p_ff_pic );
834         return;
835     }
836
837     p_pic = (picture_t*)p_ff_pic->opaque;
838
839     p_ff_pic->data[0] = NULL;
840     p_ff_pic->data[1] = NULL;
841     p_ff_pic->data[2] = NULL;
842     p_ff_pic->data[3] = NULL;
843
844     if( p_ff_pic->reference != 0 )
845     {
846         vout_UnlinkPicture( p_vdec->p_vout, p_pic );
847     }
848 }