FFmpeg
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/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;
40  int64_t lfg_seed;
42  int *codeword;
45  int *codebook;
48  uint8_t rgba_map[4];
49  int use_alpha;
50  int pal8;
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 
69 
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 
87 {
88  ELBGFilterContext *const elbg = ctx->priv;
89  int ret;
90 
91  static const enum AVPixelFormat pix_fmts[] = {
95  };
96  if (!elbg->pal8) {
98  } else {
99  static const enum AVPixelFormat pal8_fmt[] = {
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 
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 
126  sizeof(*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 
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) {
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) {
188  return AVERROR(ENOMEM);
189  }
190  out->pts = frame->pts;
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 
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);
244 }
245 
246 static const AVFilterPad elbg_inputs[] = {
247  {
248  .name = "default",
249  .type = AVMEDIA_TYPE_VIDEO,
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 
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,
273 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:101
OFFSET
#define OFFSET(x)
Definition: vf_elbg.c:53
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
ELBGFilterContext::codeword_closest_codebook_idxs
int * codeword_closest_codebook_idxs
Definition: vf_elbg.c:44
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:380
out
FILE * out
Definition: movenc.c:54
av_lfg_init
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:32
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:999
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2662
ELBGFilterContext::lfg_seed
int64_t lfg_seed
Definition: vf_elbg.c:40
ELBGFilterContext::lfg
AVLFG lfg
Definition: vf_elbg.c:39
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
A
#define A
Definition: vf_elbg.c:143
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
pixdesc.h
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:167
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
G
#define G
Definition: vf_elbg.c:141
ELBGFilterContext::codeword_length
int codeword_length
Definition: vf_elbg.c:43
ELBGFilterContext::pix_desc
const AVPixFmtDescriptor * pix_desc
Definition: vf_elbg.c:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:175
video.h
ELBGFilterContext::rgba_map
uint8_t rgba_map[4]
Definition: vf_elbg.c:48
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:120
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_elbg.c:86
elbg_inputs
static const AVFilterPad elbg_inputs[]
Definition: vf_elbg.c:246
ELBGFilterContext::use_alpha
int use_alpha
Definition: vf_elbg.c:49
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
avpriv_elbg_do
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:446
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_vf_elbg
const AVFilter ff_vf_elbg
Definition: vf_elbg.c:263
ELBGFilterContext::codebook
int * codebook
Definition: vf_elbg.c:45
elbg_outputs
static const AVFilterPad elbg_outputs[]
Definition: vf_elbg.c:256
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:596
ff_set_common_formats_from_list
int ff_set_common_formats_from_list(AVFilterContext *ctx, const int *fmts)
Equivalent to ff_set_common_formats(ctx, ff_make_format_list(fmts))
Definition: formats.c:755
AV_OPT_TYPE_INT64
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:190
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_elbg.c:70
elbg.h
av_realloc_f
#define av_realloc_f(p, o, n)
Definition: tableprint_vlc.h:32
ELBGFilterContext::ctx
struct ELBGContext * ctx
Definition: vf_elbg.c:38
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVPixFmtDescriptor::nb_components
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
AV_PIX_FMT_ABGR
@ AV_PIX_FMT_ABGR
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:94
AVLFG
Context structure for the Lagged Fibonacci PRNG.
Definition: lfg.h:33
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
avpriv_elbg_free
av_cold void avpriv_elbg_free(ELBGContext **elbgp)
Free an ELBGContext and reset the pointer to it.
Definition: elbg.c:499
ELBGFilterContext::max_steps_nb
int max_steps_nb
Definition: vf_elbg.c:41
B
#define B
Definition: vf_elbg.c:142
NB_COMPONENTS
#define NB_COMPONENTS
Definition: vf_elbg.c:110
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_elbg.c:145
elbg_options
static const AVOption elbg_options[]
Definition: vf_elbg.c:56
internal.h
AV_PIX_FMT_ARGB
@ AV_PIX_FMT_ARGB
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:92
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
ELBGContext
ELBG internal data.
Definition: elbg.c:46
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
AVFilter
Filter definition.
Definition: avfilter.h:171
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ELBGFilterContext
Definition: vf_elbg.c:36
ret
ret
Definition: filter_design.txt:187
R
#define R
Definition: vf_elbg.c:140
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FLAGS
#define FLAGS
Definition: vf_elbg.c:54
ELBGFilterContext::codebook_length
int codebook_length
Definition: vf_elbg.c:46
random_seed.h
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:408
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_elbg.c:235
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(elbg)
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_elbg.c:112
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:191
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
ff_fill_rgba_map
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
ELBGFilterContext::codeword
int * codeword
Definition: vf_elbg.c:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ELBGFilterContext::pal8
int pal8
Definition: vf_elbg.c:50
drawutils.h
AVFILTERPAD_FLAG_NEEDS_WRITABLE
#define AVFILTERPAD_FLAG_NEEDS_WRITABLE
The filter expects writable frames from its input link, duplicating data buffers if needed.
Definition: internal.h:68