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.

354 lines
11KB

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