You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

450 lines
14KB

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