]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/avcodec.c
d403202bb6d2a730eaf2cdd590d95c8ae5d3f1ba
[vlc] / modules / codec / avcodec / avcodec.c
1 /*****************************************************************************
2  * avcodec.c: video and audio decoder and encoder using libavcodec
3  *****************************************************************************
4  * Copyright (C) 1999-2008 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 #define HAVE_MMX 1
39 #include <libavcodec/avcodec.h>
40
41 #include "avcodec.h"
42 #include "chroma.h"
43 #include "avcommon.h"
44
45 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 53, 34, 0 )
46 #   error You must update libavcodec to a version >= 53.34.0
47 #elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 54, 25, 0 )
48 #   warning You should update libavcodec to a version >= 54.25.0
49 #endif
50
51 /*****************************************************************************
52  * decoder_sys_t: decoder descriptor
53  *****************************************************************************/
54 struct decoder_sys_t
55 {
56     /* Common part between video and audio decoder */
57     AVCODEC_COMMON_MEMBERS
58 };
59
60 /****************************************************************************
61  * Local prototypes
62  ****************************************************************************/
63 static int OpenDecoder( vlc_object_t * );
64 static void CloseDecoder( vlc_object_t * );
65
66 static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
67 static const char *const nloopf_list_text[] =
68   { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
69
70 #ifdef ENABLE_SOUT
71 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
72 static const char *const enc_hq_list_text[] = {
73     N_("rd"), N_("bits"), N_("simple") };
74 #endif
75
76 #ifdef MERGE_FFMPEG
77 # include "../../demux/avformat/avformat.h"
78 # include "../../access/avio.h"
79 #endif
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 #define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders " \
85         "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
86         "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")
87
88 vlc_module_begin ()
89     set_shortname( "FFmpeg")
90     add_shortcut( "ffmpeg" )
91     set_category( CAT_INPUT )
92     set_subcategory( SUBCAT_INPUT_VCODEC )
93     /* decoder main module */
94     set_description( N_("FFmpeg audio/video decoder") )
95     set_help( MODULE_DESCRIPTION )
96     set_capability( "decoder", 70 )
97     set_section( N_("Decoding") , NULL )
98     set_callbacks( OpenDecoder, CloseDecoder )
99
100
101     add_obsolete_bool( "ffmpeg-dr" ) /* removed since 2.1.0 */
102     add_bool( "avcodec-dr", true, DR_TEXT, DR_TEXT, true )
103     add_obsolete_integer ( "ffmpeg-error-resilience" ) /* removed since 2.1.0 */
104     add_integer ( "avcodec-error-resilience", 1, ERROR_TEXT,
105         ERROR_LONGTEXT, true )
106     add_obsolete_integer ( "ffmpeg-workaround-bugs" ) /* removed since 2.1.0 */
107     add_integer ( "avcodec-workaround-bugs", 1, BUGS_TEXT, BUGS_LONGTEXT,
108         false )
109     add_obsolete_bool( "ffmpeg-hurry-up" ) /* removed since 2.1.0 */
110     add_bool( "avcodec-hurry-up", true, HURRYUP_TEXT, HURRYUP_LONGTEXT,
111         false )
112     add_obsolete_integer( "ffmpeg-skip-frame") /* removed since 2.1.0 */
113     add_integer( "avcodec-skip-frame", 0, SKIP_FRAME_TEXT,
114         SKIP_FRAME_LONGTEXT, true )
115         change_integer_range( -1, 4 )
116     add_obsolete_integer( "ffmpeg-skip-idct" ) /* removed since 2.1.0 */
117     add_integer( "avcodec-skip-idct", 0, SKIP_IDCT_TEXT,
118         SKIP_IDCT_LONGTEXT, true )
119         change_integer_range( -1, 4 )
120     add_obsolete_integer ( "ffmpeg-vismv" ) /* removed since 2.1.0 */
121     add_integer ( "avcodec-vismv", 0, VISMV_TEXT, VISMV_LONGTEXT,
122         true )
123     add_obsolete_integer ( "ffmpeg-lowres" ) /* removed since 2.1.0 */
124     add_obsolete_bool( "ffmpeg-fast" ) /* removed since 2.1.0 */
125     add_bool( "avcodec-fast", false, FAST_TEXT, FAST_LONGTEXT, false )
126     add_obsolete_integer ( "ffmpeg-skiploopfilter" ) /* removed since 2.1.0 */
127     add_integer ( "avcodec-skiploopfilter", 0, SKIPLOOPF_TEXT,
128                   SKIPLOOPF_LONGTEXT, false)
129         change_safe ()
130         change_integer_list( nloopf_list, nloopf_list_text )
131
132     add_obsolete_integer( "ffmpeg-debug" ) /* removed since 2.1.0 */
133     add_integer( "avcodec-debug", 0, DEBUG_TEXT, DEBUG_LONGTEXT,
134                  true )
135     add_obsolete_string( "ffmpeg-codec" ) /* removed since 2.1.0 */
136     add_string( "avcodec-codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
137     add_obsolete_bool( "ffmpeg-hw" ) /* removed since 2.1.0 */
138     add_module( "avcodec-hw", "hw decoder", NULL, HW_TEXT, HW_LONGTEXT, false )
139 #if defined(FF_THREAD_FRAME)
140     add_obsolete_integer( "ffmpeg-threads" ) /* removed since 2.1.0 */
141     add_integer( "avcodec-threads", 0, THREADS_TEXT, THREADS_LONGTEXT, true );
142 #endif
143     add_string( "avcodec-options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
144
145
146 #ifdef ENABLE_SOUT
147     /* encoder submodule */
148     add_submodule ()
149     add_shortcut( "ffmpeg" )
150     set_section( N_("Encoding") , NULL )
151     set_description( N_("FFmpeg audio/video encoder") )
152     set_capability( "encoder", 100 )
153     set_callbacks( OpenEncoder, CloseEncoder )
154
155     /* removed in 2.1.0 */
156     add_obsolete_string( "sout-ffmpeg-codec" )
157     add_obsolete_string( "sout-ffmpeg-hq" )
158     add_obsolete_integer( "sout-ffmpeg-keyint" )
159     add_obsolete_integer( "sout-ffmpeg-bframes" )
160     add_obsolete_bool( "sout-ffmpeg-hurry-up" )
161     add_obsolete_bool( "sout-ffmpeg-interlace" )
162     add_obsolete_bool( "sout-ffmpeg-interlace-me" )
163     add_obsolete_integer( "sout-ffmpeg-vt" )
164     add_obsolete_bool( "sout-ffmpeg-pre-me" )
165     add_obsolete_integer( "sout-ffmpeg-rc-buffer-size" )
166     add_obsolete_float( "sout-ffmpeg-rc-buffer-aggressivity" )
167     add_obsolete_float( "sout-ffmpeg-i-quant-factor" )
168     add_obsolete_integer( "sout-ffmpeg-noise-reduction" )
169     add_obsolete_bool( "sout-ffmpeg-mpeg4-matrix" )
170     add_obsolete_integer( "sout-ffmpeg-qmin" )
171     add_obsolete_integer( "sout-ffmpeg-qmax" )
172     add_obsolete_bool( "sout-ffmpeg-trellis" )
173     add_obsolete_float( "sout-ffmpeg-qscale" )
174     add_obsolete_integer( "sout-ffmpeg-strict" )
175     add_obsolete_float( "sout-ffmpeg-lumi-masking" )
176     add_obsolete_float( "sout-ffmpeg-dark-masking" )
177     add_obsolete_float( "sout-ffmpeg-p-masking" )
178     add_obsolete_float( "sout-ffmpeg-border-masking" )
179     add_obsolete_integer( "sout-ffmpeg-luma-elim-threshold" )
180     add_obsolete_integer( "sout-ffmpeg-chroma-elim-threshold" )
181     add_obsolete_string( "sout-ffmpeg-aac-profile" )
182
183
184     add_string( ENC_CFG_PREFIX "codec", NULL, CODEC_TEXT, CODEC_LONGTEXT, true )
185     add_string( ENC_CFG_PREFIX "hq", "rd", ENC_HQ_TEXT,
186                 ENC_HQ_LONGTEXT, false )
187         change_string_list( enc_hq_list, enc_hq_list_text )
188     add_integer( ENC_CFG_PREFIX "keyint", 0, ENC_KEYINT_TEXT,
189                  ENC_KEYINT_LONGTEXT, false )
190     add_integer( ENC_CFG_PREFIX "bframes", 0, ENC_BFRAMES_TEXT,
191                  ENC_BFRAMES_LONGTEXT, false )
192     add_bool( ENC_CFG_PREFIX "hurry-up", false, ENC_HURRYUP_TEXT,
193               ENC_HURRYUP_LONGTEXT, false )
194     add_bool( ENC_CFG_PREFIX "interlace", false, ENC_INTERLACE_TEXT,
195               ENC_INTERLACE_LONGTEXT, true )
196     add_bool( ENC_CFG_PREFIX "interlace-me", true, ENC_INTERLACE_ME_TEXT,
197               ENC_INTERLACE_ME_LONGTEXT, true )
198     add_integer( ENC_CFG_PREFIX "vt", 0, ENC_VT_TEXT,
199                  ENC_VT_LONGTEXT, true )
200     add_bool( ENC_CFG_PREFIX "pre-me", false, ENC_PRE_ME_TEXT,
201               ENC_PRE_ME_LONGTEXT, true )
202     add_integer( ENC_CFG_PREFIX "rc-buffer-size", 0,
203                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
204     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0,
205                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
206     add_float( ENC_CFG_PREFIX "i-quant-factor", 0,
207                ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
208     add_integer( ENC_CFG_PREFIX "noise-reduction", 0,
209                  ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
210     add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false,
211               ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
212     add_integer( ENC_CFG_PREFIX "qmin", 0,
213                  ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
214     add_integer( ENC_CFG_PREFIX "qmax", 0,
215                  ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
216     add_bool( ENC_CFG_PREFIX "trellis", false,
217               ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
218     add_float( ENC_CFG_PREFIX "qscale", 0,
219                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
220     add_integer( ENC_CFG_PREFIX "strict", 0,
221                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
222         change_integer_range( -2, 2 )
223     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0,
224                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
225     add_float( ENC_CFG_PREFIX "dark-masking", 0.0,
226                ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
227     add_float( ENC_CFG_PREFIX "p-masking", 0.0,
228                ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
229     add_float( ENC_CFG_PREFIX "border-masking", 0.0,
230                ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
231     add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0,
232                  ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
233     add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0,
234                  ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
235
236     /* Audio AAC encoder profile */
237     add_string( ENC_CFG_PREFIX "aac-profile", "low",
238                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
239
240     add_string( ENC_CFG_PREFIX "options", NULL, AV_OPTIONS_TEXT, AV_OPTIONS_LONGTEXT, true )
241 #endif /* ENABLE_SOUT */
242
243 #ifdef MERGE_FFMPEG
244     add_submodule ()
245 #   include "../../demux/avformat/avformat.c"
246     add_submodule ()
247         AVIO_MODULE
248 #endif
249 vlc_module_end ()
250
251 /*****************************************************************************
252  * OpenDecoder: probe the decoder and return score
253  *****************************************************************************/
254 static int OpenDecoder( vlc_object_t *p_this )
255 {
256     decoder_t *p_dec = (decoder_t*) p_this;
257     unsigned i_codec_id;
258     int i_cat, i_result;
259     const char *psz_namecodec;
260
261     AVCodecContext *p_context = NULL;
262     AVCodec        *p_codec = NULL;
263
264     /* *** determine codec type *** */
265     if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
266                              &psz_namecodec ) )
267     {
268         return VLC_EGENERIC;
269     }
270
271     /* Initialization must be done before avcodec_find_decoder() */
272     vlc_init_avcodec();
273
274     /* *** ask ffmpeg for a decoder *** */
275     char *psz_decoder = var_CreateGetString( p_this, "avcodec-codec" );
276     if( psz_decoder && *psz_decoder )
277     {
278         p_codec = avcodec_find_decoder_by_name( psz_decoder );
279         if( !p_codec )
280             msg_Err( p_this, "Decoder `%s' not found", psz_decoder );
281         else if( p_codec->id != i_codec_id )
282         {
283             msg_Err( p_this, "Decoder `%s' can't handle %4.4s",
284                     psz_decoder, (char*)&p_dec->fmt_in.i_codec );
285             p_codec = NULL;
286         }
287     }
288     free( psz_decoder );
289     if( !p_codec )
290         p_codec = avcodec_find_decoder( i_codec_id );
291     if( !p_codec )
292     {
293         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
294         return VLC_EGENERIC;
295     }
296
297     /* *** get a p_context *** */
298     p_context = avcodec_alloc_context3(p_codec);
299     if( !p_context )
300         return VLC_ENOMEM;
301     p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
302     p_context->opaque = (void *)p_this;
303
304     /* set CPU capabilities */
305 #if LIBAVUTIL_VERSION_CHECK(51, 25, 0, 42, 100)
306     av_set_cpu_flags_mask( INT_MAX & ~GetVlcDspMask() );
307 #else
308     p_context->dsp_mask = GetVlcDspMask();
309 #endif
310
311     p_dec->b_need_packetized = true;
312     switch( i_cat )
313     {
314     case VIDEO_ES:
315         p_dec->pf_decode_video = DecodeVideo;
316         i_result =  InitVideoDec ( p_dec, p_context, p_codec,
317                                        i_codec_id, psz_namecodec );
318         break;
319     case AUDIO_ES:
320         p_dec->pf_decode_audio = DecodeAudio;
321         i_result =  InitAudioDec ( p_dec, p_context, p_codec,
322                                        i_codec_id, psz_namecodec );
323         break;
324     case SPU_ES:
325         p_dec->pf_decode_sub = DecodeSubtitle;
326         i_result =  InitSubtitleDec( p_dec, p_context, p_codec,
327                                      i_codec_id, psz_namecodec );
328         break;
329     default:
330         i_result = VLC_EGENERIC;
331     }
332
333     if( i_result == VLC_SUCCESS )
334     {
335         p_dec->p_sys->i_cat = i_cat;
336         if( p_context->profile != FF_PROFILE_UNKNOWN)
337             p_dec->fmt_in.i_profile = p_context->profile;
338         if( p_context->level != FF_LEVEL_UNKNOWN)
339             p_dec->fmt_in.i_level = p_context->level;
340     }
341
342     return i_result;
343 }
344
345 /*****************************************************************************
346  * CloseDecoder: decoder destruction
347  *****************************************************************************/
348 static void CloseDecoder( vlc_object_t *p_this )
349 {
350     decoder_t *p_dec = (decoder_t *)p_this;
351     decoder_sys_t *p_sys = p_dec->p_sys;
352
353     switch( p_sys->i_cat )
354     {
355     case VIDEO_ES:
356          EndVideoDec ( p_dec );
357         break;
358     }
359
360     if( p_sys->p_context )
361     {
362         av_free( p_sys->p_context->extradata );
363         p_sys->p_context->extradata = NULL;
364
365         if( !p_sys->b_delayed_open )
366         {
367             vlc_avcodec_lock();
368             avcodec_close( p_sys->p_context );
369             vlc_avcodec_unlock();
370         }
371         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
372         av_free( p_sys->p_context );
373     }
374
375     free( p_sys );
376 }
377
378 /*****************************************************************************
379  * ffmpeg_OpenCodec:
380  *****************************************************************************/
381 int ffmpeg_OpenCodec( decoder_t *p_dec )
382 {
383     decoder_sys_t *p_sys = p_dec->p_sys;
384
385     if( p_sys->p_context->extradata_size <= 0 )
386     {
387         if( p_sys->i_codec_id == AV_CODEC_ID_VC1 ||
388             p_sys->i_codec_id == AV_CODEC_ID_VORBIS ||
389             p_sys->i_codec_id == AV_CODEC_ID_THEORA ||
390             ( p_sys->i_codec_id == AV_CODEC_ID_AAC &&
391               !p_dec->fmt_in.b_packetized ) )
392         {
393             msg_Warn( p_dec, "waiting for extra data for codec %s",
394                       p_sys->psz_namecodec );
395             return 1;
396         }
397     }
398     if( p_dec->fmt_in.i_cat == VIDEO_ES )
399     {
400         p_sys->p_context->width  = p_dec->fmt_in.video.i_visible_width;
401         p_sys->p_context->height = p_dec->fmt_in.video.i_visible_height;
402         if (p_sys->p_context->width  == 0)
403             p_sys->p_context->width  = p_dec->fmt_in.video.i_width;
404         else if (p_sys->p_context->width != p_dec->fmt_in.video.i_width)
405             p_sys->p_context->coded_width = p_dec->fmt_in.video.i_width;
406         if (p_sys->p_context->height == 0)
407             p_sys->p_context->height = p_dec->fmt_in.video.i_height;
408         else if (p_sys->p_context->height != p_dec->fmt_in.video.i_height)
409             p_sys->p_context->coded_height = p_dec->fmt_in.video.i_height;
410         p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
411     }
412     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
413     {
414         p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
415         p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
416
417         p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
418         p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
419         p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
420         if( p_sys->i_codec_id == AV_CODEC_ID_ADPCM_G726 &&
421             p_sys->p_context->bit_rate > 0 &&
422             p_sys->p_context->sample_rate >  0)
423             p_sys->p_context->bits_per_coded_sample = p_sys->p_context->bit_rate /
424                                                       p_sys->p_context->sample_rate;
425     }
426     int ret;
427     char *psz_opts = var_InheritString( p_dec, "avcodec-options" );
428     AVDictionary *options = NULL;
429     if (psz_opts && *psz_opts)
430         options = vlc_av_get_options(psz_opts);
431     free(psz_opts);
432
433     vlc_avcodec_lock();
434     ret = avcodec_open2( p_sys->p_context, p_sys->p_codec, options ? &options : NULL );
435     vlc_avcodec_unlock();
436
437     AVDictionaryEntry *t = NULL;
438     while ((t = av_dict_get(options, "", t, AV_DICT_IGNORE_SUFFIX))) {
439         msg_Err( p_dec, "Unknown option \"%s\"", t->key );
440     }
441     av_dict_free(&options);
442
443     if( ret < 0 )
444         return VLC_EGENERIC;
445     msg_Dbg( p_dec, "avcodec codec (%s) started", p_sys->psz_namecodec );
446
447 #ifdef HAVE_AVCODEC_MT
448     if( p_dec->fmt_in.i_cat == VIDEO_ES )
449     {
450         switch( p_sys->p_context->active_thread_type )
451         {
452             case FF_THREAD_FRAME:
453                 msg_Dbg( p_dec, "using frame thread mode with %d threads",
454                          p_sys->p_context->thread_count );
455                 break;
456             case FF_THREAD_SLICE:
457                 msg_Dbg( p_dec, "using slice thread mode with %d threads",
458                          p_sys->p_context->thread_count );
459                 break;
460             case 0:
461                 if( p_sys->p_context->thread_count > 1 )
462                     msg_Warn( p_dec, "failed to enable threaded decoding" );
463                 break;
464             default:
465                 msg_Warn( p_dec, "using unknown thread mode with %d threads",
466                           p_sys->p_context->thread_count );
467                 break;
468         }
469     }
470 #endif
471
472     p_sys->b_delayed_open = false;
473
474     return VLC_SUCCESS;
475 }