]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/avcodec.c
Use <vlc_cpu.h>
[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( 51, 48, 0 )
52 #   error You must update libavcodec to a version >= 51.48.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 #endif
85
86 /*****************************************************************************
87  * Module descriptor
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")
92
93 vlc_module_begin ()
94     set_shortname( "FFmpeg")
95     add_shortcut( "ffmpeg" )
96     set_category( CAT_INPUT )
97     set_subcategory( SUBCAT_INPUT_SCODEC )
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 )
104 #else
105     set_description( N_("FFmpeg audio/video decoder") )
106     set_help( MODULE_DESCRIPTION )
107     set_capability( "decoder", 70 )
108 #endif
109     set_section( N_("Decoding") , NULL )
110     set_callbacks( OpenDecoder, CloseDecoder )
111
112
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,
117         false )
118     add_bool( "ffmpeg-hurry-up", true, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT,
119         false )
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,
127         true )
128     add_integer ( "ffmpeg-lowres", 0, NULL, LOWRES_TEXT, LOWRES_LONGTEXT,
129         true )
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 )
134         change_safe ()
135         change_integer_list( nloopf_list, nloopf_list_text, NULL )
136
137     add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT,
138                  true )
139 #ifdef HAVE_AVCODEC_VAAPI
140     add_bool( "ffmpeg-hw", true, NULL, HW_TEXT, HW_LONGTEXT, true )
141 #endif
142
143 #ifdef ENABLE_SOUT
144     /* encoder submodule */
145     add_submodule ()
146     add_shortcut( "ffmpeg" )
147     set_section( N_("Encoding") , NULL )
148     set_description( N_("FFmpeg audio/video encoder") )
149     set_capability( "encoder", 100 )
150     set_callbacks( OpenEncoder, CloseEncoder )
151
152     add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT,
153                 ENC_HQ_LONGTEXT, false )
154         change_string_list( enc_hq_list, enc_hq_list_text, 0 )
155     add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT,
156                  ENC_KEYINT_LONGTEXT, false )
157     add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT,
158                  ENC_BFRAMES_LONGTEXT, false )
159     add_bool( ENC_CFG_PREFIX "hurry-up", false, NULL, ENC_HURRYUP_TEXT,
160               ENC_HURRYUP_LONGTEXT, false )
161     add_bool( ENC_CFG_PREFIX "interlace", false, NULL, ENC_INTERLACE_TEXT,
162               ENC_INTERLACE_LONGTEXT, true )
163     add_bool( ENC_CFG_PREFIX "interlace-me", true, NULL, ENC_INTERLACE_ME_TEXT,
164               ENC_INTERLACE_ME_LONGTEXT, true )
165     add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT,
166                  ENC_VT_LONGTEXT, true )
167     add_bool( ENC_CFG_PREFIX "pre-me", false, NULL, ENC_PRE_ME_TEXT,
168               ENC_PRE_ME_LONGTEXT, true )
169     add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL,
170                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
171     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL,
172                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
173     add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL,
174                ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
175     add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL,
176                  ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
177     add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false, NULL,
178               ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
179     add_integer( ENC_CFG_PREFIX "qmin", 0, NULL,
180                  ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
181     add_integer( ENC_CFG_PREFIX "qmax", 0, NULL,
182                  ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
183     add_bool( ENC_CFG_PREFIX "trellis", false, NULL,
184               ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
185     add_float( ENC_CFG_PREFIX "qscale", 0, NULL,
186                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
187     add_integer( ENC_CFG_PREFIX "strict", 0, NULL,
188                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
189     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL,
190                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
191     add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL,
192                ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
193     add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL,
194                ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
195     add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL,
196                ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
197     add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL,
198                  ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
199     add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL,
200                  ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
201
202     /* Audio AAC encoder profile */
203     add_string( ENC_CFG_PREFIX "aac-profile", "low", NULL,
204                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
205 #endif /* ENABLE_SOUT */
206
207     /* video filter submodule */
208     add_submodule ()
209     set_capability( "video filter2", 0 )
210     set_callbacks( OpenDeinterlace, CloseDeinterlace )
211     set_description( N_("FFmpeg deinterlace video filter") )
212     add_shortcut( "ffmpeg-deinterlace" )
213
214 #ifdef MERGE_FFMPEG
215     add_submodule ()
216 #   include "../../demux/avformat/avformat.c"
217 #endif
218 vlc_module_end ()
219
220 /*****************************************************************************
221  * OpenDecoder: probe the decoder and return score
222  *****************************************************************************/
223 static int OpenDecoder( vlc_object_t *p_this )
224 {
225     decoder_t *p_dec = (decoder_t*) p_this;
226     int i_cat, i_codec_id, i_result;
227     const char *psz_namecodec;
228
229     AVCodecContext *p_context = NULL;
230     AVCodec        *p_codec = NULL;
231
232     /* *** determine codec type *** */
233     if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
234                              &psz_namecodec ) )
235     {
236         return VLC_EGENERIC;
237     }
238
239     /* Initialization must be done before avcodec_find_decoder() */
240     InitLibavcodec(p_this);
241
242     /* *** ask ffmpeg for a decoder *** */
243     p_codec = avcodec_find_decoder( i_codec_id );
244     if( !p_codec )
245     {
246         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
247         return VLC_EGENERIC;
248     }
249
250     /* *** get a p_context *** */
251     p_context = avcodec_alloc_context();
252     if( !p_context )
253         return VLC_ENOMEM;
254     p_context->debug = config_GetInt( p_dec, "ffmpeg-debug" );
255     p_context->opaque = (void *)p_this;
256
257     /* Set CPU capabilities */
258     unsigned i_cpu = vlc_CPU();
259     p_context->dsp_mask = 0;
260     if( !(i_cpu & CPU_CAPABILITY_MMX) )
261     {
262         p_context->dsp_mask |= FF_MM_MMX;
263     }
264     if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
265     {
266         p_context->dsp_mask |= FF_MM_MMXEXT;
267     }
268     if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
269     {
270         p_context->dsp_mask |= FF_MM_3DNOW;
271     }
272     if( !(i_cpu & CPU_CAPABILITY_SSE) )
273     {
274         p_context->dsp_mask |= FF_MM_SSE;
275     }
276     if( !(i_cpu & CPU_CAPABILITY_SSE2) )
277     {
278         p_context->dsp_mask |= FF_MM_SSE2;
279     }
280
281     p_dec->b_need_packetized = true;
282     switch( i_cat )
283     {
284     case VIDEO_ES:
285         p_dec->pf_decode_video = DecodeVideo;
286         i_result =  InitVideoDec ( p_dec, p_context, p_codec,
287                                        i_codec_id, psz_namecodec );
288         break;
289     case AUDIO_ES:
290         p_dec->pf_decode_audio = DecodeAudio;
291         i_result =  InitAudioDec ( p_dec, p_context, p_codec,
292                                        i_codec_id, psz_namecodec );
293         break;
294 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
295     case SPU_ES:
296         p_dec->pf_decode_sub = DecodeSubtitle;
297         i_result =  InitSubtitleDec( p_dec, p_context, p_codec,
298                                      i_codec_id, psz_namecodec );
299         break;
300 #endif
301     default:
302         i_result = VLC_EGENERIC;
303     }
304
305     if( i_result == VLC_SUCCESS ) p_dec->p_sys->i_cat = i_cat;
306
307     return i_result;
308 }
309
310 /*****************************************************************************
311  * CloseDecoder: decoder destruction
312  *****************************************************************************/
313 static void CloseDecoder( vlc_object_t *p_this )
314 {
315     decoder_t *p_dec = (decoder_t *)p_this;
316     decoder_sys_t *p_sys = p_dec->p_sys;
317
318     switch( p_sys->i_cat )
319     {
320     case AUDIO_ES:
321          EndAudioDec ( p_dec );
322         break;
323     case VIDEO_ES:
324          EndVideoDec ( p_dec );
325         break;
326 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
327     case SPU_ES:
328          EndSubtitleDec( p_dec );
329         break;
330 #endif
331     }
332
333     if( p_sys->p_context )
334     {
335         free( p_sys->p_context->extradata );
336         p_sys->p_context->extradata = NULL;
337
338         if( !p_sys->b_delayed_open )
339         {
340             vlc_avcodec_lock();
341             avcodec_close( p_sys->p_context );
342             vlc_avcodec_unlock();
343         }
344         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
345         av_free( p_sys->p_context );
346     }
347
348     free( p_sys );
349 }
350
351 void InitLibavcodec( vlc_object_t *p_object )
352 {
353     static bool b_ffmpeginit = false;
354
355     vlc_avcodec_lock();
356
357     /* *** init ffmpeg library (libavcodec) *** */
358     if( !b_ffmpeginit )
359     {
360         avcodec_init();
361         avcodec_register_all();
362         av_log_set_callback( LibavutilCallback );
363         b_ffmpeginit = true;
364
365         msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
366                  LIBAVCODEC_VERSION_INT );
367     }
368     else
369     {
370         msg_Dbg( p_object, "libavcodec already initialized" );
371     }
372
373     vlc_avcodec_unlock();
374 }