]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/avcodec.c
40489df8aeb29ca481aca32c277ff9b6ccd5504c
[vlc] / modules / codec / avcodec / avcodec.c
1 /*****************************************************************************
2  * avcodec.c: video and audio decoder and encoder using libavcodec
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 #include <vlc_cpu.h>
37
38 #define HAVE_MMX 1
39 #include <libavcodec/avcodec.h>
40
41 #include "avcodec.h"
42 #include "chroma.h"
43 #include "avcommon.h"
44
45 /*****************************************************************************
46  * decoder_sys_t: decoder descriptor
47  *****************************************************************************/
48 struct decoder_sys_t
49 {
50     /* Common part between video and audio decoder */
51     AVCODEC_COMMON_MEMBERS
52 };
53
54 /****************************************************************************
55  * Local prototypes
56  ****************************************************************************/
57 static int OpenDecoder( vlc_object_t * );
58 static void CloseDecoder( vlc_object_t * );
59
60 static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
61 static const char *const nloopf_list_text[] =
62   { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
63
64 #ifdef ENABLE_SOUT
65 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
66 static const char *const enc_hq_list_text[] = {
67     N_("rd"), N_("bits"), N_("simple") };
68 #endif
69
70 #ifdef MERGE_FFMPEG
71 # include "../../demux/avformat/avformat.h"
72 # include "../../access/avio.h"
73 #endif
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
79         "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
80         "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
81
82 vlc_module_begin ()
83     set_shortname( "FFmpeg")
84     add_shortcut( "ffmpeg" )
85     set_category( CAT_INPUT )
86     set_subcategory( SUBCAT_INPUT_VCODEC )
87     /* decoder main module */
88     set_description( N_("FFmpeg audio/video decoder") )
89     set_help( MODULE_DESCRIPTION )
90     set_capability( "decoder", 70 )
91     set_section( N_("Decoding") , NULL )
92     set_callbacks( OpenDecoder, CloseDecoder )
93
94
95     add_obsolete_bool( "ffmpeg-dr" ) /* removed since 2.1.0 */
96     add_bool( "avcodec-dr", true, DR_TEXT, DR_TEXT, true )
97     add_obsolete_integer ( "ffmpeg-error-resilience" ) /* removed since 2.1.0 */
98     add_integer ( "avcodec-error-resilience", 1, ERROR_TEXT,
99         ERROR_LONGTEXT, true )
100     add_obsolete_integer ( "ffmpeg-workaround-bugs" ) /* removed since 2.1.0 */
101     add_integer ( "avcodec-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
102         false )
103     add_obsolete_bool( "ffmpeg-hurry-up" ) /* removed since 2.1.0 */
104     add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
105         false )
106     add_obsolete_integer( "ffmpeg-skip-frame") /* removed since 2.1.0 */
107     add_integer( "avcodec-skip-frame", 0, SKIP_FRAME_TEXT,
108         SKIP_FRAME_LONGTEXT, true )
109         change_integer_range( -1, 4 )
110     add_obsolete_integer( "ffmpeg-skip-idct" ) /* removed since 2.1.0 */
111     add_integer( "avcodec-skip-idct", 0, SKIP_IDCT_TEXT,
112         SKIP_IDCT_LONGTEXT, true )
113         change_integer_range( -1, 4 )
114     add_obsolete_integer( "ffmpeg-vismv" ) /* removed since 2.1.0 */
115     add_obsolete_integer( "avcodec-vismv" ) /* removed since 3.0.0 */
116     add_obsolete_integer ( "ffmpeg-lowres" ) /* removed since 2.1.0 */
117     add_obsolete_bool( "ffmpeg-fast" ) /* removed since 2.1.0 */
118     add_bool( "avcodec-fast", false, FAST_TEXT, FAST_LONGTEXT, false )
119     add_obsolete_integer ( "ffmpeg-skiploopfilter" ) /* removed since 2.1.0 */
120     add_integer ( "avcodec-skiploopfilter", 0, SKIPLOOPF_TEXT,
121                   SKIPLOOPF_LONGTEXT, false)
122         change_safe ()
123         change_integer_list( nloopf_list, nloopf_list_text )
124
125     add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
126     add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
127                  true )
128     add_obsolete_string( "ffmpeg-codec" ) /* removed since 2.1.0 */
129     add_string( "avcodec-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
130     add_obsolete_bool( "ffmpeg-hw" ) /* removed since 2.1.0 */
131     add_module( "avcodec-hw", "hw decoder", "any", HW_TEXT, HW_LONGTEXT, false )
132 #if defined(FF_THREAD_FRAME)
133     add_obsolete_integer( "ffmpeg-threads" ) /* removed since 2.1.0 */
134     add_integer( "avcodec-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
135 #endif
136     add_string( "avcodec-options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
137
138
139 #ifdef ENABLE_SOUT
140     /* encoder submodule */
141     add_submodule ()
142     add_shortcut( "ffmpeg" )
143     set_section( N_("Encoding") , NULL )
144     set_description( N_("FFmpeg audio/video encoder") )
145     set_capability( "encoder", 100 )
146     set_callbacks( OpenEncoder, CloseEncoder )
147
148     /* removed in 2.1.0 */
149     add_obsolete_string( "sout-ffmpeg-codec" )
150     add_obsolete_string( "sout-ffmpeg-hq" )
151     add_obsolete_integer( "sout-ffmpeg-keyint" )
152     add_obsolete_integer( "sout-ffmpeg-bframes" )
153     add_obsolete_bool( "sout-ffmpeg-hurry-up" )
154     add_obsolete_bool( "sout-ffmpeg-interlace" )
155     add_obsolete_bool( "sout-ffmpeg-interlace-me" )
156     add_obsolete_integer( "sout-ffmpeg-vt" )
157     add_obsolete_bool( "sout-ffmpeg-pre-me" )
158     add_obsolete_integer( "sout-ffmpeg-rc-buffer-size" )
159     add_obsolete_float( "sout-ffmpeg-rc-buffer-aggressivity" )
160     add_obsolete_float( "sout-ffmpeg-i-quant-factor" )
161     add_obsolete_integer( "sout-ffmpeg-noise-reduction" )
162     add_obsolete_bool( "sout-ffmpeg-mpeg4-matrix" )
163     add_obsolete_integer( "sout-ffmpeg-qmin" )
164     add_obsolete_integer( "sout-ffmpeg-qmax" )
165     add_obsolete_bool( "sout-ffmpeg-trellis" )
166     add_obsolete_float( "sout-ffmpeg-qscale" )
167     add_obsolete_integer( "sout-ffmpeg-strict" )
168     add_obsolete_float( "sout-ffmpeg-lumi-masking" )
169     add_obsolete_float( "sout-ffmpeg-dark-masking" )
170     add_obsolete_float( "sout-ffmpeg-p-masking" )
171     add_obsolete_float( "sout-ffmpeg-border-masking" )
172     add_obsolete_integer( "sout-ffmpeg-luma-elim-threshold" )
173     add_obsolete_integer( "sout-ffmpeg-chroma-elim-threshold" )
174     add_obsolete_string( "sout-ffmpeg-aac-profile" )
175
176
177     add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
178     add_string( ENC_CFG_PREFIX "hq", "rd", ENC_HQ_TEXT,
179                 ENC_HQ_LONGTEXT, false )
180         change_string_list( enc_hq_list, enc_hq_list_text )
181     add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
182                  ENC_KEYINT_LONGTEXT, false )
183     add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
184                  ENC_BFRAMES_LONGTEXT, false )
185     add_bool( ENC_CFG_PREFIX "hurry-up", false, ENC_HURRYUP_TEXT,
186               ENC_HURRYUP_LONGTEXT, false )
187     add_bool( ENC_CFG_PREFIX "interlace", false, ENC_INTERLACE_TEXT,
188               ENC_INTERLACE_LONGTEXT, true )
189     add_bool( ENC_CFG_PREFIX "interlace-me", true, ENC_INTERLACE_ME_TEXT,
190               ENC_INTERLACE_ME_LONGTEXT, true )
191     add_integer( ENC_CFG_PREFIX "vt", 0, ENC_VT_TEXT,
192                  ENC_VT_LONGTEXT, true )
193     add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
194               ENC_PRE_ME_LONGTEXT, true )
195     add_integer( ENC_CFG_PREFIX "rc-buffer-size", 0,
196                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
197     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
198                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
199     add_float( ENC_CFG_PREFIX "i-quant-factor", 0,
200                ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
201     add_integer( ENC_CFG_PREFIX "noise-reduction", 0,
202                  ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
203     add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false,
204               ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
205     add_integer( ENC_CFG_PREFIX "qmin", 0,
206                  ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
207     add_integer( ENC_CFG_PREFIX "qmax", 0,
208                  ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
209     add_bool( ENC_CFG_PREFIX "trellis", false,
210               ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
211     add_float( ENC_CFG_PREFIX "qscale", 0,
212                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
213     add_integer( ENC_CFG_PREFIX "strict", 0,
214                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
215         change_integer_range( -2, 2 )
216     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
217                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
218     add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
219                ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
220     add_float( ENC_CFG_PREFIX "p-masking", 0.0,
221                ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
222     add_float( ENC_CFG_PREFIX "border-masking", 0.0,
223                ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
224     add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0,
225                  ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
226     add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0,
227                  ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
228
229     /* Audio AAC encoder profile */
230     add_string( ENC_CFG_PREFIX "aac-profile", "low",
231                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
232
233     add_string( ENC_CFG_PREFIX "options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
234 #endif /* ENABLE_SOUT */
235
236 #ifdef MERGE_FFMPEG
237     add_submodule ()
238 #   include "../../demux/avformat/avformat.c"
239     add_submodule ()
240         AVIO_MODULE
241 #endif
242 vlc_module_end ()
243
244 /*****************************************************************************
245  * OpenDecoder: probe the decoder and return score
246  *****************************************************************************/
247 static int OpenDecoder( vlc_object_t *p_this )
248 {
249     decoder_t *p_dec = (decoder_t*) p_this;
250     unsigned i_codec_id;
251     int i_cat, i_result;
252     const char *psz_namecodec;
253
254     AVCodecContext *p_context = NULL;
255     const AVCodec  *p_codec = NULL;
256
257     /* *** determine codec type *** */
258     if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
259                              &psz_namecodec ) )
260     {
261         return VLC_EGENERIC;
262     }
263
264     /* Initialization must be done before avcodec_find_decoder() */
265     vlc_init_avcodec(p_this);
266
267     /* *** ask ffmpeg for a decoder *** */
268     char *psz_decoder = var_CreateGetString( p_this, "avcodec-codec" );
269     if( psz_decoder && *psz_decoder )
270     {
271         p_codec = avcodec_find_decoder_by_name( psz_decoder );
272         if( !p_codec )
273             msg_Err( p_this, "Decoder `%s' not found", psz_decoder );
274         else if( p_codec->id != i_codec_id )
275         {
276             msg_Err( p_this, "Decoder `%s' can't handle %4.4s",
277                     psz_decoder, (char*)&p_dec->fmt_in.i_codec );
278             p_codec = NULL;
279         }
280     }
281     free( psz_decoder );
282     if( !p_codec )
283         p_codec = avcodec_find_decoder( i_codec_id );
284     if( !p_codec )
285     {
286         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
287         return VLC_EGENERIC;
288     }
289
290     /* *** get a p_context *** */
291     p_context = avcodec_alloc_context3(p_codec);
292     if( !p_context )
293         return VLC_ENOMEM;
294     p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
295     p_context->opaque = (void *)p_this;
296
297     p_dec->b_need_packetized = true;
298     switch( i_cat )
299     {
300     case VIDEO_ES:
301         i_result =  InitVideoDec( p_dec, p_context, p_codec, i_codec_id );
302         break;
303     case AUDIO_ES:
304         i_result =  InitAudioDec( p_dec, p_context, p_codec, i_codec_id );
305         break;
306     case SPU_ES:
307         i_result =  InitSubtitleDec( p_dec, p_context, p_codec, i_codec_id );
308         break;
309     default:
310         return VLC_EGENERIC;
311     }
312
313     if( i_result == VLC_SUCCESS )
314     {
315         if( p_context->profile != FF_PROFILE_UNKNOWN)
316             p_dec->fmt_in.i_profile = p_context->profile;
317         if( p_context->level != FF_LEVEL_UNKNOWN)
318             p_dec->fmt_in.i_level = p_context->level;
319     }
320
321     return i_result;
322 }
323
324 /*****************************************************************************
325  * CloseDecoder: decoder destruction
326  *****************************************************************************/
327 static void CloseDecoder( vlc_object_t *p_this )
328 {
329     decoder_t *p_dec = (decoder_t *)p_this;
330     decoder_sys_t *p_sys = p_dec->p_sys;
331
332     switch( p_dec->fmt_out.i_cat )
333     {
334     case VIDEO_ES:
335          EndVideoDec ( p_dec );
336         break;
337     }
338
339     if( p_sys->p_context )
340     {
341         av_free( p_sys->p_context->extradata );
342         p_sys->p_context->extradata = NULL;
343
344         if( !p_sys->b_delayed_open )
345         {
346             vlc_avcodec_lock();
347             avcodec_close( p_sys->p_context );
348             vlc_avcodec_unlock();
349         }
350         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->p_codec->name );
351         av_free( p_sys->p_context );
352     }
353
354     free( p_sys );
355 }
356
357 /*****************************************************************************
358  * ffmpeg_OpenCodec:
359  *****************************************************************************/
360 int ffmpeg_OpenCodec( decoder_t *p_dec )
361 {
362     decoder_sys_t *p_sys = p_dec->p_sys;
363     char *psz_opts = var_InheritString( p_dec, "avcodec-options" );
364     AVDictionary *options = NULL;
365     int ret;
366
367     if (psz_opts && *psz_opts)
368         options = vlc_av_get_options(psz_opts);
369     free(psz_opts);
370
371     vlc_avcodec_lock();
372     ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, options ? &options : NULL );
373     vlc_avcodec_unlock();
374
375     AVDictionaryEntry *t = NULL;
376     while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
377         msg_Err( p_dec, "Unknown option \"%s\"", t->key );
378     }
379     av_dict_free(&options);
380
381     if( ret < 0 )
382     {
383         msg_Err( p_dec, "cannot start codec (%s)", p_sys->p_codec->name );
384         return VLC_EGENERIC;
385     }
386
387     msg_Dbg( p_dec, "codec (%s) started", p_sys->p_codec->name );
388     p_sys->b_delayed_open = false;
389     return VLC_SUCCESS;
390 }