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.

353 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_YUVA420P,
  149. AV_PIX_FMT_NONE
  150. };
  151. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  152. return 0;
  153. }
  154. static int config_props(AVFilterLink *inlink)
  155. {
  156. HueContext *hue = inlink->dst->priv;
  157. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  158. hue->hsub = desc->log2_chroma_w;
  159. hue->vsub = desc->log2_chroma_h;
  160. hue->var_values[VAR_N] = 0;
  161. hue->var_values[VAR_TB] = av_q2d(inlink->time_base);
  162. hue->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
  163. NAN : av_q2d(inlink->frame_rate);
  164. return 0;
  165. }
  166. static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
  167. uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
  168. int w, int h,
  169. const int32_t c, const int32_t s)
  170. {
  171. int32_t u, v, new_u, new_v;
  172. int i;
  173. /*
  174. * If we consider U and V as the components of a 2D vector then its angle
  175. * is the hue and the norm is the saturation
  176. */
  177. while (h--) {
  178. for (i = 0; i < w; i++) {
  179. /* Normalize the components from range [16;140] to [-112;112] */
  180. u = usrc[i] - 128;
  181. v = vsrc[i] - 128;
  182. /*
  183. * Apply the rotation of the vector : (c * u) - (s * v)
  184. * (s * u) + (c * v)
  185. * De-normalize the components (without forgetting to scale 128
  186. * by << 16)
  187. * Finally scale back the result by >> 16
  188. */
  189. new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
  190. new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
  191. /* Prevent a potential overflow */
  192. udst[i] = av_clip_uint8_c(new_u);
  193. vdst[i] = av_clip_uint8_c(new_v);
  194. }
  195. usrc += src_linesize;
  196. vsrc += src_linesize;
  197. udst += dst_linesize;
  198. vdst += dst_linesize;
  199. }
  200. }
  201. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  202. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  203. static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
  204. {
  205. HueContext *hue = inlink->dst->priv;
  206. AVFilterLink *outlink = inlink->dst->outputs[0];
  207. AVFrame *outpic;
  208. int direct = 0;
  209. if (av_frame_is_writable(inpic)) {
  210. direct = 1;
  211. outpic = inpic;
  212. } else {
  213. outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  214. if (!outpic) {
  215. av_frame_free(&inpic);
  216. return AVERROR(ENOMEM);
  217. }
  218. av_frame_copy_props(outpic, inpic);
  219. }
  220. hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
  221. hue->var_values[VAR_PTS] = TS2D(inpic->pts);
  222. if (hue->saturation_expr) {
  223. hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
  224. if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
  225. hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
  226. av_log(inlink->dst, AV_LOG_WARNING,
  227. "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
  228. SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
  229. }
  230. }
  231. if (hue->hue_deg_expr) {
  232. hue->hue_deg = av_expr_eval(hue->hue_deg_pexpr, hue->var_values, NULL);
  233. hue->hue = hue->hue_deg * M_PI / 180;
  234. } else if (hue->hue_expr) {
  235. hue->hue = av_expr_eval(hue->hue_pexpr, hue->var_values, NULL);
  236. hue->hue_deg = hue->hue * 180 / M_PI;
  237. }
  238. av_log(inlink->dst, AV_LOG_DEBUG,
  239. "H:%0.1f*PI h:%0.1f s:%0.f t:%0.1f n:%d\n",
  240. hue->hue/M_PI, hue->hue_deg, hue->saturation,
  241. hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
  242. compute_sin_and_cos(hue);
  243. hue->var_values[VAR_N] += 1;
  244. if (!direct) {
  245. av_image_copy_plane(outpic->data[0], outpic->linesize[0],
  246. inpic->data[0], inpic->linesize[0],
  247. inlink->w, inlink->h);
  248. if (inpic->data[3])
  249. av_image_copy_plane(outpic->data[3], outpic->linesize[3],
  250. inpic->data[3], inpic->linesize[3],
  251. inlink->w, inlink->h);
  252. }
  253. process_chrominance(outpic->data[1], outpic->data[2], outpic->linesize[1],
  254. inpic->data[1], inpic->data[2], inpic->linesize[1],
  255. inlink->w >> hue->hsub, inlink->h >> hue->vsub,
  256. hue->hue_cos, hue->hue_sin);
  257. if (!direct)
  258. av_frame_free(&inpic);
  259. return ff_filter_frame(outlink, outpic);
  260. }
  261. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  262. char *res, int res_len, int flags)
  263. {
  264. HueContext *hue = ctx->priv;
  265. #define SET_CMD(expr, option) do { \
  266. if (!strcmp(cmd, option)) \
  267. return set_expr(&hue->expr##_pexpr, args, cmd, ctx); \
  268. } while (0)
  269. SET_CMD(hue_deg, "h");
  270. SET_CMD(hue, "H");
  271. SET_CMD(saturation, "s");
  272. return AVERROR(ENOSYS);
  273. }
  274. static const AVFilterPad hue_inputs[] = {
  275. {
  276. .name = "default",
  277. .type = AVMEDIA_TYPE_VIDEO,
  278. .filter_frame = filter_frame,
  279. .config_props = config_props,
  280. },
  281. { NULL }
  282. };
  283. static const AVFilterPad hue_outputs[] = {
  284. {
  285. .name = "default",
  286. .type = AVMEDIA_TYPE_VIDEO,
  287. },
  288. { NULL }
  289. };
  290. AVFilter avfilter_vf_hue = {
  291. .name = "hue",
  292. .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
  293. .priv_size = sizeof(HueContext),
  294. .init = init,
  295. .uninit = uninit,
  296. .query_formats = query_formats,
  297. .process_command = process_command,
  298. .inputs = hue_inputs,
  299. .outputs = hue_outputs,
  300. .priv_class = &hue_class,
  301. };