vf_colorchannelmixer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  75. return 0;
  76. }
  77. static int config_output(AVFilterLink *outlink)
  78. {
  79. AVFilterContext *ctx = outlink->src;
  80. ColorChannelMixerContext *s = ctx->priv;
  81. int i, j, size, *buffer;
  82. ff_fill_rgba_map(s->rgba_map, outlink->format);
  83. switch (outlink->format) {
  84. case AV_PIX_FMT_RGB48:
  85. case AV_PIX_FMT_BGR48:
  86. case AV_PIX_FMT_RGBA64:
  87. case AV_PIX_FMT_BGRA64:
  88. size = 65536;
  89. break;
  90. default:
  91. size = 256;
  92. }
  93. s->buffer = buffer = av_malloc(16 * size * sizeof(*s->buffer));
  94. if (!s->buffer)
  95. return AVERROR(ENOMEM);
  96. for (i = 0; i < 4; i++)
  97. for (j = 0; j < 4; j++, buffer += size)
  98. s->lut[i][j] = buffer;
  99. for (i = 0; i < size; i++) {
  100. s->lut[R][R][i] = round(i * s->rr);
  101. s->lut[R][G][i] = round(i * s->rg);
  102. s->lut[R][B][i] = round(i * s->rb);
  103. s->lut[R][A][i] = round(i * s->ra);
  104. s->lut[G][R][i] = round(i * s->gr);
  105. s->lut[G][G][i] = round(i * s->gg);
  106. s->lut[G][B][i] = round(i * s->gb);
  107. s->lut[G][A][i] = round(i * s->ga);
  108. s->lut[B][R][i] = round(i * s->br);
  109. s->lut[B][G][i] = round(i * s->bg);
  110. s->lut[B][B][i] = round(i * s->bb);
  111. s->lut[B][A][i] = round(i * s->ba);
  112. s->lut[A][R][i] = round(i * s->ar);
  113. s->lut[A][G][i] = round(i * s->ag);
  114. s->lut[A][B][i] = round(i * s->ab);
  115. s->lut[A][A][i] = round(i * s->aa);
  116. }
  117. return 0;
  118. }
  119. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  120. {
  121. AVFilterContext *ctx = inlink->dst;
  122. ColorChannelMixerContext *s = ctx->priv;
  123. AVFilterLink *outlink = ctx->outputs[0];
  124. const uint8_t roffset = s->rgba_map[R];
  125. const uint8_t goffset = s->rgba_map[G];
  126. const uint8_t boffset = s->rgba_map[B];
  127. const uint8_t aoffset = s->rgba_map[A];
  128. const uint8_t *srcrow = in->data[0];
  129. uint8_t *dstrow;
  130. AVFrame *out;
  131. int i, j;
  132. if (av_frame_is_writable(in)) {
  133. out = in;
  134. } else {
  135. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  136. if (!out) {
  137. av_frame_free(&in);
  138. return AVERROR(ENOMEM);
  139. }
  140. av_frame_copy_props(out, in);
  141. }
  142. dstrow = out->data[0];
  143. switch (outlink->format) {
  144. case AV_PIX_FMT_BGR24:
  145. case AV_PIX_FMT_RGB24:
  146. for (i = 0; i < outlink->h; i++) {
  147. const uint8_t *src = srcrow;
  148. uint8_t *dst = dstrow;
  149. for (j = 0; j < outlink->w * 3; j += 3) {
  150. const uint8_t rin = src[j + roffset];
  151. const uint8_t gin = src[j + goffset];
  152. const uint8_t bin = src[j + boffset];
  153. dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
  154. s->lut[R][G][gin] +
  155. s->lut[R][B][bin]);
  156. dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
  157. s->lut[G][G][gin] +
  158. s->lut[G][B][bin]);
  159. dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
  160. s->lut[B][G][gin] +
  161. s->lut[B][B][bin]);
  162. }
  163. srcrow += in->linesize[0];
  164. dstrow += out->linesize[0];
  165. }
  166. break;
  167. case AV_PIX_FMT_0BGR:
  168. case AV_PIX_FMT_0RGB:
  169. case AV_PIX_FMT_BGR0:
  170. case AV_PIX_FMT_RGB0:
  171. for (i = 0; i < outlink->h; i++) {
  172. const uint8_t *src = srcrow;
  173. uint8_t *dst = dstrow;
  174. for (j = 0; j < outlink->w * 4; j += 4) {
  175. const uint8_t rin = src[j + roffset];
  176. const uint8_t gin = src[j + goffset];
  177. const uint8_t bin = src[j + boffset];
  178. dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
  179. s->lut[R][G][gin] +
  180. s->lut[R][B][bin]);
  181. dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
  182. s->lut[G][G][gin] +
  183. s->lut[G][B][bin]);
  184. dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
  185. s->lut[B][G][gin] +
  186. s->lut[B][B][bin]);
  187. if (in != out)
  188. dst[j + aoffset] = 0;
  189. }
  190. srcrow += in->linesize[0];
  191. dstrow += out->linesize[0];
  192. }
  193. break;
  194. case AV_PIX_FMT_ABGR:
  195. case AV_PIX_FMT_ARGB:
  196. case AV_PIX_FMT_BGRA:
  197. case AV_PIX_FMT_RGBA:
  198. for (i = 0; i < outlink->h; i++) {
  199. const uint8_t *src = srcrow;
  200. uint8_t *dst = dstrow;
  201. for (j = 0; j < outlink->w * 4; j += 4) {
  202. const uint8_t rin = src[j + roffset];
  203. const uint8_t gin = src[j + goffset];
  204. const uint8_t bin = src[j + boffset];
  205. const uint8_t ain = src[j + aoffset];
  206. dst[j + roffset] = av_clip_uint8(s->lut[R][R][rin] +
  207. s->lut[R][G][gin] +
  208. s->lut[R][B][bin] +
  209. s->lut[R][A][ain]);
  210. dst[j + goffset] = av_clip_uint8(s->lut[G][R][rin] +
  211. s->lut[G][G][gin] +
  212. s->lut[G][B][bin] +
  213. s->lut[G][A][ain]);
  214. dst[j + boffset] = av_clip_uint8(s->lut[B][R][rin] +
  215. s->lut[B][G][gin] +
  216. s->lut[B][B][bin] +
  217. s->lut[B][A][ain]);
  218. dst[j + aoffset] = av_clip_uint8(s->lut[A][R][rin] +
  219. s->lut[A][G][gin] +
  220. s->lut[A][B][bin] +
  221. s->lut[A][A][ain]);
  222. }
  223. srcrow += in->linesize[0];
  224. dstrow += out->linesize[0];
  225. }
  226. break;
  227. case AV_PIX_FMT_BGR48:
  228. case AV_PIX_FMT_RGB48:
  229. for (i = 0; i < outlink->h; i++) {
  230. const uint16_t *src = (const uint16_t *)srcrow;
  231. uint16_t *dst = (uint16_t *)dstrow;
  232. for (j = 0; j < outlink->w * 3; j += 3) {
  233. const uint16_t rin = src[j + roffset];
  234. const uint16_t gin = src[j + goffset];
  235. const uint16_t bin = src[j + boffset];
  236. dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
  237. s->lut[R][G][gin] +
  238. s->lut[R][B][bin]);
  239. dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
  240. s->lut[G][G][gin] +
  241. s->lut[G][B][bin]);
  242. dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
  243. s->lut[B][G][gin] +
  244. s->lut[B][B][bin]);
  245. }
  246. srcrow += in->linesize[0];
  247. dstrow += out->linesize[0];
  248. }
  249. break;
  250. case AV_PIX_FMT_BGRA64:
  251. case AV_PIX_FMT_RGBA64:
  252. for (i = 0; i < outlink->h; i++) {
  253. const uint16_t *src = (const uint16_t *)srcrow;
  254. uint16_t *dst = (uint16_t *)dstrow;
  255. for (j = 0; j < outlink->w * 4; j += 4) {
  256. const uint16_t rin = src[j + roffset];
  257. const uint16_t gin = src[j + goffset];
  258. const uint16_t bin = src[j + boffset];
  259. const uint16_t ain = src[j + aoffset];
  260. dst[j + roffset] = av_clip_uint16(s->lut[R][R][rin] +
  261. s->lut[R][G][gin] +
  262. s->lut[R][B][bin] +
  263. s->lut[R][A][ain]);
  264. dst[j + goffset] = av_clip_uint16(s->lut[G][R][rin] +
  265. s->lut[G][G][gin] +
  266. s->lut[G][B][bin] +
  267. s->lut[G][A][ain]);
  268. dst[j + boffset] = av_clip_uint16(s->lut[B][R][rin] +
  269. s->lut[B][G][gin] +
  270. s->lut[B][B][bin] +
  271. s->lut[B][A][ain]);
  272. dst[j + aoffset] = av_clip_uint16(s->lut[A][R][rin] +
  273. s->lut[A][G][gin] +
  274. s->lut[A][B][bin] +
  275. s->lut[A][A][ain]);
  276. }
  277. srcrow += in->linesize[0];
  278. dstrow += out->linesize[0];
  279. }
  280. }
  281. if (in != out)
  282. av_frame_free(&in);
  283. return ff_filter_frame(ctx->outputs[0], out);
  284. }
  285. static av_cold void uninit(AVFilterContext *ctx)
  286. {
  287. ColorChannelMixerContext *s = ctx->priv;
  288. av_freep(&s->buffer);
  289. }
  290. static const AVFilterPad colorchannelmixer_inputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .filter_frame = filter_frame,
  295. },
  296. { NULL }
  297. };
  298. static const AVFilterPad colorchannelmixer_outputs[] = {
  299. {
  300. .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. .config_props = config_output,
  303. },
  304. { NULL }
  305. };
  306. AVFilter ff_vf_colorchannelmixer = {
  307. .name = "colorchannelmixer",
  308. .description = NULL_IF_CONFIG_SMALL("Adjust colors by mixing color channels."),
  309. .priv_size = sizeof(ColorChannelMixerContext),
  310. .priv_class = &colorchannelmixer_class,
  311. .uninit = uninit,
  312. .query_formats = query_formats,
  313. .inputs = colorchannelmixer_inputs,
  314. .outputs = colorchannelmixer_outputs,
  315. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  316. };