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.

439 lines
15KB

  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 HUE_DEFAULT_VAL 0
  36. #define SAT_DEFAULT_VAL 1
  37. #define HUE_DEFAULT_VAL_STRING AV_STRINGIFY(HUE_DEFAULT_VAL)
  38. #define SAT_DEFAULT_VAL_STRING AV_STRINGIFY(SAT_DEFAULT_VAL)
  39. #define SAT_MIN_VAL -10
  40. #define SAT_MAX_VAL 10
  41. static const char *const var_names[] = {
  42. "n", // frame count
  43. "pts", // presentation timestamp expressed in AV_TIME_BASE units
  44. "r", // frame rate
  45. "t", // timestamp expressed in seconds
  46. "tb", // timebase
  47. NULL
  48. };
  49. enum var_name {
  50. VAR_N,
  51. VAR_PTS,
  52. VAR_R,
  53. VAR_T,
  54. VAR_TB,
  55. VAR_NB
  56. };
  57. typedef struct {
  58. const AVClass *class;
  59. float hue_deg; /* hue expressed in degrees */
  60. float hue; /* hue expressed in radians */
  61. char *hue_deg_expr;
  62. char *hue_expr;
  63. AVExpr *hue_deg_pexpr;
  64. AVExpr *hue_pexpr;
  65. float saturation;
  66. char *saturation_expr;
  67. AVExpr *saturation_pexpr;
  68. int hsub;
  69. int vsub;
  70. int32_t hue_sin;
  71. int32_t hue_cos;
  72. int flat_syntax;
  73. double var_values[VAR_NB];
  74. } HueContext;
  75. #define OFFSET(x) offsetof(HueContext, x)
  76. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  77. static const AVOption hue_options[] = {
  78. { "h", "set the hue angle degrees expression", OFFSET(hue_deg_expr), AV_OPT_TYPE_STRING,
  79. { .str = NULL }, .flags = FLAGS },
  80. { "H", "set the hue angle radians expression", OFFSET(hue_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 = NULL }, .flags = FLAGS },
  84. { NULL }
  85. };
  86. AVFILTER_DEFINE_CLASS(hue);
  87. static inline void compute_sin_and_cos(HueContext *hue)
  88. {
  89. /*
  90. * Scale the value to the norm of the resulting (U,V) vector, that is
  91. * the saturation.
  92. * This will be useful in the process_chrominance function.
  93. */
  94. hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
  95. hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
  96. }
  97. #define PARSE_EXPRESSION(attr, name) \
  98. do { \
  99. if ((ret = av_expr_parse(&hue->attr##_pexpr, hue->attr##_expr, var_names, \
  100. NULL, NULL, NULL, NULL, 0, ctx)) < 0) { \
  101. av_log(ctx, AV_LOG_ERROR, \
  102. "Parsing failed for expression " #name "='%s'", \
  103. hue->attr##_expr); \
  104. hue->attr##_expr = old_##attr##_expr; \
  105. hue->attr##_pexpr = old_##attr##_pexpr; \
  106. return AVERROR(EINVAL); \
  107. } else if (old_##attr##_pexpr) { \
  108. av_freep(&old_##attr##_expr); \
  109. av_expr_free(old_##attr##_pexpr); \
  110. old_##attr##_pexpr = NULL; \
  111. } \
  112. } while (0)
  113. static inline int set_options(AVFilterContext *ctx, const char *args)
  114. {
  115. HueContext *hue = ctx->priv;
  116. int n, ret;
  117. char c1 = 0, c2 = 0;
  118. char *old_hue_expr, *old_hue_deg_expr, *old_saturation_expr;
  119. AVExpr *old_hue_pexpr, *old_hue_deg_pexpr, *old_saturation_pexpr;
  120. if (args) {
  121. /* named options syntax */
  122. if (strchr(args, '=')) {
  123. old_hue_expr = hue->hue_expr;
  124. old_hue_deg_expr = hue->hue_deg_expr;
  125. old_saturation_expr = hue->saturation_expr;
  126. old_hue_pexpr = hue->hue_pexpr;
  127. old_hue_deg_pexpr = hue->hue_deg_pexpr;
  128. old_saturation_pexpr = hue->saturation_pexpr;
  129. hue->hue_expr = NULL;
  130. hue->hue_deg_expr = NULL;
  131. hue->saturation_expr = NULL;
  132. if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
  133. return ret;
  134. if (hue->hue_expr && hue->hue_deg_expr) {
  135. av_log(ctx, AV_LOG_ERROR,
  136. "H and h options are incompatible and cannot be specified "
  137. "at the same time\n");
  138. hue->hue_expr = old_hue_expr;
  139. hue->hue_deg_expr = old_hue_deg_expr;
  140. return AVERROR(EINVAL);
  141. }
  142. /*
  143. * if both 'H' and 'h' options have not been specified, restore the
  144. * old values
  145. */
  146. if (!hue->hue_expr && !hue->hue_deg_expr) {
  147. hue->hue_expr = old_hue_expr;
  148. hue->hue_deg_expr = old_hue_deg_expr;
  149. }
  150. if (hue->hue_deg_expr)
  151. PARSE_EXPRESSION(hue_deg, h);
  152. if (hue->hue_expr)
  153. PARSE_EXPRESSION(hue, H);
  154. if (hue->saturation_expr)
  155. PARSE_EXPRESSION(saturation, s);
  156. hue->flat_syntax = 0;
  157. av_log(ctx, AV_LOG_VERBOSE,
  158. "H_expr:%s h_deg_expr:%s s_expr:%s\n",
  159. hue->hue_expr, hue->hue_deg_expr, hue->saturation_expr);
  160. /* compatibility h:s syntax */
  161. } else {
  162. n = sscanf(args, "%f%c%f%c", &hue->hue_deg, &c1, &hue->saturation, &c2);
  163. if (n != 1 && (n != 3 || c1 != ':')) {
  164. av_log(ctx, AV_LOG_ERROR,
  165. "Invalid syntax for argument '%s': "
  166. "must be in the form 'hue[:saturation]'\n", args);
  167. return AVERROR(EINVAL);
  168. }
  169. if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
  170. av_log(ctx, AV_LOG_ERROR,
  171. "Invalid value for saturation %0.1f: "
  172. "must be included between range %d and +%d\n",
  173. hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
  174. return AVERROR(EINVAL);
  175. }
  176. hue->hue = hue->hue_deg * M_PI / 180;
  177. hue->flat_syntax = 1;
  178. av_log(ctx, AV_LOG_VERBOSE,
  179. "H:%0.1f h:%0.1f s:%0.1f\n",
  180. hue->hue, hue->hue_deg, hue->saturation);
  181. }
  182. }
  183. compute_sin_and_cos(hue);
  184. return 0;
  185. }
  186. static av_cold int init(AVFilterContext *ctx, const char *args)
  187. {
  188. HueContext *hue = ctx->priv;
  189. hue->class = &hue_class;
  190. av_opt_set_defaults(hue);
  191. hue->saturation = SAT_DEFAULT_VAL;
  192. hue->hue = HUE_DEFAULT_VAL;
  193. hue->hue_deg_pexpr = NULL;
  194. hue->hue_pexpr = NULL;
  195. hue->flat_syntax = 1;
  196. return set_options(ctx, args);
  197. }
  198. static av_cold void uninit(AVFilterContext *ctx)
  199. {
  200. HueContext *hue = ctx->priv;
  201. av_opt_free(hue);
  202. av_free(hue->hue_deg_expr);
  203. av_expr_free(hue->hue_deg_pexpr);
  204. av_free(hue->hue_expr);
  205. av_expr_free(hue->hue_pexpr);
  206. av_free(hue->saturation_expr);
  207. av_expr_free(hue->saturation_pexpr);
  208. }
  209. static int query_formats(AVFilterContext *ctx)
  210. {
  211. static const enum PixelFormat pix_fmts[] = {
  212. PIX_FMT_YUV444P, PIX_FMT_YUV422P,
  213. PIX_FMT_YUV420P, PIX_FMT_YUV411P,
  214. PIX_FMT_YUV410P, PIX_FMT_YUV440P,
  215. PIX_FMT_YUVA420P,
  216. PIX_FMT_NONE
  217. };
  218. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  219. return 0;
  220. }
  221. static int config_props(AVFilterLink *inlink)
  222. {
  223. HueContext *hue = inlink->dst->priv;
  224. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
  225. hue->hsub = desc->log2_chroma_w;
  226. hue->vsub = desc->log2_chroma_h;
  227. hue->var_values[VAR_N] = 0;
  228. hue->var_values[VAR_TB] = av_q2d(inlink->time_base);
  229. hue->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
  230. NAN : av_q2d(inlink->frame_rate);
  231. return 0;
  232. }
  233. static void process_chrominance(uint8_t *udst, uint8_t *vdst, const int dst_linesize,
  234. uint8_t *usrc, uint8_t *vsrc, const int src_linesize,
  235. int w, int h,
  236. const int32_t c, const int32_t s)
  237. {
  238. int32_t u, v, new_u, new_v;
  239. int i;
  240. /*
  241. * If we consider U and V as the components of a 2D vector then its angle
  242. * is the hue and the norm is the saturation
  243. */
  244. while (h--) {
  245. for (i = 0; i < w; i++) {
  246. /* Normalize the components from range [16;140] to [-112;112] */
  247. u = usrc[i] - 128;
  248. v = vsrc[i] - 128;
  249. /*
  250. * Apply the rotation of the vector : (c * u) - (s * v)
  251. * (s * u) + (c * v)
  252. * De-normalize the components (without forgetting to scale 128
  253. * by << 16)
  254. * Finally scale back the result by >> 16
  255. */
  256. new_u = ((c * u) - (s * v) + (1 << 15) + (128 << 16)) >> 16;
  257. new_v = ((s * u) + (c * v) + (1 << 15) + (128 << 16)) >> 16;
  258. /* Prevent a potential overflow */
  259. udst[i] = av_clip_uint8_c(new_u);
  260. vdst[i] = av_clip_uint8_c(new_v);
  261. }
  262. usrc += src_linesize;
  263. vsrc += src_linesize;
  264. udst += dst_linesize;
  265. vdst += dst_linesize;
  266. }
  267. }
  268. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  269. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  270. static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpic)
  271. {
  272. HueContext *hue = inlink->dst->priv;
  273. AVFilterLink *outlink = inlink->dst->outputs[0];
  274. AVFilterBufferRef *buf_out;
  275. outlink->out_buf = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  276. if (!outlink->out_buf)
  277. return AVERROR(ENOMEM);
  278. avfilter_copy_buffer_ref_props(outlink->out_buf, inpic);
  279. outlink->out_buf->video->w = outlink->w;
  280. outlink->out_buf->video->h = outlink->h;
  281. buf_out = avfilter_ref_buffer(outlink->out_buf, ~0);
  282. if (!buf_out)
  283. return AVERROR(ENOMEM);
  284. if (!hue->flat_syntax) {
  285. hue->var_values[VAR_T] = TS2T(inpic->pts, inlink->time_base);
  286. hue->var_values[VAR_PTS] = TS2D(inpic->pts);
  287. if (hue->saturation_expr) {
  288. hue->saturation = av_expr_eval(hue->saturation_pexpr, hue->var_values, NULL);
  289. if (hue->saturation < SAT_MIN_VAL || hue->saturation > SAT_MAX_VAL) {
  290. hue->saturation = av_clip(hue->saturation, SAT_MIN_VAL, SAT_MAX_VAL);
  291. av_log(inlink->dst, AV_LOG_WARNING,
  292. "Saturation value not in range [%d,%d]: clipping value to %0.1f\n",
  293. SAT_MIN_VAL, SAT_MAX_VAL, hue->saturation);
  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. }
  302. av_log(inlink->dst, AV_LOG_DEBUG,
  303. "H:%0.1f s:%0.f t:%0.1f n:%d\n",
  304. hue->hue, hue->saturation,
  305. hue->var_values[VAR_T], (int)hue->var_values[VAR_N]);
  306. compute_sin_and_cos(hue);
  307. }
  308. hue->var_values[VAR_N] += 1;
  309. return ff_start_frame(outlink, buf_out);
  310. }
  311. static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
  312. {
  313. HueContext *hue = inlink->dst->priv;
  314. AVFilterBufferRef *inpic = inlink->cur_buf;
  315. AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
  316. uint8_t *inrow[3], *outrow[3]; // 0 : Y, 1 : U, 2 : V
  317. int plane;
  318. inrow[0] = inpic->data[0] + y * inpic->linesize[0];
  319. outrow[0] = outpic->data[0] + y * outpic->linesize[0];
  320. for (plane = 1; plane < 3; plane++) {
  321. inrow[plane] = inpic->data[plane] + (y >> hue->vsub) * inpic->linesize[plane];
  322. outrow[plane] = outpic->data[plane] + (y >> hue->vsub) * outpic->linesize[plane];
  323. }
  324. av_image_copy_plane(outrow[0], outpic->linesize[0],
  325. inrow[0], inpic->linesize[0],
  326. inlink->w, inlink->h);
  327. process_chrominance(outrow[1], outrow[2], outpic->linesize[1],
  328. inrow[1], inrow[2], inpic->linesize[1],
  329. inlink->w >> hue->hsub, inlink->h >> hue->vsub,
  330. hue->hue_cos, hue->hue_sin);
  331. return ff_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
  332. }
  333. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  334. char *res, int res_len, int flags)
  335. {
  336. if (!strcmp(cmd, "reinit"))
  337. return set_options(ctx, args);
  338. else
  339. return AVERROR(ENOSYS);
  340. }
  341. AVFilter avfilter_vf_hue = {
  342. .name = "hue",
  343. .description = NULL_IF_CONFIG_SMALL("Adjust the hue and saturation of the input video."),
  344. .priv_size = sizeof(HueContext),
  345. .init = init,
  346. .uninit = uninit,
  347. .query_formats = query_formats,
  348. .process_command = process_command,
  349. .inputs = (const AVFilterPad[]) {
  350. {
  351. .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. .start_frame = start_frame,
  354. .draw_slice = draw_slice,
  355. .config_props = config_props,
  356. .min_perms = AV_PERM_READ,
  357. },
  358. { .name = NULL }
  359. },
  360. .outputs = (const AVFilterPad[]) {
  361. {
  362. .name = "default",
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. },
  365. { .name = NULL }
  366. },
  367. .priv_class = &hue_class,
  368. };