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.

378 lines
12KB

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