af_aconvert.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/channel_layout.h"
  27. #include "libavutil/opt.h"
  28. #include "libswresample/swresample.h"
  29. #include "avfilter.h"
  30. #include "audio.h"
  31. #include "internal.h"
  32. typedef struct {
  33. const AVClass *class;
  34. enum AVSampleFormat out_sample_fmt;
  35. int64_t out_chlayout;
  36. struct SwrContext *swr;
  37. char *format_str;
  38. char *channel_layout_str;
  39. } AConvertContext;
  40. #define OFFSET(x) offsetof(AConvertContext, x)
  41. #define A AV_OPT_FLAG_AUDIO_PARAM
  42. #define F AV_OPT_FLAG_FILTERING_PARAM
  43. static const AVOption aconvert_options[] = {
  44. { "sample_fmt", "", OFFSET(format_str), AV_OPT_TYPE_STRING, .flags = A|F },
  45. { "channel_layout", "", OFFSET(channel_layout_str), AV_OPT_TYPE_STRING, .flags = A|F },
  46. { NULL }
  47. };
  48. AVFILTER_DEFINE_CLASS(aconvert);
  49. static av_cold int init(AVFilterContext *ctx)
  50. {
  51. AConvertContext *aconvert = ctx->priv;
  52. int ret = 0;
  53. av_log(ctx, AV_LOG_WARNING, "This filter is deprecated, use aformat instead\n");
  54. aconvert->out_sample_fmt = AV_SAMPLE_FMT_NONE;
  55. aconvert->out_chlayout = 0;
  56. if (aconvert->format_str && strcmp(aconvert->format_str, "auto") &&
  57. (ret = ff_parse_sample_format(&aconvert->out_sample_fmt, aconvert->format_str, ctx)) < 0)
  58. return ret;
  59. if (aconvert->channel_layout_str && strcmp(aconvert->channel_layout_str, "auto"))
  60. return ff_parse_channel_layout(&aconvert->out_chlayout, NULL, aconvert->channel_layout_str, ctx);
  61. return ret;
  62. }
  63. static av_cold void uninit(AVFilterContext *ctx)
  64. {
  65. AConvertContext *aconvert = ctx->priv;
  66. swr_free(&aconvert->swr);
  67. }
  68. static int query_formats(AVFilterContext *ctx)
  69. {
  70. AVFilterFormats *formats = NULL;
  71. AConvertContext *aconvert = ctx->priv;
  72. AVFilterLink *inlink = ctx->inputs[0];
  73. AVFilterLink *outlink = ctx->outputs[0];
  74. AVFilterChannelLayouts *layouts;
  75. ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
  76. &inlink->out_formats);
  77. if (aconvert->out_sample_fmt != AV_SAMPLE_FMT_NONE) {
  78. formats = NULL;
  79. ff_add_format(&formats, aconvert->out_sample_fmt);
  80. ff_formats_ref(formats, &outlink->in_formats);
  81. } else
  82. ff_formats_ref(ff_all_formats(AVMEDIA_TYPE_AUDIO),
  83. &outlink->in_formats);
  84. ff_channel_layouts_ref(ff_all_channel_layouts(),
  85. &inlink->out_channel_layouts);
  86. if (aconvert->out_chlayout != 0) {
  87. layouts = NULL;
  88. ff_add_channel_layout(&layouts, aconvert->out_chlayout);
  89. ff_channel_layouts_ref(layouts, &outlink->in_channel_layouts);
  90. } else
  91. ff_channel_layouts_ref(ff_all_channel_layouts(),
  92. &outlink->in_channel_layouts);
  93. return 0;
  94. }
  95. static int config_output(AVFilterLink *outlink)
  96. {
  97. int ret;
  98. AVFilterContext *ctx = outlink->src;
  99. AVFilterLink *inlink = ctx->inputs[0];
  100. AConvertContext *aconvert = ctx->priv;
  101. char buf1[64], buf2[64];
  102. /* if not specified in args, use the format and layout of the output */
  103. if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
  104. aconvert->out_sample_fmt = outlink->format;
  105. if (aconvert->out_chlayout == 0)
  106. aconvert->out_chlayout = outlink->channel_layout;
  107. aconvert->swr = swr_alloc_set_opts(aconvert->swr,
  108. aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
  109. inlink->channel_layout, inlink->format, inlink->sample_rate,
  110. 0, ctx);
  111. if (!aconvert->swr)
  112. return AVERROR(ENOMEM);
  113. ret = swr_init(aconvert->swr);
  114. if (ret < 0)
  115. return ret;
  116. av_get_channel_layout_string(buf1, sizeof(buf1),
  117. -1, inlink ->channel_layout);
  118. av_get_channel_layout_string(buf2, sizeof(buf2),
  119. -1, outlink->channel_layout);
  120. av_log(ctx, AV_LOG_VERBOSE,
  121. "fmt:%s cl:%s -> fmt:%s cl:%s\n",
  122. av_get_sample_fmt_name(inlink ->format), buf1,
  123. av_get_sample_fmt_name(outlink->format), buf2);
  124. return 0;
  125. }
  126. static int filter_frame(AVFilterLink *inlink, AVFrame *insamplesref)
  127. {
  128. AConvertContext *aconvert = inlink->dst->priv;
  129. const int n = insamplesref->nb_samples;
  130. AVFilterLink *const outlink = inlink->dst->outputs[0];
  131. AVFrame *outsamplesref = ff_get_audio_buffer(outlink, n);
  132. int ret;
  133. if (!outsamplesref)
  134. return AVERROR(ENOMEM);
  135. swr_convert(aconvert->swr, outsamplesref->extended_data, n,
  136. (void *)insamplesref->extended_data, n);
  137. av_frame_copy_props(outsamplesref, insamplesref);
  138. av_frame_set_channels(outsamplesref, outlink->channels);
  139. outsamplesref->channel_layout = outlink->channel_layout;
  140. ret = ff_filter_frame(outlink, outsamplesref);
  141. av_frame_free(&insamplesref);
  142. return ret;
  143. }
  144. static const AVFilterPad aconvert_inputs[] = {
  145. {
  146. .name = "default",
  147. .type = AVMEDIA_TYPE_AUDIO,
  148. .filter_frame = filter_frame,
  149. },
  150. { NULL }
  151. };
  152. static const AVFilterPad aconvert_outputs[] = {
  153. {
  154. .name = "default",
  155. .type = AVMEDIA_TYPE_AUDIO,
  156. .config_props = config_output,
  157. },
  158. { NULL }
  159. };
  160. AVFilter ff_af_aconvert = {
  161. .name = "aconvert",
  162. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout."),
  163. .priv_size = sizeof(AConvertContext),
  164. .priv_class = &aconvert_class,
  165. .init = init,
  166. .uninit = uninit,
  167. .query_formats = query_formats,
  168. .inputs = aconvert_inputs,
  169. .outputs = aconvert_outputs,
  170. };