af_asetrate.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 {
  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. ff_formats_ref(ff_make_format_list(sample_rates),
  46. &ctx->outputs[0]->in_samplerates);
  47. return 0;
  48. }
  49. static av_cold int config_props(AVFilterLink *outlink)
  50. {
  51. AVFilterContext *ctx = outlink->src;
  52. ASetRateContext *sr = ctx->priv;
  53. AVFilterLink *inlink = ctx->inputs[0];
  54. AVRational intb = ctx->inputs[0]->time_base;
  55. int inrate = inlink->sample_rate;
  56. if (intb.num == 1 && intb.den == inrate) {
  57. outlink->time_base.num = 1;
  58. outlink->time_base.den = outlink->sample_rate;
  59. } else {
  60. outlink->time_base = intb;
  61. sr->rescale_pts = 1;
  62. if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate))
  63. av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n");
  64. }
  65. return 0;
  66. }
  67. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  68. {
  69. AVFilterContext *ctx = inlink->dst;
  70. ASetRateContext *sr = ctx->priv;
  71. AVFilterLink *outlink = ctx->outputs[0];
  72. frame->sample_rate = outlink->sample_rate;
  73. if (sr->rescale_pts)
  74. frame->pts = av_rescale(frame->pts, inlink->sample_rate,
  75. outlink->sample_rate);
  76. return ff_filter_frame(outlink, frame);
  77. }
  78. static const AVFilterPad asetrate_inputs[] = {
  79. {
  80. .name = "default",
  81. .type = AVMEDIA_TYPE_AUDIO,
  82. .filter_frame = filter_frame,
  83. },
  84. { NULL }
  85. };
  86. static const AVFilterPad asetrate_outputs[] = {
  87. {
  88. .name = "default",
  89. .type = AVMEDIA_TYPE_AUDIO,
  90. .config_props = config_props,
  91. },
  92. { NULL }
  93. };
  94. AVFilter ff_af_asetrate = {
  95. .name = "asetrate",
  96. .description = NULL_IF_CONFIG_SMALL("Change the sample rate without "
  97. "altering the data."),
  98. .query_formats = query_formats,
  99. .priv_size = sizeof(ASetRateContext),
  100. .inputs = asetrate_inputs,
  101. .outputs = asetrate_outputs,
  102. .priv_class = &asetrate_class,
  103. };