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