af_aresample.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2011 Stefano Sabatini
  3. * Copyright (c) 2011 Mina Nagy Zaki
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * resampling audio filter
  24. */
  25. #include "libavutil/eval.h"
  26. #include "libavcodec/avcodec.h"
  27. #include "avfilter.h"
  28. #include "internal.h"
  29. typedef struct {
  30. struct AVResampleContext *resample;
  31. int out_rate;
  32. double ratio;
  33. AVFilterBufferRef *outsamplesref;
  34. int unconsumed_nb_samples,
  35. max_cached_nb_samples;
  36. int16_t *cached_data[8],
  37. *resampled_data[8];
  38. } AResampleContext;
  39. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  40. {
  41. AResampleContext *aresample = ctx->priv;
  42. int ret;
  43. if (args) {
  44. if ((ret = ff_parse_sample_rate(&aresample->out_rate, args, ctx)) < 0)
  45. return ret;
  46. } else {
  47. aresample->out_rate = -1;
  48. }
  49. return 0;
  50. }
  51. static av_cold void uninit(AVFilterContext *ctx)
  52. {
  53. AResampleContext *aresample = ctx->priv;
  54. if (aresample->outsamplesref) {
  55. int nb_channels =
  56. av_get_channel_layout_nb_channels(
  57. aresample->outsamplesref->audio->channel_layout);
  58. avfilter_unref_buffer(aresample->outsamplesref);
  59. while (nb_channels--) {
  60. av_freep(&(aresample->cached_data[nb_channels]));
  61. av_freep(&(aresample->resampled_data[nb_channels]));
  62. }
  63. }
  64. if (aresample->resample)
  65. av_resample_close(aresample->resample);
  66. }
  67. static int config_output(AVFilterLink *outlink)
  68. {
  69. AVFilterContext *ctx = outlink->src;
  70. AVFilterLink *inlink = ctx->inputs[0];
  71. AResampleContext *aresample = ctx->priv;
  72. if (aresample->out_rate == -1)
  73. aresample->out_rate = outlink->sample_rate;
  74. else
  75. outlink->sample_rate = aresample->out_rate;
  76. outlink->time_base = (AVRational) {1, aresample->out_rate};
  77. //TODO: make the resampling parameters configurable
  78. aresample->resample = av_resample_init(aresample->out_rate, inlink->sample_rate,
  79. 16, 10, 0, 0.8);
  80. aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
  81. av_log(ctx, AV_LOG_INFO, "r:%"PRId64"Hz -> r:%"PRId64"Hz\n",
  82. inlink->sample_rate, outlink->sample_rate);
  83. return 0;
  84. }
  85. static int query_formats(AVFilterContext *ctx)
  86. {
  87. AVFilterFormats *formats = NULL;
  88. avfilter_add_format(&formats, AV_SAMPLE_FMT_S16);
  89. if (!formats)
  90. return AVERROR(ENOMEM);
  91. avfilter_set_common_sample_formats(ctx, formats);
  92. formats = avfilter_make_all_channel_layouts();
  93. if (!formats)
  94. return AVERROR(ENOMEM);
  95. avfilter_set_common_channel_layouts(ctx, formats);
  96. formats = avfilter_make_all_packing_formats();
  97. if (!formats)
  98. return AVERROR(ENOMEM);
  99. avfilter_set_common_packing_formats(ctx, formats);
  100. return 0;
  101. }
  102. static void deinterleave(int16_t **outp, int16_t *in,
  103. int nb_channels, int nb_samples)
  104. {
  105. int16_t *out[8];
  106. memcpy(out, outp, nb_channels * sizeof(int16_t*));
  107. switch (nb_channels) {
  108. case 2:
  109. while (nb_samples--) {
  110. *out[0]++ = *in++;
  111. *out[1]++ = *in++;
  112. }
  113. break;
  114. case 3:
  115. while (nb_samples--) {
  116. *out[0]++ = *in++;
  117. *out[1]++ = *in++;
  118. *out[2]++ = *in++;
  119. }
  120. break;
  121. case 4:
  122. while (nb_samples--) {
  123. *out[0]++ = *in++;
  124. *out[1]++ = *in++;
  125. *out[2]++ = *in++;
  126. *out[3]++ = *in++;
  127. }
  128. break;
  129. case 5:
  130. while (nb_samples--) {
  131. *out[0]++ = *in++;
  132. *out[1]++ = *in++;
  133. *out[2]++ = *in++;
  134. *out[3]++ = *in++;
  135. *out[4]++ = *in++;
  136. }
  137. break;
  138. case 6:
  139. while (nb_samples--) {
  140. *out[0]++ = *in++;
  141. *out[1]++ = *in++;
  142. *out[2]++ = *in++;
  143. *out[3]++ = *in++;
  144. *out[4]++ = *in++;
  145. *out[5]++ = *in++;
  146. }
  147. break;
  148. case 8:
  149. while (nb_samples--) {
  150. *out[0]++ = *in++;
  151. *out[1]++ = *in++;
  152. *out[2]++ = *in++;
  153. *out[3]++ = *in++;
  154. *out[4]++ = *in++;
  155. *out[5]++ = *in++;
  156. *out[6]++ = *in++;
  157. *out[7]++ = *in++;
  158. }
  159. break;
  160. }
  161. }
  162. static void interleave(int16_t *out, int16_t **inp,
  163. int nb_channels, int nb_samples)
  164. {
  165. int16_t *in[8];
  166. memcpy(in, inp, nb_channels * sizeof(int16_t*));
  167. switch (nb_channels) {
  168. case 2:
  169. while (nb_samples--) {
  170. *out++ = *in[0]++;
  171. *out++ = *in[1]++;
  172. }
  173. break;
  174. case 3:
  175. while (nb_samples--) {
  176. *out++ = *in[0]++;
  177. *out++ = *in[1]++;
  178. *out++ = *in[2]++;
  179. }
  180. break;
  181. case 4:
  182. while (nb_samples--) {
  183. *out++ = *in[0]++;
  184. *out++ = *in[1]++;
  185. *out++ = *in[2]++;
  186. *out++ = *in[3]++;
  187. }
  188. break;
  189. case 5:
  190. while (nb_samples--) {
  191. *out++ = *in[0]++;
  192. *out++ = *in[1]++;
  193. *out++ = *in[2]++;
  194. *out++ = *in[3]++;
  195. *out++ = *in[4]++;
  196. }
  197. break;
  198. case 6:
  199. while (nb_samples--) {
  200. *out++ = *in[0]++;
  201. *out++ = *in[1]++;
  202. *out++ = *in[2]++;
  203. *out++ = *in[3]++;
  204. *out++ = *in[4]++;
  205. *out++ = *in[5]++;
  206. }
  207. break;
  208. case 8:
  209. while (nb_samples--) {
  210. *out++ = *in[0]++;
  211. *out++ = *in[1]++;
  212. *out++ = *in[2]++;
  213. *out++ = *in[3]++;
  214. *out++ = *in[4]++;
  215. *out++ = *in[5]++;
  216. *out++ = *in[6]++;
  217. *out++ = *in[7]++;
  218. }
  219. break;
  220. }
  221. }
  222. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  223. {
  224. AResampleContext *aresample = inlink->dst->priv;
  225. AVFilterLink * const outlink = inlink->dst->outputs[0];
  226. int i,
  227. in_nb_samples = insamplesref->audio->nb_samples,
  228. cached_nb_samples = in_nb_samples + aresample->unconsumed_nb_samples,
  229. requested_out_nb_samples = aresample->ratio * cached_nb_samples,
  230. nb_channels =
  231. av_get_channel_layout_nb_channels(inlink->channel_layout);
  232. if (cached_nb_samples > aresample->max_cached_nb_samples) {
  233. for (i = 0; i < nb_channels; i++) {
  234. aresample->cached_data[i] =
  235. av_realloc(aresample->cached_data[i], cached_nb_samples * sizeof(int16_t));
  236. aresample->resampled_data[i] =
  237. av_realloc(aresample->resampled_data[i],
  238. FFALIGN(sizeof(int16_t) * requested_out_nb_samples, 16));
  239. if (aresample->cached_data[i] == NULL || aresample->resampled_data[i] == NULL)
  240. return;
  241. }
  242. aresample->max_cached_nb_samples = cached_nb_samples;
  243. if (aresample->outsamplesref)
  244. avfilter_unref_buffer(aresample->outsamplesref);
  245. aresample->outsamplesref =
  246. avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, requested_out_nb_samples);
  247. outlink->out_buf = aresample->outsamplesref;
  248. }
  249. avfilter_copy_buffer_ref_props(aresample->outsamplesref, insamplesref);
  250. aresample->outsamplesref->audio->sample_rate = outlink->sample_rate;
  251. aresample->outsamplesref->pts =
  252. av_rescale(outlink->sample_rate, insamplesref->pts, inlink->sample_rate);
  253. /* av_resample() works with planar audio buffers */
  254. if (!inlink->planar && nb_channels > 1) {
  255. int16_t *out[8];
  256. for (i = 0; i < nb_channels; i++)
  257. out[i] = aresample->cached_data[i] + aresample->unconsumed_nb_samples;
  258. deinterleave(out, (int16_t *)insamplesref->data[0],
  259. nb_channels, in_nb_samples);
  260. } else {
  261. for (i = 0; i < nb_channels; i++)
  262. memcpy(aresample->cached_data[i] + aresample->unconsumed_nb_samples,
  263. insamplesref->data[i],
  264. in_nb_samples * sizeof(int16_t));
  265. }
  266. for (i = 0; i < nb_channels; i++) {
  267. int consumed_nb_samples;
  268. const int is_last = i+1 == nb_channels;
  269. aresample->outsamplesref->audio->nb_samples =
  270. av_resample(aresample->resample,
  271. aresample->resampled_data[i], aresample->cached_data[i],
  272. &consumed_nb_samples,
  273. cached_nb_samples,
  274. requested_out_nb_samples, is_last);
  275. /* move unconsumed data back to the beginning of the cache */
  276. aresample->unconsumed_nb_samples = cached_nb_samples - consumed_nb_samples;
  277. memmove(aresample->cached_data[i],
  278. aresample->cached_data[i] + consumed_nb_samples,
  279. aresample->unconsumed_nb_samples * sizeof(int16_t));
  280. }
  281. /* copy resampled data to the output samplesref */
  282. if (!inlink->planar && nb_channels > 1) {
  283. interleave((int16_t *)aresample->outsamplesref->data[0],
  284. aresample->resampled_data,
  285. nb_channels, aresample->outsamplesref->audio->nb_samples);
  286. } else {
  287. for (i = 0; i < nb_channels; i++)
  288. memcpy(aresample->outsamplesref->data[i], aresample->resampled_data[i],
  289. aresample->outsamplesref->audio->nb_samples * sizeof(int16_t));
  290. }
  291. avfilter_filter_samples(outlink, avfilter_ref_buffer(aresample->outsamplesref, ~0));
  292. avfilter_unref_buffer(insamplesref);
  293. }
  294. AVFilter avfilter_af_aresample = {
  295. .name = "aresample",
  296. .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
  297. .init = init,
  298. .uninit = uninit,
  299. .query_formats = query_formats,
  300. .priv_size = sizeof(AResampleContext),
  301. .inputs = (const AVFilterPad[]) {{ .name = "default",
  302. .type = AVMEDIA_TYPE_AUDIO,
  303. .filter_samples = filter_samples,
  304. .min_perms = AV_PERM_READ, },
  305. { .name = NULL}},
  306. .outputs = (const AVFilterPad[]) {{ .name = "default",
  307. .config_props = config_output,
  308. .type = AVMEDIA_TYPE_AUDIO, },
  309. { .name = NULL}},
  310. };