yading@11: /* yading@11: * audio conversion yading@11: * Copyright (c) 2006 Michael Niedermayer yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * audio conversion yading@11: * @author Michael Niedermayer yading@11: */ yading@11: yading@11: #include "libavutil/avstring.h" yading@11: #include "libavutil/avassert.h" yading@11: #include "libavutil/libm.h" yading@11: #include "libavutil/samplefmt.h" yading@11: #include "audioconvert.h" yading@11: yading@11: yading@11: #define CONV_FUNC_NAME(dst_fmt, src_fmt) conv_ ## src_fmt ## _to_ ## dst_fmt yading@11: yading@11: //FIXME rounding ? yading@11: #define CONV_FUNC(ofmt, otype, ifmt, expr)\ yading@11: static void CONV_FUNC_NAME(ofmt, ifmt)(uint8_t *po, const uint8_t *pi, int is, int os, uint8_t *end)\ yading@11: {\ yading@11: uint8_t *end2 = end - 3*os;\ yading@11: while(po < end2){\ yading@11: *(otype*)po = expr; pi += is; po += os;\ yading@11: *(otype*)po = expr; pi += is; po += os;\ yading@11: *(otype*)po = expr; pi += is; po += os;\ yading@11: *(otype*)po = expr; pi += is; po += os;\ yading@11: }\ yading@11: while(po < end){\ yading@11: *(otype*)po = expr; pi += is; po += os;\ yading@11: }\ yading@11: } yading@11: yading@11: //FIXME put things below under ifdefs so we do not waste space for cases no codec will need yading@11: CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 , *(const uint8_t*)pi) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24) yading@11: CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0f/ (1<<7))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi<<16) yading@11: CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0f/ (1<<15))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi>>16) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi) yading@11: CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0f/ (1U<<31))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8( lrintf(*(const float*)pi * (1<<7)) + 0x80)) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16( lrintf(*(const float*)pi * (1<<15)))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31)))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_FLT, *(const float*)pi) yading@11: CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi) yading@11: CONV_FUNC(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8( lrint(*(const double*)pi * (1<<7)) + 0x80)) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16( lrint(*(const double*)pi * (1<<15)))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31)))) yading@11: CONV_FUNC(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_DBL, *(const double*)pi) yading@11: CONV_FUNC(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi) yading@11: yading@11: #define FMT_PAIR_FUNC(out, in) [out + AV_SAMPLE_FMT_NB*in] = CONV_FUNC_NAME(out, in) yading@11: yading@11: static conv_func_type * const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB*AV_SAMPLE_FMT_NB] = { yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_U8 ), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8 ), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8 ), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8 ), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8 ), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_S16), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_S32), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_FLT), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8 , AV_SAMPLE_FMT_DBL), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), yading@11: FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), yading@11: }; yading@11: yading@11: static void cpy1(uint8_t **dst, const uint8_t **src, int len){ yading@11: memcpy(*dst, *src, len); yading@11: } yading@11: static void cpy2(uint8_t **dst, const uint8_t **src, int len){ yading@11: memcpy(*dst, *src, 2*len); yading@11: } yading@11: static void cpy4(uint8_t **dst, const uint8_t **src, int len){ yading@11: memcpy(*dst, *src, 4*len); yading@11: } yading@11: static void cpy8(uint8_t **dst, const uint8_t **src, int len){ yading@11: memcpy(*dst, *src, 8*len); yading@11: } yading@11: yading@11: AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, yading@11: enum AVSampleFormat in_fmt, yading@11: int channels, const int *ch_map, yading@11: int flags) yading@11: { yading@11: AudioConvert *ctx; yading@11: 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)]; yading@11: yading@11: if (!f) yading@11: return NULL; yading@11: ctx = av_mallocz(sizeof(*ctx)); yading@11: if (!ctx) yading@11: return NULL; yading@11: yading@11: if(channels == 1){ yading@11: in_fmt = av_get_planar_sample_fmt( in_fmt); yading@11: out_fmt = av_get_planar_sample_fmt(out_fmt); yading@11: } yading@11: yading@11: ctx->channels = channels; yading@11: ctx->conv_f = f; yading@11: ctx->ch_map = ch_map; yading@11: if (in_fmt == AV_SAMPLE_FMT_U8 || in_fmt == AV_SAMPLE_FMT_U8P) yading@11: memset(ctx->silence, 0x80, sizeof(ctx->silence)); yading@11: yading@11: if(out_fmt == in_fmt && !ch_map) { yading@11: switch(av_get_bytes_per_sample(in_fmt)){ yading@11: case 1:ctx->simd_f = cpy1; break; yading@11: case 2:ctx->simd_f = cpy2; break; yading@11: case 4:ctx->simd_f = cpy4; break; yading@11: case 8:ctx->simd_f = cpy8; break; yading@11: } yading@11: } yading@11: yading@11: if(HAVE_YASM && HAVE_MMX) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels); yading@11: if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels); yading@11: yading@11: return ctx; yading@11: } yading@11: yading@11: void swri_audio_convert_free(AudioConvert **ctx) yading@11: { yading@11: av_freep(ctx); yading@11: } yading@11: yading@11: int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) yading@11: { yading@11: int ch; yading@11: int off=0; yading@11: const int os= (out->planar ? 1 :out->ch_count) *out->bps; yading@11: unsigned misaligned = 0; yading@11: yading@11: av_assert0(ctx->channels == out->ch_count); yading@11: yading@11: if (ctx->in_simd_align_mask) { yading@11: int planes = in->planar ? in->ch_count : 1; yading@11: unsigned m = 0; yading@11: for (ch = 0; ch < planes; ch++) yading@11: m |= (intptr_t)in->ch[ch]; yading@11: misaligned |= m & ctx->in_simd_align_mask; yading@11: } yading@11: if (ctx->out_simd_align_mask) { yading@11: int planes = out->planar ? out->ch_count : 1; yading@11: unsigned m = 0; yading@11: for (ch = 0; ch < planes; ch++) yading@11: m |= (intptr_t)out->ch[ch]; yading@11: misaligned |= m & ctx->out_simd_align_mask; yading@11: } yading@11: yading@11: //FIXME optimize common cases yading@11: yading@11: if(ctx->simd_f && !ctx->ch_map && !misaligned){ yading@11: off = len&~15; yading@11: av_assert1(off>=0); yading@11: av_assert1(off<=len); yading@11: av_assert2(ctx->channels == SWR_CH_MAX || !in->ch[ctx->channels]); yading@11: if(off>0){ yading@11: if(out->planar == in->planar){ yading@11: int planes = out->planar ? out->ch_count : 1; yading@11: for(ch=0; chsimd_f(out->ch+ch, (const uint8_t **)in->ch+ch, off * (out->planar ? 1 :out->ch_count)); yading@11: } yading@11: }else{ yading@11: ctx->simd_f(out->ch, (const uint8_t **)in->ch, off); yading@11: } yading@11: } yading@11: if(off == len) yading@11: return 0; yading@11: } yading@11: yading@11: for(ch=0; chchannels; ch++){ yading@11: const int ich= ctx->ch_map ? ctx->ch_map[ch] : ch; yading@11: const int is= ich < 0 ? 0 : (in->planar ? 1 : in->ch_count) * in->bps; yading@11: const uint8_t *pi= ich < 0 ? ctx->silence : in->ch[ich]; yading@11: uint8_t *po= out->ch[ch]; yading@11: uint8_t *end= po + os*len; yading@11: if(!po) yading@11: continue; yading@11: ctx->conv_f(po+off*os, pi+off*is, is, os, end); yading@11: } yading@11: return 0; yading@11: }