00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include <float.h>
00029 #include "libavutil/eval.h"
00030 #include "libavutil/imgutils.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/pixdesc.h"
00033
00034 #include "avfilter.h"
00035 #include "formats.h"
00036 #include "internal.h"
00037 #include "video.h"
00038
00039 #define HUE_DEFAULT_VAL 0
00040 #define SAT_DEFAULT_VAL 1
00041
00042 #define HUE_DEFAULT_VAL_STRING AV_STRINGIFY(HUE_DEFAULT_VAL)
00043 #define SAT_DEFAULT_VAL_STRING AV_STRINGIFY(SAT_DEFAULT_VAL)
00044
00045 #define SAT_MIN_VAL -10
00046 #define SAT_MAX_VAL 10
00047
00048 static const char *const var_names[] = {
00049 "n",
00050 "pts",
00051 "r",
00052 "t",
00053 "tb",
00054 NULL
00055 };
00056
00057 enum var_name {
00058 VAR_N,
00059 VAR_PTS,
00060 VAR_R,
00061 VAR_T,
00062 VAR_TB,
00063 VAR_NB
00064 };
00065
00066 typedef struct {
00067 const AVClass *class;
00068 float hue_deg;
00069 float hue;
00070 char *hue_deg_expr;
00071 char *hue_expr;
00072 AVExpr *hue_deg_pexpr;
00073 AVExpr *hue_pexpr;
00074 float saturation;
00075 char *saturation_expr;
00076 AVExpr *saturation_pexpr;
00077 int hsub;
00078 int vsub;
00079 int32_t hue_sin;
00080 int32_t hue_cos;
00081 int flat_syntax;
00082 double var_values[VAR_NB];
00083 } HueContext;
00084
00085 #define OFFSET(x) offsetof(HueContext, x)
00086 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00087 static const AVOption hue_options[] = {
00088 { "h", "set the hue angle degrees expression", OFFSET(hue_deg_expr), AV_OPT_TYPE_STRING,
00089 { .str = NULL }, .flags = FLAGS },
00090 { "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
00091 { .str = NULL }, .flags = FLAGS },
00092 { "s", "set the saturation expression", OFFSET(saturation_expr), AV_OPT_TYPE_STRING,
00093 { .str = NULL }, .flags = FLAGS },
00094 { NULL }
00095 };
00096
00097 AVFILTER_DEFINE_CLASS(hue);
00098
00099 static inline void compute_sin_and_cos(HueContext *hue)
00100 {
00101
00102
00103
00104
00105
00106 hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
00107 hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
00108 }
00109
00110 #define SET_EXPRESSION(attr, name) do { \
00111 if (hue->attr##_expr) { \
00112 if ((ret = av_expr_parse(&hue->attr##_pexpr, hue->attr##_expr, var_names, \
00113 NULL, NULL, NULL, NULL, 0, ctx)) < 0) { \
00114 av_log(ctx, AV_LOG_ERROR, \
00115 "Parsing failed for expression " #name "='%s'", \
00116 hue->attr##_expr); \
00117 hue->attr##_expr = old_##attr##_expr; \
00118 hue->attr##_pexpr = old_##attr##_pexpr; \
00119 return AVERROR(EINVAL); \
00120 } else if (old_##attr##_pexpr) { \
00121 av_freep(&old_##attr##_expr); \
00122 av_expr_free(old_##attr##_pexpr); \
00123 old_##attr##_pexpr = NULL; \
00124 } \
00125 } else { \
00126 hue->attr##_expr = old_##attr##_expr; \
00127 } \
00128 } while (0)
00129
00130 static inline int set_options(AVFilterContext *ctx, const char *args)
00131 {
00132 HueContext *hue = ctx->priv;
00133 int n, ret;
00134 char c1 = 0, c2 = 0;
00135 char *old_hue_expr, *old_hue_deg_expr, *old_saturation_expr;
00136 AVExpr *old_hue_pexpr, *old_hue_deg_pexpr, *old_saturation_pexpr;
00137
00138 if (args) {
00139
00140 if (strchr(args, '=')) {
00141 old_hue_expr = hue->hue_expr;
00142 old_hue_deg_expr = hue->hue_deg_expr;
00143 old_saturation_expr = hue->saturation_expr;
00144
00145 old_hue_pexpr = hue->hue_pexpr;
00146 old_hue_deg_pexpr = hue->hue_deg_pexpr;
00147 old_saturation_pexpr = hue->saturation_pexpr;
00148
00149 hue->hue_expr = NULL;
00150 hue->hue_deg_expr = NULL;
00151 hue->saturation_expr = NULL;
00152
00153 if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
00154 return ret;
00155 if (hue->hue_expr && hue->hue_deg_expr) {
00156 av_log(ctx, AV_LOG_ERROR,
00157 "H and h options are incompatible and cannot be specified "
00158 "at the same time\n");
00159 hue->hue_expr = old_hue_expr;
00160 hue->hue_deg_expr = old_hue_deg_expr;
00161
00162 return AVERROR(EINVAL);
00163 }
00164
00165 SET_EXPRESSION(hue_deg, h);
00166 SET_EXPRESSION(hue, H);
00167 SET_EXPRESSION(saturation, s);
00168
00169 hue->flat_syntax = 0;
00170
00171 av_log(ctx, AV_LOG_VERBOSE,
00172 "H_expr:%s h_deg_expr:%s s_expr:%s\n",
00173 hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr);
00174
00175
00176 } else {
00177 n = sscanf(args, "%f%c%f%c", &hue->hue_deg, &c1, &hue->saturation, &c2);
00178 if (n != 1 && (n != 3 || c1 != ':')) {
00179 av_log(ctx, AV_LOG_ERROR,
00180 "Invalid syntax for argument '%s': "
00181 "must be in the form 'hue[:saturation]'\n", args);
00182 return AVERROR(EINVAL);
00183 }
00184
00185 if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
00186 av_log(ctx, AV_LOG_ERROR,
00187 "Invalid value for saturation %0.1f: "
00188 "must be included between range %d and +%d\n",
00189 hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
00190 return AVERROR(EINVAL);
00191 }
00192
00193 hue->hue = hue->hue_deg * M_PI / 180;
00194 hue->flat_syntax = 1;
00195
00196 av_log(ctx, AV_LOG_VERBOSE,
00197 "H:%0.1f h:%0.1f s:%0.1f\n",
00198 hue->hue, hue->hue_deg, hue->saturation);
00199 }
00200 }
00201
00202 compute_sin_and_cos(hue);
00203
00204 return 0;
00205 }
00206
00207 static av_cold int init(AVFilterContext *ctx, const char *args)
00208 {
00209 HueContext *hue = ctx->priv;
00210
00211 hue->class = &hue_class;
00212 av_opt_set_defaults(hue);
00213
00214 hue->saturation = SAT_DEFAULT_VAL;
00215 hue->hue = HUE_DEFAULT_VAL;
00216 hue->hue_deg_pexpr = NULL;
00217 hue->hue_pexpr = NULL;
00218 hue->flat_syntax = 1;
00219
00220 return set_options(ctx, args);
00221 }
00222
00223 static av_cold void uninit(AVFilterContext *ctx)
00224 {
00225 HueContext *hue = ctx->priv;
00226
00227 av_opt_free(hue);
00228
00229 av_free(hue->hue_deg_expr);
00230 av_expr_free(hue->hue_deg_pexpr);
00231 av_free(hue->hue_expr);
00232 av_expr_free(hue->hue_pexpr);
00233 av_free(hue->saturation_expr);
00234 av_expr_free(hue->saturation_pexpr);
00235 }
00236
00237 static int query_formats(AVFilterContext *ctx)
00238 {
00239 static const enum PixelFormat pix_fmts[] = {
00240 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
00241 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
00242 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
00243 PIX_FMT_YUVA420P,
00244 PIX_FMT_NONE
00245 };
00246
00247 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00248
00249 return 0;
00250 }
00251
00252 static int config_props(AVFilterLink *inlink)
00253 {
00254 HueContext *hue = inlink->dst->priv;
00255 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
00256
00257 hue->hsub = desc->log2_chroma_w;
00258 hue->vsub = desc->log2_chroma_h;
00259
00260 hue->var_values[VAR_N] = 0;
00261 hue->var_values[VAR_TB] = av_q2d(inlink->time_base);
00262 hue->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
00263 NAN : av_q2d(inlink->frame_rate);
00264
00265 return 0;
00266 }
00267
00268 static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
00269 uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
00270 int w, int h,
00271 const int32_t c, const int32_t s)
00272 {
00273 int32_t u, v, new_u, new_v;
00274 int i;
00275
00276
00277
00278
00279
00280 while (h--) {
00281 for (i = 0; i < w; i++) {
00282
00283 u = usrc[i] - 128;
00284 v = vsrc[i] - 128;
00285
00286
00287
00288
00289
00290
00291
00292 new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
00293 new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
00294
00295
00296 udst[i] = av_clip_uint8_c(new_u);
00297 vdst[i] = av_clip_uint8_c(new_v);
00298 }
00299
00300 usrc += src_linesize;
00301 vsrc += src_linesize;
00302 udst += dst_linesize;
00303 vdst += dst_linesize;
00304 }
00305 }
00306
00307 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
00308 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
00309
00310 static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
00311 {
00312 HueContext *hue = inlink->dst->priv;
00313 AVFilterLink *outlink = inlink->dst->outputs[0];
00314 AVFilterBufferRef *buf_out;
00315
00316 outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00317 if (!outlink->out_buf)
00318 return AVERROR(ENOMEM);
00319
00320 avfilter_copy_buffer_ref_props(outlink->out_buf, inpic);
00321 outlink->out_buf->video->w = outlink->w;
00322 outlink->out_buf->video->h = outlink->h;
00323 buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
00324 if (!buf_out)
00325 return AVERROR(ENOMEM);
00326
00327 if (!hue->flat_syntax) {
00328 hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
00329 hue->var_values[VAR_PTS] = TS2D(inpic->pts);
00330
00331 if (hue->saturation_expr) {
00332 hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
00333
00334 if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
00335 hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
00336 av_log(inlink->dst, AV_LOG_WARNING,
00337 "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
00338 SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
00339 }
00340 }
00341
00342 if (hue->hue_deg_expr) {
00343 hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
00344 hue->hue = hue->hue_deg * M_PI / 180;
00345 } else if (hue->hue_expr) {
00346 hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
00347 }
00348
00349 av_log(inlink->dst, AV_LOG_DEBUG,
00350 "H:%0.1f s:%0.f t:%0.1f n:%d\n",
00351 hue->hue, hue->saturation,
00352 hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
00353
00354 compute_sin_and_cos(hue);
00355 }
00356
00357 hue->var_values[VAR_N] += 1;
00358
00359 return ff_start_frame(outlink, buf_out);
00360 }
00361
00362 static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00363 {
00364 HueContext *hue = inlink->dst->priv;
00365 AVFilterBufferRef *inpic = inlink->cur_buf;
00366 AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
00367 uint8_t *inrow[3], *outrow[3];
00368 int plane;
00369
00370 inrow[0] = inpic->data[0] + y * inpic->linesize[0];
00371 outrow[0] = outpic->data[0] + y * outpic->linesize[0];
00372
00373 for (plane = 1; plane < 3; plane++) {
00374 inrow[plane] = inpic->data[plane] + (y >> hue->vsub) * inpic->linesize[plane];
00375 outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
00376 }
00377
00378 av_image_copy_plane(outrow[0], outpic->linesize[0],
00379 inrow[0], inpic->linesize[0],
00380 inlink->w, inlink->h);
00381
00382 process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
00383 inrow[1], inrow[2], inpic->linesize[1],
00384 inlink->w >> hue->hsub, inlink->h >> hue->vsub,
00385 hue->hue_cos, hue->hue_sin);
00386
00387 return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
00388 }
00389
00390 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
00391 char *res, int res_len, int flags)
00392 {
00393 if (!strcmp(cmd, "reinit"))
00394 return set_options(ctx, args);
00395 else
00396 return AVERROR(ENOSYS);
00397 }
00398
00399 AVFilter avfilter_vf_hue = {
00400 .name = "hue",
00401 .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
00402
00403 .priv_size = sizeof(HueContext),
00404
00405 .init = init,
00406 .uninit = uninit,
00407 .query_formats = query_formats,
00408 .process_command = process_command,
00409
00410 .inputs = (const AVFilterPad[]) {
00411 {
00412 .name = "default",
00413 .type = AVMEDIA_TYPE_VIDEO,
00414 .start_frame = start_frame,
00415 .draw_slice = draw_slice,
00416 .config_props = config_props,
00417 .min_perms = AV_PERM_READ,
00418 },
00419 { .name = NULL }
00420 },
00421 .outputs = (const AVFilterPad[]) {
00422 {
00423 .name = "default",
00424 .type = AVMEDIA_TYPE_VIDEO,
00425 },
00426 { .name = NULL }
00427 },
00428 .priv_class = &hue_class,
00429 };