]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/ffmpeg.c
* ffmpeg : new way to gather a frame
[vlc] / modules / codec / ffmpeg / ffmpeg.c
1 /*****************************************************************************
2  * ffmpeg.c: video decoder using ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: ffmpeg.c,v 1.10 2002/10/24 10:33:09 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/vout.h>
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>                                              /* getpid() */
36 #endif
37
38 #include <errno.h>
39 #include <string.h>
40
41 #ifdef HAVE_SYS_TIMES_H
42 #   include <sys/times.h>
43 #endif
44
45 #include "avcodec.h"                                            /* ffmpeg */
46
47 #include "postprocessing/postprocessing.h"
48
49 #include "ffmpeg.h"
50
51 /*
52  * Local prototypes
53  */
54 static int      OpenDecoder     ( vlc_object_t * );
55 static int      RunDecoder      ( decoder_fifo_t * );
56 static int      InitThread      ( videodec_thread_t * );
57 static void     EndThread       ( videodec_thread_t * );
58 static void     DecodeThread    ( videodec_thread_t * );
59
60
61 static int      b_ffmpeginit = 0;
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66
67 #define ERROR_RESILIENCE_LONGTEXT \
68     "ffmpeg can make errors resiliences.          \n"\
69     "Nevertheless, with buggy encoder (like ISO MPEG-4 encoder from M$) " \
70     "this will produce a lot of errors.\n" \
71     "Valid range is -1 to 99 (-1 disable all errors resiliences)."
72
73 #define HURRY_UP_LONGTEXT \
74     "Allow the decoder to partially decode or skip frame(s) " \
75     "when there not enough time.\n It's usefull with low CPU power " \
76     "but it could produce broken pictures."
77
78 #define POSTPROCESSING_Q_LONGTEXT \
79     "Quality of post processing\n"\
80     "Valid range is 0 to 6\n" \
81     "( Overridden by others setting)"
82     
83 #define POSTPROCESSING_AQ_LONGTEXT \
84     "Post processing quality is selected upon time left" \
85     "but no more than requested quality\n" \
86     "Not yet implemented !"
87
88 #define WORAROUND_BUG_LONGTEXT \
89     "Try to fix some bugs\n" \
90     "1  autodetect\n" \
91     "2  old msmpeg4\n" \
92     "4  xvid interlaced\n" \
93     "8  ump4 \n" \
94     "16 no padding\n" \
95     "32 ac vlc" \
96     "64 Qpel chroma"
97
98 vlc_module_begin();
99     add_category_hint( N_("Ffmpeg"), NULL );
100 #if LIBAVCODEC_BUILD >= 4611
101     add_integer ( "ffmpeg-error-resilience", -1, NULL, 
102                   "error resilience", ERROR_RESILIENCE_LONGTEXT );
103     add_integer ( "ffmpeg-workaround-bugs", 0, NULL, 
104                   "workaround bugs", WORAROUND_BUG_LONGTEXT );
105 #endif
106     add_bool( "ffmpeg-hurry-up", 0, NULL, "hurry up", HURRY_UP_LONGTEXT );
107     
108     add_category_hint( N_("Post processing"), NULL );
109     add_module( "ffmpeg-pp", "postprocessing",NULL, NULL,
110                 N_( "ffmpeg postprocessing module" ), NULL ); 
111     add_integer( "ffmpeg-pp-q", 0, NULL,
112                  "post processing quality", POSTPROCESSING_Q_LONGTEXT );
113     add_bool( "ffmpeg-pp-auto", 0, NULL,
114               "auto-level Post processing quality", POSTPROCESSING_AQ_LONGTEXT );
115     add_bool( "ffmpeg-db-yv", 0, NULL, 
116               "force vertical luminance deblocking", 
117               "force vertical luminance deblocking (override other settings)" );
118     add_bool( "ffmpeg-db-yh", 0, NULL, 
119               "force horizontal luminance deblocking",
120               "force horizontal luminance deblocking (override other settings)" );
121     add_bool( "ffmpeg-db-cv", 0, NULL, 
122               "force vertical chrominance deblocking",
123               "force vertical chrominance deblocking (override other settings)" );
124     add_bool( "ffmpeg-db-ch", 0, NULL, 
125               "force horizontal chrominance deblocking",
126               "force horizontal chrominance deblocking (override other settings) " );
127     add_bool( "ffmpeg-dr-y", 0, NULL,
128               "force luminance deringing",
129               "force luminance deringing (override other settings)" );
130     add_bool( "ffmpeg-dr-c", 0, NULL,
131               "force chrominance deringing",
132               "force chrominance deringing (override other settings)" );
133       
134     set_description( _("ffmpeg video decoder((MS)MPEG4,SVQ1,H263)") );
135     set_capability( "decoder", 70 );
136     set_callbacks( OpenDecoder, NULL );
137 vlc_module_end();
138
139 /*****************************************************************************
140  * OpenDecoder: probe the decoder and return score
141  *****************************************************************************
142  * Tries to launch a decoder and return score so that the interface is able 
143  * to chose.
144  *****************************************************************************/
145 static int OpenDecoder( vlc_object_t *p_this )
146 {
147     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
148
149     if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL ) )
150     {
151         p_fifo->pf_run = RunDecoder;
152         return VLC_SUCCESS;
153     }
154
155     return VLC_EGENERIC;
156 }
157
158 /*****************************************************************************
159  * RunDecoder: this function is called just after the thread is created
160  *****************************************************************************/
161 static int RunDecoder( decoder_fifo_t *p_fifo )
162 {
163     videodec_thread_t   *p_vdec;
164     int b_error;
165
166     if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
167     {
168         msg_Err( p_fifo, "out of memory" );
169         DecoderError( p_fifo );
170         return( -1 );
171     }
172     memset( p_vdec, 0, sizeof( videodec_thread_t ) );
173
174     p_vdec->p_fifo = p_fifo;
175
176     if( InitThread( p_vdec ) != 0 )
177     {
178         DecoderError( p_fifo );
179         return( -1 );
180     }
181      
182     while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
183     {
184         DecodeThread( p_vdec );
185     }
186
187     if( ( b_error = p_vdec->p_fifo->b_error ) )
188     {
189         DecoderError( p_vdec->p_fifo );
190     }
191
192     EndThread( p_vdec );
193
194     if( b_error )
195     {
196         return( -1 );
197     }
198    
199     return( 0 );
200
201
202
203 /*****************************************************************************
204  * locales Functions
205  *****************************************************************************/
206
207 #define GetWLE( p ) \
208     ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
209
210 #define GetDWLE( p ) \
211     (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
212       ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
213
214 #define FREE( p ) if( p ) free( p ); p = NULL
215     
216 static void ffmpeg_ParseBitMapInfoHeader( bitmapinfoheader_t *p_bh, 
217                                           u8 *p_data )
218 {
219     p_bh->i_size          = GetDWLE( p_data );
220     p_bh->i_width         = GetDWLE( p_data + 4 );
221     p_bh->i_height        = GetDWLE( p_data + 8 );
222     p_bh->i_planes        = GetWLE( p_data + 12 );
223     p_bh->i_bitcount      = GetWLE( p_data + 14 );
224     p_bh->i_compression   = GetDWLE( p_data + 16 );
225     p_bh->i_sizeimage     = GetDWLE( p_data + 20 );
226     p_bh->i_xpelspermeter = GetDWLE( p_data + 24 );
227     p_bh->i_ypelspermeter = GetDWLE( p_data + 28 );
228     p_bh->i_clrused       = GetDWLE( p_data + 32 );
229     p_bh->i_clrimportant  = GetDWLE( p_data + 36 );
230
231     if( p_bh->i_size > 40 )
232     {
233         p_bh->i_data = p_bh->i_size - 40;
234         if( ( p_bh->p_data = malloc( p_bh->i_data ) ) )
235         {
236             memcpy( p_bh->p_data, p_data + 40, p_bh->i_data );
237         }
238         else
239         {
240             p_bh->i_data = 0;
241         }
242     }
243     else
244     {
245         p_bh->i_data = 0;
246         p_bh->p_data = NULL;
247     } 
248
249 }
250
251 static void GetPESData( u8 *p_buf, int i_max, pes_packet_t *p_pes )
252 {   
253     int i_copy; 
254     int i_count;
255
256     data_packet_t   *p_data;
257
258     i_count = 0;
259     p_data = p_pes->p_first;
260     while( p_data != NULL && i_count < i_max )
261     {
262
263         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, 
264                         i_max - i_count );
265
266         if( i_copy > 0 )
267         {
268             memcpy( p_buf,
269                     p_data->p_payload_start,
270                     i_copy );
271         }
272
273         p_data = p_data->p_next;
274         i_count += i_copy;
275         p_buf   += i_copy;
276     }
277
278     if( i_count < i_max )
279     {
280         memset( p_buf, 0, i_max - i_count );
281     }
282 }
283
284 /* Check if we have a Vout with good parameters */
285 static int ffmpeg_CheckVout( vout_thread_t *p_vout,
286                              int i_width,
287                              int i_height,
288                              int i_aspect,
289                              int i_chroma )
290 {
291     if( !p_vout )
292     {
293         return( 0 );
294     }
295     if( !i_chroma )
296     {
297         /* we will try to make conversion */
298         i_chroma = VLC_FOURCC('I','4','2','0');
299     } 
300     
301     if( ( p_vout->render.i_width != i_width )||
302         ( p_vout->render.i_height != i_height )||
303         ( p_vout->render.i_chroma != i_chroma )||
304         ( p_vout->render.i_aspect != 
305                 ffmpeg_FfAspect( i_width, i_height, i_aspect ) ) )
306     {
307         return( 0 );
308     }
309     else
310     {
311         return( 1 );
312     }
313 }
314
315 /* Return a Vout */
316
317 static vout_thread_t *ffmpeg_CreateVout( videodec_thread_t *p_vdec,
318                                          int i_width,
319                                          int i_height,
320                                          int i_aspect,
321                                          int i_chroma )
322 {
323     vout_thread_t *p_vout;
324
325     if( (!i_width)||(!i_height) )
326     {
327         return( NULL ); /* Can't create a new vout without display size */
328     }
329
330     if( !i_chroma )
331     {
332         /* we make conversion if possible*/
333         i_chroma = VLC_FOURCC('I','4','2','0');
334         msg_Warn( p_vdec->p_fifo, "Internal chroma conversion (FIXME)");
335         /* It's mainly for I410 -> I420 conversion that I've made,
336            it's buggy and very slow */
337     } 
338
339     i_aspect = ffmpeg_FfAspect( i_width, i_height, i_aspect );
340     
341     /* Spawn a video output if there is none. First we look for our children,
342      * then we look for any other vout that might be available. */
343     p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
344                                               FIND_CHILD );
345     if( !p_vout )
346     {
347         p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
348                                                   FIND_ANYWHERE );
349     }
350
351     if( p_vout )
352     {
353         if( !ffmpeg_CheckVout( p_vout, 
354                                i_width, i_height, i_aspect,i_chroma ) )
355         {
356             /* We are not interested in this format, close this vout */
357             vlc_object_detach( p_vout );
358             vlc_object_release( p_vout );
359             vout_DestroyThread( p_vout );
360             p_vout = NULL;
361         }
362         else
363         {
364             /* This video output is cool! Hijack it. */
365             vlc_object_detach( p_vout );
366             vlc_object_attach( p_vout, p_vdec->p_fifo );
367             vlc_object_release( p_vout );
368         }
369     }
370
371     if( p_vout == NULL )
372     {
373         msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
374     
375         p_vout = vout_CreateThread( p_vdec->p_fifo,
376                                     i_width, i_height,
377                                     i_chroma, i_aspect );
378     }
379     
380     return( p_vout );
381 }
382
383 /* FIXME FIXME FIXME this is a big shit
384    does someone want to rewrite this function ? 
385    or said to me how write a better thing
386    FIXME FIXME FIXME
387 */
388 static void ffmpeg_ConvertPictureI410toI420( picture_t *p_pic,
389                                              AVPicture *p_avpicture,
390                                              videodec_thread_t   *p_vdec )
391 {
392     u8 *p_src, *p_dst;
393     u8 *p_plane[3];
394     int i_plane;
395     
396     int i_stride, i_lines;
397     int i_height, i_width;
398     int i_y, i_x;
399     
400     i_height = p_vdec->p_context->height;
401     i_width  = p_vdec->p_context->width;
402     
403     p_dst = p_pic->p[0].p_pixels;
404     p_src  = p_avpicture->data[0];
405
406     /* copy first plane */
407     for( i_y = 0; i_y < i_height; i_y++ )
408     {
409         p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width);
410         p_dst += p_pic->p[0].i_pitch;
411         p_src += p_avpicture->linesize[0];
412     }
413     
414     /* process each plane in a temporary buffer */
415     for( i_plane = 1; i_plane < 3; i_plane++ )
416     {
417         i_stride = p_avpicture->linesize[i_plane];
418         i_lines = i_height / 4;
419
420         p_dst = p_plane[i_plane] = malloc( i_lines * i_stride * 2 * 2 );
421         p_src  = p_avpicture->data[i_plane];
422
423         /* for each source line */
424         for( i_y = 0; i_y < i_lines; i_y++ )
425         {
426             for( i_x = 0; i_x < i_stride - 1; i_x++ )
427             {
428                 p_dst[2 * i_x    ] = p_src[i_x];
429                 p_dst[2 * i_x + 1] = ( p_src[i_x] + p_src[i_x + 1]) / 2;
430
431             }
432             p_dst[2 * i_stride - 2] = p_src[i_x];
433             p_dst[2 * i_stride - 1] = p_src[i_x];
434                            
435             p_dst += 4 * i_stride; /* process the next even lines */
436             p_src += i_stride;
437         }
438
439
440     }
441
442     for( i_plane = 1; i_plane < 3; i_plane++ )
443     {
444         i_stride = p_avpicture->linesize[i_plane];
445         i_lines = i_height / 4;
446
447         p_dst = p_plane[i_plane] + 2*i_stride;
448         p_src  = p_plane[i_plane];
449
450         for( i_y = 0; i_y < i_lines - 1; i_y++ )
451         {
452             for( i_x = 0; i_x <  2 * i_stride ; i_x++ )
453             {
454                 p_dst[i_x] = ( p_src[i_x] + p_src[i_x + 4*i_stride])/2;
455             }
456                            
457             p_dst += 4 * i_stride; /* process the next odd lines */
458             p_src += 4 * i_stride;
459         }
460         /* last line */
461         p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, 2*i_stride );
462     }
463     /* copy to p_pic, by block
464        if I do pixel per pixel it segfault. It's why I use 
465        temporaries buffers */
466     for( i_plane = 1; i_plane < 3; i_plane++ )
467     {
468
469         int i_size; 
470         p_src  = p_plane[i_plane];
471         p_dst = p_pic->p[i_plane].p_pixels;
472
473         i_size = __MIN( 2*i_stride, p_pic->p[i_plane].i_pitch);
474         for( i_y = 0; i_y < __MIN(p_pic->p[i_plane].i_lines, 2 * i_lines); i_y++ )
475         {
476             p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
477             p_src += 2 * i_stride;
478             p_dst += p_pic->p[i_plane].i_pitch;
479         }
480         free( p_plane[i_plane] );
481     }
482
483 }
484
485 static void ffmpeg_GetPicture( picture_t *p_pic,
486                                AVPicture *p_avpicture,
487                                videodec_thread_t   *p_vdec )
488 {
489     int i_plane; 
490     int i_size;
491     int i_line;
492
493     u8  *p_dst;
494     u8  *p_src;
495     int i_src_stride;
496     int i_dst_stride;
497
498     if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
499     {
500         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
501         {
502             p_src  = p_avpicture->data[i_plane];
503             p_dst = p_pic->p[i_plane].p_pixels;
504             i_src_stride = p_avpicture->linesize[i_plane];
505             i_dst_stride = p_pic->p[i_plane].i_pitch;
506             
507             i_size = __MIN( i_src_stride, i_dst_stride );
508             for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
509             {
510                 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
511                 p_src += i_src_stride;
512                 p_dst += i_dst_stride;
513             }
514         }
515         if( ( p_vdec->i_pp_mode )&&
516             ( ( p_vdec->p_vout->render.i_chroma == 
517                     VLC_FOURCC( 'I','4','2','0' ) )||
518               ( p_vdec->p_vout->render.i_chroma == 
519                     VLC_FOURCC( 'Y','V','1','2' ) ) ) )
520         {
521             /* Make postproc */
522 #if LIBAVCODEC_BUILD > 4313
523             p_vdec->p_pp->pf_postprocess( p_pic,
524                                           p_vdec->p_context->quant_store, 
525                                           p_vdec->p_context->qstride,
526                                           p_vdec->i_pp_mode );
527 #endif
528         }
529     }
530     else
531     {
532         /* we need to convert to I420 */
533         switch( p_vdec->p_context->pix_fmt )
534         {
535 #if LIBAVCODEC_BUILD >= 4615
536             case( PIX_FMT_YUV410P ):
537                 ffmpeg_ConvertPictureI410toI420( p_pic, p_avpicture, p_vdec );
538                 break;
539 #endif            
540             default:
541                 p_vdec->p_fifo->b_error = 1;
542                 break;
543         }
544
545     }
546   
547 }
548
549
550
551 /*****************************************************************************
552  *
553  * Functions that initialize, decode and end the decoding process
554  *
555  *****************************************************************************/
556
557 /*****************************************************************************
558  * InitThread: initialize vdec output thread
559  *****************************************************************************
560  * This function is called from decoder_Run and performs the second step 
561  * of the initialization. It returns 0 on success. Note that the thread's 
562  * flag are not modified inside this function.
563  *
564  * ffmpeg codec will be open, some memory allocated. But Vout is not yet
565  *   open (done after the first decoded frame)
566  *****************************************************************************/
567 static int InitThread( videodec_thread_t *p_vdec )
568 {
569     int i_ffmpeg_codec; 
570     int i_tmp;
571     
572     if( p_vdec->p_fifo->p_demux_data )
573     {
574         ffmpeg_ParseBitMapInfoHeader( &p_vdec->format, 
575                                       (u8*)p_vdec->p_fifo->p_demux_data );
576     }
577     else
578     {
579         msg_Warn( p_vdec->p_fifo, "display informations missing" );
580     }
581
582     /* **** init ffmpeg library (libavcodec) ***** */
583     if( !b_ffmpeginit )
584     {
585         avcodec_init();
586         avcodec_register_all();
587         b_ffmpeginit = 1;
588         msg_Dbg( p_vdec->p_fifo, "library ffmpeg initialized" );
589     }
590     else
591     {
592         msg_Dbg( p_vdec->p_fifo, "library ffmpeg already initialized" );
593     }
594
595     /* ***** Search for codec ***** */
596     ffmpeg_GetFfmpegCodec( p_vdec->p_fifo->i_fourcc,
597                            &i_ffmpeg_codec,
598                            &p_vdec->psz_namecodec );
599     p_vdec->p_codec = 
600         avcodec_find_decoder( i_ffmpeg_codec );
601     
602     if( !p_vdec->p_codec )
603     {
604         msg_Err( p_vdec->p_fifo, "codec not found (%s)",
605                                  p_vdec->psz_namecodec );
606         return( -1 );
607     }
608
609     /* ***** Fill p_context with init values ***** */
610 #if LIBAVCODEC_BUILD >= 4624
611     p_vdec->p_context = avcodec_alloc_context();
612 #else
613     p_vdec->p_context = malloc( sizeof( AVCodecContext ) );
614     memset( p_vdec->p_context, 0, sizeof( AVCodecContext ) );
615 #endif
616
617     p_vdec->p_context->width  = p_vdec->format.i_width;
618     p_vdec->p_context->height = p_vdec->format.i_height;
619     
620     /*  ***** Get configuration of ffmpeg plugin ***** */
621 #if LIBAVCODEC_BUILD >= 4611
622     i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
623     p_vdec->p_context->workaround_bugs  = __MAX( __MIN( i_tmp, 99 ), 0 );
624
625     i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
626     p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
627 #endif
628 #if LIBAVCODEC_BUILD >= 4614
629     if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
630     {
631         p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
632     }
633 #endif
634     p_vdec->b_hurry_up = config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up");
635     
636
637     /* ***** Open the codec ***** */ 
638     if (avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0)
639     {
640         msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
641                                  p_vdec->psz_namecodec );
642         return( -1 );
643     }
644     else
645     {
646         msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
647                                  p_vdec->psz_namecodec );
648     }
649
650     /* ***** init this codec with special data ***** */
651     if( p_vdec->format.i_data )
652     {
653         AVPicture avpicture;
654         int b_gotpicture;
655         
656         switch( i_ffmpeg_codec )
657         {
658             case( CODEC_ID_MPEG4 ):
659                 avcodec_decode_video( p_vdec->p_context, &avpicture, 
660                                       &b_gotpicture,
661                                       p_vdec->format.p_data,
662                                       p_vdec->format.i_data );
663                 break;
664             default:
665                 if( p_vdec->p_fifo->i_fourcc == FOURCC_MP4S ||
666                     p_vdec->p_fifo->i_fourcc == FOURCC_mp4s ||
667                     p_vdec->p_fifo->i_fourcc == FOURCC_M4S2 ||
668                     p_vdec->p_fifo->i_fourcc == FOURCC_m4s2 )
669                 {
670                     p_vdec->p_context->extradata_size = p_vdec->format.i_data;
671                     p_vdec->p_context->extradata = malloc( p_vdec->format.i_data );
672                     memcpy( p_vdec->p_context->extradata,
673                             p_vdec->format.p_data,
674                             p_vdec->format.i_data );
675                 }
676
677                 break;
678         }
679     }
680     
681     /* ***** Load post processing ***** */
682
683     /* get overridding settings */
684     p_vdec->i_pp_mode = 0;
685     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yv" ) )
686         p_vdec->i_pp_mode |= PP_DEBLOCK_Y_V;
687     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-yh" ) )
688         p_vdec->i_pp_mode |= PP_DEBLOCK_Y_H;
689     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-cv" ) )
690         p_vdec->i_pp_mode |= PP_DEBLOCK_C_V;
691     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-db-ch" ) )
692         p_vdec->i_pp_mode |= PP_DEBLOCK_C_H;
693     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-y" ) )
694         p_vdec->i_pp_mode |= PP_DERING_Y;
695     if( config_GetInt( p_vdec->p_fifo, "ffmpeg-dr-c" ) )
696         p_vdec->i_pp_mode |= PP_DERING_C;
697     
698     if( ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ) > 0 )||
699         ( config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" )  )||
700         ( p_vdec->i_pp_mode != 0 ) )
701     {
702         /* check if the codec support postproc. */
703         switch( i_ffmpeg_codec )
704         {
705 #if LIBAVCODEC_BUILD > 4608
706             case( CODEC_ID_MSMPEG4V1 ):
707             case( CODEC_ID_MSMPEG4V2 ):
708             case( CODEC_ID_MSMPEG4V3 ):
709 #else
710             case( CODEC_ID_MSMPEG4 ):
711 #endif
712             case( CODEC_ID_MPEG4 ):
713             case( CODEC_ID_H263 ):
714 //            case( CODEC_ID_H263P ): I don't use it up to now
715             case( CODEC_ID_H263I ):
716                 /* Ok we can make postprocessing :)) */
717                /* first try to get a postprocess module */
718 #if LIBAVCODEC_BUILD > 4613
719                 p_vdec->p_pp = vlc_object_create( p_vdec->p_fifo,
720                                                   sizeof( postprocessing_t ) );
721                 p_vdec->p_pp->psz_object_name = "postprocessing";
722                 p_vdec->p_pp->p_module = 
723                    module_Need( p_vdec->p_pp, "postprocessing", "$ffmpeg-pp" );
724
725                 if( !p_vdec->p_pp->p_module )
726                 {
727                     msg_Warn( p_vdec->p_fifo, 
728                               "no suitable postprocessing module" );
729                     vlc_object_destroy( p_vdec->p_pp );
730                     p_vdec->p_pp = NULL;
731                     p_vdec->i_pp_mode = 0;
732                 }
733                 else
734                 {
735                     /* get mode upon quality */
736                     p_vdec->i_pp_mode |= 
737                         p_vdec->p_pp->pf_getmode( 
738                               config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-q" ),
739                               config_GetInt( p_vdec->p_fifo, "ffmpeg-pp-auto" )
740                                                 );
741
742                     /* allocate table for postprocess */
743                     p_vdec->p_context->quant_store = 
744                         malloc( sizeof( int ) * ( MBR + 1 ) * ( MBC + 1 ) );
745                     p_vdec->p_context->qstride = MBC + 1;
746                 }
747 #else
748                 p_vdec->i_pp_mode = 0;
749                 msg_Warn( p_vdec->p_fifo, 
750                           "post-processing not supported, upgrade ffmpeg" );
751 #endif
752                 break;
753             default:
754                 p_vdec->i_pp_mode = 0;
755                 msg_Warn( p_vdec->p_fifo, 
756                           "Post processing unsupported for this codec" );
757                 break;
758         }
759
760     }
761 //    memset( &p_vdec->statistic, 0, sizeof( statistic_t ) );
762
763     return( 0 );
764 }
765
766 /*****************************************************************************
767  * DecodeThread: Called for decode one frame
768  *****************************************************************************/
769 static void  DecodeThread( videodec_thread_t *p_vdec )
770 {
771     pes_packet_t    *p_pes;
772     int     i_frame_size;
773     int     i_status;
774     int     b_drawpicture;
775     int     b_gotpicture;
776     AVPicture avpicture;  /* ffmpeg picture */
777     picture_t *p_pic; /* videolan picture */
778     /* we have to get a frame stored in a pes 
779        give it to ffmpeg decoder 
780        and send the image to the output */ 
781
782     /* TODO implement it in a better way */
783     /* A good idea could be to decode all I pictures and see for the other */
784     if( ( p_vdec->b_hurry_up )&&
785         ( p_vdec->i_frame_late > 4 ) )
786     {
787 #if LIBAVCODEC_BUILD > 4603
788         b_drawpicture = 0;
789         if( p_vdec->i_frame_late < 8 )
790         {
791             p_vdec->p_context->hurry_up = 2;
792         }
793         else
794         {
795             /* too much late picture, won't decode 
796                but break picture until a new I, and for mpeg4 ...*/
797             p_vdec->i_frame_late--; /* needed else it will never be decrease */
798             NextPES( p_vdec->p_fifo );
799             return;
800         }
801 #else
802         if( p_vdec->i_frame_late < 8 )
803         {
804             b_drawpicture = 0; /* not really good but .. UPGRADE FFMPEG !! */
805         }
806         else
807         {
808             /* too much late picture, won't decode 
809                but break picture until a new I, and for mpeg4 ...*/
810             p_vdec->i_frame_late--; /* needed else it will never be decrease */
811             NextPES( p_vdec->p_fifo );
812             return;
813         }
814 #endif
815     }
816     else
817     {
818         b_drawpicture = 1;
819 #if LIBAVCODEC_BUILD > 4603
820         p_vdec->p_context->hurry_up = 0;
821 #endif
822     }
823
824     do
825     {
826         if( !( p_pes = GetPES( p_vdec->p_fifo ) ) )
827         {
828             p_vdec->p_fifo->b_error = 1;
829             return;
830         }
831         p_vdec->i_pts = p_pes->i_pts;
832         i_frame_size = p_pes->i_pes_size;
833
834         if( i_frame_size > 0 )
835         {
836             if( p_vdec->i_buffer < i_frame_size + 16 )
837             {
838                 FREE( p_vdec->p_buffer );
839                 p_vdec->p_buffer = malloc( i_frame_size + 16 );
840                 p_vdec->i_buffer = i_frame_size + 16;
841             }
842             
843             GetPESData( p_vdec->p_buffer, p_vdec->i_buffer, p_pes );
844         }
845         NextPES( p_vdec->p_fifo );
846     } while( i_frame_size <= 0 );
847
848
849     i_status = avcodec_decode_video( p_vdec->p_context,
850                                      &avpicture,
851                                      &b_gotpicture,
852                                      p_vdec->p_buffer,
853                                      i_frame_size );
854
855
856     if( i_status < 0 )
857     {
858         msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
859                                   i_frame_size );
860         p_vdec->i_frame_error++;
861         return;
862     }
863     /* Update frame late count*/
864     /* I don't make statistic on decoding time */
865     if( p_vdec->i_pts <= mdate()) 
866     {
867         p_vdec->i_frame_late++;
868     }
869     else
870     {
871         p_vdec->i_frame_late = 0;
872     }
873
874     if( !b_gotpicture || avpicture.linesize[0] == 0 || !b_drawpicture)
875     {
876         return;
877     }
878     
879     /* Check our vout */
880     if( !ffmpeg_CheckVout( p_vdec->p_vout,
881                            p_vdec->p_context->width,
882                            p_vdec->p_context->height,
883                            p_vdec->p_context->aspect_ratio_info,
884                            ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
885     {
886         p_vdec->p_vout = 
887           ffmpeg_CreateVout( p_vdec,
888                              p_vdec->p_context->width,
889                              p_vdec->p_context->height,
890                              p_vdec->p_context->aspect_ratio_info,
891                              ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt));
892         if( !p_vdec->p_vout )
893         {
894             msg_Err( p_vdec->p_fifo, "cannot create vout" );
895             p_vdec->p_fifo->b_error = 1; /* abort */
896             return;
897         }
898     }
899
900     /* Get a new picture */
901     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
902     {
903         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
904         {
905             return;
906         }
907         msleep( VOUT_OUTMEM_SLEEP );
908     }
909     /* fill p_picture_t from avpicture, do I410->I420 if needed
910        and do post-processing if requested */    
911     ffmpeg_GetPicture( p_pic, &avpicture, p_vdec );
912
913     /* FIXME correct avi and use i_dts */
914
915     /* Send decoded frame to vout */
916     vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
917     vout_DisplayPicture( p_vdec->p_vout, p_pic );
918     
919     return;
920 }
921
922
923 /*****************************************************************************
924  * EndThread: thread destruction
925  *****************************************************************************
926  * This function is called when the thread ends after a sucessful
927  * initialization.
928  *****************************************************************************/
929 static void EndThread( videodec_thread_t *p_vdec )
930 {
931     
932     if( !p_vdec )
933     {
934         return;
935     }
936     if( p_vdec->p_pp )
937     {
938         /* release postprocessing module */
939         module_Unneed( p_vdec->p_pp, p_vdec->p_pp->p_module );
940         vlc_object_destroy( p_vdec->p_pp );
941         p_vdec->p_pp = NULL;
942     }
943
944     if( p_vdec->p_context != NULL)
945     {
946         FREE( p_vdec->p_context->quant_store );
947         FREE( p_vdec->p_context->extradata );
948         avcodec_close( p_vdec->p_context );
949         msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
950                                  p_vdec->psz_namecodec );
951         free( p_vdec->p_context );
952     }
953
954     if( p_vdec->p_vout != NULL )
955     {
956         /* We are about to die. Reattach video output to p_vlc. */
957         vlc_object_detach( p_vdec->p_vout );
958         vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
959     }
960
961     FREE( p_vdec->format.p_data );
962     FREE( p_vdec->p_buffer );
963
964     free( p_vdec );
965 }
966