]> git.sesse.net Git - vlc/blob - modules/codec/avcodec/avcodec.c
9b54976087d0bdd66fab064d237844013427486a
[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 #include "chroma.h"
51
52 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
53 #   error You must update libavcodec to a version >= 52.25.0
54 #endif
55
56 /*****************************************************************************
57  * decoder_sys_t: decoder descriptor
58  *****************************************************************************/
59 struct decoder_sys_t
60 {
61     /* Common part between video and audio decoder */
62     FFMPEG_COMMON_MEMBERS
63 };
64
65 /****************************************************************************
66  * Local prototypes
67  ****************************************************************************/
68 static int OpenDecoder( vlc_object_t * );
69 static void CloseDecoder( vlc_object_t * );
70
71 static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
72 static const char *const nloopf_list_text[] =
73   { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };
74
75 #ifdef ENABLE_SOUT
76 static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
77 static const char *const enc_hq_list_text[] = {
78     N_("rd"), N_("bits"), N_("simple") };
79 #endif
80
81 #ifdef MERGE_FFMPEG
82 # include "../../demux/avformat/avformat.h"
83 # include "../../access/avio.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_VCODEC )
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 #if defined(HAVE_AVCODEC_VAAPI) || defined(HAVE_AVCODEC_DXVA2)
140     add_bool( "ffmpeg-hw", false, NULL, HW_TEXT, HW_LONGTEXT, true )
141 #endif
142 #if defined(FF_THREAD_FRAME)
143     add_integer( "ffmpeg-threads", 0, NULL, THREADS_TEXT, THREADS_LONGTEXT, true );
144 #endif
145
146
147 #ifdef ENABLE_SOUT
148     /* encoder submodule */
149     add_submodule ()
150     add_shortcut( "ffmpeg" )
151     set_section( N_("Encoding") , NULL )
152     set_description( N_("FFmpeg audio/video encoder") )
153     set_capability( "encoder", 100 )
154     set_callbacks( OpenEncoder, CloseEncoder )
155
156     add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT,
157                 ENC_HQ_LONGTEXT, false )
158         change_string_list( enc_hq_list, enc_hq_list_text, 0 )
159     add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT,
160                  ENC_KEYINT_LONGTEXT, false )
161     add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT,
162                  ENC_BFRAMES_LONGTEXT, false )
163     add_bool( ENC_CFG_PREFIX "hurry-up", false, NULL, ENC_HURRYUP_TEXT,
164               ENC_HURRYUP_LONGTEXT, false )
165     add_bool( ENC_CFG_PREFIX "interlace", false, NULL, ENC_INTERLACE_TEXT,
166               ENC_INTERLACE_LONGTEXT, true )
167     add_bool( ENC_CFG_PREFIX "interlace-me", true, NULL, ENC_INTERLACE_ME_TEXT,
168               ENC_INTERLACE_ME_LONGTEXT, true )
169     add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT,
170                  ENC_VT_LONGTEXT, true )
171     add_bool( ENC_CFG_PREFIX "pre-me", false, NULL, ENC_PRE_ME_TEXT,
172               ENC_PRE_ME_LONGTEXT, true )
173     add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL,
174                  ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true )
175     add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL,
176                ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true )
177     add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL,
178                ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true )
179     add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL,
180                  ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true )
181     add_bool( ENC_CFG_PREFIX "mpeg4-matrix", false, NULL,
182               ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true )
183     add_integer( ENC_CFG_PREFIX "qmin", 0, NULL,
184                  ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true )
185     add_integer( ENC_CFG_PREFIX "qmax", 0, NULL,
186                  ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true )
187     add_bool( ENC_CFG_PREFIX "trellis", false, NULL,
188               ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true )
189     add_float( ENC_CFG_PREFIX "qscale", 0, NULL,
190                ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true )
191     add_integer( ENC_CFG_PREFIX "strict", 0, NULL,
192                  ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true )
193     add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL,
194                ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true )
195     add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL,
196                ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true )
197     add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL,
198                ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true )
199     add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL,
200                ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true )
201     add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL,
202                  ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true )
203     add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL,
204                  ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true )
205
206     /* Audio AAC encoder profile */
207     add_string( ENC_CFG_PREFIX "aac-profile", "low", NULL,
208                 ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
209 #endif /* ENABLE_SOUT */
210
211     /* video filter submodule */
212     add_submodule ()
213     set_capability( "video filter2", 0 )
214     set_callbacks( OpenDeinterlace, CloseDeinterlace )
215     set_description( N_("FFmpeg deinterlace video filter") )
216     add_shortcut( "ffmpeg-deinterlace" )
217
218 #ifdef MERGE_FFMPEG
219     add_submodule ()
220 #   include "../../demux/avformat/avformat.c"
221     add_submodule ()
222         AVIO_MODULE
223 #endif
224 vlc_module_end ()
225
226 /*****************************************************************************
227  * OpenDecoder: probe the decoder and return score
228  *****************************************************************************/
229 static int OpenDecoder( vlc_object_t *p_this )
230 {
231     decoder_t *p_dec = (decoder_t*) p_this;
232     int i_cat, i_codec_id, i_result;
233     const char *psz_namecodec;
234
235     AVCodecContext *p_context = NULL;
236     AVCodec        *p_codec = NULL;
237
238     /* *** determine codec type *** */
239     if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
240                              &psz_namecodec ) )
241     {
242         return VLC_EGENERIC;
243     }
244
245     /* Initialization must be done before avcodec_find_decoder() */
246     InitLibavcodec(p_this);
247
248     /* *** ask ffmpeg for a decoder *** */
249     p_codec = avcodec_find_decoder( i_codec_id );
250     if( !p_codec )
251     {
252         msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
253         return VLC_EGENERIC;
254     }
255
256     /* *** get a p_context *** */
257     p_context = avcodec_alloc_context();
258     if( !p_context )
259         return VLC_ENOMEM;
260     p_context->debug = var_InheritInteger( p_dec, "ffmpeg-debug" );
261     p_context->opaque = (void *)p_this;
262
263     /* Set CPU capabilities */
264     unsigned i_cpu = vlc_CPU();
265     p_context->dsp_mask = 0;
266     if( !(i_cpu & CPU_CAPABILITY_MMX) )
267     {
268         p_context->dsp_mask |= FF_MM_MMX;
269     }
270     if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
271     {
272         p_context->dsp_mask |= FF_MM_MMXEXT;
273     }
274     if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
275     {
276         p_context->dsp_mask |= FF_MM_3DNOW;
277     }
278     if( !(i_cpu & CPU_CAPABILITY_SSE) )
279     {
280         p_context->dsp_mask |= FF_MM_SSE;
281     }
282     if( !(i_cpu & CPU_CAPABILITY_SSE2) )
283     {
284         p_context->dsp_mask |= FF_MM_SSE2;
285     }
286 #ifdef FF_MM_SSE3
287     if( !(i_cpu & CPU_CAPABILITY_SSE3) )
288         p_context->dsp_mask |= FF_MM_SSE3;
289 #endif
290 #ifdef FF_MM_SSSE3
291     if( !(i_cpu & CPU_CAPABILITY_SSSE3) )
292         p_context->dsp_mask |= FF_MM_SSSE3;
293 #endif
294 #ifdef FF_MM_SSE4
295     if( !(i_cpu & CPU_CAPABILITY_SSE4_1) )
296         p_context->dsp_mask |= FF_MM_SSE4;
297 #endif
298 #ifdef FF_MM_SSE42
299     if( !(i_cpu & CPU_CAPABILITY_SSE4_2) )
300         p_context->dsp_mask |= FF_MM_SSE42;
301 #endif
302
303     p_dec->b_need_packetized = true;
304     switch( i_cat )
305     {
306     case VIDEO_ES:
307         p_dec->pf_decode_video = DecodeVideo;
308         i_result =  InitVideoDec ( p_dec, p_context, p_codec,
309                                        i_codec_id, psz_namecodec );
310         break;
311     case AUDIO_ES:
312         p_dec->pf_decode_audio = DecodeAudio;
313         i_result =  InitAudioDec ( p_dec, p_context, p_codec,
314                                        i_codec_id, psz_namecodec );
315         break;
316     case SPU_ES:
317         p_dec->pf_decode_sub = DecodeSubtitle;
318         i_result =  InitSubtitleDec( p_dec, p_context, p_codec,
319                                      i_codec_id, psz_namecodec );
320         break;
321     default:
322         i_result = VLC_EGENERIC;
323     }
324
325     if( i_result == VLC_SUCCESS )
326     {
327         p_dec->p_sys->i_cat = i_cat;
328         if( p_context->profile != FF_PROFILE_UNKNOWN)
329             p_dec->fmt_in.i_profile = p_context->profile;
330         if( p_context->level != FF_LEVEL_UNKNOWN)
331             p_dec->fmt_in.i_level = p_context->level;
332     }
333
334     return i_result;
335 }
336
337 /*****************************************************************************
338  * CloseDecoder: decoder destruction
339  *****************************************************************************/
340 static void CloseDecoder( vlc_object_t *p_this )
341 {
342     decoder_t *p_dec = (decoder_t *)p_this;
343     decoder_sys_t *p_sys = p_dec->p_sys;
344
345     switch( p_sys->i_cat )
346     {
347     case AUDIO_ES:
348          EndAudioDec ( p_dec );
349         break;
350     case VIDEO_ES:
351          EndVideoDec ( p_dec );
352         break;
353     case SPU_ES:
354          EndSubtitleDec( p_dec );
355         break;
356     }
357
358     if( p_sys->p_context )
359     {
360         free( p_sys->p_context->extradata );
361         p_sys->p_context->extradata = NULL;
362
363         if( !p_sys->b_delayed_open )
364         {
365             vlc_avcodec_lock();
366             avcodec_close( p_sys->p_context );
367             vlc_avcodec_unlock();
368         }
369         msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
370         av_free( p_sys->p_context );
371     }
372
373     free( p_sys );
374 }
375
376 void InitLibavcodec( vlc_object_t *p_object )
377 {
378     static bool b_ffmpeginit = false;
379
380     vlc_avcodec_lock();
381
382     /* *** init ffmpeg library (libavcodec) *** */
383     if( !b_ffmpeginit )
384     {
385         avcodec_init();
386         avcodec_register_all();
387         av_log_set_callback( LibavutilCallback );
388         b_ffmpeginit = true;
389
390         msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
391                  LIBAVCODEC_VERSION_INT );
392     }
393     else
394     {
395         msg_Dbg( p_object, "libavcodec already initialized" );
396     }
397
398     vlc_avcodec_unlock();
399 }
400
401 /*****************************************************************************
402  * ffmpeg_OpenCodec:
403  *****************************************************************************/
404 int ffmpeg_OpenCodec( decoder_t *p_dec )
405 {
406     decoder_sys_t *p_sys = p_dec->p_sys;
407
408     if( p_sys->p_context->extradata_size <= 0 )
409     {
410         if( p_sys->i_codec_id == CODEC_ID_VC1 ||
411             p_sys->i_codec_id == CODEC_ID_VORBIS ||
412             p_sys->i_codec_id == CODEC_ID_THEORA )
413         {
414             msg_Warn( p_dec, "waiting for extra data for codec %s",
415                       p_sys->psz_namecodec );
416             return 1;
417         }
418     }
419     if( p_dec->fmt_in.i_cat == VIDEO_ES )
420     {
421         p_sys->p_context->width  = p_dec->fmt_in.video.i_width;
422         p_sys->p_context->height = p_dec->fmt_in.video.i_height;
423         p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.video.i_bits_per_pixel;
424     }
425     else if( p_dec->fmt_in.i_cat == AUDIO_ES )
426     {
427         p_sys->p_context->sample_rate = p_dec->fmt_in.audio.i_rate;
428         p_sys->p_context->channels = p_dec->fmt_in.audio.i_channels;
429
430         p_sys->p_context->block_align = p_dec->fmt_in.audio.i_blockalign;
431         p_sys->p_context->bit_rate = p_dec->fmt_in.i_bitrate;
432         p_sys->p_context->bits_per_coded_sample = p_dec->fmt_in.audio.i_bitspersample;
433     }
434     int ret;
435     vlc_avcodec_lock();
436     ret = avcodec_open( p_sys->p_context, p_sys->p_codec );
437     vlc_avcodec_unlock();
438     if( ret < 0 )
439         return VLC_EGENERIC;
440     msg_Dbg( p_dec, "ffmpeg codec (%s) started", p_sys->psz_namecodec );
441
442 #ifdef HAVE_AVCODEC_MT
443     if( p_dec->fmt_in.i_cat == VIDEO_ES )
444     {
445         switch( p_sys->p_context->active_thread_type )
446         {
447             case FF_THREAD_FRAME:
448                 msg_Dbg( p_dec, "using frame thread mode with %d threads",
449                          p_sys->p_context->thread_count );
450                 break;
451             case FF_THREAD_SLICE:
452                 msg_Dbg( p_dec, "using slice thread mode with %d threads",
453                          p_sys->p_context->thread_count );
454                 break;
455             case 0:
456                 if( p_sys->p_context->thread_count > 1 )
457                     msg_Warn( p_dec, "failed to enable threaded decoding" );
458                 break;
459             default:
460                 msg_Warn( p_dec, "using unknown thread mode with %d threads",
461                           p_sys->p_context->thread_count );
462                 break;
463         }
464     }
465 #endif
466
467     p_sys->b_delayed_open = false;
468
469     return VLC_SUCCESS;
470 }