Libav 0.7.1
|
00001 /* 00002 * audio conversion 00003 * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include "libavutil/avstring.h" 00029 #include "libavutil/libm.h" 00030 #include "libavutil/samplefmt.h" 00031 #include "avcodec.h" 00032 #include "audioconvert.h" 00033 00034 #if FF_API_OLD_SAMPLE_FMT 00035 const char *avcodec_get_sample_fmt_name(int sample_fmt) 00036 { 00037 return av_get_sample_fmt_name(sample_fmt); 00038 } 00039 00040 enum AVSampleFormat avcodec_get_sample_fmt(const char* name) 00041 { 00042 return av_get_sample_fmt(name); 00043 } 00044 00045 void avcodec_sample_fmt_string (char *buf, int buf_size, int sample_fmt) 00046 { 00047 av_get_sample_fmt_string(buf, buf_size, sample_fmt); 00048 } 00049 #endif 00050 00051 int64_t avcodec_guess_channel_layout(int nb_channels, enum CodecID codec_id, const char *fmt_name) 00052 { 00053 switch(nb_channels) { 00054 case 1: return AV_CH_LAYOUT_MONO; 00055 case 2: return AV_CH_LAYOUT_STEREO; 00056 case 3: return AV_CH_LAYOUT_SURROUND; 00057 case 4: return AV_CH_LAYOUT_QUAD; 00058 case 5: return AV_CH_LAYOUT_5POINT0; 00059 case 6: return AV_CH_LAYOUT_5POINT1; 00060 case 8: return AV_CH_LAYOUT_7POINT1; 00061 default: return 0; 00062 } 00063 } 00064 00065 #if FF_API_OLD_AUDIOCONVERT 00066 int64_t avcodec_get_channel_layout(const char *name) 00067 { 00068 return av_get_channel_layout(name); 00069 } 00070 00071 void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout) 00072 { 00073 av_get_channel_layout_string(buf, buf_size, nb_channels, channel_layout); 00074 } 00075 00076 int avcodec_channel_layout_num_channels(int64_t channel_layout) 00077 { 00078 return av_get_channel_layout_nb_channels(channel_layout); 00079 } 00080 #endif 00081 00082 struct AVAudioConvert { 00083 int in_channels, out_channels; 00084 int fmt_pair; 00085 }; 00086 00087 AVAudioConvert *av_audio_convert_alloc(enum AVSampleFormat out_fmt, int out_channels, 00088 enum AVSampleFormat in_fmt, int in_channels, 00089 const float *matrix, int flags) 00090 { 00091 AVAudioConvert *ctx; 00092 if (in_channels!=out_channels) 00093 return NULL; /* FIXME: not supported */ 00094 ctx = av_malloc(sizeof(AVAudioConvert)); 00095 if (!ctx) 00096 return NULL; 00097 ctx->in_channels = in_channels; 00098 ctx->out_channels = out_channels; 00099 ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt; 00100 return ctx; 00101 } 00102 00103 void av_audio_convert_free(AVAudioConvert *ctx) 00104 { 00105 av_free(ctx); 00106 } 00107 00108 int av_audio_convert(AVAudioConvert *ctx, 00109 void * const out[6], const int out_stride[6], 00110 const void * const in[6], const int in_stride[6], int len) 00111 { 00112 int ch; 00113 00114 //FIXME optimize common cases 00115 00116 for(ch=0; ch<ctx->out_channels; ch++){ 00117 const int is= in_stride[ch]; 00118 const int os= out_stride[ch]; 00119 const uint8_t *pi= in[ch]; 00120 uint8_t *po= out[ch]; 00121 uint8_t *end= po + os*len; 00122 if(!out[ch]) 00123 continue; 00124 00125 #define CONV(ofmt, otype, ifmt, expr)\ 00126 if(ctx->fmt_pair == ofmt + AV_SAMPLE_FMT_NB*ifmt){\ 00127 do{\ 00128 *(otype*)po = expr; pi += is; po += os;\ 00129 }while(po < end);\ 00130 } 00131 00132 //FIXME put things below under ifdefs so we do not waste space for cases no codec will need 00133 //FIXME rounding ? 00134 00135 CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 , *(const uint8_t*)pi) 00136 else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8) 00137 else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24) 00138 else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7))) 00139 else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7))) 00140 else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80) 00141 else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi) 00142 else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi<<16) 00143 else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15))) 00144 else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15))) 00145 else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80) 00146 else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi>>16) 00147 else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi) 00148 else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31))) 00149 else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31))) 00150 else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8( lrintf(*(const float*)pi * (1<<7)) + 0x80)) 00151 else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16( lrintf(*(const float*)pi * (1<<15)))) 00152 else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31)))) 00153 else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_FLT, *(const float*)pi) 00154 else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi) 00155 else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8( lrint(*(const double*)pi * (1<<7)) + 0x80)) 00156 else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16( lrint(*(const double*)pi * (1<<15)))) 00157 else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31)))) 00158 else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_DBL, *(const double*)pi) 00159 else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi) 00160 else return -1; 00161 } 00162 return 0; 00163 }