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 "avcodec.h"
00030 #include "audioconvert.h"
00031
00032 typedef struct SampleFmtInfo {
00033 const char *name;
00034 int bits;
00035 } SampleFmtInfo;
00036
00038 static const SampleFmtInfo sample_fmt_info[SAMPLE_FMT_NB] = {
00039 [SAMPLE_FMT_U8] = { .name = "u8", .bits = 8 },
00040 [SAMPLE_FMT_S16] = { .name = "s16", .bits = 16 },
00041 [SAMPLE_FMT_S32] = { .name = "s32", .bits = 32 },
00042 [SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32 },
00043 [SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64 },
00044 };
00045
00046 const char *avcodec_get_sample_fmt_name(int sample_fmt)
00047 {
00048 if (sample_fmt < 0 || sample_fmt >= SAMPLE_FMT_NB)
00049 return NULL;
00050 return sample_fmt_info[sample_fmt].name;
00051 }
00052
00053 enum SampleFormat avcodec_get_sample_fmt(const char* name)
00054 {
00055 int i;
00056
00057 for (i=0; i < SAMPLE_FMT_NB; i++)
00058 if (!strcmp(sample_fmt_info[i].name, name))
00059 return i;
00060 return SAMPLE_FMT_NONE;
00061 }
00062
00063 void avcodec_sample_fmt_string (char *buf, int buf_size, int sample_fmt)
00064 {
00065
00066 if (sample_fmt < 0)
00067 snprintf (buf, buf_size, "name " " depth");
00068 else if (sample_fmt < SAMPLE_FMT_NB) {
00069 SampleFmtInfo info= sample_fmt_info[sample_fmt];
00070 snprintf (buf, buf_size, "%-6s" " %2d ", info.name, info.bits);
00071 }
00072 }
00073
00074 static const char* const channel_names[]={
00075 "FL", "FR", "FC", "LFE", "BL", "BR", "FLC", "FRC",
00076 "BC", "SL", "SR", "TC", "TFL", "TFC", "TFR", "TBL",
00077 "TBC", "TBR",
00078 [29] = "DL",
00079 [30] = "DR",
00080 };
00081
00082 const char *get_channel_name(int channel_id)
00083 {
00084 if (channel_id<0 || channel_id>=FF_ARRAY_ELEMS(channel_names))
00085 return NULL;
00086 return channel_names[channel_id];
00087 }
00088
00089 int64_t avcodec_guess_channel_layout(int nb_channels, enum CodecID codec_id, const char *fmt_name)
00090 {
00091 switch(nb_channels) {
00092 case 1: return CH_LAYOUT_MONO;
00093 case 2: return CH_LAYOUT_STEREO;
00094 case 3: return CH_LAYOUT_SURROUND;
00095 case 4: return CH_LAYOUT_QUAD;
00096 case 5: return CH_LAYOUT_5POINT0;
00097 case 6: return CH_LAYOUT_5POINT1;
00098 case 8: return CH_LAYOUT_7POINT1;
00099 default: return 0;
00100 }
00101 }
00102
00103 static const struct {
00104 const char *name;
00105 int nb_channels;
00106 int64_t layout;
00107 } channel_layout_map[] = {
00108 { "mono", 1, CH_LAYOUT_MONO },
00109 { "stereo", 2, CH_LAYOUT_STEREO },
00110 { "surround", 3, CH_LAYOUT_SURROUND },
00111 { "quad", 4, CH_LAYOUT_QUAD },
00112 { "5.0", 5, CH_LAYOUT_5POINT0 },
00113 { "5.1", 6, CH_LAYOUT_5POINT1 },
00114 { "5.1+downmix", 8, CH_LAYOUT_5POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
00115 { "7.1", 8, CH_LAYOUT_7POINT1 },
00116 { "7.1(wide)", 8, CH_LAYOUT_7POINT1_WIDE },
00117 { "7.1+downmix", 10, CH_LAYOUT_7POINT1|CH_LAYOUT_STEREO_DOWNMIX, },
00118 { 0 }
00119 };
00120
00121 void avcodec_get_channel_layout_string(char *buf, int buf_size, int nb_channels, int64_t channel_layout)
00122 {
00123 int i;
00124
00125 if (channel_layout==0)
00126 channel_layout = avcodec_guess_channel_layout(nb_channels, CODEC_ID_NONE, NULL);
00127
00128 for (i=0; channel_layout_map[i].name; i++)
00129 if (nb_channels == channel_layout_map[i].nb_channels &&
00130 channel_layout == channel_layout_map[i].layout) {
00131 av_strlcpy(buf, channel_layout_map[i].name, buf_size);
00132 return;
00133 }
00134
00135 snprintf(buf, buf_size, "%d channels", nb_channels);
00136 if (channel_layout) {
00137 int i,ch;
00138 av_strlcat(buf, " (", buf_size);
00139 for(i=0,ch=0; i<64; i++) {
00140 if ((channel_layout & (1L<<i))) {
00141 const char *name = get_channel_name(i);
00142 if (name) {
00143 if (ch>0) av_strlcat(buf, "|", buf_size);
00144 av_strlcat(buf, name, buf_size);
00145 }
00146 ch++;
00147 }
00148 }
00149 av_strlcat(buf, ")", buf_size);
00150 }
00151 }
00152
00153 struct AVAudioConvert {
00154 int in_channels, out_channels;
00155 int fmt_pair;
00156 };
00157
00158 AVAudioConvert *av_audio_convert_alloc(enum SampleFormat out_fmt, int out_channels,
00159 enum SampleFormat in_fmt, int in_channels,
00160 const float *matrix, int flags)
00161 {
00162 AVAudioConvert *ctx;
00163 if (in_channels!=out_channels)
00164 return NULL;
00165 ctx = av_malloc(sizeof(AVAudioConvert));
00166 if (!ctx)
00167 return NULL;
00168 ctx->in_channels = in_channels;
00169 ctx->out_channels = out_channels;
00170 ctx->fmt_pair = out_fmt + SAMPLE_FMT_NB*in_fmt;
00171 return ctx;
00172 }
00173
00174 void av_audio_convert_free(AVAudioConvert *ctx)
00175 {
00176 av_free(ctx);
00177 }
00178
00179 int av_audio_convert(AVAudioConvert *ctx,
00180 void * const out[6], const int out_stride[6],
00181 const void * const in[6], const int in_stride[6], int len)
00182 {
00183 int ch;
00184
00185
00186
00187 for(ch=0; ch<ctx->out_channels; ch++){
00188 const int is= in_stride[ch];
00189 const int os= out_stride[ch];
00190 const uint8_t *pi= in[ch];
00191 uint8_t *po= out[ch];
00192 uint8_t *end= po + os*len;
00193 if(!out[ch])
00194 continue;
00195
00196 #define CONV(ofmt, otype, ifmt, expr)\
00197 if(ctx->fmt_pair == ofmt + SAMPLE_FMT_NB*ifmt){\
00198 do{\
00199 *(otype*)po = expr; pi += is; po += os;\
00200 }while(po < end);\
00201 }
00202
00203
00204
00205
00206 CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_U8 , *(const uint8_t*)pi)
00207 else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
00208 else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
00209 else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
00210 else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
00211 else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
00212 else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_S16, *(const int16_t*)pi)
00213 else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_S16, *(const int16_t*)pi<<16)
00214 else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
00215 else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
00216 else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
00217 else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_S32, *(const int32_t*)pi>>16)
00218 else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_S32, *(const int32_t*)pi)
00219 else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1<<31)))
00220 else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1<<31)))
00221 else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<7)) + 0x80)
00222 else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<15)))
00223 else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_FLT, lrintf(*(const float*)pi * (1<<31)))
00224 else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_FLT, *(const float*)pi)
00225 else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_FLT, *(const float*)pi)
00226 else CONV(SAMPLE_FMT_U8 , uint8_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<7)) + 0x80)
00227 else CONV(SAMPLE_FMT_S16, int16_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<15)))
00228 else CONV(SAMPLE_FMT_S32, int32_t, SAMPLE_FMT_DBL, lrint(*(const double*)pi * (1<<31)))
00229 else CONV(SAMPLE_FMT_FLT, float , SAMPLE_FMT_DBL, *(const double*)pi)
00230 else CONV(SAMPLE_FMT_DBL, double , SAMPLE_FMT_DBL, *(const double*)pi)
00231 else return -1;
00232 }
00233 return 0;
00234 }