1 /*****************************************************************************
2 * avcodec.c: video and audio decoder and encoder using libavcodec
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_avcodec.h>
40 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
41 # include <libavcodec/avcodec.h>
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 # include <ffmpeg/avcodec.h>
52 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
53 # error You must update libavcodec to a version >= 52.25.0
56 /*****************************************************************************
57 * decoder_sys_t: decoder descriptor
58 *****************************************************************************/
61 /* Common part between video and audio decoder */
65 /****************************************************************************
67 ****************************************************************************/
68 static int OpenDecoder( vlc_object_t * );
69 static void CloseDecoder( vlc_object_t * );
71 static const int nloopf_list[] = { 0, 1, 2, 3, 4 };
72 static const char *const nloopf_list_text[] =
73 { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
76 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
77 static const char *const enc_hq_list_text[] = {
78 N_("rd"), N_("bits"), N_("simple") };
82 # include "../../demux/avformat/avformat.h"
83 # include "../../access/avio.h"
86 /*****************************************************************************
88 *****************************************************************************/
89 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
90 "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
91 "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
94 set_shortname( "FFmpeg")
95 add_shortcut( "ffmpeg" )
96 set_category( CAT_INPUT )
97 set_subcategory( SUBCAT_INPUT_VCODEC )
98 /* decoder main module */
99 #if defined(MODULE_NAME_is_ffmpegaltivec) \
100 || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
101 set_description( N_("AltiVec FFmpeg audio/video decoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") )
102 /*add_requirement( ALTIVEC )*/
103 set_capability( "decoder", 71 )
105 set_description( N_("FFmpeg audio/video decoder") )
106 set_help( MODULE_DESCRIPTION )
107 set_capability( "decoder", 70 )
109 set_section( N_("Decoding") , NULL )
110 set_callbacks( OpenDecoder, CloseDecoder )
113 add_bool( "ffmpeg-dr", true, NULL, DR_TEXT, DR_TEXT, true )
114 add_integer ( "ffmpeg-error-resilience", 1, NULL, ERROR_TEXT,
115 ERROR_LONGTEXT, true )
116 add_integer ( "ffmpeg-workaround-bugs", 1, NULL, BUGS_TEXT, BUGS_LONGTEXT,
118 add_bool( "ffmpeg-hurry-up", true, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT,
120 add_integer( "ffmpeg-skip-frame", 0, NULL, SKIP_FRAME_TEXT,
121 SKIP_FRAME_LONGTEXT, true )
122 change_integer_range( -1, 4 )
123 add_integer( "ffmpeg-skip-idct", 0, NULL, SKIP_IDCT_TEXT,
124 SKIP_IDCT_LONGTEXT, true )
125 change_integer_range( -1, 4 )
126 add_integer ( "ffmpeg-vismv", 0, NULL, VISMV_TEXT, VISMV_LONGTEXT,
128 add_integer ( "ffmpeg-lowres", 0, NULL, LOWRES_TEXT, LOWRES_LONGTEXT,
130 change_integer_range( 0, 2 )
131 add_bool( "ffmpeg-fast", false, NULL, FAST_TEXT, FAST_LONGTEXT, true )
132 add_integer ( "ffmpeg-skiploopfilter", 0, NULL, SKIPLOOPF_TEXT,
133 SKIPLOOPF_LONGTEXT, true )
135 change_integer_list( nloopf_list, nloopf_list_text, NULL )
137 add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT,
139 #if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2)
140 add_bool( "ffmpeg-hw", false, NULL, HW_TEXT, HW_LONGTEXT, true )
142 #if defined(FF_THREAD_FRAME)
143 add_integer( "ffmpeg-threads", 0, NULL, 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 add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT,
157 ENC_HQ_LONGTEXT, false )
158 change_string_list( enc_hq_list, enc_hq_list_text, 0 )
159 add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT,
160 ENC_KEYINT_LONGTEXT, false )
161 add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT,
162 ENC_BFRAMES_LONGTEXT, false )
163 add_bool( ENC_CFG_PREFIX "hurry-up", false, NULL, ENC_HURRYUP_TEXT,
164 ENC_HURRYUP_LONGTEXT, false )
165 add_bool( ENC_CFG_PREFIX "interlace", false, NULL, ENC_INTERLACE_TEXT,
166 ENC_INTERLACE_LONGTEXT, true )
167 add_bool( ENC_CFG_PREFIX "interlace-me", true, NULL, ENC_INTERLACE_ME_TEXT,
168 ENC_INTERLACE_ME_LONGTEXT, true )
169 add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT,
170 ENC_VT_LONGTEXT, true )
171 add_bool( ENC_CFG_PREFIX "pre-me", false, NULL, ENC_PRE_ME_TEXT,
172 ENC_PRE_ME_LONGTEXT, true )
173 add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL,
174 ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
175 add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL,
176 ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
177 add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL,
178 ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
179 add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL,
180 ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
181 add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false, NULL,
182 ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
183 add_integer( ENC_CFG_PREFIX "qmin", 0, NULL,
184 ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
185 add_integer( ENC_CFG_PREFIX "qmax", 0, NULL,
186 ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
187 add_bool( ENC_CFG_PREFIX "trellis", false, NULL,
188 ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
189 add_float( ENC_CFG_PREFIX "qscale", 0, NULL,
190 ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
191 add_integer( ENC_CFG_PREFIX "strict", 0, NULL,
192 ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
193 add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL,
194 ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
195 add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL,
196 ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
197 add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL,
198 ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
199 add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL,
200 ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
201 add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL,
202 ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
203 add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL,
204 ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
206 /* Audio AAC encoder profile */
207 add_string( ENC_CFG_PREFIX "aac-profile", "low", NULL,
208 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
209 #endif /* ENABLE_SOUT */
211 /* video filter submodule */
213 set_capability( "video filter2", 0 )
214 set_callbacks( OpenDeinterlace, CloseDeinterlace )
215 set_description( N_("FFmpeg deinterlace video filter") )
216 add_shortcut( "ffmpeg-deinterlace" )
220 # include "../../demux/avformat/avformat.c"
226 /*****************************************************************************
227 * OpenDecoder: probe the decoder and return score
228 *****************************************************************************/
229 static int OpenDecoder( vlc_object_t *p_this )
231 decoder_t *p_dec = (decoder_t*) p_this;
232 int i_cat, i_codec_id, i_result;
233 const char *psz_namecodec;
235 AVCodecContext *p_context = NULL;
236 AVCodec *p_codec = NULL;
238 /* *** determine codec type *** */
239 if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
245 /* Initialization must be done before avcodec_find_decoder() */
246 InitLibavcodec(p_this);
248 /* *** ask ffmpeg for a decoder *** */
249 p_codec = avcodec_find_decoder( i_codec_id );
252 msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
256 /* *** get a p_context *** */
257 p_context = avcodec_alloc_context();
260 p_context->debug = var_InheritInteger( p_dec, "ffmpeg-debug" );
261 p_context->opaque = (void *)p_this;
263 /* Set CPU capabilities */
264 unsigned i_cpu = vlc_CPU();
265 p_context->dsp_mask = 0;
266 if( !(i_cpu & CPU_CAPABILITY_MMX) )
268 p_context->dsp_mask |= FF_MM_MMX;
270 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
272 p_context->dsp_mask |= FF_MM_MMXEXT;
274 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
276 p_context->dsp_mask |= FF_MM_3DNOW;
278 if( !(i_cpu & CPU_CAPABILITY_SSE) )
280 p_context->dsp_mask |= FF_MM_SSE;
282 if( !(i_cpu & CPU_CAPABILITY_SSE2) )
284 p_context->dsp_mask |= FF_MM_SSE2;
287 if( !(i_cpu & CPU_CAPABILITY_SSE3) )
288 p_context->dsp_mask |= FF_MM_SSE3;
291 if( !(i_cpu & CPU_CAPABILITY_SSSE3) )
292 p_context->dsp_mask |= FF_MM_SSSE3;
295 if( !(i_cpu & CPU_CAPABILITY_SSE4_1) )
296 p_context->dsp_mask |= FF_MM_SSE4;
299 if( !(i_cpu & CPU_CAPABILITY_SSE4_2) )
300 p_context->dsp_mask |= FF_MM_SSE42;
303 p_dec->b_need_packetized = true;
307 p_dec->pf_decode_video = DecodeVideo;
308 i_result = InitVideoDec ( p_dec, p_context, p_codec,
309 i_codec_id, psz_namecodec );
312 p_dec->pf_decode_audio = DecodeAudio;
313 i_result = InitAudioDec ( p_dec, p_context, p_codec,
314 i_codec_id, psz_namecodec );
317 p_dec->pf_decode_sub = DecodeSubtitle;
318 i_result = InitSubtitleDec( p_dec, p_context, p_codec,
319 i_codec_id, psz_namecodec );
322 i_result = VLC_EGENERIC;
325 if( i_result == VLC_SUCCESS )
327 p_dec->p_sys->i_cat = i_cat;
328 if( p_context->profile != FF_PROFILE_UNKNOWN)
329 p_dec->fmt_in.i_profile = p_context->profile;
330 if( p_context->level != FF_LEVEL_UNKNOWN)
331 p_dec->fmt_in.i_level = p_context->level;
337 /*****************************************************************************
338 * CloseDecoder: decoder destruction
339 *****************************************************************************/
340 static void CloseDecoder( vlc_object_t *p_this )
342 decoder_t *p_dec = (decoder_t *)p_this;
343 decoder_sys_t *p_sys = p_dec->p_sys;
345 switch( p_sys->i_cat )
348 EndAudioDec ( p_dec );
351 EndVideoDec ( p_dec );
354 EndSubtitleDec( p_dec );
358 if( p_sys->p_context )
360 free( p_sys->p_context->extradata );
361 p_sys->p_context->extradata = NULL;
363 if( !p_sys->b_delayed_open )
366 avcodec_close( p_sys->p_context );
367 vlc_avcodec_unlock();
369 msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
370 av_free( p_sys->p_context );
376 void InitLibavcodec( vlc_object_t *p_object )
378 static bool b_ffmpeginit = false;
382 /* *** init ffmpeg library (libavcodec) *** */
386 avcodec_register_all();
387 av_log_set_callback( LibavutilCallback );
390 msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
391 LIBAVCODEC_VERSION_INT );
395 msg_Dbg( p_object, "libavcodec already initialized" );
398 vlc_avcodec_unlock();
401 /*****************************************************************************
403 *****************************************************************************/
404 int ffmpeg_OpenCodec( decoder_t *p_dec )
406 decoder_sys_t *p_sys = p_dec->p_sys;
408 if( p_sys->p_context->extradata_size <= 0 )
410 if( p_sys->i_codec_id == CODEC_ID_VC1 ||
411 p_sys->i_codec_id == CODEC_ID_VORBIS ||
412 p_sys->i_codec_id == CODEC_ID_THEORA ||
413 p_sys->i_codec_id == CODEC_ID_AAC )
415 msg_Warn( p_dec, "waiting for extra data for codec %s",
416 p_sys->psz_namecodec );
420 if( p_dec->fmt_in.i_cat == VIDEO_ES )
422 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
423 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
424 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
426 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
428 p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
429 p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
431 p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
432 p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
433 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
437 ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
438 vlc_avcodec_unlock();
441 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
443 #ifdef HAVE_AVCODEC_MT
444 if( p_dec->fmt_in.i_cat == VIDEO_ES )
446 switch( p_sys->p_context->active_thread_type )
448 case FF_THREAD_FRAME:
449 msg_Dbg( p_dec, "using frame thread mode with %d threads",
450 p_sys->p_context->thread_count );
452 case FF_THREAD_SLICE:
453 msg_Dbg( p_dec, "using slice thread mode with %d threads",
454 p_sys->p_context->thread_count );
457 if( p_sys->p_context->thread_count > 1 )
458 msg_Warn( p_dec, "failed to enable threaded decoding" );
461 msg_Warn( p_dec, "using unknown thread mode with %d threads",
462 p_sys->p_context->thread_count );
468 p_sys->b_delayed_open = false;