00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/avassert.h"
00023 #include "libavutil/common.h"
00024 #include "libavutil/audioconvert.h"
00025 #include "libavutil/opt.h"
00026 #include "swresample.h"
00027
00028 #undef time
00029 #include "time.h"
00030 #undef fprintf
00031
00032 #define SAMPLES 1000
00033
00034 #define ASSERT_LEVEL 2
00035
00036 static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
00037 const uint8_t *p;
00038 if(av_sample_fmt_is_planar(f)){
00039 f= av_get_alt_sample_fmt(f, 0);
00040 p= a[ch];
00041 }else{
00042 p= a[0];
00043 index= ch + index*ch_count;
00044 }
00045
00046 switch(f){
00047 case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/127.0-1.0;
00048 case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
00049 case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
00050 case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
00051 case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
00052 default: av_assert0(0);
00053 }
00054 }
00055
00056 static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
00057 uint8_t *p;
00058 if(av_sample_fmt_is_planar(f)){
00059 f= av_get_alt_sample_fmt(f, 0);
00060 p= a[ch];
00061 }else{
00062 p= a[0];
00063 index= ch + index*ch_count;
00064 }
00065 switch(f){
00066 case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= av_clip_uint8 (lrint((v+1.0)*127)); break;
00067 case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= av_clip_int16 (lrint(v*32767)); break;
00068 case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= av_clipl_int32(lrint(v*2147483647)); break;
00069 case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
00070 case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
00071 default: av_assert2(0);
00072 }
00073 }
00074
00075 static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
00076 int ch;
00077
00078 if(av_sample_fmt_is_planar(f)){
00079 f= av_get_alt_sample_fmt(f, 0);
00080 for(ch= 0; ch<ch_count; ch++)
00081 a[ch] += index*av_get_bytes_per_sample(f);
00082 }else{
00083 a[0] += index*ch_count*av_get_bytes_per_sample(f);
00084 }
00085 }
00086
00087 static const enum AVSampleFormat formats[] = {
00088 AV_SAMPLE_FMT_S16,
00089 AV_SAMPLE_FMT_FLTP,
00090 AV_SAMPLE_FMT_S16P,
00091 AV_SAMPLE_FMT_FLT,
00092 AV_SAMPLE_FMT_S32P,
00093 AV_SAMPLE_FMT_S32,
00094 AV_SAMPLE_FMT_U8P,
00095 AV_SAMPLE_FMT_U8,
00096 AV_SAMPLE_FMT_DBLP,
00097 AV_SAMPLE_FMT_DBL,
00098 };
00099
00100 static const int rates[] = {
00101 8000,
00102 11025,
00103 16000,
00104 22050,
00105 32000,
00106 48000,
00107 };
00108
00109 uint64_t layouts[]={
00110 AV_CH_LAYOUT_MONO ,
00111 AV_CH_LAYOUT_STEREO ,
00112 AV_CH_LAYOUT_2_1 ,
00113 AV_CH_LAYOUT_SURROUND ,
00114 AV_CH_LAYOUT_4POINT0 ,
00115 AV_CH_LAYOUT_2_2 ,
00116 AV_CH_LAYOUT_QUAD ,
00117 AV_CH_LAYOUT_5POINT0 ,
00118 AV_CH_LAYOUT_5POINT1 ,
00119 AV_CH_LAYOUT_5POINT0_BACK ,
00120 AV_CH_LAYOUT_5POINT1_BACK ,
00121 AV_CH_LAYOUT_7POINT0 ,
00122 AV_CH_LAYOUT_7POINT1 ,
00123 AV_CH_LAYOUT_7POINT1_WIDE ,
00124 };
00125
00126 static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
00127 if(av_sample_fmt_is_planar(format)){
00128 int i;
00129 int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
00130 format&=0xFF;
00131 for(i=0; i<SWR_CH_MAX; i++){
00132 out[i]= in + i*plane_size;
00133 }
00134 }else{
00135 out[0]= in;
00136 }
00137 }
00138
00139 static int cmp(const int *a, const int *b){
00140 return *a - *b;
00141 }
00142
00143 static void audiogen(void *data, enum AVSampleFormat sample_fmt,
00144 int channels, int sample_rate, int nb_samples)
00145 {
00146 int i, ch, k;
00147 double v, f, a, ampa;
00148 double tabf1[SWR_CH_MAX];
00149 double tabf2[SWR_CH_MAX];
00150 double taba[SWR_CH_MAX];
00151 unsigned static rnd;
00152
00153 #define PUT_SAMPLE set(data, ch, k, channels, sample_fmt, v);
00154 #define uint_rand(x) (x = x * 1664525 + 1013904223)
00155 #define dbl_rand(x) (uint_rand(x)*2.0 / (double)UINT_MAX - 1)
00156 k = 0;
00157
00158
00159 a = 0;
00160 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
00161 v = sin(a) * 0.30;
00162 for (ch = 0; ch < channels; ch++)
00163 PUT_SAMPLE
00164 a += M_PI * 1000.0 * 2.0 / sample_rate;
00165 }
00166
00167
00168 a = 0;
00169 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
00170 v = sin(a) * 0.30;
00171 for (ch = 0; ch < channels; ch++)
00172 PUT_SAMPLE
00173 f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
00174 a += M_PI * f * 2.0 / sample_rate;
00175 }
00176
00177
00178 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
00179 v = dbl_rand(rnd) * 0.30;
00180 for (ch = 0; ch < channels; ch++)
00181 PUT_SAMPLE
00182 }
00183
00184
00185 for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
00186 v = dbl_rand(rnd);
00187 for (ch = 0; ch < channels; ch++)
00188 PUT_SAMPLE
00189 }
00190
00191
00192 for (ch = 0; ch < channels; ch++) {
00193 taba[ch] = 0;
00194 tabf1[ch] = 100 + uint_rand(rnd) % 5000;
00195 tabf2[ch] = 100 + uint_rand(rnd) % 5000;
00196 }
00197 for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
00198 for (ch = 0; ch < channels; ch++) {
00199 v = sin(taba[ch]) * 0.30;
00200 PUT_SAMPLE
00201 f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
00202 taba[ch] += M_PI * f * 2.0 / sample_rate;
00203 }
00204 }
00205
00206
00207 a = 0;
00208 ampa = 0;
00209 for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
00210 for (ch = 0; ch < channels; ch++) {
00211 double amp = (1.0 + sin(ampa)) * 0.15;
00212 if (ch & 1)
00213 amp = 0.30 - amp;
00214 v = sin(a) * amp;
00215 PUT_SAMPLE
00216 a += M_PI * 500.0 * 2.0 / sample_rate;
00217 ampa += M_PI * 2.0 / sample_rate;
00218 }
00219 }
00220 }
00221
00222 int main(int argc, char **argv){
00223 int in_sample_rate, out_sample_rate, ch ,i, flush_count;
00224 uint64_t in_ch_layout, out_ch_layout;
00225 enum AVSampleFormat in_sample_fmt, out_sample_fmt;
00226 uint8_t array_in[SAMPLES*8*8];
00227 uint8_t array_mid[SAMPLES*8*8*3];
00228 uint8_t array_out[SAMPLES*8*8+100];
00229 uint8_t *ain[SWR_CH_MAX];
00230 uint8_t *aout[SWR_CH_MAX];
00231 uint8_t *amid[SWR_CH_MAX];
00232 int flush_i=0;
00233 int mode;
00234 int max_tests = FF_ARRAY_ELEMS(rates) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats);
00235 int num_tests = 10000;
00236 uint32_t seed = 0;
00237 uint32_t rand_seed = 0;
00238 int remaining_tests[max_tests];
00239 int test;
00240 int specific_test= -1;
00241
00242 struct SwrContext * forw_ctx= NULL;
00243 struct SwrContext *backw_ctx= NULL;
00244
00245 if (argc > 1) {
00246 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
00247 av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>[ <test>]] \n"
00248 "num_tests Default is %d\n", num_tests);
00249 return 0;
00250 }
00251 num_tests = strtol(argv[1], NULL, 0);
00252 if(num_tests < 0) {
00253 num_tests = -num_tests;
00254 rand_seed = time(0);
00255 }
00256 if(num_tests<= 0 || num_tests>max_tests)
00257 num_tests = max_tests;
00258 if(argc > 2) {
00259 specific_test = strtol(argv[1], NULL, 0);
00260 }
00261 }
00262
00263 for(i=0; i<max_tests; i++)
00264 remaining_tests[i] = i;
00265
00266 for(test=0; test<num_tests; test++){
00267 unsigned r;
00268 uint_rand(seed);
00269 r = (seed * (uint64_t)(max_tests - test)) >>32;
00270 FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
00271 }
00272 qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), (void*)cmp);
00273 in_sample_rate=16000;
00274 for(test=0; test<num_tests; test++){
00275 char in_layout_string[256];
00276 char out_layout_string[256];
00277 unsigned vector= remaining_tests[max_tests - test - 1];
00278 int in_ch_count;
00279 int out_count, mid_count, out_ch_count;
00280
00281 in_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
00282 out_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
00283 in_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
00284 out_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
00285 out_sample_rate = rates [vector % FF_ARRAY_ELEMS(rates )]; vector /= FF_ARRAY_ELEMS(rates);
00286 av_assert0(!vector);
00287
00288 if(specific_test == 0){
00289 if(out_sample_rate != in_sample_rate || in_ch_layout != out_ch_layout)
00290 continue;
00291 }
00292
00293 in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
00294 out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
00295 av_get_channel_layout_string( in_layout_string, sizeof( in_layout_string), in_ch_count, in_ch_layout);
00296 av_get_channel_layout_string(out_layout_string, sizeof(out_layout_string), out_ch_count, out_ch_layout);
00297 fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
00298 in_layout_string, out_layout_string,
00299 in_sample_rate, out_sample_rate,
00300 av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
00301 forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, out_sample_fmt, out_sample_rate,
00302 in_ch_layout, in_sample_fmt, in_sample_rate,
00303 0, 0);
00304 backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
00305 out_ch_layout, out_sample_fmt, out_sample_rate,
00306 0, 0);
00307 if(swr_init( forw_ctx) < 0)
00308 fprintf(stderr, "swr_init(->) failed\n");
00309 if(swr_init(backw_ctx) < 0)
00310 fprintf(stderr, "swr_init(<-) failed\n");
00311 if(!forw_ctx)
00312 fprintf(stderr, "Failed to init forw_cts\n");
00313 if(!backw_ctx)
00314 fprintf(stderr, "Failed to init backw_ctx\n");
00315
00316 setup_array(ain , array_in , in_sample_fmt, SAMPLES);
00317 setup_array(amid, array_mid, out_sample_fmt, 3*SAMPLES);
00318 setup_array(aout, array_out, in_sample_fmt , SAMPLES);
00319 #if 0
00320 for(ch=0; ch<in_ch_count; ch++){
00321 for(i=0; i<SAMPLES; i++)
00322 set(ain, ch, i, in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
00323 }
00324 #else
00325 audiogen(ain, in_sample_fmt, in_ch_count, SAMPLES/6+1, SAMPLES);
00326 #endif
00327 mode = uint_rand(rand_seed) % 3;
00328 if(mode==0 ) {
00329 mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, SAMPLES);
00330 } else if(mode==1){
00331 mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, SAMPLES);
00332 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
00333 } else {
00334 int tmp_count;
00335 mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, 1);
00336 av_assert0(mid_count==0);
00337 shift(ain, 1, in_ch_count, in_sample_fmt);
00338 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
00339 shift(amid, mid_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
00340 mid_count+=swr_convert(forw_ctx, amid, 2, (const uint8_t **)ain, 2);
00341 shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
00342 shift(ain, 2, in_ch_count, in_sample_fmt);
00343 mid_count+=swr_convert(forw_ctx, amid, 1, (const uint8_t **)ain, SAMPLES-3);
00344 shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
00345 shift(ain, -3, in_ch_count, in_sample_fmt);
00346 mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
00347 shift(amid, -tmp_count, out_ch_count, out_sample_fmt);
00348 }
00349 out_count= swr_convert(backw_ctx,aout, SAMPLES, (const uint8_t **)amid, mid_count);
00350
00351 for(ch=0; ch<in_ch_count; ch++){
00352 double sse, maxdiff=0;
00353 double sum_a= 0;
00354 double sum_b= 0;
00355 double sum_aa= 0;
00356 double sum_bb= 0;
00357 double sum_ab= 0;
00358 for(i=0; i<out_count; i++){
00359 double a= get(ain , ch, i, in_ch_count, in_sample_fmt);
00360 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
00361 sum_a += a;
00362 sum_b += b;
00363 sum_aa+= a*a;
00364 sum_bb+= b*b;
00365 sum_ab+= a*b;
00366 maxdiff= FFMAX(maxdiff, FFABS(a-b));
00367 }
00368 sse= sum_aa + sum_bb - 2*sum_ab;
00369 if(sse < 0 && sse > -0.00001) sse=0;
00370
00371 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);
00372 }
00373
00374 flush_i++;
00375 flush_i%=21;
00376 flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
00377 shift(aout, flush_i, in_ch_count, in_sample_fmt);
00378 flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
00379 shift(aout, -flush_i, in_ch_count, in_sample_fmt);
00380 if(flush_count){
00381 for(ch=0; ch<in_ch_count; ch++){
00382 double sse, maxdiff=0;
00383 double sum_a= 0;
00384 double sum_b= 0;
00385 double sum_aa= 0;
00386 double sum_bb= 0;
00387 double sum_ab= 0;
00388 for(i=0; i<flush_count; i++){
00389 double a= get(ain , ch, i+out_count, in_ch_count, in_sample_fmt);
00390 double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
00391 sum_a += a;
00392 sum_b += b;
00393 sum_aa+= a*a;
00394 sum_bb+= b*b;
00395 sum_ab+= a*b;
00396 maxdiff= FFMAX(maxdiff, FFABS(a-b));
00397 }
00398 sse= sum_aa + sum_bb - 2*sum_ab;
00399 if(sse < 0 && sse > -0.00001) sse=0;
00400
00401 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);
00402 }
00403 }
00404
00405
00406 fprintf(stderr, "\n");
00407 }
00408
00409 return 0;
00410 }