From 0fbbb451f80b57ed92f50b85161b9ef2605fe436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Denis-Courmont?= Date: Sat, 4 Aug 2012 17:01:09 +0300 Subject: [PATCH] avcodec: factor CPU detection code In retrospect, I should have done that first. Oh well... --- modules/codec/avcodec/Modules.am | 1 + modules/codec/avcodec/avcodec.c | 32 +------------ modules/codec/avcodec/avcodec.h | 2 + modules/codec/avcodec/cpu.c | 79 ++++++++++++++++++++++++++++++++ modules/codec/avcodec/encoder.c | 32 +------------ modules/stream_out/Modules.am | 2 +- modules/stream_out/switcher.c | 66 +------------------------- 7 files changed, 87 insertions(+), 127 deletions(-) create mode 100644 modules/codec/avcodec/cpu.c diff --git a/modules/codec/avcodec/Modules.am b/modules/codec/avcodec/Modules.am index 4d32ac572c..2e48844924 100644 --- a/modules/codec/avcodec/Modules.am +++ b/modules/codec/avcodec/Modules.am @@ -5,6 +5,7 @@ libavcodec_plugin_la_SOURCES = \ video.c \ subtitle.c \ audio.c \ + cpu.c \ deinterlace.c \ fourcc.c \ chroma.h \ diff --git a/modules/codec/avcodec/avcodec.c b/modules/codec/avcodec/avcodec.c index d79d6b0c79..d23a46b744 100644 --- a/modules/codec/avcodec/avcodec.c +++ b/modules/codec/avcodec/avcodec.c @@ -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 ) diff --git a/modules/codec/avcodec/avcodec.h b/modules/codec/avcodec/avcodec.h index b05db726c4..e5a58254fc 100644 --- a/modules/codec/avcodec/avcodec.h +++ b/modules/codec/avcodec/avcodec.h @@ -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 ** ); diff --git a/modules/codec/avcodec/cpu.c b/modules/codec/avcodec/cpu.c new file mode 100644 index 0000000000..1b10aaafb3 --- /dev/null +++ b/modules/codec/avcodec/cpu.c @@ -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 +#include + +#define HAVE_MMX 1 +#ifdef HAVE_LIBAVCODEC_AVCODEC_H +# include +#else +# include +#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; +} diff --git a/modules/codec/avcodec/encoder.c b/modules/codec/avcodec/encoder.c index 018ca3c953..9a4db76f89 100644 --- a/modules/codec/avcodec/encoder.c +++ b/modules/codec/avcodec/encoder.c @@ -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" ); diff --git a/modules/stream_out/Modules.am b/modules/stream_out/Modules.am index c8e7da471d..41aad3bd2a 100644 --- a/modules/stream_out/Modules.am +++ b/modules/stream_out/Modules.am @@ -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 diff --git a/modules/stream_out/switcher.c b/modules/stream_out/switcher.c index ac7945f77e..131ad88b21 100644 --- a/modules/stream_out/switcher.c +++ b/modules/stream_out/switcher.c @@ -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,