Qt: synchronization: subtitles duration parameter
[vlc/vlc-skelet.git] / modules / codec / avcodec / avcodec.c
blob0d985642a00ee4b68d638639ca5b4d6c232cfc93
1 /*****************************************************************************
2 * avcodec.c: video and audio decoder and encoder using libavcodec
3 *****************************************************************************
4 * Copyright (C) 1999-2008 the VideoLAN team
5 * $Id$
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 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
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>
38 /* ffmpeg header */
39 #define HAVE_MMX 1
40 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
41 # include <libavcodec/avcodec.h>
42 #else
43 # include <avcodec.h>
44 #endif
46 #include "avcodec.h"
47 #include "avutil.h"
48 #include "chroma.h"
50 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
51 # error You must update libavcodec to a version >= 52.25.0
52 #endif
54 /*****************************************************************************
55 * decoder_sys_t: decoder descriptor
56 *****************************************************************************/
57 struct decoder_sys_t
59 /* Common part between video and audio decoder */
60 AVCODEC_COMMON_MEMBERS
63 /****************************************************************************
64 * Local prototypes
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 #ifdef ENABLE_SOUT
74 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
75 static const char *const enc_hq_list_text[] = {
76 N_("rd"), N_("bits"), N_("simple") };
77 #endif
79 #ifdef MERGE_FFMPEG
80 # include "../../demux/avformat/avformat.h"
81 # include "../../access/avio.h"
82 #endif
84 /*****************************************************************************
85 * Module descriptor
86 *****************************************************************************/
87 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
88 "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
89 "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
91 vlc_module_begin ()
92 set_shortname( "FFmpeg")
93 add_shortcut( "ffmpeg" )
94 set_category( CAT_INPUT )
95 set_subcategory( SUBCAT_INPUT_VCODEC )
96 /* decoder main module */
97 set_description( N_("FFmpeg audio/video decoder") )
98 set_help( MODULE_DESCRIPTION )
99 set_capability( "decoder", 70 )
100 set_section( N_("Decoding") , NULL )
101 set_callbacks( OpenDecoder, CloseDecoder )
104 add_bool( "ffmpeg-dr", true, DR_TEXT, DR_TEXT, true )
105 add_integer ( "ffmpeg-error-resilience", 1, ERROR_TEXT,
106 ERROR_LONGTEXT, true )
107 add_integer ( "ffmpeg-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
108 false )
109 add_bool( "ffmpeg-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
110 false )
111 add_integer( "ffmpeg-skip-frame", 0, SKIP_FRAME_TEXT,
112 SKIP_FRAME_LONGTEXT, true )
113 change_integer_range( -1, 4 )
114 add_integer( "ffmpeg-skip-idct", 0, SKIP_IDCT_TEXT,
115 SKIP_IDCT_LONGTEXT, true )
116 change_integer_range( -1, 4 )
117 add_integer ( "ffmpeg-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
118 true )
119 add_integer ( "ffmpeg-lowres", 0, LOWRES_TEXT, LOWRES_LONGTEXT,
120 true )
121 change_integer_range( 0, 2 )
122 add_bool( "ffmpeg-fast", false, FAST_TEXT, FAST_LONGTEXT, false )
123 add_integer ( "ffmpeg-skiploopfilter", 0, SKIPLOOPF_TEXT,
124 SKIPLOOPF_LONGTEXT, false)
125 change_safe ()
126 change_integer_list( nloopf_list, nloopf_list_text )
128 add_integer( "ffmpeg-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
129 true )
130 #if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2)
131 add_bool( "ffmpeg-hw", false, HW_TEXT, HW_LONGTEXT, false )
132 #endif
133 #if defined(FF_THREAD_FRAME)
134 add_integer( "ffmpeg-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
135 #endif
138 #ifdef ENABLE_SOUT
139 /* encoder submodule */
140 add_submodule ()
141 add_shortcut( "ffmpeg" )
142 set_section( N_("Encoding") , NULL )
143 set_description( N_("FFmpeg audio/video encoder") )
144 set_capability( "encoder", 100 )
145 set_callbacks( OpenEncoder, CloseEncoder )
147 add_string( ENC_CFG_PREFIX "hq", "simple", ENC_HQ_TEXT,
148 ENC_HQ_LONGTEXT, false )
149 change_string_list( enc_hq_list, enc_hq_list_text, 0 )
150 add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
151 ENC_KEYINT_LONGTEXT, false )
152 add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
153 ENC_BFRAMES_LONGTEXT, false )
154 add_bool( ENC_CFG_PREFIX "hurry-up", false, ENC_HURRYUP_TEXT,
155 ENC_HURRYUP_LONGTEXT, false )
156 add_bool( ENC_CFG_PREFIX "interlace", false, ENC_INTERLACE_TEXT,
157 ENC_INTERLACE_LONGTEXT, true )
158 add_bool( ENC_CFG_PREFIX "interlace-me", true, ENC_INTERLACE_ME_TEXT,
159 ENC_INTERLACE_ME_LONGTEXT, true )
160 add_integer( ENC_CFG_PREFIX "vt", 0, ENC_VT_TEXT,
161 ENC_VT_LONGTEXT, true )
162 add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
163 ENC_PRE_ME_LONGTEXT, true )
164 add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8,
165 ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
166 add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
167 ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
168 add_float( ENC_CFG_PREFIX "i-quant-factor", 0,
169 ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
170 add_integer( ENC_CFG_PREFIX "noise-reduction", 0,
171 ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
172 add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false,
173 ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
174 add_integer( ENC_CFG_PREFIX "qmin", 0,
175 ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
176 add_integer( ENC_CFG_PREFIX "qmax", 0,
177 ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
178 add_bool( ENC_CFG_PREFIX "trellis", false,
179 ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
180 add_float( ENC_CFG_PREFIX "qscale", 0,
181 ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
182 add_integer( ENC_CFG_PREFIX "strict", 0,
183 ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
184 add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
185 ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
186 add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
187 ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
188 add_float( ENC_CFG_PREFIX "p-masking", 0.0,
189 ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
190 add_float( ENC_CFG_PREFIX "border-masking", 0.0,
191 ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
192 add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0,
193 ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
194 add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0,
195 ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
197 /* Audio AAC encoder profile */
198 add_string( ENC_CFG_PREFIX "aac-profile", "low",
199 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
200 #endif /* ENABLE_SOUT */
202 /* video filter submodule */
203 add_submodule ()
204 set_capability( "video filter2", 0 )
205 set_callbacks( OpenDeinterlace, CloseDeinterlace )
206 set_description( N_("FFmpeg deinterlace video filter") )
207 add_shortcut( "ffmpeg-deinterlace" )
209 #ifdef MERGE_FFMPEG
210 add_submodule ()
211 # include "../../demux/avformat/avformat.c"
212 add_submodule ()
213 AVIO_MODULE
214 #endif
215 vlc_module_end ()
217 /*****************************************************************************
218 * OpenDecoder: probe the decoder and return score
219 *****************************************************************************/
220 static int OpenDecoder( vlc_object_t *p_this )
222 decoder_t *p_dec = (decoder_t*) p_this;
223 int i_cat, i_codec_id, i_result;
224 const char *psz_namecodec;
226 AVCodecContext *p_context = NULL;
227 AVCodec *p_codec = NULL;
229 /* *** determine codec type *** */
230 if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
231 &psz_namecodec ) )
233 return VLC_EGENERIC;
236 /* Initialization must be done before avcodec_find_decoder() */
237 InitLibavcodec(p_this);
239 /* *** ask ffmpeg for a decoder *** */
240 p_codec = avcodec_find_decoder( i_codec_id );
241 if( !p_codec )
243 msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
244 return VLC_EGENERIC;
247 /* *** get a p_context *** */
248 p_context = avcodec_alloc_context();
249 if( !p_context )
250 return VLC_ENOMEM;
251 p_context->debug = var_InheritInteger( p_dec, "ffmpeg-debug" );
252 p_context->opaque = (void *)p_this;
254 /* Set CPU capabilities */
255 unsigned i_cpu = vlc_CPU();
256 p_context->dsp_mask = 0;
257 if( !(i_cpu & CPU_CAPABILITY_MMX) )
259 p_context->dsp_mask |= AV_CPU_FLAG_MMX;
261 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
263 p_context->dsp_mask |= AV_CPU_FLAG_MMX2;
265 if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
267 p_context->dsp_mask |= AV_CPU_FLAG_3DNOW;
269 if( !(i_cpu & CPU_CAPABILITY_SSE) )
271 p_context->dsp_mask |= AV_CPU_FLAG_SSE;
273 if( !(i_cpu & CPU_CAPABILITY_SSE2) )
275 p_context->dsp_mask |= AV_CPU_FLAG_SSE2;
277 #ifdef AV_CPU_FLAG_SSE3
278 if( !(i_cpu & CPU_CAPABILITY_SSE3) )
279 p_context->dsp_mask |= AV_CPU_FLAG_SSE3;
280 #endif
281 #ifdef AV_CPU_FLAG_SSSE3
282 if( !(i_cpu & CPU_CAPABILITY_SSSE3) )
283 p_context->dsp_mask |= AV_CPU_FLAG_SSSE3;
284 #endif
285 #ifdef AV_CPU_FLAG_SSE4
286 if( !(i_cpu & CPU_CAPABILITY_SSE4_1) )
287 p_context->dsp_mask |= AV_CPU_FLAG_SSE4;
288 #endif
289 #ifdef AV_CPU_FLAG_SSE42
290 if( !(i_cpu & CPU_CAPABILITY_SSE4_2) )
291 p_context->dsp_mask |= AV_CPU_FLAG_SSE42;
292 #endif
294 p_dec->b_need_packetized = true;
295 switch( i_cat )
297 case VIDEO_ES:
298 p_dec->pf_decode_video = DecodeVideo;
299 i_result = InitVideoDec ( p_dec, p_context, p_codec,
300 i_codec_id, psz_namecodec );
301 break;
302 case AUDIO_ES:
303 p_dec->pf_decode_audio = DecodeAudio;
304 i_result = InitAudioDec ( p_dec, p_context, p_codec,
305 i_codec_id, psz_namecodec );
306 break;
307 case SPU_ES:
308 p_dec->pf_decode_sub = DecodeSubtitle;
309 i_result = InitSubtitleDec( p_dec, p_context, p_codec,
310 i_codec_id, psz_namecodec );
311 break;
312 default:
313 i_result = VLC_EGENERIC;
316 if( i_result == VLC_SUCCESS )
318 p_dec->p_sys->i_cat = i_cat;
319 if( p_context->profile != FF_PROFILE_UNKNOWN)
320 p_dec->fmt_in.i_profile = p_context->profile;
321 if( p_context->level != FF_LEVEL_UNKNOWN)
322 p_dec->fmt_in.i_level = p_context->level;
325 return i_result;
328 /*****************************************************************************
329 * CloseDecoder: decoder destruction
330 *****************************************************************************/
331 static void CloseDecoder( vlc_object_t *p_this )
333 decoder_t *p_dec = (decoder_t *)p_this;
334 decoder_sys_t *p_sys = p_dec->p_sys;
336 switch( p_sys->i_cat )
338 case AUDIO_ES:
339 EndAudioDec ( p_dec );
340 break;
341 case VIDEO_ES:
342 EndVideoDec ( p_dec );
343 break;
344 case SPU_ES:
345 EndSubtitleDec( p_dec );
346 break;
349 if( p_sys->p_context )
351 free( p_sys->p_context->extradata );
352 p_sys->p_context->extradata = NULL;
354 if( !p_sys->b_delayed_open )
356 vlc_avcodec_lock();
357 avcodec_close( p_sys->p_context );
358 vlc_avcodec_unlock();
360 msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
361 av_free( p_sys->p_context );
364 free( p_sys );
367 void InitLibavcodec( vlc_object_t *p_object )
369 static bool b_ffmpeginit = false;
371 vlc_avcodec_lock();
373 /* *** init ffmpeg library (libavcodec) *** */
374 if( !b_ffmpeginit )
376 avcodec_init();
377 avcodec_register_all();
378 av_log_set_callback( LibavutilCallback );
379 b_ffmpeginit = true;
381 msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
382 LIBAVCODEC_VERSION_INT );
384 else
386 msg_Dbg( p_object, "libavcodec already initialized" );
389 vlc_avcodec_unlock();
392 /*****************************************************************************
393 * ffmpeg_OpenCodec:
394 *****************************************************************************/
395 int ffmpeg_OpenCodec( decoder_t *p_dec )
397 decoder_sys_t *p_sys = p_dec->p_sys;
399 if( p_sys->p_context->extradata_size <= 0 )
401 if( p_sys->i_codec_id == CODEC_ID_VC1 ||
402 p_sys->i_codec_id == CODEC_ID_VORBIS ||
403 p_sys->i_codec_id == CODEC_ID_THEORA ||
404 p_sys->i_codec_id == CODEC_ID_AAC )
406 msg_Warn( p_dec, "waiting for extra data for codec %s",
407 p_sys->psz_namecodec );
408 return 1;
411 if( p_dec->fmt_in.i_cat == VIDEO_ES )
413 p_sys->p_context->width = p_dec->fmt_in.video.i_width;
414 p_sys->p_context->height = p_dec->fmt_in.video.i_height;
415 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
417 else if( p_dec->fmt_in.i_cat == AUDIO_ES )
419 p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
420 p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
422 p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
423 p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
424 p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
426 int ret;
427 vlc_avcodec_lock();
428 ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
429 vlc_avcodec_unlock();
430 if( ret < 0 )
431 return VLC_EGENERIC;
432 msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
434 #ifdef HAVE_AVCODEC_MT
435 if( p_dec->fmt_in.i_cat == VIDEO_ES )
437 switch( p_sys->p_context->active_thread_type )
439 case FF_THREAD_FRAME:
440 msg_Dbg( p_dec, "using frame thread mode with %d threads",
441 p_sys->p_context->thread_count );
442 break;
443 case FF_THREAD_SLICE:
444 msg_Dbg( p_dec, "using slice thread mode with %d threads",
445 p_sys->p_context->thread_count );
446 break;
447 case 0:
448 if( p_sys->p_context->thread_count > 1 )
449 msg_Warn( p_dec, "failed to enable threaded decoding" );
450 break;
451 default:
452 msg_Warn( p_dec, "using unknown thread mode with %d threads",
453 p_sys->p_context->thread_count );
454 break;
457 #endif
459 p_sys->b_delayed_open = false;
461 return VLC_SUCCESS;