vf_colorbalance.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/opt.h"
22 #include "libavutil/pixdesc.h"
23 #include "avfilter.h"
24 #include "drawutils.h"
25 #include "formats.h"
26 #include "internal.h"
27 #include "video.h"
28 
29 #define R 0
30 #define G 1
31 #define B 2
32 #define A 3
33 
34 typedef struct {
35  double shadows;
36  double midtones;
37  double highlights;
38 } Range;
39 
40 typedef struct {
41  const AVClass *class;
45 
46  uint8_t lut[3][256];
47 
48  uint8_t rgba_map[4];
49  int step;
51 
52 #define OFFSET(x) offsetof(ColorBalanceContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption colorbalance_options[] = {
55  { "rs", "set red shadows", OFFSET(cyan_red.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
56  { "gs", "set green shadows", OFFSET(magenta_green.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
57  { "bs", "set blue shadows", OFFSET(yellow_blue.shadows), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
58  { "rm", "set red midtones", OFFSET(cyan_red.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
59  { "gm", "set green midtones", OFFSET(magenta_green.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
60  { "bm", "set blue midtones", OFFSET(yellow_blue.midtones), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
61  { "rh", "set red highlights", OFFSET(cyan_red.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
62  { "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
63  { "bh", "set blue highlights", OFFSET(yellow_blue.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
64  { NULL }
65 };
66 
67 AVFILTER_DEFINE_CLASS(colorbalance);
68 
70 {
71  static const enum AVPixelFormat pix_fmts[] = {
78  };
79 
81  return 0;
82 }
83 
84 static int config_output(AVFilterLink *outlink)
85 {
86  AVFilterContext *ctx = outlink->src;
87  ColorBalanceContext *cb = ctx->priv;
88  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
89  double *shadows, *midtones, *highlights, *buffer;
90  int i, r, g, b;
91 
92  buffer = av_malloc(256 * 3 * sizeof(*buffer));
93  if (!buffer)
94  return AVERROR(ENOMEM);
95 
96  shadows = buffer + 256 * 0;
97  midtones = buffer + 256 * 1;
98  highlights = buffer + 256 * 2;
99 
100  for (i = 0; i < 256; i++) {
101  double low = av_clipd((i - 85.0) / -64.0 + 0.5, 0, 1) * 178.5;
102  double mid = av_clipd((i - 85.0) / 64.0 + 0.5, 0, 1) *
103  av_clipd((i + 85.0 - 255.0) / -64.0 + 0.5, 0, 1) * 178.5;
104 
105  shadows[i] = low;
106  midtones[i] = mid;
107  highlights[255 - i] = low;
108  }
109 
110  for (i = 0; i < 256; i++) {
111  r = g = b = i;
112 
113  r = av_clip_uint8(r + cb->cyan_red.shadows * shadows[r]);
114  r = av_clip_uint8(r + cb->cyan_red.midtones * midtones[r]);
115  r = av_clip_uint8(r + cb->cyan_red.highlights * highlights[r]);
116 
117  g = av_clip_uint8(g + cb->magenta_green.shadows * shadows[g]);
118  g = av_clip_uint8(g + cb->magenta_green.midtones * midtones[g]);
119  g = av_clip_uint8(g + cb->magenta_green.highlights * highlights[g]);
120 
121  b = av_clip_uint8(b + cb->yellow_blue.shadows * shadows[b]);
122  b = av_clip_uint8(b + cb->yellow_blue.midtones * midtones[b]);
123  b = av_clip_uint8(b + cb->yellow_blue.highlights * highlights[b]);
124 
125  cb->lut[R][i] = r;
126  cb->lut[G][i] = g;
127  cb->lut[B][i] = b;
128  }
129 
130  av_free(buffer);
131 
132  ff_fill_rgba_map(cb->rgba_map, outlink->format);
133  cb->step = av_get_padded_bits_per_pixel(desc) >> 3;
134 
135  return 0;
136 }
137 
138 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
139 {
140  AVFilterContext *ctx = inlink->dst;
141  ColorBalanceContext *cb = ctx->priv;
142  AVFilterLink *outlink = ctx->outputs[0];
143  const uint8_t roffset = cb->rgba_map[R];
144  const uint8_t goffset = cb->rgba_map[G];
145  const uint8_t boffset = cb->rgba_map[B];
146  const uint8_t aoffset = cb->rgba_map[A];
147  const int step = cb->step;
148  const uint8_t *srcrow = in->data[0];
149  uint8_t *dstrow;
150  AVFrame *out;
151  int i, j;
152 
153  if (av_frame_is_writable(in)) {
154  out = in;
155  } else {
156  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
157  if (!out) {
158  av_frame_free(&in);
159  return AVERROR(ENOMEM);
160  }
161  av_frame_copy_props(out, in);
162  }
163 
164  dstrow = out->data[0];
165  for (i = 0; i < outlink->h; i++) {
166  const uint8_t *src = srcrow;
167  uint8_t *dst = dstrow;
168 
169  for (j = 0; j < outlink->w * step; j += step) {
170  dst[j + roffset] = cb->lut[R][src[j + roffset]];
171  dst[j + goffset] = cb->lut[G][src[j + goffset]];
172  dst[j + boffset] = cb->lut[B][src[j + boffset]];
173  if (in != out && step == 4)
174  dst[j + aoffset] = src[j + aoffset];
175  }
176 
177  srcrow += in->linesize[0];
178  dstrow += out->linesize[0];
179  }
180 
181  if (in != out)
182  av_frame_free(&in);
183  return ff_filter_frame(ctx->outputs[0], out);
184 }
185 
187  {
188  .name = "default",
189  .type = AVMEDIA_TYPE_VIDEO,
190  .filter_frame = filter_frame,
191  },
192  { NULL }
193 };
194 
196  {
197  .name = "default",
198  .type = AVMEDIA_TYPE_VIDEO,
199  .config_props = config_output,
200  },
201  { NULL }
202 };
203 
205  .name = "colorbalance",
206  .description = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
207  .priv_size = sizeof(ColorBalanceContext),
208  .priv_class = &colorbalance_class,
210  .inputs = colorbalance_inputs,
211  .outputs = colorbalance_outputs,
212 };
static const AVFilterPad colorbalance_inputs[]
#define A
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
static const AVFilterPad colorbalance_outputs[]
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
packed BGR 8:8:8, 32bpp, 0BGR0BGR...
Definition: pixfmt.h:216
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:308
const char * name
Pad name.
static int query_formats(AVFilterContext *ctx)
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
packed RGB 8:8:8, 32bpp, RGB0RGB0...
Definition: pixfmt.h:215
static int config_output(AVFilterLink *outlink)
AVOptions.
#define b
Definition: input.c:42
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:98
double shadows
void 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:545
A filter pad used for either input or output.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
AVFILTER_DEFINE_CLASS(colorbalance)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:99
const char * r
Definition: vf_curves.c:94
void * priv
private data for use by the filter
Definition: avfilter.h:545
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:1744
double highlights
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:361
#define B
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:96
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:97
FFT buffer for g
Definition: stft_peak.m:17
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:71
#define R
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:33
#define G
uint8_t lut[3][256]
NULL
Definition: eval.c:55
AVS_Value src
Definition: avisynth_c.h:523
misc drawing utilities
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
FIXME Range Coding of cb
Definition: snow.txt:367
double midtones
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
synthesis window for stochastic i
packed BGR 8:8:8, 32bpp, BGR0BGR0...
Definition: pixfmt.h:217
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
the buffer and buffer reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFilterBuffer structures They must not be accessed but through references stored in AVFilterBufferRef structures Several references can point to the same buffer
#define OFFSET(x)
AVFilter avfilter_vf_colorbalance
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
An instance of a filter.
Definition: avfilter.h:524
static const AVOption colorbalance_options[]
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;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);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
internal API functions
#define FLAGS
packed RGB 8:8:8, 32bpp, 0RGB0RGB...
Definition: pixfmt.h:214
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
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 inputs
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step