avresample-test.c 12 KB

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