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
21 #include "libavutil/avassert.h"
22 #include "libavutil/channel_layout.h"
23 #include "libavutil/common.h"
24 #include "libavutil/float_dsp.h"
25 #include "libavutil/opt.h"
26
27 #include "audio.h"
28 #include "avfilter.h"
29 #include "formats.h"
30 #include "filters.h"
31 #include "internal.h"
32
33 typedef struct AudioMultiplyContext {
34 const AVClass *class;
35
36 AVFrame *frames[2];
37 int planes;
38 int channels;
39 int samples_align;
40
41 AVFloatDSPContext *fdsp;
42 } AudioMultiplyContext;
43
query_formats(AVFilterContext * ctx)44 static int query_formats(AVFilterContext *ctx)
45 {
46 AVFilterFormats *formats;
47 AVFilterChannelLayouts *layouts;
48 static const enum AVSampleFormat sample_fmts[] = {
49 AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLTP,
50 AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBLP,
51 AV_SAMPLE_FMT_NONE
52 };
53 int ret;
54
55 layouts = ff_all_channel_counts();
56 if (!layouts)
57 return AVERROR(ENOMEM);
58 ret = ff_set_common_channel_layouts(ctx, layouts);
59 if (ret < 0)
60 return ret;
61
62 formats = ff_make_format_list(sample_fmts);
63 if (!formats)
64 return AVERROR(ENOMEM);
65 ret = ff_set_common_formats(ctx, formats);
66 if (ret < 0)
67 return ret;
68
69 formats = ff_all_samplerates();
70 if (!formats)
71 return AVERROR(ENOMEM);
72 return ff_set_common_samplerates(ctx, formats);
73 }
74
activate(AVFilterContext * ctx)75 static int activate(AVFilterContext *ctx)
76 {
77 AudioMultiplyContext *s = ctx->priv;
78 int i, ret, status;
79 int nb_samples;
80 int64_t pts;
81
82 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
83
84 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
85 ff_inlink_queued_samples(ctx->inputs[1]));
86 for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
87 if (s->frames[i])
88 continue;
89
90 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
91 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]);
92 if (ret < 0)
93 return ret;
94 }
95 }
96
97 if (s->frames[0] && s->frames[1]) {
98 AVFrame *out;
99 int plane_samples;
100
101 if (av_sample_fmt_is_planar(ctx->inputs[0]->format))
102 plane_samples = FFALIGN(s->frames[0]->nb_samples, s->samples_align);
103 else
104 plane_samples = FFALIGN(s->frames[0]->nb_samples * s->channels, s->samples_align);
105
106 out = ff_get_audio_buffer(ctx->outputs[0], s->frames[0]->nb_samples);
107 if (!out)
108 return AVERROR(ENOMEM);
109
110 out->pts = s->frames[0]->pts;
111
112 if (av_get_packed_sample_fmt(ctx->inputs[0]->format) == AV_SAMPLE_FMT_FLT) {
113 for (i = 0; i < s->planes; i++) {
114 s->fdsp->vector_fmul((float *)out->extended_data[i],
115 (const float *)s->frames[0]->extended_data[i],
116 (const float *)s->frames[1]->extended_data[i],
117 plane_samples);
118 }
119 } else {
120 for (i = 0; i < s->planes; i++) {
121 s->fdsp->vector_dmul((double *)out->extended_data[i],
122 (const double *)s->frames[0]->extended_data[i],
123 (const double *)s->frames[1]->extended_data[i],
124 plane_samples);
125 }
126 }
127 emms_c();
128
129 av_frame_free(&s->frames[0]);
130 av_frame_free(&s->frames[1]);
131
132 ret = ff_filter_frame(ctx->outputs[0], out);
133 if (ret < 0)
134 return ret;
135 }
136
137 if (!nb_samples) {
138 for (i = 0; i < 2; i++) {
139 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
140 ff_outlink_set_status(ctx->outputs[0], status, pts);
141 return 0;
142 }
143 }
144 }
145
146 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
147 for (i = 0; i < 2; i++) {
148 if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
149 continue;
150 ff_inlink_request_frame(ctx->inputs[i]);
151 return 0;
152 }
153 }
154 return 0;
155 }
156
config_output(AVFilterLink * outlink)157 static int config_output(AVFilterLink *outlink)
158 {
159 AVFilterContext *ctx = outlink->src;
160 AudioMultiplyContext *s = ctx->priv;
161 AVFilterLink *inlink = ctx->inputs[0];
162
163 s->channels = inlink->channels;
164 s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->channels : 1;
165 s->samples_align = 16;
166
167 return 0;
168 }
169
init(AVFilterContext * ctx)170 static av_cold int init(AVFilterContext *ctx)
171 {
172 AudioMultiplyContext *s = ctx->priv;
173
174 s->fdsp = avpriv_float_dsp_alloc(0);
175 if (!s->fdsp)
176 return AVERROR(ENOMEM);
177
178 return 0;
179 }
180
uninit(AVFilterContext * ctx)181 static av_cold void uninit(AVFilterContext *ctx)
182 {
183 AudioMultiplyContext *s = ctx->priv;
184 av_freep(&s->fdsp);
185 }
186
187 static const AVFilterPad inputs[] = {
188 {
189 .name = "multiply0",
190 .type = AVMEDIA_TYPE_AUDIO,
191 },
192 {
193 .name = "multiply1",
194 .type = AVMEDIA_TYPE_AUDIO,
195 },
196 { NULL }
197 };
198
199 static const AVFilterPad outputs[] = {
200 {
201 .name = "default",
202 .type = AVMEDIA_TYPE_AUDIO,
203 .config_props = config_output,
204 },
205 { NULL }
206 };
207
208 AVFilter ff_af_amultiply = {
209 .name = "amultiply",
210 .description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."),
211 .priv_size = sizeof(AudioMultiplyContext),
212 .init = init,
213 .uninit = uninit,
214 .activate = activate,
215 .query_formats = query_formats,
216 .inputs = inputs,
217 .outputs = outputs,
218 };
219