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.

179 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 "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. int out_packing = av_sample_fmt_is_planar(aconvert->out_sample_fmt);
  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. avfilter_formats_ref(avfilter_make_all_channel_layouts(),
  78. &inlink->out_chlayouts);
  79. if (aconvert->out_chlayout != 0) {
  80. formats = NULL;
  81. avfilter_add_format(&formats, aconvert->out_chlayout);
  82. avfilter_formats_ref(formats, &outlink->in_chlayouts);
  83. } else
  84. avfilter_formats_ref(avfilter_make_all_channel_layouts(),
  85. &outlink->in_chlayouts);
  86. avfilter_formats_ref(avfilter_make_all_packing_formats(),
  87. &inlink->out_packing);
  88. formats = NULL;
  89. avfilter_add_format(&formats, out_packing);
  90. avfilter_formats_ref(formats, &outlink->in_packing);
  91. return 0;
  92. }
  93. static int config_output(AVFilterLink *outlink)
  94. {
  95. int ret;
  96. AVFilterContext *ctx = outlink->src;
  97. AVFilterLink *inlink = ctx->inputs[0];
  98. AConvertContext *aconvert = ctx->priv;
  99. char buf1[64], buf2[64];
  100. /* if not specified in args, use the format and layout of the output */
  101. if (aconvert->out_sample_fmt == AV_SAMPLE_FMT_NONE)
  102. aconvert->out_sample_fmt = outlink->format;
  103. if (aconvert->out_chlayout == 0)
  104. aconvert->out_chlayout = outlink->channel_layout;
  105. aconvert->swr = swr_alloc_set_opts(aconvert->swr,
  106. aconvert->out_chlayout, aconvert->out_sample_fmt, inlink->sample_rate,
  107. inlink->channel_layout, inlink->format, inlink->sample_rate,
  108. 0, ctx);
  109. if (!aconvert->swr)
  110. return AVERROR(ENOMEM);
  111. ret = swr_init(aconvert->swr);
  112. if (ret < 0)
  113. return ret;
  114. av_get_channel_layout_string(buf1, sizeof(buf1),
  115. -1, inlink ->channel_layout);
  116. av_get_channel_layout_string(buf2, sizeof(buf2),
  117. -1, outlink->channel_layout);
  118. av_log(ctx, AV_LOG_INFO,
  119. "fmt:%s cl:%s planar:%i -> fmt:%s cl:%s planar:%i\n",
  120. av_get_sample_fmt_name(inlink ->format), buf1, inlink ->planar,
  121. av_get_sample_fmt_name(outlink->format), buf2, outlink->planar);
  122. return 0;
  123. }
  124. static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref)
  125. {
  126. AConvertContext *aconvert = inlink->dst->priv;
  127. const int n = insamplesref->audio->nb_samples;
  128. AVFilterLink *const outlink = inlink->dst->outputs[0];
  129. AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n);
  130. swr_convert(aconvert->swr, outsamplesref->data, n,
  131. (void *)insamplesref->data, n);
  132. avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
  133. outsamplesref->audio->channel_layout = outlink->channel_layout;
  134. outsamplesref->audio->planar = outlink->planar;
  135. ff_filter_samples(outlink, outsamplesref);
  136. avfilter_unref_buffer(insamplesref);
  137. }
  138. AVFilter avfilter_af_aconvert = {
  139. .name = "aconvert",
  140. .description = NULL_IF_CONFIG_SMALL("Convert the input audio to sample_fmt:channel_layout:packed_fmt."),
  141. .priv_size = sizeof(AConvertContext),
  142. .init = init,
  143. .uninit = uninit,
  144. .query_formats = query_formats,
  145. .inputs = (const AVFilterPad[]) {{ .name = "default",
  146. .type = AVMEDIA_TYPE_AUDIO,
  147. .filter_samples = filter_samples,
  148. .min_perms = AV_PERM_READ, },
  149. { .name = NULL}},
  150. .outputs = (const AVFilterPad[]) {{ .name = "default",
  151. .type = AVMEDIA_TYPE_AUDIO,
  152. .config_props = config_output, },
  153. { .name = NULL}},
  154. };