1 /*****************************************************************************
2 * avcodec.c: video and audio decoder and encoder using libavcodec
3 *****************************************************************************
4 * Copyright (C) 1999-2008 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_avcodec.h>
39 #include <libavcodec/avcodec.h>
45 /*****************************************************************************
46 * decoder_sys_t: decoder descriptor
47 *****************************************************************************/
50 /* Common part between video and audio decoder */
51 AVCODEC_COMMON_MEMBERS
54 /****************************************************************************
56 ****************************************************************************/
57 static int OpenDecoder( vlc_object_t * );
58 static void CloseDecoder( vlc_object_t * );
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") };
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") };
71 # include "../../demux/avformat/avformat.h"
72 # include "../../access/avio.h"
75 /*****************************************************************************
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")
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 )
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,
103 add_obsolete_bool( "ffmpeg-hurry-up" ) /* removed since 2.1.0 */
104 add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
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)
123 change_integer_list( nloopf_list, nloopf_list_text )
125 add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
126 add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
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 );
136 add_string( "avcodec-options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
140 /* encoder 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 )
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" )
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 )
229 /* Audio AAC encoder profile */
230 add_string( ENC_CFG_PREFIX "aac-profile", "low",
231 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
233 add_string( ENC_CFG_PREFIX "options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
234 #endif /* ENABLE_SOUT */
238 # include "../../demux/avformat/avformat.c"
244 /*****************************************************************************
245 * OpenDecoder: probe the decoder and return score
246 *****************************************************************************/
247 static int OpenDecoder( vlc_object_t *p_this )
249 decoder_t *p_dec = (decoder_t*) p_this;
252 const char *psz_namecodec;
254 const AVCodec *p_codec = NULL;
256 /* *** determine codec type *** */
257 if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
263 /* Initialization must be done before avcodec_find_decoder() */
264 vlc_init_avcodec(p_this);
266 /* *** ask ffmpeg for a decoder *** */
267 char *psz_decoder = var_CreateGetString( p_this, "avcodec-codec" );
268 if( psz_decoder && *psz_decoder )
270 p_codec = avcodec_find_decoder_by_name( psz_decoder );
272 msg_Err( p_this, "Decoder `%s' not found", psz_decoder );
273 else if( p_codec->id != i_codec_id )
275 msg_Err( p_this, "Decoder `%s' can't handle %4.4s",
276 psz_decoder, (char*)&p_dec->fmt_in.i_codec );
282 p_codec = avcodec_find_decoder( i_codec_id );
285 msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
289 /* *** get a p_context *** */
290 AVCodecContext *avctx = avcodec_alloc_context3(p_codec);
291 if( unlikely(avctx == NULL) )
294 avctx->debug = var_InheritInteger( p_dec, "avcodec-debug" );
295 avctx->opaque = p_dec;
302 ret = InitVideoDec( p_dec, avctx, p_codec );
305 ret = InitAudioDec( p_dec, avctx, p_codec );
308 ret = InitSubtitleDec( p_dec, avctx, p_codec );
314 if( ret != VLC_SUCCESS )
316 avcodec_free_context( &avctx );
320 if( avctx->profile != FF_PROFILE_UNKNOWN)
321 p_dec->fmt_in.i_profile = avctx->profile;
322 if( avctx->level != FF_LEVEL_UNKNOWN)
323 p_dec->fmt_in.i_level = avctx->level;
325 p_dec->b_need_packetized = true;
330 /*****************************************************************************
331 * CloseDecoder: decoder destruction
332 *****************************************************************************/
333 static void CloseDecoder( vlc_object_t *p_this )
335 decoder_t *p_dec = (decoder_t *)p_this;
337 if( p_dec->fmt_out.i_cat == VIDEO_ES )
338 EndVideoDec( p_dec );
340 ffmpeg_CloseCodec( p_dec );
342 decoder_sys_t *p_sys = p_dec->p_sys;
344 av_free( p_sys->p_context->extradata );
345 avcodec_free_context( &p_sys->p_context );
349 /*****************************************************************************
351 *****************************************************************************/
352 int ffmpeg_OpenCodec( decoder_t *p_dec )
354 decoder_sys_t *p_sys = p_dec->p_sys;
355 char *psz_opts = var_InheritString( p_dec, "avcodec-options" );
356 AVDictionary *options = NULL;
359 if (psz_opts && *psz_opts)
360 options = vlc_av_get_options(psz_opts);
364 ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, options ? &options : NULL );
365 vlc_avcodec_unlock();
367 AVDictionaryEntry *t = NULL;
368 while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
369 msg_Err( p_dec, "Unknown option \"%s\"", t->key );
371 av_dict_free(&options);
375 msg_Err( p_dec, "cannot start codec (%s)", p_sys->p_codec->name );
379 msg_Dbg( p_dec, "codec (%s) started", p_sys->p_codec->name );
380 p_sys->b_delayed_open = false;
384 void ffmpeg_CloseCodec( decoder_t *p_dec )
386 decoder_sys_t *p_sys = p_dec->p_sys;
388 if( p_sys->b_delayed_open )
392 avcodec_close( p_sys->p_context );
393 vlc_avcodec_unlock();
394 msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->p_codec->name );