FFmpeg
Loading...
Searching...
No Matches
vf_elbg.c
Go to the documentation of this file.
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"
31
32#include "avfilter.h"
33#include "drawutils.h"
34#include "filters.h"
35#include "formats.h"
36#include "video.h"
37
54
55#define OFFSET(x) offsetof(ELBGFilterContext, x)
56#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
57
58static 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
71
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)
83
84 av_lfg_init(&elbg->lfg, elbg->lfg_seed);
85 return 0;
86}
87
89 AVFilterFormatsConfig **cfg_in,
90 AVFilterFormatsConfig **cfg_out)
91{
92 const ELBGFilterContext *const elbg = ctx->priv;
93 int ret;
94
95 static const enum AVPixelFormat pix_fmts[] = {
99 };
100 if (!elbg->pal8) {
101 return ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
102 } else {
103 static const enum AVPixelFormat pal8_fmt[] = {
106 };
107 if ((ret = ff_formats_ref(ff_make_pixel_format_list(pix_fmts), &cfg_in[0]->formats)) < 0 ||
108 (ret = ff_formats_ref(ff_make_pixel_format_list(pal8_fmt), &cfg_out[0]->formats)) < 0)
109 return ret;
110 }
111 return 0;
112}
113
114#define NB_COMPONENTS 4
115
116static int config_input(AVFilterLink *inlink)
117{
118 AVFilterContext *ctx = inlink->dst;
119 ELBGFilterContext *const elbg = ctx->priv;
120
121 elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
122 elbg->codeword_length = inlink->w * inlink->h;
123 elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
124 NB_COMPONENTS * sizeof(*elbg->codeword));
125 if (!elbg->codeword)
126 return AVERROR(ENOMEM);
127
130 sizeof(*elbg->codeword_closest_codebook_idxs));
132 return AVERROR(ENOMEM);
133
134 elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
135 NB_COMPONENTS * sizeof(*elbg->codebook));
136 if (!elbg->codebook)
137 return AVERROR(ENOMEM);
138
139 ff_fill_rgba_map(elbg->rgba_map, inlink->format);
140
141 return 0;
142}
143
144#define R 0
145#define G 1
146#define B 2
147#define A 3
148
150{
151 ELBGFilterContext *const elbg = inlink->dst->priv;
152 int i, j, k, ret;
153 uint8_t *p, *p0;
154
155 const uint8_t r_idx = elbg->rgba_map[R];
156 const uint8_t g_idx = elbg->rgba_map[G];
157 const uint8_t b_idx = elbg->rgba_map[B];
158 const uint8_t a_idx = elbg->rgba_map[A];
159
160 /* build the codeword */
161 p0 = frame->data[0];
162 k = 0;
163 for (i = 0; i < inlink->h; i++) {
164 p = p0;
165 for (j = 0; j < inlink->w; j++) {
166 elbg->codeword[k++] = p[b_idx];
167 elbg->codeword[k++] = p[g_idx];
168 elbg->codeword[k++] = p[r_idx];
169 elbg->codeword[k++] = elbg->use_alpha ? p[a_idx] : 0xff;
170 p += elbg->pix_desc->nb_components;
171 }
172 p0 += frame->linesize[0];
173 }
174
175 /* compute the codebook */
176 ret = avpriv_elbg_do(&elbg->ctx, elbg->codeword, NB_COMPONENTS,
177 elbg->codeword_length, elbg->codebook,
178 elbg->codebook_length, elbg->max_steps_nb,
179 elbg->codeword_closest_codebook_idxs, &elbg->lfg, 0);
180 if (ret < 0) {
182 return ret;
183 }
184
185 if (elbg->pal8) {
186 AVFilterLink *outlink = inlink->dst->outputs[0];
187 AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
188 uint32_t *pal;
189
190 if (!out) {
192 return AVERROR(ENOMEM);
193 }
196 pal = (uint32_t *)out->data[1];
197 p0 = (uint8_t *)out->data[0];
198
199 for (i = 0; i < elbg->codebook_length; i++) {
200 const unsigned al = elbg->use_alpha ? elbg->codebook[i*4+3] : 0xff;
201 pal[i] = al << 24 |
202 (elbg->codebook[i*4+2] << 16) |
203 (elbg->codebook[i*4+1] << 8) |
204 elbg->codebook[i*4 ];
205 }
206
207 k = 0;
208 for (i = 0; i < inlink->h; i++) {
209 p = p0;
210 for (j = 0; j < inlink->w; j++, p++) {
211 p[0] = elbg->codeword_closest_codebook_idxs[k++];
212 }
213 p0 += out->linesize[0];
214 }
215
216 return ff_filter_frame(outlink, out);
217 }
218
219 /* fill the output with the codebook values */
220 p0 = frame->data[0];
221
222 k = 0;
223 for (i = 0; i < inlink->h; i++) {
224 p = p0;
225 for (j = 0; j < inlink->w; j++) {
226 int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
227 p[b_idx] = elbg->codebook[cb_idx];
228 p[g_idx] = elbg->codebook[cb_idx+1];
229 p[r_idx] = elbg->codebook[cb_idx+2];
230 p[a_idx] = elbg->use_alpha ? elbg->codebook[cb_idx+3] : 0xFFu;
231 p += elbg->pix_desc->nb_components;
232 }
233 p0 += frame->linesize[0];
234 }
235
236 return ff_filter_frame(inlink->dst->outputs[0], frame);
237}
238
240{
241 ELBGFilterContext *const elbg = ctx->priv;
242
243 avpriv_elbg_free(&elbg->ctx);
244
245 av_freep(&elbg->codebook);
246 av_freep(&elbg->codeword);
248}
249
250static const AVFilterPad elbg_inputs[] = {
251 {
252 .name = "default",
253 .type = AVMEDIA_TYPE_VIDEO,
255 .config_props = config_input,
256 .filter_frame = filter_frame,
257 },
258};
259
261 .p.name = "elbg",
262 .p.description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
263 .p.priv_class = &elbg_class,
264 .priv_size = sizeof(ELBGFilterContext),
265 .init = init,
266 .uninit = uninit,
270};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
static int config_input(AVFilterLink *inlink)
const FFFilter ff_vf_elbg
Definition vf_elbg.c:260
#define A(x)
Definition vpx_arith.h:28
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition drawutils.c:80
misc drawing utilities
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int avpriv_elbg_do(ELBGContext **elbgp, int *points, int dim, int numpoints, int *codebook, int num_cb, int max_steps, int *closest_cb, AVLFG *rand_state, uintptr_t flags)
Implementation of the Enhanced LBG Algorithm Based on the paper "Neural Networks 14:1219-1237" that c...
Definition elbg.c:463
av_cold void avpriv_elbg_free(ELBGContext **elbgp)
Free an ELBGContext and reset the pointer to it.
Definition elbg.c:516
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add ref as a new reference to formats.
Definition formats.c:756
int ff_set_pixel_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVPixelFormat *fmts)
Definition formats.c:1162
av_warn_unused_result AVFilterFormats * ff_make_pixel_format_list(const enum AVPixelFormat *fmts)
Create a list of supported pixel formats.
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define B
Definition huffyuv.h:42
#define R
Definition huffyuv.h:44
#define G
Definition huffyuv.h:43
static av_cold void uninit(AVBitStreamFilterContext *ctx)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition filters.h:59
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
Memory handling functions.
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition pixfmt.h:99
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition pixfmt.h:101
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition pixfmt.h:100
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
formats
Definition signature.h:47
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition pixdesc.h:71
ELBG internal data.
Definition elbg.c:47
struct ELBGContext * ctx
Definition vf_elbg.c:40
int * codeword_closest_codebook_idxs
Definition vf_elbg.c:46
uint8_t rgba_map[4]
Definition vf_elbg.c:50
int64_t lfg_seed
Definition vf_elbg.c:42
const AVPixFmtDescriptor * pix_desc
Definition vf_elbg.c:49
#define av_realloc_f(p, o, n)
#define av_freep(p)
#define av_log(a,...)
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
static int config_input(AVFilterLink *inlink)
Definition vf_elbg.c:116
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition vf_elbg.c:149
static const AVOption elbg_options[]
Definition vf_elbg.c:58
#define NB_COMPONENTS
Definition vf_elbg.c:114
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_elbg.c:88
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_elbg.c:239
#define OFFSET(x)
Definition vf_elbg.c:55
static const AVFilterPad elbg_inputs[]
Definition vf_elbg.c:250
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89