af_aformat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "avfilter.h"
  27. #include "internal.h"
  28. typedef struct {
  29. AVFilterFormats *formats, *chlayouts, *packing;
  30. } AFormatContext;
  31. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  32. {
  33. AFormatContext * const aformat = ctx->priv;
  34. char *fmts_str = NULL, *fmt_str, *ptr = NULL;
  35. int64_t fmt;
  36. int ret;
  37. if (!args)
  38. goto arg_fail;
  39. #define ADD_FORMATS(all_formats, fmt_name, fmt_type, fmts_list) \
  40. fmts_str = av_get_token(&args, ":"); \
  41. if (!fmts_str || !*fmts_str) \
  42. goto arg_fail; \
  43. if (!strcmp(fmts_str, "all")) { \
  44. aformat->fmts_list = all_formats; \
  45. } else { \
  46. for (fmt_str = fmts_str; \
  47. fmt_str = av_strtok(fmt_str, ",", &ptr); fmt_str = NULL) { \
  48. if ((ret = ff_parse_##fmt_name((fmt_type *)&fmt, \
  49. fmt_str, ctx)) < 0) { \
  50. av_freep(&fmts_str); \
  51. return ret; \
  52. } \
  53. avfilter_add_format(&aformat->fmts_list, fmt); \
  54. } \
  55. } \
  56. av_freep(&fmts_str); \
  57. if (*args) \
  58. args++;
  59. ADD_FORMATS(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO), sample_format, int, formats);
  60. ADD_FORMATS(avfilter_make_all_channel_layouts(), channel_layout, int64_t, chlayouts);
  61. ADD_FORMATS(avfilter_make_all_packing_formats(), packing_format, int, packing);
  62. return 0;
  63. arg_fail:
  64. av_log(ctx, AV_LOG_ERROR, "Invalid arguments, they must be of the form "
  65. "sample_fmts:channel_layouts:packing_fmts\n");
  66. av_freep(&fmts_str);
  67. return AVERROR(EINVAL);
  68. }
  69. static int query_formats(AVFilterContext *ctx)
  70. {
  71. AFormatContext * const aformat = ctx->priv;
  72. avfilter_set_common_sample_formats (ctx, aformat->formats);
  73. avfilter_set_common_channel_layouts(ctx, aformat->chlayouts);
  74. avfilter_set_common_packing_formats(ctx, aformat->packing);
  75. return 0;
  76. }
  77. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  78. {
  79. avfilter_filter_samples(inlink->dst->outputs[0], insamplesref);
  80. }
  81. AVFilter avfilter_af_aformat = {
  82. .name = "aformat",
  83. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."),
  84. .init = init,
  85. .query_formats = query_formats,
  86. .priv_size = sizeof(AFormatContext),
  87. .inputs = (const AVFilterPad[]) {{ .name = "default",
  88. .type = AVMEDIA_TYPE_AUDIO,
  89. .filter_samples = filter_samples},
  90. { .name = NULL}},
  91. .outputs = (const AVFilterPad[]) {{ .name = "default",
  92. .type = AVMEDIA_TYPE_AUDIO},
  93. { .name = NULL}},
  94. };