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.

127 lines
4.4KB

  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 "libswresample/swresample.h"
  26. #include "avfilter.h"
  27. #include "internal.h"
  28. typedef struct {
  29. int out_rate;
  30. double ratio;
  31. struct SwrContext *swr;
  32. } AResampleContext;
  33. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  34. {
  35. AResampleContext *aresample = ctx->priv;
  36. int ret;
  37. if (args) {
  38. if ((ret = ff_parse_sample_rate(&aresample->out_rate, args, ctx)) < 0)
  39. return ret;
  40. } else {
  41. aresample->out_rate = -1;
  42. }
  43. return 0;
  44. }
  45. static av_cold void uninit(AVFilterContext *ctx)
  46. {
  47. AResampleContext *aresample = ctx->priv;
  48. swr_free(&aresample->swr);
  49. }
  50. static int config_output(AVFilterLink *outlink)
  51. {
  52. int ret;
  53. AVFilterContext *ctx = outlink->src;
  54. AVFilterLink *inlink = ctx->inputs[0];
  55. AResampleContext *aresample = ctx->priv;
  56. if (aresample->out_rate == -1)
  57. aresample->out_rate = outlink->sample_rate;
  58. else
  59. outlink->sample_rate = aresample->out_rate;
  60. outlink->time_base = (AVRational) {1, aresample->out_rate};
  61. //TODO: make the resampling parameters (filter size, phrase shift, linear, cutoff) configurable
  62. aresample->swr = swr_alloc_set_opts(aresample->swr,
  63. inlink->channel_layout, inlink->format, aresample->out_rate,
  64. inlink->channel_layout, inlink->format, inlink->sample_rate,
  65. 0, ctx);
  66. if (!aresample->swr)
  67. return AVERROR(ENOMEM);
  68. ret = swr_init(aresample->swr);
  69. if (ret < 0)
  70. return ret;
  71. aresample->ratio = (double)outlink->sample_rate / inlink->sample_rate;
  72. av_log(ctx, AV_LOG_INFO, "r:%"PRId64"Hz -> r:%"PRId64"Hz\n",
  73. inlink->sample_rate, outlink->sample_rate);
  74. return 0;
  75. }
  76. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  77. {
  78. AResampleContext *aresample = inlink->dst->priv;
  79. const int n_in = insamplesref->audio->nb_samples;
  80. int n_out = n_in * aresample->ratio;
  81. AVFilterLink *const outlink = inlink->dst->outputs[0];
  82. AVFilterBufferRef *outsamplesref = avfilter_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
  83. n_out = swr_convert(aresample->swr, outsamplesref->data, n_out,
  84. (void *)insamplesref->data, n_in);
  85. avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
  86. outsamplesref->audio->sample_rate = outlink->sample_rate;
  87. outsamplesref->audio->nb_samples = n_out;
  88. outsamplesref->pts = insamplesref->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
  89. av_rescale(outlink->sample_rate, insamplesref->pts, inlink ->sample_rate);
  90. avfilter_filter_samples(outlink, outsamplesref);
  91. avfilter_unref_buffer(insamplesref);
  92. }
  93. AVFilter avfilter_af_aresample = {
  94. .name = "aresample",
  95. .description = NULL_IF_CONFIG_SMALL("Resample audio data."),
  96. .init = init,
  97. .uninit = uninit,
  98. .priv_size = sizeof(AResampleContext),
  99. .inputs = (const AVFilterPad[]) {{ .name = "default",
  100. .type = AVMEDIA_TYPE_AUDIO,
  101. .filter_samples = filter_samples,
  102. .min_perms = AV_PERM_READ, },
  103. { .name = NULL}},
  104. .outputs = (const AVFilterPad[]) {{ .name = "default",
  105. .config_props = config_output,
  106. .type = AVMEDIA_TYPE_AUDIO, },
  107. { .name = NULL}},
  108. };