af_aconvert.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram <smeenaks@ucsd.edu>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2011 Mina Nagy Zaki
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * sample format and channel layout conversion audio filter
  25. */
  26. #include "libavutil/avstring.h"
  27. #include "libswresample/swresample.h"
  28. #include "avfilter.h"
  29. #include "audio.h"
  30. #include "internal.h"
  31. typedef struct {
  32. enum AVSampleFormat out_sample_fmt;
  33. int64_t out_chlayout;
  34. struct SwrContext *swr;
  35. } AConvertContext;
  36. static av_cold int init(AVFilterContext *ctx, const char *args0, void *opaque)
  37. {
  38. AConvertContext *aconvert = ctx->priv;
  39. char *arg, *ptr = NULL;
  40. int ret = 0;
  41. char *args = av_strdup(args0);
  42. aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
  43. aconvert->out_chlayout = 0;
  44. if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
  45. if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
  46. goto end;
  47. }
  48. if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
  49. if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
  50. goto end;
  51. }
  52. end:
  53. av_freep(&args);
  54. return ret;
  55. }
  56. static av_cold void uninit(AVFilterContext *ctx)
  57. {
  58. AConvertContext *aconvert = ctx->priv;
  59. swr_free(&aconvert->swr);
  60. }
  61. static int query_formats(AVFilterContext *ctx)
  62. {
  63. AVFilterFormats *formats = NULL;
  64. AConvertContext *aconvert = ctx->priv;
  65. AVFilterLink *inlink = ctx->inputs[0];
  66. AVFilterLink *outlink = ctx->outputs[0];
  67. AVFilterChannelLayouts *layouts;
  68. avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
  69. &inlink->out_formats);
  70. if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
  71. formats = NULL;
  72. avfilter_add_format(&formats, aconvert->out_sample_fmt);
  73. avfilter_formats_ref(formats, &outlink->in_formats);
  74. } else
  75. avfilter_formats_ref(avfilter_make_all_formats(AVMEDIA_TYPE_AUDIO),
  76. &outlink->in_formats);
  77. ff_channel_layouts_ref(ff_all_channel_layouts(),
  78. &inlink->out_channel_layouts);
  79. if (aconvert->out_chlayout != 0) {
  80. layouts = NULL;
  81. ff_add_channel_layout(&layouts, aconvert->out_chlayout);
  82. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  83. } else
  84. ff_channel_layouts_ref(ff_all_channel_layouts(),
  85. &outlink->in_channel_layouts);
  86. return 0;
  87. }
  88. static int config_output(AVFilterLink *outlink)
  89. {
  90. int ret;
  91. AVFilterContext *ctx = outlink->src;
  92. AVFilterLink *inlink = ctx->inputs[0];
  93. AConvertContext *aconvert = ctx->priv;
  94. char buf1[64], buf2[64];
  95. /* if not specified in args, use the format and layout of the output */
  96. if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
  97. aconvert->out_sample_fmt = outlink->format;
  98. if (aconvert->out_chlayout == 0)
  99. aconvert->out_chlayout = outlink->channel_layout;
  100. aconvert->swr = swr_alloc_set_opts(aconvert->swr,
  101. aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
  102. inlink->channel_layout, inlink->format, inlink->sample_rate,
  103. 0, ctx);
  104. if (!aconvert->swr)
  105. return AVERROR(ENOMEM);
  106. ret = swr_init(aconvert->swr);
  107. if (ret < 0)
  108. return ret;
  109. av_get_channel_layout_string(buf1, sizeof(buf1),
  110. -1, inlink ->channel_layout);
  111. av_get_channel_layout_string(buf2, sizeof(buf2),
  112. -1, outlink->channel_layout);
  113. av_log(ctx, AV_LOG_INFO,
  114. "fmt:%s cl:%s -> fmt:%s cl:%s\n",
  115. av_get_sample_fmt_name(inlink ->format), buf1,
  116. av_get_sample_fmt_name(outlink->format), buf2);
  117. return 0;
  118. }
  119. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  120. {
  121. AConvertContext *aconvert = inlink->dst->priv;
  122. const int n = insamplesref->audio->nb_samples;
  123. AVFilterLink *const outlink = inlink->dst->outputs[0];
  124. AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  125. swr_convert(aconvert->swr, outsamplesref->data, n,
  126. (void *)insamplesref->data, n);
  127. avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
  128. outsamplesref->audio->channel_layout = outlink->channel_layout;
  129. ff_filter_samples(outlink, outsamplesref);
  130. avfilter_unref_buffer(insamplesref);
  131. }
  132. AVFilter avfilter_af_aconvert = {
  133. .name = "aconvert",
  134. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
  135. .priv_size = sizeof(AConvertContext),
  136. .init = init,
  137. .uninit = uninit,
  138. .query_formats = query_formats,
  139. .inputs = (const AVFilterPad[]) {{ .name = "default",
  140. .type = AVMEDIA_TYPE_AUDIO,
  141. .filter_samples = filter_samples,
  142. .min_perms = AV_PERM_READ, },
  143. { .name = NULL}},
  144. .outputs = (const AVFilterPad[]) {{ .name = "default",
  145. .type = AVMEDIA_TYPE_AUDIO,
  146. .config_props = config_output, },
  147. { .name = NULL}},
  148. };