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 AVPixelFormat pix_fmts[] = {
00240 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
00241 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
00242 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
00243 AV_PIX_FMT_YUVA420P,
00244 AV_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_desc_get(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 filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
00311 {
00312 HueContext *hue = inlink->dst->priv;
00313 AVFilterLink *outlink = inlink->dst->outputs[0];
00314 AVFilterBufferRef *outpic;
00315
00316 outpic = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00317 if (!outpic) {
00318 avfilter_unref_bufferp(&inpic);
00319 return AVERROR(ENOMEM);
00320 }
00321 avfilter_copy_buffer_ref_props(outpic, inpic);
00322
00323 if (!hue->flat_syntax) {
00324 hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
00325 hue->var_values[VAR_PTS] = TS2D(inpic->pts);
00326
00327 if (hue->saturation_expr) {
00328 hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
00329
00330 if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
00331 hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
00332 av_log(inlink->dst, AV_LOG_WARNING,
00333 "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
00334 SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
00335 }
00336 }
00337
00338 if (hue->hue_deg_expr) {
00339 hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
00340 hue->hue = hue->hue_deg * M_PI / 180;
00341 } else if (hue->hue_expr) {
00342 hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
00343 }
00344
00345 av_log(inlink->dst, AV_LOG_DEBUG,
00346 "H:%0.1f s:%0.f t:%0.1f n:%d\n",
00347 hue->hue, hue->saturation,
00348 hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
00349
00350 compute_sin_and_cos(hue);
00351 }
00352
00353 hue->var_values[VAR_N] += 1;
00354
00355 av_image_copy_plane(outpic->data[0], outpic->linesize[0],
00356 inpic->data[0], inpic->linesize[0],
00357 inlink->w, inlink->h);
00358
00359 process_chrominance(outpic->data[1], outpic->data[2], outpic->linesize[1],
00360 inpic->data[1], inpic->data[2], inpic->linesize[1],
00361 inlink->w >> hue->hsub, inlink->h >> hue->vsub,
00362 hue->hue_cos, hue->hue_sin);
00363
00364 avfilter_unref_bufferp(&inpic);
00365 return ff_filter_frame(outlink, outpic);
00366 }
00367
00368 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
00369 char *res, int res_len, int flags)
00370 {
00371 if (!strcmp(cmd, "reinit"))
00372 return set_options(ctx, args);
00373 else
00374 return AVERROR(ENOSYS);
00375 }
00376
00377 static const AVFilterPad hue_inputs[] = {
00378 {
00379 .name = "default",
00380 .type = AVMEDIA_TYPE_VIDEO,
00381 .filter_frame = filter_frame,
00382 .config_props = config_props,
00383 .min_perms = AV_PERM_READ,
00384 },
00385 { NULL }
00386 };
00387
00388 static const AVFilterPad hue_outputs[] = {
00389 {
00390 .name = "default",
00391 .type = AVMEDIA_TYPE_VIDEO,
00392 },
00393 { NULL }
00394 };
00395
00396 AVFilter avfilter_vf_hue = {
00397 .name = "hue",
00398 .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
00399
00400 .priv_size = sizeof(HueContext),
00401
00402 .init = init,
00403 .uninit = uninit,
00404 .query_formats = query_formats,
00405 .process_command = process_command,
00406 .inputs = hue_inputs,
00407 .outputs = hue_outputs,
00408 .priv_class = &hue_class,
00409 };