FFmpeg
af_amultiply.c
Go to the documentation of this file.
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 
22 #include "libavutil/common.h"
23 #include "libavutil/float_dsp.h"
24 #include "libavutil/opt.h"
25 
26 #include "audio.h"
27 #include "avfilter.h"
28 #include "formats.h"
29 #include "filters.h"
30 #include "internal.h"
31 
32 typedef struct AudioMultiplyContext {
33  const AVClass *class;
34 
36  int planes;
37  int channels;
39 
42 
44 {
47  static const enum AVSampleFormat sample_fmts[] = {
51  };
52  int ret;
53 
55  if (!layouts)
56  return AVERROR(ENOMEM);
58  if (ret < 0)
59  return ret;
60 
62  if (!formats)
63  return AVERROR(ENOMEM);
65  if (ret < 0)
66  return ret;
67 
69  if (!formats)
70  return AVERROR(ENOMEM);
72 }
73 
75 {
76  AudioMultiplyContext *s = ctx->priv;
77  int i, ret, status;
78  int nb_samples;
79  int64_t pts;
80 
82 
83  nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
84  ff_inlink_queued_samples(ctx->inputs[1]));
85  for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
86  if (s->frames[i])
87  continue;
88 
89  if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
90  ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]);
91  if (ret < 0)
92  return ret;
93  }
94  }
95 
96  if (s->frames[0] && s->frames[1]) {
97  AVFrame *out;
98  int plane_samples;
99 
100  if (av_sample_fmt_is_planar(ctx->inputs[0]->format))
101  plane_samples = FFALIGN(s->frames[0]->nb_samples, s->samples_align);
102  else
103  plane_samples = FFALIGN(s->frames[0]->nb_samples * s->channels, s->samples_align);
104 
105  out = ff_get_audio_buffer(ctx->outputs[0], s->frames[0]->nb_samples);
106  if (!out)
107  return AVERROR(ENOMEM);
108 
109  out->pts = s->frames[0]->pts;
110 
111  if (av_get_packed_sample_fmt(ctx->inputs[0]->format) == AV_SAMPLE_FMT_FLT) {
112  for (i = 0; i < s->planes; i++) {
113  s->fdsp->vector_fmul((float *)out->extended_data[i],
114  (const float *)s->frames[0]->extended_data[i],
115  (const float *)s->frames[1]->extended_data[i],
116  plane_samples);
117  }
118  } else {
119  for (i = 0; i < s->planes; i++) {
120  s->fdsp->vector_dmul((double *)out->extended_data[i],
121  (const double *)s->frames[0]->extended_data[i],
122  (const double *)s->frames[1]->extended_data[i],
123  plane_samples);
124  }
125  }
126  emms_c();
127 
128  av_frame_free(&s->frames[0]);
129  av_frame_free(&s->frames[1]);
130 
131  ret = ff_filter_frame(ctx->outputs[0], out);
132  if (ret < 0)
133  return ret;
134  }
135 
136  if (!nb_samples) {
137  for (i = 0; i < 2; i++) {
138  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
139  ff_outlink_set_status(ctx->outputs[0], status, pts);
140  return 0;
141  }
142  }
143  }
144 
145  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
146  for (i = 0; i < 2; i++) {
147  if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
148  continue;
149  ff_inlink_request_frame(ctx->inputs[i]);
150  return 0;
151  }
152  }
153  return 0;
154 }
155 
156 static int config_output(AVFilterLink *outlink)
157 {
158  AVFilterContext *ctx = outlink->src;
159  AudioMultiplyContext *s = ctx->priv;
160  AVFilterLink *inlink = ctx->inputs[0];
161 
162  s->channels = inlink->channels;
163  s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->channels : 1;
164  s->samples_align = 16;
165 
166  return 0;
167 }
168 
170 {
171  AudioMultiplyContext *s = ctx->priv;
172 
173  s->fdsp = avpriv_float_dsp_alloc(0);
174  if (!s->fdsp)
175  return AVERROR(ENOMEM);
176 
177  return 0;
178 }
179 
181 {
182  AudioMultiplyContext *s = ctx->priv;
183  av_freep(&s->fdsp);
184 }
185 
186 static const AVFilterPad inputs[] = {
187  {
188  .name = "multiply0",
189  .type = AVMEDIA_TYPE_AUDIO,
190  },
191  {
192  .name = "multiply1",
193  .type = AVMEDIA_TYPE_AUDIO,
194  },
195  { NULL }
196 };
197 
198 static const AVFilterPad outputs[] = {
199  {
200  .name = "default",
201  .type = AVMEDIA_TYPE_AUDIO,
202  .config_props = config_output,
203  },
204  { NULL }
205 };
206 
208  .name = "amultiply",
209  .description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."),
210  .priv_size = sizeof(AudioMultiplyContext),
211  .init = init,
212  .uninit = uninit,
213  .activate = activate,
215  .inputs = inputs,
216  .outputs = outputs,
217 };
formats
formats
Definition: signature.h:48
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
AV_SAMPLE_FMT_FLTP
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition: samplefmt.h:69
AVFilterChannelLayouts
A list of supported channel layouts.
Definition: formats.h:85
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
sample_fmts
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:948
layouts
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
ff_all_channel_counts
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition.
Definition: formats.c:429
AudioMultiplyContext::fdsp
AVFloatDSPContext * fdsp
Definition: af_amultiply.c:40
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
FF_FILTER_FORWARD_STATUS_BACK_ALL
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
pts
static int64_t pts
Definition: transcode_aac.c:653
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
ff_inlink_check_available_samples
int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
Definition: avfilter.c:1361
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:580
config_output
static int config_output(AVFilterLink *outlink)
Definition: af_amultiply.c:156
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
ff_inlink_request_frame
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1502
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
filters.h
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_amultiply.c:180
ctx
AVFormatContext * ctx
Definition: movenc.c:48
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_amultiply.c:169
inputs
static const AVFilterPad inputs[]
Definition: af_amultiply.c:186
AudioMultiplyContext::samples_align
int samples_align
Definition: af_amultiply.c:38
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_inlink_consume_samples
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition: avfilter.c:1395
NULL
#define NULL
Definition: coverity.c:32
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1331
float_dsp.h
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:116
AudioMultiplyContext::frames
AVFrame * frames[2]
Definition: af_amultiply.c:35
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
AVFloatDSPContext
Definition: float_dsp.h:24
activate
static int activate(AVFilterContext *ctx)
Definition: af_amultiply.c:74
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
internal.h
i
int i
Definition: input.c:406
AudioMultiplyContext::channels
int channels
Definition: af_amultiply.c:37
common.h
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AudioMultiplyContext::planes
int planes
Definition: af_amultiply.c:36
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1356
AVFilter
Filter definition.
Definition: avfilter.h:145
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: af_amultiply.c:43
ret
ret
Definition: filter_design.txt:187
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:414
channel_layout.h
avfilter.h
av_get_packed_sample_fmt
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition: samplefmt.c:75
AV_SAMPLE_FMT_DBLP
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition: samplefmt.h:70
AVFilterContext
An instance of a filter.
Definition: avfilter.h:333
ff_af_amultiply
const AVFilter ff_af_amultiply
Definition: af_amultiply.c:207
audio.h
outputs
static const AVFilterPad outputs[]
Definition: af_amultiply.c:198
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:48
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
avpriv_float_dsp_alloc
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
ff_set_common_samplerates
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:568
ff_outlink_frame_wanted
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the ff_outlink_frame_wanted() function. If this function returns true
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
AudioMultiplyContext
Definition: af_amultiply.c:32
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
ff_set_common_channel_layouts
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *channel_layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates.
Definition: formats.c:561