avresample-test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Copyright (c) 2002 Fabrice Bellard
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/lfg.h"
  25. #include "libavutil/libm.h"
  26. #include "libavutil/log.h"
  27. #include "libavutil/mem.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/samplefmt.h"
  30. #include "avresample.h"
  31. static double dbl_rand(AVLFG *lfg)
  32. {
  33. return 2.0 * (av_lfg_get(lfg) / (double)UINT_MAX) - 1.0;
  34. }
  35. #define PUT_FUNC(name, fmt, type, expr) \
  36. static void put_sample_ ## name(void **data, enum AVSampleFormat sample_fmt,\
  37. int channels, int sample, int ch, \
  38. double v_dbl) \
  39. { \
  40. type v = expr; \
  41. type **out = (type **)data; \
  42. if (av_sample_fmt_is_planar(sample_fmt)) \
  43. out[ch][sample] = v; \
  44. else \
  45. out[0][sample * channels + ch] = v; \
  46. }
  47. PUT_FUNC(u8, AV_SAMPLE_FMT_U8, uint8_t, av_clip_uint8 ( lrint(v_dbl * (1 << 7)) + 128))
  48. PUT_FUNC(s16, AV_SAMPLE_FMT_S16, int16_t, av_clip_int16 ( lrint(v_dbl * (1 << 15))))
  49. PUT_FUNC(s32, AV_SAMPLE_FMT_S32, int32_t, av_clipl_int32(llrint(v_dbl * (1U << 31))))
  50. PUT_FUNC(flt, AV_SAMPLE_FMT_FLT, float, v_dbl)
  51. PUT_FUNC(dbl, AV_SAMPLE_FMT_DBL, double, v_dbl)
  52. static void put_sample(void **data, enum AVSampleFormat sample_fmt,
  53. int channels, int sample, int ch, double v_dbl)
  54. {
  55. switch (av_get_packed_sample_fmt(sample_fmt)) {
  56. case AV_SAMPLE_FMT_U8:
  57. put_sample_u8(data, sample_fmt, channels, sample, ch, v_dbl);
  58. break;
  59. case AV_SAMPLE_FMT_S16:
  60. put_sample_s16(data, sample_fmt, channels, sample, ch, v_dbl);
  61. break;
  62. case AV_SAMPLE_FMT_S32:
  63. put_sample_s32(data, sample_fmt, channels, sample, ch, v_dbl);
  64. break;
  65. case AV_SAMPLE_FMT_FLT:
  66. put_sample_flt(data, sample_fmt, channels, sample, ch, v_dbl);
  67. break;
  68. case AV_SAMPLE_FMT_DBL:
  69. put_sample_dbl(data, sample_fmt, channels, sample, ch, v_dbl);
  70. break;
  71. }
  72. }
  73. static void audiogen(AVLFG *rnd, void **data, enum AVSampleFormat sample_fmt,
  74. int channels, int sample_rate, int nb_samples)
  75. {
  76. int i, ch, k;
  77. double v, f, a, ampa;
  78. double tabf1[AVRESAMPLE_MAX_CHANNELS];
  79. double tabf2[AVRESAMPLE_MAX_CHANNELS];
  80. double taba[AVRESAMPLE_MAX_CHANNELS];
  81. #define PUT_SAMPLE put_sample(data, sample_fmt, channels, k, ch, v);
  82. k = 0;
  83. /* 1 second of single freq sinus at 1000 Hz */
  84. a = 0;
  85. for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
  86. v = sin(a) * 0.30;
  87. for (ch = 0; ch < channels; ch++)
  88. PUT_SAMPLE
  89. a += M_PI * 1000.0 * 2.0 / sample_rate;
  90. }
  91. /* 1 second of varing frequency between 100 and 10000 Hz */
  92. a = 0;
  93. for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
  94. v = sin(a) * 0.30;
  95. for (ch = 0; ch < channels; ch++)
  96. PUT_SAMPLE
  97. f = 100.0 + (((10000.0 - 100.0) * i) / sample_rate);
  98. a += M_PI * f * 2.0 / sample_rate;
  99. }
  100. /* 0.5 second of low amplitude white noise */
  101. for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
  102. v = dbl_rand(rnd) * 0.30;
  103. for (ch = 0; ch < channels; ch++)
  104. PUT_SAMPLE
  105. }
  106. /* 0.5 second of high amplitude white noise */
  107. for (i = 0; i < sample_rate / 2 && k < nb_samples; i++, k++) {
  108. v = dbl_rand(rnd);
  109. for (ch = 0; ch < channels; ch++)
  110. PUT_SAMPLE
  111. }
  112. /* 1 second of unrelated ramps for each channel */
  113. for (ch = 0; ch < channels; ch++) {
  114. taba[ch] = 0;
  115. tabf1[ch] = 100 + av_lfg_get(rnd) % 5000;
  116. tabf2[ch] = 100 + av_lfg_get(rnd) % 5000;
  117. }
  118. for (i = 0; i < 1 * sample_rate && k < nb_samples; i++, k++) {
  119. for (ch = 0; ch < channels; ch++) {
  120. v = sin(taba[ch]) * 0.30;
  121. PUT_SAMPLE
  122. f = tabf1[ch] + (((tabf2[ch] - tabf1[ch]) * i) / sample_rate);
  123. taba[ch] += M_PI * f * 2.0 / sample_rate;
  124. }
  125. }
  126. /* 2 seconds of 500 Hz with varying volume */
  127. a = 0;
  128. ampa = 0;
  129. for (i = 0; i < 2 * sample_rate && k < nb_samples; i++, k++) {
  130. for (ch = 0; ch < channels; ch++) {
  131. double amp = (1.0 + sin(ampa)) * 0.15;
  132. if (ch & 1)
  133. amp = 0.30 - amp;
  134. v = sin(a) * amp;
  135. PUT_SAMPLE
  136. a += M_PI * 500.0 * 2.0 / sample_rate;
  137. ampa += M_PI * 2.0 / sample_rate;
  138. }
  139. }
  140. }
  141. /* formats, rates, and layouts are ordered for priority in testing.
  142. e.g. 'avresample-test 4 2 2' will test all input/output combinations of
  143. S16/FLTP/S16P/FLT, 48000/44100, and stereo/mono */
  144. static const enum AVSampleFormat formats[] = {
  145. AV_SAMPLE_FMT_S16,
  146. AV_SAMPLE_FMT_FLTP,
  147. AV_SAMPLE_FMT_S16P,
  148. AV_SAMPLE_FMT_FLT,
  149. AV_SAMPLE_FMT_S32P,
  150. AV_SAMPLE_FMT_S32,
  151. AV_SAMPLE_FMT_U8P,
  152. AV_SAMPLE_FMT_U8,
  153. AV_SAMPLE_FMT_DBLP,
  154. AV_SAMPLE_FMT_DBL,
  155. };
  156. static const int rates[] = {
  157. 48000,
  158. 44100,
  159. 16000
  160. };
  161. static const uint64_t layouts[] = {
  162. AV_CH_LAYOUT_STEREO,
  163. AV_CH_LAYOUT_MONO,
  164. AV_CH_LAYOUT_5POINT1,
  165. AV_CH_LAYOUT_7POINT1,
  166. };
  167. int main(int argc, char **argv)
  168. {
  169. AVAudioResampleContext *s;
  170. AVLFG rnd;
  171. int ret = 0;
  172. uint8_t *in_buf = NULL;
  173. uint8_t *out_buf = NULL;
  174. unsigned int in_buf_size;
  175. unsigned int out_buf_size;
  176. uint8_t *in_data[AVRESAMPLE_MAX_CHANNELS] = { 0 };
  177. uint8_t *out_data[AVRESAMPLE_MAX_CHANNELS] = { 0 };
  178. int in_linesize;
  179. int out_linesize;
  180. uint64_t in_ch_layout;
  181. int in_channels;
  182. enum AVSampleFormat in_fmt;
  183. int in_rate;
  184. uint64_t out_ch_layout;
  185. int out_channels;
  186. enum AVSampleFormat out_fmt;
  187. int out_rate;
  188. int num_formats, num_rates, num_layouts;
  189. int i, j, k, l, m, n;
  190. num_formats = 2;
  191. num_rates = 2;
  192. num_layouts = 2;
  193. if (argc > 1) {
  194. if (!av_strncasecmp(argv[1], "-h", 3)) {
  195. av_log(NULL, AV_LOG_INFO, "Usage: avresample-test [<num formats> "
  196. "[<num sample rates> [<num channel layouts>]]]\n"
  197. "Default is 2 2 2\n");
  198. return 0;
  199. }
  200. num_formats = strtol(argv[1], NULL, 0);
  201. num_formats = av_clip(num_formats, 1, FF_ARRAY_ELEMS(formats));
  202. }
  203. if (argc > 2) {
  204. num_rates = strtol(argv[2], NULL, 0);
  205. num_rates = av_clip(num_rates, 1, FF_ARRAY_ELEMS(rates));
  206. }
  207. if (argc > 3) {
  208. num_layouts = strtol(argv[3], NULL, 0);
  209. num_layouts = av_clip(num_layouts, 1, FF_ARRAY_ELEMS(layouts));
  210. }
  211. av_log_set_level(AV_LOG_DEBUG);
  212. av_lfg_init(&rnd, 0xC0FFEE);
  213. in_buf_size = av_samples_get_buffer_size(&in_linesize, 8, 48000 * 6,
  214. AV_SAMPLE_FMT_DBLP, 0);
  215. out_buf_size = in_buf_size;
  216. in_buf = av_malloc(in_buf_size);
  217. if (!in_buf)
  218. goto end;
  219. out_buf = av_malloc(out_buf_size);
  220. if (!out_buf)
  221. goto end;
  222. s = avresample_alloc_context();
  223. if (!s) {
  224. av_log(NULL, AV_LOG_ERROR, "Error allocating AVAudioResampleContext\n");
  225. ret = 1;
  226. goto end;
  227. }
  228. for (i = 0; i < num_formats; i++) {
  229. in_fmt = formats[i];
  230. for (k = 0; k < num_layouts; k++) {
  231. in_ch_layout = layouts[k];
  232. in_channels = av_get_channel_layout_nb_channels(in_ch_layout);
  233. for (m = 0; m < num_rates; m++) {
  234. in_rate = rates[m];
  235. ret = av_samples_fill_arrays(in_data, &in_linesize, in_buf,
  236. in_channels, in_rate * 6,
  237. in_fmt, 0);
  238. if (ret < 0) {
  239. av_log(s, AV_LOG_ERROR, "failed in_data fill arrays\n");
  240. goto end;
  241. }
  242. audiogen(&rnd, (void **)in_data, in_fmt, in_channels, in_rate, in_rate * 6);
  243. for (j = 0; j < num_formats; j++) {
  244. out_fmt = formats[j];
  245. for (l = 0; l < num_layouts; l++) {
  246. out_ch_layout = layouts[l];
  247. out_channels = av_get_channel_layout_nb_channels(out_ch_layout);
  248. for (n = 0; n < num_rates; n++) {
  249. out_rate = rates[n];
  250. av_log(NULL, AV_LOG_INFO, "%s to %s, %d to %d channels, %d Hz to %d Hz\n",
  251. av_get_sample_fmt_name(in_fmt), av_get_sample_fmt_name(out_fmt),
  252. in_channels, out_channels, in_rate, out_rate);
  253. ret = av_samples_fill_arrays(out_data, &out_linesize,
  254. out_buf, out_channels,
  255. out_rate * 6, out_fmt, 0);
  256. if (ret < 0) {
  257. av_log(s, AV_LOG_ERROR, "failed out_data fill arrays\n");
  258. goto end;
  259. }
  260. av_opt_set_int(s, "in_channel_layout", in_ch_layout, 0);
  261. av_opt_set_int(s, "in_sample_fmt", in_fmt, 0);
  262. av_opt_set_int(s, "in_sample_rate", in_rate, 0);
  263. av_opt_set_int(s, "out_channel_layout", out_ch_layout, 0);
  264. av_opt_set_int(s, "out_sample_fmt", out_fmt, 0);
  265. av_opt_set_int(s, "out_sample_rate", out_rate, 0);
  266. av_opt_set_int(s, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
  267. ret = avresample_open(s);
  268. if (ret < 0) {
  269. av_log(s, AV_LOG_ERROR, "Error opening context\n");
  270. goto end;
  271. }
  272. ret = avresample_convert(s, (void **)out_data, out_linesize, out_rate * 6,
  273. (void **) in_data, in_linesize, in_rate * 6);
  274. if (ret < 0) {
  275. char errbuf[256];
  276. av_strerror(ret, errbuf, sizeof(errbuf));
  277. av_log(NULL, AV_LOG_ERROR, "%s\n", errbuf);
  278. goto end;
  279. }
  280. av_log(NULL, AV_LOG_INFO, "Converted %d samples to %d samples\n",
  281. in_rate * 6, ret);
  282. if (avresample_get_delay(s) > 0)
  283. av_log(NULL, AV_LOG_INFO, "%d delay samples not converted\n",
  284. avresample_get_delay(s));
  285. if (avresample_available(s) > 0)
  286. av_log(NULL, AV_LOG_INFO, "%d samples available for output\n",
  287. avresample_available(s));
  288. av_log(NULL, AV_LOG_INFO, "\n");
  289. avresample_close(s);
  290. }
  291. }
  292. }
  293. }
  294. }
  295. }
  296. ret = 0;
  297. end:
  298. av_freep(&in_buf);
  299. av_freep(&out_buf);
  300. avresample_free(&s);
  301. return ret;
  302. }