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.

280 lines
12KB

  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. 0
  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. int main(int argc, char **argv){
  124. int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
  125. uint64_t in_ch_layout, out_ch_layout;
  126. enum AVSampleFormat in_sample_fmt, out_sample_fmt;
  127. uint8_t array_in[SAMPLES*8*8];
  128. uint8_t array_mid[SAMPLES*8*8*3];
  129. uint8_t array_out[SAMPLES*8*8+100];
  130. uint8_t *ain[SWR_CH_MAX];
  131. uint8_t *aout[SWR_CH_MAX];
  132. uint8_t *amid[SWR_CH_MAX];
  133. int flush_i=0;
  134. int mode = 0;
  135. struct SwrContext * forw_ctx= NULL;
  136. struct SwrContext *backw_ctx= NULL;
  137. in_sample_rate=16000;
  138. for(osr=0; osr<FF_ARRAY_ELEMS(rates); osr++){
  139. out_sample_rate= rates[osr];
  140. for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
  141. for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
  142. for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
  143. int in_ch_count;
  144. in_ch_layout= layouts[in_ch_layout_index];
  145. in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
  146. for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
  147. int out_count, mid_count, out_ch_count;
  148. out_ch_layout= layouts[out_ch_layout_index];
  149. out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
  150. fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s\n",
  151. in_ch_count, out_ch_count,
  152. in_sample_rate, out_sample_rate,
  153. av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
  154. forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
  155. in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1), in_sample_rate,
  156. 0, 0);
  157. backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
  158. out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
  159. 0, 0);
  160. if(swr_init( forw_ctx) < 0)
  161. fprintf(stderr, "swr_init(->) failed\n");
  162. if(swr_init(backw_ctx) < 0)
  163. fprintf(stderr, "swr_init(<-) failed\n");
  164. if(!forw_ctx)
  165. fprintf(stderr, "Failed to init forw_cts\n");
  166. if(!backw_ctx)
  167. fprintf(stderr, "Failed to init backw_ctx\n");
  168. //FIXME test planar
  169. setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1), SAMPLES);
  170. setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES);
  171. setup_array(aout, array_out, in_sample_fmt , SAMPLES);
  172. for(ch=0; ch<in_ch_count; ch++){
  173. for(i=0; i<SAMPLES; i++)
  174. set(ain, ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1), sin(i*i*3/SAMPLES));
  175. }
  176. mode++;
  177. mode%=3;
  178. if(mode==0 /*|| out_sample_rate == in_sample_rate*/) {
  179. mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
  180. } else if(mode==1){
  181. mid_count= swr_convert(forw_ctx, amid, 0, ain, SAMPLES);
  182. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain, 0);
  183. } else {
  184. int tmp_count;
  185. mid_count= swr_convert(forw_ctx, amid, 0, ain, 1);
  186. av_assert0(mid_count==0);
  187. shift(ain, 1, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  188. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain, 0);
  189. shift(amid, mid_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
  190. mid_count+=swr_convert(forw_ctx, amid, 2, ain, 2);
  191. shift(amid, mid_count-tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
  192. shift(ain, 2, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  193. mid_count+=swr_convert(forw_ctx, amid, 1, ain, SAMPLES-3);
  194. shift(amid, mid_count-tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1)); tmp_count = mid_count;
  195. shift(ain, -3, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  196. mid_count+=swr_convert(forw_ctx, amid, 3*SAMPLES, ain, 0);
  197. shift(amid, -tmp_count, out_ch_count, av_get_alt_sample_fmt(out_sample_fmt, 1));
  198. }
  199. out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
  200. for(ch=0; ch<in_ch_count; ch++){
  201. double sse, x, maxdiff=0;
  202. double sum_a= 0;
  203. double sum_b= 0;
  204. double sum_aa= 0;
  205. double sum_bb= 0;
  206. double sum_ab= 0;
  207. for(i=0; i<out_count; i++){
  208. double a= get(ain , ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  209. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  210. sum_a += a;
  211. sum_b += b;
  212. sum_aa+= a*a;
  213. sum_bb+= b*b;
  214. sum_ab+= a*b;
  215. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  216. }
  217. x = sum_ab/sum_bb;
  218. sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
  219. fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/out_count), x, maxdiff, out_count);
  220. }
  221. flush_i++;
  222. flush_i%=21;
  223. flush_count = swr_convert(backw_ctx,aout, flush_i, 0, 0);
  224. shift(aout, flush_i, in_ch_count, in_sample_fmt);
  225. flush_count+= swr_convert(backw_ctx,aout, SAMPLES-flush_i, 0, 0);
  226. shift(aout, -flush_i, in_ch_count, in_sample_fmt);
  227. if(flush_count){
  228. for(ch=0; ch<in_ch_count; ch++){
  229. double sse, x, maxdiff=0;
  230. double sum_a= 0;
  231. double sum_b= 0;
  232. double sum_aa= 0;
  233. double sum_bb= 0;
  234. double sum_ab= 0;
  235. for(i=0; i<flush_count; i++){
  236. double a= get(ain , ch, i+out_count, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  237. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  238. sum_a += a;
  239. sum_b += b;
  240. sum_aa+= a*a;
  241. sum_bb+= b*b;
  242. sum_ab+= a*b;
  243. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  244. }
  245. x = sum_ab/sum_bb;
  246. sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
  247. fprintf(stderr, "[%f %f %f] len:%5d F:%3d\n", sqrt(sse/flush_count), x, maxdiff, flush_count, flush_i);
  248. }
  249. }
  250. fprintf(stderr, "\n");
  251. }
  252. }
  253. }
  254. }
  255. }
  256. return 0;
  257. }