avcodec.c 12.7 KB
Newer Older
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 11 12 13
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
14
 *
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_codec.h>
35

Gildas Bazin's avatar
 
Gildas Bazin committed
36
/* ffmpeg header */
37
#define HAVE_MMX 1
38 39 40
#ifdef HAVE_LIBAVCODEC_AVCODEC_H
#   include <libavcodec/avcodec.h>
#elif defined(HAVE_FFMPEG_AVCODEC_H)
41
#   include <ffmpeg/avcodec.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
42 43 44
#else
#   include <avcodec.h>
#endif
45

46
#if LIBAVCODEC_BUILD < 5000
47
#   error You must have a libavcodec >= 5000 (get svn)
48
#endif
49

50 51
#include "avcodec.h"
#include "fourcc.h"
52
#include "avutil.h"
53

Gildas Bazin's avatar
 
Gildas Bazin committed
54 55 56 57 58 59
/*****************************************************************************
 * decoder_sys_t: decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    /* Common part between video and audio decoder */
60
    FFMPEG_COMMON_MEMBERS
Gildas Bazin's avatar
 
Gildas Bazin committed
61
};
62

Gildas Bazin's avatar
 
Gildas Bazin committed
63 64 65 66
/****************************************************************************
 * Local prototypes
 ****************************************************************************/
static int OpenDecoder( vlc_object_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
67
static void CloseDecoder( vlc_object_t * );
68

69 70
static const int  nloopf_list[] = { 0, 1, 2, 3, 4 };
static const char *const nloopf_list_text[] =
71 72
  { N_("None"), N_("Non-ref"), N_("Bidir"), N_("Non-key"), N_("All") };

73
#ifdef ENABLE_SOUT
74 75 76
static const char *const enc_hq_list[] = { "rd", "bits", "simple" };
static const char *const enc_hq_list_text[] = {
    N_("rd"), N_("bits"), N_("simple") };
77
#endif
78

79 80 81
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
82 83 84 85
#define MODULE_DESCRIPTION N_( "Various audio and video decoders/encoders" \
        "delivered by the FFmpeg library. This includes (MS)MPEG4, DivX, SV1,"\
        "H261, H263, H264, WMV, WMA, AAC, AMR, DV, MJPEG and other codecs")

86
vlc_module_begin();
87
    set_shortname( "FFmpeg");
88
    add_shortcut( "ffmpeg" );
Clément Stenac's avatar
Clément Stenac committed
89 90
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_SCODEC );
Gildas Bazin's avatar
 
Gildas Bazin committed
91
    /* decoder main module */
92 93
#if defined(MODULE_NAME_is_ffmpegaltivec) \
     || (defined(CAN_COMPILE_ALTIVEC) && !defined(NO_ALTIVEC_IN_FFMPEG))
94
    set_description( N_("AltiVec FFmpeg audio/video decoder ((MS)MPEG4,SVQ1,H263,WMV,WMA)") );
Sam Hocevar's avatar
Sam Hocevar committed
95
    /*add_requirement( ALTIVEC );*/
96 97
    set_capability( "decoder", 71 );
#else
98
    set_description( N_("FFmpeg audio/video decoder") );
99
    set_help( MODULE_DESCRIPTION );
Gildas Bazin's avatar
 
Gildas Bazin committed
100
    set_capability( "decoder", 70 );
101
#endif
Clément Stenac's avatar
Clément Stenac committed
102
    set_section( N_("Decoding") , NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
103
    set_callbacks( OpenDecoder, CloseDecoder );
Gildas Bazin's avatar
 
Gildas Bazin committed
104

105

106
    add_bool( "ffmpeg-dr", 1, NULL, DR_TEXT, DR_TEXT, true );
Gildas Bazin's avatar
Gildas Bazin committed
107
    add_integer ( "ffmpeg-error-resilience", 1, NULL, ERROR_TEXT,
108
        ERROR_LONGTEXT, true );
Gildas Bazin's avatar
 
Gildas Bazin committed
109
    add_integer ( "ffmpeg-workaround-bugs", 1, NULL, BUGS_TEXT, BUGS_LONGTEXT,
110
        false );
111
    add_bool( "ffmpeg-hurry-up", 1, NULL, HURRYUP_TEXT, HURRYUP_LONGTEXT,
112
        false );
113
    add_integer( "ffmpeg-skip-frame", 0, NULL, SKIP_FRAME_TEXT,
114
        SKIP_FRAME_LONGTEXT, true );
115 116
        change_integer_range( -1, 4 );
    add_integer( "ffmpeg-skip-idct", 0, NULL, SKIP_IDCT_TEXT,
117
        SKIP_IDCT_LONGTEXT, true );
118
        change_integer_range( -1, 4 );
Gildas Bazin's avatar
Gildas Bazin committed
119
    add_integer ( "ffmpeg-vismv", 0, NULL, VISMV_TEXT, VISMV_LONGTEXT,
120
        true );
121
    add_integer ( "ffmpeg-lowres", 0, NULL, LOWRES_TEXT, LOWRES_LONGTEXT,
122
        true );
123
        change_integer_range( 0, 2 );
124
    add_integer ( "ffmpeg-skiploopfilter", 0, NULL, SKIPLOOPF_TEXT,
125
                  SKIPLOOPF_LONGTEXT, true );
126
        change_integer_list( nloopf_list, nloopf_list_text, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
127

128
    add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT,
129
                 true );
Gildas Bazin's avatar
 
Gildas Bazin committed
130

131
#ifdef ENABLE_SOUT
Gildas Bazin's avatar
 
Gildas Bazin committed
132 133
    /* encoder submodule */
    add_submodule();
Clément Stenac's avatar
Clément Stenac committed
134
    set_section( N_("Encoding") , NULL );
135
    set_description( N_("FFmpeg audio/video encoder") );
Gildas Bazin's avatar
 
Gildas Bazin committed
136
    set_capability( "encoder", 100 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
137
    set_callbacks( OpenEncoder, CloseEncoder );
Gildas Bazin's avatar
 
Gildas Bazin committed
138

139
    add_string( ENC_CFG_PREFIX "hq", "simple", NULL, ENC_HQ_TEXT,
140
                ENC_HQ_LONGTEXT, false );
141 142
        change_string_list( enc_hq_list, enc_hq_list_text, 0 );
    add_integer( ENC_CFG_PREFIX "keyint", 0, NULL, ENC_KEYINT_TEXT,
143
                 ENC_KEYINT_LONGTEXT, false );
144
    add_integer( ENC_CFG_PREFIX "bframes", 0, NULL, ENC_BFRAMES_TEXT,
145
                 ENC_BFRAMES_LONGTEXT, false );
146
    add_bool( ENC_CFG_PREFIX "hurry-up", 0, NULL, ENC_HURRYUP_TEXT,
147
              ENC_HURRYUP_LONGTEXT, false );
148
    add_bool( ENC_CFG_PREFIX "interlace", 0, NULL, ENC_INTERLACE_TEXT,
149
              ENC_INTERLACE_LONGTEXT, true );
150
    add_bool( ENC_CFG_PREFIX "interlace-me", 1, NULL, ENC_INTERLACE_ME_TEXT,
151
              ENC_INTERLACE_ME_LONGTEXT, true );
152
    add_integer( ENC_CFG_PREFIX "vt", 0, NULL, ENC_VT_TEXT,
153
                 ENC_VT_LONGTEXT, true );
154
    add_bool( ENC_CFG_PREFIX "pre-me", 0, NULL, ENC_PRE_ME_TEXT,
155
              ENC_PRE_ME_LONGTEXT, true );
156
    add_integer( ENC_CFG_PREFIX "rc-buffer-size", 224*1024*8, NULL,
157
                 ENC_RC_BUF_TEXT, ENC_RC_BUF_LONGTEXT, true );
158
    add_float( ENC_CFG_PREFIX "rc-buffer-aggressivity", 1.0, NULL,
159
               ENC_RC_BUF_AGGR_TEXT, ENC_RC_BUF_AGGR_LONGTEXT, true );
160
    add_float( ENC_CFG_PREFIX "i-quant-factor", 0, NULL,
161
               ENC_IQUANT_FACTOR_TEXT, ENC_IQUANT_FACTOR_LONGTEXT, true );
162
    add_integer( ENC_CFG_PREFIX "noise-reduction", 0, NULL,
163
                 ENC_NOISE_RED_TEXT, ENC_NOISE_RED_LONGTEXT, true );
164
    add_bool( ENC_CFG_PREFIX "mpeg4-matrix", 0, NULL,
165
              ENC_MPEG4_MATRIX_TEXT, ENC_MPEG4_MATRIX_LONGTEXT, true );
166
    add_integer( ENC_CFG_PREFIX "qmin", 0, NULL,
167
                 ENC_QMIN_TEXT, ENC_QMIN_LONGTEXT, true );
168
    add_integer( ENC_CFG_PREFIX "qmax", 0, NULL,
169
                 ENC_QMAX_TEXT, ENC_QMAX_LONGTEXT, true );
170
    add_bool( ENC_CFG_PREFIX "trellis", 0, NULL,
171
              ENC_TRELLIS_TEXT, ENC_TRELLIS_LONGTEXT, true );
Gildas Bazin's avatar
Gildas Bazin committed
172
    add_float( ENC_CFG_PREFIX "qscale", 0, NULL,
173
               ENC_QSCALE_TEXT, ENC_QSCALE_LONGTEXT, true );
Gildas Bazin's avatar
Gildas Bazin committed
174
    add_integer( ENC_CFG_PREFIX "strict", 0, NULL,
175
                 ENC_STRICT_TEXT, ENC_STRICT_LONGTEXT, true );
176
    add_float( ENC_CFG_PREFIX "lumi-masking", 0.0, NULL,
177
               ENC_LUMI_MASKING_TEXT, ENC_LUMI_MASKING_LONGTEXT, true );
178
    add_float( ENC_CFG_PREFIX "dark-masking", 0.0, NULL,
179
               ENC_DARK_MASKING_TEXT, ENC_DARK_MASKING_LONGTEXT, true );
180
    add_float( ENC_CFG_PREFIX "p-masking", 0.0, NULL,
181
               ENC_P_MASKING_TEXT, ENC_P_MASKING_LONGTEXT, true );
182
    add_float( ENC_CFG_PREFIX "border-masking", 0.0, NULL,
183
               ENC_BORDER_MASKING_TEXT, ENC_BORDER_MASKING_LONGTEXT, true );
184
    add_integer( ENC_CFG_PREFIX "luma-elim-threshold", 0, NULL,
185
                 ENC_LUMA_ELIM_TEXT, ENC_LUMA_ELIM_LONGTEXT, true );
186
    add_integer( ENC_CFG_PREFIX "chroma-elim-threshold", 0, NULL,
187
                 ENC_CHROMA_ELIM_TEXT, ENC_CHROMA_ELIM_LONGTEXT, true );
188

189
#if LIBAVCODEC_VERSION_INT >= ((51<<16)+(40<<8)+4)
190 191
    /* Audio AAC encoder profile */
    add_string( ENC_CFG_PREFIX "aac-profile", "main", NULL,
192
                ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true );
193
#endif
194
#endif /* ENABLE_SOUT */
195

196 197 198
    /* video filter submodule */
    add_submodule();
    set_capability( "video filter2", 0 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
199
    set_callbacks( OpenDeinterlace, CloseDeinterlace );
200
    set_description( N_("FFmpeg deinterlace video filter") );
201
    add_shortcut( "ffmpeg-deinterlace" );
202

203 204
vlc_module_end();

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
205 206
vlc_mutex_t avcodec_lock = VLC_STATIC_MUTEX;

207 208 209 210 211
/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
212
    decoder_t *p_dec = (decoder_t*) p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
213
    int i_cat, i_codec_id, i_result;
214
    const char *psz_namecodec;
Gildas Bazin's avatar
 
Gildas Bazin committed
215

Jean-Paul Saman's avatar
Jean-Paul Saman committed
216 217
    AVCodecContext *p_context = NULL;
    AVCodec        *p_codec = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
218 219

    /* *** determine codec type *** */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
220
    if( !GetFfmpegCodec( p_dec->fmt_in.i_codec, &i_cat, &i_codec_id,
Gildas Bazin's avatar
 
Gildas Bazin committed
221 222 223 224
                             &psz_namecodec ) )
    {
        return VLC_EGENERIC;
    }
225

Gildas Bazin's avatar
 
Gildas Bazin committed
226 227 228 229 230 231 232 233
    /* Bail out if buggy decoder */
    if( i_codec_id == CODEC_ID_AAC )
    {
        msg_Dbg( p_dec, "refusing to use ffmpeg's (%s) decoder which is buggy",
                 psz_namecodec );
        return VLC_EGENERIC;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
234
    /* Initialization must be done before avcodec_find_decoder() */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
235
    InitLibavcodec(p_this);
Gildas Bazin's avatar
 
Gildas Bazin committed
236

237
    /* *** ask ffmpeg for a decoder *** */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
238 239
    p_codec = avcodec_find_decoder( i_codec_id );
    if( !p_codec )
240
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
241
        msg_Dbg( p_dec, "codec not found (%s)", psz_namecodec );
Gildas Bazin's avatar
 
Gildas Bazin committed
242
        return VLC_EGENERIC;
243 244
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
245 246
    /* *** get a p_context *** */
    p_context = avcodec_alloc_context();
Jean-Paul Saman's avatar
Jean-Paul Saman committed
247
    if( !p_context )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
248
        return VLC_ENOMEM;
249
    p_context->debug = config_GetInt( p_dec, "ffmpeg-debug" );
250
    p_context->opaque = (void *)p_this;
251

Gildas Bazin's avatar
 
Gildas Bazin committed
252
    /* Set CPU capabilities */
253
    unsigned i_cpu = vlc_CPU();
Gildas Bazin's avatar
 
Gildas Bazin committed
254
    p_context->dsp_mask = 0;
255
    if( !(i_cpu & CPU_CAPABILITY_MMX) )
Gildas Bazin's avatar
 
Gildas Bazin committed
256
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
257
        p_context->dsp_mask |= FF_MM_MMX;
Gildas Bazin's avatar
 
Gildas Bazin committed
258
    }
259
    if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
Gildas Bazin's avatar
 
Gildas Bazin committed
260
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
261
        p_context->dsp_mask |= FF_MM_MMXEXT;
Gildas Bazin's avatar
 
Gildas Bazin committed
262
    }
263
    if( !(i_cpu & CPU_CAPABILITY_3DNOW) )
Gildas Bazin's avatar
 
Gildas Bazin committed
264
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
265
        p_context->dsp_mask |= FF_MM_3DNOW;
Gildas Bazin's avatar
 
Gildas Bazin committed
266
    }
267
    if( !(i_cpu & CPU_CAPABILITY_SSE) )
Gildas Bazin's avatar
 
Gildas Bazin committed
268
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
269
        p_context->dsp_mask |= FF_MM_SSE;
270
    }
271
    if( !(i_cpu & CPU_CAPABILITY_SSE2) )
272
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
273
        p_context->dsp_mask |= FF_MM_SSE2;
Gildas Bazin's avatar
 
Gildas Bazin committed
274 275
    }

276
    p_dec->b_need_packetized = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
277
    switch( i_cat )
278
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
279
    case VIDEO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
280 281
        p_dec->pf_decode_video = DecodeVideo;
        i_result =  InitVideoDec ( p_dec, p_context, p_codec,
Gildas Bazin's avatar
 
Gildas Bazin committed
282 283 284
                                       i_codec_id, psz_namecodec );
        break;
    case AUDIO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
285 286
        p_dec->pf_decode_audio = DecodeAudio;
        i_result =  InitAudioDec ( p_dec, p_context, p_codec,
Gildas Bazin's avatar
 
Gildas Bazin committed
287 288 289 290
                                       i_codec_id, psz_namecodec );
        break;
    default:
        i_result = VLC_EGENERIC;
291
    }
292

293
    if( i_result == VLC_SUCCESS ) p_dec->p_sys->i_cat = i_cat;
294

Gildas Bazin's avatar
 
Gildas Bazin committed
295
    return i_result;
296
}
Gildas Bazin's avatar
 
Gildas Bazin committed
297

298
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
299
 * CloseDecoder: decoder destruction
300
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
301
static void CloseDecoder( vlc_object_t *p_this )
302
{
Gildas Bazin's avatar
 
Gildas Bazin committed
303
    decoder_t *p_dec = (decoder_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
304
    decoder_sys_t *p_sys = p_dec->p_sys;
305

Gildas Bazin's avatar
 
Gildas Bazin committed
306
    switch( p_sys->i_cat )
307
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
308
    case AUDIO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
309
         EndAudioDec ( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
310 311
        break;
    case VIDEO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
312
         EndVideoDec ( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
313
        break;
314
    }
315

316 317
    if( p_sys->p_context )
    {
318 319
        vlc_mutex_t *lock;

320 321
        if( p_sys->p_context->extradata )
            free( p_sys->p_context->extradata );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
322
        p_sys->p_context->extradata = NULL;
323

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
324
        vlc_mutex_lock( &avcodec_lock );
325
        avcodec_close( p_sys->p_context );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
326
        vlc_mutex_unlock( &avcodec_lock );
327 328 329 330
        msg_Dbg( p_dec, "ffmpeg codec (%s) stopped", p_sys->psz_namecodec );
        av_free( p_sys->p_context );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
331
    free( p_sys );
332
}
333

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
334
void InitLibavcodec( vlc_object_t *p_object )
Gildas Bazin's avatar
 
Gildas Bazin committed
335
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
336 337 338
    static bool b_ffmpeginit = false;

    vlc_mutex_lock( &avcodec_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
339 340 341 342 343 344

    /* *** init ffmpeg library (libavcodec) *** */
    if( !b_ffmpeginit )
    {
        avcodec_init();
        avcodec_register_all();
345
        av_log_set_callback( LibavutilCallback );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
346
        b_ffmpeginit = true;
347

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
348
        msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
349
                 LIBAVCODEC_VERSION_INT );
Gildas Bazin's avatar
 
Gildas Bazin committed
350 351
    }
    else
352
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
353 354
        msg_Dbg( p_object, "libavcodec already initialized" );
    }
355

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
356
    vlc_mutex_unlock( &avcodec_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
357
}