2 * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of libswresample
7 * libswresample is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * libswresample 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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with libswresample; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "swresample.h"
34 #define ASSERT_LEVEL 2
36 static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
38 if(av_sample_fmt_is_planar(f)){
39 f= av_get_alt_sample_fmt(f, 0);
43 index= ch + index*ch_count;
47 case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/127.0-1.0;
48 case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
49 case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
50 case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
51 case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
52 default: av_assert0(0);
56 static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
58 if(av_sample_fmt_is_planar(f)){
59 f= av_get_alt_sample_fmt(f, 0);
63 index= ch + index*ch_count;
66 case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= av_clip_uint8 (lrint((v+1.0)*127)); break;
67 case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= av_clip_int16 (lrint(v*32767)); break;
68 case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= av_clipl_int32(lrint(v*2147483647)); break;
69 case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
70 case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
71 default: av_assert2(0);
75 static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
78 if(av_sample_fmt_is_planar(f)){
79 f= av_get_alt_sample_fmt(f, 0);
80 for(ch= 0; ch<ch_count; ch++)
81 a[ch] += index*av_get_bytes_per_sample(f);
83 a[0] += index*ch_count*av_get_bytes_per_sample(f);
87 static const enum AVSampleFormat formats[] = {
100 static const int rates[] = {
111 AV_CH_LAYOUT_STEREO ,
113 AV_CH_LAYOUT_SURROUND ,
114 AV_CH_LAYOUT_4POINT0 ,
117 AV_CH_LAYOUT_5POINT0 ,
118 AV_CH_LAYOUT_5POINT1 ,
119 AV_CH_LAYOUT_5POINT0_BACK ,
120 AV_CH_LAYOUT_5POINT1_BACK ,
121 AV_CH_LAYOUT_7POINT0 ,
122 AV_CH_LAYOUT_7POINT1 ,
123 AV_CH_LAYOUT_7POINT1_WIDE ,
126 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
127 if(av_sample_fmt_is_planar(format)){
129 int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
131 for(i=0; i<SWR_CH_MAX; i++){
132 out[i]= in + i*plane_size;
139 static int cmp(const int *a, const int *b){
143 static void audiogen(void *data, enum AVSampleFormat sample_fmt,
144 int channels, int sample_rate, int nb_samples)
147 double v, f, a, ampa;
148 double tabf1[SWR_CH_MAX];
149 double tabf2[SWR_CH_MAX];
150 double taba[SWR_CH_MAX];
153 #define PUT_SAMPLE set(data, ch, k, channels, sample_fmt, v);
154 #define uint_rand(x) (x = x * 1664525 + 1013904223)
155 #define dbl_rand(x) (uint_rand(x)*2.0 / (double)UINT_MAX - 1)
158 /* 1 second of single freq sinus at 1000 Hz */
160 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
162 for (ch = 0; ch < channels; ch++)
164 a += M_PI * 1000.0 * 2.0 / sample_rate;
167 /* 1 second of varing frequency between 100 and 10000 Hz */
169 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
171 for (ch = 0; ch < channels; ch++)
173 f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
174 a += M_PI * f * 2.0 / sample_rate;
177 /* 0.5 second of low amplitude white noise */
178 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
179 v = dbl_rand(rnd) * 0.30;
180 for (ch = 0; ch < channels; ch++)
184 /* 0.5 second of high amplitude white noise */
185 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
187 for (ch = 0; ch < channels; ch++)
191 /* 1 second of unrelated ramps for each channel */
192 for (ch = 0; ch < channels; ch++) {
194 tabf1[ch] = 100 + uint_rand(rnd) % 5000;
195 tabf2[ch] = 100 + uint_rand(rnd) % 5000;
197 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
198 for (ch = 0; ch < channels; ch++) {
199 v = sin(taba[ch]) * 0.30;
201 f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
202 taba[ch] += M_PI * f * 2.0 / sample_rate;
206 /* 2 seconds of 500 Hz with varying volume */
209 for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
210 for (ch = 0; ch < channels; ch++) {
211 double amp = (1.0 + sin(ampa)) * 0.15;
216 a += M_PI * 500.0 * 2.0 / sample_rate;
217 ampa += M_PI * 2.0 / sample_rate;
222 int main(int argc, char **argv){
223 int in_sample_rate, out_sample_rate, ch ,i, flush_count;
224 uint64_t in_ch_layout, out_ch_layout;
225 enum AVSampleFormat in_sample_fmt, out_sample_fmt;
226 uint8_t array_in[SAMPLES*8*8];
227 uint8_t array_mid[SAMPLES*8*8*3];
228 uint8_t array_out[SAMPLES*8*8+100];
229 uint8_t *ain[SWR_CH_MAX];
230 uint8_t *aout[SWR_CH_MAX];
231 uint8_t *amid[SWR_CH_MAX];
234 int num_tests = 10000;
236 uint32_t rand_seed = 0;
237 int remaining_tests[FF_ARRAY_ELEMS(rates) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats)];
238 int max_tests = FF_ARRAY_ELEMS(remaining_tests);
240 int specific_test= -1;
242 struct SwrContext * forw_ctx= NULL;
243 struct SwrContext *backw_ctx= NULL;
246 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
247 av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>[ <test>]] \n"
248 "num_tests Default is %d\n", num_tests);
251 num_tests = strtol(argv[1], NULL, 0);
253 num_tests = -num_tests;
256 if(num_tests<= 0 || num_tests>max_tests)
257 num_tests = max_tests;
259 specific_test = strtol(argv[1], NULL, 0);
263 for(i=0; i<max_tests; i++)
264 remaining_tests[i] = i;
266 for(test=0; test<num_tests; test++){
269 r = (seed * (uint64_t)(max_tests - test)) >>32;
270 FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
272 qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), (void*)cmp);
273 in_sample_rate=16000;
274 for(test=0; test<num_tests; test++){
275 char in_layout_string[256];
276 char out_layout_string[256];
277 unsigned vector= remaining_tests[max_tests - test - 1];
279 int out_count, mid_count, out_ch_count;
281 in_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
282 out_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
283 in_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
284 out_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
285 out_sample_rate = rates [vector % FF_ARRAY_ELEMS(rates )]; vector /= FF_ARRAY_ELEMS(rates);
288 if(specific_test == 0){
289 if(out_sample_rate != in_sample_rate || in_ch_layout != out_ch_layout)
293 in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
294 out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
295 av_get_channel_layout_string( in_layout_string, sizeof( in_layout_string), in_ch_count, in_ch_layout);
296 av_get_channel_layout_string(out_layout_string, sizeof(out_layout_string), out_ch_count, out_ch_layout);
297 fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
298 in_layout_string, out_layout_string,
299 in_sample_rate, out_sample_rate,
300 av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
301 forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, out_sample_fmt, out_sample_rate,
302 in_ch_layout, in_sample_fmt, in_sample_rate,
304 backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
305 out_ch_layout, out_sample_fmt, out_sample_rate,
308 fprintf(stderr, "Failed to init forw_cts\n");
312 fprintf(stderr, "Failed to init backw_ctx\n");
315 if(swr_init( forw_ctx) < 0)
316 fprintf(stderr, "swr_init(->) failed\n");
317 if(swr_init(backw_ctx) < 0)
318 fprintf(stderr, "swr_init(<-) failed\n");
320 setup_array(ain , array_in , in_sample_fmt, SAMPLES);
321 setup_array(amid, array_mid, out_sample_fmt, 3*SAMPLES);
322 setup_array(aout, array_out, in_sample_fmt , SAMPLES);
324 for(ch=0; ch<in_ch_count; ch++){
325 for(i=0; i<SAMPLES; i++)
326 set(ain, ch, i, in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
329 audiogen(ain, in_sample_fmt, in_ch_count, SAMPLES/6+1, SAMPLES);
331 mode = uint_rand(rand_seed) % 3;
332 if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
333 mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, SAMPLES);
335 mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, SAMPLES);
336 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
339 mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, 1);
340 av_assert0(mid_count==0);
341 shift(ain, 1, in_ch_count, in_sample_fmt);
342 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
343 shift(amid, mid_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
344 mid_count+=swr_convert(forw_ctx, amid, 2, (const uint8_t **)ain, 2);
345 shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
346 shift(ain, 2, in_ch_count, in_sample_fmt);
347 mid_count+=swr_convert(forw_ctx, amid, 1, (const uint8_t **)ain, SAMPLES-3);
348 shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
349 shift(ain, -3, in_ch_count, in_sample_fmt);
350 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
351 shift(amid, -tmp_count, out_ch_count, out_sample_fmt);
353 out_count= swr_convert(backw_ctx,aout, SAMPLES, (const uint8_t **)amid, mid_count);
355 for(ch=0; ch<in_ch_count; ch++){
356 double sse, maxdiff=0;
362 for(i=0; i<out_count; i++){
363 double a= get(ain , ch, i, in_ch_count, in_sample_fmt);
364 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
370 maxdiff= FFMAX(maxdiff, FFABS(a-b));
372 sse= sum_aa + sum_bb - 2*sum_ab;
373 if(sse < 0 && sse > -0.00001) sse=0; //fix rounding error
375 fprintf(stderr, "[e:%f c:%f max:%f] len:%5d\n", out_count ? sqrt(sse/out_count) : 0, sum_ab/(sqrt(sum_aa*sum_bb)), maxdiff, out_count);
380 flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
381 shift(aout, flush_i, in_ch_count, in_sample_fmt);
382 flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
383 shift(aout, -flush_i, in_ch_count, in_sample_fmt);
385 for(ch=0; ch<in_ch_count; ch++){
386 double sse, maxdiff=0;
392 for(i=0; i<flush_count; i++){
393 double a= get(ain , ch, i+out_count, in_ch_count, in_sample_fmt);
394 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
400 maxdiff= FFMAX(maxdiff, FFABS(a-b));
402 sse= sum_aa + sum_bb - 2*sum_ab;
403 if(sse < 0 && sse > -0.00001) sse=0; //fix rounding error
405 fprintf(stderr, "[e:%f c:%f max:%f] len:%5d F:%3d\n", sqrt(sse/flush_count), sum_ab/(sqrt(sum_aa*sum_bb)), maxdiff, flush_count, flush_i);
410 fprintf(stderr, "\n");