FFmpeg
af_dcshift.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000 Chris Ausbrooks <weed@bucket.pp.ualr.edu>
3  * Copyright (c) 2000 Fabien COELHO <fabien@coelho.net>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/opt.h"
23 #include "libavutil/samplefmt.h"
24 #include "avfilter.h"
25 #include "audio.h"
26 #include "internal.h"
27 
28 typedef struct DCShiftContext {
29  const AVClass *class;
30  double dcshift;
32  double limitergain;
34 
35 #define OFFSET(x) offsetof(DCShiftContext, x)
36 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
37 
38 static const AVOption dcshift_options[] = {
39  { "shift", "set DC shift", OFFSET(dcshift), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
40  { "limitergain", "set limiter gain", OFFSET(limitergain), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, A },
41  { NULL }
42 };
43 
44 AVFILTER_DEFINE_CLASS(dcshift);
45 
47 {
48  DCShiftContext *s = ctx->priv;
49 
50  s->limiterthreshold = INT32_MAX * (1.0 - (fabs(s->dcshift) - s->limitergain));
51 
52  return 0;
53 }
54 
56 {
57  AVFilterContext *ctx = inlink->dst;
58  AVFilterLink *outlink = ctx->outputs[0];
59  AVFrame *out;
60  DCShiftContext *s = ctx->priv;
61  int i, j;
62  double dcshift = s->dcshift;
63 
64  if (av_frame_is_writable(in)) {
65  out = in;
66  } else {
67  out = ff_get_audio_buffer(outlink, in->nb_samples);
68  if (!out) {
69  av_frame_free(&in);
70  return AVERROR(ENOMEM);
71  }
73  }
74 
75  if (s->limitergain > 0) {
76  for (i = 0; i < inlink->ch_layout.nb_channels; i++) {
77  const int32_t *src = (int32_t *)in->extended_data[i];
78  int32_t *dst = (int32_t *)out->extended_data[i];
79 
80  for (j = 0; j < in->nb_samples; j++) {
81  double d;
82 
83  d = src[j];
84 
85  if (d > s->limiterthreshold && dcshift > 0) {
86  d = (d - s->limiterthreshold) * s->limitergain /
87  (INT32_MAX - s->limiterthreshold) +
88  s->limiterthreshold + dcshift;
89  } else if (d < -s->limiterthreshold && dcshift < 0) {
90  d = (d + s->limiterthreshold) * s->limitergain /
91  (INT32_MAX - s->limiterthreshold) -
92  s->limiterthreshold + dcshift;
93  } else {
94  d = dcshift * INT32_MAX + d;
95  }
96 
97  dst[j] = av_clipl_int32(d);
98  }
99  }
100  } else {
101  for (i = 0; i < inlink->ch_layout.nb_channels; i++) {
102  const int32_t *src = (int32_t *)in->extended_data[i];
103  int32_t *dst = (int32_t *)out->extended_data[i];
104 
105  for (j = 0; j < in->nb_samples; j++) {
106  double d = dcshift * (INT32_MAX + 1.) + src[j];
107 
108  dst[j] = av_clipl_int32(d);
109  }
110  }
111  }
112 
113  if (out != in)
114  av_frame_free(&in);
115  return ff_filter_frame(outlink, out);
116 }
117 static const AVFilterPad dcshift_inputs[] = {
118  {
119  .name = "default",
120  .type = AVMEDIA_TYPE_AUDIO,
121  .filter_frame = filter_frame,
122  },
123 };
124 
126  .name = "dcshift",
127  .description = NULL_IF_CONFIG_SMALL("Apply a DC shift to the audio."),
128  .priv_size = sizeof(DCShiftContext),
129  .priv_class = &dcshift_class,
130  .init = init,
135 };
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:97
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
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1015
FILTER_SINGLE_SAMPLEFMT
#define FILTER_SINGLE_SAMPLEFMT(sample_fmt_)
Definition: internal.h:175
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:160
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:375
AVOption
AVOption.
Definition: opt.h:346
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
DCShiftContext::limiterthreshold
double limiterthreshold
Definition: af_dcshift.c:31
DCShiftContext::limitergain
double limitergain
Definition: af_dcshift.c:32
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
OFFSET
#define OFFSET(x)
Definition: af_dcshift.c:35
samplefmt.h
DCShiftContext::dcshift
double dcshift
Definition: af_dcshift.c:30
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
av_cold
#define av_cold
Definition: attributes.h:90
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:237
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:49
A
#define A
Definition: af_dcshift.c:36
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:709
ff_audio_default_filterpad
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition: audio.c:33
init
static av_cold int init(AVFilterContext *ctx)
Definition: af_dcshift.c:46
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:94
av_clipl_int32
#define av_clipl_int32
Definition: common.h:117
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:645
internal.h
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:147
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:455
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:436
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AVFilter
Filter definition.
Definition: avfilter.h:166
ff_af_dcshift
const AVFilter ff_af_dcshift
Definition: af_dcshift.c:125
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: af_dcshift.c:55
avfilter.h
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(dcshift)
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
audio.h
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
d
d
Definition: ffmpeg_filter.c:424
int32_t
int32_t
Definition: audioconvert.c:56
dcshift_options
static const AVOption dcshift_options[]
Definition: af_dcshift.c:38
dcshift_inputs
static const AVFilterPad dcshift_inputs[]
Definition: af_dcshift.c:117
DCShiftContext
Definition: af_dcshift.c:28