avcodec.c 13 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
#include <vlc_avcodec.h>
36

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

Rémi Duraffort's avatar
Rémi Duraffort committed
47 48 49
#include "avcodec.h"
#include "avutil.h"

50 51 52 53
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 51, 48, 0 )
#   error You must update libavcodec to a version >= 51.48.0
#elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
#   warning You should update libavcodec to get subtitle support
54
#endif
55

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

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

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

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

81 82 83
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
84 85 86 87
#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")

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

107

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

132
    add_integer( "ffmpeg-debug", 0, NULL, DEBUG_TEXT, DEBUG_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
133
                 true )
134 135 136
#ifdef HAVE_AVCODEC_VAAPI
    add_bool( "ffmpeg-hw", true, NULL, HW_TEXT, HW_LONGTEXT, true )
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
137

138
#ifdef ENABLE_SOUT
Gildas Bazin's avatar
 
Gildas Bazin committed
139
    /* encoder submodule */
140
    add_submodule ()
141
    add_shortcut( "ffmpeg" )
142 143 144 145
    set_section( N_("Encoding") , NULL )
    set_description( N_("FFmpeg audio/video encoder") )
    set_capability( "encoder", 100 )
    set_callbacks( OpenEncoder, CloseEncoder )
Gildas Bazin's avatar
 
Gildas Bazin committed
146

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

197
    /* Audio AAC encoder profile */
198
    add_string( ENC_CFG_PREFIX "aac-profile", "low", NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
199
                ENC_PROFILE_TEXT, ENC_PROFILE_LONGTEXT, true )
200
#endif /* ENABLE_SOUT */
201

202
    /* video filter submodule */
203 204 205 206 207
    add_submodule ()
    set_capability( "video filter2", 0 )
    set_callbacks( OpenDeinterlace, CloseDeinterlace )
    set_description( N_("FFmpeg deinterlace video filter") )
    add_shortcut( "ffmpeg-deinterlace" )
208

209
vlc_module_end ()
210 211 212 213 214 215

/*****************************************************************************
 * OpenDecoder: probe the decoder and return score
 *****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
216
    decoder_t *p_dec = (decoder_t*) p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
217
    int i_cat, i_codec_id, i_result;
218
    const char *psz_namecodec;
Gildas Bazin's avatar
 
Gildas Bazin committed
219

Jean-Paul Saman's avatar
Jean-Paul Saman committed
220 221
    AVCodecContext *p_context = NULL;
    AVCodec        *p_codec = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
222 223

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

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

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

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

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

272
    p_dec->b_need_packetized = true;
Gildas Bazin's avatar
 
Gildas Bazin committed
273
    switch( i_cat )
274
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
275
    case VIDEO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
276 277
        p_dec->pf_decode_video = DecodeVideo;
        i_result =  InitVideoDec ( p_dec, p_context, p_codec,
Gildas Bazin's avatar
 
Gildas Bazin committed
278 279 280
                                       i_codec_id, psz_namecodec );
        break;
    case AUDIO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
281 282
        p_dec->pf_decode_audio = DecodeAudio;
        i_result =  InitAudioDec ( p_dec, p_context, p_codec,
Gildas Bazin's avatar
 
Gildas Bazin committed
283 284
                                       i_codec_id, psz_namecodec );
        break;
285
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
286 287 288 289 290
    case SPU_ES:
        p_dec->pf_decode_sub = DecodeSubtitle;
        i_result =  InitSubtitleDec( p_dec, p_context, p_codec,
                                     i_codec_id, psz_namecodec );
        break;
291
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
292 293
    default:
        i_result = VLC_EGENERIC;
294
    }
295

296
    if( i_result == VLC_SUCCESS ) p_dec->p_sys->i_cat = i_cat;
297

Gildas Bazin's avatar
 
Gildas Bazin committed
298
    return i_result;
299
}
Gildas Bazin's avatar
 
Gildas Bazin committed
300

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

Gildas Bazin's avatar
 
Gildas Bazin committed
309
    switch( p_sys->i_cat )
310
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
311
    case AUDIO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
312
         EndAudioDec ( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
313 314
        break;
    case VIDEO_ES:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
315
         EndVideoDec ( p_dec );
316
        break;
Rémi Duraffort's avatar
Rémi Duraffort committed
317
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT( 52, 25, 0 )
318 319
    case SPU_ES:
         EndSubtitleDec( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
320
        break;
Rémi Duraffort's avatar
Rémi Duraffort committed
321
#endif
322
    }
323

324 325
    if( p_sys->p_context )
    {
326
        free( p_sys->p_context->extradata );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
327
        p_sys->p_context->extradata = NULL;
328

329 330
        if( !p_sys->b_delayed_open )
        {
331
            vlc_avcodec_lock();
332
            avcodec_close( p_sys->p_context );
333
            vlc_avcodec_unlock();
334
        }
335 336 337 338
        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
339
    free( p_sys );
340
}
341

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
342
void InitLibavcodec( vlc_object_t *p_object )
Gildas Bazin's avatar
 
Gildas Bazin committed
343
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
344 345
    static bool b_ffmpeginit = false;

346
    vlc_avcodec_lock();
Gildas Bazin's avatar
 
Gildas Bazin committed
347 348 349 350 351 352

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
356
        msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
357
                 LIBAVCODEC_VERSION_INT );
Gildas Bazin's avatar
 
Gildas Bazin committed
358 359
    }
    else
360
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
361 362
        msg_Dbg( p_object, "libavcodec already initialized" );
    }
363

364
    vlc_avcodec_unlock();
Gildas Bazin's avatar
 
Gildas Bazin committed
365
}