swresample-test.c 15 KB

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