af_aformat.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2011 Mina Nagy Zaki
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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. static const AVOption options[] = {
  44. { "sample_fmts", "A comma-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A },
  45. { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A },
  46. { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A },
  47. { NULL },
  48. };
  49. static const AVClass aformat_class = {
  50. .class_name = "aformat filter",
  51. .item_name = av_default_item_name,
  52. .option = options,
  53. .version = LIBAVUTIL_VERSION_INT,
  54. };
  55. #define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \
  56. do { \
  57. char *next, *cur = str, sep; \
  58. \
  59. if (str && strchr(str, ',')) { \
  60. av_log(ctx, AV_LOG_WARNING, "This syntax is deprecated, use '|' to "\
  61. "separate %s.\n", desc); \
  62. sep = ','; \
  63. } else \
  64. sep = '|'; \
  65. \
  66. while (cur) { \
  67. type fmt; \
  68. next = strchr(cur, sep); \
  69. if (next) \
  70. *next++ = 0; \
  71. \
  72. if ((fmt = get_fmt(cur)) == none) { \
  73. av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\
  74. return AVERROR(EINVAL); \
  75. } \
  76. add_to_list(&list, fmt); \
  77. \
  78. cur = next; \
  79. } \
  80. } while (0)
  81. static int get_sample_rate(const char *samplerate)
  82. {
  83. int ret = strtol(samplerate, NULL, 0);
  84. return FFMAX(ret, 0);
  85. }
  86. static av_cold int init(AVFilterContext *ctx)
  87. {
  88. AFormatContext *s = ctx->priv;
  89. PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats,
  90. ff_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format");
  91. PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, ff_add_format,
  92. get_sample_rate, 0, "sample rate");
  93. PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts,
  94. ff_add_channel_layout, av_get_channel_layout, 0,
  95. "channel layout");
  96. return 0;
  97. }
  98. static int query_formats(AVFilterContext *ctx)
  99. {
  100. AFormatContext *s = ctx->priv;
  101. ff_set_common_formats(ctx, s->formats ? s->formats :
  102. ff_all_formats(AVMEDIA_TYPE_AUDIO));
  103. ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates :
  104. ff_all_samplerates());
  105. ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts :
  106. ff_all_channel_layouts());
  107. return 0;
  108. }
  109. static const AVFilterPad avfilter_af_aformat_inputs[] = {
  110. {
  111. .name = "default",
  112. .type = AVMEDIA_TYPE_AUDIO,
  113. },
  114. { NULL }
  115. };
  116. static const AVFilterPad avfilter_af_aformat_outputs[] = {
  117. {
  118. .name = "default",
  119. .type = AVMEDIA_TYPE_AUDIO
  120. },
  121. { NULL }
  122. };
  123. AVFilter ff_af_aformat = {
  124. .name = "aformat",
  125. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
  126. .init = init,
  127. .query_formats = query_formats,
  128. .priv_size = sizeof(AFormatContext),
  129. .priv_class = &aformat_class,
  130. .inputs = avfilter_af_aformat_inputs,
  131. .outputs = avfilter_af_aformat_outputs,
  132. };