Commit c8030242 authored by Rafaël Carré's avatar Rafaël Carré

libav*: move avformat/avcodec init to a single header file

Avoid repeating the same implementation several times:
 incorrectly (switcher and mux didn't lock)
 redundantly (avcodec_register_all can be called several times)

Move libavXXX init at the top of a few functions, just in case.

Check uses of vlc_avcodec_(un)lock and factorize
parent 8a3d02d5
......@@ -33,6 +33,7 @@
#include <vlc_avcodec.h>
#include "avio.h"
#include "../codec/avcodec/avcommon.h"
#if LIBAVFORMAT_VERSION_MAJOR < 54
# define AVIOContext URLContext
......@@ -137,9 +138,7 @@ int OpenAvio(vlc_object_t *object)
}
/* */
vlc_avcodec_lock();
av_register_all();
vlc_avcodec_unlock();
vlc_init_avformat();
int ret;
#if LIBAVFORMAT_VERSION_MAJOR < 54
......@@ -204,9 +203,7 @@ int OutOpenAvio(vlc_object_t *object)
sys->context = NULL;
/* */
vlc_avcodec_lock();
av_register_all();
vlc_avcodec_unlock();
vlc_init_avformat();
if (!access->psz_path)
goto error;
......
libavcodec_plugin_la_SOURCES = \
avcodec.c \
avcodec.h \
avcommon.h \
video.c \
subtitle.c \
audio.c \
......
......@@ -45,6 +45,7 @@
#include "avcodec.h"
#include "chroma.h"
#include "avcommon.h"
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT( 52, 25, 0 )
# error You must update libavcodec to a version >= 52.25.0
......@@ -236,7 +237,7 @@ static int OpenDecoder( vlc_object_t *p_this )
}
/* Initialization must be done before avcodec_find_decoder() */
InitLibavcodec(p_this);
vlc_init_avcodec();
/* *** ask ffmpeg for a decoder *** */
char *psz_decoder = var_CreateGetString( p_this, "ffmpeg-codec" );
......@@ -385,32 +386,6 @@ static void CloseDecoder( vlc_object_t *p_this )
free( p_sys );
}
void InitLibavcodec( vlc_object_t *p_object )
{
static bool b_ffmpeginit = false;
vlc_avcodec_lock();
/* *** init ffmpeg library (libavcodec) *** */
if( !b_ffmpeginit )
{
#if LIBAVCODEC_VERSION_MAJOR < 54
avcodec_init();
#endif
avcodec_register_all();
b_ffmpeginit = true;
msg_Dbg( p_object, "libavcodec initialized (interface 0x%x)",
LIBAVCODEC_VERSION_INT );
}
else
{
msg_Dbg( p_object, "libavcodec already initialized" );
}
vlc_avcodec_unlock();
}
/*****************************************************************************
* ffmpeg_OpenCodec:
*****************************************************************************/
......
......@@ -45,8 +45,6 @@ void CloseAudioEncoder( vlc_object_t * );
int OpenDeinterlace( vlc_object_t * );
void CloseDeinterlace( vlc_object_t * );
void InitLibavcodec( vlc_object_t *p_object );
/* Video Decoder */
int InitVideoDec( decoder_t *p_dec, AVCodecContext *p_context,
AVCodec *p_codec, int i_codec_id, const char *psz_namecodec );
......
/*****************************************************************************
* avinit.h: common code for libav* initialization
*****************************************************************************
* Copyright (C) 2012 the VideoLAN team
* $Id$
*
* Authors: Rafaël Carré <funman@videolanorg>
*
* 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.
*
* 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <vlc_avcodec.h>
#ifdef HAVE_LIBAVFORMAT_AVFORMAT_H
# include <libavformat/avformat.h>
static inline void vlc_init_avformat(void)
{
vlc_avcodec_lock();
av_register_all();
vlc_avcodec_unlock();
}
#endif
#ifdef HAVE_LIBAVCODEC_AVCODEC_H
# include <libavcodec/avcodec.h>
static inline void vlc_init_avcodec(void)
{
vlc_avcodec_lock();
#if LIBAVCODEC_VERSION_MAJOR < 54
avcodec_init();
#endif
avcodec_register_all();
vlc_avcodec_unlock();
}
#endif
......@@ -41,6 +41,7 @@
#endif
#include "avcodec.h"
#include "avcommon.h"
static picture_t *Deinterlace( filter_t *p_filter, picture_t *p_pic );
......@@ -70,6 +71,9 @@ int OpenDeinterlace( vlc_object_t *p_this )
filter_t *p_filter = (filter_t*)p_this;
filter_sys_t *p_sys;
/* libavcodec needs to be initialized for some chroma conversions */
vlc_init_avcodec();
/* Check if we can handle that formats */
if( TestFfmpegChroma( -1, p_filter->fmt_in.i_codec ) != VLC_SUCCESS )
{
......@@ -95,9 +99,6 @@ int OpenDeinterlace( vlc_object_t *p_this )
msg_Dbg( p_filter, "deinterlacing" );
/* libavcodec needs to be initialized for some chroma conversions */
InitLibavcodec(p_this);
return VLC_SUCCESS;
}
......
......@@ -49,6 +49,7 @@
#endif
#include "avcodec.h"
#include "avcommon.h"
#define HURRY_UP_GUARD1 (450000)
#define HURRY_UP_GUARD2 (300000)
......@@ -202,6 +203,9 @@ int OpenEncoder( vlc_object_t *p_this )
float f_val;
char *psz_val;
/* Initialization must be done before avcodec_find_encoder() */
vlc_init_avcodec();
config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
if( p_enc->fmt_out.i_codec == VLC_CODEC_MP3 )
......@@ -251,9 +255,6 @@ int OpenEncoder( vlc_object_t *p_this )
return VLC_EGENERIC;
}
/* Initialization must be done before avcodec_find_encoder() */
InitLibavcodec( p_this );
char *psz_encoder = var_GetString( p_this, ENC_CFG_PREFIX "codec" );
if( psz_encoder && *psz_encoder )
{
......
......@@ -97,14 +97,13 @@ int InitSubtitleDec(decoder_t *dec, AVCodecContext *context,
#else
ret = avcodec_open2(context, codec, NULL /* options */);
#endif
vlc_avcodec_unlock();
if (ret < 0) {
vlc_avcodec_unlock();
msg_Err(dec, "cannot open codec (%s)", namecodec);
free(context->extradata);
free(sys);
return VLC_EGENERIC;
}
vlc_avcodec_unlock();
/* */
msg_Dbg(dec, "ffmpeg codec (%s) started", namecodec);
......
......@@ -4,6 +4,7 @@ SOURCES_avformat = \
demux.c \
../../codec/avcodec/fourcc.c \
../../codec/avcodec/chroma.c \
../../codec/avcodec/avcommon.h \
../vobsub.h \
$(NULL)
if ENABLE_SOUT
......
......@@ -42,6 +42,7 @@
#include "../../codec/avcodec/avcodec.h"
#include "../../codec/avcodec/chroma.h"
#include "../../codec/avcodec/avcommon.h"
#include "avformat.h"
#include "../xiph.h"
#include "../vobsub.h"
......@@ -137,9 +138,7 @@ int OpenDemux( vlc_object_t *p_this )
}
stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_can_seek );
vlc_avcodec_lock();
av_register_all(); /* Can be called several times */
vlc_avcodec_unlock();
vlc_init_avformat();
char *psz_format = var_InheritString( p_this, "ffmpeg-format" );
if( psz_format )
......@@ -257,12 +256,12 @@ int OpenDemux( vlc_object_t *p_this )
#else
error = av_find_stream_info( p_sys->ic );
#endif
vlc_avcodec_unlock();
if( error < 0 )
{
errno = AVUNERROR(error);
msg_Warn( p_demux, "Could not find stream info: %m" );
}
vlc_avcodec_unlock();
for( i = 0; i < p_sys->ic->nb_streams; i++ )
{
......
......@@ -37,6 +37,7 @@
#include "avformat.h"
#include "../../codec/avcodec/avcodec.h"
#include "../../codec/avcodec/avcommon.h"
/* Support for deprecated APIs */
#if LIBAVFORMAT_VERSION_INT < ((52<<16)+(105<<8)+0)
......@@ -91,8 +92,7 @@ int OpenMux( vlc_object_t *p_this )
sout_mux_sys_t *p_sys;
char *psz_mux;
/* Should we call it only once ? */
av_register_all();
vlc_init_avformat();
config_ChainParse( p_mux, "ffmpeg-", ppsz_mux_options, p_mux->p_cfg );
......
......@@ -52,6 +52,7 @@
#endif
#include "../codec/avcodec/avcodec.h"
#include "../codec/avcodec/avcommon.h"
#define SOUT_CFG_PREFIX "sout-switcher-"
#define MAX_PICTURES 10
......@@ -286,10 +287,7 @@ static int Open( vlc_object_t *p_this )
p_stream->pf_send = Send;
p_stream->p_sys = p_sys;
#if LIBAVCODEC_VERSION_MAJOR < 54
avcodec_init();
#endif
avcodec_register_all();
vlc_init_avcodec();
return VLC_SUCCESS;
}
......@@ -811,17 +809,18 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
id->ff_enc_c->pix_fmt = PIX_FMT_YUV420P;
vlc_avcodec_lock();
int ret;
#if LIBAVCODEC_VERSION_MAJOR >= 54
if( avcodec_open2( id->ff_enc_c, id->ff_enc, NULL /* options */ ) )
ret = avcodec_open2( id->ff_enc_c, id->ff_enc, NULL /* options */ );
#else
if( avcodec_open( id->ff_enc_c, id->ff_enc ) )
ret = avcodec_open( id->ff_enc_c, id->ff_enc );
#endif
vlc_avcodec_unlock();
if (ret)
{
vlc_avcodec_unlock();
msg_Err( p_stream, "cannot open encoder" );
return 0;
}
vlc_avcodec_unlock();
id->p_buffer_out = malloc( id->ff_enc_c->width * id->ff_enc_c->height * 3 );
id->p_frame = avcodec_alloc_frame();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment