You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
6.1KB

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