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 static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
42 {
43  ExposureContext *s = ctx->priv;
44  AVFrame *frame = arg;
45  const int width = frame->width;
46  const int height = frame->height;
47  const int slice_start = (height * jobnr) / nb_jobs;
48  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
49  const float black = s->black;
50  const float scale = s->scale;
51 
52  for (int p = 0; p < 3; p++) {
53  const int linesize = frame->linesize[p] / 4;
54  float *ptr = (float *)frame->data[p] + slice_start * linesize;
55  for (int y = slice_start; y < slice_end; y++) {
56  for (int x = 0; x < width; x++)
57  ptr[x] = (ptr[x] - black) * scale;
58 
59  ptr += linesize;
60  }
61  }
62 
63  return 0;
64 }
65 
67 {
68  AVFilterContext *ctx = inlink->dst;
69  ExposureContext *s = ctx->priv;
70 
71  s->scale = 1.f / (exp2f(-s->exposure) - s->black);
72  ctx->internal->execute(ctx, s->do_slice, frame, NULL,
74 
75  return ff_filter_frame(ctx->outputs[0], frame);
76 }
77 
79 {
80  static const enum AVPixelFormat pixel_fmts[] = {
83  };
84 
86 
87  formats = ff_make_format_list(pixel_fmts);
88  if (!formats)
89  return AVERROR(ENOMEM);
90 
92 }
93 
95 {
96  AVFilterContext *ctx = inlink->dst;
97  ExposureContext *s = ctx->priv;
98 
99  s->do_slice = exposure_slice;
100 
101  return 0;
102 }
103 
104 static const AVFilterPad exposure_inputs[] = {
105  {
106  .name = "default",
107  .type = AVMEDIA_TYPE_VIDEO,
108  .needs_writable = 1,
109  .filter_frame = filter_frame,
110  .config_props = config_input,
111  },
112  { NULL }
113 };
114 
115 static const AVFilterPad exposure_outputs[] = {
116  {
117  .name = "default",
118  .type = AVMEDIA_TYPE_VIDEO,
119  },
120  { NULL }
121 };
122 
123 #define OFFSET(x) offsetof(ExposureContext, x)
124 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
125 
126 static const AVOption exposure_options[] = {
127  { "exposure", "set the exposure correction", OFFSET(exposure), AV_OPT_TYPE_FLOAT, {.dbl=0}, -3, 3, VF },
128  { "black", "set the black level correction", OFFSET(black), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF },
129  { NULL }
130 };
131 
132 AVFILTER_DEFINE_CLASS(exposure);
133 
135  .name = "exposure",
136  .description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."),
137  .priv_size = sizeof(ExposureContext),
138  .priv_class = &exposure_class,
144 };
formats
formats
Definition: signature.h:48
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
ff_vf_exposure
AVFilter ff_vf_exposure
Definition: vf_exposure.c:134
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
OFFSET
#define OFFSET(x)
Definition: vf_exposure.c:123
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
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:41
AVOption
AVOption.
Definition: opt.h:248
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
video.h
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
formats.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_exposure.c:66
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
exposure_outputs
static const AVFilterPad exposure_outputs[]
Definition: vf_exposure.c:115
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:587
width
#define width
s
#define s(width, name)
Definition: cbs_vp9.c:257
slice_end
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2033
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
ctx
AVFormatContext * ctx
Definition: movenc.c:48
exp2f
#define exp2f(x)
Definition: libm.h:293
arg
const char * arg
Definition: jacosubdec.c:66
ExposureContext::black
float black
Definition: vf_exposure.c:34
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
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:126
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
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:117
process_command
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args, char *res, int res_len, int flags)
Definition: af_acrusher.c:336
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:428
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:882
height
#define height
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
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:126
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:802
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:145
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(exposure)
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: vf_exposure.c:78
VF
#define VF
Definition: vf_exposure.c:124
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
avfilter.h
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:429
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
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:104
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int
int
Definition: ffmpeg_filter.c:170
config_input
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_exposure.c:94