7fa4d3762753c5ec5c83138d7621ace64a3f7d21
[ffmpeg.git] / libswresample / swresample_test.c
1 /*
2  * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * libswresample is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with libswresample; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/common.h"
23 #include "libavutil/audioconvert.h"
24 #include "libavutil/opt.h"
25 #include "swresample.h"
26 #undef fprintf
27
28 #define SAMPLES 1000
29
30 #define ASSERT_LEVEL 2
31
32 static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
33     const uint8_t *p;
34     if(av_sample_fmt_is_planar(f)){
35         f= av_get_alt_sample_fmt(f, 0);
36         p= a[ch];
37     }else{
38         p= a[0];
39         index= ch + index*ch_count;
40     }
41
42     switch(f){
43     case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
44     case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
45     case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
46     case AV_SAMPLE_FMT_FLT: return ((const float  *)p)[index];
47     case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
48     default: av_assert0(0);
49     }
50 }
51
52 static void  set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
53     uint8_t *p;
54     if(av_sample_fmt_is_planar(f)){
55         f= av_get_alt_sample_fmt(f, 0);
56         p= a[ch];
57     }else{
58         p= a[0];
59         index= ch + index*ch_count;
60     }
61     switch(f){
62     case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= av_clip_uint8 (lrint((v+1.0)*255.0/2)); break;
63     case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= av_clip_int16 (lrint(v*32767));         break;
64     case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= av_clipl_int32(lrint(v*2147483647));    break;
65     case AV_SAMPLE_FMT_FLT: ((float  *)p)[index]= v;                                      break;
66     case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v;                                      break;
67     default: av_assert2(0);
68     }
69 }
70
71 static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
72     int ch;
73
74     if(av_sample_fmt_is_planar(f)){
75         f= av_get_alt_sample_fmt(f, 0);
76         for(ch= 0; ch<ch_count; ch++)
77             a[ch] += index*av_get_bytes_per_sample(f);
78     }else{
79         a[0] += index*ch_count*av_get_bytes_per_sample(f);
80     }
81 }
82
83 static const enum AVSampleFormat formats[] = {
84     AV_SAMPLE_FMT_S16,
85     AV_SAMPLE_FMT_FLTP,
86     AV_SAMPLE_FMT_S16P,
87     AV_SAMPLE_FMT_FLT,
88     AV_SAMPLE_FMT_S32P,
89     AV_SAMPLE_FMT_S32,
90     AV_SAMPLE_FMT_U8P,
91     AV_SAMPLE_FMT_U8,
92     AV_SAMPLE_FMT_DBLP,
93     AV_SAMPLE_FMT_DBL,
94 };
95
96 static const int rates[] = {
97     8000,
98     11025,
99     16000,
100     22050,
101     32000,
102     48000,
103 };
104
105 uint64_t layouts[]={
106     AV_CH_LAYOUT_MONO                    ,
107     AV_CH_LAYOUT_STEREO                  ,
108     AV_CH_LAYOUT_2_1                     ,
109     AV_CH_LAYOUT_SURROUND                ,
110     AV_CH_LAYOUT_4POINT0                 ,
111     AV_CH_LAYOUT_2_2                     ,
112     AV_CH_LAYOUT_QUAD                    ,
113     AV_CH_LAYOUT_5POINT0                 ,
114     AV_CH_LAYOUT_5POINT1                 ,
115     AV_CH_LAYOUT_5POINT0_BACK            ,
116     AV_CH_LAYOUT_5POINT1_BACK            ,
117     AV_CH_LAYOUT_7POINT0                 ,
118     AV_CH_LAYOUT_7POINT1                 ,
119     AV_CH_LAYOUT_7POINT1_WIDE            ,
120 };
121
122 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
123     if(av_sample_fmt_is_planar(format)){
124         int i;
125         int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
126         format&=0xFF;
127         for(i=0; i<SWR_CH_MAX; i++){
128             out[i]= in + i*plane_size;
129         }
130     }else{
131         out[0]= in;
132     }
133 }
134
135 static int cmp(const int *a, const int *b){
136     return *a - *b;
137 }
138
139 int main(int argc, char **argv){
140     int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
141     int in_sample_fmt_index, out_sample_fmt_index;
142     uint64_t in_ch_layout, out_ch_layout;
143     enum AVSampleFormat in_sample_fmt, out_sample_fmt;
144     uint8_t array_in[SAMPLES*8*8];
145     uint8_t array_mid[SAMPLES*8*8*3];
146     uint8_t array_out[SAMPLES*8*8+100];
147     uint8_t *ain[SWR_CH_MAX];
148     uint8_t *aout[SWR_CH_MAX];
149     uint8_t *amid[SWR_CH_MAX];
150     int flush_i=0;
151     int mode = 0;
152     int max_tests = FF_ARRAY_ELEMS(rates) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats);
153     int num_tests = 10000;
154     uint32_t seed = 0;
155     int remaining_tests[max_tests];
156     int test;
157
158     struct SwrContext * forw_ctx= NULL;
159     struct SwrContext *backw_ctx= NULL;
160
161     if (argc > 1) {
162         if (!strcmp(argv[1], "-h")) {
163             av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>]\n"
164                    "Default is %d\n", num_tests);
165             return 0;
166         }
167         num_tests = strtol(argv[1], NULL, 0);
168         if(num_tests<= 0 || num_tests>max_tests)
169             num_tests = max_tests;
170     }
171
172     for(i=0; i<max_tests; i++)
173         remaining_tests[i] = i;
174
175     for(test=0; test<num_tests; test++){
176         unsigned r;
177         seed = seed * 1664525 + 1013904223;
178         r = (seed * (uint64_t)(max_tests - test)) >>32;
179         FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
180     }
181     qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), cmp);
182     in_sample_rate=16000;
183     for(test=0; test<num_tests; test++){
184         char  in_layout_string[256];
185         char out_layout_string[256];
186         unsigned vector= remaining_tests[max_tests - test - 1];
187
188         in_ch_layout    = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
189         out_ch_layout   = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
190         in_sample_fmt   = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
191         out_sample_fmt  = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
192         out_sample_rate = rates  [vector % FF_ARRAY_ELEMS(rates  )]; vector /= FF_ARRAY_ELEMS(rates);
193         av_assert0(!vector);
194
195                     int in_ch_count;
196                     in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
197                         int out_count, mid_count, out_ch_count;
198                         out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
199                         av_get_channel_layout_string( in_layout_string, sizeof( in_layout_string),  in_ch_count,  in_ch_layout);
200                         av_get_channel_layout_string(out_layout_string, sizeof(out_layout_string), out_ch_count, out_ch_layout);
201                         fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
202                                in_layout_string, out_layout_string,
203                                in_sample_rate, out_sample_rate,
204                                av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
205                         forw_ctx  = swr_alloc_set_opts(forw_ctx, out_ch_layout, out_sample_fmt,  out_sample_rate,
206                                                                   in_ch_layout,  in_sample_fmt,  in_sample_rate,
207                                                        0, 0);
208                         backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout,  in_sample_fmt,             in_sample_rate,
209                                                                  out_ch_layout, out_sample_fmt, out_sample_rate,
210                                                        0, 0);
211                         if(swr_init( forw_ctx) < 0)
212                             fprintf(stderr, "swr_init(->) failed\n");
213                         if(swr_init(backw_ctx) < 0)
214                             fprintf(stderr, "swr_init(<-) failed\n");
215                         if(!forw_ctx)
216                             fprintf(stderr, "Failed to init forw_cts\n");
217                         if(!backw_ctx)
218                             fprintf(stderr, "Failed to init backw_ctx\n");
219                                //FIXME test planar
220                         setup_array(ain , array_in ,  in_sample_fmt,   SAMPLES);
221                         setup_array(amid, array_mid, out_sample_fmt, 3*SAMPLES);
222                         setup_array(aout, array_out,  in_sample_fmt           ,   SAMPLES);
223                         for(ch=0; ch<in_ch_count; ch++){
224                             for(i=0; i<SAMPLES; i++)
225                                 set(ain, ch, i, in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
226                         }
227                         mode++;
228                         mode%=3;
229                         if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
230                             mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
231                         } else if(mode==1){
232                             mid_count= swr_convert(forw_ctx, amid,         0, ain, SAMPLES);
233                             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain,       0);
234                         } else {
235                             int tmp_count;
236                             mid_count= swr_convert(forw_ctx, amid,         0, ain,       1);
237                             av_assert0(mid_count==0);
238                             shift(ain,  1, in_ch_count, in_sample_fmt);
239                             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain,       0);
240                             shift(amid,  mid_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
241                             mid_count+=swr_convert(forw_ctx, amid,         2, ain,       2);
242                             shift(amid,  mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
243                             shift(ain,  2, in_ch_count, in_sample_fmt);
244                             mid_count+=swr_convert(forw_ctx, amid,         1, ain, SAMPLES-3);
245                             shift(amid,  mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
246                             shift(ain, -3, in_ch_count, in_sample_fmt);
247                             mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain,       0);
248                             shift(amid,  -tmp_count, out_ch_count, out_sample_fmt);
249                         }
250                         out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
251
252                         for(ch=0; ch<in_ch_count; ch++){
253                             double sse, x, maxdiff=0;
254                             double sum_a= 0;
255                             double sum_b= 0;
256                             double sum_aa= 0;
257                             double sum_bb= 0;
258                             double sum_ab= 0;
259                             for(i=0; i<out_count; i++){
260                                 double a= get(ain , ch, i, in_ch_count, in_sample_fmt);
261                                 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
262                                 sum_a += a;
263                                 sum_b += b;
264                                 sum_aa+= a*a;
265                                 sum_bb+= b*b;
266                                 sum_ab+= a*b;
267                                 maxdiff= FFMAX(maxdiff, FFABS(a-b));
268                             }
269                             sse= sum_aa + sum_bb - 2*sum_ab;
270
271                             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);
272                         }
273
274                         flush_i++;
275                         flush_i%=21;
276                         flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
277                         shift(aout,  flush_i, in_ch_count, in_sample_fmt);
278                         flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
279                         shift(aout, -flush_i, in_ch_count, in_sample_fmt);
280                         if(flush_count){
281                             for(ch=0; ch<in_ch_count; ch++){
282                                 double sse, x, maxdiff=0;
283                                 double sum_a= 0;
284                                 double sum_b= 0;
285                                 double sum_aa= 0;
286                                 double sum_bb= 0;
287                                 double sum_ab= 0;
288                                 for(i=0; i<flush_count; i++){
289                                     double a= get(ain , ch, i+out_count, in_ch_count, in_sample_fmt);
290                                     double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
291                                     sum_a += a;
292                                     sum_b += b;
293                                     sum_aa+= a*a;
294                                     sum_bb+= b*b;
295                                     sum_ab+= a*b;
296                                     maxdiff= FFMAX(maxdiff, FFABS(a-b));
297                                 }
298                                 sse= sum_aa + sum_bb - 2*sum_ab;
299
300                                 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);
301                             }
302                         }
303
304
305                         fprintf(stderr, "\n");
306     }
307
308     return 0;
309 }