swresample.c 15 KB

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