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