FFmpeg
Loading...
Searching...
No Matches
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 "filters.h"
26#include "video.h"
27
28typedef struct ExposureContext {
29 const AVClass *class;
30
31 float exposure;
32 float black;
33
34 float scale;
35 int (*do_slice)(AVFilterContext *s, void *arg,
36 int jobnr, int nb_jobs);
38
39typedef struct ThreadData {
40 AVFrame *out, *in;
42
43static 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 = ff_slice_pos(height, jobnr, nb_jobs);
50 const int slice_end = ff_slice_pos(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
83static int filter_frame(AVFilterLink *inlink, AVFrame *in)
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
125static 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
137static 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
144
146 .p.name = "exposure",
147 .p.description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."),
148 .p.priv_class = &exposure_class,
150 .priv_size = sizeof(ExposureContext),
154 .process_command = ff_filter_process_command,
155};
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_exposure
static FILE * out
static AVFormatContext * ctx
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
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:906
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
static __device__ float fabsf(float a)
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
#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:196
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition frame.c:535
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
const char * arg
Definition jacosubdec.c:65
#define VF
Definition af_afir.c:731
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define FILTER_PIXFMTS(...)
Definition filters.h:250
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define exp2f(x)
Definition libm.h:295
#define FFMIN(a, b)
Definition macros.h:49
static int slice_end(AVCodecContext *avctx, AVFrame *pict, int *got_output)
Handle slice ends.
Definition mpeg12dec.c:1697
AVOptions.
#define AV_PIX_FMT_GBRPF32
Definition pixfmt.h:584
#define AV_PIX_FMT_GBRAPF32
Definition pixfmt.h:585
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
AVOption.
Definition opt.h:428
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition vf_exposure.c:35
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
#define src
Definition vp8dsp.c:248
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
static const AVFilterPad exposure_inputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_exposure.c:83
static av_cold int config_input(AVFilterLink *inlink)
static const AVOption exposure_options[]
#define OFFSET(x)
static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_exposure.c:43
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
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
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static int slice_start(SliceContext *sc, VVCContext *s, VVCFrameContext *fc, const CodedBitstreamUnit *unit, const int is_first_slice)
Definition dec.c:844