2 * Copyright (c) 2003 Michael Niedermayer
3 * Copyright (c) 2012 Jeremy Tran
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Apply a hue/saturation filter to the input video
25 * Ported from MPlayer libmpcodecs/vf_hue.c.
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
38 #define HUE_DEFAULT_VAL 0
39 #define SAT_DEFAULT_VAL 1
43 float hue_deg; /* hue expressed in degrees */
44 float hue; /* hue expressed in radians */
52 #define OFFSET(x) offsetof(HueContext, x)
53 static const AVOption hue_options[] = {
54 { "h", "set the hue angle degrees", OFFSET(hue_deg), AV_OPT_TYPE_FLOAT,
55 { -FLT_MAX }, -FLT_MAX, FLT_MAX, AV_OPT_FLAG_VIDEO_PARAM },
56 { "H", "set the hue angle radians", OFFSET(hue), AV_OPT_TYPE_FLOAT,
57 { -FLT_MAX }, -FLT_MAX, FLT_MAX, AV_OPT_FLAG_VIDEO_PARAM },
58 { "s", "set the saturation value", OFFSET(saturation), AV_OPT_TYPE_FLOAT,
59 { SAT_DEFAULT_VAL }, -10, 10, AV_OPT_FLAG_VIDEO_PARAM },
63 AVFILTER_DEFINE_CLASS(hue);
65 static av_cold int init(AVFilterContext *ctx, const char *args)
67 HueContext *hue = ctx->priv;
68 float h = HUE_DEFAULT_VAL, s = SAT_DEFAULT_VAL;
73 hue->class = &hue_class;
75 /* named options syntax */
76 if (equal = strchr(args, '=')) {
77 av_opt_set_defaults(hue);
78 if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
80 if (hue->hue != -FLT_MAX && hue->hue_deg != -FLT_MAX) {
81 av_log(ctx, AV_LOG_ERROR,
82 "H and h options are incompatible and cannot be specified "
83 "at the same time\n");
84 return AVERROR(EINVAL);
86 if (hue->hue == -FLT_MAX)
87 hue->hue = HUE_DEFAULT_VAL;
88 /* compatibility syntax */
91 n = sscanf(args, "%f%c%f%c", &h, &c1, &s, &c2);
92 if (n != 0 && n != 1 && (n != 3 || c1 != ':')) {
93 av_log(ctx, AV_LOG_ERROR,
94 "Invalid syntax for argument '%s': "
95 "must be in the form 'hue[:saturation]'\n", args);
96 return AVERROR(EINVAL);
99 if (s < -10 || s > 10) {
100 av_log(ctx, AV_LOG_ERROR,
101 "Invalid value for saturation %0.1f: "
102 "must be included between range -10 and +10\n", s);
103 return AVERROR(EINVAL);
110 if (hue->hue_deg != -FLT_MAX)
111 /* Convert angle from degrees to radians */
112 hue->hue = hue->hue_deg * M_PI / 180;
117 static av_cold void uninit(AVFilterContext *ctx)
119 HueContext *hue = ctx->priv;
124 static int query_formats(AVFilterContext *ctx)
126 static const enum PixelFormat pix_fmts[] = {
127 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
128 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
129 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
134 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
139 static int config_props(AVFilterLink *inlink)
141 HueContext *hue = inlink->dst->priv;
142 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
144 hue->hsub = desc->log2_chroma_w;
145 hue->vsub = desc->log2_chroma_h;
147 * Scale the value to the norm of the resulting (U,V) vector, that is
149 * This will be useful in the process_chrominance function.
151 hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
152 hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
157 static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
158 uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
160 const int32_t c, const int32_t s)
162 int32_t u, v, new_u, new_v;
166 * If we consider U and V as the components of a 2D vector then its angle
167 * is the hue and the norm is the saturation
170 for (i = 0; i < w; i++) {
171 /* Normalize the components from range [16;140] to [-112;112] */
175 * Apply the rotation of the vector : (c * u) - (s * v)
177 * De-normalize the components (without forgetting to scale 128
179 * Finally scale back the result by >> 16
181 new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
182 new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
184 /* Prevent a potential overflow */
185 udst[i] = av_clip_uint8_c(new_u);
186 vdst[i] = av_clip_uint8_c(new_v);
189 usrc += src_linesize;
190 vsrc += src_linesize;
191 udst += dst_linesize;
192 vdst += dst_linesize;
196 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
198 HueContext *hue = inlink->dst->priv;
199 AVFilterBufferRef *inpic = inlink->cur_buf;
200 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
201 uint8_t *inrow[3], *outrow[3]; // 0 : Y, 1 : U, 2 : V
204 inrow[0] = inpic->data[0] + y * inpic->linesize[0];
205 outrow[0] = outpic->data[0] + y * outpic->linesize[0];
207 for (plane = 1; plane < 3; plane++) {
208 inrow[plane] = inpic->data[plane] + (y >> hue->vsub) * inpic->linesize[plane];
209 outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
212 av_image_copy_plane(outrow[0], outpic->linesize[0],
213 inrow[0], inpic->linesize[0],
214 inlink->w, inlink->h);
216 process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
217 inrow[1], inrow[2], inpic->linesize[1],
218 inlink->w >> hue->hsub, inlink->h >> hue->vsub,
219 hue->hue_cos, hue->hue_sin);
221 return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
224 AVFilter avfilter_vf_hue = {
226 .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
228 .priv_size = sizeof(HueContext),
232 .query_formats = query_formats,
234 .inputs = (const AVFilterPad[]) {
237 .type = AVMEDIA_TYPE_VIDEO,
238 .draw_slice = draw_slice,
239 .config_props = config_props,
240 .min_perms = AV_PERM_READ,
244 .outputs = (const AVFilterPad[]) {
247 .type = AVMEDIA_TYPE_VIDEO,