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 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 54, 25, 0 )
46 # error You must update libavcodec to a version >= 54.25.0
49 /*****************************************************************************
50 * decoder_sys_t: decoder descriptor
51 *****************************************************************************/
54 /* Common part between video and audio decoder */
55 AVCODEC_COMMON_MEMBERS
58 /****************************************************************************
60 ****************************************************************************/
61 static int OpenDecoder( vlc_object_t * );
62 static void CloseDecoder( vlc_object_t * );
64 static const int nloopf_list[] = { 0, 1, 2, 3, 4 };
65 static const char *const nloopf_list_text[] =
66 { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
69 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
70 static const char *const enc_hq_list_text[] = {
71 N_("rd"), N_("bits"), N_("simple") };
75 # include "../../demux/avformat/avformat.h"
76 # include "../../access/avio.h"
79 /*****************************************************************************
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")
87 set_shortname( "FFmpeg")
88 add_shortcut( "ffmpeg" )
89 set_category( CAT_INPUT )
90 set_subcategory( SUBCAT_INPUT_VCODEC )
91 /* decoder main module */
92 set_description( N_("FFmpeg audio/video decoder") )
93 set_help( MODULE_DESCRIPTION )
94 set_capability( "decoder", 70 )
95 set_section( N_("Decoding") , NULL )
96 set_callbacks( OpenDecoder, CloseDecoder )
99 add_obsolete_bool( "ffmpeg-dr" ) /* removed since 2.1.0 */
100 add_bool( "avcodec-dr", true, DR_TEXT, DR_TEXT, true )
101 add_obsolete_integer ( "ffmpeg-error-resilience" ) /* removed since 2.1.0 */
102 add_integer ( "avcodec-error-resilience", 1, ERROR_TEXT,
103 ERROR_LONGTEXT, true )
104 add_obsolete_integer ( "ffmpeg-workaround-bugs" ) /* removed since 2.1.0 */
105 add_integer ( "avcodec-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
107 add_obsolete_bool( "ffmpeg-hurry-up" ) /* removed since 2.1.0 */
108 add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
110 add_obsolete_integer( "ffmpeg-skip-frame") /* removed since 2.1.0 */
111 add_integer( "avcodec-skip-frame", 0, SKIP_FRAME_TEXT,
112 SKIP_FRAME_LONGTEXT, true )
113 change_integer_range( -1, 4 )
114 add_obsolete_integer( "ffmpeg-skip-idct" ) /* removed since 2.1.0 */
115 add_integer( "avcodec-skip-idct", 0, SKIP_IDCT_TEXT,
116 SKIP_IDCT_LONGTEXT, true )
117 change_integer_range( -1, 4 )
118 add_obsolete_integer ( "ffmpeg-vismv" ) /* removed since 2.1.0 */
119 add_integer ( "avcodec-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
121 add_obsolete_integer ( "ffmpeg-lowres" ) /* removed since 2.1.0 */
122 add_obsolete_bool( "ffmpeg-fast" ) /* removed since 2.1.0 */
123 add_bool( "avcodec-fast", false, FAST_TEXT, FAST_LONGTEXT, false )
124 add_obsolete_integer ( "ffmpeg-skiploopfilter" ) /* removed since 2.1.0 */
125 add_integer ( "avcodec-skiploopfilter", 0, SKIPLOOPF_TEXT,
126 SKIPLOOPF_LONGTEXT, false)
128 change_integer_list( nloopf_list, nloopf_list_text )
129 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 54, 41, 0 )
130 add_bool( "avcodec-ignorecrop", false, IGNORECROP_TEXT, IGNORECROP_LONGTEXT,
134 add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
135 add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
137 add_obsolete_string( "ffmpeg-codec" ) /* removed since 2.1.0 */
138 add_string( "avcodec-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
139 add_obsolete_bool( "ffmpeg-hw" ) /* removed since 2.1.0 */
140 add_module( "avcodec-hw", "hw decoder", "none", HW_TEXT, HW_LONGTEXT, false )
141 #if defined(FF_THREAD_FRAME)
142 add_obsolete_integer( "ffmpeg-threads" ) /* removed since 2.1.0 */
143 add_integer( "avcodec-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
148 /* encoder submodule */
150 add_shortcut( "ffmpeg" )
151 set_section( N_("Encoding") , NULL )
152 set_description( N_("FFmpeg audio/video encoder") )
153 set_capability( "encoder", 100 )
154 set_callbacks( OpenEncoder, CloseEncoder )
156 /* removed in 2.1.0 */
157 add_obsolete_string( "sout-ffmpeg-codec" )
158 add_obsolete_string( "sout-ffmpeg-hq" )
159 add_obsolete_integer( "sout-ffmpeg-keyint" )
160 add_obsolete_integer( "sout-ffmpeg-bframes" )
161 add_obsolete_bool( "sout-ffmpeg-hurry-up" )
162 add_obsolete_bool( "sout-ffmpeg-interlace" )
163 add_obsolete_bool( "sout-ffmpeg-interlace-me" )
164 add_obsolete_integer( "sout-ffmpeg-vt" )
165 add_obsolete_bool( "sout-ffmpeg-pre-me" )
166 add_obsolete_integer( "sout-ffmpeg-rc-buffer-size" )
167 add_obsolete_float( "sout-ffmpeg-rc-buffer-aggressivity" )
168 add_obsolete_float( "sout-ffmpeg-i-quant-factor" )
169 add_obsolete_integer( "sout-ffmpeg-noise-reduction" )
170 add_obsolete_bool( "sout-ffmpeg-mpeg4-matrix" )
171 add_obsolete_integer( "sout-ffmpeg-qmin" )
172 add_obsolete_integer( "sout-ffmpeg-qmax" )
173 add_obsolete_bool( "sout-ffmpeg-trellis" )
174 add_obsolete_float( "sout-ffmpeg-qscale" )
175 add_obsolete_integer( "sout-ffmpeg-strict" )
176 add_obsolete_float( "sout-ffmpeg-lumi-masking" )
177 add_obsolete_float( "sout-ffmpeg-dark-masking" )
178 add_obsolete_float( "sout-ffmpeg-p-masking" )
179 add_obsolete_float( "sout-ffmpeg-border-masking" )
180 add_obsolete_integer( "sout-ffmpeg-luma-elim-threshold" )
181 add_obsolete_integer( "sout-ffmpeg-chroma-elim-threshold" )
182 add_obsolete_string( "sout-ffmpeg-aac-profile" )
185 add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
186 add_string( ENC_CFG_PREFIX "hq", "simple", ENC_HQ_TEXT,
187 ENC_HQ_LONGTEXT, false )
188 change_string_list( enc_hq_list, enc_hq_list_text )
189 add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
190 ENC_KEYINT_LONGTEXT, false )
191 add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
192 ENC_BFRAMES_LONGTEXT, false )
193 add_bool( ENC_CFG_PREFIX "hurry-up", false, ENC_HURRYUP_TEXT,
194 ENC_HURRYUP_LONGTEXT, false )
195 add_bool( ENC_CFG_PREFIX "interlace", false, ENC_INTERLACE_TEXT,
196 ENC_INTERLACE_LONGTEXT, true )
197 add_bool( ENC_CFG_PREFIX "interlace-me", true, ENC_INTERLACE_ME_TEXT,
198 ENC_INTERLACE_ME_LONGTEXT, true )
199 add_integer( ENC_CFG_PREFIX "vt", 0, ENC_VT_TEXT,
200 ENC_VT_LONGTEXT, true )
201 add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
202 ENC_PRE_ME_LONGTEXT, true )
203 add_integer( ENC_CFG_PREFIX "rc-buffer-size", 0,
204 ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
205 add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
206 ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
207 add_float( ENC_CFG_PREFIX "i-quant-factor", 0,
208 ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
209 add_integer( ENC_CFG_PREFIX "noise-reduction", 0,
210 ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
211 add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false,
212 ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
213 add_integer( ENC_CFG_PREFIX "qmin", 0,
214 ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
215 add_integer( ENC_CFG_PREFIX "qmax", 0,
216 ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
217 add_bool( ENC_CFG_PREFIX "trellis", false,
218 ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
219 add_float( ENC_CFG_PREFIX "qscale", 0,
220 ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
221 add_integer( ENC_CFG_PREFIX "strict", 0,
222 ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
223 change_integer_range( -2, 2 )
224 add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
225 ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
226 add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
227 ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
228 add_float( ENC_CFG_PREFIX "p-masking", 0.0,
229 ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
230 add_float( ENC_CFG_PREFIX "border-masking", 0.0,
231 ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
232 add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0,
233 ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
234 add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0,
235 ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
237 /* Audio AAC encoder profile */
238 add_string( ENC_CFG_PREFIX "aac-profile", "low",
239 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
240 #endif /* ENABLE_SOUT */
244 # include "../../demux/avformat/avformat.c"
250 /*****************************************************************************
251 * OpenDecoder: probe the decoder and return score
252 *****************************************************************************/
253 static int OpenDecoder( vlc_object_t *p_this )
255 decoder_t *p_dec = (decoder_t*) p_this;
256 int i_cat, i_codec_id, i_result;
257 const char *psz_namecodec;
259 AVCodecContext *p_context = NULL;
260 AVCodec *p_codec = NULL;
262 /* *** determine codec type *** */
263 if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
269 /* Initialization must be done before avcodec_find_decoder() */
272 /* *** ask ffmpeg for a decoder *** */
273 char *psz_decoder = var_CreateGetString( p_this, "avcodec-codec" );
274 if( psz_decoder && *psz_decoder )
276 p_codec = avcodec_find_decoder_by_name( psz_decoder );
278 msg_Err( p_this, "Decoder `%s' not found", psz_decoder );
279 else if( p_codec->id != i_codec_id )
281 msg_Err( p_this, "Decoder `%s' can't handle %4.4s",
282 psz_decoder, (char*)&p_dec->fmt_in.i_codec );
288 p_codec = avcodec_find_decoder( i_codec_id );
291 msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
295 /* *** get a p_context *** */
296 p_context = avcodec_alloc_context3(p_codec);
299 p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
300 p_context->opaque = (void *)p_this;
302 /* set CPU capabilities */
303 #if LIBAVUTIL_VERSION_CHECK(51, 25, 0, 42, 100)
304 av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
306 p_context->dsp_mask = GetVlcDspMask();
309 p_dec->b_need_packetized = true;
313 p_dec->pf_decode_video = DecodeVideo;
314 i_result = InitVideoDec ( p_dec, p_context, p_codec,
315 i_codec_id, psz_namecodec );
318 p_dec->pf_decode_audio = DecodeAudio;
319 i_result = InitAudioDec ( p_dec, p_context, p_codec,
320 i_codec_id, psz_namecodec );
323 p_dec->pf_decode_sub = DecodeSubtitle;
324 i_result = InitSubtitleDec( p_dec, p_context, p_codec,
325 i_codec_id, psz_namecodec );
328 i_result = VLC_EGENERIC;
331 if( i_result == VLC_SUCCESS )
333 p_dec->p_sys->i_cat = i_cat;
334 if( p_context->profile != FF_PROFILE_UNKNOWN)
335 p_dec->fmt_in.i_profile = p_context->profile;
336 if( p_context->level != FF_LEVEL_UNKNOWN)
337 p_dec->fmt_in.i_level = p_context->level;
343 /*****************************************************************************
344 * CloseDecoder: decoder destruction
345 *****************************************************************************/
346 static void CloseDecoder( vlc_object_t *p_this )
348 decoder_t *p_dec = (decoder_t *)p_this;
349 decoder_sys_t *p_sys = p_dec->p_sys;
351 switch( p_sys->i_cat )
354 EndVideoDec ( p_dec );
357 EndSubtitleDec( p_dec );
361 if( p_sys->p_context )
363 free( p_sys->p_context->extradata );
364 p_sys->p_context->extradata = NULL;
366 if( !p_sys->b_delayed_open )
369 avcodec_close( p_sys->p_context );
370 vlc_avcodec_unlock();
372 msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
373 av_free( p_sys->p_context );
379 /*****************************************************************************
381 *****************************************************************************/
382 int ffmpeg_OpenCodec( decoder_t *p_dec )
384 decoder_sys_t *p_sys = p_dec->p_sys;
386 if( p_sys->p_context->extradata_size <= 0 )
388 if( p_sys->i_codec_id == AV_CODEC_ID_VC1 ||
389 p_sys->i_codec_id == AV_CODEC_ID_VORBIS ||
390 p_sys->i_codec_id == AV_CODEC_ID_THEORA ||
391 ( p_sys->i_codec_id == AV_CODEC_ID_AAC &&
392 !p_dec->fmt_in.b_packetized ) )
394 msg_Warn( p_dec, "waiting for extra data for codec %s",
395 p_sys->psz_namecodec );
399 if( p_dec->fmt_in.i_cat == VIDEO_ES )
401 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
402 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
403 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
405 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
407 p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
408 p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
410 p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
411 p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
412 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
413 if( p_sys->i_codec_id == AV_CODEC_ID_ADPCM_G726 &&
414 p_sys->p_context->bit_rate > 0 &&
415 p_sys->p_context->sample_rate > 0)
416 p_sys->p_context->bits_per_coded_sample = p_sys->p_context->bit_rate /
417 p_sys->p_context->sample_rate;
421 ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, NULL /* options */ );
422 vlc_avcodec_unlock();
425 msg_Dbg( p_dec, "avcodec codec (%s) started", p_sys->psz_namecodec );
427 #ifdef HAVE_AVCODEC_MT
428 if( p_dec->fmt_in.i_cat == VIDEO_ES )
430 switch( p_sys->p_context->active_thread_type )
432 case FF_THREAD_FRAME:
433 msg_Dbg( p_dec, "using frame thread mode with %d threads",
434 p_sys->p_context->thread_count );
436 case FF_THREAD_SLICE:
437 msg_Dbg( p_dec, "using slice thread mode with %d threads",
438 p_sys->p_context->thread_count );
441 if( p_sys->p_context->thread_count > 1 )
442 msg_Warn( p_dec, "failed to enable threaded decoding" );
445 msg_Warn( p_dec, "using unknown thread mode with %d threads",
446 p_sys->p_context->thread_count );
452 p_sys->b_delayed_open = false;