avcodec: factor CPU detection code

In retrospect, I should have done that first. Oh well...
This commit is contained in:
Rémi Denis-Courmont 2012-08-04 17:01:09 +03:00
parent b1a0789432
commit 0fbbb451f8
7 changed files with 87 additions and 127 deletions

View File

@ -5,6 +5,7 @@ libavcodec_plugin_la_SOURCES = \
video.c \
subtitle.c \
audio.c \
cpu.c \
deinterlace.c \
fourcc.c \
chroma.h \

View File

@ -327,37 +327,7 @@ static int OpenDecoder( vlc_object_t *p_this )
return VLC_ENOMEM;
p_context->debug = var_InheritInteger( p_dec, "avcodec-debug" );
p_context->opaque = (void *)p_this;
/* Set CPU capabilities */
p_context->dsp_mask = 0;
#if defined (__i386__) || defined (__x86_64__)
if( !vlc_CPU_MMX() )
p_context->dsp_mask |= AV_CPU_FLAG_MMX;
if( !vlc_CPU_MMXEXT() )
p_context->dsp_mask |= AV_CPU_FLAG_MMX2;
if( !vlc_CPU_3dNOW() )
p_context->dsp_mask |= AV_CPU_FLAG_3DNOW;
if( !vlc_CPU_SSE() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE;
if( !vlc_CPU_SSE2() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE2;
# ifdef AV_CPU_FLAG_SSE3
if( !vlc_CPU_SSE3() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE3;
# endif
# ifdef AV_CPU_FLAG_SSSE3
if( !vlc_CPU_SSE3() )
p_context->dsp_mask |= AV_CPU_FLAG_SSSE3;
# endif
# ifdef AV_CPU_FLAG_SSE4
if( !vlc_CPU_SSE4_1() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE4;
# endif
# ifdef AV_CPU_FLAG_SSE42
if( !vlc_CPU_SSE4_2() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE42;
# endif
#endif
p_context->dsp_mask = GetVlcDspMask(); /* set CPU capabilities */
p_dec->b_need_packetized = true;
switch( i_cat )

View File

@ -29,6 +29,8 @@ int GetVlcFourcc( int i_ffmpeg_codec, int *pi_cat,
vlc_fourcc_t *pi_fourcc, const char **ppsz_name );
void GetVlcAudioFormat( vlc_fourcc_t *, unsigned *pi_bits, int i_sample_fmt );
unsigned GetVlcDspMask( void );
picture_t * DecodeVideo( decoder_t *, block_t ** );
block_t * DecodeAudio( decoder_t *, block_t ** );
subpicture_t *DecodeSubtitle( decoder_t *p_dec, block_t ** );

View File

@ -0,0 +1,79 @@
/*****************************************************************************
* cpu.c: CPU capabilities for libavcodec
*****************************************************************************
* Copyright (C) 1999-2012 the VideoLAN team
*
* 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_common.h>
#include <vlc_cpu.h>
#define HAVE_MMX 1
#ifdef HAVE_LIBAVCODEC_AVCODEC_H
# include <libavcodec/avcodec.h>
#else
# include <avcodec.h>
#endif
#include "avcodec.h"
/**
* Maps CPU capabilities computed by VLC to libav DSP mask.
*/
unsigned GetVlcDspMask( void )
{
unsigned mask = 0;
#if defined (__i386__) || defined (__x86_64__)
if( !vlc_CPU_MMX() )
mask |= AV_CPU_FLAG_MMX;
if( !vlc_CPU_MMXEXT() )
mask |= AV_CPU_FLAG_MMX2;
if( !vlc_CPU_3dNOW() )
mask |= AV_CPU_FLAG_3DNOW;
if( !vlc_CPU_SSE() )
mask |= AV_CPU_FLAG_SSE;
if( !vlc_CPU_SSE2() )
mask |= AV_CPU_FLAG_SSE2;
# ifdef AV_CPU_FLAG_SSE3
if( !vlc_CPU_SSE3() )
mask |= AV_CPU_FLAG_SSE3;
# endif
# ifdef AV_CPU_FLAG_SSSE3
if( !vlc_CPU_SSE3() )
mask |= AV_CPU_FLAG_SSSE3;
# endif
# ifdef AV_CPU_FLAG_SSE4
if( !vlc_CPU_SSE4_1() )
mask |= AV_CPU_FLAG_SSE4;
# endif
# ifdef AV_CPU_FLAG_SSE42
if( !vlc_CPU_SSE4_2() )
mask |= AV_CPU_FLAG_SSE42;
# endif
// TODO: AVX
#endif
#if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
if( !vlc_CPU_ALTIVEC() )
mask |= AV_CPU_FLAG_ALTIVEC;
#endif
return mask;
}

View File

@ -321,37 +321,7 @@ int OpenEncoder( vlc_object_t *p_this )
p_sys->p_context->codec_id = p_sys->p_codec->id;
p_context->debug = var_InheritInteger( p_enc, "avcodec-debug" );
p_context->opaque = (void *)p_this;
/* Set CPU capabilities */
p_context->dsp_mask = 0;
#if defined (__i386__) || defined (__x86_64__)
if( !vlc_CPU_MMX() )
p_context->dsp_mask |= AV_CPU_FLAG_MMX;
if( !vlc_CPU_MMXEXT() )
p_context->dsp_mask |= AV_CPU_FLAG_MMX2;
if( !vlc_CPU_3dNOW() )
p_context->dsp_mask |= AV_CPU_FLAG_3DNOW;
if( !vlc_CPU_SSE() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE;
if( !vlc_CPU_SSE2() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE2;
# ifdef AV_CPU_FLAG_SSE3
if( !vlc_CPU_SSE3() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE3;
# endif
# ifdef AV_CPU_FLAG_SSSE3
if( !vlc_CPU_SSSE3() )
p_context->dsp_mask |= AV_CPU_FLAG_SSSE3;
# endif
# ifdef AV_CPU_FLAG_SSE4
if( !vlc_CPU_SSE4_1() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE4;
# endif
# ifdef AV_CPU_FLAG_SSE42
if( !vlc_CPU_SSE4_2() )
p_context->dsp_mask |= AV_CPU_FLAG_SSE42;
# endif
#endif
p_context->dsp_mask = GetVlcDspMask(); /* set CPU capabilities */
p_sys->i_key_int = var_GetInteger( p_enc, ENC_CFG_PREFIX "keyint" );
p_sys->i_b_frames = var_GetInteger( p_enc, ENC_CFG_PREFIX "bframes" );

View File

@ -8,7 +8,7 @@ SOURCES_stream_out_duplicate = duplicate.c
SOURCES_stream_out_es = es.c
SOURCES_stream_out_display = display.c
SOURCES_stream_out_gather = gather.c
SOURCES_stream_out_switcher = switcher.c
SOURCES_stream_out_switcher = switcher.c ../codec/avcodec/cpu.c
SOURCES_stream_out_bridge = bridge.c
SOURCES_stream_out_mosaic_bridge = mosaic_bridge.c
SOURCES_stream_out_autodel = autodel.c

View File

@ -376,38 +376,7 @@ static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
#else
id->ff_enc_c = avcodec_alloc_context3( id->ff_enc );
#endif
/* Set CPU capabilities */
id->ff_enc_c->dsp_mask = 0;
#if defined (__i386__) || defined (__x86_64__)
if( !vlc_CPU_MMX() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_MMX;
if( !vlc_CPU_MMXEXT() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_MMX2;
if( !vlc_CPU_3dNOW() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_3DNOW;
if( !vlc_CPU_SSE() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE;
if( !vlc_cpu_SSE2() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE2;
# ifdef AV_CPU_FLAG_SSE3
if( !vlc_CPU_SSE3() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE3;
# endif
# ifdef AV_CPU_FLAG_SSSE3
if( !vlc_CPU_SSSE3() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSSE3;
# endif
# ifdef AV_CPU_FLAG_SSE4
if( !vlc_CPU_SSE4_1() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE4;
# endif
# ifdef AV_CPU_FLAG_SSE42
if( !vlc_CPU_SSE4_2() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE42;
# endif
#endif
id->ff_enc_c->dsp_mask = GetVlcDspMask();
id->ff_enc_c->sample_rate = p_fmt->audio.i_rate;
id->ff_enc_c->time_base.num = 1;
id->ff_enc_c->time_base.den = p_fmt->audio.i_rate;
@ -796,38 +765,7 @@ static mtime_t VideoCommand( sout_stream_t *p_stream, sout_stream_id_t *id )
#else
id->ff_enc_c = avcodec_alloc_context3( id->ff_enc );
#endif
/* Set CPU capabilities */
id->ff_enc_c->dsp_mask = 0;
#if defined (__i386__) || defined (__x86_64__)
if( !vlc_CPU_MMX() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_MMX;
if( !vlc_CPU_MMXEXT() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_MMX2;
if( !vlc_CPU_3dNOW() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_3DNOW;
if( !vlc_CPU_SSE() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE;
if( !vlc_CPU_SSE2() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE2;
# ifdef AV_CPU_FLAG_SSE3
if( !vlc_CPU_SSE3() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE3;
# endif
# ifdef AV_CPU_FLAG_SSSE3
if( !vlc_CPU_SSSE3() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSSE3;
# endif
# ifdef AV_CPU_FLAG_SSE4
if( !vlc_CPU_SSE4_1() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE4;
# endif
# ifdef AV_CPU_FLAG_SSE42
if( !vlc_CPU_SSE4_2() )
id->ff_enc_c->dsp_mask |= AV_CPU_FLAG_SSE42;
# endif
#endif
id->ff_enc_c->dsp_mask = GetVlcDspMask();
id->ff_enc_c->width = p_sys->p_pictures[p_sys->i_cmd-1].format.i_width;
id->ff_enc_c->height = p_sys->p_pictures[p_sys->i_cmd-1].format.i_height;
av_reduce( &i_aspect_num, &i_aspect_den,