FFmpeg coverage


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