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 "avfilter.h"
25 #include "internal.h"
26 #include "video.h"
27 
28 typedef struct ExposureContext {
29  const AVClass *class;
30 
31  float exposure;
32  float black;
33 
34  float scale;
36  int jobnr, int nb_jobs);
38 
39 typedef struct ThreadData {
40  AVFrame *out, *in;
41 } ThreadData;
42 
43 static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
44 {
45  ExposureContext *s = ctx->priv;
46  ThreadData *td = arg;
47  const int width = td->out->width;
48  const int height = td->out->height;
49  const int slice_start = (height * jobnr) / nb_jobs;
50  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
51  const float black = s->black;
52  const float scale = s->scale;
53 
54  for (int p = 0; p < 3; p++) {
55  const int slinesize = td->in->linesize[p] / 4;
56  const int dlinesize = td->out->linesize[p] / 4;
57  const float *src = (const float *)td->in->data[p] + slice_start * slinesize;
58  float *ptr = (float *)td->out->data[p] + slice_start * dlinesize;
59  for (int y = slice_start; y < slice_end; y++) {
60  for (int x = 0; x < width; x++)
61  ptr[x] = (src[x] - black) * scale;
62 
63  ptr += dlinesize;
64  src += slinesize;
65  }
66  }
67 
68  if (td->in->data[3] && td->in->linesize[3] && td->in != td->out) {
69  const int slinesize = td->in->linesize[3] / 4;
70  const int dlinesize = td->out->linesize[3] / 4;
71  const float *src = (const float *)td->in->data[3] + slice_start * slinesize;
72  float *ptr = (float *)td->out->data[3] + slice_start * dlinesize;
73  for (int y = slice_start; y < slice_end; y++) {
74  memcpy(ptr, src, width * sizeof(*ptr));
75  ptr += dlinesize;
76  src += slinesize;
77  }
78  }
79 
80  return 0;
81 }
82 
84 {
85  AVFilterContext *ctx = inlink->dst;
86  AVFilterLink *outlink = ctx->outputs[0];
87  ExposureContext *s = ctx->priv;
88  float diff = fabsf(exp2f(-s->exposure) - s->black);
89  ThreadData td;
90  AVFrame *out;
91 
92  if (av_frame_is_writable(in)) {
93  out = in;
94  } else {
95  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
96  if (!out) {
97  av_frame_free(&in);
98  return AVERROR(ENOMEM);
99  }
101  }
102 
103  diff = diff > 0.f ? diff : 1.f / 1024.f;
104  s->scale = 1.f / diff;
105  td.out = out;
106  td.in = in;
107  ff_filter_execute(ctx, s->do_slice, &td, NULL,
109 
110  if (out != in)
111  av_frame_free(&in);
112  return ff_filter_frame(outlink, out);
113 }
114 
116 {
117  AVFilterContext *ctx = inlink->dst;
118  ExposureContext *s = ctx->priv;
119 
120  s->do_slice = exposure_slice;
121 
122  return 0;
123 }
124 
125 static const AVFilterPad exposure_inputs[] = {
126  {
127  .name = "default",
128  .type = AVMEDIA_TYPE_VIDEO,
129  .filter_frame = filter_frame,
130  .config_props = config_input,
131  },
132 };
133 
134 #define OFFSET(x) offsetof(ExposureContext, x)
135 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
136 
137 static const AVOption exposure_options[] = {
138  { "exposure", "set the exposure correction", OFFSET(exposure), AV_OPT_TYPE_FLOAT, {.dbl=0}, -3, 3, VF },
139  { "black", "set the black level correction", OFFSET(black), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
140  { NULL }
141 };
142 
143 AVFILTER_DEFINE_CLASS(exposure);
144 
146  .name = "exposure",
147  .description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."),
148  .priv_size = sizeof(ExposureContext),
149  .priv_class = &exposure_class,
154  .process_command = ff_filter_process_command,
155 };
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:112
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:134
out
FILE * out
Definition: movenc.c:54
ff_vf_exposure
const AVFilter ff_vf_exposure
Definition: vf_exposure.c:145
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
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
ExposureContext::exposure
float exposure
Definition: vf_exposure.c:31
exposure_slice
static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_exposure.c:43
AVOption
AVOption.
Definition: opt.h:346
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:153
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:33
slice_start
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition: vvcdec.c:685
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:198
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:1725
ctx
AVFormatContext * ctx
Definition: movenc.c:48
exp2f
#define exp2f(x)
Definition: libm.h:293
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
arg
const char * arg
Definition: jacosubdec.c:67
ExposureContext::black
float black
Definition: vf_exposure.c:32
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:709
ExposureContext::do_slice
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition: vf_exposure.c:35
exposure_options
static const AVOption exposure_options[]
Definition: vf_exposure.c:137
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: vvc_intra.c:291
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:106
FILTER_PIXFMTS
#define FILTER_PIXFMTS(...)
Definition: internal.h:168
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:508
av_frame_is_writable
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:645
ExposureContext
Definition: vf_exposure.c:28
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:890
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:164
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:147
AV_OPT_TYPE_FLOAT
@ AV_OPT_TYPE_FLOAT
Definition: opt.h:238
ExposureContext::scale
float scale
Definition: vf_exposure.c:34
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:825
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:39
AVFilter
Filter definition.
Definition: avfilter.h:166
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(exposure)
VF
#define VF
Definition: vf_exposure.c:135
avfilter.h
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:509
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
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:125
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_exposure.c:83
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
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:134
int
int
Definition: ffmpeg_filter.c:410
config_input
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_exposure.c:115