]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/avcodec.c
Used VA_INVALID_ID when appropriate.
[vlc] / modules / codec / avcodec / avcodec.c
1 /*****************************************************************************
2  * avcodec.c: video and audio decoder and encoder using libavcodec
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
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>
37
38 /* ffmpeg header */
39 #define HAVE_MMX 1
40 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
41 #   include <libavcodec/avcodec.h>
42 #elif defined(HAVE_FFMPEG_AVCODEC_H)
43 #   include <ffmpeg/avcodec.h>
44 #else
45 #   include <avcodec.h>
46 #endif
47
48 #include "avcodec.h"
49 #include "avutil.h"
50
51 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 2, 0 )
52 #   error You must update libavcodec to a version >= 52.2.0
53 #elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
54 #   warning You should update libavcodec to get subtitle support
55 #endif
56
57 /*****************************************************************************
58  * decoder_sys_t: decoder descriptor
59  *****************************************************************************/
60 struct decoder_sys_t
61 {
62     /* Common part between video and audio decoder */
63     FFMPEG_COMMON_MEMBERS
64 };
65
66 /****************************************************************************
67  * Local prototypes
68  ****************************************************************************/
69 static int OpenDecoder( vlc_object_t * );
70 static void CloseDecoder( vlc_object_t * );
71
72 static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
73 static const char *const nloopf_list_text[] =
74   { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
75
76 #ifdef ENABLE_SOUT
77 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
78 static const char *const enc_hq_list_text[] = {
79     N_("rd"), N_("bits"), N_("simple") };
80 #endif
81
82 #ifdef MERGE_FFMPEG
83 # include "../../demux/avformat/avformat.h"
84 # include "../../access/avio.h"
85 #endif
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
91         "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
92         "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
93
94 vlc_module_begin ()
95     set_shortname( "FFmpeg")
96     add_shortcut( "ffmpeg" )
97     set_category( CAT_INPUT )
98     set_subcategory( SUBCAT_INPUT_VCODEC )
99     /* decoder main module */
100 #if defined(MODULE_NAME_is_ffmpegaltivec) \
101      || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
102     set_description( N_("AltiVec FFmpeg audio/video decoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") )
103     /*add_requirement( ALTIVEC )*/
104     set_capability( "decoder", 71 )
105 #else
106     set_description( N_("FFmpeg audio/video decoder") )
107     set_help( MODULE_DESCRIPTION )
108     set_capability( "decoder", 70 )
109 #endif
110     set_section( N_("Decoding") , NULL )
111     set_callbacks( OpenDecoder, CloseDecoder )
112
113
114     add_bool( "ffmpeg-dr", true, NULL, DR_TEXT, DR_TEXT, true )
115     add_integer ( "ffmpeg-error-resilience", 1, NULL, ERROR_TEXT,
116         ERROR_LONGTEXT, true )
117     add_integer ( "ffmpeg-workaround-bugs", 1, NULL, BUGS_TEXT, BUGS_LONGTEXT,
118         false )
119     add_bool( "ffmpeg-hurry-up", true, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT,
120         false )
121     add_integer( "ffmpeg-skip-frame", 0, NULL, SKIP_FRAME_TEXT,
122         SKIP_FRAME_LONGTEXT, true )
123         change_integer_range( -1, 4 )
124     add_integer( "ffmpeg-skip-idct", 0, NULL, SKIP_IDCT_TEXT,
125         SKIP_IDCT_LONGTEXT, true )
126         change_integer_range( -1, 4 )
127     add_integer ( "ffmpeg-vismv", 0, NULL, VISMV_TEXT, VISMV_LONGTEXT,
128         true )
129     add_integer ( "ffmpeg-lowres", 0, NULL, LOWRES_TEXT, LOWRES_LONGTEXT,
130         true )
131         change_integer_range( 0, 2 )
132     add_bool( "ffmpeg-fast", false, NULL, FAST_TEXT, FAST_LONGTEXT, true )
133     add_integer ( "ffmpeg-skiploopfilter", 0, NULL, SKIPLOOPF_TEXT,
134                   SKIPLOOPF_LONGTEXT, true )
135         change_safe ()
136         change_integer_list( nloopf_list, nloopf_list_text, NULL )
137
138     add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT,
139                  true )
140 #if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2)
141     add_bool( "ffmpeg-hw", false, NULL, HW_TEXT, HW_LONGTEXT, true )
142 #endif
143 #if defined(FF_THREAD_FRAME)
144     add_integer( "ffmpeg-threads", 0, NULL, THREADS_TEXT, THREADS_LONGTEXT, true );
145 #endif
146
147
148 #ifdef ENABLE_SOUT
149     /* encoder submodule */
150     add_submodule ()
151     add_shortcut( "ffmpeg" )
152     set_section( N_("Encoding") , NULL )
153     set_description( N_("FFmpeg audio/video encoder") )
154     set_capability( "encoder", 100 )
155     set_callbacks( OpenEncoder, CloseEncoder )
156
157     add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT,
158                 ENC_HQ_LONGTEXT, false )
159         change_string_list( enc_hq_list, enc_hq_list_text, 0 )
160     add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT,
161                  ENC_KEYINT_LONGTEXT, false )
162     add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT,
163                  ENC_BFRAMES_LONGTEXT, false )
164     add_bool( ENC_CFG_PREFIX "hurry-up", false, NULL, ENC_HURRYUP_TEXT,
165               ENC_HURRYUP_LONGTEXT, false )
166     add_bool( ENC_CFG_PREFIX "interlace", false, NULL, ENC_INTERLACE_TEXT,
167               ENC_INTERLACE_LONGTEXT, true )
168     add_bool( ENC_CFG_PREFIX "interlace-me", true, NULL, ENC_INTERLACE_ME_TEXT,
169               ENC_INTERLACE_ME_LONGTEXT, true )
170     add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT,
171                  ENC_VT_LONGTEXT, true )
172     add_bool( ENC_CFG_PREFIX "pre-me", false, NULL, ENC_PRE_ME_TEXT,
173               ENC_PRE_ME_LONGTEXT, true )
174     add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL,
175                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
176     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL,
177                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
178     add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL,
179                ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
180     add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL,
181                  ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
182     add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false, NULL,
183               ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
184     add_integer( ENC_CFG_PREFIX "qmin", 0, NULL,
185                  ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
186     add_integer( ENC_CFG_PREFIX "qmax", 0, NULL,
187                  ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
188     add_bool( ENC_CFG_PREFIX "trellis", false, NULL,
189               ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
190     add_float( ENC_CFG_PREFIX "qscale", 0, NULL,
191                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
192     add_integer( ENC_CFG_PREFIX "strict", 0, NULL,
193                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
194     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL,
195                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
196     add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL,
197                ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
198     add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL,
199                ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
200     add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL,
201                ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
202     add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL,
203                  ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
204     add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL,
205                  ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
206
207     /* Audio AAC encoder profile */
208     add_string( ENC_CFG_PREFIX "aac-profile", "low", NULL,
209                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
210 #endif /* ENABLE_SOUT */
211
212     /* video filter submodule */
213     add_submodule ()
214     set_capability( "video filter2", 0 )
215     set_callbacks( OpenDeinterlace, CloseDeinterlace )
216     set_description( N_("FFmpeg deinterlace video filter") )
217     add_shortcut( "ffmpeg-deinterlace" )
218
219 #ifdef MERGE_FFMPEG
220     add_submodule ()
221 #   include "../../demux/avformat/avformat.c"
222     add_submodule ()
223         AVIO_MODULE
224 #endif
225 vlc_module_end ()
226
227 /*****************************************************************************
228  * OpenDecoder: probe the decoder and return score
229  *****************************************************************************/
230 static int OpenDecoder( vlc_object_t *p_this )
231 {
232     decoder_t *p_dec = (decoder_t*) p_this;
233     int i_cat, i_codec_id, i_result;
234     const char *psz_namecodec;
235
236     AVCodecContext *p_context = NULL;
237     AVCodec        *p_codec = NULL;
238
239     /* *** determine codec type *** */
240     if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
241                              &psz_namecodec ) )
242     {
243         return VLC_EGENERIC;
244     }
245
246     /* Initialization must be done before avcodec_find_decoder() */
247     InitLibavcodec(p_this);
248
249     /* *** ask ffmpeg for a decoder *** */
250     p_codec = avcodec_find_decoder( i_codec_id );
251     if( !p_codec )
252     {
253         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
254         return VLC_EGENERIC;
255     }
256
257     /* *** get a p_context *** */
258     p_context = avcodec_alloc_context();
259     if( !p_context )
260         return VLC_ENOMEM;
261     p_context->debug = var_InheritInteger( p_dec, "ffmpeg-debug" );
262     p_context->opaque = (void *)p_this;
263
264     /* Set CPU capabilities */
265     unsigned i_cpu = vlc_CPU();
266     p_context->dsp_mask = 0;
267     if( !(i_cpu & CPU_CAPABILITY_MMX) )
268     {
269         p_context->dsp_mask |= FF_MM_MMX;
270     }
271     if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
272     {
273         p_context->dsp_mask |= FF_MM_MMXEXT;
274     }
275     if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
276     {
277         p_context->dsp_mask |= FF_MM_3DNOW;
278     }
279     if( !(i_cpu & CPU_CAPABILITY_SSE) )
280     {
281         p_context->dsp_mask |= FF_MM_SSE;
282     }
283     if( !(i_cpu & CPU_CAPABILITY_SSE2) )
284     {
285         p_context->dsp_mask |= FF_MM_SSE2;
286     }
287 #ifdef FF_MM_SSE3
288     if( !(i_cpu & CPU_CAPABILITY_SSE3) )
289         p_context->dsp_mask |= FF_MM_SSE3;
290 #endif
291 #ifdef FF_MM_SSSE3
292     if( !(i_cpu & CPU_CAPABILITY_SSSE3) )
293         p_context->dsp_mask |= FF_MM_SSSE3;
294 #endif
295 #ifdef FF_MM_SSE4
296     if( !(i_cpu & CPU_CAPABILITY_SSE4_1) )
297         p_context->dsp_mask |= FF_MM_SSE4;
298 #endif
299 #ifdef FF_MM_SSE42
300     if( !(i_cpu & CPU_CAPABILITY_SSE4_2) )
301         p_context->dsp_mask |= FF_MM_SSE42;
302 #endif
303
304     p_dec->b_need_packetized = true;
305     switch( i_cat )
306     {
307     case VIDEO_ES:
308         p_dec->pf_decode_video = DecodeVideo;
309         i_result =  InitVideoDec ( p_dec, p_context, p_codec,
310                                        i_codec_id, psz_namecodec );
311         break;
312     case AUDIO_ES:
313         p_dec->pf_decode_audio = DecodeAudio;
314         i_result =  InitAudioDec ( p_dec, p_context, p_codec,
315                                        i_codec_id, psz_namecodec );
316         break;
317 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
318     case SPU_ES:
319         p_dec->pf_decode_sub = DecodeSubtitle;
320         i_result =  InitSubtitleDec( p_dec, p_context, p_codec,
321                                      i_codec_id, psz_namecodec );
322         break;
323 #endif
324     default:
325         i_result = VLC_EGENERIC;
326     }
327
328     if( i_result == VLC_SUCCESS )
329     {
330         p_dec->p_sys->i_cat = i_cat;
331         if( p_context->profile != FF_PROFILE_UNKNOWN)
332             p_dec->fmt_in.i_profile = p_context->profile;
333         if( p_context->level != FF_LEVEL_UNKNOWN)
334             p_dec->fmt_in.i_level = p_context->level;
335     }
336
337     return i_result;
338 }
339
340 /*****************************************************************************
341  * CloseDecoder: decoder destruction
342  *****************************************************************************/
343 static void CloseDecoder( vlc_object_t *p_this )
344 {
345     decoder_t *p_dec = (decoder_t *)p_this;
346     decoder_sys_t *p_sys = p_dec->p_sys;
347
348     switch( p_sys->i_cat )
349     {
350     case AUDIO_ES:
351          EndAudioDec ( p_dec );
352         break;
353     case VIDEO_ES:
354          EndVideoDec ( p_dec );
355         break;
356 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
357     case SPU_ES:
358          EndSubtitleDec( p_dec );
359         break;
360 #endif
361     }
362
363     if( p_sys->p_context )
364     {
365         free( p_sys->p_context->extradata );
366         p_sys->p_context->extradata = NULL;
367
368         if( !p_sys->b_delayed_open )
369         {
370             vlc_avcodec_lock();
371             avcodec_close( p_sys->p_context );
372             vlc_avcodec_unlock();
373         }
374         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
375         av_free( p_sys->p_context );
376     }
377
378     free( p_sys );
379 }
380
381 void InitLibavcodec( vlc_object_t *p_object )
382 {
383     static bool b_ffmpeginit = false;
384
385     vlc_avcodec_lock();
386
387     /* *** init ffmpeg library (libavcodec) *** */
388     if( !b_ffmpeginit )
389     {
390         avcodec_init();
391         avcodec_register_all();
392         av_log_set_callback( LibavutilCallback );
393         b_ffmpeginit = true;
394
395         msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
396                  LIBAVCODEC_VERSION_INT );
397     }
398     else
399     {
400         msg_Dbg( p_object, "libavcodec already initialized" );
401     }
402
403     vlc_avcodec_unlock();
404 }