af_aconvert.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/audioconvert.h"
  27. #include "libavutil/avstring.h"
  28. #include "libswresample/swresample.h"
  29. #include "avfilter.h"
  30. #include "audio.h"
  31. #include "internal.h"
  32. typedef struct {
  33. enum AVSampleFormat out_sample_fmt;
  34. int64_t out_chlayout;
  35. struct SwrContext *swr;
  36. } AConvertContext;
  37. static av_cold int init(AVFilterContext *ctx, const char *args0)
  38. {
  39. AConvertContext *aconvert = ctx->priv;
  40. char *arg, *ptr = NULL;
  41. int ret = 0;
  42. char *args = av_strdup(args0);
  43. aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
  44. aconvert->out_chlayout = 0;
  45. if ((arg = av_strtok(args, ":", &ptr)) && strcmp(arg, "auto")) {
  46. if ((ret = ff_parse_sample_format(&aconvert->out_sample_fmt, arg, ctx)) < 0)
  47. goto end;
  48. }
  49. if ((arg = av_strtok(NULL, ":", &ptr)) && strcmp(arg, "auto")) {
  50. if ((ret = ff_parse_channel_layout(&aconvert->out_chlayout, arg, ctx)) < 0)
  51. goto end;
  52. }
  53. end:
  54. av_freep(&args);
  55. return ret;
  56. }
  57. static av_cold void uninit(AVFilterContext *ctx)
  58. {
  59. AConvertContext *aconvert = ctx->priv;
  60. swr_free(&aconvert->swr);
  61. }
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. AVFilterFormats *formats = NULL;
  65. AConvertContext *aconvert = ctx->priv;
  66. AVFilterLink *inlink = ctx->inputs[0];
  67. AVFilterLink *outlink = ctx->outputs[0];
  68. AVFilterChannelLayouts *layouts;
  69. ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
  70. &inlink->out_formats);
  71. if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
  72. formats = NULL;
  73. ff_add_format(&formats, aconvert->out_sample_fmt);
  74. ff_formats_ref(formats, &outlink->in_formats);
  75. } else
  76. ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
  77. &outlink->in_formats);
  78. ff_channel_layouts_ref(ff_all_channel_layouts(),
  79. &inlink->out_channel_layouts);
  80. if (aconvert->out_chlayout != 0) {
  81. layouts = NULL;
  82. ff_add_channel_layout(&layouts, aconvert->out_chlayout);
  83. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  84. } else
  85. ff_channel_layouts_ref(ff_all_channel_layouts(),
  86. &outlink->in_channel_layouts);
  87. return 0;
  88. }
  89. static int config_output(AVFilterLink *outlink)
  90. {
  91. int ret;
  92. AVFilterContext *ctx = outlink->src;
  93. AVFilterLink *inlink = ctx->inputs[0];
  94. AConvertContext *aconvert = ctx->priv;
  95. char buf1[64], buf2[64];
  96. /* if not specified in args, use the format and layout of the output */
  97. if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
  98. aconvert->out_sample_fmt = outlink->format;
  99. if (aconvert->out_chlayout == 0)
  100. aconvert->out_chlayout = outlink->channel_layout;
  101. aconvert->swr = swr_alloc_set_opts(aconvert->swr,
  102. aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
  103. inlink->channel_layout, inlink->format, inlink->sample_rate,
  104. 0, ctx);
  105. if (!aconvert->swr)
  106. return AVERROR(ENOMEM);
  107. ret = swr_init(aconvert->swr);
  108. if (ret < 0)
  109. return ret;
  110. av_get_channel_layout_string(buf1, sizeof(buf1),
  111. -1, inlink ->channel_layout);
  112. av_get_channel_layout_string(buf2, sizeof(buf2),
  113. -1, outlink->channel_layout);
  114. av_log(ctx, AV_LOG_VERBOSE,
  115. "fmt:%s cl:%s -> fmt:%s cl:%s\n",
  116. av_get_sample_fmt_name(inlink ->format), buf1,
  117. av_get_sample_fmt_name(outlink->format), buf2);
  118. return 0;
  119. }
  120. static int filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  121. {
  122. AConvertContext *aconvert = inlink->dst->priv;
  123. const int n = insamplesref->audio->nb_samples;
  124. AVFilterLink *const outlink = inlink->dst->outputs[0];
  125. AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  126. int ret;
  127. swr_convert(aconvert->swr, outsamplesref->data, n,
  128. (void *)insamplesref->data, n);
  129. avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
  130. outsamplesref->audio->channel_layout = outlink->channel_layout;
  131. ret = ff_filter_samples(outlink, outsamplesref);
  132. avfilter_unref_buffer(insamplesref);
  133. return ret;
  134. }
  135. AVFilter avfilter_af_aconvert = {
  136. .name = "aconvert",
  137. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
  138. .priv_size = sizeof(AConvertContext),
  139. .init = init,
  140. .uninit = uninit,
  141. .query_formats = query_formats,
  142. .inputs = (const AVFilterPad[]) {{ .name = "default",
  143. .type = AVMEDIA_TYPE_AUDIO,
  144. .filter_samples = filter_samples,
  145. .min_perms = AV_PERM_READ, },
  146. { .name = NULL}},
  147. .outputs = (const AVFilterPad[]) {{ .name = "default",
  148. .type = AVMEDIA_TYPE_AUDIO,
  149. .config_props = config_output, },
  150. { .name = NULL}},
  151. };