FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_colorlevels.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Paul B Mahol
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 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "drawutils.h"
26 #include "formats.h"
27 #include "internal.h"
28 #include "video.h"
29 
30 #define R 0
31 #define G 1
32 #define B 2
33 #define A 3
34 
35 typedef struct {
36  double in_min, in_max;
37  double out_min, out_max;
38 } Range;
39 
40 typedef struct {
41  const AVClass *class;
42  Range range[4];
43  int nb_comp;
44  int bpp;
45  int step;
46  uint8_t rgba_map[4];
47  int linesize;
49 
50 #define OFFSET(x) offsetof(ColorLevelsContext, x)
51 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
52 static const AVOption colorlevels_options[] = {
53  { "rimin", "set input red black point", OFFSET(range[R].in_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
54  { "gimin", "set input green black point", OFFSET(range[G].in_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
55  { "bimin", "set input blue black point", OFFSET(range[B].in_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
56  { "aimin", "set input alpha black point", OFFSET(range[A].in_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
57  { "rimax", "set input red white point", OFFSET(range[R].in_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
58  { "gimax", "set input green white point", OFFSET(range[G].in_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
59  { "bimax", "set input blue white point", OFFSET(range[B].in_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
60  { "aimax", "set input alpha white point", OFFSET(range[A].in_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -1, 1, FLAGS },
61  { "romin", "set output red black point", OFFSET(range[R].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
62  { "gomin", "set output green black point", OFFSET(range[G].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
63  { "bomin", "set output blue black point", OFFSET(range[B].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
64  { "aomin", "set output alpha black point", OFFSET(range[A].out_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1, FLAGS },
65  { "romax", "set output red white point", OFFSET(range[R].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
66  { "gomax", "set output green white point", OFFSET(range[G].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
67  { "bomax", "set output blue white point", OFFSET(range[B].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
68  { "aomax", "set output alpha white point", OFFSET(range[A].out_max), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 1, FLAGS },
69  { NULL }
70 };
71 
72 AVFILTER_DEFINE_CLASS(colorlevels);
73 
75 {
76  static const enum AVPixelFormat pix_fmts[] = {
85  };
86 
87  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
88  if (!fmts_list)
89  return AVERROR(ENOMEM);
90  return ff_set_common_formats(ctx, fmts_list);
91 }
92 
93 static int config_input(AVFilterLink *inlink)
94 {
95  AVFilterContext *ctx = inlink->dst;
96  ColorLevelsContext *s = ctx->priv;
98 
99  s->nb_comp = desc->nb_components;
100  s->bpp = desc->comp[0].depth >> 3;
101  s->step = (av_get_padded_bits_per_pixel(desc) >> 3) / s->bpp;
102  s->linesize = inlink->w * s->step;
103  ff_fill_rgba_map(s->rgba_map, inlink->format);
104 
105  return 0;
106 }
107 
108 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
109 {
110  AVFilterContext *ctx = inlink->dst;
111  ColorLevelsContext *s = ctx->priv;
112  AVFilterLink *outlink = ctx->outputs[0];
113  const int step = s->step;
114  AVFrame *out;
115  int x, y, i;
116 
117  if (av_frame_is_writable(in)) {
118  out = in;
119  } else {
120  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
121  if (!out) {
122  av_frame_free(&in);
123  return AVERROR(ENOMEM);
124  }
125  av_frame_copy_props(out, in);
126  }
127 
128  switch (s->bpp) {
129  case 1:
130  for (i = 0; i < s->nb_comp; i++) {
131  Range *r = &s->range[i];
132  const uint8_t offset = s->rgba_map[i];
133  const uint8_t *srcrow = in->data[0];
134  uint8_t *dstrow = out->data[0];
135  int imin = lrint(r->in_min * UINT8_MAX);
136  int imax = lrint(r->in_max * UINT8_MAX);
137  int omin = lrint(r->out_min * UINT8_MAX);
138  int omax = lrint(r->out_max * UINT8_MAX);
139  double coeff;
140 
141  if (imin < 0) {
142  imin = UINT8_MAX;
143  for (y = 0; y < inlink->h; y++) {
144  const uint8_t *src = srcrow;
145 
146  for (x = 0; x < s->linesize; x += step)
147  imin = FFMIN(imin, src[x + offset]);
148  srcrow += in->linesize[0];
149  }
150  }
151  if (imax < 0) {
152  srcrow = in->data[0];
153  imax = 0;
154  for (y = 0; y < inlink->h; y++) {
155  const uint8_t *src = srcrow;
156 
157  for (x = 0; x < s->linesize; x += step)
158  imax = FFMAX(imax, src[x + offset]);
159  srcrow += in->linesize[0];
160  }
161  }
162 
163  srcrow = in->data[0];
164  coeff = (omax - omin) / (double)(imax - imin);
165  for (y = 0; y < inlink->h; y++) {
166  const uint8_t *src = srcrow;
167  uint8_t *dst = dstrow;
168 
169  for (x = 0; x < s->linesize; x += step)
170  dst[x + offset] = av_clip_uint8((src[x + offset] - imin) * coeff + omin);
171  dstrow += out->linesize[0];
172  srcrow += in->linesize[0];
173  }
174  }
175  break;
176  case 2:
177  for (i = 0; i < s->nb_comp; i++) {
178  Range *r = &s->range[i];
179  const uint8_t offset = s->rgba_map[i];
180  const uint8_t *srcrow = in->data[0];
181  uint8_t *dstrow = out->data[0];
182  int imin = lrint(r->in_min * UINT16_MAX);
183  int imax = lrint(r->in_max * UINT16_MAX);
184  int omin = lrint(r->out_min * UINT16_MAX);
185  int omax = lrint(r->out_max * UINT16_MAX);
186  double coeff;
187 
188  if (imin < 0) {
189  imin = UINT16_MAX;
190  for (y = 0; y < inlink->h; y++) {
191  const uint16_t *src = (const uint16_t *)srcrow;
192 
193  for (x = 0; x < s->linesize; x += step)
194  imin = FFMIN(imin, src[x + offset]);
195  srcrow += in->linesize[0];
196  }
197  }
198  if (imax < 0) {
199  srcrow = in->data[0];
200  imax = 0;
201  for (y = 0; y < inlink->h; y++) {
202  const uint16_t *src = (const uint16_t *)srcrow;
203 
204  for (x = 0; x < s->linesize; x += step)
205  imax = FFMAX(imax, src[x + offset]);
206  srcrow += in->linesize[0];
207  }
208  }
209 
210  srcrow = in->data[0];
211  coeff = (omax - omin) / (double)(imax - imin);
212  for (y = 0; y < inlink->h; y++) {
213  const uint16_t *src = (const uint16_t*)srcrow;
214  uint16_t *dst = (uint16_t *)dstrow;
215 
216  for (x = 0; x < s->linesize; x += step)
217  dst[x + offset] = av_clip_uint16((src[x + offset] - imin) * coeff + omin);
218  dstrow += out->linesize[0];
219  srcrow += in->linesize[0];
220  }
221  }
222  }
223 
224  if (in != out)
225  av_frame_free(&in);
226  return ff_filter_frame(outlink, out);
227 }
228 
229 static const AVFilterPad colorlevels_inputs[] = {
230  {
231  .name = "default",
232  .type = AVMEDIA_TYPE_VIDEO,
233  .filter_frame = filter_frame,
234  .config_props = config_input,
235  },
236  { NULL }
237 };
238 
240  {
241  .name = "default",
242  .type = AVMEDIA_TYPE_VIDEO,
243  },
244  { NULL }
245 };
246 
248  .name = "colorlevels",
249  .description = NULL_IF_CONFIG_SMALL("Adjust the color levels."),
250  .priv_size = sizeof(ColorLevelsContext),
251  .priv_class = &colorlevels_class,
253  .inputs = colorlevels_inputs,
254  .outputs = colorlevels_outputs,
256 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define G
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
misc image utilities
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
const char * desc
Definition: nvenc.c:89
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:331
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:336
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:252
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:123
const char * name
Pad name.
Definition: internal.h:59
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1180
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:251
AVOptions.
#define A
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
static int query_formats(AVFilterContext *ctx)
#define AV_PIX_FMT_BGR48
Definition: pixfmt.h:332
double out_min
A filter pad used for either input or output.
Definition: internal.h:53
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:568
#define FLAGS
static const AVFilterPad colorlevels_inputs[]
#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:153
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
const char * r
Definition: vf_curves.c:107
void * priv
private data for use by the filter
Definition: avfilter.h:320
int av_get_padded_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel for the pixel format described by pixdesc, including any padding ...
Definition: pixdesc.c:2187
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:327
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define FFMAX(a, b)
Definition: common.h:94
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
static int config_input(AVFilterLink *inlink)
#define FFMIN(a, b)
Definition: common.h:96
static const AVOption colorlevels_options[]
AVFormatContext * ctx
Definition: movenc.c:48
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define src
Definition: vp9dsp.c:530
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
misc drawing utilities
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:520
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:215
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
double in_min
#define B
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-> in
static const AVFilterPad colorlevels_outputs[]
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:142
double out_max
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:253
const char * name
Filter name.
Definition: avfilter.h:146
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:317
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:198
#define OFFSET(x)
double in_max
AVFilter ff_vf_colorlevels
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static const double coeff[2][5]
Definition: vf_owdenoise.c:71
#define lrint
Definition: tablegen.h:53
An instance of a filter.
Definition: avfilter.h:305
#define R
FILE * out
Definition: movenc.c:54
AVFILTER_DEFINE_CLASS(colorlevels)
internal API functions
int depth
Number of bits in the component.
Definition: pixdesc.h:58
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:250
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:580