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 ELBGContext {
37  const AVClass *class;
39  unsigned int lfg_seed;
41  int *codeword;
44  int *codebook;
48  int pal8;
49 } ELBGContext;
50 
51 #define OFFSET(x) offsetof(ELBGContext, x)
52 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
53 
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_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
60  { "s", "set the random seed", OFFSET(lfg_seed), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, UINT32_MAX, FLAGS },
61  { "pal8", "set the pal8 output", OFFSET(pal8), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS },
62  { NULL }
63 };
64 
66 
67 static av_cold int init(AVFilterContext *ctx)
68 {
69  ELBGContext *elbg = ctx->priv;
70 
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);
74  }
75 
76  if (elbg->lfg_seed == -1)
77  elbg->lfg_seed = av_get_random_seed();
78 
79  av_lfg_init(&elbg->lfg, elbg->lfg_seed);
80  return 0;
81 }
82 
84 {
85  ELBGContext *elbg = ctx->priv;
86 
87  static const enum AVPixelFormat pix_fmts[] = {
91  };
92  if (!elbg->pal8) {
93  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
94  if (!fmts_list)
95  return AVERROR(ENOMEM);
96  return ff_set_common_formats(ctx, fmts_list);
97  } else {
98  static const enum AVPixelFormat pal8_fmt[] = {
101  };
104  }
105  return 0;
106 }
107 
108 #define NB_COMPONENTS 3
109 
110 static int config_input(AVFilterLink *inlink)
111 {
112  AVFilterContext *ctx = inlink->dst;
113  ELBGContext *elbg = ctx->priv;
114 
115  elbg->pix_desc = av_pix_fmt_desc_get(inlink->format);
116  elbg->codeword_length = inlink->w * inlink->h;
117  elbg->codeword = av_realloc_f(elbg->codeword, elbg->codeword_length,
118  NB_COMPONENTS * sizeof(*elbg->codeword));
119  if (!elbg->codeword)
120  return AVERROR(ENOMEM);
121 
124  sizeof(*elbg->codeword_closest_codebook_idxs));
126  return AVERROR(ENOMEM);
127 
128  elbg->codebook = av_realloc_f(elbg->codebook, elbg->codebook_length,
129  NB_COMPONENTS * sizeof(*elbg->codebook));
130  if (!elbg->codebook)
131  return AVERROR(ENOMEM);
132 
133  ff_fill_rgba_map(elbg->rgba_map, inlink->format);
134 
135  return 0;
136 }
137 
138 #define R 0
139 #define G 1
140 #define B 2
141 
142 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
143 {
144  ELBGContext *elbg = inlink->dst->priv;
145  int i, j, k;
146  uint8_t *p, *p0;
147 
148  const uint8_t r_idx = elbg->rgba_map[R];
149  const uint8_t g_idx = elbg->rgba_map[G];
150  const uint8_t b_idx = elbg->rgba_map[B];
151 
152  /* build the codeword */
153  p0 = frame->data[0];
154  k = 0;
155  for (i = 0; i < inlink->h; i++) {
156  p = p0;
157  for (j = 0; j < inlink->w; j++) {
158  elbg->codeword[k++] = p[r_idx];
159  elbg->codeword[k++] = p[g_idx];
160  elbg->codeword[k++] = p[b_idx];
161  p += elbg->pix_desc->nb_components;
162  }
163  p0 += frame->linesize[0];
164  }
165 
166  /* compute the codebook */
168  elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
169  elbg->codeword_closest_codebook_idxs, &elbg->lfg);
171  elbg->codebook, elbg->codebook_length, elbg->max_steps_nb,
172  elbg->codeword_closest_codebook_idxs, &elbg->lfg);
173 
174  if (elbg->pal8) {
175  AVFilterLink *outlink = inlink->dst->outputs[0];
176  AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
177  uint32_t *pal;
178 
179  if (!out)
180  return AVERROR(ENOMEM);
181  out->pts = frame->pts;
182  av_frame_free(&frame);
183  pal = (uint32_t *)out->data[1];
184  p0 = (uint8_t *)out->data[0];
185 
186  for (i = 0; i < elbg->codebook_length; i++) {
187  pal[i] = (elbg->codebook[i*3 ] << 16) |
188  (elbg->codebook[i*3+1] << 8) |
189  elbg->codebook[i*3+2];
190  }
191 
192  k = 0;
193  for (i = 0; i < inlink->h; i++) {
194  p = p0;
195  for (j = 0; j < inlink->w; j++, p++) {
196  p[0] = elbg->codeword_closest_codebook_idxs[k++];
197  }
198  p0 += out->linesize[0];
199  }
200 
201  return ff_filter_frame(outlink, out);
202  }
203 
204  /* fill the output with the codebook values */
205  p0 = frame->data[0];
206 
207  k = 0;
208  for (i = 0; i < inlink->h; i++) {
209  p = p0;
210  for (j = 0; j < inlink->w; j++) {
211  int cb_idx = NB_COMPONENTS * elbg->codeword_closest_codebook_idxs[k++];
212  p[r_idx] = elbg->codebook[cb_idx];
213  p[g_idx] = elbg->codebook[cb_idx+1];
214  p[b_idx] = elbg->codebook[cb_idx+2];
215  p += elbg->pix_desc->nb_components;
216  }
217  p0 += frame->linesize[0];
218  }
219 
220  return ff_filter_frame(inlink->dst->outputs[0], frame);
221 }
222 
223 static av_cold void uninit(AVFilterContext *ctx)
224 {
225  ELBGContext *elbg = ctx->priv;
226 
227  av_freep(&elbg->codebook);
228  av_freep(&elbg->codeword);
230 }
231 
232 static const AVFilterPad elbg_inputs[] = {
233  {
234  .name = "default",
235  .type = AVMEDIA_TYPE_VIDEO,
236  .config_props = config_input,
237  .filter_frame = filter_frame,
238  .needs_writable = 1,
239  },
240  { NULL }
241 };
242 
243 static const AVFilterPad elbg_outputs[] = {
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_VIDEO,
247  },
248  { NULL }
249 };
250 
252  .name = "elbg",
253  .description = NULL_IF_CONFIG_SMALL("Apply posterize effect, using the ELBG algorithm."),
254  .priv_size = sizeof(ELBGContext),
255  .priv_class = &elbg_class,
257  .init = init,
258  .uninit = uninit,
259  .inputs = elbg_inputs,
260  .outputs = elbg_outputs,
261 };
Definition: lfg.h:25
#define NULL
Definition: coverity.c:32
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_elbg.c:223
AVFilter ff_vf_elbg
Definition: vf_elbg.c:251
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2129
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:51
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:110
#define B
Definition: vf_elbg.c:140
int * codebook
Definition: vf_elbg.c:44
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
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:232
const char * name
Pad name.
Definition: internal.h:69
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1158
uint8_t
#define av_cold
Definition: attributes.h:74
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
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
int pal8
Definition: vf_elbg.c:48
#define av_log(a,...)
AVLFG lfg
Definition: vf_elbg.c:38
A filter pad used for either input or output.
Definition: internal.h:63
int max_steps_nb
Definition: vf_elbg.c:40
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:83
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#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:52
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:54
int * codeword
Definition: vf_elbg.c:41
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:422
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:34
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_elbg.c:142
static const AVFilterPad elbg_outputs[]
Definition: vf_elbg.c:243
static av_cold int init(AVFilterContext *ctx)
Definition: vf_elbg.c:67
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
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:209
#define G
Definition: vf_elbg.c:139
#define NB_COMPONENTS
Definition: vf_elbg.c:108
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
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
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:138
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int * codeword_closest_codebook_idxs
Definition: vf_elbg.c:43