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.

178 lines
6.5KB

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