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.

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