]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/ffmpeg.c
* modules/codec/ffmpeg/: proper generation of pts in the video decoder + couple of...
[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.47 2003/08/08 17:08:32 gbazin 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/aout.h>
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
34
35 #include <string.h>
36
37 #ifdef HAVE_SYS_TIMES_H
38 #   include <sys/times.h>
39 #endif
40
41 /* ffmpeg header */
42 #ifdef HAVE_FFMPEG_AVCODEC_H
43 #   include <ffmpeg/avcodec.h>
44 #else
45 #   include <avcodec.h>
46 #endif
47
48 #if LIBAVCODEC_BUILD < 4655
49 #   error You must have a libavcodec >= 4655 (get CVS)
50 #endif
51
52
53 #include "ffmpeg.h"
54
55 #ifdef LIBAVCODEC_PP
56 #   ifdef HAVE_POSTPROC_POSTPROCESS_H
57 #       include <postproc/postprocess.h>
58 #   else
59 #       include <libpostproc/postprocess.h>
60 #   endif
61 #endif
62
63 #include "video.h" // video ffmpeg specific
64 #include "audio.h" // audio ffmpeg specific
65
66 /*
67  * Local prototypes
68  */
69 int             E_(OpenChroma)  ( vlc_object_t * );
70 static int      OpenDecoder     ( vlc_object_t * );
71 static int      RunDecoder      ( decoder_fifo_t * );
72
73 static int      InitThread      ( generic_thread_t * );
74 static void     EndThread       ( generic_thread_t * );
75
76
77 static int      b_ffmpeginit = 0;
78
79 static int ffmpeg_GetFfmpegCodec( vlc_fourcc_t, int *, int *, char ** );
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define DR_TEXT N_("Direct rendering")
85
86 #define ERROR_TEXT N_("Error resilience")
87 #define ERROR_LONGTEXT N_( \
88     "ffmpeg can make errors resiliences.          \n" \
89     "Nevertheless, with a buggy encoder (like ISO MPEG-4 encoder from M$) " \
90     "this will produce a lot of errors.\n" \
91     "Valid range is -1 to 99 (-1 disables all errors resiliences).")
92
93 #define BUGS_TEXT N_("Workaround bugs")
94 #define BUGS_LONGTEXT N_( \
95     "Try to fix some bugs\n" \
96     "1  autodetect\n" \
97     "2  old msmpeg4\n" \
98     "4  xvid interlaced\n" \
99     "8  ump4 \n" \
100     "16 no padding\n" \
101     "32 ac vlc\n" \
102     "64 Qpel chroma")
103
104 #define HURRYUP_TEXT N_("Hurry up")
105 #define HURRYUP_LONGTEXT N_( \
106     "Allow the decoder to partially decode or skip frame(s) " \
107     "when there is not enough time. It's useful with low CPU power " \
108     "but it can produce distorted pictures.")
109
110 #define TRUNC_TEXT N_("Truncated stream")
111 #define TRUNC_LONGTEXT N_("truncated stream -1:auto,0:disable,:1:enable")
112
113 #define PP_Q_TEXT N_("Post processing quality")
114 #define PP_Q_LONGTEXT N_( \
115     "Quality of post processing. Valid range is 0 to 6\n" \
116     "Higher levels require considerable more CPU power, but produce " \
117     "better looking pictures." )
118
119 #define LIBAVCODEC_PP_TEXT N_("Ffmpeg postproc filter chains")
120 /* FIXME (cut/past from ffmpeg */
121 #define LIBAVCODEC_PP_LONGTEXT \
122 "<filterName>[:<option>[:<option>...]][[,|/][-]<filterName>[:<option>...]]...\n" \
123 "long form example:\n" \
124 "vdeblock:autoq/hdeblock:autoq/linblenddeint    default,-vdeblock\n" \
125 "short form example:\n" \
126 "vb:a/hb:a/lb de,-vb\n" \
127 "more examples:\n" \
128 "tn:64:128:256\n" \
129 "Filters                        Options\n" \
130 "short  long name       short   long option     Description\n" \
131 "*      *               a       autoq           cpu power dependant enabler\n" \
132 "                       c       chrom           chrominance filtring enabled\n" \
133 "                       y       nochrom         chrominance filtring disabled\n" \
134 "hb     hdeblock        (2 Threshold)           horizontal deblocking filter\n" \
135 "       1. difference factor: default=64, higher -> more deblocking\n" \
136 "       2. flatness threshold: default=40, lower -> more deblocking\n" \
137 "                       the h & v deblocking filters share these\n" \
138 "                       so u cant set different thresholds for h / v\n" \
139 "vb     vdeblock        (2 Threshold)           vertical deblocking filter\n" \
140 "h1     x1hdeblock                              Experimental h deblock filter 1\n" \
141 "v1     x1vdeblock                              Experimental v deblock filter 1\n" \
142 "dr     dering                                  Deringing filter\n" \
143 "al     autolevels                              automatic brightness / contrast\n" \
144 "                       f       fullyrange      stretch luminance to (0..255)\n" \
145 "lb     linblenddeint                           linear blend deinterlacer\n" \
146 "li     linipoldeint                            linear interpolating deinterlace\n" \
147 "ci     cubicipoldeint                          cubic interpolating deinterlacer\n" \
148 "md     mediandeint                             median deinterlacer\n" \
149 "fd     ffmpegdeint                             ffmpeg deinterlacer\n" \
150 "de     default                                 hb:a,vb:a,dr:a,al\n" \
151 "fa     fast                                    h1:a,v1:a,dr:a,al\n" \
152 "tn     tmpnoise        (3 Thresholds)          Temporal Noise Reducer\n" \
153 "                       1. <= 2. <= 3.          larger -> stronger filtering\n" \
154 "fq     forceQuant      <quantizer>             Force quantizer\n"
155
156 vlc_module_begin();
157     add_category_hint( N_("ffmpeg"), NULL, VLC_FALSE );
158     set_capability( "decoder", 70 );
159     set_callbacks( OpenDecoder, NULL );
160     set_description( _("ffmpeg audio/video decoder((MS)MPEG4,SVQ1,H263,WMV,WMA)") );
161
162     add_bool( "ffmpeg-dr", 1, NULL, DR_TEXT, DR_TEXT, VLC_TRUE );
163     add_integer ( "ffmpeg-error-resilience", -1, NULL, ERROR_TEXT, ERROR_LONGTEXT, VLC_TRUE );
164     add_integer ( "ffmpeg-workaround-bugs", 1, NULL, BUGS_TEXT, BUGS_LONGTEXT, VLC_FALSE );
165     add_bool( "ffmpeg-hurry-up", 0, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT, VLC_FALSE );
166     add_integer( "ffmpeg-truncated", -1, NULL, TRUNC_TEXT, TRUNC_LONGTEXT, VLC_FALSE );
167
168     add_category_hint( N_("Post processing"), NULL, VLC_FALSE );
169
170     add_integer( "ffmpeg-pp-q", 0, NULL, PP_Q_TEXT, PP_Q_LONGTEXT, VLC_FALSE );
171 #ifdef LIBAVCODEC_PP
172     add_string( "ffmpeg-pp-name", "default", NULL, LIBAVCODEC_PP_TEXT, LIBAVCODEC_PP_LONGTEXT, VLC_TRUE );
173 #endif
174
175     /* chroma conversion submodule */
176     add_submodule();
177     set_capability( "chroma", 50 );
178     set_callbacks( E_(OpenChroma), NULL );
179     set_description( _("ffmpeg chroma conversion") );
180
181     var_Create( p_module->p_libvlc, "avcodec", VLC_VAR_MUTEX );
182 vlc_module_end();
183
184 /*****************************************************************************
185  * OpenDecoder: probe the decoder and return score
186  *****************************************************************************
187  * Tries to launch a decoder and return score so that the interface is able
188  * to chose.
189  *****************************************************************************/
190 static int OpenDecoder( vlc_object_t *p_this )
191 {
192     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
193
194     if( ffmpeg_GetFfmpegCodec( p_fifo->i_fourcc, NULL, NULL, NULL ) )
195     {
196         p_fifo->pf_run = RunDecoder;
197         return VLC_SUCCESS;
198     }
199
200     return VLC_EGENERIC;
201 }
202
203 typedef union decoder_thread_u
204 {
205     generic_thread_t gen;
206     adec_thread_t    audio;
207     vdec_thread_t    video;
208
209 } decoder_thread_t;
210
211
212 /*****************************************************************************
213  * RunDecoder: this function is called just after the thread is created
214  *****************************************************************************/
215 static int RunDecoder( decoder_fifo_t *p_fifo )
216 {
217     generic_thread_t *p_decoder;
218     int b_error;
219
220     if ( !(p_decoder = malloc( sizeof( decoder_thread_t ) ) ) )
221     {
222         msg_Err( p_fifo, "out of memory" );
223         DecoderError( p_fifo );
224         return( -1 );
225     }
226     memset( p_decoder, 0, sizeof( decoder_thread_t ) );
227
228     p_decoder->p_fifo = p_fifo;
229
230     if( InitThread( p_decoder ) != 0 )
231     {
232         msg_Err( p_fifo, "initialization failed" );
233         DecoderError( p_fifo );
234         return( -1 );
235     }
236
237     while( (!p_decoder->p_fifo->b_die) && (!p_decoder->p_fifo->b_error) )
238     {
239         switch( p_decoder->i_cat )
240         {
241             case VIDEO_ES:
242                 E_( DecodeThread_Video )( (vdec_thread_t*)p_decoder );
243                 break;
244             case AUDIO_ES:
245                 E_( DecodeThread_Audio )( (adec_thread_t*)p_decoder );
246                 break;
247         }
248     }
249
250     if( ( b_error = p_decoder->p_fifo->b_error ) )
251     {
252         DecoderError( p_decoder->p_fifo );
253     }
254
255     EndThread( p_decoder );
256
257     if( b_error )
258     {
259         return( -1 );
260     }
261
262     return( 0 );
263 }
264
265 /*****************************************************************************
266  *
267  * Functions that initialize, decode and end the decoding process
268  *
269  *****************************************************************************/
270
271 /*****************************************************************************
272  * InitThread: initialize vdec output thread
273  *****************************************************************************
274  * This function is called from decoder_Run and performs the second step
275  * of the initialization. It returns 0 on success. Note that the thread's
276  * flag are not modified inside this function.
277  *
278  * ffmpeg codec will be open, some memory allocated. But Vout is not yet
279  *   open (done after the first decoded frame)
280  *****************************************************************************/
281
282 static int InitThread( generic_thread_t *p_decoder )
283 {
284     int i_result;
285     vlc_value_t lockval;
286
287
288     var_Get( p_decoder->p_fifo->p_libvlc, "avcodec", &lockval );
289     vlc_mutex_lock( lockval.p_address );
290
291      /* *** init ffmpeg library (libavcodec) *** */
292     if( !b_ffmpeginit )
293     {
294
295         avcodec_init();
296         avcodec_register_all();
297         b_ffmpeginit = 1;
298
299         msg_Dbg( p_decoder->p_fifo, "libavcodec initialized (interface "
300                                     LIBAVCODEC_BUILD_STR ")" );
301     }
302     else
303     {
304         msg_Dbg( p_decoder->p_fifo, "libavcodec already initialized" );
305     }
306     vlc_mutex_unlock( lockval.p_address );
307
308     /* *** determine codec type *** */
309     ffmpeg_GetFfmpegCodec( p_decoder->p_fifo->i_fourcc,
310                            &p_decoder->i_cat,
311                            &p_decoder->i_codec_id,
312                            &p_decoder->psz_namecodec );
313
314     /* *** ask ffmpeg for a decoder *** */
315     if( !( p_decoder->p_codec =
316                 avcodec_find_decoder( p_decoder->i_codec_id ) ) )
317     {
318         msg_Err( p_decoder->p_fifo,
319                  "codec not found (%s)",
320                  p_decoder->psz_namecodec );
321         return( -1 );
322     }
323
324      /* *** Get a p_context *** */
325     p_decoder->p_context = avcodec_alloc_context();
326
327     switch( p_decoder->i_cat )
328     {
329         case VIDEO_ES:
330             i_result = E_( InitThread_Video )( (vdec_thread_t*)p_decoder );
331             break;
332         case AUDIO_ES:
333             i_result = E_( InitThread_Audio )( (adec_thread_t*)p_decoder );
334             break;
335         default:
336             i_result = -1;
337     }
338
339     p_decoder->pts = 0;
340     p_decoder->p_buffer = NULL;
341     p_decoder->i_buffer = 0;
342     p_decoder->i_buffer_size = 0;
343
344     return( i_result );
345 }
346
347 /*****************************************************************************
348  * EndThread: thread destruction
349  *****************************************************************************
350  * This function is called when the thread ends after a sucessful
351  * initialization.
352  *****************************************************************************/
353 static void EndThread( generic_thread_t *p_decoder )
354 {
355
356     if( !p_decoder )
357     {
358         return;
359     }
360
361     if( p_decoder->p_context != NULL)
362     {
363         FREE( p_decoder->p_context->extradata );
364         avcodec_close( p_decoder->p_context );
365         msg_Dbg( p_decoder->p_fifo,
366                  "ffmpeg codec (%s) stopped",
367                  p_decoder->psz_namecodec );
368         free( p_decoder->p_context );
369     }
370
371     FREE( p_decoder->p_buffer );
372
373     switch( p_decoder->i_cat )
374     {
375         case AUDIO_ES:
376             E_( EndThread_Audio )( (adec_thread_t*)p_decoder );
377             break;
378         case VIDEO_ES:
379             E_( EndThread_Video )( (vdec_thread_t*)p_decoder );
380             break;
381     }
382
383     free( p_decoder );
384 }
385
386 /*****************************************************************************
387  * locales Functions
388  *****************************************************************************/
389
390 int E_( GetPESData )( u8 *p_buf, int i_max, pes_packet_t *p_pes )
391 {
392     int i_copy;
393     int i_count;
394
395     data_packet_t   *p_data;
396
397     i_count = 0;
398     p_data = p_pes->p_first;
399     while( p_data != NULL && i_count < i_max )
400     {
401
402         i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start,
403                         i_max - i_count );
404
405         if( i_copy > 0 )
406         {
407             memcpy( p_buf,
408                     p_data->p_payload_start,
409                     i_copy );
410         }
411
412         p_data = p_data->p_next;
413         i_count += i_copy;
414         p_buf   += i_copy;
415     }
416
417     if( i_count < i_max )
418     {
419         memset( p_buf, 0, i_max - i_count );
420     }
421     return( i_count );
422 }
423
424
425 static int ffmpeg_GetFfmpegCodec( vlc_fourcc_t i_fourcc,
426                                   int *pi_cat,
427                                   int *pi_ffmpeg_codec,
428                                   char **ppsz_name )
429 {
430     int i_cat;
431     int i_codec;
432     char *psz_name;
433
434     switch( i_fourcc )
435     {
436         case FOURCC_mpgv:
437             i_cat = VIDEO_ES;
438             i_codec = CODEC_ID_MPEG1VIDEO;
439             psz_name = "MPEG-1/2 Video";
440             break;
441
442         case FOURCC_DIV1:
443         case FOURCC_div1:
444         case FOURCC_MPG4:
445         case FOURCC_mpg4:
446             i_cat = VIDEO_ES;
447             i_codec = CODEC_ID_MSMPEG4V1;
448             psz_name = "MS MPEG-4 v1";
449             break;
450
451         case FOURCC_DIV2:
452         case FOURCC_div2:
453         case FOURCC_MP42:
454         case FOURCC_mp42:
455             i_cat = VIDEO_ES;
456             i_codec = CODEC_ID_MSMPEG4V2;
457             psz_name = "MS MPEG-4 v2";
458             break;
459
460         case FOURCC_MPG3:
461         case FOURCC_mpg3:
462         case FOURCC_div3:
463         case FOURCC_MP43:
464         case FOURCC_mp43:
465         case FOURCC_DIV3:
466         case FOURCC_DIV4:
467         case FOURCC_div4:
468         case FOURCC_DIV5:
469         case FOURCC_div5:
470         case FOURCC_DIV6:
471         case FOURCC_div6:
472         case FOURCC_AP41:
473         case FOURCC_3VID:
474         case FOURCC_3vid:
475         case FOURCC_3IVD:
476         case FOURCC_3ivd:
477             i_cat = VIDEO_ES;
478             i_codec = CODEC_ID_MSMPEG4V3;
479             psz_name = "MS MPEG-4 v3";
480             break;
481
482         case FOURCC_SVQ1:
483             i_cat = VIDEO_ES;
484             i_codec = CODEC_ID_SVQ1;
485             psz_name = "SVQ-1 (Sorenson Video v1)";
486             break;
487 #if LIBAVCODEC_BUILD >= 4666
488         case FOURCC_SVQ3:
489             i_cat = VIDEO_ES;
490             i_codec = CODEC_ID_SVQ3;
491             psz_name = "SVQ-3 (Sorenson Video v3)";
492             break;
493 #endif
494
495         case FOURCC_DIVX:
496         case FOURCC_divx:
497         case FOURCC_MP4S:
498         case FOURCC_mp4s:
499         case FOURCC_M4S2:
500         case FOURCC_m4s2:
501         case FOURCC_xvid:
502         case FOURCC_XVID:
503         case FOURCC_XviD:
504         case FOURCC_DX50:
505         case FOURCC_mp4v:
506         case FOURCC_4:
507         /* 3iv1 is unsupported by ffmpeg
508            putting it here gives extreme distorted images
509         case FOURCC_3IV1:
510         case FOURCC_3iv1:
511         */
512         case FOURCC_3IV2:
513         case FOURCC_3iv2:
514             i_cat = VIDEO_ES;
515             i_codec = CODEC_ID_MPEG4;
516             psz_name = "MPEG-4";
517             break;
518 /* FIXME FOURCC_H263P exist but what fourcc ? */
519         case FOURCC_H263:
520         case FOURCC_h263:
521         case FOURCC_U263:
522             i_cat = VIDEO_ES;
523             i_codec = CODEC_ID_H263;
524             psz_name = "H263";
525             break;
526
527         case FOURCC_I263:
528         case FOURCC_i263:
529             i_cat = VIDEO_ES;
530             i_codec = CODEC_ID_H263I;
531             psz_name = "I263.I";
532             break;
533         case FOURCC_WMV1:
534             i_cat = VIDEO_ES;
535             i_codec = CODEC_ID_WMV1;
536             psz_name ="Windows Media Video 1";
537             break;
538         case FOURCC_WMV2:
539         case FOURCC_MSS1:
540             i_cat = VIDEO_ES;
541             i_codec = CODEC_ID_WMV2;
542             psz_name ="Windows Media Video 2";
543             break;
544         case FOURCC_MJPG:
545         case FOURCC_mjpg:
546         case FOURCC_mjpa:
547         case FOURCC_jpeg:
548         case FOURCC_JPEG:
549         case FOURCC_JFIF:
550             i_cat = VIDEO_ES;
551             i_codec = CODEC_ID_MJPEG;
552             psz_name = "Motion JPEG";
553             break;
554         case FOURCC_mjpb:
555             i_cat = VIDEO_ES;
556             i_codec = CODEC_ID_MJPEGB;
557             psz_name = "Motion JPEG B";
558             break;
559         case FOURCC_dvsl:
560         case FOURCC_dvsd:
561         case FOURCC_DVSD:
562         case FOURCC_dvhd:
563         case FOURCC_dvc:
564         case FOURCC_dvp:
565         case FOURCC_CDVC:
566             i_cat = VIDEO_ES;
567             i_codec = CODEC_ID_DVVIDEO;
568             psz_name = "DV video";
569             break;
570
571         case FOURCC_MAC3:
572             i_cat = AUDIO_ES;
573             i_codec = CODEC_ID_MACE3;
574             psz_name = "MACE-3 audio";
575             break;
576         case FOURCC_MAC6:
577             i_cat = AUDIO_ES;
578             i_codec = CODEC_ID_MACE6;
579             psz_name = "MACE-6 audio";
580             break;
581         case FOURCC_dvau:
582             i_cat = AUDIO_ES;
583             i_codec = CODEC_ID_DVAUDIO;
584             psz_name = "DV audio";
585             break;
586
587         case FOURCC_WMA1:
588         case FOURCC_wma1:
589             i_cat = AUDIO_ES;
590             i_codec = CODEC_ID_WMAV1;
591             psz_name ="Windows Media Audio 1";
592             break;
593         case FOURCC_WMA2:
594         case FOURCC_wma2:
595             i_cat = AUDIO_ES;
596             i_codec = CODEC_ID_WMAV2;
597             psz_name ="Windows Media Audio 2";
598             break;
599
600 #if( ( LIBAVCODEC_BUILD >= 4663 ) && ( !defined( WORDS_BIGENDIAN ) ) )
601         /* Quality of this decoder on ppc is not good */
602         case FOURCC_IV31:
603         case FOURCC_iv31:
604         case FOURCC_IV32:
605         case FOURCC_iv32:
606             i_cat    = VIDEO_ES;
607             i_codec  = CODEC_ID_INDEO3;
608             psz_name = "Indeo v3";
609             break;
610 #endif
611
612 #if LIBAVCODEC_BUILD >= 4668
613         /* Not yet finished 
614         case FOURCC_vp31:
615         case FOURCC_VP31:
616             i_cat    = VIDEO_ES;
617             i_codec  = CODEC_ID_VP3;
618             psz_name = "On2's VP3 Video";
619             break;
620
621         case FOURCC_asv1:
622         case FOURCC_ASV1:
623             i_cat    = VIDEO_ES;
624             i_codec  = CODEC_ID_ASV1;
625             psz_name = "Asus V1";
626             break; */
627 #endif
628
629         default:
630             i_cat = UNKNOWN_ES;
631             i_codec = CODEC_ID_NONE;
632             psz_name = NULL;
633             break;
634     }
635
636     if( i_codec != CODEC_ID_NONE )
637     {
638         if( pi_cat ) *pi_cat = i_cat;
639         if( pi_ffmpeg_codec ) *pi_ffmpeg_codec = i_codec;
640         if( ppsz_name ) *ppsz_name = psz_name;
641         return( VLC_TRUE );
642     }
643
644     return( VLC_FALSE );
645 }