af_aformat.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2011 Mina Nagy Zaki
  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
  8. * License 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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * format audio filter
  23. */
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/channel_layout.h"
  26. #include "libavutil/common.h"
  27. #include "libavutil/opt.h"
  28. #include "audio.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. typedef struct AFormatContext {
  33. const AVClass *class;
  34. AVFilterFormats *formats;
  35. AVFilterFormats *sample_rates;
  36. AVFilterChannelLayouts *channel_layouts;
  37. char *formats_str;
  38. char *sample_rates_str;
  39. char *channel_layouts_str;
  40. } AFormatContext;
  41. #define OFFSET(x) offsetof(AFormatContext, x)
  42. #define A AV_OPT_FLAG_AUDIO_PARAM
  43. #define F AV_OPT_FLAG_FILTERING_PARAM
  44. static const AVOption aformat_options[] = {
  45. { "sample_fmts", "A comma-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A|F },
  46. { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A|F },
  47. { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A|F },
  48. { NULL }
  49. };
  50. AVFILTER_DEFINE_CLASS(aformat);
  51. #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
  52. do { \
  53. char *next, *cur = str, sep; \
  54. \
  55. if (str && strchr(str, ',')) { \
  56. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
  57. "separate %s.\n", desc); \
  58. sep = ','; \
  59. } else \
  60. sep = '|'; \
  61. \
  62. while (cur) { \
  63. type fmt; \
  64. next = strchr(cur, sep); \
  65. if (next) \
  66. *next++ = 0; \
  67. \
  68. if ((fmt = get_fmt(cur)) == none) { \
  69. av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
  70. return AVERROR(EINVAL); \
  71. } \
  72. add_to_list(&list, fmt); \
  73. \
  74. cur = next; \
  75. } \
  76. } while (0)
  77. static int get_sample_rate(const char *samplerate)
  78. {
  79. int ret = strtol(samplerate, NULL, 0);
  80. return FFMAX(ret, 0);
  81. }
  82. static av_cold int init(AVFilterContext *ctx)
  83. {
  84. AFormatContext *s = ctx->priv;
  85. PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
  86. ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
  87. PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
  88. get_sample_rate, 0, "sample rate");
  89. PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
  90. ff_add_channel_layout, av_get_channel_layout, 0,
  91. "channel layout");
  92. return 0;
  93. }
  94. static int query_formats(AVFilterContext *ctx)
  95. {
  96. AFormatContext *s = ctx->priv;
  97. ff_set_common_formats(ctx, s->formats ? s->formats :
  98. ff_all_formats(AVMEDIA_TYPE_AUDIO));
  99. ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
  100. ff_all_samplerates());
  101. ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
  102. ff_all_channel_counts());
  103. return 0;
  104. }
  105. static const AVFilterPad avfilter_af_aformat_inputs[] = {
  106. {
  107. .name = "default",
  108. .type = AVMEDIA_TYPE_AUDIO,
  109. },
  110. { NULL }
  111. };
  112. static const AVFilterPad avfilter_af_aformat_outputs[] = {
  113. {
  114. .name = "default",
  115. .type = AVMEDIA_TYPE_AUDIO
  116. },
  117. { NULL }
  118. };
  119. AVFilter ff_af_aformat = {
  120. .name = "aformat",
  121. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
  122. .init = init,
  123. .query_formats = query_formats,
  124. .priv_size = sizeof(AFormatContext),
  125. .priv_class = &aformat_class,
  126. .inputs = avfilter_af_aformat_inputs,
  127. .outputs = avfilter_af_aformat_outputs,
  128. };