00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/avstring.h"
00029 #include "libavutil/avassert.h"
00030 #include "libavutil/libm.h"
00031 #include "libavutil/samplefmt.h"
00032 #include "audioconvert.h"
00033
00034
00035 #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt
00036
00037
00038 #define CONV_FUNC(ofmt, otype, ifmt, expr)\
00039 static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *po, const uint8_t *pi, int is, int os, uint8_t *end)\
00040 {\
00041 uint8_t *end2 = end - 3*os;\
00042 while(po < end2){\
00043 *(otype*)po = expr; pi += is; po += os;\
00044 *(otype*)po = expr; pi += is; po += os;\
00045 *(otype*)po = expr; pi += is; po += os;\
00046 *(otype*)po = expr; pi += is; po += os;\
00047 }\
00048 while(po < end){\
00049 *(otype*)po = expr; pi += is; po += os;\
00050 }\
00051 }
00052
00053
00054 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 , *(const uint8_t*)pi)
00055 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
00056 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
00057 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0f/ (1<<7)))
00058 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
00059 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
00060 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi)
00061 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi<<16)
00062 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0f/ (1<<15)))
00063 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
00064 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
00065 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi>>16)
00066 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi)
00067 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0f/ (1U<<31)))
00068 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31)))
00069 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8( lrintf(*(const float*)pi * (1<<7)) + 0x80))
00070 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16( lrintf(*(const float*)pi * (1<<15))))
00071 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31))))
00072 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_FLT, *(const float*)pi)
00073 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi)
00074 CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8( lrint(*(const double*)pi * (1<<7)) + 0x80))
00075 CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16( lrint(*(const double*)pi * (1<<15))))
00076 CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31))))
00077 CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_DBL, *(const double*)pi)
00078 CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi)
00079
00080 #define FMT_PAIR_FUNC(out, in) [out + AV_SAMPLE_FMT_NB*in] = CONV_FUNC_NAME(out, in)
00081
00082 static conv_func_type * const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB*AV_SAMPLE_FMT_NB] = {
00083 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_U8 ),
00084 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8 ),
00085 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8 ),
00086 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8 ),
00087 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8 ),
00088 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_S16),
00089 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16),
00090 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16),
00091 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16),
00092 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16),
00093 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_S32),
00094 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32),
00095 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32),
00096 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32),
00097 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32),
00098 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_FLT),
00099 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT),
00100 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT),
00101 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT),
00102 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT),
00103 FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_DBL),
00104 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL),
00105 FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL),
00106 FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL),
00107 FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL),
00108 };
00109
00110 static void cpy1(uint8_t **dst, const uint8_t **src, int len){
00111 memcpy(*dst, *src, len);
00112 }
00113 static void cpy2(uint8_t **dst, const uint8_t **src, int len){
00114 memcpy(*dst, *src, 2*len);
00115 }
00116 static void cpy4(uint8_t **dst, const uint8_t **src, int len){
00117 memcpy(*dst, *src, 4*len);
00118 }
00119 static void cpy8(uint8_t **dst, const uint8_t **src, int len){
00120 memcpy(*dst, *src, 8*len);
00121 }
00122
00123 AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt,
00124 enum AVSampleFormat in_fmt,
00125 int channels, const int *ch_map,
00126 int flags)
00127 {
00128 AudioConvert *ctx;
00129 conv_func_type *f = fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt) + AV_SAMPLE_FMT_NB*av_get_packed_sample_fmt(in_fmt)];
00130
00131 if (!f)
00132 return NULL;
00133 ctx = av_mallocz(sizeof(*ctx));
00134 if (!ctx)
00135 return NULL;
00136
00137 if(channels == 1){
00138 in_fmt = av_get_planar_sample_fmt( in_fmt);
00139 out_fmt = av_get_planar_sample_fmt(out_fmt);
00140 }
00141
00142 ctx->channels = channels;
00143 ctx->conv_f = f;
00144 ctx->ch_map = ch_map;
00145 if (in_fmt == AV_SAMPLE_FMT_U8 || in_fmt == AV_SAMPLE_FMT_U8P)
00146 memset(ctx->silence, 0x80, sizeof(ctx->silence));
00147
00148 if(out_fmt == in_fmt && !ch_map) {
00149 switch(av_get_bytes_per_sample(in_fmt)){
00150 case 1:ctx->simd_f = cpy1; break;
00151 case 2:ctx->simd_f = cpy2; break;
00152 case 4:ctx->simd_f = cpy4; break;
00153 case 8:ctx->simd_f = cpy8; break;
00154 }
00155 }
00156
00157 if(HAVE_YASM && HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);
00158
00159 return ctx;
00160 }
00161
00162 void swri_audio_convert_free(AudioConvert **ctx)
00163 {
00164 av_freep(ctx);
00165 }
00166
00167 int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len)
00168 {
00169 int ch;
00170 int off=0;
00171 const int os= (out->planar ? 1 :out->ch_count) *out->bps;
00172
00173 av_assert0(ctx->channels == out->ch_count);
00174
00175
00176
00177 if(ctx->simd_f && !ctx->ch_map){
00178 off = len/16 * 16;
00179 av_assert1(off>=0);
00180 av_assert1(off<=len);
00181 if(off>0){
00182 if(out->planar == in->planar){
00183 int planes = out->planar ? out->ch_count : 1;
00184 for(ch=0; ch<planes; ch++){
00185 ctx->simd_f(out->ch+ch, in->ch+ch, off * (out->planar ? 1 :out->ch_count));
00186 }
00187 }else{
00188 ctx->simd_f(out->ch, in->ch, off);
00189 }
00190 }
00191 if(off == len)
00192 return 0;
00193 }
00194
00195 for(ch=0; ch<ctx->channels; ch++){
00196 const int ich= ctx->ch_map ? ctx->ch_map[ch] : ch;
00197 const int is= ich < 0 ? 0 : (in->planar ? 1 : in->ch_count) * in->bps;
00198 const uint8_t *pi= ich < 0 ? ctx->silence : in->ch[ich];
00199 uint8_t *po= out->ch[ch];
00200 uint8_t *end= po + os*len;
00201 if(!po)
00202 continue;
00203 ctx->conv_f(po+off*os, pi+off*is, is, os, end);
00204 }
00205 return 0;
00206 }