FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
21 #include "libavutil/avassert.h"
23 #include "libavutil/common.h"
24 #include "libavutil/float_dsp.h"
25 #include "libavutil/opt.h"
26 
27 #define FF_INTERNAL_FIELDS 1
28 #include "framequeue.h"
29 
30 #include "audio.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "filters.h"
34 #include "internal.h"
35 
36 typedef struct AudioMultiplyContext {
37  const AVClass *class;
38 
40  int64_t pts;
41  int planes;
42  int channels;
44 
47 
49 {
52  static const enum AVSampleFormat sample_fmts[] = {
56  };
57  int ret;
58 
59  layouts = ff_all_channel_counts();
60  if (!layouts)
61  return AVERROR(ENOMEM);
62  ret = ff_set_common_channel_layouts(ctx, layouts);
63  if (ret < 0)
64  return ret;
65 
66  formats = ff_make_format_list(sample_fmts);
67  if (!formats)
68  return AVERROR(ENOMEM);
69  ret = ff_set_common_formats(ctx, formats);
70  if (ret < 0)
71  return ret;
72 
73  formats = ff_all_samplerates();
74  if (!formats)
75  return AVERROR(ENOMEM);
76  return ff_set_common_samplerates(ctx, formats);
77 }
78 
80 {
81  AudioMultiplyContext *s = ctx->priv;
82  int i, ret, status;
83  int nb_samples;
84  int64_t pts;
85 
87 
88  nb_samples = FFMIN(ff_framequeue_queued_samples(&ctx->inputs[0]->fifo),
89  ff_framequeue_queued_samples(&ctx->inputs[1]->fifo));
90  for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
91  if (s->frames[i])
92  continue;
93 
94  if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
95  ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frames[i]);
96  if (ret < 0)
97  return ret;
98  }
99  }
100 
101  if (nb_samples > 0 && s->frames[0] && s->frames[1]) {
102  AVFrame *out;
103  int plane_samples;
104 
105  if (av_sample_fmt_is_planar(ctx->inputs[0]->format))
106  plane_samples = FFALIGN(nb_samples, s->samples_align);
107  else
108  plane_samples = FFALIGN(nb_samples * s->channels, s->samples_align);
109 
110  out = ff_get_audio_buffer(ctx->outputs[0], nb_samples);
111  if (!out)
112  return AVERROR(ENOMEM);
113 
114  out->pts = s->pts;
115  s->pts += nb_samples;
116 
118  for (i = 0; i < s->planes; i++) {
119  s->fdsp->vector_fmul((float *)out->extended_data[i],
120  (const float *)s->frames[0]->extended_data[i],
121  (const float *)s->frames[1]->extended_data[i],
122  plane_samples);
123  }
124  } else {
125  for (i = 0; i < s->planes; i++) {
126  s->fdsp->vector_dmul((double *)out->extended_data[i],
127  (const double *)s->frames[0]->extended_data[i],
128  (const double *)s->frames[1]->extended_data[i],
129  plane_samples);
130  }
131  }
132  emms_c();
133 
134  av_frame_free(&s->frames[0]);
135  av_frame_free(&s->frames[1]);
136 
137  ret = ff_filter_frame(ctx->outputs[0], out);
138  if (ret < 0)
139  return ret;
140  }
141 
142  if (!nb_samples) {
143  for (i = 0; i < 2; i++) {
144  if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
145  ff_outlink_set_status(ctx->outputs[0], status, pts);
146  return 0;
147  }
148  }
149  }
150 
151  if (ff_outlink_frame_wanted(ctx->outputs[0])) {
152  for (i = 0; i < 2; i++) {
153  if (ff_framequeue_queued_samples(&ctx->inputs[i]->fifo) > 0)
154  continue;
156  return 0;
157  }
158  }
159  return 0;
160 }
161 
162 static int config_output(AVFilterLink *outlink)
163 {
164  AVFilterContext *ctx = outlink->src;
165  AudioMultiplyContext *s = ctx->priv;
166  AVFilterLink *inlink = ctx->inputs[0];
167 
168  s->channels = inlink->channels;
169  s->planes = av_sample_fmt_is_planar(inlink->format) ? inlink->channels : 1;
170  s->samples_align = 16;
171 
172  return 0;
173 }
174 
176 {
177  AudioMultiplyContext *s = ctx->priv;
178 
180  if (!s->fdsp)
181  return AVERROR(ENOMEM);
182 
183  return 0;
184 }
185 
187 {
188  AudioMultiplyContext *s = ctx->priv;
189  av_freep(&s->fdsp);
190 }
191 
192 static const AVFilterPad inputs[] = {
193  {
194  .name = "multiply0",
195  .type = AVMEDIA_TYPE_AUDIO,
196  },
197  {
198  .name = "multiply1",
199  .type = AVMEDIA_TYPE_AUDIO,
200  },
201  { NULL }
202 };
203 
204 static const AVFilterPad outputs[] = {
205  {
206  .name = "default",
207  .type = AVMEDIA_TYPE_AUDIO,
208  .config_props = config_output,
209  },
210  { NULL }
211 };
212 
214  .name = "amultiply",
215  .description = NULL_IF_CONFIG_SMALL("Multiply two audio streams."),
216  .priv_size = sizeof(AudioMultiplyContext),
217  .init = init,
218  .uninit = uninit,
219  .activate = activate,
221  .inputs = inputs,
222  .outputs = outputs,
223 };
float, planar
Definition: samplefmt.h:69
#define NULL
Definition: coverity.c:32
int ff_set_common_channel_layouts(AVFilterContext *ctx, AVFilterChannelLayouts *layouts)
A helper for query_formats() which sets all links to the same list of channel layouts/sample rates...
Definition: formats.c:549
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
static int config_output(AVFilterLink *outlink)
Definition: af_amultiply.c:162
#define FF_FILTER_FORWARD_STATUS_BACK_ALL(outlink, filter)
Forward the status on an output link to all input links.
Definition: filters.h:212
Main libavfilter public API header.
double, planar
Definition: samplefmt.h:70
static av_cold void uninit(AVFilterContext *ctx)
Definition: af_amultiply.c:186
static av_cold int init(AVFilterContext *ctx)
Definition: af_amultiply.c:175
int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
Test if enough samples are available on the link.
Definition: avfilter.c:1466
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
void ff_inlink_request_frame(AVFilterLink *link)
Mark that a frame is wanted on the link.
Definition: avfilter.c:1607
static int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition: filters.h:172
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
#define av_cold
Definition: attributes.h:82
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:319
#define FFALIGN(x, a)
Definition: macros.h:48
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:112
A filter pad used for either input or output.
Definition: internal.h:54
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:1436
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:568
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition: float_dsp.c:135
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
void(* vector_fmul)(float *dst, const float *src0, const float *src1, int len)
Calculate the entry wise product of two vectors of floats and store the result in a vector of floats...
Definition: float_dsp.h:38
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
simple assert() macros that are a bit more flexible than ISO C assert().
AVFrame * frames[2]
Definition: af_amultiply.c:39
audio channel layout utility functions
unsigned nb_inputs
number of input pads
Definition: avfilter.h:347
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad inputs[]
Definition: af_amultiply.c:192
#define s(width, name)
Definition: cbs_vp9.c:257
A list of supported channel layouts.
Definition: formats.h:85
AVFloatDSPContext * fdsp
Definition: af_amultiply.c:45
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
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:1500
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
static uint64_t ff_framequeue_queued_samples(const FFFrameQueue *fq)
Get the number of queued samples.
Definition: framequeue.h:154
static const AVFilterPad outputs[]
Definition: af_amultiply.c:204
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
enum MovChannelLayoutTag * layouts
Definition: mov_chan.c:434
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:395
static int64_t pts
common internal and external API header
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
AVFilter ff_af_amultiply
Definition: af_amultiply.c:213
static int activate(AVFilterContext *ctx)
Definition: af_amultiply.c:79
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static int query_formats(AVFilterContext *ctx)
Definition: af_amultiply.c:48
An instance of a filter.
Definition: avfilter.h:338
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
FILE * out
Definition: movenc.c:54
#define av_freep(p)
formats
Definition: signature.h:48
internal API functions
AVFilterChannelLayouts * ff_all_channel_counts(void)
Construct an AVFilterChannelLayouts coding for any channel layout, with known or unknown disposition...
Definition: formats.c:410
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
int ff_set_common_samplerates(AVFilterContext *ctx, AVFilterFormats *samplerates)
Definition: formats.c:556
void(* vector_dmul)(double *dst, const double *src0, const double *src1, int len)
Calculate the entry wise product of two vectors of doubles and store the result in a vector of double...
Definition: float_dsp.h:190