vf_hue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 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 SAT_MIN_VAL -10
  36. #define SAT_MAX_VAL 10
  37. static const char *const var_names[] = {
  38. "n", // frame count
  39. "pts", // presentation timestamp expressed in AV_TIME_BASE units
  40. "r", // frame rate
  41. "t", // timestamp expressed in seconds
  42. "tb", // timebase
  43. NULL
  44. };
  45. enum var_name {
  46. VAR_N,
  47. VAR_PTS,
  48. VAR_R,
  49. VAR_T,
  50. VAR_TB,
  51. VAR_NB
  52. };
  53. typedef struct {
  54. const AVClass *class;
  55. float hue_deg; /* hue expressed in degrees */
  56. float hue; /* hue expressed in radians */
  57. char *hue_deg_expr;
  58. char *hue_expr;
  59. AVExpr *hue_deg_pexpr;
  60. AVExpr *hue_pexpr;
  61. float saturation;
  62. char *saturation_expr;
  63. AVExpr *saturation_pexpr;
  64. float brightness;
  65. char *brightness_expr;
  66. AVExpr *brightness_pexpr;
  67. int hsub;
  68. int vsub;
  69. int is_first;
  70. int32_t hue_sin;
  71. int32_t hue_cos;
  72. double var_values[VAR_NB];
  73. uint8_t lut_l[256];
  74. uint8_t lut_u[256][256];
  75. uint8_t lut_v[256][256];
  76. } HueContext;
  77. #define OFFSET(x) offsetof(HueContext, x)
  78. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  79. static const AVOption hue_options[] = {
  80. { "h", "set the hue angle degrees expression", OFFSET(hue_deg_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 = "1" }, .flags = FLAGS },
  84. { "H", "set the hue angle radians expression", OFFSET(hue_expr), AV_OPT_TYPE_STRING,
  85. { .str = NULL }, .flags = FLAGS },
  86. { "b", "set the brightness expression", OFFSET(brightness_expr), AV_OPT_TYPE_STRING,
  87. { .str = "0" }, .flags = FLAGS },
  88. { NULL }
  89. };
  90. AVFILTER_DEFINE_CLASS(hue);
  91. static inline void compute_sin_and_cos(HueContext *hue)
  92. {
  93. /*
  94. * Scale the value to the norm of the resulting (U,V) vector, that is
  95. * the saturation.
  96. * This will be useful in the apply_lut function.
  97. */
  98. hue->hue_sin = lrint(sin(hue->hue) * (1 << 16) * hue->saturation);
  99. hue->hue_cos = lrint(cos(hue->hue) * (1 << 16) * hue->saturation);
  100. }
  101. static inline void create_luma_lut(HueContext *h)
  102. {
  103. const float b = h->brightness;
  104. int i;
  105. for (i = 0; i < 256; i++) {
  106. h->lut_l[i] = av_clip_uint8(i + b * 25.5);
  107. }
  108. }
  109. static inline void create_chrominance_lut(HueContext *h, const int32_t c,
  110. const int32_t s)
  111. {
  112. int32_t i, j, u, v, new_u, new_v;
  113. /*
  114. * If we consider U and V as the components of a 2D vector then its angle
  115. * is the hue and the norm is the saturation
  116. */
  117. for (i = 0; i < 256; i++) {
  118. for (j = 0; j < 256; j++) {
  119. /* Normalize the components from range [16;140] to [-112;112] */
  120. u = i - 128;
  121. v = j - 128;
  122. /*
  123. * Apply the rotation of the vector : (c * u) - (s * v)
  124. * (s * u) + (c * v)
  125. * De-normalize the components (without forgetting to scale 128
  126. * by << 16)
  127. * Finally scale back the result by >> 16
  128. */
  129. new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
  130. new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
  131. /* Prevent a potential overflow */
  132. h->lut_u[i][j] = av_clip_uint8(new_u);
  133. h->lut_v[i][j] = av_clip_uint8(new_v);
  134. }
  135. }
  136. }
  137. static int set_expr(AVExpr **pexpr_ptr, char **expr_ptr,
  138. const char *expr, const char *option, void *log_ctx)
  139. {
  140. int ret;
  141. AVExpr *new_pexpr;
  142. char *new_expr;
  143. new_expr = av_strdup(expr);
  144. if (!new_expr)
  145. return AVERROR(ENOMEM);
  146. ret = av_expr_parse(&new_pexpr, expr, var_names,
  147. NULL, NULL, NULL, NULL, 0, log_ctx);
  148. if (ret < 0) {
  149. av_log(log_ctx, AV_LOG_ERROR,
  150. "Error when evaluating the expression '%s' for %s\n",
  151. expr, option);
  152. av_free(new_expr);
  153. return ret;
  154. }
  155. if (*pexpr_ptr)
  156. av_expr_free(*pexpr_ptr);
  157. *pexpr_ptr = new_pexpr;
  158. av_freep(expr_ptr);
  159. *expr_ptr = new_expr;
  160. return 0;
  161. }
  162. static av_cold int init(AVFilterContext *ctx)
  163. {
  164. HueContext *hue = ctx->priv;
  165. int ret;
  166. if (hue->hue_expr && hue->hue_deg_expr) {
  167. av_log(ctx, AV_LOG_ERROR,
  168. "H and h options are incompatible and cannot be specified "
  169. "at the same time\n");
  170. return AVERROR(EINVAL);
  171. }
  172. #define SET_EXPR(expr, option) \
  173. if (hue->expr##_expr) do { \
  174. ret = set_expr(&hue->expr##_pexpr, &hue->expr##_expr, \
  175. hue->expr##_expr, option, ctx); \
  176. if (ret < 0) \
  177. return ret; \
  178. } while (0)
  179. SET_EXPR(brightness, "b");
  180. SET_EXPR(saturation, "s");
  181. SET_EXPR(hue_deg, "h");
  182. SET_EXPR(hue, "H");
  183. #undef SET_EXPR
  184. av_log(ctx, AV_LOG_VERBOSE,
  185. "H_expr:%s h_deg_expr:%s s_expr:%s b_expr:%s\n",
  186. hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr, hue->brightness_expr);
  187. compute_sin_and_cos(hue);
  188. hue->is_first = 1;
  189. return 0;
  190. }
  191. static av_cold void uninit(AVFilterContext *ctx)
  192. {
  193. HueContext *hue = ctx->priv;
  194. av_expr_free(hue->brightness_pexpr);
  195. av_expr_free(hue->hue_deg_pexpr);
  196. av_expr_free(hue->hue_pexpr);
  197. av_expr_free(hue->saturation_pexpr);
  198. }
  199. static int query_formats(AVFilterContext *ctx)
  200. {
  201. static const enum AVPixelFormat pix_fmts[] = {
  202. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  203. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  204. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  205. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P,
  206. AV_PIX_FMT_YUVA420P,
  207. AV_PIX_FMT_NONE
  208. };
  209. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  210. if (!fmts_list)
  211. return AVERROR(ENOMEM);
  212. return ff_set_common_formats(ctx, fmts_list);
  213. }
  214. static int config_props(AVFilterLink *inlink)
  215. {
  216. HueContext *hue = inlink->dst->priv;
  217. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  218. hue->hsub = desc->log2_chroma_w;
  219. hue->vsub = desc->log2_chroma_h;
  220. hue->var_values[VAR_N] = 0;
  221. hue->var_values[VAR_TB] = av_q2d(inlink->time_base);
  222. hue->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
  223. NAN : av_q2d(inlink->frame_rate);
  224. return 0;
  225. }
  226. static void apply_luma_lut(HueContext *s,
  227. uint8_t *ldst, const int dst_linesize,
  228. uint8_t *lsrc, const int src_linesize,
  229. int w, int h)
  230. {
  231. int i;
  232. while (h--) {
  233. for (i = 0; i < w; i++)
  234. ldst[i] = s->lut_l[lsrc[i]];
  235. lsrc += src_linesize;
  236. ldst += dst_linesize;
  237. }
  238. }
  239. static void apply_lut(HueContext *s,
  240. uint8_t *udst, uint8_t *vdst, const int dst_linesize,
  241. uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
  242. int w, int h)
  243. {
  244. int i;
  245. while (h--) {
  246. for (i = 0; i < w; i++) {
  247. const int u = usrc[i];
  248. const int v = vsrc[i];
  249. udst[i] = s->lut_u[u][v];
  250. vdst[i] = s->lut_v[u][v];
  251. }
  252. usrc += src_linesize;
  253. vsrc += src_linesize;
  254. udst += dst_linesize;
  255. vdst += dst_linesize;
  256. }
  257. }
  258. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  259. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  260. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  261. {
  262. HueContext *hue = inlink->dst->priv;
  263. AVFilterLink *outlink = inlink->dst->outputs[0];
  264. AVFrame *outpic;
  265. const int32_t old_hue_sin = hue->hue_sin, old_hue_cos = hue->hue_cos;
  266. const float old_brightness = hue->brightness;
  267. int direct = 0;
  268. if (av_frame_is_writable(inpic)) {
  269. direct = 1;
  270. outpic = inpic;
  271. } else {
  272. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  273. if (!outpic) {
  274. av_frame_free(&inpic);
  275. return AVERROR(ENOMEM);
  276. }
  277. av_frame_copy_props(outpic, inpic);
  278. }
  279. hue->var_values[VAR_N] = inlink->frame_count;
  280. hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
  281. hue->var_values[VAR_PTS] = TS2D(inpic->pts);
  282. if (hue->saturation_expr) {
  283. hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
  284. if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
  285. hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
  286. av_log(inlink->dst, AV_LOG_WARNING,
  287. "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
  288. SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
  289. }
  290. }
  291. if (hue->brightness_expr) {
  292. hue->brightness = av_expr_eval(hue->brightness_pexpr, hue->var_values, NULL);
  293. if (hue->brightness < -10 || hue->brightness > 10) {
  294. hue->brightness = av_clipf(hue->brightness, -10, 10);
  295. av_log(inlink->dst, AV_LOG_WARNING,
  296. "Brightness value not in range [%d,%d]: clipping value to %0.1f\n",
  297. -10, 10, hue->brightness);
  298. }
  299. }
  300. if (hue->hue_deg_expr) {
  301. hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
  302. hue->hue = hue->hue_deg * M_PI / 180;
  303. } else if (hue->hue_expr) {
  304. hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
  305. hue->hue_deg = hue->hue * 180 / M_PI;
  306. }
  307. av_log(inlink->dst, AV_LOG_DEBUG,
  308. "H:%0.1f*PI h:%0.1f s:%0.1f b:%0.f t:%0.1f n:%d\n",
  309. hue->hue/M_PI, hue->hue_deg, hue->saturation, hue->brightness,
  310. hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
  311. compute_sin_and_cos(hue);
  312. if (hue->is_first || (old_hue_sin != hue->hue_sin || old_hue_cos != hue->hue_cos))
  313. create_chrominance_lut(hue, hue->hue_cos, hue->hue_sin);
  314. if (hue->is_first || (old_brightness != hue->brightness && hue->brightness))
  315. create_luma_lut(hue);
  316. if (!direct) {
  317. if (!hue->brightness)
  318. av_image_copy_plane(outpic->data[0], outpic->linesize[0],
  319. inpic->data[0], inpic->linesize[0],
  320. inlink->w, inlink->h);
  321. if (inpic->data[3])
  322. av_image_copy_plane(outpic->data[3], outpic->linesize[3],
  323. inpic->data[3], inpic->linesize[3],
  324. inlink->w, inlink->h);
  325. }
  326. apply_lut(hue, outpic->data[1], outpic->data[2], outpic->linesize[1],
  327. inpic->data[1], inpic->data[2], inpic->linesize[1],
  328. AV_CEIL_RSHIFT(inlink->w, hue->hsub),
  329. AV_CEIL_RSHIFT(inlink->h, hue->vsub));
  330. if (hue->brightness)
  331. apply_luma_lut(hue, outpic->data[0], outpic->linesize[0],
  332. inpic->data[0], inpic->linesize[0], inlink->w, inlink->h);
  333. if (!direct)
  334. av_frame_free(&inpic);
  335. hue->is_first = 0;
  336. return ff_filter_frame(outlink, outpic);
  337. }
  338. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  339. char *res, int res_len, int flags)
  340. {
  341. HueContext *hue = ctx->priv;
  342. int ret;
  343. #define SET_EXPR(expr, option) \
  344. do { \
  345. ret = set_expr(&hue->expr##_pexpr, &hue->expr##_expr, \
  346. args, option, ctx); \
  347. if (ret < 0) \
  348. return ret; \
  349. } while (0)
  350. if (!strcmp(cmd, "h")) {
  351. SET_EXPR(hue_deg, "h");
  352. av_freep(&hue->hue_expr);
  353. } else if (!strcmp(cmd, "H")) {
  354. SET_EXPR(hue, "H");
  355. av_freep(&hue->hue_deg_expr);
  356. } else if (!strcmp(cmd, "s")) {
  357. SET_EXPR(saturation, "s");
  358. } else if (!strcmp(cmd, "b")) {
  359. SET_EXPR(brightness, "b");
  360. } else
  361. return AVERROR(ENOSYS);
  362. return 0;
  363. }
  364. static const AVFilterPad hue_inputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .filter_frame = filter_frame,
  369. .config_props = config_props,
  370. },
  371. { NULL }
  372. };
  373. static const AVFilterPad hue_outputs[] = {
  374. {
  375. .name = "default",
  376. .type = AVMEDIA_TYPE_VIDEO,
  377. },
  378. { NULL }
  379. };
  380. AVFilter ff_vf_hue = {
  381. .name = "hue",
  382. .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
  383. .priv_size = sizeof(HueContext),
  384. .init = init,
  385. .uninit = uninit,
  386. .query_formats = query_formats,
  387. .process_command = process_command,
  388. .inputs = hue_inputs,
  389. .outputs = hue_outputs,
  390. .priv_class = &hue_class,
  391. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  392. };