]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/ffmpeg.c
* ./modules/*: moved plugins to the new tree. Yet untested builds include
[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.1 2002/08/04 17:23:42 sam 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 "vdec_ext-plugins.h"
46 #include "avcodec.h"                                            /* ffmpeg */
47 #include "ffmpeg.h"
48
49 /*
50  * Local prototypes
51  */
52 static int      OpenDecoder     ( vlc_object_t * );
53 static int      RunDecoder      ( decoder_fifo_t * );
54 static int      InitThread      ( videodec_thread_t * );
55 static void     EndThread       ( videodec_thread_t * );
56 static void     DecodeThread    ( videodec_thread_t * );
57
58
59 static int      b_ffmpeginit = 0;
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64
65 #define ERROR_RESILIENCE_LONGTEXT \
66     "ffmpeg can make errors resiliences.          \n"\
67     "Nevertheless, with buggy encoder (like ISO MPEG-4 encoder from M$) " \
68     "this will produce a lot of errors.\n" \
69     "Valid range is -1 to 99 (-1 disable all errors resiliences)."
70
71 #define HURRY_UP_LONGTEXT \
72     "Allow the decoder to partially decode or skip frame(s) " \
73     "when there not enough time.\n It's usefull with low CPU power " \
74     "but it could produce broken pictures."
75     
76 vlc_module_begin();
77     add_category_hint( N_("Miscellaneous"), NULL );
78 #if LIBAVCODEC_BUILD >= 4611
79     add_integer ( "ffmpeg-error-resilience", 0, NULL, 
80                   "error resilience", ERROR_RESILIENCE_LONGTEXT );
81     add_integer ( "ffmpeg-workaround-bugs", 0, NULL, 
82                   "workaround bugs", "0-99, seems to be for msmpeg v3\n"  );
83 #endif
84     add_bool( "ffmpeg-hurry-up", 0, NULL, "hurry up", HURRY_UP_LONGTEXT );
85     set_description( _("ffmpeg video decoder((MS)MPEG4,SVQ1,H263)") );
86     set_capability( "decoder", 70 );
87     set_callbacks( OpenDecoder, NULL );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * OpenDecoder: probe the decoder and return score
92  *****************************************************************************
93  * Tries to launch a decoder and return score so that the interface is able 
94  * to chose.
95  *****************************************************************************/
96 static int OpenDecoder( vlc_object_t *p_this )
97 {
98     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
99
100     if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL ) )
101     {
102         p_fifo->pf_run = RunDecoder;
103         return VLC_SUCCESS;
104     }
105
106     return VLC_EGENERIC;
107 }
108
109 /*****************************************************************************
110  * RunDecoder: this function is called just after the thread is created
111  *****************************************************************************/
112 static int RunDecoder( decoder_fifo_t *p_fifo )
113 {
114     videodec_thread_t   *p_vdec;
115     int b_error;
116
117     if ( !(p_vdec = (videodec_thread_t*)malloc( sizeof(videodec_thread_t))) )
118     {
119         msg_Err( p_fifo, "out of memory" );
120         DecoderError( p_fifo );
121         return( -1 );
122     }
123     memset( p_vdec, 0, sizeof( videodec_thread_t ) );
124
125     p_vdec->p_fifo = p_fifo;
126
127     if( InitThread( p_vdec ) != 0 )
128     {
129         DecoderError( p_fifo );
130         return( -1 );
131     }
132      
133     while( (!p_vdec->p_fifo->b_die) && (!p_vdec->p_fifo->b_error) )
134     {
135         DecodeThread( p_vdec );
136     }
137
138     if( ( b_error = p_vdec->p_fifo->b_error ) )
139     {
140         DecoderError( p_vdec->p_fifo );
141     }
142
143     EndThread( p_vdec );
144
145     if( b_error )
146     {
147         return( -1 );
148     }
149    
150     return( 0 );
151
152
153
154 /*****************************************************************************
155  * locales Functions
156  *****************************************************************************/
157
158 #define GetWLE( p ) \
159     ( *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) )
160
161 #define GetDWLE( p ) \
162     (  *(u8*)(p) + ( *((u8*)(p)+1) << 8 ) + \
163       ( *((u8*)(p)+2) << 16 ) + ( *((u8*)(p)+3) << 24 ) )
164
165 static void ffmpeg_ParseBitMapInfoHeader( bitmapinfoheader_t *p_bh, 
166                                           u8 *p_data )
167 {
168     p_bh->i_size          = GetDWLE( p_data );
169     p_bh->i_width         = GetDWLE( p_data + 4 );
170     p_bh->i_height        = GetDWLE( p_data + 8 );
171     p_bh->i_planes        = GetWLE( p_data + 12 );
172     p_bh->i_bitcount      = GetWLE( p_data + 14 );
173     p_bh->i_compression   = GetDWLE( p_data + 16 );
174     p_bh->i_sizeimage     = GetDWLE( p_data + 20 );
175     p_bh->i_xpelspermeter = GetDWLE( p_data + 24 );
176     p_bh->i_ypelspermeter = GetDWLE( p_data + 28 );
177     p_bh->i_clrused       = GetDWLE( p_data + 32 );
178     p_bh->i_clrimportant  = GetDWLE( p_data + 36 );
179
180     if( p_bh->i_size > 40 )
181     {
182         p_bh->i_data = p_bh->i_size - 40;
183         p_bh->p_data = malloc( p_bh->i_data ); 
184         memcpy( p_bh->p_data, p_data + 40, p_bh->i_data );
185     }
186     else
187     {
188         p_bh->i_data = 0;
189         p_bh->p_data = NULL;
190     } 
191
192 }
193 /* get the first pes from fifo */
194 static pes_packet_t *__PES_GET( decoder_fifo_t *p_fifo )
195 {
196     pes_packet_t *p_pes;
197
198     vlc_mutex_lock( &p_fifo->data_lock );
199
200     /* if fifo is emty wait */
201     while( !p_fifo->p_first )
202     {
203         if( p_fifo->b_die )
204         {
205             vlc_mutex_unlock( &p_fifo->data_lock );
206             return( NULL );
207         }
208         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
209     }
210     p_pes = p_fifo->p_first;
211
212     vlc_mutex_unlock( &p_fifo->data_lock );
213
214     return( p_pes );
215 }
216
217 /* free the first pes and go to next */
218 static void __PES_NEXT( decoder_fifo_t *p_fifo )
219 {
220     pes_packet_t *p_next;
221
222     vlc_mutex_lock( &p_fifo->data_lock );
223     
224     p_next = p_fifo->p_first->p_next;
225     p_fifo->p_first->p_next = NULL;
226     input_DeletePES( p_fifo->p_packets_mgt, p_fifo->p_first );
227     p_fifo->p_first = p_next;
228     p_fifo->i_depth--;
229
230     if( !p_fifo->p_first )
231     {
232         /* No PES in the fifo */
233         /* pp_last no longer valid */
234         p_fifo->pp_last = &p_fifo->p_first;
235         while( !p_fifo->p_first )
236         {
237             vlc_cond_signal( &p_fifo->data_wait );
238             vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
239         }
240     }
241     vlc_mutex_unlock( &p_fifo->data_lock );
242 }
243
244 static inline void __GetFrame( videodec_thread_t *p_vdec )
245 {
246     pes_packet_t  *p_pes;
247     data_packet_t *p_data;
248     byte_t        *p_buffer;
249
250     p_pes = __PES_GET( p_vdec->p_fifo );
251     p_vdec->i_pts = p_pes->i_pts;
252
253     while( ( !p_pes->i_nb_data )||( !p_pes->i_pes_size ) )
254     {
255         __PES_NEXT( p_vdec->p_fifo );
256         p_pes = __PES_GET( p_vdec->p_fifo );
257     }
258     p_vdec->i_framesize = p_pes->i_pes_size;
259     if( p_pes->i_nb_data == 1 )
260     {
261         p_vdec->p_framedata = p_pes->p_first->p_payload_start;
262         return;    
263     }
264     /* get a buffer and gather all data packet */
265     p_vdec->p_framedata = p_buffer = malloc( p_pes->i_pes_size );
266     p_data = p_pes->p_first;
267     do
268     {
269         p_vdec->p_fifo->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, 
270                      p_data->p_payload_end - p_data->p_payload_start );
271         p_buffer += p_data->p_payload_end - p_data->p_payload_start;
272         p_data = p_data->p_next;
273     } while( p_data );
274 }
275
276 static inline void __NextFrame( videodec_thread_t *p_vdec )
277 {
278     pes_packet_t  *p_pes;
279
280     p_pes = __PES_GET( p_vdec->p_fifo );
281     if( p_pes->i_nb_data != 1 )
282     {
283         free( p_vdec->p_framedata ); /* FIXME keep this buffer */
284     }
285     __PES_NEXT( p_vdec->p_fifo );
286 }
287
288 /* FIXME FIXME some of them are wrong */
289 static int i_ffmpeg_PixFmtToChroma[] = 
290 {
291 /* PIX_FMT_ANY = -1,PIX_FMT_YUV420P, PIX_FMT_YUV422,
292    PIX_FMT_RGB24, PIX_FMT_BGR24, PIX_FMT_YUV422P, 
293    PIX_FMT_YUV444P, PIX_FMT_YUV410P */
294
295     0, VLC_FOURCC('I','4','2','0'), VLC_FOURCC('I','4','2','0'), 
296     VLC_FOURCC('R','V','2','4'), 0, VLC_FOURCC('Y','4','2','2'), 
297     VLC_FOURCC('I','4','4','4'), 0 
298 };
299
300 static inline u32 ffmpeg_PixFmtToChroma( int i_ffmpegchroma )
301 {
302     if( ++i_ffmpegchroma > 7 )
303     {
304         return( 0 );
305     }
306     else
307     {
308         return( i_ffmpeg_PixFmtToChroma[i_ffmpegchroma] );
309     }
310 }
311
312 static inline int ffmpeg_FfAspect( int i_width, int i_height, int i_ffaspect )
313 {
314     switch( i_ffaspect )
315     {
316         case( FF_ASPECT_4_3_625 ):
317         case( FF_ASPECT_4_3_525 ):
318             return( VOUT_ASPECT_FACTOR * 4 / 3);
319         case( FF_ASPECT_16_9_625 ):
320         case( FF_ASPECT_16_9_525 ):
321             return( VOUT_ASPECT_FACTOR * 16 / 9 );
322         case( FF_ASPECT_SQUARE ):
323         default:
324             return( VOUT_ASPECT_FACTOR * i_width / i_height );
325     }
326 }
327
328 static int ffmpeg_CheckVout( vout_thread_t *p_vout,
329                              int i_width,
330                              int i_height,
331                              int i_aspect,
332                              int i_chroma )
333 {
334     if( !p_vout )
335     {
336         return( 0 );
337     }
338     if( !i_chroma )
339     {
340         /* we will try to make conversion */
341         i_chroma = VLC_FOURCC('I','4','2','0');
342     } 
343     
344     if( ( p_vout->render.i_width != i_width )||
345         ( p_vout->render.i_height != i_height )||
346         ( p_vout->render.i_chroma != i_chroma )||
347         ( p_vout->render.i_aspect != 
348                 ffmpeg_FfAspect( i_width, i_height, i_aspect ) ) )
349     {
350         return( 0 );
351     }
352     else
353     {
354         return( 1 );
355     }
356 }
357
358 /* Return a Vout */
359
360 static vout_thread_t *ffmpeg_CreateVout( videodec_thread_t *p_vdec,
361                                          int i_width,
362                                          int i_height,
363                                          int i_aspect,
364                                          int i_chroma )
365 {
366     vout_thread_t *p_vout;
367
368     if( (!i_width)||(!i_height) )
369     {
370         return( NULL ); /* Can't create a new vout without display size */
371     }
372
373     if( !i_chroma )
374     {
375         /* we make conversion if possible*/
376         i_chroma = VLC_FOURCC('I','4','2','0');
377         msg_Warn( p_vdec->p_fifo, "Internal chroma conversion (FIXME)");
378         /* It's mainly for I410 -> I420 conversion that I've made,
379            it's buggy and very slow */
380     } 
381
382     i_aspect = ffmpeg_FfAspect( i_width, i_height, i_aspect );
383     
384     /* Spawn a video output if there is none. First we look for our children,
385      * then we look for any other vout that might be available. */
386     p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
387                                               FIND_CHILD );
388     if( !p_vout )
389     {
390         p_vout = vlc_object_find( p_vdec->p_fifo, VLC_OBJECT_VOUT,
391                                                   FIND_ANYWHERE );
392     }
393
394     if( p_vout )
395     {
396         if( !ffmpeg_CheckVout( p_vout, 
397                                i_width, i_height, i_aspect,i_chroma ) )
398         {
399             /* We are not interested in this format, close this vout */
400             vlc_object_detach_all( p_vout );
401             vlc_object_release( p_vout );
402             vout_DestroyThread( p_vout );
403             p_vout = NULL;
404         }
405         else
406         {
407             /* This video output is cool! Hijack it. */
408             vlc_object_detach_all( p_vout );
409             vlc_object_attach( p_vout, p_vdec->p_fifo );
410             vlc_object_release( p_vout );
411         }
412     }
413
414     if( p_vout == NULL )
415     {
416         msg_Dbg( p_vdec->p_fifo, "no vout present, spawning one" );
417     
418         p_vout = vout_CreateThread( p_vdec->p_fifo,
419                                     i_width,
420                                     i_height,
421                                     i_chroma,
422                                     i_aspect );
423     }
424
425     return( p_vout );
426 }
427
428 /* FIXME FIXME FIXME this is a big shit
429    does someone want to rewrite this function ? 
430    or said to me how write a better thing
431    FIXME FIXME FIXME
432 */
433
434 static void ffmpeg_ConvertPictureI410toI420( picture_t *p_pic,
435                                              AVPicture *p_avpicture,
436                                              videodec_thread_t   *p_vdec )
437 {
438     u8 *p_src, *p_dst;
439     u8 *p_plane[3];
440     int i_plane;
441     
442     int i_stride, i_lines;
443     int i_height, i_width;
444     int i_y, i_x;
445     
446     i_height = p_vdec->p_context->height;
447     i_width  = p_vdec->p_context->width;
448     
449     p_dst = p_pic->p[0].p_pixels;
450     p_src  = p_avpicture->data[0];
451
452     /* copy first plane */
453     for( i_y = 0; i_y < i_height; i_y++ )
454     {
455         p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width);
456         p_dst += p_pic->p[0].i_pitch;
457         p_src += p_avpicture->linesize[0];
458     }
459     
460     /* process each plane in a temporary buffer */
461     for( i_plane = 1; i_plane < 3; i_plane++ )
462     {
463         i_stride = p_avpicture->linesize[i_plane];
464         i_lines = i_height / 4;
465
466         p_dst = p_plane[i_plane] = malloc( i_lines * i_stride * 2 * 2 );
467         p_src  = p_avpicture->data[i_plane];
468
469         /* for each source line */
470         for( i_y = 0; i_y < i_lines; i_y++ )
471         {
472             for( i_x = 0; i_x < i_stride - 1; i_x++ )
473             {
474                 p_dst[2 * i_x    ] = p_src[i_x];
475                 p_dst[2 * i_x + 1] = ( p_src[i_x] + p_src[i_x + 1]) / 2;
476
477             }
478             p_dst[2 * i_stride - 2] = p_src[i_x];
479             p_dst[2 * i_stride - 1] = p_src[i_x];
480                            
481             p_dst += 4 * i_stride; /* process the next even lines */
482             p_src += i_stride;
483         }
484
485
486     }
487
488     for( i_plane = 1; i_plane < 3; i_plane++ )
489     {
490         i_stride = p_avpicture->linesize[i_plane];
491         i_lines = i_height / 4;
492
493         p_dst = p_plane[i_plane] + 2*i_stride;
494         p_src  = p_plane[i_plane];
495
496         for( i_y = 0; i_y < i_lines - 1; i_y++ )
497         {
498             for( i_x = 0; i_x <  2 * i_stride ; i_x++ )
499             {
500                 p_dst[i_x] = ( p_src[i_x] + p_src[i_x + 4*i_stride])/2;
501             }
502                            
503             p_dst += 4 * i_stride; /* process the next odd lines */
504             p_src += 4 * i_stride;
505         }
506         /* last line */
507         p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, 2*i_stride );
508     }
509     /* copy to p_pic, by block
510        if I do pixel per pixel it segfault. It's why I use 
511        temporaries buffers */
512     for( i_plane = 1; i_plane < 3; i_plane++ )
513     {
514
515         int i_size; 
516         p_src  = p_plane[i_plane];
517         p_dst = p_pic->p[i_plane].p_pixels;
518
519         i_size = __MIN( 2*i_stride, p_pic->p[i_plane].i_pitch);
520         for( i_y = 0; i_y < __MIN(p_pic->p[i_plane].i_lines, 2 * i_lines); i_y++ )
521         {
522             p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
523             p_src += 2 * i_stride;
524             p_dst += p_pic->p[i_plane].i_pitch;
525         }
526         free( p_plane[i_plane] );
527     }
528
529 }
530
531 static void ffmpeg_ConvertPicture( picture_t *p_pic,
532                                    AVPicture *p_avpicture,
533                                    videodec_thread_t   *p_vdec )
534 {
535     int i_plane; 
536     int i_size;
537     int i_line;
538
539     u8  *p_dst;
540     u8  *p_src;
541     int i_src_stride;
542     int i_dst_stride;
543     
544     if( ffmpeg_PixFmtToChroma( p_vdec->p_context->pix_fmt ) )
545     {
546         /* convert ffmpeg picture to our format */
547         for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
548         {
549             p_src  = p_avpicture->data[i_plane];
550             p_dst = p_pic->p[i_plane].p_pixels;
551             i_src_stride = p_avpicture->linesize[i_plane];
552             i_dst_stride = p_pic->p[i_plane].i_pitch;
553             
554             i_size = __MIN( i_src_stride, i_dst_stride );
555             for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
556             {
557                 p_vdec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_size );
558                 p_src += i_src_stride;
559                 p_dst += i_dst_stride;
560             }
561         }
562         return;
563     }
564
565     /* we need to convert to I420 */
566     switch( p_vdec->p_context->pix_fmt )
567     {
568 #if LIBAVCODEC_BUILD >= 4615
569         case( PIX_FMT_YUV410P ):
570             ffmpeg_ConvertPictureI410toI420( p_pic, p_avpicture, p_vdec );
571             break;
572 #endif            
573         default:
574             p_vdec->p_fifo->b_error =1;
575             break;
576     }
577 }
578
579
580 /*****************************************************************************
581  *
582  * Functions that initialize, decode and end the decoding process
583  *
584  *****************************************************************************/
585
586 /*****************************************************************************
587  * InitThread: initialize vdec output thread
588  *****************************************************************************
589  * This function is called from RunDecoderoder and performs the second step 
590  * of the initialization. It returns 0 on success. Note that the thread's 
591  * flag are not modified inside this function.
592  *****************************************************************************/
593
594 static int InitThread( videodec_thread_t *p_vdec )
595 {
596     int i_ffmpeg_codec; 
597     int i_tmp;
598     
599     if( p_vdec->p_fifo->p_demux_data )
600     {
601         ffmpeg_ParseBitMapInfoHeader( &p_vdec->format, 
602                                       (u8*)p_vdec->p_fifo->p_demux_data );
603     }
604     else
605     {
606         msg_Warn( p_vdec->p_fifo, "display informations missing" );
607     }
608
609     /*init ffmpeg */
610     if( !b_ffmpeginit )
611     {
612         avcodec_init();
613         avcodec_register_all();
614         b_ffmpeginit = 1;
615         msg_Dbg( p_vdec->p_fifo, "library ffmpeg initialized" );
616     }
617     else
618     {
619         msg_Dbg( p_vdec->p_fifo, "library ffmpeg already initialized" );
620     }
621     ffmpeg_GetFfmpegCodec( p_vdec->p_fifo->i_fourcc,
622                            &i_ffmpeg_codec,
623                            &p_vdec->psz_namecodec );
624     p_vdec->p_codec = 
625         avcodec_find_decoder( i_ffmpeg_codec );
626     
627     if( !p_vdec->p_codec )
628     {
629         msg_Err( p_vdec->p_fifo, "codec not found (%s)",
630                                  p_vdec->psz_namecodec );
631         return( -1 );
632     }
633
634     p_vdec->p_context = &p_vdec->context;
635     memset( p_vdec->p_context, 0, sizeof( AVCodecContext ) );
636
637     p_vdec->p_context->width  = p_vdec->format.i_width;
638     p_vdec->p_context->height = p_vdec->format.i_height;
639     
640 /*  XXX
641     p_vdec->p_context->workaround_bugs 
642       --> seems to be for msmpeg 3 but can't know what is supposed to do
643
644     p_vdec->p_context->strict_std_compliance
645       --> strictly follow mpeg4 standard for decoder or encoder ??
646       
647     p_vdec->p_context->error_resilience
648       --> don't make error resilience, because of some ms encoder witch 
649       use some wrong VLC code.
650 */
651
652 #if LIBAVCODEC_BUILD >= 4611
653     i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-workaround-bugs" );
654     p_vdec->p_context->workaround_bugs  = __MAX( __MIN( i_tmp, 99 ), 0 );
655
656     i_tmp = config_GetInt( p_vdec->p_fifo, "ffmpeg-error-resilience" );
657     p_vdec->p_context->error_resilience = __MAX( __MIN( i_tmp, 99 ), -1 );
658 #endif
659 #if LIBAVCODEC_BUILD >= 4614
660     if( config_GetInt( p_vdec->p_fifo, "grayscale" ) )
661     {
662         p_vdec->p_context->flags|= CODEC_FLAG_GRAY;
663     }
664 #endif
665     
666     if (avcodec_open(p_vdec->p_context, p_vdec->p_codec) < 0)
667     {
668         msg_Err( p_vdec->p_fifo, "cannot open codec (%s)",
669                                  p_vdec->psz_namecodec );
670         return( -1 );
671     }
672     else
673     {
674         msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) started",
675                                  p_vdec->psz_namecodec );
676     }
677     
678     /* first give init data */
679     if( p_vdec->format.i_data )
680     {
681         AVPicture avpicture;
682         int b_gotpicture;
683         
684         switch( i_ffmpeg_codec )
685         {
686             case( CODEC_ID_MPEG4 ):
687                 avcodec_decode_video( p_vdec->p_context, &avpicture, 
688                                       &b_gotpicture,
689                                       p_vdec->format.p_data,
690                                       p_vdec->format.i_data );
691                 break;
692             default:
693                 break;
694         }
695     }
696     
697     /* This will be created after the first decoded frame */
698     p_vdec->p_vout = NULL;
699     
700     return( 0 );
701 }
702
703 /*****************************************************************************
704  * DecodeThread: Called for decode one frame
705  *****************************************************************************/
706 static void  DecodeThread( videodec_thread_t *p_vdec )
707 {
708     int     i_status;
709     int     b_drawpicture;
710     int     b_gotpicture;
711     AVPicture avpicture;  /* ffmpeg picture */
712     picture_t *p_pic; /* videolan picture */
713     /* we have to get a frame stored in a pes 
714        give it to ffmpeg decoder 
715        and send the image to the output */ 
716
717     /* TODO implement it in a better way */
718
719     if( ( config_GetInt(p_vdec->p_fifo, "ffmpeg-hurry-up") )&&
720         ( p_vdec->i_frame_late > 4 ) )
721     {
722 #if LIBAVCODEC_BUILD > 4603
723         b_drawpicture = 0;
724         if( p_vdec->i_frame_late < 8 )
725         {
726             p_vdec->p_context->hurry_up = 2;
727         }
728         else
729         {
730             /* too much late picture, won't decode 
731                but break picture until a new I, and for mpeg4 ...*/
732             p_vdec->i_frame_late--; /* needed else it will never be decrease */
733             __PES_NEXT( p_vdec->p_fifo );
734             return;
735         }
736 #else
737         if( p_vdec->i_frame_late < 8 )
738         {
739             b_drawpicture = 0; /* not really good but .. */
740         }
741         else
742         {
743             /* too much late picture, won't decode 
744                but break picture until a new I, and for mpeg4 ...*/
745             p_vdec->i_frame_late--; /* needed else it will never be decrease */
746             __PES_NEXT( p_vdec->p_fifo );
747             return;
748         }
749 #endif
750     }
751     else
752     {
753         b_drawpicture = 1;
754 #if LIBAVCODEC_BUILD > 4603
755         p_vdec->p_context->hurry_up = 0;
756 #endif
757     }
758
759     __GetFrame( p_vdec );
760
761     i_status = avcodec_decode_video( p_vdec->p_context,
762                                      &avpicture,
763                                      &b_gotpicture,
764                                      p_vdec->p_framedata,
765                                      p_vdec->i_framesize);
766
767     __NextFrame( p_vdec );
768
769     if( i_status < 0 )
770     {
771         msg_Warn( p_vdec->p_fifo, "cannot decode one frame (%d bytes)",
772                                   p_vdec->i_framesize );
773         p_vdec->i_frame_error++;
774         return;
775     }
776     /* Update frame late count*/
777     /* I don't make statistic on decoding time */
778     if( p_vdec->i_pts <= mdate()) 
779     {
780         p_vdec->i_frame_late++;
781     }
782     else
783     {
784         p_vdec->i_frame_late = 0;
785     }
786
787     if( !b_gotpicture || avpicture.linesize[0] == 0 || !b_drawpicture)
788     {
789         return;
790     }
791     
792     /* Check our vout */
793     if( !ffmpeg_CheckVout( p_vdec->p_vout,
794                            p_vdec->p_context->width,
795                            p_vdec->p_context->height,
796                            p_vdec->p_context->aspect_ratio_info,
797                            ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt)) )
798     {
799         p_vdec->p_vout = 
800           ffmpeg_CreateVout( p_vdec,
801                              p_vdec->p_context->width,
802                              p_vdec->p_context->height,
803                              p_vdec->p_context->aspect_ratio_info,
804                              ffmpeg_PixFmtToChroma(p_vdec->p_context->pix_fmt));
805         if( !p_vdec->p_vout )
806         {
807             msg_Err( p_vdec->p_fifo, "cannot create vout" );
808             p_vdec->p_fifo->b_error = 1; /* abort */
809             return;
810         }
811     }
812
813     /* Send decoded frame to vout */
814     while( !(p_pic = vout_CreatePicture( p_vdec->p_vout, 0, 0, 0 ) ) )
815     {
816         if( p_vdec->p_fifo->b_die || p_vdec->p_fifo->b_error )
817         {
818             return;
819         }
820         msleep( VOUT_OUTMEM_SLEEP );
821     }
822     
823     ffmpeg_ConvertPicture( p_pic, 
824                            &avpicture, 
825                            p_vdec );
826     
827
828     /* FIXME correct avi and use i_dts */
829     vout_DatePicture( p_vdec->p_vout, p_pic, p_vdec->i_pts);
830     vout_DisplayPicture( p_vdec->p_vout, p_pic );
831     
832     return;
833 }
834
835
836 /*****************************************************************************
837  * EndThread: thread destruction
838  *****************************************************************************
839  * This function is called when the thread ends after a sucessful
840  * initialization.
841  *****************************************************************************/
842 static void EndThread( videodec_thread_t *p_vdec )
843 {
844     if( !p_vdec )
845     {
846         return;
847     }
848
849     if( p_vdec->p_context != NULL)
850     {
851         avcodec_close( p_vdec->p_context );
852         msg_Dbg( p_vdec->p_fifo, "ffmpeg codec (%s) stopped",
853                                  p_vdec->psz_namecodec );
854     }
855
856     if( p_vdec->p_vout != NULL )
857     {
858         /* We are about to die. Reattach video output to p_vlc. */
859         vlc_object_detach( p_vdec->p_vout, p_vdec->p_fifo );
860         vlc_object_attach( p_vdec->p_vout, p_vdec->p_fifo->p_vlc );
861     }
862
863     if( p_vdec->format.p_data != NULL)
864     {
865         free( p_vdec->format.p_data );
866     }
867     
868     free( p_vdec );
869 }