FFmpeg
vf_exposure.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 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 <float.h>
22 
23 #include "libavutil/opt.h"
24 #include "libavutil/imgutils.h"
25 #include "avfilter.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 typedef struct ExposureContext {
31  const AVClass *class;
32 
33  float exposure;
34  float black;
35 
36  float scale;
38  int jobnr, int nb_jobs);
40 
41 typedef struct ThreadData {
42  AVFrame *out, *in;
43 } ThreadData;
44 
45 static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
46 {
47  ExposureContext *s = ctx->priv;
48  ThreadData *td = arg;
49  const int width = td->out->width;
50  const int height = td->out->height;
51  const int slice_start = (height * jobnr) / nb_jobs;
52  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
53  const float black = s->black;
54  const float scale = s->scale;
55 
56  for (int p = 0; p < 3; p++) {
57  const int slinesize = td->in->linesize[p] / 4;
58  const int dlinesize = td->out->linesize[p] / 4;
59  const float *src = (const float *)td->in->data[p] + slice_start * slinesize;
60  float *ptr = (float *)td->out->data[p] + slice_start * dlinesize;
61  for (int y = slice_start; y < slice_end; y++) {
62  for (int x = 0; x < width; x++)
63  ptr[x] = (src[x] - black) * scale;
64 
65  ptr += dlinesize;
66  src += slinesize;
67  }
68  }
69 
70  if (td->in->data[3] && td->in->linesize[3] && td->in != td->out) {
71  const int slinesize = td->in->linesize[3] / 4;
72  const int dlinesize = td->out->linesize[3] / 4;
73  const float *src = (const float *)td->in->data[3] + slice_start * slinesize;
74  float *ptr = (float *)td->out->data[3] + slice_start * dlinesize;
75  for (int y = slice_start; y < slice_end; y++) {
76  memcpy(ptr, src, width * sizeof(*ptr));
77  ptr += dlinesize;
78  src += slinesize;
79  }
80  }
81 
82  return 0;
83 }
84 
86 {
87  AVFilterContext *ctx = inlink->dst;
88  AVFilterLink *outlink = ctx->outputs[0];
89  ExposureContext *s = ctx->priv;
90  float diff = fabsf(exp2f(-s->exposure) - s->black);
91  ThreadData td;
92  AVFrame *out;
93 
94  if (av_frame_is_writable(in)) {
95  out = in;
96  } else {
97  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
98  if (!out) {
99  av_frame_free(&in);
100  return AVERROR(ENOMEM);
101  }
103  }
104 
105  diff = diff > 0.f ? diff : 1.f / 1024.f;
106  s->scale = 1.f / diff;
107  td.out = out;
108  td.in = in;
109  ff_filter_execute(ctx, s->do_slice, &td, NULL,
111 
112  if (out != in)
113  av_frame_free(&in);
114  return ff_filter_frame(outlink, out);
115 }
116 
118 {
119  AVFilterContext *ctx = inlink->dst;
120  ExposureContext *s = ctx->priv;
121 
122  s->do_slice = exposure_slice;
123 
124  return 0;
125 }
126 
127 static const AVFilterPad exposure_inputs[] = {
128  {
129  .name = "default",
130  .type = AVMEDIA_TYPE_VIDEO,
131  .filter_frame = filter_frame,
132  .config_props = config_input,
133  },
134 };
135 
136 static const AVFilterPad exposure_outputs[] = {
137  {
138  .name = "default",
139  .type = AVMEDIA_TYPE_VIDEO,
140  },
141 };
142 
143 #define OFFSET(x) offsetof(ExposureContext, x)
144 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
145 
146 static const AVOption exposure_options[] = {
147  { "exposure", "set the exposure correction", OFFSET(exposure), AV_OPT_TYPE_FLOAT, {.dbl=0}, -3, 3, VF },
148  { "black", "set the black level correction", OFFSET(black), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
149  { NULL }
150 };
151 
152 AVFILTER_DEFINE_CLASS(exposure);
153 
155  .name = "exposure",
156  .description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."),
157  .priv_size = sizeof(ExposureContext),
158  .priv_class = &exposure_class,
163  .process_command = ff_filter_process_command,
164 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
td
#define td
Definition: regdef.h:70
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
OFFSET
#define OFFSET(x)
Definition: vf_exposure.c:143
out
FILE * out
Definition: movenc.c:54
ff_vf_exposure
const AVFilter ff_vf_exposure
Definition: vf_exposure.c:154
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
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:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
ExposureContext::exposure
float exposure
Definition: vf_exposure.c:33
exposure_slice
static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_exposure.c:45
AVOption
AVOption.
Definition: opt.h:251
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
formats.h
scale
static av_always_inline float scale(float x, float s)
Definition: vf_v360.c:1389
fabsf
static __device__ float fabsf(float a)
Definition: cuda_runtime.h:181
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
exposure_outputs
static const AVFilterPad exposure_outputs[]
Definition: vf_exposure.c:136
av_cold
#define av_cold
Definition: attributes.h:90
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:256
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2006
ctx
AVFormatContext * ctx
Definition: movenc.c:48
exp2f
#define exp2f(x)
Definition: libm.h:293
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
arg
const char * arg
Definition: jacosubdec.c:67
ExposureContext::black
float black
Definition: vf_exposure.c:34
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:594
ExposureContext::do_slice
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition: vf_exposure.c:37
exposure_options
static const AVOption exposure_options[]
Definition: vf_exposure.c:146
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:115
FILTER_PIXFMTS
#define FILTER_PIXFMTS(...)
Definition: internal.h:180
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:488
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:524
ExposureContext
Definition: vf_exposure.c:30
ff_filter_process_command
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition: avfilter.c:842
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:162
height
#define height
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:142
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:228
ExposureContext::scale
float scale
Definition: vf_exposure.c:36
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:777
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:161
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(exposure)
VF
#define VF
Definition: vf_exposure.c:144
avfilter.h
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:489
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:117
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
exposure_inputs
static const AVFilterPad exposure_inputs[]
Definition: vf_exposure.c:127
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_exposure.c:85
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
imgutils.h
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:146
int
int
Definition: ffmpeg_filter.c:156
config_input
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_exposure.c:117