vf_hue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer
  3. * Copyright (c) 2012 Jeremy Tran
  4. *
  5. * This file is part of FFmpeg.
  6. *
  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.
  11. *
  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.
  16. *
  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.
  20. */
  21. /**
  22. * @file
  23. * Apply a hue/saturation filter to the input video
  24. * Ported from MPlayer libmpcodecs/vf_hue.c.
  25. */
  26. #include <float.h>
  27. #include "libavutil/eval.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "avfilter.h"
  32. #include "formats.h"
  33. #include "internal.h"
  34. #include "video.h"
  35. #define HUE_DEFAULT_VAL 0
  36. #define SAT_DEFAULT_VAL 1
  37. #define HUE_DEFAULT_VAL_STRING AV_STRINGIFY(HUE_DEFAULT_VAL)
  38. #define SAT_DEFAULT_VAL_STRING AV_STRINGIFY(SAT_DEFAULT_VAL)
  39. #define SAT_MIN_VAL -10
  40. #define SAT_MAX_VAL 10
  41. static const char *const var_names[] = {
  42. "n", // frame count
  43. "pts", // presentation timestamp expressed in AV_TIME_BASE units
  44. "r", // frame rate
  45. "t", // timestamp expressed in seconds
  46. "tb", // timebase
  47. NULL
  48. };
  49. enum var_name {
  50. VAR_N,
  51. VAR_PTS,
  52. VAR_R,
  53. VAR_T,
  54. VAR_TB,
  55. VAR_NB
  56. };
  57. typedef struct {
  58. const AVClass *class;
  59. float hue_deg; /* hue expressed in degrees */
  60. float hue; /* hue expressed in radians */
  61. char *hue_deg_expr;
  62. char *hue_expr;
  63. AVExpr *hue_deg_pexpr;
  64. AVExpr *hue_pexpr;
  65. float saturation;
  66. char *saturation_expr;
  67. AVExpr *saturation_pexpr;
  68. int hsub;
  69. int vsub;
  70. int32_t hue_sin;
  71. int32_t hue_cos;
  72. int flat_syntax;
  73. double var_values[VAR_NB];
  74. } HueContext;
  75. #define OFFSET(x) offsetof(HueContext, x)
  76. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  77. static const AVOption hue_options[] = {
  78. { "h", "set the hue angle degrees expression", OFFSET(hue_deg_expr), AV_OPT_TYPE_STRING,
  79. { .str = NULL }, .flags = FLAGS },
  80. { "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
  81. { .str = NULL }, .flags = FLAGS },
  82. { "s", "set the saturation expression", OFFSET(saturation_expr), AV_OPT_TYPE_STRING,
  83. { .str = NULL }, .flags = FLAGS },
  84. { NULL }
  85. };
  86. AVFILTER_DEFINE_CLASS(hue);
  87. static inline void compute_sin_and_cos(HueContext *hue)
  88. {
  89. /*
  90. * Scale the value to the norm of the resulting (U,V) vector, that is
  91. * the saturation.
  92. * This will be useful in the process_chrominance function.
  93. */
  94. hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
  95. hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
  96. }
  97. #define SET_EXPRESSION(attr, name) do { \
  98. if (hue->attr##_expr) { \
  99. if ((ret = av_expr_parse(&hue->attr##_pexpr, hue->attr##_expr, var_names, \
  100. NULL, NULL, NULL, NULL, 0, ctx)) < 0) { \
  101. av_log(ctx, AV_LOG_ERROR, \
  102. "Parsing failed for expression " #name "='%s'", \
  103. hue->attr##_expr); \
  104. hue->attr##_expr = old_##attr##_expr; \
  105. hue->attr##_pexpr = old_##attr##_pexpr; \
  106. return AVERROR(EINVAL); \
  107. } else if (old_##attr##_pexpr) { \
  108. av_freep(&old_##attr##_expr); \
  109. av_expr_free(old_##attr##_pexpr); \
  110. old_##attr##_pexpr = NULL; \
  111. } \
  112. } else { \
  113. hue->attr##_expr = old_##attr##_expr; \
  114. } \
  115. } while (0)
  116. static inline int set_options(AVFilterContext *ctx, const char *args)
  117. {
  118. HueContext *hue = ctx->priv;
  119. int n, ret;
  120. char c1 = 0, c2 = 0;
  121. char *old_hue_expr, *old_hue_deg_expr, *old_saturation_expr;
  122. AVExpr *old_hue_pexpr, *old_hue_deg_pexpr, *old_saturation_pexpr;
  123. if (args) {
  124. /* named options syntax */
  125. if (strchr(args, '=')) {
  126. old_hue_expr = hue->hue_expr;
  127. old_hue_deg_expr = hue->hue_deg_expr;
  128. old_saturation_expr = hue->saturation_expr;
  129. old_hue_pexpr = hue->hue_pexpr;
  130. old_hue_deg_pexpr = hue->hue_deg_pexpr;
  131. old_saturation_pexpr = hue->saturation_pexpr;
  132. hue->hue_expr = NULL;
  133. hue->hue_deg_expr = NULL;
  134. hue->saturation_expr = NULL;
  135. if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
  136. return ret;
  137. if (hue->hue_expr && hue->hue_deg_expr) {
  138. av_log(ctx, AV_LOG_ERROR,
  139. "H and h options are incompatible and cannot be specified "
  140. "at the same time\n");
  141. hue->hue_expr = old_hue_expr;
  142. hue->hue_deg_expr = old_hue_deg_expr;
  143. return AVERROR(EINVAL);
  144. }
  145. SET_EXPRESSION(hue_deg, h);
  146. SET_EXPRESSION(hue, H);
  147. SET_EXPRESSION(saturation, s);
  148. hue->flat_syntax = 0;
  149. av_log(ctx, AV_LOG_VERBOSE,
  150. "H_expr:%s h_deg_expr:%s s_expr:%s\n",
  151. hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr);
  152. /* compatibility h:s syntax */
  153. } else {
  154. n = sscanf(args, "%f%c%f%c", &hue->hue_deg, &c1, &hue->saturation, &c2);
  155. if (n != 1 && (n != 3 || c1 != ':')) {
  156. av_log(ctx, AV_LOG_ERROR,
  157. "Invalid syntax for argument '%s': "
  158. "must be in the form 'hue[:saturation]'\n", args);
  159. return AVERROR(EINVAL);
  160. }
  161. if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
  162. av_log(ctx, AV_LOG_ERROR,
  163. "Invalid value for saturation %0.1f: "
  164. "must be included between range %d and +%d\n",
  165. hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
  166. return AVERROR(EINVAL);
  167. }
  168. hue->hue = hue->hue_deg * M_PI / 180;
  169. hue->flat_syntax = 1;
  170. av_log(ctx, AV_LOG_VERBOSE,
  171. "H:%0.1f h:%0.1f s:%0.1f\n",
  172. hue->hue, hue->hue_deg, hue->saturation);
  173. }
  174. }
  175. compute_sin_and_cos(hue);
  176. return 0;
  177. }
  178. static av_cold int init(AVFilterContext *ctx, const char *args)
  179. {
  180. HueContext *hue = ctx->priv;
  181. hue->class = &hue_class;
  182. av_opt_set_defaults(hue);
  183. hue->saturation = SAT_DEFAULT_VAL;
  184. hue->hue = HUE_DEFAULT_VAL;
  185. hue->hue_deg_pexpr = NULL;
  186. hue->hue_pexpr = NULL;
  187. hue->flat_syntax = 1;
  188. return set_options(ctx, args);
  189. }
  190. static av_cold void uninit(AVFilterContext *ctx)
  191. {
  192. HueContext *hue = ctx->priv;
  193. av_opt_free(hue);
  194. av_free(hue->hue_deg_expr);
  195. av_expr_free(hue->hue_deg_pexpr);
  196. av_free(hue->hue_expr);
  197. av_expr_free(hue->hue_pexpr);
  198. av_free(hue->saturation_expr);
  199. av_expr_free(hue->saturation_pexpr);
  200. }
  201. static int query_formats(AVFilterContext *ctx)
  202. {
  203. static const enum PixelFormat pix_fmts[] = {
  204. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  205. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  206. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  207. PIX_FMT_YUVA420P,
  208. PIX_FMT_NONE
  209. };
  210. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  211. return 0;
  212. }
  213. static int config_props(AVFilterLink *inlink)
  214. {
  215. HueContext *hue = inlink->dst->priv;
  216. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  217. hue->hsub = desc->log2_chroma_w;
  218. hue->vsub = desc->log2_chroma_h;
  219. hue->var_values[VAR_N] = 0;
  220. hue->var_values[VAR_TB] = av_q2d(inlink->time_base);
  221. hue->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
  222. NAN : av_q2d(inlink->frame_rate);
  223. return 0;
  224. }
  225. static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
  226. uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
  227. int w, int h,
  228. const int32_t c, const int32_t s)
  229. {
  230. int32_t u, v, new_u, new_v;
  231. int i;
  232. /*
  233. * If we consider U and V as the components of a 2D vector then its angle
  234. * is the hue and the norm is the saturation
  235. */
  236. while (h--) {
  237. for (i = 0; i < w; i++) {
  238. /* Normalize the components from range [16;140] to [-112;112] */
  239. u = usrc[i] - 128;
  240. v = vsrc[i] - 128;
  241. /*
  242. * Apply the rotation of the vector : (c * u) - (s * v)
  243. * (s * u) + (c * v)
  244. * De-normalize the components (without forgetting to scale 128
  245. * by << 16)
  246. * Finally scale back the result by >> 16
  247. */
  248. new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
  249. new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
  250. /* Prevent a potential overflow */
  251. udst[i] = av_clip_uint8_c(new_u);
  252. vdst[i] = av_clip_uint8_c(new_v);
  253. }
  254. usrc += src_linesize;
  255. vsrc += src_linesize;
  256. udst += dst_linesize;
  257. vdst += dst_linesize;
  258. }
  259. }
  260. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  261. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  262. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
  263. {
  264. HueContext *hue = inlink->dst->priv;
  265. AVFilterLink *outlink = inlink->dst->outputs[0];
  266. AVFilterBufferRef *buf_out;
  267. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  268. if (!outlink->out_buf)
  269. return AVERROR(ENOMEM);
  270. avfilter_copy_buffer_ref_props(outlink->out_buf, inpic);
  271. outlink->out_buf->video->w = outlink->w;
  272. outlink->out_buf->video->h = outlink->h;
  273. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  274. if (!buf_out)
  275. return AVERROR(ENOMEM);
  276. if (!hue->flat_syntax) {
  277. hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
  278. hue->var_values[VAR_PTS] = TS2D(inpic->pts);
  279. if (hue->saturation_expr) {
  280. hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
  281. if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
  282. hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
  283. av_log(inlink->dst, AV_LOG_WARNING,
  284. "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
  285. SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
  286. }
  287. }
  288. if (hue->hue_deg_expr) {
  289. hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
  290. hue->hue = hue->hue_deg * M_PI / 180;
  291. } else if (hue->hue_expr) {
  292. hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
  293. }
  294. av_log(inlink->dst, AV_LOG_DEBUG,
  295. "H:%0.1f s:%0.f t:%0.1f n:%d\n",
  296. hue->hue, hue->saturation,
  297. hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
  298. compute_sin_and_cos(hue);
  299. }
  300. hue->var_values[VAR_N] += 1;
  301. return ff_start_frame(outlink, buf_out);
  302. }
  303. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  304. {
  305. HueContext *hue = inlink->dst->priv;
  306. AVFilterBufferRef *inpic = inlink->cur_buf;
  307. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  308. uint8_t *inrow[3], *outrow[3]; // 0 : Y, 1 : U, 2 : V
  309. int plane;
  310. inrow[0] = inpic->data[0] + y * inpic->linesize[0];
  311. outrow[0] = outpic->data[0] + y * outpic->linesize[0];
  312. for (plane = 1; plane < 3; plane++) {
  313. inrow[plane] = inpic->data[plane] + (y >> hue->vsub) * inpic->linesize[plane];
  314. outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
  315. }
  316. av_image_copy_plane(outrow[0], outpic->linesize[0],
  317. inrow[0], inpic->linesize[0],
  318. inlink->w, inlink->h);
  319. process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
  320. inrow[1], inrow[2], inpic->linesize[1],
  321. inlink->w >> hue->hsub, inlink->h >> hue->vsub,
  322. hue->hue_cos, hue->hue_sin);
  323. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  324. }
  325. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  326. char *res, int res_len, int flags)
  327. {
  328. if (!strcmp(cmd, "reinit"))
  329. return set_options(ctx, args);
  330. else
  331. return AVERROR(ENOSYS);
  332. }
  333. AVFilter avfilter_vf_hue = {
  334. .name = "hue",
  335. .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
  336. .priv_size = sizeof(HueContext),
  337. .init = init,
  338. .uninit = uninit,
  339. .query_formats = query_formats,
  340. .process_command = process_command,
  341. .inputs = (const AVFilterPad[]) {
  342. {
  343. .name = "default",
  344. .type = AVMEDIA_TYPE_VIDEO,
  345. .start_frame = start_frame,
  346. .draw_slice = draw_slice,
  347. .config_props = config_props,
  348. .min_perms = AV_PERM_READ,
  349. },
  350. { .name = NULL }
  351. },
  352. .outputs = (const AVFilterPad[]) {
  353. {
  354. .name = "default",
  355. .type = AVMEDIA_TYPE_VIDEO,
  356. },
  357. { .name = NULL }
  358. },
  359. .priv_class = &hue_class,
  360. };