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.

532 lines
17KB

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