Add libavresample
[libav.git] / libavresample / avresample-test.c
CommitLineData
c8af852b
JR
1/*
2 * Copyright (c) 2002 Fabrice Bellard
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdint.h>
23#include <stdio.h>
24
25#include "libavutil/avstring.h"
26#include "libavutil/lfg.h"
27#include "libavutil/libm.h"
28#include "libavutil/log.h"
29#include "libavutil/mem.h"
30#include "libavutil/opt.h"
31#include "libavutil/samplefmt.h"
32#include "avresample.h"
33
34static double dbl_rand(AVLFG *lfg)
35{
36 return 2.0 * (av_lfg_get(lfg) / (double)UINT_MAX) - 1.0;
37}
38
39#define PUT_FUNC(name, fmt, type, expr) \
40static void put_sample_ ## name(void **data, enum AVSampleFormat sample_fmt,\
41 int channels, int sample, int ch, \
42 double v_dbl) \
43{ \
44 type v = expr; \
45 type **out = (type **)data; \
46 if (av_sample_fmt_is_planar(sample_fmt)) \
47 out[ch][sample] = v; \
48 else \
49 out[0][sample * channels + ch] = v; \
50}
51
52PUT_FUNC(u8, AV_SAMPLE_FMT_U8, uint8_t, av_clip_uint8 ( lrint(v_dbl * (1 << 7)) + 128))
53PUT_FUNC(s16, AV_SAMPLE_FMT_S16, int16_t, av_clip_int16 ( lrint(v_dbl * (1 << 15))))
54PUT_FUNC(s32, AV_SAMPLE_FMT_S32, int32_t, av_clipl_int32(llrint(v_dbl * (1U << 31))))
55PUT_FUNC(flt, AV_SAMPLE_FMT_FLT, float, v_dbl)
56PUT_FUNC(dbl, AV_SAMPLE_FMT_DBL, double, v_dbl)
57
58static void put_sample(void **data, enum AVSampleFormat sample_fmt,
59 int channels, int sample, int ch, double v_dbl)
60{
61 switch (av_get_packed_sample_fmt(sample_fmt)) {
62 case AV_SAMPLE_FMT_U8:
63 put_sample_u8(data, sample_fmt, channels, sample, ch, v_dbl);
64 break;
65 case AV_SAMPLE_FMT_S16:
66 put_sample_s16(data, sample_fmt, channels, sample, ch, v_dbl);
67 break;
68 case AV_SAMPLE_FMT_S32:
69 put_sample_s32(data, sample_fmt, channels, sample, ch, v_dbl);
70 break;
71 case AV_SAMPLE_FMT_FLT:
72 put_sample_flt(data, sample_fmt, channels, sample, ch, v_dbl);
73 break;
74 case AV_SAMPLE_FMT_DBL:
75 put_sample_dbl(data, sample_fmt, channels, sample, ch, v_dbl);
76 break;
77 }
78}
79
80static void audiogen(AVLFG *rnd, void **data, enum AVSampleFormat sample_fmt,
81 int channels, int sample_rate, int nb_samples)
82{
83 int i, ch, k;
84 double v, f, a, ampa;
85 double tabf1[AVRESAMPLE_MAX_CHANNELS];
86 double tabf2[AVRESAMPLE_MAX_CHANNELS];
87 double taba[AVRESAMPLE_MAX_CHANNELS];
88
89#define PUT_SAMPLE put_sample(data, sample_fmt, channels, k, ch, v);
90
91 k = 0;
92
93 /* 1 second of single freq sinus at 1000 Hz */
94 a = 0;
95 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
96 v = sin(a) * 0.30;
97 for (ch = 0; ch < channels; ch++)
98 PUT_SAMPLE
99 a += M_PI * 1000.0 * 2.0 / sample_rate;
100 }
101
102 /* 1 second of varing frequency between 100 and 10000 Hz */
103 a = 0;
104 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
105 v = sin(a) * 0.30;
106 for (ch = 0; ch < channels; ch++)
107 PUT_SAMPLE
108 f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
109 a += M_PI * f * 2.0 / sample_rate;
110 }
111
112 /* 0.5 second of low amplitude white noise */
113 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
114 v = dbl_rand(rnd) * 0.30;
115 for (ch = 0; ch < channels; ch++)
116 PUT_SAMPLE
117 }
118
119 /* 0.5 second of high amplitude white noise */
120 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
121 v = dbl_rand(rnd);
122 for (ch = 0; ch < channels; ch++)
123 PUT_SAMPLE
124 }
125
126 /* 1 second of unrelated ramps for each channel */
127 for (ch = 0; ch < channels; ch++) {
128 taba[ch] = 0;
129 tabf1[ch] = 100 + av_lfg_get(rnd) % 5000;
130 tabf2[ch] = 100 + av_lfg_get(rnd) % 5000;
131 }
132 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
133 for (ch = 0; ch < channels; ch++) {
134 v = sin(taba[ch]) * 0.30;
135 PUT_SAMPLE
136 f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
137 taba[ch] += M_PI * f * 2.0 / sample_rate;
138 }
139 }
140
141 /* 2 seconds of 500 Hz with varying volume */
142 a = 0;
143 ampa = 0;
144 for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
145 for (ch = 0; ch < channels; ch++) {
146 double amp = (1.0 + sin(ampa)) * 0.15;
147 if (ch & 1)
148 amp = 0.30 - amp;
149 v = sin(a) * amp;
150 PUT_SAMPLE
151 a += M_PI * 500.0 * 2.0 / sample_rate;
152 ampa += M_PI * 2.0 / sample_rate;
153 }
154 }
155}
156
157/* formats, rates, and layouts are ordered for priority in testing.
158 e.g. 'avresample-test 4 2 2' will test all input/output combinations of
159 S16/FLTP/S16P/FLT, 48000/44100, and stereo/mono */
160
161static const enum AVSampleFormat formats[] = {
162 AV_SAMPLE_FMT_S16,
163 AV_SAMPLE_FMT_FLTP,
164 AV_SAMPLE_FMT_S16P,
165 AV_SAMPLE_FMT_FLT,
166 AV_SAMPLE_FMT_S32P,
167 AV_SAMPLE_FMT_S32,
168 AV_SAMPLE_FMT_U8P,
169 AV_SAMPLE_FMT_U8,
170 AV_SAMPLE_FMT_DBLP,
171 AV_SAMPLE_FMT_DBL,
172};
173
174static const int rates[] = {
175 48000,
176 44100,
177 16000
178};
179
180static const uint64_t layouts[] = {
181 AV_CH_LAYOUT_STEREO,
182 AV_CH_LAYOUT_MONO,
183 AV_CH_LAYOUT_5POINT1,
184 AV_CH_LAYOUT_7POINT1,
185};
186
187int main(int argc, char **argv)
188{
189 AVAudioResampleContext *s;
190 AVLFG rnd;
191 int ret = 0;
192 uint8_t *in_buf = NULL;
193 uint8_t *out_buf = NULL;
194 unsigned int in_buf_size;
195 unsigned int out_buf_size;
196 uint8_t *in_data[AVRESAMPLE_MAX_CHANNELS] = { 0 };
197 uint8_t *out_data[AVRESAMPLE_MAX_CHANNELS] = { 0 };
198 int in_linesize;
199 int out_linesize;
200 uint64_t in_ch_layout;
201 int in_channels;
202 enum AVSampleFormat in_fmt;
203 int in_rate;
204 uint64_t out_ch_layout;
205 int out_channels;
206 enum AVSampleFormat out_fmt;
207 int out_rate;
208 int num_formats, num_rates, num_layouts;
209 int i, j, k, l, m, n;
210
211 num_formats = 2;
212 num_rates = 2;
213 num_layouts = 2;
214 if (argc > 1) {
215 if (!av_strncasecmp(argv[1], "-h", 3)) {
216 av_log(NULL, AV_LOG_INFO, "Usage: avresample-test [<num formats> "
217 "[<num sample rates> [<num channel layouts>]]]\n"
218 "Default is 2 2 2\n");
219 return 0;
220 }
221 num_formats = strtol(argv[1], NULL, 0);
222 num_formats = av_clip(num_formats, 1, FF_ARRAY_ELEMS(formats));
223 }
224 if (argc > 2) {
225 num_rates = strtol(argv[2], NULL, 0);
226 num_rates = av_clip(num_rates, 1, FF_ARRAY_ELEMS(rates));
227 }
228 if (argc > 3) {
229 num_layouts = strtol(argv[3], NULL, 0);
230 num_layouts = av_clip(num_layouts, 1, FF_ARRAY_ELEMS(layouts));
231 }
232
233 av_log_set_level(AV_LOG_DEBUG);
234
235 av_lfg_init(&rnd, 0xC0FFEE);
236
237 in_buf_size = av_samples_get_buffer_size(&in_linesize, 8, 48000 * 6,
238 AV_SAMPLE_FMT_DBLP, 0);
239 out_buf_size = in_buf_size;
240
241 in_buf = av_malloc(in_buf_size);
242 if (!in_buf)
243 goto end;
244 out_buf = av_malloc(out_buf_size);
245 if (!out_buf)
246 goto end;
247
248 s = avresample_alloc_context();
249 if (!s) {
250 av_log(NULL, AV_LOG_ERROR, "Error allocating AVAudioResampleContext\n");
251 ret = 1;
252 goto end;
253 }
254
255 for (i = 0; i < num_formats; i++) {
256 in_fmt = formats[i];
257 for (k = 0; k < num_layouts; k++) {
258 in_ch_layout = layouts[k];
259 in_channels = av_get_channel_layout_nb_channels(in_ch_layout);
260 for (m = 0; m < num_rates; m++) {
261 in_rate = rates[m];
262
263 ret = av_samples_fill_arrays(in_data, &in_linesize, in_buf,
264 in_channels, in_rate * 6,
265 in_fmt, 0);
266 if (ret < 0) {
267 av_log(s, AV_LOG_ERROR, "failed in_data fill arrays\n");
268 goto end;
269 }
270 audiogen(&rnd, (void **)in_data, in_fmt, in_channels, in_rate, in_rate * 6);
271
272 for (j = 0; j < num_formats; j++) {
273 out_fmt = formats[j];
274 for (l = 0; l < num_layouts; l++) {
275 out_ch_layout = layouts[l];
276 out_channels = av_get_channel_layout_nb_channels(out_ch_layout);
277 for (n = 0; n < num_rates; n++) {
278 out_rate = rates[n];
279
280 av_log(NULL, AV_LOG_INFO, "%s to %s, %d to %d channels, %d Hz to %d Hz\n",
281 av_get_sample_fmt_name(in_fmt), av_get_sample_fmt_name(out_fmt),
282 in_channels, out_channels, in_rate, out_rate);
283
284 ret = av_samples_fill_arrays(out_data, &out_linesize,
285 out_buf, out_channels,
286 out_rate * 6, out_fmt, 0);
287 if (ret < 0) {
288 av_log(s, AV_LOG_ERROR, "failed out_data fill arrays\n");
289 goto end;
290 }
291
292 av_opt_set_int(s, "in_channel_layout", in_ch_layout, 0);
293 av_opt_set_int(s, "in_sample_fmt", in_fmt, 0);
294 av_opt_set_int(s, "in_sample_rate", in_rate, 0);
295 av_opt_set_int(s, "out_channel_layout", out_ch_layout, 0);
296 av_opt_set_int(s, "out_sample_fmt", out_fmt, 0);
297 av_opt_set_int(s, "out_sample_rate", out_rate, 0);
298
299 av_opt_set_int(s, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
300
301 ret = avresample_open(s);
302 if (ret < 0) {
303 av_log(s, AV_LOG_ERROR, "Error opening context\n");
304 goto end;
305 }
306
307 ret = avresample_convert(s, (void **)out_data, out_linesize, out_rate * 6,
308 (void **) in_data, in_linesize, in_rate * 6);
309 if (ret < 0) {
310 char errbuf[256];
311 av_strerror(ret, errbuf, sizeof(errbuf));
312 av_log(NULL, AV_LOG_ERROR, "%s\n", errbuf);
313 goto end;
314 }
315 av_log(NULL, AV_LOG_INFO, "Converted %d samples to %d samples\n",
316 in_rate * 6, ret);
317 if (avresample_get_delay(s) > 0)
318 av_log(NULL, AV_LOG_INFO, "%d delay samples not converted\n",
319 avresample_get_delay(s));
320 if (avresample_available(s) > 0)
321 av_log(NULL, AV_LOG_INFO, "%d samples available for output\n",
322 avresample_available(s));
323 av_log(NULL, AV_LOG_INFO, "\n");
324
325 avresample_close(s);
326 }
327 }
328 }
329 }
330 }
331 }
332
333 ret = 0;
334
335end:
336 av_freep(&in_buf);
337 av_freep(&out_buf);
338 avresample_free(&s);
339 return ret;
340}