You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
15KB

  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. #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 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(lrint(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 max_tests = FF_ARRAY_ELEMS(rates) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats);
  212. int num_tests = 10000;
  213. uint32_t seed = 0;
  214. uint32_t rand_seed = 0;
  215. int remaining_tests[max_tests];
  216. int test;
  217. struct SwrContext * forw_ctx= NULL;
  218. struct SwrContext *backw_ctx= NULL;
  219. if (argc > 1) {
  220. if (!strcmp(argv[1], "-h")) {
  221. av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>]\n"
  222. "Default is %d\n", num_tests);
  223. return 0;
  224. }
  225. num_tests = strtol(argv[1], NULL, 0);
  226. if(num_tests < 0) {
  227. num_tests = -num_tests;
  228. rand_seed = time(0);
  229. }
  230. if(num_tests<= 0 || num_tests>max_tests)
  231. num_tests = max_tests;
  232. }
  233. for(i=0; i<max_tests; i++)
  234. remaining_tests[i] = i;
  235. for(test=0; test<num_tests; test++){
  236. unsigned r;
  237. uint_rand(seed);
  238. r = (seed * (uint64_t)(max_tests - test)) >>32;
  239. FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
  240. }
  241. qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), (void*)cmp);
  242. in_sample_rate=16000;
  243. for(test=0; test<num_tests; test++){
  244. char in_layout_string[256];
  245. char out_layout_string[256];
  246. unsigned vector= remaining_tests[max_tests - test - 1];
  247. int in_ch_count;
  248. int out_count, mid_count, out_ch_count;
  249. in_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
  250. out_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
  251. in_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
  252. out_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
  253. out_sample_rate = rates [vector % FF_ARRAY_ELEMS(rates )]; vector /= FF_ARRAY_ELEMS(rates);
  254. av_assert0(!vector);
  255. in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
  256. out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
  257. av_get_channel_layout_string( in_layout_string, sizeof( in_layout_string), in_ch_count, in_ch_layout);
  258. av_get_channel_layout_string(out_layout_string, sizeof(out_layout_string), out_ch_count, out_ch_layout);
  259. fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
  260. in_layout_string, out_layout_string,
  261. in_sample_rate, out_sample_rate,
  262. av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
  263. forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, out_sample_fmt, out_sample_rate,
  264. in_ch_layout, in_sample_fmt, in_sample_rate,
  265. 0, 0);
  266. backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
  267. out_ch_layout, out_sample_fmt, out_sample_rate,
  268. 0, 0);
  269. if(swr_init( forw_ctx) < 0)
  270. fprintf(stderr, "swr_init(->) failed\n");
  271. if(swr_init(backw_ctx) < 0)
  272. fprintf(stderr, "swr_init(<-) failed\n");
  273. if(!forw_ctx)
  274. fprintf(stderr, "Failed to init forw_cts\n");
  275. if(!backw_ctx)
  276. fprintf(stderr, "Failed to init backw_ctx\n");
  277. //FIXME test planar
  278. setup_array(ain , array_in , in_sample_fmt, SAMPLES);
  279. setup_array(amid, array_mid, out_sample_fmt, 3*SAMPLES);
  280. setup_array(aout, array_out, in_sample_fmt , SAMPLES);
  281. #if 0
  282. for(ch=0; ch<in_ch_count; ch++){
  283. for(i=0; i<SAMPLES; i++)
  284. set(ain, ch, i, in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
  285. }
  286. #else
  287. audiogen(ain, in_sample_fmt, in_ch_count, SAMPLES/6+1, SAMPLES);
  288. #endif
  289. mode = uint_rand(rand_seed) % 3;
  290. if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
  291. mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, SAMPLES);
  292. } else if(mode==1){
  293. mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, SAMPLES);
  294. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
  295. } else {
  296. int tmp_count;
  297. mid_count= swr_convert(forw_ctx, amid, 0, (const uint8_t **)ain, 1);
  298. av_assert0(mid_count==0);
  299. shift(ain, 1, in_ch_count, in_sample_fmt);
  300. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
  301. shift(amid, mid_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
  302. mid_count+=swr_convert(forw_ctx, amid, 2, (const uint8_t **)ain, 2);
  303. shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
  304. shift(ain, 2, in_ch_count, in_sample_fmt);
  305. mid_count+=swr_convert(forw_ctx, amid, 1, (const uint8_t **)ain, SAMPLES-3);
  306. shift(amid, mid_count-tmp_count, out_ch_count, out_sample_fmt); tmp_count = mid_count;
  307. shift(ain, -3, in_ch_count, in_sample_fmt);
  308. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, (const uint8_t **)ain, 0);
  309. shift(amid, -tmp_count, out_ch_count, out_sample_fmt);
  310. }
  311. out_count= swr_convert(backw_ctx,aout, SAMPLES, (const uint8_t **)amid, mid_count);
  312. for(ch=0; ch<in_ch_count; ch++){
  313. double sse, maxdiff=0;
  314. double sum_a= 0;
  315. double sum_b= 0;
  316. double sum_aa= 0;
  317. double sum_bb= 0;
  318. double sum_ab= 0;
  319. for(i=0; i<out_count; i++){
  320. double a= get(ain , ch, i, in_ch_count, in_sample_fmt);
  321. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  322. sum_a += a;
  323. sum_b += b;
  324. sum_aa+= a*a;
  325. sum_bb+= b*b;
  326. sum_ab+= a*b;
  327. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  328. }
  329. sse= sum_aa + sum_bb - 2*sum_ab;
  330. 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);
  331. }
  332. flush_i++;
  333. flush_i%=21;
  334. flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
  335. shift(aout, flush_i, in_ch_count, in_sample_fmt);
  336. flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
  337. shift(aout, -flush_i, in_ch_count, in_sample_fmt);
  338. if(flush_count){
  339. for(ch=0; ch<in_ch_count; ch++){
  340. double sse, maxdiff=0;
  341. double sum_a= 0;
  342. double sum_b= 0;
  343. double sum_aa= 0;
  344. double sum_bb= 0;
  345. double sum_ab= 0;
  346. for(i=0; i<flush_count; i++){
  347. double a= get(ain , ch, i+out_count, in_ch_count, in_sample_fmt);
  348. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  349. sum_a += a;
  350. sum_b += b;
  351. sum_aa+= a*a;
  352. sum_bb+= b*b;
  353. sum_ab+= a*b;
  354. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  355. }
  356. sse= sum_aa + sum_bb - 2*sum_ab;
  357. 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);
  358. }
  359. }
  360. fprintf(stderr, "\n");
  361. }
  362. return 0;
  363. }