swresample_test.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "swresample.h"
  24. #undef fprintf
  25. #define SAMPLES 1000
  26. #define ASSERT_LEVEL 2
  27. static double get(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){
  28. const uint8_t *p;
  29. if(av_sample_fmt_is_planar(f)){
  30. f= av_get_alt_sample_fmt(f, 0);
  31. p= a[ch];
  32. }else{
  33. p= a[0];
  34. index= ch + index*ch_count;
  35. }
  36. switch(f){
  37. case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
  38. case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
  39. case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
  40. case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
  41. case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
  42. default: av_assert0(0);
  43. }
  44. }
  45. static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){
  46. uint8_t *p;
  47. if(av_sample_fmt_is_planar(f)){
  48. f= av_get_alt_sample_fmt(f, 0);
  49. p= a[ch];
  50. }else{
  51. p= a[0];
  52. index= ch + index*ch_count;
  53. }
  54. switch(f){
  55. case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= (v+1.0)*255.0/2; break;
  56. case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= v*32767; break;
  57. case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= v*2147483647; break;
  58. case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
  59. case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
  60. default: av_assert2(0);
  61. }
  62. }
  63. uint64_t layouts[]={
  64. AV_CH_LAYOUT_MONO ,
  65. AV_CH_LAYOUT_STEREO ,
  66. AV_CH_LAYOUT_2_1 ,
  67. AV_CH_LAYOUT_SURROUND ,
  68. AV_CH_LAYOUT_4POINT0 ,
  69. AV_CH_LAYOUT_2_2 ,
  70. AV_CH_LAYOUT_QUAD ,
  71. AV_CH_LAYOUT_5POINT0 ,
  72. AV_CH_LAYOUT_5POINT1 ,
  73. AV_CH_LAYOUT_5POINT0_BACK ,
  74. AV_CH_LAYOUT_5POINT1_BACK ,
  75. AV_CH_LAYOUT_7POINT0 ,
  76. AV_CH_LAYOUT_7POINT1 ,
  77. AV_CH_LAYOUT_7POINT1_WIDE ,
  78. 0
  79. };
  80. static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){
  81. if(av_sample_fmt_is_planar(format)){
  82. int i;
  83. int plane_size= av_get_bytes_per_sample(format&0xFF)*samples;
  84. format&=0xFF;
  85. for(i=0; i<SWR_CH_MAX; i++){
  86. out[i]= in + i*plane_size;
  87. }
  88. }else{
  89. out[0]= in;
  90. }
  91. }
  92. int main(int argc, char **argv){
  93. int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr, flush_count;
  94. uint64_t in_ch_layout, out_ch_layout;
  95. enum AVSampleFormat in_sample_fmt, out_sample_fmt;
  96. int sample_rates[]={8000,11025,16000,22050,32000};
  97. uint8_t array_in[SAMPLES*8*8];
  98. uint8_t array_mid[SAMPLES*8*8*3];
  99. uint8_t array_out[SAMPLES*8*8+100];
  100. uint8_t *ain[SWR_CH_MAX];
  101. uint8_t *aout[SWR_CH_MAX];
  102. uint8_t *amid[SWR_CH_MAX];
  103. struct SwrContext * forw_ctx= NULL;
  104. struct SwrContext *backw_ctx= NULL;
  105. in_sample_rate=16000;
  106. for(osr=0; osr<5; osr++){
  107. out_sample_rate= sample_rates[osr];
  108. for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
  109. for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
  110. for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
  111. int in_ch_count;
  112. in_ch_layout= layouts[in_ch_layout_index];
  113. in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
  114. for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
  115. int out_count, mid_count, out_ch_count;
  116. out_ch_layout= layouts[out_ch_layout_index];
  117. out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
  118. fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s",
  119. in_ch_count, out_ch_count,
  120. in_sample_rate, out_sample_rate,
  121. av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
  122. forw_ctx = swr_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
  123. in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1), in_sample_rate,
  124. 0, 0);
  125. backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate,
  126. out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate,
  127. 0, 0);
  128. if(swr_init( forw_ctx) < 0)
  129. fprintf(stderr, "swr_init(->) failed\n");
  130. if(swr_init(backw_ctx) < 0)
  131. fprintf(stderr, "swr_init(<-) failed\n");
  132. if(!forw_ctx)
  133. fprintf(stderr, "Failed to init forw_cts\n");
  134. if(!backw_ctx)
  135. fprintf(stderr, "Failed to init backw_ctx\n");
  136. //FIXME test planar
  137. setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1), SAMPLES);
  138. setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES);
  139. setup_array(aout, array_out, in_sample_fmt , SAMPLES);
  140. for(ch=0; ch<in_ch_count; ch++){
  141. for(i=0; i<SAMPLES; i++)
  142. set(ain, ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1), sin(i*i*3/SAMPLES));
  143. }
  144. mid_count= swr_convert(forw_ctx, amid, 3*SAMPLES, ain, SAMPLES);
  145. out_count= swr_convert(backw_ctx,aout, SAMPLES, amid, mid_count);
  146. for(ch=0; ch<in_ch_count; ch++){
  147. double sse, x, maxdiff=0;
  148. double sum_a= 0;
  149. double sum_b= 0;
  150. double sum_aa= 0;
  151. double sum_bb= 0;
  152. double sum_ab= 0;
  153. for(i=0; i<out_count; i++){
  154. double a= get(ain , ch, i, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  155. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  156. sum_a += a;
  157. sum_b += b;
  158. sum_aa+= a*a;
  159. sum_bb+= b*b;
  160. sum_ab+= a*b;
  161. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  162. }
  163. x = sum_ab/sum_bb;
  164. sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
  165. fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/out_count), x, maxdiff, out_count);
  166. }
  167. flush_count=swr_convert(backw_ctx,aout, SAMPLES, 0, 0);
  168. if(flush_count){
  169. for(ch=0; ch<in_ch_count; ch++){
  170. double sse, x, maxdiff=0;
  171. double sum_a= 0;
  172. double sum_b= 0;
  173. double sum_aa= 0;
  174. double sum_bb= 0;
  175. double sum_ab= 0;
  176. for(i=0; i<flush_count; i++){
  177. double a= get(ain , ch, i+out_count, in_ch_count, av_get_alt_sample_fmt(in_sample_fmt, 1));
  178. double b= get(aout, ch, i, in_ch_count, in_sample_fmt);
  179. sum_a += a;
  180. sum_b += b;
  181. sum_aa+= a*a;
  182. sum_bb+= b*b;
  183. sum_ab+= a*b;
  184. maxdiff= FFMAX(maxdiff, FFABS(a-b));
  185. }
  186. x = sum_ab/sum_bb;
  187. sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
  188. fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/flush_count), x, maxdiff, flush_count);
  189. }
  190. }
  191. fprintf(stderr, "\n");
  192. }
  193. }
  194. }
  195. }
  196. }
  197. return 0;
  198. }