FFmpeg coverage


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