2 * Copyright (c) 2013 Stefano Sabatini
4 * This file is part of FFmpeg.
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.
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.
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
23 * video quantizer filter based on ELBG
26 #include "libavcodec/elbg.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/random_seed.h"
32 #include "drawutils.h"
36 typedef struct ELBGContext {
43 int *codeword_closest_codebook_idxs;
46 const AVPixFmtDescriptor *pix_desc;
51 #define OFFSET(x) offsetof(ELBGContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
54 static const AVOption elbg_options[] = {
55 { "codebook_length", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
56 { "l", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
57 { "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 },
58 { "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 },
59 { "seed", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
60 { "s", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
61 { "pal8", "set the pal8 output", OFFSET(pal8), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
65 AVFILTER_DEFINE_CLASS(elbg);
67 static av_cold int init(AVFilterContext *ctx)
69 ELBGContext *elbg = ctx->priv;
71 if (elbg->pal8 && elbg->codebook_length > 256) {
72 av_log(ctx, AV_LOG_ERROR, "pal8 output allows max 256 codebook length.\n");
73 return AVERROR(EINVAL);
76 if (elbg->lfg_seed == -1)
77 elbg->lfg_seed = av_get_random_seed();
79 av_lfg_init(&elbg->lfg, elbg->lfg_seed);
83 static int query_formats(AVFilterContext *ctx)
85 ELBGContext *elbg = ctx->priv;
88 static const enum AVPixelFormat pix_fmts[] = {
89 AV_PIX_FMT_ARGB, AV_PIX_FMT_RGBA, AV_PIX_FMT_ABGR, AV_PIX_FMT_BGRA,
90 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
94 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
96 return AVERROR(ENOMEM);
97 return ff_set_common_formats(ctx, fmts_list);
99 static const enum AVPixelFormat pal8_fmt[] = {
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)
110 #define NB_COMPONENTS 3
112 static int config_input(AVFilterLink *inlink)
114 AVFilterContext *ctx = inlink->dst;
115 ELBGContext *elbg = ctx->priv;
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));
122 return AVERROR(ENOMEM);
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);
130 elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
131 NB_COMPONENTS * sizeof(*elbg->codebook));
133 return AVERROR(ENOMEM);
135 ff_fill_rgba_map(elbg->rgba_map, inlink->format);
144 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
146 ELBGContext *elbg = inlink->dst->priv;
150 const uint8_t r_idx = elbg->rgba_map[R];
151 const uint8_t g_idx = elbg->rgba_map[G];
152 const uint8_t b_idx = elbg->rgba_map[B];
154 /* build the codeword */
157 for (i = 0; i < inlink->h; i++) {
159 for (j = 0; j < inlink->w; j++) {
160 elbg->codeword[k++] = p[r_idx];
161 elbg->codeword[k++] = p[g_idx];
162 elbg->codeword[k++] = p[b_idx];
163 p += elbg->pix_desc->nb_components;
165 p0 += frame->linesize[0];
168 /* compute the codebook */
169 avpriv_init_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
170 elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
171 elbg->codeword_closest_codebook_idxs, &elbg->lfg);
172 avpriv_do_elbg(elbg->codeword, NB_COMPONENTS, elbg->codeword_length,
173 elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
174 elbg->codeword_closest_codebook_idxs, &elbg->lfg);
177 AVFilterLink *outlink = inlink->dst->outputs[0];
178 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
182 av_frame_free(&frame);
183 return AVERROR(ENOMEM);
185 out->pts = frame->pts;
186 av_frame_free(&frame);
187 pal = (uint32_t *)out->data[1];
188 p0 = (uint8_t *)out->data[0];
190 for (i = 0; i < elbg->codebook_length; i++) {
191 pal[i] = 0xFFU << 24 |
192 (elbg->codebook[i*3 ] << 16) |
193 (elbg->codebook[i*3+1] << 8) |
194 elbg->codebook[i*3+2];
198 for (i = 0; i < inlink->h; i++) {
200 for (j = 0; j < inlink->w; j++, p++) {
201 p[0] = elbg->codeword_closest_codebook_idxs[k++];
203 p0 += out->linesize[0];
206 return ff_filter_frame(outlink, out);
209 /* fill the output with the codebook values */
213 for (i = 0; i < inlink->h; i++) {
215 for (j = 0; j < inlink->w; j++) {
216 int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
217 p[r_idx] = elbg->codebook[cb_idx];
218 p[g_idx] = elbg->codebook[cb_idx+1];
219 p[b_idx] = elbg->codebook[cb_idx+2];
220 p += elbg->pix_desc->nb_components;
222 p0 += frame->linesize[0];
225 return ff_filter_frame(inlink->dst->outputs[0], frame);
228 static av_cold void uninit(AVFilterContext *ctx)
230 ELBGContext *elbg = ctx->priv;
232 av_freep(&elbg->codebook);
233 av_freep(&elbg->codeword);
234 av_freep(&elbg->codeword_closest_codebook_idxs);
237 static const AVFilterPad elbg_inputs[] = {
240 .type = AVMEDIA_TYPE_VIDEO,
241 .config_props = config_input,
242 .filter_frame = filter_frame,
248 static const AVFilterPad elbg_outputs[] = {
251 .type = AVMEDIA_TYPE_VIDEO,
256 AVFilter ff_vf_elbg = {
258 .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
259 .priv_size = sizeof(ELBGContext),
260 .priv_class = &elbg_class,
261 .query_formats = query_formats,
264 .inputs = elbg_inputs,
265 .outputs = elbg_outputs,