FFmpeg coverage


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