vf_selectivecolor.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 2015 Clément Bœsch <u pkh me>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @todo
  22. * - use integers so it can be made bitexact and a FATE test can be added
  23. * - >8 bit support
  24. */
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/file.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/opt.h"
  29. #include "avfilter.h"
  30. #include "formats.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. enum color_range {
  34. // WARNING: do NOT reorder (see parse_psfile())
  35. RANGE_REDS,
  36. RANGE_YELLOWS,
  37. RANGE_GREENS,
  38. RANGE_CYANS,
  39. RANGE_BLUES,
  40. RANGE_MAGENTAS,
  41. RANGE_WHITES,
  42. RANGE_NEUTRALS,
  43. RANGE_BLACKS,
  44. NB_RANGES
  45. };
  46. enum correction_method {
  47. CORRECTION_METHOD_ABSOLUTE,
  48. CORRECTION_METHOD_RELATIVE,
  49. NB_CORRECTION_METHODS,
  50. };
  51. static const char *color_names[NB_RANGES] = {
  52. "red", "yellow", "green", "cyan", "blue", "magenta", "white", "neutral", "black"
  53. };
  54. typedef int (*get_adjust_range_func)(int r, int g, int b, int min_val, int max_val);
  55. struct process_range {
  56. int range_id;
  57. uint32_t mask;
  58. get_adjust_range_func get_adjust_range;
  59. };
  60. typedef struct ThreadData {
  61. AVFrame *in, *out;
  62. } ThreadData;
  63. typedef struct {
  64. const AVClass *class;
  65. int correction_method;
  66. char *opt_cmyk_adjust[NB_RANGES];
  67. float cmyk_adjust[NB_RANGES][4];
  68. struct process_range process_ranges[NB_RANGES]; // color ranges to process
  69. int nb_process_ranges;
  70. char *psfile;
  71. } SelectiveColorContext;
  72. #define OFFSET(x) offsetof(SelectiveColorContext, x)
  73. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  74. #define RANGE_OPTION(color_name, range) \
  75. { color_name"s", "adjust "color_name" regions", OFFSET(opt_cmyk_adjust[range]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS }
  76. static const AVOption selectivecolor_options[] = {
  77. { "correction_method", "select correction method", OFFSET(correction_method), AV_OPT_TYPE_INT, {.i64 = CORRECTION_METHOD_ABSOLUTE}, 0, NB_CORRECTION_METHODS-1, FLAGS, "correction_method" },
  78. { "absolute", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CORRECTION_METHOD_ABSOLUTE}, INT_MIN, INT_MAX, FLAGS, "correction_method" },
  79. { "relative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CORRECTION_METHOD_RELATIVE}, INT_MIN, INT_MAX, FLAGS, "correction_method" },
  80. RANGE_OPTION("red", RANGE_REDS),
  81. RANGE_OPTION("yellow", RANGE_YELLOWS),
  82. RANGE_OPTION("green", RANGE_GREENS),
  83. RANGE_OPTION("cyan", RANGE_CYANS),
  84. RANGE_OPTION("blue", RANGE_BLUES),
  85. RANGE_OPTION("magenta", RANGE_MAGENTAS),
  86. RANGE_OPTION("white", RANGE_WHITES),
  87. RANGE_OPTION("neutral", RANGE_NEUTRALS),
  88. RANGE_OPTION("black", RANGE_BLACKS),
  89. { "psfile", "set Photoshop selectivecolor file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  90. { NULL }
  91. };
  92. AVFILTER_DEFINE_CLASS(selectivecolor);
  93. static inline int get_mid_val(int r, int g, int b)
  94. {
  95. if ((r < g && r > b) || (r < b && r > g)) return r;
  96. if ((g < r && g > b) || (g < b && g > r)) return g;
  97. if ((b < r && b > g) || (b < g && b > r)) return b;
  98. return -1;
  99. }
  100. static int get_rgb_adjust_range(int r, int g, int b, int min_val, int max_val)
  101. {
  102. // max - mid
  103. const int mid_val = get_mid_val(r, g, b);
  104. if (mid_val == -1) {
  105. // XXX: can be simplified
  106. if ((r != min_val && g == min_val && b == min_val) ||
  107. (r == min_val && g != min_val && b == min_val) ||
  108. (r == min_val && g == min_val && b != min_val))
  109. return max_val - min_val;
  110. return 0;
  111. }
  112. return max_val - mid_val;
  113. }
  114. static int get_cmy_adjust_range(int r, int g, int b, int min_val, int max_val)
  115. {
  116. // mid - min
  117. const int mid_val = get_mid_val(r, g, b);
  118. if (mid_val == -1) {
  119. // XXX: refactor with rgb
  120. if ((r != max_val && g == max_val && b == max_val) ||
  121. (r == max_val && g != max_val && b == max_val) ||
  122. (r == max_val && g == max_val && b != max_val))
  123. return max_val - min_val;
  124. return 0;
  125. }
  126. return mid_val - min_val;
  127. }
  128. static int get_neutrals_adjust_range(int r, int g, int b, int min_val, int max_val)
  129. {
  130. // 1 - (|max-0.5| + |min-0.5|)
  131. return (255*2 - (abs((max_val<<1) - 255) + abs((min_val<<1) - 255)) + 1) >> 1;
  132. }
  133. static int get_whites_adjust_range(int r, int g, int b, int min_val, int max_val)
  134. {
  135. // (min - 0.5) * 2
  136. return (min_val<<1) - 255;
  137. }
  138. static int get_blacks_adjust_range(int r, int g, int b, int min_val, int max_val)
  139. {
  140. // (0.5 - max) * 2
  141. return 255 - (max_val<<1);
  142. }
  143. static int register_range(SelectiveColorContext *s, int range_id)
  144. {
  145. const float *cmyk = s->cmyk_adjust[range_id];
  146. /* If the color range has user settings, register the color range
  147. * as "to be processed" */
  148. if (cmyk[0] || cmyk[1] || cmyk[2] || cmyk[3]) {
  149. struct process_range *pr = &s->process_ranges[s->nb_process_ranges++];
  150. if (cmyk[0] < -1.0 || cmyk[0] > 1.0 ||
  151. cmyk[1] < -1.0 || cmyk[1] > 1.0 ||
  152. cmyk[2] < -1.0 || cmyk[2] > 1.0 ||
  153. cmyk[3] < -1.0 || cmyk[3] > 1.0) {
  154. av_log(s, AV_LOG_ERROR, "Invalid %s adjustments (%g %g %g %g). "
  155. "Settings must be set in [-1;1] range\n",
  156. color_names[range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
  157. return AVERROR(EINVAL);
  158. }
  159. pr->range_id = range_id;
  160. pr->mask = 1 << range_id;
  161. if (pr->mask & (1<<RANGE_REDS | 1<<RANGE_GREENS | 1<<RANGE_BLUES)) pr->get_adjust_range = get_rgb_adjust_range;
  162. else if (pr->mask & (1<<RANGE_CYANS | 1<<RANGE_MAGENTAS | 1<<RANGE_YELLOWS)) pr->get_adjust_range = get_cmy_adjust_range;
  163. else if (pr->mask & 1<<RANGE_WHITES) pr->get_adjust_range = get_whites_adjust_range;
  164. else if (pr->mask & 1<<RANGE_NEUTRALS) pr->get_adjust_range = get_neutrals_adjust_range;
  165. else if (pr->mask & 1<<RANGE_BLACKS) pr->get_adjust_range = get_blacks_adjust_range;
  166. else
  167. av_assert0(0);
  168. }
  169. return 0;
  170. }
  171. static int parse_psfile(AVFilterContext *ctx, const char *fname)
  172. {
  173. int16_t val;
  174. int ret, i, version;
  175. uint8_t *buf;
  176. size_t size;
  177. SelectiveColorContext *s = ctx->priv;
  178. ret = av_file_map(fname, &buf, &size, 0, NULL);
  179. if (ret < 0)
  180. return ret;
  181. #define READ16(dst) do { \
  182. if (size < 2) { \
  183. ret = AVERROR_INVALIDDATA; \
  184. goto end; \
  185. } \
  186. dst = AV_RB16(buf); \
  187. buf += 2; \
  188. size -= 2; \
  189. } while (0)
  190. READ16(version);
  191. if (version != 1)
  192. av_log(s, AV_LOG_WARNING, "Unsupported selective color file version %d, "
  193. "the settings might not be loaded properly\n", version);
  194. READ16(s->correction_method);
  195. // 1st CMYK entry is reserved/unused
  196. for (i = 0; i < FF_ARRAY_ELEMS(s->cmyk_adjust[0]); i++) {
  197. READ16(val);
  198. if (val)
  199. av_log(s, AV_LOG_WARNING, "%c value of first CMYK entry is not 0 "
  200. "but %d\n", "CMYK"[i], val);
  201. }
  202. for (i = 0; i < FF_ARRAY_ELEMS(s->cmyk_adjust); i++) {
  203. int k;
  204. for (k = 0; k < FF_ARRAY_ELEMS(s->cmyk_adjust[0]); k++) {
  205. READ16(val);
  206. s->cmyk_adjust[i][k] = val / 100.;
  207. }
  208. ret = register_range(s, i);
  209. if (ret < 0)
  210. goto end;
  211. }
  212. end:
  213. av_file_unmap(buf, size);
  214. return ret;
  215. }
  216. static av_cold int init(AVFilterContext *ctx)
  217. {
  218. int i, ret;
  219. SelectiveColorContext *s = ctx->priv;
  220. /* If the following conditions are not met, it will cause trouble while
  221. * parsing the PS file */
  222. av_assert0(FF_ARRAY_ELEMS(s->cmyk_adjust) == 10 - 1);
  223. av_assert0(FF_ARRAY_ELEMS(s->cmyk_adjust[0]) == 4);
  224. if (s->psfile) {
  225. ret = parse_psfile(ctx, s->psfile);
  226. if (ret < 0)
  227. return ret;
  228. } else {
  229. for (i = 0; i < FF_ARRAY_ELEMS(s->opt_cmyk_adjust); i++) {
  230. const char *opt_cmyk_adjust = s->opt_cmyk_adjust[i];
  231. if (opt_cmyk_adjust) {
  232. float *cmyk = s->cmyk_adjust[i];
  233. sscanf(s->opt_cmyk_adjust[i], "%f %f %f %f", cmyk, cmyk+1, cmyk+2, cmyk+3);
  234. ret = register_range(s, i);
  235. if (ret < 0)
  236. return ret;
  237. }
  238. }
  239. }
  240. av_log(s, AV_LOG_VERBOSE, "Adjustments:%s\n", s->nb_process_ranges ? "" : " none");
  241. for (i = 0; i < s->nb_process_ranges; i++) {
  242. const struct process_range *pr = &s->process_ranges[i];
  243. const float *cmyk = s->cmyk_adjust[pr->range_id];
  244. av_log(s, AV_LOG_VERBOSE, "%8ss: C=%6g M=%6g Y=%6g K=%6g\n",
  245. color_names[pr->range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
  246. }
  247. return 0;
  248. }
  249. static int query_formats(AVFilterContext *ctx)
  250. {
  251. static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE};
  252. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  253. if (!fmts_list)
  254. return AVERROR(ENOMEM);
  255. return ff_set_common_formats(ctx, fmts_list);
  256. }
  257. static inline int comp_adjust(int adjust_range, float value, float adjust, float k, int correction_method)
  258. {
  259. const float min = -value;
  260. const float max = 1. - value;
  261. float res = (-1. - adjust) * k - adjust;
  262. if (correction_method == CORRECTION_METHOD_RELATIVE)
  263. res *= max;
  264. return lrint(av_clipf(res, min, max) * adjust_range);
  265. }
  266. static inline int selective_color(AVFilterContext *ctx, ThreadData *td,
  267. int jobnr, int nb_jobs, int direct, int correction_method)
  268. {
  269. int i, x, y;
  270. const AVFrame *in = td->in;
  271. AVFrame *out = td->out;
  272. const SelectiveColorContext *s = ctx->priv;
  273. const int height = in->height;
  274. const int width = in->width;
  275. const int slice_start = (height * jobnr ) / nb_jobs;
  276. const int slice_end = (height * (jobnr+1)) / nb_jobs;
  277. const int dst_linesize = out->linesize[0];
  278. const int src_linesize = in->linesize[0];
  279. uint8_t *dst = out->data[0] + slice_start * dst_linesize;
  280. const uint8_t *src = in->data[0] + slice_start * src_linesize;
  281. for (y = slice_start; y < slice_end; y++) {
  282. const uint32_t *src32 = (const uint32_t *)src;
  283. uint32_t *dst32 = (uint32_t *)dst;
  284. for (x = 0; x < width; x++) {
  285. const uint32_t color = *src32++;
  286. const int r = color >> 16 & 0xff;
  287. const int g = color >> 8 & 0xff;
  288. const int b = color & 0xff;
  289. const int min_color = FFMIN3(r, g, b);
  290. const int max_color = FFMAX3(r, g, b);
  291. const uint32_t range_flag = (r == max_color) << RANGE_REDS
  292. | (r == min_color) << RANGE_CYANS
  293. | (g == max_color) << RANGE_GREENS
  294. | (g == min_color) << RANGE_MAGENTAS
  295. | (b == max_color) << RANGE_BLUES
  296. | (b == min_color) << RANGE_YELLOWS
  297. | (r > 128 && g > 128 && b > 128) << RANGE_WHITES
  298. | (color && (color & 0xffffff) != 0xffffff) << RANGE_NEUTRALS
  299. | (r < 128 && g < 128 && b < 128) << RANGE_BLACKS;
  300. const float rnorm = r / 255.;
  301. const float gnorm = g / 255.;
  302. const float bnorm = b / 255.;
  303. int adjust_r = 0, adjust_g = 0, adjust_b = 0;
  304. for (i = 0; i < s->nb_process_ranges; i++) {
  305. const struct process_range *pr = &s->process_ranges[i];
  306. if (range_flag & pr->mask) {
  307. const int adjust_range = pr->get_adjust_range(r, g, b, min_color, max_color);
  308. if (adjust_range > 0) {
  309. const float *cmyk_adjust = s->cmyk_adjust[pr->range_id];
  310. const float adj_c = cmyk_adjust[0];
  311. const float adj_m = cmyk_adjust[1];
  312. const float adj_y = cmyk_adjust[2];
  313. const float k = cmyk_adjust[3];
  314. adjust_r += comp_adjust(adjust_range, rnorm, adj_c, k, correction_method);
  315. adjust_g += comp_adjust(adjust_range, gnorm, adj_m, k, correction_method);
  316. adjust_b += comp_adjust(adjust_range, bnorm, adj_y, k, correction_method);
  317. }
  318. }
  319. }
  320. if (!direct || adjust_r || adjust_g || adjust_b)
  321. *dst32 = (color & 0xff000000)
  322. | av_clip_uint8(r + adjust_r) << 16
  323. | av_clip_uint8(g + adjust_g) << 8
  324. | av_clip_uint8(b + adjust_b);
  325. dst32++;
  326. }
  327. src += src_linesize;
  328. dst += dst_linesize;
  329. }
  330. return 0;
  331. }
  332. #define DEF_SELECTIVE_COLOR_FUNC(name, direct, correction_method) \
  333. static int selective_color_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) \
  334. { \
  335. return selective_color(ctx, arg, jobnr, nb_jobs, direct, correction_method); \
  336. }
  337. DEF_SELECTIVE_COLOR_FUNC(indirect_absolute, 0, CORRECTION_METHOD_ABSOLUTE)
  338. DEF_SELECTIVE_COLOR_FUNC(indirect_relative, 0, CORRECTION_METHOD_RELATIVE)
  339. DEF_SELECTIVE_COLOR_FUNC( direct_absolute, 1, CORRECTION_METHOD_ABSOLUTE)
  340. DEF_SELECTIVE_COLOR_FUNC( direct_relative, 1, CORRECTION_METHOD_RELATIVE)
  341. typedef int (*selective_color_func_type)(AVFilterContext *ctx, void *td, int jobnr, int nb_jobs);
  342. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  343. {
  344. AVFilterContext *ctx = inlink->dst;
  345. AVFilterLink *outlink = ctx->outputs[0];
  346. int direct;
  347. AVFrame *out;
  348. ThreadData td;
  349. const SelectiveColorContext *s = ctx->priv;
  350. static const selective_color_func_type funcs[2][2] = {
  351. {selective_color_indirect_absolute, selective_color_indirect_relative},
  352. {selective_color_direct_absolute, selective_color_direct_relative},
  353. };
  354. if (av_frame_is_writable(in)) {
  355. direct = 1;
  356. out = in;
  357. } else {
  358. direct = 0;
  359. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  360. if (!out) {
  361. av_frame_free(&in);
  362. return AVERROR(ENOMEM);
  363. }
  364. av_frame_copy_props(out, in);
  365. }
  366. td.in = in;
  367. td.out = out;
  368. ctx->internal->execute(ctx, funcs[direct][s->correction_method], &td, NULL,
  369. FFMIN(inlink->h, ctx->graph->nb_threads));
  370. if (!direct)
  371. av_frame_free(&in);
  372. return ff_filter_frame(outlink, out);
  373. }
  374. static const AVFilterPad selectivecolor_inputs[] = {
  375. {
  376. .name = "default",
  377. .type = AVMEDIA_TYPE_VIDEO,
  378. .filter_frame = filter_frame,
  379. },
  380. { NULL }
  381. };
  382. static const AVFilterPad selectivecolor_outputs[] = {
  383. {
  384. .name = "default",
  385. .type = AVMEDIA_TYPE_VIDEO,
  386. },
  387. { NULL }
  388. };
  389. AVFilter ff_vf_selectivecolor = {
  390. .name = "selectivecolor",
  391. .description = NULL_IF_CONFIG_SMALL("Apply CMYK adjustments to specific color ranges."),
  392. .priv_size = sizeof(SelectiveColorContext),
  393. .init = init,
  394. .query_formats = query_formats,
  395. .inputs = selectivecolor_inputs,
  396. .outputs = selectivecolor_outputs,
  397. .priv_class = &selectivecolor_class,
  398. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  399. };