00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdint.h>
00023 #include <stdio.h>
00024
00025 #include "libavutil/avstring.h"
00026 #include "libavutil/common.h"
00027 #include "libavutil/lfg.h"
00028 #include "libavutil/libm.h"
00029 #include "libavutil/log.h"
00030 #include "libavutil/mem.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/samplefmt.h"
00033 #include "avresample.h"
00034
00035 static double dbl_rand(AVLFG *lfg)
00036 {
00037 return 2.0 * (av_lfg_get(lfg) / (double)UINT_MAX) - 1.0;
00038 }
00039
00040 #define PUT_FUNC(name, fmt, type, expr) \
00041 static void put_sample_ ## name(void **data, enum AVSampleFormat sample_fmt,\
00042 int channels, int sample, int ch, \
00043 double v_dbl) \
00044 { \
00045 type v = expr; \
00046 type **out = (type **)data; \
00047 if (av_sample_fmt_is_planar(sample_fmt)) \
00048 out[ch][sample] = v; \
00049 else \
00050 out[0][sample * channels + ch] = v; \
00051 }
00052
00053 PUT_FUNC(u8, AV_SAMPLE_FMT_U8, uint8_t, av_clip_uint8 ( lrint(v_dbl * (1 << 7)) + 128))
00054 PUT_FUNC(s16, AV_SAMPLE_FMT_S16, int16_t, av_clip_int16 ( lrint(v_dbl * (1 << 15))))
00055 PUT_FUNC(s32, AV_SAMPLE_FMT_S32, int32_t, av_clipl_int32(llrint(v_dbl * (1U << 31))))
00056 PUT_FUNC(flt, AV_SAMPLE_FMT_FLT, float, v_dbl)
00057 PUT_FUNC(dbl, AV_SAMPLE_FMT_DBL, double, v_dbl)
00058
00059 static void put_sample(void **data, enum AVSampleFormat sample_fmt,
00060 int channels, int sample, int ch, double v_dbl)
00061 {
00062 switch (av_get_packed_sample_fmt(sample_fmt)) {
00063 case AV_SAMPLE_FMT_U8:
00064 put_sample_u8(data, sample_fmt, channels, sample, ch, v_dbl);
00065 break;
00066 case AV_SAMPLE_FMT_S16:
00067 put_sample_s16(data, sample_fmt, channels, sample, ch, v_dbl);
00068 break;
00069 case AV_SAMPLE_FMT_S32:
00070 put_sample_s32(data, sample_fmt, channels, sample, ch, v_dbl);
00071 break;
00072 case AV_SAMPLE_FMT_FLT:
00073 put_sample_flt(data, sample_fmt, channels, sample, ch, v_dbl);
00074 break;
00075 case AV_SAMPLE_FMT_DBL:
00076 put_sample_dbl(data, sample_fmt, channels, sample, ch, v_dbl);
00077 break;
00078 }
00079 }
00080
00081 static void audiogen(AVLFG *rnd, void **data, enum AVSampleFormat sample_fmt,
00082 int channels, int sample_rate, int nb_samples)
00083 {
00084 int i, ch, k;
00085 double v, f, a, ampa;
00086 double tabf1[AVRESAMPLE_MAX_CHANNELS];
00087 double tabf2[AVRESAMPLE_MAX_CHANNELS];
00088 double taba[AVRESAMPLE_MAX_CHANNELS];
00089
00090 #define PUT_SAMPLE put_sample(data, sample_fmt, channels, k, ch, v);
00091
00092 k = 0;
00093
00094
00095 a = 0;
00096 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
00097 v = sin(a) * 0.30;
00098 for (ch = 0; ch < channels; ch++)
00099 PUT_SAMPLE
00100 a += M_PI * 1000.0 * 2.0 / sample_rate;
00101 }
00102
00103
00104 a = 0;
00105 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
00106 v = sin(a) * 0.30;
00107 for (ch = 0; ch < channels; ch++)
00108 PUT_SAMPLE
00109 f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
00110 a += M_PI * f * 2.0 / sample_rate;
00111 }
00112
00113
00114 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
00115 v = dbl_rand(rnd) * 0.30;
00116 for (ch = 0; ch < channels; ch++)
00117 PUT_SAMPLE
00118 }
00119
00120
00121 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
00122 v = dbl_rand(rnd);
00123 for (ch = 0; ch < channels; ch++)
00124 PUT_SAMPLE
00125 }
00126
00127
00128 for (ch = 0; ch < channels; ch++) {
00129 taba[ch] = 0;
00130 tabf1[ch] = 100 + av_lfg_get(rnd) % 5000;
00131 tabf2[ch] = 100 + av_lfg_get(rnd) % 5000;
00132 }
00133 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
00134 for (ch = 0; ch < channels; ch++) {
00135 v = sin(taba[ch]) * 0.30;
00136 PUT_SAMPLE
00137 f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
00138 taba[ch] += M_PI * f * 2.0 / sample_rate;
00139 }
00140 }
00141
00142
00143 a = 0;
00144 ampa = 0;
00145 for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
00146 for (ch = 0; ch < channels; ch++) {
00147 double amp = (1.0 + sin(ampa)) * 0.15;
00148 if (ch & 1)
00149 amp = 0.30 - amp;
00150 v = sin(a) * amp;
00151 PUT_SAMPLE
00152 a += M_PI * 500.0 * 2.0 / sample_rate;
00153 ampa += M_PI * 2.0 / sample_rate;
00154 }
00155 }
00156 }
00157
00158
00159
00160
00161
00162 static const enum AVSampleFormat formats[] = {
00163 AV_SAMPLE_FMT_S16,
00164 AV_SAMPLE_FMT_FLTP,
00165 AV_SAMPLE_FMT_S16P,
00166 AV_SAMPLE_FMT_FLT,
00167 AV_SAMPLE_FMT_S32P,
00168 AV_SAMPLE_FMT_S32,
00169 AV_SAMPLE_FMT_U8P,
00170 AV_SAMPLE_FMT_U8,
00171 AV_SAMPLE_FMT_DBLP,
00172 AV_SAMPLE_FMT_DBL,
00173 };
00174
00175 static const int rates[] = {
00176 48000,
00177 44100,
00178 16000
00179 };
00180
00181 static const uint64_t layouts[] = {
00182 AV_CH_LAYOUT_STEREO,
00183 AV_CH_LAYOUT_MONO,
00184 AV_CH_LAYOUT_5POINT1,
00185 AV_CH_LAYOUT_7POINT1,
00186 };
00187
00188 int main(int argc, char **argv)
00189 {
00190 AVAudioResampleContext *s;
00191 AVLFG rnd;
00192 int ret = 0;
00193 uint8_t *in_buf = NULL;
00194 uint8_t *out_buf = NULL;
00195 unsigned int in_buf_size;
00196 unsigned int out_buf_size;
00197 uint8_t *in_data[AVRESAMPLE_MAX_CHANNELS] = { 0 };
00198 uint8_t *out_data[AVRESAMPLE_MAX_CHANNELS] = { 0 };
00199 int in_linesize;
00200 int out_linesize;
00201 uint64_t in_ch_layout;
00202 int in_channels;
00203 enum AVSampleFormat in_fmt;
00204 int in_rate;
00205 uint64_t out_ch_layout;
00206 int out_channels;
00207 enum AVSampleFormat out_fmt;
00208 int out_rate;
00209 int num_formats, num_rates, num_layouts;
00210 int i, j, k, l, m, n;
00211
00212 num_formats = 2;
00213 num_rates = 2;
00214 num_layouts = 2;
00215 if (argc > 1) {
00216 if (!av_strncasecmp(argv[1], "-h", 3)) {
00217 av_log(NULL, AV_LOG_INFO, "Usage: avresample-test [<num formats> "
00218 "[<num sample rates> [<num channel layouts>]]]\n"
00219 "Default is 2 2 2\n");
00220 return 0;
00221 }
00222 num_formats = strtol(argv[1], NULL, 0);
00223 num_formats = av_clip(num_formats, 1, FF_ARRAY_ELEMS(formats));
00224 }
00225 if (argc > 2) {
00226 num_rates = strtol(argv[2], NULL, 0);
00227 num_rates = av_clip(num_rates, 1, FF_ARRAY_ELEMS(rates));
00228 }
00229 if (argc > 3) {
00230 num_layouts = strtol(argv[3], NULL, 0);
00231 num_layouts = av_clip(num_layouts, 1, FF_ARRAY_ELEMS(layouts));
00232 }
00233
00234 av_log_set_level(AV_LOG_DEBUG);
00235
00236 av_lfg_init(&rnd, 0xC0FFEE);
00237
00238 in_buf_size = av_samples_get_buffer_size(&in_linesize, 8, 48000 * 6,
00239 AV_SAMPLE_FMT_DBLP, 0);
00240 out_buf_size = in_buf_size;
00241
00242 in_buf = av_malloc(in_buf_size);
00243 if (!in_buf)
00244 goto end;
00245 out_buf = av_malloc(out_buf_size);
00246 if (!out_buf)
00247 goto end;
00248
00249 s = avresample_alloc_context();
00250 if (!s) {
00251 av_log(NULL, AV_LOG_ERROR, "Error allocating AVAudioResampleContext\n");
00252 ret = 1;
00253 goto end;
00254 }
00255
00256 for (i = 0; i < num_formats; i++) {
00257 in_fmt = formats[i];
00258 for (k = 0; k < num_layouts; k++) {
00259 in_ch_layout = layouts[k];
00260 in_channels = av_get_channel_layout_nb_channels(in_ch_layout);
00261 for (m = 0; m < num_rates; m++) {
00262 in_rate = rates[m];
00263
00264 ret = av_samples_fill_arrays(in_data, &in_linesize, in_buf,
00265 in_channels, in_rate * 6,
00266 in_fmt, 0);
00267 if (ret < 0) {
00268 av_log(s, AV_LOG_ERROR, "failed in_data fill arrays\n");
00269 goto end;
00270 }
00271 audiogen(&rnd, (void **)in_data, in_fmt, in_channels, in_rate, in_rate * 6);
00272
00273 for (j = 0; j < num_formats; j++) {
00274 out_fmt = formats[j];
00275 for (l = 0; l < num_layouts; l++) {
00276 out_ch_layout = layouts[l];
00277 out_channels = av_get_channel_layout_nb_channels(out_ch_layout);
00278 for (n = 0; n < num_rates; n++) {
00279 out_rate = rates[n];
00280
00281 av_log(NULL, AV_LOG_INFO, "%s to %s, %d to %d channels, %d Hz to %d Hz\n",
00282 av_get_sample_fmt_name(in_fmt), av_get_sample_fmt_name(out_fmt),
00283 in_channels, out_channels, in_rate, out_rate);
00284
00285 ret = av_samples_fill_arrays(out_data, &out_linesize,
00286 out_buf, out_channels,
00287 out_rate * 6, out_fmt, 0);
00288 if (ret < 0) {
00289 av_log(s, AV_LOG_ERROR, "failed out_data fill arrays\n");
00290 goto end;
00291 }
00292
00293 av_opt_set_int(s, "in_channel_layout", in_ch_layout, 0);
00294 av_opt_set_int(s, "in_sample_fmt", in_fmt, 0);
00295 av_opt_set_int(s, "in_sample_rate", in_rate, 0);
00296 av_opt_set_int(s, "out_channel_layout", out_ch_layout, 0);
00297 av_opt_set_int(s, "out_sample_fmt", out_fmt, 0);
00298 av_opt_set_int(s, "out_sample_rate", out_rate, 0);
00299
00300 av_opt_set_int(s, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
00301
00302 ret = avresample_open(s);
00303 if (ret < 0) {
00304 av_log(s, AV_LOG_ERROR, "Error opening context\n");
00305 goto end;
00306 }
00307
00308 ret = avresample_convert(s, (void **)out_data, out_linesize, out_rate * 6,
00309 (void **) in_data, in_linesize, in_rate * 6);
00310 if (ret < 0) {
00311 char errbuf[256];
00312 av_strerror(ret, errbuf, sizeof(errbuf));
00313 av_log(NULL, AV_LOG_ERROR, "%s\n", errbuf);
00314 goto end;
00315 }
00316 av_log(NULL, AV_LOG_INFO, "Converted %d samples to %d samples\n",
00317 in_rate * 6, ret);
00318 if (avresample_get_delay(s) > 0)
00319 av_log(NULL, AV_LOG_INFO, "%d delay samples not converted\n",
00320 avresample_get_delay(s));
00321 if (avresample_available(s) > 0)
00322 av_log(NULL, AV_LOG_INFO, "%d samples available for output\n",
00323 avresample_available(s));
00324 av_log(NULL, AV_LOG_INFO, "\n");
00325
00326 avresample_close(s);
00327 }
00328 }
00329 }
00330 }
00331 }
00332 }
00333
00334 ret = 0;
00335
00336 end:
00337 av_freep(&in_buf);
00338 av_freep(&out_buf);
00339 avresample_free(&s);
00340 return ret;
00341 }