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.

310 lines
14KB

  1. /*
  2. * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
  3. *
  4. * This file is part of libswresample
  5. *
  6. * libswresample is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * libswresample is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with libswresample; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/common.h"
  22. #include "libavutil/audioconvert.h"
  23. #include "libavutil/opt.h"
  24. #include "swresample.h"
  25. #undef fprintf
  26. #define SAMPLES 1000
  27. #define ASSERT_LEVEL 2
  28. static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
  29. const uint8_t *p;
  30. if(av_sample_fmt_is_planar(f)){
  31. f= av_get_alt_sample_fmt(f, 0);
  32. p= a[ch];
  33. }else{
  34. p= a[0];
  35. index= ch + index*ch_count;
  36. }
  37. switch(f){
  38. case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
  39. case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
  40. case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
  41. case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
  42. case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
  43. default: av_assert0(0);
  44. }
  45. }
  46. static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
  47. uint8_t *p;
  48. if(av_sample_fmt_is_planar(f)){
  49. f= av_get_alt_sample_fmt(f, 0);
  50. p= a[ch];
  51. }else{
  52. p= a[0];
  53. index= ch + index*ch_count;
  54. }
  55. switch(f){
  56. case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= (v+1.0)*255.0/2; break;
  57. case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= v*32767; break;
  58. case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= v*2147483647; break;
  59. case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
  60. case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
  61. default: av_assert2(0);
  62. }
  63. }
  64. static void shift(uint8_t *a[], int index, int ch_count, enum AVSampleFormat f){
  65. int ch;
  66. if(av_sample_fmt_is_planar(f)){
  67. f= av_get_alt_sample_fmt(f, 0);
  68. for(ch= 0; ch<ch_count; ch++)
  69. a[ch] += index*av_get_bytes_per_sample(f);
  70. }else{
  71. a[0] += index*ch_count*av_get_bytes_per_sample(f);
  72. }
  73. }
  74. static const enum AVSampleFormat formats[] = {
  75. AV_SAMPLE_FMT_S16,
  76. AV_SAMPLE_FMT_FLTP,
  77. AV_SAMPLE_FMT_S16P,
  78. AV_SAMPLE_FMT_FLT,
  79. AV_SAMPLE_FMT_S32P,
  80. AV_SAMPLE_FMT_S32,
  81. AV_SAMPLE_FMT_U8P,
  82. AV_SAMPLE_FMT_U8,
  83. AV_SAMPLE_FMT_DBLP,
  84. AV_SAMPLE_FMT_DBL,
  85. };
  86. static const int rates[] = {
  87. 8000,
  88. 11025,
  89. 16000,
  90. 22050,
  91. 32000,
  92. 48000,
  93. };
  94. uint64_t layouts[]={
  95. AV_CH_LAYOUT_MONO ,
  96. AV_CH_LAYOUT_STEREO ,
  97. AV_CH_LAYOUT_2_1 ,
  98. AV_CH_LAYOUT_SURROUND ,
  99. AV_CH_LAYOUT_4POINT0 ,
  100. AV_CH_LAYOUT_2_2 ,
  101. AV_CH_LAYOUT_QUAD ,
  102. AV_CH_LAYOUT_5POINT0 ,
  103. AV_CH_LAYOUT_5POINT1 ,
  104. AV_CH_LAYOUT_5POINT0_BACK ,
  105. AV_CH_LAYOUT_5POINT1_BACK ,
  106. AV_CH_LAYOUT_7POINT0 ,
  107. AV_CH_LAYOUT_7POINT1 ,
  108. AV_CH_LAYOUT_7POINT1_WIDE ,
  109. };
  110. static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
  111. if(av_sample_fmt_is_planar(format)){
  112. int i;
  113. int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
  114. format&=0xFF;
  115. for(i=0; i<SWR_CH_MAX; i++){
  116. out[i]= in + i*plane_size;
  117. }
  118. }else{
  119. out[0]= in;
  120. }
  121. }
  122. static int cmp(const int *a, const int *b){
  123. return *a - *b;
  124. }
  125. int main(int argc, char **argv){
  126. int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
  127. int in_sample_fmt_index, out_sample_fmt_index;
  128. uint64_t in_ch_layout, out_ch_layout;
  129. enum AVSampleFormat in_sample_fmt, out_sample_fmt;
  130. uint8_t array_in[SAMPLES*8*8];
  131. uint8_t array_mid[SAMPLES*8*8*3];
  132. uint8_t array_out[SAMPLES*8*8+100];
  133. uint8_t *ain[SWR_CH_MAX];
  134. uint8_t *aout[SWR_CH_MAX];
  135. uint8_t *amid[SWR_CH_MAX];
  136. int flush_i=0;
  137. int mode = 0;
  138. int max_tests = FF_ARRAY_ELEMS(rates) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats) * FF_ARRAY_ELEMS(layouts) * FF_ARRAY_ELEMS(formats);
  139. int num_tests = 10000;
  140. uint32_t seed = 0;
  141. int remaining_tests[max_tests];
  142. int test;
  143. struct SwrContext * forw_ctx= NULL;
  144. struct SwrContext *backw_ctx= NULL;
  145. if (argc > 1) {
  146. if (!strcmp(argv[1], "-h")) {
  147. av_log(NULL, AV_LOG_INFO, "Usage: swresample-test [<num_tests>]\n"
  148. "Default is %d\n", num_tests);
  149. return 0;
  150. }
  151. num_tests = strtol(argv[1], NULL, 0);
  152. if(num_tests<= 0 || num_tests>max_tests)
  153. num_tests = max_tests;
  154. }
  155. for(i=0; i<max_tests; i++)
  156. remaining_tests[i] = i;
  157. for(test=0; test<num_tests; test++){
  158. unsigned r;
  159. seed = seed * 1664525 + 1013904223;
  160. r = (seed * (uint64_t)(max_tests - test)) >>32;
  161. FFSWAP(int, remaining_tests[r], remaining_tests[max_tests - test - 1]);
  162. }
  163. qsort(remaining_tests + max_tests - num_tests, num_tests, sizeof(remaining_tests[0]), cmp);
  164. in_sample_rate=16000;
  165. for(test=0; test<num_tests; test++){
  166. char in_layout_string[256];
  167. char out_layout_string[256];
  168. unsigned vector= remaining_tests[max_tests - test - 1];
  169. in_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
  170. out_ch_layout = layouts[vector % FF_ARRAY_ELEMS(layouts)]; vector /= FF_ARRAY_ELEMS(layouts);
  171. in_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
  172. out_sample_fmt = formats[vector % FF_ARRAY_ELEMS(formats)]; vector /= FF_ARRAY_ELEMS(formats);
  173. out_sample_rate = rates [vector % FF_ARRAY_ELEMS(rates )]; vector /= FF_ARRAY_ELEMS(rates);
  174. av_assert0(!vector);
  175. int in_ch_count;
  176. in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
  177. int out_count, mid_count, out_ch_count;
  178. out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
  179. av_get_channel_layout_string( in_layout_string, sizeof( in_layout_string), in_ch_count, in_ch_layout);
  180. av_get_channel_layout_string(out_layout_string, sizeof(out_layout_string), out_ch_count, out_ch_layout);
  181. fprintf(stderr, "TEST: %s->%s, rate:%5d->%5d, fmt:%s->%s\n",
  182. in_layout_string, out_layout_string,
  183. in_sample_rate, out_sample_rate,
  184. av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
  185. forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
  186. in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1), in_sample_rate,
  187. 0, 0);
  188. backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
  189. out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
  190. 0, 0);
  191. if(swr_init( forw_ctx) < 0)
  192. fprintf(stderr, "swr_init(->) failed\n");
  193. if(swr_init(backw_ctx) < 0)
  194. fprintf(stderr, "swr_init(<-) failed\n");
  195. if(!forw_ctx)
  196. fprintf(stderr, "Failed to init forw_cts\n");
  197. if(!backw_ctx)
  198. fprintf(stderr, "Failed to init backw_ctx\n");
  199. //FIXME test planar
  200. setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1), SAMPLES);
  201. setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES);
  202. setup_array(aout, array_out, in_sample_fmt , SAMPLES);
  203. for(ch=0; ch<in_ch_count; ch++){
  204. for(i=0; i<SAMPLES; i++)
  205. set(ain, ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1), sin(i*i*3/SAMPLES));
  206. }
  207. mode++;
  208. mode%=3;
  209. if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
  210. mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
  211. } else if(mode==1){
  212. mid_count= swr_convert(forw_ctx, amid, 0, ain, SAMPLES);
  213. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain, 0);
  214. } else {
  215. int tmp_count;
  216. mid_count= swr_convert(forw_ctx, amid, 0, ain, 1);
  217. av_assert0(mid_count==0);
  218. shift(ain, 1, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  219. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain, 0);
  220. shift(amid, mid_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
  221. mid_count+=swr_convert(forw_ctx, amid, 2, ain, 2);
  222. shift(amid, mid_count-tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
  223. shift(ain, 2, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  224. mid_count+=swr_convert(forw_ctx, amid, 1, ain, SAMPLES-3);
  225. shift(amid, mid_count-tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
  226. shift(ain, -3, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  227. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain, 0);
  228. shift(amid, -tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1));
  229. }
  230. out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
  231. for(ch=0; ch<in_ch_count; ch++){
  232. double sse, x, maxdiff=0;
  233. double sum_a= 0;
  234. double sum_b= 0;
  235. double sum_aa= 0;
  236. double sum_bb= 0;
  237. double sum_ab= 0;
  238. for(i=0; i<out_count; i++){
  239. double a= get(ain , ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  240. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  241. sum_a += a;
  242. sum_b += b;
  243. sum_aa+= a*a;
  244. sum_bb+= b*b;
  245. sum_ab+= a*b;
  246. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  247. }
  248. sse= sum_aa + sum_bb - 2*sum_ab;
  249. 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);
  250. }
  251. flush_i++;
  252. flush_i%=21;
  253. flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
  254. shift(aout, flush_i, in_ch_count, in_sample_fmt);
  255. flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
  256. shift(aout, -flush_i, in_ch_count, in_sample_fmt);
  257. if(flush_count){
  258. for(ch=0; ch<in_ch_count; ch++){
  259. double sse, x, maxdiff=0;
  260. double sum_a= 0;
  261. double sum_b= 0;
  262. double sum_aa= 0;
  263. double sum_bb= 0;
  264. double sum_ab= 0;
  265. for(i=0; i<flush_count; i++){
  266. double a= get(ain , ch, i+out_count, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  267. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  268. sum_a += a;
  269. sum_b += b;
  270. sum_aa+= a*a;
  271. sum_bb+= b*b;
  272. sum_ab+= a*b;
  273. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  274. }
  275. sse= sum_aa + sum_bb - 2*sum_ab;
  276. 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);
  277. }
  278. }
  279. fprintf(stderr, "\n");
  280. }
  281. return 0;
  282. }