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.

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