af_aformat.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/audioconvert.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/opt.h"
  27. #include "audio.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. typedef struct AFormatContext {
  32. const AVClass *class;
  33. AVFilterFormats *formats;
  34. AVFilterFormats *sample_rates;
  35. AVFilterChannelLayouts *channel_layouts;
  36. char *formats_str;
  37. char *sample_rates_str;
  38. char *channel_layouts_str;
  39. } AFormatContext;
  40. #define OFFSET(x) offsetof(AFormatContext, x)
  41. #define A AV_OPT_FLAG_AUDIO_PARAM
  42. static const AVOption options[] = {
  43. { "sample_fmts", "A comma-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A },
  44. { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A },
  45. { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A },
  46. { NULL },
  47. };
  48. static const AVClass aformat_class = {
  49. .class_name = "aformat filter",
  50. .item_name = av_default_item_name,
  51. .option = options,
  52. .version = LIBAVUTIL_VERSION_INT,
  53. };
  54. #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
  55. do { \
  56. char *next, *cur = str; \
  57. while (cur) { \
  58. type fmt; \
  59. next = strchr(cur, ','); \
  60. if (next) \
  61. *next++ = 0; \
  62. \
  63. if ((fmt = get_fmt(cur)) == none) { \
  64. av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
  65. ret = AVERROR(EINVAL); \
  66. goto fail; \
  67. } \
  68. add_to_list(&list, fmt); \
  69. \
  70. cur = next; \
  71. } \
  72. } while (0)
  73. static int get_sample_rate(const char *samplerate)
  74. {
  75. int ret = strtol(samplerate, NULL, 0);
  76. return FFMAX(ret, 0);
  77. }
  78. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  79. {
  80. AFormatContext *s = ctx->priv;
  81. int ret;
  82. if (!args) {
  83. av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n");
  84. return AVERROR(EINVAL);
  85. }
  86. s->class = &aformat_class;
  87. av_opt_set_defaults(s);
  88. if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
  89. av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
  90. return ret;
  91. }
  92. PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
  93. avfilter_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
  94. PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, avfilter_add_format,
  95. get_sample_rate, 0, "sample rate");
  96. PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
  97. ff_add_channel_layout, av_get_channel_layout, 0,
  98. "channel layout");
  99. fail:
  100. av_opt_free(s);
  101. return ret;
  102. }
  103. static int query_formats(AVFilterContext *ctx)
  104. {
  105. AFormatContext *s = ctx->priv;
  106. avfilter_set_common_formats(ctx, s->formats ? s->formats :
  107. avfilter_all_formats(AVMEDIA_TYPE_AUDIO));
  108. ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
  109. ff_all_samplerates());
  110. ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
  111. ff_all_channel_layouts());
  112. return 0;
  113. }
  114. AVFilter avfilter_af_aformat = {
  115. .name = "aformat",
  116. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
  117. .init = init,
  118. .query_formats = query_formats,
  119. .priv_size = sizeof(AFormatContext),
  120. .inputs = (AVFilterPad[]) {{ .name = "default",
  121. .type = AVMEDIA_TYPE_AUDIO,
  122. .filter_samples = ff_null_filter_samples },
  123. { .name = NULL}},
  124. .outputs = (AVFilterPad[]) {{ .name = "default",
  125. .type = AVMEDIA_TYPE_AUDIO},
  126. { .name = NULL}},
  127. };