af_asetrate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2013 Nicolas George
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "internal.h"
  23. typedef struct ASetRateContext {
  24. const AVClass *class;
  25. int sample_rate;
  26. int rescale_pts;
  27. } ASetRateContext;
  28. #define CONTEXT ASetRateContext
  29. #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  30. #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
  31. { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
  32. { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
  33. #define OPT_INT(name, field, def, min, max, descr, ...) \
  34. OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
  35. static const AVOption asetrate_options[] = {
  36. OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
  37. OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
  38. {NULL},
  39. };
  40. AVFILTER_DEFINE_CLASS(asetrate);
  41. static av_cold int query_formats(AVFilterContext *ctx)
  42. {
  43. ASetRateContext *sr = ctx->priv;
  44. int sample_rates[] = { sr->sample_rate, -1 };
  45. return ff_formats_ref(ff_make_format_list(sample_rates),
  46. &ctx->outputs[0]->in_samplerates);
  47. }
  48. static av_cold int config_props(AVFilterLink *outlink)
  49. {
  50. AVFilterContext *ctx = outlink->src;
  51. ASetRateContext *sr = ctx->priv;
  52. AVFilterLink *inlink = ctx->inputs[0];
  53. AVRational intb = ctx->inputs[0]->time_base;
  54. int inrate = inlink->sample_rate;
  55. if (intb.num == 1 && intb.den == inrate) {
  56. outlink->time_base.num = 1;
  57. outlink->time_base.den = outlink->sample_rate;
  58. } else {
  59. outlink->time_base = intb;
  60. sr->rescale_pts = 1;
  61. if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate))
  62. av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n");
  63. }
  64. return 0;
  65. }
  66. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  67. {
  68. AVFilterContext *ctx = inlink->dst;
  69. ASetRateContext *sr = ctx->priv;
  70. AVFilterLink *outlink = ctx->outputs[0];
  71. frame->sample_rate = outlink->sample_rate;
  72. if (sr->rescale_pts)
  73. frame->pts = av_rescale(frame->pts, inlink->sample_rate,
  74. outlink->sample_rate);
  75. return ff_filter_frame(outlink, frame);
  76. }
  77. static const AVFilterPad asetrate_inputs[] = {
  78. {
  79. .name = "default",
  80. .type = AVMEDIA_TYPE_AUDIO,
  81. .filter_frame = filter_frame,
  82. },
  83. { NULL }
  84. };
  85. static const AVFilterPad asetrate_outputs[] = {
  86. {
  87. .name = "default",
  88. .type = AVMEDIA_TYPE_AUDIO,
  89. .config_props = config_props,
  90. },
  91. { NULL }
  92. };
  93. AVFilter ff_af_asetrate = {
  94. .name = "asetrate",
  95. .description = NULL_IF_CONFIG_SMALL("Change the sample rate without "
  96. "altering the data."),
  97. .query_formats = query_formats,
  98. .priv_size = sizeof(ASetRateContext),
  99. .inputs = asetrate_inputs,
  100. .outputs = asetrate_outputs,
  101. .priv_class = &asetrate_class,
  102. };