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