]> git.sesse.net Git - ffmpeg/blob - libswresample/swresample_test.c
swr: use memcpy when input and output match
[ffmpeg] / libswresample / swresample_test.c
1 /*
2  * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of libswresample
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/common.h"
24 #include "libavutil/audioconvert.h"
25 #include "libavutil/opt.h"
26 #include "swresample.h"
27 #undef fprintf
28
29 #define SAMPLES 1000
30
31 #define ASSERT_LEVEL 2
32
33 static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
34     const uint8_t *p;
35     if(av_sample_fmt_is_planar(f)){
36         f= av_get_alt_sample_fmt(f, 0);
37         p= a[ch];
38     }else{
39         p= a[0];
40         index= ch + index*ch_count;
41     }
42
43     switch(f){
44     case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/127.0-1.0;
45     case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
46     case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
47     case AV_SAMPLE_FMT_FLT: return ((const float  *)p)[index];
48     case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
49     default: av_assert0(0);
50     }
51 }
52
53 static void  set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
54     uint8_t *p;
55     if(av_sample_fmt_is_planar(f)){
56         f= av_get_alt_sample_fmt(f, 0);
57         p= a[ch];
58     }else{
59         p= a[0];
60         index= ch + index*ch_count;
61     }
62     switch(f){
63     case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= av_clip_uint8 (lrint((v+1.0)*127));     break;
64     case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= av_clip_int16 (lrint(v*32767));         break;
65     case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= av_clipl_int32(lrint(v*2147483647));    break;
66     case AV_SAMPLE_FMT_FLT: ((float  *)p)[index]= v;                                      break;
67     case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v;                                      break;
68     default: av_assert2(0);
69     }
70 }
71
72 static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
73     int ch;
74
75     if(av_sample_fmt_is_planar(f)){
76         f= av_get_alt_sample_fmt(f, 0);
77         for(ch= 0; ch<ch_count; ch++)
78             a[ch] += index*av_get_bytes_per_sample(f);
79     }else{
80         a[0] += index*ch_count*av_get_bytes_per_sample(f);
81     }
82 }
83
84 static const enum AVSampleFormat formats[] = {
85     AV_SAMPLE_FMT_S16,
86     AV_SAMPLE_FMT_FLTP,
87     AV_SAMPLE_FMT_S16P,
88     AV_SAMPLE_FMT_FLT,
89     AV_SAMPLE_FMT_S32P,
90     AV_SAMPLE_FMT_S32,
91     AV_SAMPLE_FMT_U8P,
92     AV_SAMPLE_FMT_U8,
93     AV_SAMPLE_FMT_DBLP,
94     AV_SAMPLE_FMT_DBL,
95 };
96
97 static const int rates[] = {
98     8000,
99     11025,
100     16000,
101     22050,
102     32000,
103     48000,
104 };
105
106 uint64_t layouts[]={
107     AV_CH_LAYOUT_MONO                    ,
108     AV_CH_LAYOUT_STEREO                  ,
109     AV_CH_LAYOUT_2_1                     ,
110     AV_CH_LAYOUT_SURROUND                ,
111     AV_CH_LAYOUT_4POINT0                 ,
112     AV_CH_LAYOUT_2_2                     ,
113     AV_CH_LAYOUT_QUAD                    ,
114     AV_CH_LAYOUT_5POINT0                 ,
115     AV_CH_LAYOUT_5POINT1                 ,
116     AV_CH_LAYOUT_5POINT0_BACK            ,
117     AV_CH_LAYOUT_5POINT1_BACK            ,
118     AV_CH_LAYOUT_7POINT0                 ,
119     AV_CH_LAYOUT_7POINT1                 ,
120     AV_CH_LAYOUT_7POINT1_WIDE            ,
121 };
122
123 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
124     if(av_sample_fmt_is_planar(format)){
125         int i;
126         int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
127         format&=0xFF;
128         for(i=0; i<SWR_CH_MAX; i++){
129             out[i]= in + i*plane_size;
130         }
131     }else{
132         out[0]= in;
133     }
134 }
135
136 static int cmp(const int *a, const int *b){
137     return *a - *b;
138 }
139
140 static void audiogen(void *data, enum AVSampleFormat sample_fmt,
141                      int channels, int sample_rate, int nb_samples)
142 {
143     int i, ch, k;
144     double v, f, a, ampa;
145     double tabf1[SWR_CH_MAX];
146     double tabf2[SWR_CH_MAX];
147     double taba[SWR_CH_MAX];
148     unsigned static rnd;
149
150 #define PUT_SAMPLE set(data, ch, k, channels, sample_fmt, v);
151 #define uint_rand(x) (x = x * 1664525 + 1013904223)
152 #define dbl_rand(x) (uint_rand(x)*2.0 / (double)UINT_MAX - 1)
153     k = 0;
154
155     /* 1 second of single freq sinus at 1000 Hz */
156     a = 0;
157     for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
158         v = sin(a) * 0.30;
159         for (ch = 0; ch < channels; ch++)
160             PUT_SAMPLE
161         a += M_PI * 1000.0 * 2.0 / sample_rate;
162     }
163
164     /* 1 second of varing frequency between 100 and 10000 Hz */
165     a = 0;
166     for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
167         v = sin(a) * 0.30;
168         for (ch = 0; ch < channels; ch++)
169             PUT_SAMPLE
170         f  = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
171         a += M_PI * f * 2.0 / sample_rate;
172     }
173
174     /* 0.5 second of low amplitude white noise */
175     for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
176         v = dbl_rand(rnd) * 0.30;
177         for (ch = 0; ch < channels; ch++)
178             PUT_SAMPLE
179     }
180
181     /* 0.5 second of high amplitude white noise */
182     for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
183         v = dbl_rand(rnd);
184         for (ch = 0; ch < channels; ch++)
185             PUT_SAMPLE
186     }
187
188     /* 1 second of unrelated ramps for each channel */
189     for (ch = 0; ch < channels; ch++) {
190         taba[ch]  = 0;
191         tabf1[ch] = 100 + uint_rand(rnd) % 5000;
192         tabf2[ch] = 100 + uint_rand(rnd) % 5000;
193     }
194     for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
195         for (ch = 0; ch < channels; ch++) {
196             v = sin(taba[ch]) * 0.30;
197             PUT_SAMPLE
198             f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
199             taba[ch] += M_PI * f * 2.0 / sample_rate;
200         }
201     }
202
203     /* 2 seconds of 500 Hz with varying volume */
204     a    = 0;
205     ampa = 0;
206     for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
207         for (ch = 0; ch < channels; ch++) {
208             double amp = (1.0 + sin(ampa)) * 0.15;
209             if (ch & 1)
210                 amp = 0.30 - amp;
211             v = sin(a) * amp;
212             PUT_SAMPLE
213             a    += M_PI * 500.0 * 2.0 / sample_rate;
214             ampa += M_PI *  2.0 / sample_rate;
215         }
216     }
217 }
218
219 int main(int argc, char **argv){
220     int in_sample_rate, out_sample_rate, ch ,i, flush_count;
221     uint64_t in_ch_layout, out_ch_layout;
222     enum AVSampleFormat in_sample_fmt, out_sample_fmt;
223     uint8_t array_in[SAMPLES*8*8];
224     uint8_t array_mid[SAMPLES*8*8*3];
225     uint8_t array_out[SAMPLES*8*8+100];
226     uint8_t *ain[SWR_CH_MAX];
227     uint8_t *aout[SWR_CH_MAX];
228     uint8_t *amid[SWR_CH_MAX];
229     int flush_i=0;
230     int mode = 0;
231     int max_tests = FF_ARRAY_ELEMS(rates) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats);
232     int num_tests = 10000;
233     uint32_t seed = 0;
234     int remaining_tests[max_tests];
235     int test;
236
237     struct SwrContext * forw_ctx= NULL;
238     struct SwrContext *backw_ctx= NULL;
239
240     if (argc > 1) {
241         if (!strcmp(argv[1], "-h")) {
242             av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>]\n"
243                    "Default is %d\n", num_tests);
244             return 0;
245         }
246         num_tests = strtol(argv[1], NULL, 0);
247         if(num_tests<= 0 || num_tests>max_tests)
248             num_tests = max_tests;
249     }
250
251     for(i=0; i<max_tests; i++)
252         remaining_tests[i] = i;
253
254     for(test=0; test<num_tests; test++){
255         unsigned r;
256         seed = seed * 1664525 + 1013904223;
257         r = (seed * (uint64_t)(max_tests - test)) >>32;
258         FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
259     }
260     qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), (void*)cmp);
261     in_sample_rate=16000;
262     for(test=0; test<num_tests; test++){
263         char  in_layout_string[256];
264         char out_layout_string[256];
265         unsigned vector= remaining_tests[max_tests - test - 1];
266         int in_ch_count;
267         int out_count, mid_count, out_ch_count;
268
269         in_ch_layout    = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
270         out_ch_layout   = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
271         in_sample_fmt   = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
272         out_sample_fmt  = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
273         out_sample_rate = rates  [vector % FF_ARRAY_ELEMS(rates  )]; vector /= FF_ARRAY_ELEMS(rates);
274         av_assert0(!vector);
275
276         in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
277         out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
278         av_get_channel_layout_string( in_layout_string, sizeof( in_layout_string),  in_ch_count,  in_ch_layout);
279         av_get_channel_layout_string(out_layout_string, sizeof(out_layout_string), out_ch_count, out_ch_layout);
280         fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
281                 in_layout_string, out_layout_string,
282                 in_sample_rate, out_sample_rate,
283                 av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
284         forw_ctx  = swr_alloc_set_opts(forw_ctx, out_ch_layout, out_sample_fmt,  out_sample_rate,
285                                                     in_ch_layout,  in_sample_fmt,  in_sample_rate,
286                                         0, 0);
287         backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout,  in_sample_fmt,             in_sample_rate,
288                                                     out_ch_layout, out_sample_fmt, out_sample_rate,
289                                         0, 0);
290         if(swr_init( forw_ctx) < 0)
291             fprintf(stderr, "swr_init(->) failed\n");
292         if(swr_init(backw_ctx) < 0)
293             fprintf(stderr, "swr_init(<-) failed\n");
294         if(!forw_ctx)
295             fprintf(stderr, "Failed to init forw_cts\n");
296         if(!backw_ctx)
297             fprintf(stderr, "Failed to init backw_ctx\n");
298                 //FIXME test planar
299         setup_array(ain , array_in ,  in_sample_fmt,   SAMPLES);
300         setup_array(amid, array_mid, out_sample_fmt, 3*SAMPLES);
301         setup_array(aout, array_out,  in_sample_fmt           ,   SAMPLES);
302 #if 0
303         for(ch=0; ch<in_ch_count; ch++){
304             for(i=0; i<SAMPLES; i++)
305                 set(ain, ch, i, in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
306         }
307 #else
308         audiogen(ain, in_sample_fmt, in_ch_count, SAMPLES/6+1, SAMPLES);
309 #endif
310         mode++;
311         mode%=3;
312         if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
313             mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, SAMPLES);
314         } else if(mode==1){
315             mid_count= swr_convert(forw_ctx, amid,         0, (const uint8_t **)ain, SAMPLES);
316             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain,       0);
317         } else {
318             int tmp_count;
319             mid_count= swr_convert(forw_ctx, amid,         0, (const uint8_t **)ain,       1);
320             av_assert0(mid_count==0);
321             shift(ain,  1, in_ch_count, in_sample_fmt);
322             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain,       0);
323             shift(amid,  mid_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
324             mid_count+=swr_convert(forw_ctx, amid,         2, (const uint8_t **)ain,       2);
325             shift(amid,  mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
326             shift(ain,  2, in_ch_count, in_sample_fmt);
327             mid_count+=swr_convert(forw_ctx, amid,         1, (const uint8_t **)ain, SAMPLES-3);
328             shift(amid,  mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
329             shift(ain, -3, in_ch_count, in_sample_fmt);
330             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain,       0);
331             shift(amid,  -tmp_count, out_ch_count, out_sample_fmt);
332         }
333         out_count= swr_convert(backw_ctx,aout, SAMPLES, (const uint8_t **)amid, mid_count);
334
335         for(ch=0; ch<in_ch_count; ch++){
336             double sse, maxdiff=0;
337             double sum_a= 0;
338             double sum_b= 0;
339             double sum_aa= 0;
340             double sum_bb= 0;
341             double sum_ab= 0;
342             for(i=0; i<out_count; i++){
343                 double a= get(ain , ch, i, in_ch_count, in_sample_fmt);
344                 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
345                 sum_a += a;
346                 sum_b += b;
347                 sum_aa+= a*a;
348                 sum_bb+= b*b;
349                 sum_ab+= a*b;
350                 maxdiff= FFMAX(maxdiff, FFABS(a-b));
351             }
352             sse= sum_aa + sum_bb - 2*sum_ab;
353
354             fprintf(stderr, "[e:%f c:%f max:%f] len:%5d\n", sqrt(sse/out_count), sum_ab/(sqrt(sum_aa*sum_bb)), maxdiff, out_count);
355         }
356
357         flush_i++;
358         flush_i%=21;
359         flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
360         shift(aout,  flush_i, in_ch_count, in_sample_fmt);
361         flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
362         shift(aout, -flush_i, in_ch_count, in_sample_fmt);
363         if(flush_count){
364             for(ch=0; ch<in_ch_count; ch++){
365                 double sse, maxdiff=0;
366                 double sum_a= 0;
367                 double sum_b= 0;
368                 double sum_aa= 0;
369                 double sum_bb= 0;
370                 double sum_ab= 0;
371                 for(i=0; i<flush_count; i++){
372                     double a= get(ain , ch, i+out_count, in_ch_count, in_sample_fmt);
373                     double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
374                     sum_a += a;
375                     sum_b += b;
376                     sum_aa+= a*a;
377                     sum_bb+= b*b;
378                     sum_ab+= a*b;
379                     maxdiff= FFMAX(maxdiff, FFABS(a-b));
380                 }
381                 sse= sum_aa + sum_bb - 2*sum_ab;
382
383                 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);
384             }
385         }
386
387
388         fprintf(stderr, "\n");
389     }
390
391     return 0;
392 }