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>
50 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
51 # error You must update libavcodec to a version >= 52.25.0
54 /*****************************************************************************
55 * decoder_sys_t: decoder descriptor
56 *****************************************************************************/
59 /* Common part between video and audio decoder */
60 AVCODEC_COMMON_MEMBERS
63 /****************************************************************************
65 ****************************************************************************/
66 static int OpenDecoder( vlc_object_t * );
67 static void CloseDecoder( vlc_object_t * );
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") };
73 #if defined(HAVE_AVCODEC_VDA)
74 static const int nvda_pix_fmt_list[] = { 0, 1 };
75 static const char *const nvda_pix_fmt_list_text[] =
76 { N_("420YpCbCr8Planar"), N_("422YpCbCr8") };
80 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
81 static const char *const enc_hq_list_text[] = {
82 N_("rd"), N_("bits"), N_("simple") };
86 # include "../../demux/avformat/avformat.h"
87 # include "../../access/avio.h"
90 /*****************************************************************************
92 *****************************************************************************/
93 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
94 "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
95 "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
98 set_shortname( "FFmpeg")
99 add_shortcut( "ffmpeg" )
100 set_category( CAT_INPUT )
101 set_subcategory( SUBCAT_INPUT_VCODEC )
102 /* decoder main module */
103 set_description( N_("FFmpeg audio/video decoder") )
104 set_help( MODULE_DESCRIPTION )
105 set_capability( "decoder", 70 )
106 set_section( N_("Decoding") , NULL )
107 set_callbacks( OpenDecoder, CloseDecoder )
110 add_obsolete_bool( "ffmpeg-dr" ) /* removed since 2.1.0 */
111 add_bool( "avcodec-dr", true, DR_TEXT, DR_TEXT, true )
112 add_obsolete_integer ( "ffmpeg-error-resilience" ) /* removed since 2.1.0 */
113 add_integer ( "avcodec-error-resilience", 1, ERROR_TEXT,
114 ERROR_LONGTEXT, true )
115 add_obsolete_integer ( "ffmpeg-workaround-bugs" ) /* removed since 2.1.0 */
116 add_integer ( "avcodec-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
118 add_obsolete_bool( "ffmpeg-hurry-up" ) /* removed since 2.1.0 */
119 add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
121 add_obsolete_integer( "ffmpeg-skip-frame") /* removed since 2.1.0 */
122 add_integer( "avcodec-skip-frame", 0, SKIP_FRAME_TEXT,
123 SKIP_FRAME_LONGTEXT, true )
124 change_integer_range( -1, 4 )
125 add_obsolete_integer( "ffmpeg-skip-idct" ) /* removed since 2.1.0 */
126 add_integer( "avcodec-skip-idct", 0, SKIP_IDCT_TEXT,
127 SKIP_IDCT_LONGTEXT, true )
128 change_integer_range( -1, 4 )
129 add_obsolete_integer ( "ffmpeg-vismv" ) /* removed since 2.1.0 */
130 add_integer ( "avcodec-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
132 add_obsolete_integer ( "ffmpeg-lowres" ) /* removed since 2.1.0 */
133 add_integer ( "avcodec-lowres", 0, LOWRES_TEXT, LOWRES_LONGTEXT,
135 change_integer_range( 0, 2 )
136 add_obsolete_bool( "ffmpeg-fast" ) /* removed since 2.1.0 */
137 add_bool( "avcodec-fast", false, FAST_TEXT, FAST_LONGTEXT, false )
138 add_obsolete_integer ( "ffmpeg-skiploopfilter" ) /* removed since 2.1.0 */
139 add_integer ( "avcodec-skiploopfilter", 0, SKIPLOOPF_TEXT,
140 SKIPLOOPF_LONGTEXT, false)
142 change_integer_list( nloopf_list, nloopf_list_text )
144 add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
145 add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
147 add_obsolete_string( "ffmpeg-codec" ) /* removed since 2.1.0 */
148 add_string( "avcodec-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
149 #if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2) || defined(HAVE_AVCODEC_VDA)
150 add_obsolete_bool( "ffmpeg-hw" ) /* removed since 2.1.0 */
151 add_bool( "avcodec-hw", false, HW_TEXT, HW_LONGTEXT, false )
152 #if defined(HAVE_AVCODEC_VDA)
153 add_integer ( "avcodec-vda-pix-fmt", 0, VDA_PIX_FMT_TEXT,
154 VDA_PIX_FMT_LONGTEXT, false)
156 change_integer_list( nvda_pix_fmt_list, nvda_pix_fmt_list_text )
159 #if defined(FF_THREAD_FRAME)
160 add_obsolete_integer( "ffmpeg-threads" ) /* removed since 2.1.0 */
161 add_integer( "avcodec-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
166 /* encoder submodule */
168 add_shortcut( "ffmpeg" )
169 set_section( N_("Encoding") , NULL )
170 set_description( N_("FFmpeg audio/video encoder") )
171 set_capability( "encoder", 100 )
172 set_callbacks( OpenEncoder, CloseEncoder )
174 /* removed in 2.1.0 */
175 add_obsolete_string( "sout-ffmpeg-codec" )
176 add_obsolete_string( "sout-ffmpeg-hq" )
177 add_obsolete_integer( "sout-ffmpeg-keyint" )
178 add_obsolete_integer( "sout-ffmpeg-bframes" )
179 add_obsolete_bool( "sout-ffmpeg-hurry-up" )
180 add_obsolete_bool( "sout-ffmpeg-interlace" )
181 add_obsolete_bool( "sout-ffmpeg-interlace-me" )
182 add_obsolete_integer( "sout-ffmpeg-vt" )
183 add_obsolete_bool( "sout-ffmpeg-pre-me" )
184 add_obsolete_integer( "sout-ffmpeg-rc-buffer-size" )
185 add_obsolete_float( "sout-ffmpeg-rc-buffer-aggressivity" )
186 add_obsolete_float( "sout-ffmpeg-i-quant-factor" )
187 add_obsolete_integer( "sout-ffmpeg-noise-reduction" )
188 add_obsolete_bool( "sout-ffmpeg-mpeg4-matrix" )
189 add_obsolete_integer( "sout-ffmpeg-qmin" )
190 add_obsolete_integer( "sout-ffmpeg-qmax" )
191 add_obsolete_bool( "sout-ffmpeg-trellis" )
192 add_obsolete_float( "sout-ffmpeg-qscale" )
193 add_obsolete_integer( "sout-ffmpeg-strict" )
194 add_obsolete_float( "sout-ffmpeg-lumi-masking" )
195 add_obsolete_float( "sout-ffmpeg-dark-masking" )
196 add_obsolete_float( "sout-ffmpeg-p-masking" )
197 add_obsolete_float( "sout-ffmpeg-border-masking" )
198 add_obsolete_integer( "sout-ffmpeg-luma-elim-threshold" )
199 add_obsolete_integer( "sout-ffmpeg-chroma-elim-threshold" )
200 add_obsolete_string( "sout-ffmpeg-aac-profile" )
203 add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
204 add_string( ENC_CFG_PREFIX "hq", "simple", ENC_HQ_TEXT,
205 ENC_HQ_LONGTEXT, false )
206 change_string_list( enc_hq_list, enc_hq_list_text, 0 )
207 add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
208 ENC_KEYINT_LONGTEXT, false )
209 add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
210 ENC_BFRAMES_LONGTEXT, false )
211 add_bool( ENC_CFG_PREFIX "hurry-up", false, ENC_HURRYUP_TEXT,
212 ENC_HURRYUP_LONGTEXT, false )
213 add_bool( ENC_CFG_PREFIX "interlace", false, ENC_INTERLACE_TEXT,
214 ENC_INTERLACE_LONGTEXT, true )
215 add_bool( ENC_CFG_PREFIX "interlace-me", true, ENC_INTERLACE_ME_TEXT,
216 ENC_INTERLACE_ME_LONGTEXT, true )
217 add_integer( ENC_CFG_PREFIX "vt", 0, ENC_VT_TEXT,
218 ENC_VT_LONGTEXT, true )
219 add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
220 ENC_PRE_ME_LONGTEXT, true )
221 add_integer( ENC_CFG_PREFIX "rc-buffer-size", 0,
222 ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
223 add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
224 ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
225 add_float( ENC_CFG_PREFIX "i-quant-factor", 0,
226 ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
227 add_integer( ENC_CFG_PREFIX "noise-reduction", 0,
228 ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
229 add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false,
230 ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
231 add_integer( ENC_CFG_PREFIX "qmin", 0,
232 ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
233 add_integer( ENC_CFG_PREFIX "qmax", 0,
234 ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
235 add_bool( ENC_CFG_PREFIX "trellis", false,
236 ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
237 add_float( ENC_CFG_PREFIX "qscale", 0,
238 ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
239 add_integer( ENC_CFG_PREFIX "strict", 0,
240 ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
241 change_integer_range( -2, 2 )
242 add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
243 ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
244 add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
245 ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
246 add_float( ENC_CFG_PREFIX "p-masking", 0.0,
247 ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
248 add_float( ENC_CFG_PREFIX "border-masking", 0.0,
249 ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
250 add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0,
251 ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
252 add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0,
253 ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
255 /* Audio AAC encoder profile */
256 add_string( ENC_CFG_PREFIX "aac-profile", "low",
257 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
258 #endif /* ENABLE_SOUT */
260 /* video filter submodule */
262 set_capability( "video filter2", 0 )
263 set_callbacks( OpenDeinterlace, CloseDeinterlace )
264 set_description( N_("FFmpeg deinterlace video filter") )
265 add_shortcut( "ffmpeg-deinterlace" )
269 # include "../../demux/avformat/avformat.c"
275 /*****************************************************************************
276 * OpenDecoder: probe the decoder and return score
277 *****************************************************************************/
278 static int OpenDecoder( vlc_object_t *p_this )
280 decoder_t *p_dec = (decoder_t*) p_this;
281 int i_cat, i_codec_id, i_result;
282 const char *psz_namecodec;
284 AVCodecContext *p_context = NULL;
285 AVCodec *p_codec = NULL;
287 /* *** determine codec type *** */
288 if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
294 /* Initialization must be done before avcodec_find_decoder() */
297 /* *** ask ffmpeg for a decoder *** */
298 char *psz_decoder = var_CreateGetString( p_this, "avcodec-codec" );
299 if( psz_decoder && *psz_decoder )
301 p_codec = avcodec_find_decoder_by_name( psz_decoder );
303 msg_Err( p_this, "Decoder `%s' not found", psz_decoder );
304 else if( p_codec->id != i_codec_id )
306 msg_Err( p_this, "Decoder `%s' can't handle %4.4s",
307 psz_decoder, (char*)&p_dec->fmt_in.i_codec );
313 p_codec = avcodec_find_decoder( i_codec_id );
316 msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
320 /* *** get a p_context *** */
321 #if LIBAVCODEC_VERSION_MAJOR >= 54
322 p_context = avcodec_alloc_context3(p_codec);
324 p_context = avcodec_alloc_context();
328 p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
329 p_context->opaque = (void *)p_this;
331 /* Set CPU capabilities */
332 p_context->dsp_mask = 0;
333 #if defined (__i386__) || defined (__x86_64__)
335 p_context->dsp_mask |= AV_CPU_FLAG_MMX;
336 if( !vlc_CPU_MMXEXT() )
337 p_context->dsp_mask |= AV_CPU_FLAG_MMX2;
338 if( !vlc_CPU_3dNOW() )
339 p_context->dsp_mask |= AV_CPU_FLAG_3DNOW;
341 p_context->dsp_mask |= AV_CPU_FLAG_SSE;
342 if( !vlc_CPU_SSE2() )
343 p_context->dsp_mask |= AV_CPU_FLAG_SSE2;
344 # ifdef AV_CPU_FLAG_SSE3
345 if( !vlc_CPU_SSE3() )
346 p_context->dsp_mask |= AV_CPU_FLAG_SSE3;
348 # ifdef AV_CPU_FLAG_SSSE3
349 if( !vlc_CPU_SSE3() )
350 p_context->dsp_mask |= AV_CPU_FLAG_SSSE3;
352 # ifdef AV_CPU_FLAG_SSE4
353 if( !vlc_CPU_SSE4_1() )
354 p_context->dsp_mask |= AV_CPU_FLAG_SSE4;
356 # ifdef AV_CPU_FLAG_SSE42
357 if( !vlc_CPU_SSE4_2() )
358 p_context->dsp_mask |= AV_CPU_FLAG_SSE42;
362 p_dec->b_need_packetized = true;
366 p_dec->pf_decode_video = DecodeVideo;
367 i_result = InitVideoDec ( p_dec, p_context, p_codec,
368 i_codec_id, psz_namecodec );
371 p_dec->pf_decode_audio = DecodeAudio;
372 i_result = InitAudioDec ( p_dec, p_context, p_codec,
373 i_codec_id, psz_namecodec );
376 p_dec->pf_decode_sub = DecodeSubtitle;
377 i_result = InitSubtitleDec( p_dec, p_context, p_codec,
378 i_codec_id, psz_namecodec );
381 i_result = VLC_EGENERIC;
384 if( i_result == VLC_SUCCESS )
386 p_dec->p_sys->i_cat = i_cat;
387 if( p_context->profile != FF_PROFILE_UNKNOWN)
388 p_dec->fmt_in.i_profile = p_context->profile;
389 if( p_context->level != FF_LEVEL_UNKNOWN)
390 p_dec->fmt_in.i_level = p_context->level;
396 /*****************************************************************************
397 * CloseDecoder: decoder destruction
398 *****************************************************************************/
399 static void CloseDecoder( vlc_object_t *p_this )
401 decoder_t *p_dec = (decoder_t *)p_this;
402 decoder_sys_t *p_sys = p_dec->p_sys;
404 switch( p_sys->i_cat )
407 EndAudioDec ( p_dec );
410 EndVideoDec ( p_dec );
413 EndSubtitleDec( p_dec );
417 if( p_sys->p_context )
419 free( p_sys->p_context->extradata );
420 p_sys->p_context->extradata = NULL;
422 if( !p_sys->b_delayed_open )
425 avcodec_close( p_sys->p_context );
426 vlc_avcodec_unlock();
428 msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
429 av_free( p_sys->p_context );
435 /*****************************************************************************
437 *****************************************************************************/
438 int ffmpeg_OpenCodec( decoder_t *p_dec )
440 decoder_sys_t *p_sys = p_dec->p_sys;
442 if( p_sys->p_context->extradata_size <= 0 )
444 if( p_sys->i_codec_id == CODEC_ID_VC1 ||
445 p_sys->i_codec_id == CODEC_ID_VORBIS ||
446 p_sys->i_codec_id == CODEC_ID_THEORA ||
447 ( p_sys->i_codec_id == CODEC_ID_AAC &&
448 !p_dec->fmt_in.b_packetized ) )
450 msg_Warn( p_dec, "waiting for extra data for codec %s",
451 p_sys->psz_namecodec );
455 if( p_dec->fmt_in.i_cat == VIDEO_ES )
457 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
458 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
459 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
461 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
463 p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
464 p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
466 p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
467 p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
468 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
469 if( p_sys->i_codec_id == CODEC_ID_ADPCM_G726 &&
470 p_sys->p_context->bit_rate > 0 &&
471 p_sys->p_context->sample_rate > 0)
472 p_sys->p_context->bits_per_coded_sample = p_sys->p_context->bit_rate /
473 p_sys->p_context->sample_rate;
477 #if LIBAVCODEC_VERSION_MAJOR >= 54
478 ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, NULL /* options */ );
480 ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
482 vlc_avcodec_unlock();
485 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
487 #ifdef HAVE_AVCODEC_MT
488 if( p_dec->fmt_in.i_cat == VIDEO_ES )
490 switch( p_sys->p_context->active_thread_type )
492 case FF_THREAD_FRAME:
493 msg_Dbg( p_dec, "using frame thread mode with %d threads",
494 p_sys->p_context->thread_count );
496 case FF_THREAD_SLICE:
497 msg_Dbg( p_dec, "using slice thread mode with %d threads",
498 p_sys->p_context->thread_count );
501 if( p_sys->p_context->thread_count > 1 )
502 msg_Warn( p_dec, "failed to enable threaded decoding" );
505 msg_Warn( p_dec, "using unknown thread mode with %d threads",
506 p_sys->p_context->thread_count );
512 p_sys->b_delayed_open = false;