FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_elbg.c
Date: 2024-07-16 12:46:59
Exec Total Coverage
Lines: 0 98 0.0%
Functions: 0 5 0.0%
Branches: 0 44 0.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2013 Stefano Sabatini
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 /**
22 * @file
23 * video quantizer filter based on ELBG
24 */
25
26 #include "libavcodec/elbg.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "libavutil/random_seed.h"
31
32 #include "avfilter.h"
33 #include "drawutils.h"
34 #include "formats.h"
35 #include "internal.h"
36 #include "video.h"
37
38 typedef struct ELBGFilterContext {
39 const AVClass *class;
40 struct ELBGContext *ctx;
41 AVLFG lfg;
42 int64_t lfg_seed;
43 int max_steps_nb;
44 int *codeword;
45 int codeword_length;
46 int *codeword_closest_codebook_idxs;
47 int *codebook;
48 int codebook_length;
49 const AVPixFmtDescriptor *pix_desc;
50 uint8_t rgba_map[4];
51 int use_alpha;
52 int pal8;
53 } ELBGFilterContext;
54
55 #define OFFSET(x) offsetof(ELBGFilterContext, x)
56 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57
58 static const AVOption elbg_options[] = {
59 { "codebook_length", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
60 { "l", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
61 { "nb_steps", "set max number of steps used to compute the mapping", OFFSET(max_steps_nb), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
62 { "n", "set max number of steps used to compute the mapping", OFFSET(max_steps_nb), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, INT_MAX, FLAGS },
63 { "seed", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
64 { "s", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
65 { "pal8", "set the pal8 output", OFFSET(pal8), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
66 { "use_alpha", "use alpha channel for mapping", OFFSET(use_alpha), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
67 { NULL }
68 };
69
70 AVFILTER_DEFINE_CLASS(elbg);
71
72 static av_cold int init(AVFilterContext *ctx)
73 {
74 ELBGFilterContext *const elbg = ctx->priv;
75
76 if (elbg->pal8 && elbg->codebook_length > 256) {
77 av_log(ctx, AV_LOG_ERROR, "pal8 output allows max 256 codebook length.\n");
78 return AVERROR(EINVAL);
79 }
80
81 if (elbg->lfg_seed == -1)
82 elbg->lfg_seed = av_get_random_seed();
83
84 av_lfg_init(&elbg->lfg, elbg->lfg_seed);
85 return 0;
86 }
87
88 static int query_formats(AVFilterContext *ctx)
89 {
90 ELBGFilterContext *const elbg = ctx->priv;
91 int ret;
92
93 static const enum AVPixelFormat pix_fmts[] = {
94 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
95 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
96 AV_PIX_FMT_NONE
97 };
98 if (!elbg->pal8) {
99 return ff_set_common_formats_from_list(ctx, pix_fmts);
100 } else {
101 static const enum AVPixelFormat pal8_fmt[] = {
102 AV_PIX_FMT_PAL8,
103 AV_PIX_FMT_NONE
104 };
105 if ((ret = ff_formats_ref(ff_make_format_list(pix_fmts), &ctx->inputs[0]->outcfg.formats)) < 0 ||
106 (ret = ff_formats_ref(ff_make_format_list(pal8_fmt), &ctx->outputs[0]->incfg.formats)) < 0)
107 return ret;
108 }
109 return 0;
110 }
111
112 #define NB_COMPONENTS 4
113
114 static int config_input(AVFilterLink *inlink)
115 {
116 AVFilterContext *ctx = inlink->dst;
117 ELBGFilterContext *const elbg = ctx->priv;
118
119 elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
120 elbg->codeword_length = inlink->w * inlink->h;
121 elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
122 NB_COMPONENTS * sizeof(*elbg->codeword));
123 if (!elbg->codeword)
124 return AVERROR(ENOMEM);
125
126 elbg->codeword_closest_codebook_idxs =
127 av_realloc_f(elbg->codeword_closest_codebook_idxs, elbg->codeword_length,
128 sizeof(*elbg->codeword_closest_codebook_idxs));
129 if (!elbg->codeword_closest_codebook_idxs)
130 return AVERROR(ENOMEM);
131
132 elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
133 NB_COMPONENTS * sizeof(*elbg->codebook));
134 if (!elbg->codebook)
135 return AVERROR(ENOMEM);
136
137 ff_fill_rgba_map(elbg->rgba_map, inlink->format);
138
139 return 0;
140 }
141
142 #define R 0
143 #define G 1
144 #define B 2
145 #define A 3
146
147 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
148 {
149 ELBGFilterContext *const elbg = inlink->dst->priv;
150 int i, j, k, ret;
151 uint8_t *p, *p0;
152
153 const uint8_t r_idx = elbg->rgba_map[R];
154 const uint8_t g_idx = elbg->rgba_map[G];
155 const uint8_t b_idx = elbg->rgba_map[B];
156 const uint8_t a_idx = elbg->rgba_map[A];
157
158 /* build the codeword */
159 p0 = frame->data[0];
160 k = 0;
161 for (i = 0; i < inlink->h; i++) {
162 p = p0;
163 for (j = 0; j < inlink->w; j++) {
164 elbg->codeword[k++] = p[b_idx];
165 elbg->codeword[k++] = p[g_idx];
166 elbg->codeword[k++] = p[r_idx];
167 elbg->codeword[k++] = elbg->use_alpha ? p[a_idx] : 0xff;
168 p += elbg->pix_desc->nb_components;
169 }
170 p0 += frame->linesize[0];
171 }
172
173 /* compute the codebook */
174 ret = avpriv_elbg_do(&elbg->ctx, elbg->codeword, NB_COMPONENTS,
175 elbg->codeword_length, elbg->codebook,
176 elbg->codebook_length, elbg->max_steps_nb,
177 elbg->codeword_closest_codebook_idxs, &elbg->lfg, 0);
178 if (ret < 0) {
179 av_frame_free(&frame);
180 return ret;
181 }
182
183 if (elbg->pal8) {
184 AVFilterLink *outlink = inlink->dst->outputs[0];
185 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
186 uint32_t *pal;
187
188 if (!out) {
189 av_frame_free(&frame);
190 return AVERROR(ENOMEM);
191 }
192 av_frame_copy_props(out, frame);
193 av_frame_free(&frame);
194 pal = (uint32_t *)out->data[1];
195 p0 = (uint8_t *)out->data[0];
196
197 for (i = 0; i < elbg->codebook_length; i++) {
198 const int al = elbg->use_alpha ? elbg->codebook[i*4+3] : 0xff;
199 pal[i] = al << 24 |
200 (elbg->codebook[i*4+2] << 16) |
201 (elbg->codebook[i*4+1] << 8) |
202 elbg->codebook[i*4 ];
203 }
204
205 k = 0;
206 for (i = 0; i < inlink->h; i++) {
207 p = p0;
208 for (j = 0; j < inlink->w; j++, p++) {
209 p[0] = elbg->codeword_closest_codebook_idxs[k++];
210 }
211 p0 += out->linesize[0];
212 }
213
214 return ff_filter_frame(outlink, out);
215 }
216
217 /* fill the output with the codebook values */
218 p0 = frame->data[0];
219
220 k = 0;
221 for (i = 0; i < inlink->h; i++) {
222 p = p0;
223 for (j = 0; j < inlink->w; j++) {
224 int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
225 p[b_idx] = elbg->codebook[cb_idx];
226 p[g_idx] = elbg->codebook[cb_idx+1];
227 p[r_idx] = elbg->codebook[cb_idx+2];
228 p[a_idx] = elbg->use_alpha ? elbg->codebook[cb_idx+3] : 0xFFu;
229 p += elbg->pix_desc->nb_components;
230 }
231 p0 += frame->linesize[0];
232 }
233
234 return ff_filter_frame(inlink->dst->outputs[0], frame);
235 }
236
237 static av_cold void uninit(AVFilterContext *ctx)
238 {
239 ELBGFilterContext *const elbg = ctx->priv;
240
241 avpriv_elbg_free(&elbg->ctx);
242
243 av_freep(&elbg->codebook);
244 av_freep(&elbg->codeword);
245 av_freep(&elbg->codeword_closest_codebook_idxs);
246 }
247
248 static const AVFilterPad elbg_inputs[] = {
249 {
250 .name = "default",
251 .type = AVMEDIA_TYPE_VIDEO,
252 .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
253 .config_props = config_input,
254 .filter_frame = filter_frame,
255 },
256 };
257
258 const AVFilter ff_vf_elbg = {
259 .name = "elbg",
260 .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
261 .priv_size = sizeof(ELBGFilterContext),
262 .priv_class = &elbg_class,
263 .init = init,
264 .uninit = uninit,
265 FILTER_INPUTS(elbg_inputs),
266 FILTER_OUTPUTS(ff_video_default_filterpad),
267 FILTER_QUERY_FUNC(query_formats),
268 };
269