]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/avcodec.c
c4f9ff3470947bd84b41d86529b34d8873a7083a
[vlc] / modules / codec / avcodec / avcodec.c
1 /*****************************************************************************
2  * avcodec.c: video and audio decoder and encoder using libavcodec
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_avcodec.h>
36
37 /* ffmpeg header */
38 #define HAVE_MMX 1
39 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
40 #   include <libavcodec/avcodec.h>
41 #elif defined(HAVE_FFMPEG_AVCODEC_H)
42 #   include <ffmpeg/avcodec.h>
43 #else
44 #   include <avcodec.h>
45 #endif
46
47 #if LIBAVCODEC_BUILD < 5000
48 #   error You must have a libavcodec >= 5000 (get svn)
49 #endif
50
51 #include "avcodec.h"
52 #include "avutil.h"
53
54 /*****************************************************************************
55  * decoder_sys_t: decoder descriptor
56  *****************************************************************************/
57 struct decoder_sys_t
58 {
59     /* Common part between video and audio decoder */
60     FFMPEG_COMMON_MEMBERS
61 };
62
63 /****************************************************************************
64  * Local prototypes
65  ****************************************************************************/
66 static int OpenDecoder( vlc_object_t * );
67 static void CloseDecoder( vlc_object_t * );
68
69 static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
70 static const char *const nloopf_list_text[] =
71   { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
72
73 #ifdef ENABLE_SOUT
74 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
75 static const char *const enc_hq_list_text[] = {
76     N_("rd"), N_("bits"), N_("simple") };
77 #endif
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders" \
83         "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
84         "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
85
86 vlc_module_begin ()
87     set_shortname( "FFmpeg")
88     add_shortcut( "ffmpeg" )
89     set_category( CAT_INPUT )
90     set_subcategory( SUBCAT_INPUT_SCODEC )
91     /* decoder main module */
92 #if defined(MODULE_NAME_is_ffmpegaltivec) \
93      || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
94     set_description( N_("AltiVec FFmpeg audio/video decoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") )
95     /*add_requirement( ALTIVEC )*/
96     set_capability( "decoder", 71 )
97 #else
98     set_description( N_("FFmpeg audio/video decoder") )
99     set_help( MODULE_DESCRIPTION )
100     set_capability( "decoder", 70 )
101 #endif
102     set_section( N_("Decoding") , NULL )
103     set_callbacks( OpenDecoder, CloseDecoder )
104
105
106     add_bool( "ffmpeg-dr", 1, NULL, DR_TEXT, DR_TEXT, true )
107     add_integer ( "ffmpeg-error-resilience", 1, NULL, ERROR_TEXT,
108         ERROR_LONGTEXT, true )
109     add_integer ( "ffmpeg-workaround-bugs", 1, NULL, BUGS_TEXT, BUGS_LONGTEXT,
110         false )
111     add_bool( "ffmpeg-hurry-up", 1, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT,
112         false )
113     add_integer( "ffmpeg-skip-frame", 0, NULL, SKIP_FRAME_TEXT,
114         SKIP_FRAME_LONGTEXT, true )
115         change_integer_range( -1, 4 )
116     add_integer( "ffmpeg-skip-idct", 0, NULL, SKIP_IDCT_TEXT,
117         SKIP_IDCT_LONGTEXT, true )
118         change_integer_range( -1, 4 )
119     add_integer ( "ffmpeg-vismv", 0, NULL, VISMV_TEXT, VISMV_LONGTEXT,
120         true )
121     add_integer ( "ffmpeg-lowres", 0, NULL, LOWRES_TEXT, LOWRES_LONGTEXT,
122         true )
123         change_integer_range( 0, 2 )
124     add_integer ( "ffmpeg-skiploopfilter", 0, NULL, SKIPLOOPF_TEXT,
125                   SKIPLOOPF_LONGTEXT, true )
126         change_safe ()
127         change_integer_list( nloopf_list, nloopf_list_text, NULL )
128
129     add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT,
130                  true )
131
132 #ifdef ENABLE_SOUT
133     /* encoder submodule */
134     add_submodule ()
135     add_shortcut( "ffmpeg" )
136     set_section( N_("Encoding") , NULL )
137     set_description( N_("FFmpeg audio/video encoder") )
138     set_capability( "encoder", 100 )
139     set_callbacks( OpenEncoder, CloseEncoder )
140
141     add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT,
142                 ENC_HQ_LONGTEXT, false )
143         change_string_list( enc_hq_list, enc_hq_list_text, 0 )
144     add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT,
145                  ENC_KEYINT_LONGTEXT, false )
146     add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT,
147                  ENC_BFRAMES_LONGTEXT, false )
148     add_bool( ENC_CFG_PREFIX "hurry-up", 0, NULL, ENC_HURRYUP_TEXT,
149               ENC_HURRYUP_LONGTEXT, false )
150     add_bool( ENC_CFG_PREFIX "interlace", 0, NULL, ENC_INTERLACE_TEXT,
151               ENC_INTERLACE_LONGTEXT, true )
152     add_bool( ENC_CFG_PREFIX "interlace-me", 1, NULL, ENC_INTERLACE_ME_TEXT,
153               ENC_INTERLACE_ME_LONGTEXT, true )
154     add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT,
155                  ENC_VT_LONGTEXT, true )
156     add_bool( ENC_CFG_PREFIX "pre-me", 0, NULL, ENC_PRE_ME_TEXT,
157               ENC_PRE_ME_LONGTEXT, true )
158     add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL,
159                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
160     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL,
161                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
162     add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL,
163                ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
164     add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL,
165                  ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
166     add_bool( ENC_CFG_PREFIX "mpeg4-matrix", 0, NULL,
167               ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
168     add_integer( ENC_CFG_PREFIX "qmin", 0, NULL,
169                  ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
170     add_integer( ENC_CFG_PREFIX "qmax", 0, NULL,
171                  ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
172     add_bool( ENC_CFG_PREFIX "trellis", 0, NULL,
173               ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
174     add_float( ENC_CFG_PREFIX "qscale", 0, NULL,
175                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
176     add_integer( ENC_CFG_PREFIX "strict", 0, NULL,
177                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
178     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL,
179                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
180     add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL,
181                ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
182     add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL,
183                ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
184     add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL,
185                ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
186     add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL,
187                  ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
188     add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL,
189                  ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
190
191 #if LIBAVCODEC_VERSION_INT >= ((51<<16)+(40<<8)+4)
192     /* Audio AAC encoder profile */
193     add_string( ENC_CFG_PREFIX "aac-profile", "low", NULL,
194                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
195 #endif
196 #endif /* ENABLE_SOUT */
197
198     /* video filter submodule */
199     add_submodule ()
200     set_capability( "video filter2", 0 )
201     set_callbacks( OpenDeinterlace, CloseDeinterlace )
202     set_description( N_("FFmpeg deinterlace video filter") )
203     add_shortcut( "ffmpeg-deinterlace" )
204
205 vlc_module_end ()
206
207 /*****************************************************************************
208  * OpenDecoder: probe the decoder and return score
209  *****************************************************************************/
210 static int OpenDecoder( vlc_object_t *p_this )
211 {
212     decoder_t *p_dec = (decoder_t*) p_this;
213     int i_cat, i_codec_id, i_result;
214     const char *psz_namecodec;
215
216     AVCodecContext *p_context = NULL;
217     AVCodec        *p_codec = NULL;
218
219     /* *** determine codec type *** */
220     if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
221                              &psz_namecodec ) )
222     {
223         return VLC_EGENERIC;
224     }
225
226     /* Initialization must be done before avcodec_find_decoder() */
227     InitLibavcodec(p_this);
228
229     /* *** ask ffmpeg for a decoder *** */
230     p_codec = avcodec_find_decoder( i_codec_id );
231     if( !p_codec )
232     {
233         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
234         return VLC_EGENERIC;
235     }
236
237     /* *** get a p_context *** */
238     p_context = avcodec_alloc_context();
239     if( !p_context )
240         return VLC_ENOMEM;
241     p_context->debug = config_GetInt( p_dec, "ffmpeg-debug" );
242     p_context->opaque = (void *)p_this;
243
244     /* Set CPU capabilities */
245     unsigned i_cpu = vlc_CPU();
246     p_context->dsp_mask = 0;
247     if( !(i_cpu & CPU_CAPABILITY_MMX) )
248     {
249         p_context->dsp_mask |= FF_MM_MMX;
250     }
251     if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
252     {
253         p_context->dsp_mask |= FF_MM_MMXEXT;
254     }
255     if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
256     {
257         p_context->dsp_mask |= FF_MM_3DNOW;
258     }
259     if( !(i_cpu & CPU_CAPABILITY_SSE) )
260     {
261         p_context->dsp_mask |= FF_MM_SSE;
262     }
263     if( !(i_cpu & CPU_CAPABILITY_SSE2) )
264     {
265         p_context->dsp_mask |= FF_MM_SSE2;
266     }
267
268     p_dec->b_need_packetized = true;
269     switch( i_cat )
270     {
271     case VIDEO_ES:
272         p_dec->pf_decode_video = DecodeVideo;
273         i_result =  InitVideoDec ( p_dec, p_context, p_codec,
274                                        i_codec_id, psz_namecodec );
275         break;
276     case AUDIO_ES:
277         p_dec->pf_decode_audio = DecodeAudio;
278         i_result =  InitAudioDec ( p_dec, p_context, p_codec,
279                                        i_codec_id, psz_namecodec );
280         break;
281     default:
282         i_result = VLC_EGENERIC;
283     }
284
285     if( i_result == VLC_SUCCESS ) p_dec->p_sys->i_cat = i_cat;
286
287     return i_result;
288 }
289
290 /*****************************************************************************
291  * CloseDecoder: decoder destruction
292  *****************************************************************************/
293 static void CloseDecoder( vlc_object_t *p_this )
294 {
295     decoder_t *p_dec = (decoder_t *)p_this;
296     decoder_sys_t *p_sys = p_dec->p_sys;
297
298     switch( p_sys->i_cat )
299     {
300     case AUDIO_ES:
301          EndAudioDec ( p_dec );
302         break;
303     case VIDEO_ES:
304          EndVideoDec ( p_dec );
305         break;
306     }
307
308     if( p_sys->p_context )
309     {
310         free( p_sys->p_context->extradata );
311         p_sys->p_context->extradata = NULL;
312
313         if( !p_sys->b_delayed_open )
314         {
315             vlc_avcodec_lock();
316             avcodec_close( p_sys->p_context );
317             vlc_avcodec_unlock();
318         }
319         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
320         av_free( p_sys->p_context );
321     }
322
323     free( p_sys );
324 }
325
326 void InitLibavcodec( vlc_object_t *p_object )
327 {
328     static bool b_ffmpeginit = false;
329
330     vlc_avcodec_lock();
331
332     /* *** init ffmpeg library (libavcodec) *** */
333     if( !b_ffmpeginit )
334     {
335         avcodec_init();
336         avcodec_register_all();
337         av_log_set_callback( LibavutilCallback );
338         b_ffmpeginit = true;
339
340         msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
341                  LIBAVCODEC_VERSION_INT );
342     }
343     else
344     {
345         msg_Dbg( p_object, "libavcodec already initialized" );
346     }
347
348     vlc_avcodec_unlock();
349 }