vf_colorchannelmixer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2013 Paul B Mahol
  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. #include "libavutil/opt.h"
  21. #include "avfilter.h"
  22. #include "drawutils.h"
  23. #include "formats.h"
  24. #include "internal.h"
  25. #include "video.h"
  26. #define R 0
  27. #define G 1
  28. #define B 2
  29. #define A 3
  30. typedef struct {
  31. const AVClass *class;
  32. double rr, rg, rb, ra;
  33. double gr, gg, gb, ga;
  34. double br, bg, bb, ba;
  35. double ar, ag, ab, aa;
  36. int *lut[4][4];
  37. int *buffer;
  38. uint8_t rgba_map[4];
  39. } ColorChannelMixerContext;
  40. #define OFFSET(x) offsetof(ColorChannelMixerContext, x)
  41. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  42. static const AVOption colorchannelmixer_options[] = {
  43. { "rr", "set the red gain for the red channel", OFFSET(rr), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
  44. { "rg", "set the green gain for the red channel", OFFSET(rg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  45. { "rb", "set the blue gain for the red channel", OFFSET(rb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  46. { "ra", "set the alpha gain for the red channel", OFFSET(ra), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  47. { "gr", "set the red gain for the green channel", OFFSET(gr), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  48. { "gg", "set the green gain for the green channel", OFFSET(gg), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
  49. { "gb", "set the blue gain for the green channel", OFFSET(gb), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  50. { "ga", "set the alpha gain for the green channel", OFFSET(ga), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  51. { "br", "set the red gain for the blue channel", OFFSET(br), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  52. { "bg", "set the green gain for the blue channel", OFFSET(bg), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  53. { "bb", "set the blue gain for the blue channel", OFFSET(bb), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
  54. { "ba", "set the alpha gain for the blue channel", OFFSET(ba), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  55. { "ar", "set the red gain for the alpha channel", OFFSET(ar), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  56. { "ag", "set the green gain for the alpha channel", OFFSET(ag), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  57. { "ab", "set the blue gain for the alpha channel", OFFSET(ab), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -2, 2, FLAGS },
  58. { "aa", "set the alpha gain for the alpha channel", OFFSET(aa), AV_OPT_TYPE_DOUBLE, {.dbl=1}, -2, 2, FLAGS },
  59. { NULL }
  60. };
  61. AVFILTER_DEFINE_CLASS(colorchannelmixer);
  62. static int query_formats(AVFilterContext *ctx)
  63. {
  64. static const enum AVPixelFormat pix_fmts[] = {
  65. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  66. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  67. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  68. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  69. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  70. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  71. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  72. AV_PIX_FMT_NONE
  73. };
  74. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  75. if (!fmts_list)
  76. return AVERROR(ENOMEM);
  77. return ff_set_common_formats(ctx, fmts_list);
  78. }
  79. static int config_output(AVFilterLink *outlink)
  80. {
  81. AVFilterContext *ctx = outlink->src;
  82. ColorChannelMixerContext *s = ctx->priv;
  83. int i, j, size, *buffer;
  84. ff_fill_rgba_map(s->rgba_map, outlink->format);
  85. switch (outlink->format) {
  86. case AV_PIX_FMT_RGB48:
  87. case AV_PIX_FMT_BGR48:
  88. case AV_PIX_FMT_RGBA64:
  89. case AV_PIX_FMT_BGRA64:
  90. size = 65536;
  91. break;
  92. default:
  93. size = 256;
  94. }
  95. s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
  96. if (!s->buffer)
  97. return AVERROR(ENOMEM);
  98. for (i = 0; i < 4; i++)
  99. for (j = 0; j < 4; j++, buffer += size)
  100. s->lut[i][j] = buffer;
  101. for (i = 0; i < size; i++) {
  102. s->lut[R][R][i] = lrint(i * s->rr);
  103. s->lut[R][G][i] = lrint(i * s->rg);
  104. s->lut[R][B][i] = lrint(i * s->rb);
  105. s->lut[R][A][i] = lrint(i * s->ra);
  106. s->lut[G][R][i] = lrint(i * s->gr);
  107. s->lut[G][G][i] = lrint(i * s->gg);
  108. s->lut[G][B][i] = lrint(i * s->gb);
  109. s->lut[G][A][i] = lrint(i * s->ga);
  110. s->lut[B][R][i] = lrint(i * s->br);
  111. s->lut[B][G][i] = lrint(i * s->bg);
  112. s->lut[B][B][i] = lrint(i * s->bb);
  113. s->lut[B][A][i] = lrint(i * s->ba);
  114. s->lut[A][R][i] = lrint(i * s->ar);
  115. s->lut[A][G][i] = lrint(i * s->ag);
  116. s->lut[A][B][i] = lrint(i * s->ab);
  117. s->lut[A][A][i] = lrint(i * s->aa);
  118. }
  119. return 0;
  120. }
  121. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  122. {
  123. AVFilterContext *ctx = inlink->dst;
  124. ColorChannelMixerContext *s = ctx->priv;
  125. AVFilterLink *outlink = ctx->outputs[0];
  126. const uint8_t roffset = s->rgba_map[R];
  127. const uint8_t goffset = s->rgba_map[G];
  128. const uint8_t boffset = s->rgba_map[B];
  129. const uint8_t aoffset = s->rgba_map[A];
  130. const uint8_t *srcrow = in->data[0];
  131. uint8_t *dstrow;
  132. AVFrame *out;
  133. int i, j;
  134. if (av_frame_is_writable(in)) {
  135. out = in;
  136. } else {
  137. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  138. if (!out) {
  139. av_frame_free(&in);
  140. return AVERROR(ENOMEM);
  141. }
  142. av_frame_copy_props(out, in);
  143. }
  144. dstrow = out->data[0];
  145. switch (outlink->format) {
  146. case AV_PIX_FMT_BGR24:
  147. case AV_PIX_FMT_RGB24:
  148. for (i = 0; i < outlink->h; i++) {
  149. const uint8_t *src = srcrow;
  150. uint8_t *dst = dstrow;
  151. for (j = 0; j < outlink->w * 3; j += 3) {
  152. const uint8_t rin = src[j + roffset];
  153. const uint8_t gin = src[j + goffset];
  154. const uint8_t bin = src[j + boffset];
  155. dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
  156. s->lut[R][G][gin] +
  157. s->lut[R][B][bin]);
  158. dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
  159. s->lut[G][G][gin] +
  160. s->lut[G][B][bin]);
  161. dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
  162. s->lut[B][G][gin] +
  163. s->lut[B][B][bin]);
  164. }
  165. srcrow += in->linesize[0];
  166. dstrow += out->linesize[0];
  167. }
  168. break;
  169. case AV_PIX_FMT_0BGR:
  170. case AV_PIX_FMT_0RGB:
  171. case AV_PIX_FMT_BGR0:
  172. case AV_PIX_FMT_RGB0:
  173. for (i = 0; i < outlink->h; i++) {
  174. const uint8_t *src = srcrow;
  175. uint8_t *dst = dstrow;
  176. for (j = 0; j < outlink->w * 4; j += 4) {
  177. const uint8_t rin = src[j + roffset];
  178. const uint8_t gin = src[j + goffset];
  179. const uint8_t bin = src[j + boffset];
  180. dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
  181. s->lut[R][G][gin] +
  182. s->lut[R][B][bin]);
  183. dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
  184. s->lut[G][G][gin] +
  185. s->lut[G][B][bin]);
  186. dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
  187. s->lut[B][G][gin] +
  188. s->lut[B][B][bin]);
  189. if (in != out)
  190. dst[j + aoffset] = 0;
  191. }
  192. srcrow += in->linesize[0];
  193. dstrow += out->linesize[0];
  194. }
  195. break;
  196. case AV_PIX_FMT_ABGR:
  197. case AV_PIX_FMT_ARGB:
  198. case AV_PIX_FMT_BGRA:
  199. case AV_PIX_FMT_RGBA:
  200. for (i = 0; i < outlink->h; i++) {
  201. const uint8_t *src = srcrow;
  202. uint8_t *dst = dstrow;
  203. for (j = 0; j < outlink->w * 4; j += 4) {
  204. const uint8_t rin = src[j + roffset];
  205. const uint8_t gin = src[j + goffset];
  206. const uint8_t bin = src[j + boffset];
  207. const uint8_t ain = src[j + aoffset];
  208. dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
  209. s->lut[R][G][gin] +
  210. s->lut[R][B][bin] +
  211. s->lut[R][A][ain]);
  212. dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
  213. s->lut[G][G][gin] +
  214. s->lut[G][B][bin] +
  215. s->lut[G][A][ain]);
  216. dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
  217. s->lut[B][G][gin] +
  218. s->lut[B][B][bin] +
  219. s->lut[B][A][ain]);
  220. dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
  221. s->lut[A][G][gin] +
  222. s->lut[A][B][bin] +
  223. s->lut[A][A][ain]);
  224. }
  225. srcrow += in->linesize[0];
  226. dstrow += out->linesize[0];
  227. }
  228. break;
  229. case AV_PIX_FMT_BGR48:
  230. case AV_PIX_FMT_RGB48:
  231. for (i = 0; i < outlink->h; i++) {
  232. const uint16_t *src = (const uint16_t *)srcrow;
  233. uint16_t *dst = (uint16_t *)dstrow;
  234. for (j = 0; j < outlink->w * 3; j += 3) {
  235. const uint16_t rin = src[j + roffset];
  236. const uint16_t gin = src[j + goffset];
  237. const uint16_t bin = src[j + boffset];
  238. dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
  239. s->lut[R][G][gin] +
  240. s->lut[R][B][bin]);
  241. dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
  242. s->lut[G][G][gin] +
  243. s->lut[G][B][bin]);
  244. dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
  245. s->lut[B][G][gin] +
  246. s->lut[B][B][bin]);
  247. }
  248. srcrow += in->linesize[0];
  249. dstrow += out->linesize[0];
  250. }
  251. break;
  252. case AV_PIX_FMT_BGRA64:
  253. case AV_PIX_FMT_RGBA64:
  254. for (i = 0; i < outlink->h; i++) {
  255. const uint16_t *src = (const uint16_t *)srcrow;
  256. uint16_t *dst = (uint16_t *)dstrow;
  257. for (j = 0; j < outlink->w * 4; j += 4) {
  258. const uint16_t rin = src[j + roffset];
  259. const uint16_t gin = src[j + goffset];
  260. const uint16_t bin = src[j + boffset];
  261. const uint16_t ain = src[j + aoffset];
  262. dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
  263. s->lut[R][G][gin] +
  264. s->lut[R][B][bin] +
  265. s->lut[R][A][ain]);
  266. dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
  267. s->lut[G][G][gin] +
  268. s->lut[G][B][bin] +
  269. s->lut[G][A][ain]);
  270. dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
  271. s->lut[B][G][gin] +
  272. s->lut[B][B][bin] +
  273. s->lut[B][A][ain]);
  274. dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
  275. s->lut[A][G][gin] +
  276. s->lut[A][B][bin] +
  277. s->lut[A][A][ain]);
  278. }
  279. srcrow += in->linesize[0];
  280. dstrow += out->linesize[0];
  281. }
  282. }
  283. if (in != out)
  284. av_frame_free(&in);
  285. return ff_filter_frame(ctx->outputs[0], out);
  286. }
  287. static av_cold void uninit(AVFilterContext *ctx)
  288. {
  289. ColorChannelMixerContext *s = ctx->priv;
  290. av_freep(&s->buffer);
  291. }
  292. static const AVFilterPad colorchannelmixer_inputs[] = {
  293. {
  294. .name = "default",
  295. .type = AVMEDIA_TYPE_VIDEO,
  296. .filter_frame = filter_frame,
  297. },
  298. { NULL }
  299. };
  300. static const AVFilterPad colorchannelmixer_outputs[] = {
  301. {
  302. .name = "default",
  303. .type = AVMEDIA_TYPE_VIDEO,
  304. .config_props = config_output,
  305. },
  306. { NULL }
  307. };
  308. AVFilter ff_vf_colorchannelmixer = {
  309. .name = "colorchannelmixer",
  310. .description = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
  311. .priv_size = sizeof(ColorChannelMixerContext),
  312. .priv_class = &colorchannelmixer_class,
  313. .uninit = uninit,
  314. .query_formats = query_formats,
  315. .inputs = colorchannelmixer_inputs,
  316. .outputs = colorchannelmixer_outputs,
  317. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  318. };