FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 ColorContext {
37  const AVClass *class;
39  unsigned int lfg_seed;
41  int *codeword;
44  int *codebook;
47  uint8_t rgba_map[4];
48 } ELBGContext;
49 
50 #define OFFSET(x) offsetof(ELBGContext, x)
51 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
52 
53 static const AVOption elbg_options[] = {
54  { "codebook_length", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
55  { "l", "set codebook length", OFFSET(codebook_length), AV_OPT_TYPE_INT, { .i64 = 256 }, 1, INT_MAX, FLAGS },
56  { "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 },
57  { "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 },
58  { "seed", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
59  { "s", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
60  { NULL }
61 };
62 
64 
65 static av_cold int init(AVFilterContext *ctx)
66 {
67  ELBGContext *elbg = ctx->priv;
68 
69  if (elbg->lfg_seed == -1)
70  elbg->lfg_seed = av_get_random_seed();
71 
72  av_lfg_init(&elbg->lfg, elbg->lfg_seed);
73  return 0;
74 }
75 
77 {
78  static const enum AVPixelFormat pix_fmts[] = {
82  };
83  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
84  if (!fmts_list)
85  return AVERROR(ENOMEM);
86  return ff_set_common_formats(ctx, fmts_list);
87 }
88 
89 #define NB_COMPONENTS 3
90 
91 static int config_input(AVFilterLink *inlink)
92 {
93  AVFilterContext *ctx = inlink->dst;
94  ELBGContext *elbg = ctx->priv;
95 
96  elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
97  elbg->codeword_length = inlink->w * inlink->h;
98  elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
99  NB_COMPONENTS * sizeof(*elbg->codeword));
100  if (!elbg->codeword)
101  return AVERROR(ENOMEM);
102 
105  sizeof(*elbg->codeword_closest_codebook_idxs));
107  return AVERROR(ENOMEM);
108 
109  elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
110  NB_COMPONENTS * sizeof(*elbg->codebook));
111  if (!elbg->codebook)
112  return AVERROR(ENOMEM);
113 
114  ff_fill_rgba_map(elbg->rgba_map, inlink->format);
115 
116  return 0;
117 }
118 
119 #define R 0
120 #define G 1
121 #define B 2
122 
123 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
124 {
125  ELBGContext *elbg = inlink->dst->priv;
126  int i, j, k;
127  uint8_t *p, *p0;
128 
129  const uint8_t r_idx = elbg->rgba_map[R];
130  const uint8_t g_idx = elbg->rgba_map[G];
131  const uint8_t b_idx = elbg->rgba_map[B];
132 
133  /* build the codeword */
134  p0 = frame->data[0];
135  k = 0;
136  for (i = 0; i < inlink->h; i++) {
137  p = p0;
138  for (j = 0; j < inlink->w; j++) {
139  elbg->codeword[k++] = p[r_idx];
140  elbg->codeword[k++] = p[g_idx];
141  elbg->codeword[k++] = p[b_idx];
142  p += elbg->pix_desc->nb_components;
143  }
144  p0 += frame->linesize[0];
145  }
146 
147  /* compute the codebook */
149  elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
150  elbg->codeword_closest_codebook_idxs, &elbg->lfg);
152  elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
153  elbg->codeword_closest_codebook_idxs, &elbg->lfg);
154 
155  /* fill the output with the codebook values */
156  p0 = frame->data[0];
157 
158  k = 0;
159  for (i = 0; i < inlink->h; i++) {
160  p = p0;
161  for (j = 0; j < inlink->w; j++) {
162  int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
163  p[r_idx] = elbg->codebook[cb_idx];
164  p[g_idx] = elbg->codebook[cb_idx+1];
165  p[b_idx] = elbg->codebook[cb_idx+2];
166  p += elbg->pix_desc->nb_components;
167  }
168  p0 += frame->linesize[0];
169  }
170 
171  return ff_filter_frame(inlink->dst->outputs[0], frame);
172 }
173 
174 static av_cold void uninit(AVFilterContext *ctx)
175 {
176  ELBGContext *elbg = ctx->priv;
177 
178  av_freep(&elbg->codebook);
179  av_freep(&elbg->codeword);
181 }
182 
183 static const AVFilterPad elbg_inputs[] = {
184  {
185  .name = "default",
186  .type = AVMEDIA_TYPE_VIDEO,
187  .config_props = config_input,
188  .filter_frame = filter_frame,
189  .needs_writable = 1,
190  },
191  { NULL }
192 };
193 
194 static const AVFilterPad elbg_outputs[] = {
195  {
196  .name = "default",
197  .type = AVMEDIA_TYPE_VIDEO,
198  },
199  { NULL }
200 };
201 
203  .name = "elbg",
204  .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
205  .priv_size = sizeof(ELBGContext),
206  .priv_class = &elbg_class,
208  .init = init,
209  .uninit = uninit,
210  .inputs = elbg_inputs,
211  .outputs = elbg_outputs,
212 };
Definition: lfg.h:25
#define NULL
Definition: coverity.c:32
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_elbg.c:174
AVFilter ff_vf_elbg
Definition: vf_elbg.c:202
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2090
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
#define av_realloc_f(p, o, n)
AVOption.
Definition: opt.h:255
#define OFFSET(x)
Definition: vf_elbg.c:50
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
static int config_input(AVFilterLink *inlink)
Definition: vf_elbg.c:91
#define B
Definition: vf_elbg.c:121
int * codebook
Definition: vf_elbg.c:44
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
static const AVFilterPad elbg_inputs[]
Definition: vf_elbg.c:183
const char * name
Pad name.
Definition: internal.h:67
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
unsigned int lfg_seed
Definition: vf_elbg.c:39
int avpriv_do_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Implementation of the Enhanced LBG Algorithm Based on the paper "Neural Networks 14:1219-1237" that c...
Definition: elbg.c:371
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:96
static AVFrame * frame
AVLFG lfg
Definition: vf_elbg.c:38
A filter pad used for either input or output.
Definition: internal.h:61
int max_steps_nb
Definition: vf_elbg.c:40
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:542
static int query_formats(AVFilterContext *ctx)
Definition: vf_elbg.c:76
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:97
void * priv
private data for use by the filter
Definition: avfilter.h:654
#define FLAGS
Definition: vf_elbg.c:51
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:94
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:95
static const AVOption elbg_options[]
Definition: vf_elbg.c:53
int * codeword
Definition: vf_elbg.c:41
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:66
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:33
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_elbg.c:123
static const AVFilterPad elbg_outputs[]
Definition: vf_elbg.c:194
static av_cold int init(AVFilterContext *ctx)
Definition: vf_elbg.c:65
uint8_t rgba_map[4]
Definition: vf_elbg.c:47
int avpriv_init_elbg(int *points, int dim, int numpoints, int *codebook, int numCB, int max_steps, int *closest_cb, AVLFG *rand_state)
Initialize the **codebook vector for the elbg algorithm.
Definition: elbg.c:337
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
Describe the class of an AVClass context structure.
Definition: log.h:67
const AVPixFmtDescriptor * pix_desc
Definition: vf_elbg.c:46
Filter definition.
Definition: avfilter.h:470
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
const char * name
Filter name.
Definition: avfilter.h:474
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition: lfg.c:30
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
#define G
Definition: vf_elbg.c:120
#define NB_COMPONENTS
Definition: vf_elbg.c:89
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
int codeword_length
Definition: vf_elbg.c:42
int codebook_length
Definition: vf_elbg.c:45
AVFILTER_DEFINE_CLASS(elbg)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:633
#define av_freep(p)
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:109
internal API functions
#define R
Definition: vf_elbg.c:119
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int * codeword_closest_codebook_idxs
Definition: vf_elbg.c:43