FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_amultiply.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 0 64 0.0%
Functions: 0 4 0.0%
Branches: 0 50 0.0%

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