af_amultiply.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2018 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/avassert.h"
  21. #include "libavutil/channel_layout.h"
  22. #include "libavutil/common.h"
  23. #include "libavutil/float_dsp.h"
  24. #include "libavutil/opt.h"
  25. #include "audio.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "filters.h"
  29. #include "internal.h"
  30. typedef struct AudioMultiplyContext {
  31. const AVClass *class;
  32. AVFrame *frames[2];
  33. int planes;
  34. int channels;
  35. int samples_align;
  36. AVFloatDSPContext *fdsp;
  37. } AudioMultiplyContext;
  38. static int query_formats(AVFilterContext *ctx)
  39. {
  40. AVFilterFormats *formats;
  41. AVFilterChannelLayouts *layouts;
  42. static const enum AVSampleFormat sample_fmts[] = {
  43. AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
  44. AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
  45. AV_SAMPLE_FMT_NONE
  46. };
  47. int ret;
  48. layouts = ff_all_channel_counts();
  49. if (!layouts)
  50. return AVERROR(ENOMEM);
  51. ret = ff_set_common_channel_layouts(ctx, layouts);
  52. if (ret < 0)
  53. return ret;
  54. formats = ff_make_format_list(sample_fmts);
  55. if (!formats)
  56. return AVERROR(ENOMEM);
  57. ret = ff_set_common_formats(ctx, formats);
  58. if (ret < 0)
  59. return ret;
  60. formats = ff_all_samplerates();
  61. if (!formats)
  62. return AVERROR(ENOMEM);
  63. return ff_set_common_samplerates(ctx, formats);
  64. }
  65. static int activate(AVFilterContext *ctx)
  66. {
  67. AudioMultiplyContext *s = ctx->priv;
  68. int i, ret, status;
  69. int nb_samples;
  70. int64_t pts;
  71. FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
  72. nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
  73. ff_inlink_queued_samples(ctx->inputs[1]));
  74. for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
  75. if (s->frames[i])
  76. continue;
  77. if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
  78. ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]);
  79. if (ret < 0)
  80. return ret;
  81. }
  82. }
  83. if (s->frames[0] && s->frames[1]) {
  84. AVFrame *out;
  85. int plane_samples;
  86. if (av_sample_fmt_is_planar(ctx->inputs[0]->format))
  87. plane_samples = FFALIGN(s->frames[0]->nb_samples, s->samples_align);
  88. else
  89. plane_samples = FFALIGN(s->frames[0]->nb_samples * s->channels, s->samples_align);
  90. out = ff_get_audio_buffer(ctx->outputs[0], s->frames[0]->nb_samples);
  91. if (!out)
  92. return AVERROR(ENOMEM);
  93. out->pts = s->frames[0]->pts;
  94. if (av_get_packed_sample_fmt(ctx->inputs[0]->format) == AV_SAMPLE_FMT_FLT) {
  95. for (i = 0; i < s->planes; i++) {
  96. s->fdsp->vector_fmul((float *)out->extended_data[i],
  97. (const float *)s->frames[0]->extended_data[i],
  98. (const float *)s->frames[1]->extended_data[i],
  99. plane_samples);
  100. }
  101. } else {
  102. for (i = 0; i < s->planes; i++) {
  103. s->fdsp->vector_dmul((double *)out->extended_data[i],
  104. (const double *)s->frames[0]->extended_data[i],
  105. (const double *)s->frames[1]->extended_data[i],
  106. plane_samples);
  107. }
  108. }
  109. emms_c();
  110. av_frame_free(&s->frames[0]);
  111. av_frame_free(&s->frames[1]);
  112. ret = ff_filter_frame(ctx->outputs[0], out);
  113. if (ret < 0)
  114. return ret;
  115. }
  116. if (!nb_samples) {
  117. for (i = 0; i < 2; i++) {
  118. if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
  119. ff_outlink_set_status(ctx->outputs[0], status, pts);
  120. return 0;
  121. }
  122. }
  123. }
  124. if (ff_outlink_frame_wanted(ctx->outputs[0])) {
  125. for (i = 0; i < 2; i++) {
  126. if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
  127. continue;
  128. ff_inlink_request_frame(ctx->inputs[i]);
  129. return 0;
  130. }
  131. }
  132. return 0;
  133. }
  134. static int config_output(AVFilterLink *outlink)
  135. {
  136. AVFilterContext *ctx = outlink->src;
  137. AudioMultiplyContext *s = ctx->priv;
  138. AVFilterLink *inlink = ctx->inputs[0];
  139. s->channels = inlink->channels;
  140. s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->channels : 1;
  141. s->samples_align = 16;
  142. return 0;
  143. }
  144. static av_cold int init(AVFilterContext *ctx)
  145. {
  146. AudioMultiplyContext *s = ctx->priv;
  147. s->fdsp = avpriv_float_dsp_alloc(0);
  148. if (!s->fdsp)
  149. return AVERROR(ENOMEM);
  150. return 0;
  151. }
  152. static av_cold void uninit(AVFilterContext *ctx)
  153. {
  154. AudioMultiplyContext *s = ctx->priv;
  155. av_freep(&s->fdsp);
  156. }
  157. static const AVFilterPad inputs[] = {
  158. {
  159. .name = "multiply0",
  160. .type = AVMEDIA_TYPE_AUDIO,
  161. },
  162. {
  163. .name = "multiply1",
  164. .type = AVMEDIA_TYPE_AUDIO,
  165. },
  166. { NULL }
  167. };
  168. static const AVFilterPad outputs[] = {
  169. {
  170. .name = "default",
  171. .type = AVMEDIA_TYPE_AUDIO,
  172. .config_props = config_output,
  173. },
  174. { NULL }
  175. };
  176. AVFilter ff_af_amultiply = {
  177. .name = "amultiply",
  178. .description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."),
  179. .priv_size = sizeof(AudioMultiplyContext),
  180. .init = init,
  181. .uninit = uninit,
  182. .activate = activate,
  183. .query_formats = query_formats,
  184. .inputs = inputs,
  185. .outputs = outputs,
  186. };