FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_colorize.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 0 124 0.0%
Functions: 0 11 0.0%
Branches: 0 36 0.0%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/opt.h"
20 #include "libavutil/pixdesc.h"
21 #include "avfilter.h"
22 #include "internal.h"
23 #include "video.h"
24
25 typedef struct ColorizeContext {
26 const AVClass *class;
27
28 float hue;
29 float saturation;
30 float lightness;
31 float mix;
32
33 int depth;
34 int c[3];
35 int planewidth[4];
36 int planeheight[4];
37
38 int (*do_plane_slice[2])(AVFilterContext *s, void *arg,
39 int jobnr, int nb_jobs);
40 } ColorizeContext;
41
42 static inline float lerpf(float v0, float v1, float f)
43 {
44 return v0 + (v1 - v0) * f;
45 }
46
47 static int colorizey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
48 {
49 ColorizeContext *s = ctx->priv;
50 AVFrame *frame = arg;
51 const int width = s->planewidth[0];
52 const int height = s->planeheight[0];
53 const int slice_start = (height * jobnr) / nb_jobs;
54 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
55 const ptrdiff_t ylinesize = frame->linesize[0];
56 uint8_t *yptr = frame->data[0] + slice_start * ylinesize;
57 const int yv = s->c[0];
58 const float mix = s->mix;
59
60 for (int y = slice_start; y < slice_end; y++) {
61 for (int x = 0; x < width; x++)
62 yptr[x] = lerpf(yv, yptr[x], mix);
63
64 yptr += ylinesize;
65 }
66
67 return 0;
68 }
69
70 static int colorizey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
71 {
72 ColorizeContext *s = ctx->priv;
73 AVFrame *frame = arg;
74 const int width = s->planewidth[0];
75 const int height = s->planeheight[0];
76 const int slice_start = (height * jobnr) / nb_jobs;
77 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
78 const ptrdiff_t ylinesize = frame->linesize[0] / 2;
79 uint16_t *yptr = (uint16_t *)frame->data[0] + slice_start * ylinesize;
80 const int yv = s->c[0];
81 const float mix = s->mix;
82
83 for (int y = slice_start; y < slice_end; y++) {
84 for (int x = 0; x < width; x++)
85 yptr[x] = lerpf(yv, yptr[x], mix);
86
87 yptr += ylinesize;
88 }
89
90 return 0;
91 }
92
93 static int colorize_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
94 {
95 ColorizeContext *s = ctx->priv;
96 AVFrame *frame = arg;
97 const int width = s->planewidth[1];
98 const int height = s->planeheight[1];
99 const int slice_start = (height * jobnr) / nb_jobs;
100 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
101 const ptrdiff_t ulinesize = frame->linesize[1];
102 const ptrdiff_t vlinesize = frame->linesize[2];
103 uint8_t *uptr = frame->data[1] + slice_start * ulinesize;
104 uint8_t *vptr = frame->data[2] + slice_start * vlinesize;
105 const int u = s->c[1];
106 const int v = s->c[2];
107
108 for (int y = slice_start; y < slice_end; y++) {
109 for (int x = 0; x < width; x++) {
110 uptr[x] = u;
111 vptr[x] = v;
112 }
113
114 uptr += ulinesize;
115 vptr += vlinesize;
116 }
117
118 return 0;
119 }
120
121 static int colorize_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
122 {
123 ColorizeContext *s = ctx->priv;
124 AVFrame *frame = arg;
125 const int width = s->planewidth[1];
126 const int height = s->planeheight[1];
127 const int slice_start = (height * jobnr) / nb_jobs;
128 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
129 const ptrdiff_t ulinesize = frame->linesize[1] / 2;
130 const ptrdiff_t vlinesize = frame->linesize[2] / 2;
131 uint16_t *uptr = (uint16_t *)frame->data[1] + slice_start * ulinesize;
132 uint16_t *vptr = (uint16_t *)frame->data[2] + slice_start * vlinesize;
133 const int u = s->c[1];
134 const int v = s->c[2];
135
136 for (int y = slice_start; y < slice_end; y++) {
137 for (int x = 0; x < width; x++) {
138 uptr[x] = u;
139 vptr[x] = v;
140 }
141
142 uptr += ulinesize;
143 vptr += vlinesize;
144 }
145
146 return 0;
147 }
148
149 static int do_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
150 {
151 ColorizeContext *s = ctx->priv;
152
153 s->do_plane_slice[0](ctx, arg, jobnr, nb_jobs);
154 s->do_plane_slice[1](ctx, arg, jobnr, nb_jobs);
155
156 return 0;
157 }
158
159 static float hue2rgb(float p, float q, float t)
160 {
161 if (t < 0.f) t += 1.f;
162 if (t > 1.f) t -= 1.f;
163 if (t < 1.f/6.f) return p + (q - p) * 6.f * t;
164 if (t < 1.f/2.f) return q;
165 if (t < 2.f/3.f) return p + (q - p) * (2.f/3.f - t) * 6.f;
166
167 return p;
168 }
169
170 static void hsl2rgb(float h, float s, float l, float *r, float *g, float *b)
171 {
172 h /= 360.f;
173
174 if (s == 0.f) {
175 *r = *g = *b = l;
176 } else {
177 const float q = l < 0.5f ? l * (1.f + s) : l + s - l * s;
178 const float p = 2.f * l - q;
179
180 *r = hue2rgb(p, q, h + 1.f / 3.f);
181 *g = hue2rgb(p, q, h);
182 *b = hue2rgb(p, q, h - 1.f / 3.f);
183 }
184 }
185
186 static void rgb2yuv(float r, float g, float b, int *y, int *u, int *v, int depth)
187 {
188 *y = ((0.21260*219.0/255.0) * r + (0.71520*219.0/255.0) * g +
189 (0.07220*219.0/255.0) * b) * ((1 << depth) - 1);
190 *u = (-(0.11457*224.0/255.0) * r - (0.38543*224.0/255.0) * g +
191 (0.50000*224.0/255.0) * b + 0.5) * ((1 << depth) - 1);
192 *v = ((0.50000*224.0/255.0) * r - (0.45415*224.0/255.0) * g -
193 (0.04585*224.0/255.0) * b + 0.5) * ((1 << depth) - 1);
194 }
195
196 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
197 {
198 AVFilterContext *ctx = inlink->dst;
199 ColorizeContext *s = ctx->priv;
200 float c[3];
201
202 hsl2rgb(s->hue, s->saturation, s->lightness, &c[0], &c[1], &c[2]);
203 rgb2yuv(c[0], c[1], c[2], &s->c[0], &s->c[1], &s->c[2], s->depth);
204
205 ff_filter_execute(ctx, do_slice, frame, NULL,
206 FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
207
208 return ff_filter_frame(ctx->outputs[0], frame);
209 }
210
211 static const enum AVPixelFormat pixel_fmts[] = {
212 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
213 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
214 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
215 AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
216 AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
217 AV_PIX_FMT_YUVJ411P,
218 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
219 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
220 AV_PIX_FMT_YUV440P10,
221 AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
222 AV_PIX_FMT_YUV440P12,
223 AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
224 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
225 AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
226 AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
227 AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
228 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
229 AV_PIX_FMT_NONE
230 };
231
232 static av_cold int config_input(AVFilterLink *inlink)
233 {
234 AVFilterContext *ctx = inlink->dst;
235 ColorizeContext *s = ctx->priv;
236 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
237 int depth;
238
239 s->depth = depth = desc->comp[0].depth;
240
241 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
242 s->planewidth[0] = s->planewidth[3] = inlink->w;
243 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
244 s->planeheight[0] = s->planeheight[3] = inlink->h;
245
246 s->do_plane_slice[0] = depth <= 8 ? colorizey_slice8 : colorizey_slice16;
247 s->do_plane_slice[1] = depth <= 8 ? colorize_slice8 : colorize_slice16;
248
249 return 0;
250 }
251
252 static const AVFilterPad colorize_inputs[] = {
253 {
254 .name = "default",
255 .type = AVMEDIA_TYPE_VIDEO,
256 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
257 .filter_frame = filter_frame,
258 .config_props = config_input,
259 },
260 };
261
262 #define OFFSET(x) offsetof(ColorizeContext, x)
263 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
264
265 static const AVOption colorize_options[] = {
266 { "hue", "set the hue", OFFSET(hue), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 360, VF },
267 { "saturation", "set the saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl=0.5},0, 1, VF },
268 { "lightness", "set the lightness", OFFSET(lightness), AV_OPT_TYPE_FLOAT, {.dbl=0.5},0, 1, VF },
269 { "mix", "set the mix of source lightness", OFFSET(mix), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, VF },
270 { NULL }
271 };
272
273 AVFILTER_DEFINE_CLASS(colorize);
274
275 const AVFilter ff_vf_colorize = {
276 .name = "colorize",
277 .description = NULL_IF_CONFIG_SMALL("Overlay a solid color on the video stream."),
278 .priv_size = sizeof(ColorizeContext),
279 .priv_class = &colorize_class,
280 FILTER_INPUTS(colorize_inputs),
281 FILTER_OUTPUTS(ff_video_default_filterpad),
282 FILTER_PIXFMTS_ARRAY(pixel_fmts),
283 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
284 .process_command = ff_filter_process_command,
285 };
286