vf_colormatrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * ColorMatrix v2.2 for Avisynth 2.5.x
  3. *
  4. * Copyright (C) 2006-2007 Kevin Stone
  5. *
  6. * ColorMatrix 1.x is Copyright (C) Wilbert Dijkhof
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. * License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /**
  23. * @file
  24. * ColorMatrix 2.0 is based on the original ColorMatrix filter by Wilbert
  25. * Dijkhof. It adds the ability to convert between any of: Rec.709, FCC,
  26. * Rec.601, and SMPTE 240M. It also makes pre and post clipping optional,
  27. * adds an option to use scaled or non-scaled coefficients, and more...
  28. */
  29. #include <float.h>
  30. #include "avfilter.h"
  31. #include "formats.h"
  32. #include "video.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/avstring.h"
  35. #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
  36. #define CB(n) av_clip_uint8(n)
  37. static const double yuv_coeff[4][3][3] = {
  38. { { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
  39. { -0.3850, +0.5000, -0.1150 },
  40. { -0.4540, -0.0460, +0.5000 } },
  41. { { +0.5900, +0.1100, +0.3000 }, // FCC (1)
  42. { -0.3310, +0.5000, -0.1690 },
  43. { -0.4210, -0.0790, +0.5000 } },
  44. { { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
  45. { -0.3313, +0.5000, -0.1687 },
  46. { -0.4187, -0.0813, +0.5000 } },
  47. { { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
  48. { -0.3840, +0.5000, -0.1160 },
  49. { -0.4450, -0.0550, +0.5000 } },
  50. };
  51. typedef struct {
  52. int yuv_convert[16][3][3];
  53. int interlaced;
  54. int source, dest, mode;
  55. char src[256];
  56. char dst[256];
  57. int hsub, vsub;
  58. AVFilterBufferRef *outpicref;
  59. } ColorMatrixContext;
  60. #define ma m[0][0]
  61. #define mb m[0][1]
  62. #define mc m[0][2]
  63. #define md m[1][0]
  64. #define me m[1][1]
  65. #define mf m[1][2]
  66. #define mg m[2][0]
  67. #define mh m[2][1]
  68. #define mi m[2][2]
  69. #define ima im[0][0]
  70. #define imb im[0][1]
  71. #define imc im[0][2]
  72. #define imd im[1][0]
  73. #define ime im[1][1]
  74. #define imf im[1][2]
  75. #define img im[2][0]
  76. #define imh im[2][1]
  77. #define imi im[2][2]
  78. static void inverse3x3(double im[3][3], const double m[3][3])
  79. {
  80. double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
  81. det = 1.0 / det;
  82. ima = det * (me * mi - mf * mh);
  83. imb = det * (mc * mh - mb * mi);
  84. imc = det * (mb * mf - mc * me);
  85. imd = det * (mf * mg - md * mi);
  86. ime = det * (ma * mi - mc * mg);
  87. imf = det * (mc * md - ma * mf);
  88. img = det * (md * mh - me * mg);
  89. imh = det * (mb * mg - ma * mh);
  90. imi = det * (ma * me - mb * md);
  91. }
  92. static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
  93. {
  94. int i, j;
  95. for (i = 0; i < 3; i++)
  96. for (j = 0; j < 3; j++)
  97. cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
  98. }
  99. static void calc_coefficients(AVFilterContext *ctx)
  100. {
  101. ColorMatrixContext *color = ctx->priv;
  102. double rgb_coeffd[4][3][3];
  103. double yuv_convertd[16][3][3];
  104. int v = 0;
  105. int i, j, k;
  106. for (i = 0; i < 4; i++)
  107. inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
  108. for (i = 0; i < 4; i++) {
  109. for (j = 0; j < 4; j++) {
  110. solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
  111. for (k = 0; k < 3; k++) {
  112. color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
  113. color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
  114. color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
  115. }
  116. if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
  117. color->yuv_convert[v][2][0] != 0) {
  118. av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
  119. }
  120. v++;
  121. }
  122. }
  123. }
  124. static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
  125. static int get_color_mode_index(const char *name)
  126. {
  127. int i;
  128. for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
  129. if (!av_strcasecmp(color_modes[i], name))
  130. return i;
  131. return -1;
  132. }
  133. static av_cold int init(AVFilterContext *ctx, const char *args)
  134. {
  135. ColorMatrixContext *color = ctx->priv;
  136. if (!args)
  137. goto usage;
  138. if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
  139. usage:
  140. av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
  141. av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
  142. return -1;
  143. }
  144. color->source = get_color_mode_index(color->src);
  145. if (color->source < 0) {
  146. av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
  147. return AVERROR(EINVAL);
  148. }
  149. color->dest = get_color_mode_index(color->dst);
  150. if (color->dest < 0) {
  151. av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
  152. return AVERROR(EINVAL);
  153. }
  154. if (color->source == color->dest) {
  155. av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
  156. return AVERROR(EINVAL);
  157. }
  158. color->mode = color->source * 4 + color->dest;
  159. calc_coefficients(ctx);
  160. return 0;
  161. }
  162. static void process_frame_uyvy422(ColorMatrixContext *color,
  163. AVFilterBufferRef *dst, AVFilterBufferRef *src)
  164. {
  165. const unsigned char *srcp = src->data[0];
  166. const int src_pitch = src->linesize[0];
  167. const int height = src->video->h;
  168. const int width = src->video->w*2;
  169. unsigned char *dstp = dst->data[0];
  170. const int dst_pitch = dst->linesize[0];
  171. const int c2 = color->yuv_convert[color->mode][0][1];
  172. const int c3 = color->yuv_convert[color->mode][0][2];
  173. const int c4 = color->yuv_convert[color->mode][1][1];
  174. const int c5 = color->yuv_convert[color->mode][1][2];
  175. const int c6 = color->yuv_convert[color->mode][2][1];
  176. const int c7 = color->yuv_convert[color->mode][2][2];
  177. int x, y;
  178. for (y = 0; y < height; y++) {
  179. for (x = 0; x < width; x += 4) {
  180. const int u = srcp[x + 0] - 128;
  181. const int v = srcp[x + 2] - 128;
  182. const int uvval = c2 * u + c3 * v + 1081344;
  183. dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
  184. dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
  185. dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
  186. dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
  187. }
  188. srcp += src_pitch;
  189. dstp += dst_pitch;
  190. }
  191. }
  192. static void process_frame_yuv422p(ColorMatrixContext *color,
  193. AVFilterBufferRef *dst, AVFilterBufferRef *src)
  194. {
  195. const unsigned char *srcpU = src->data[1];
  196. const unsigned char *srcpV = src->data[2];
  197. const unsigned char *srcpY = src->data[0];
  198. const int src_pitchY = src->linesize[0];
  199. const int src_pitchUV = src->linesize[1];
  200. const int height = src->video->h;
  201. const int width = src->video->w;
  202. unsigned char *dstpU = dst->data[1];
  203. unsigned char *dstpV = dst->data[2];
  204. unsigned char *dstpY = dst->data[0];
  205. const int dst_pitchY = dst->linesize[0];
  206. const int dst_pitchUV = dst->linesize[1];
  207. const int c2 = color->yuv_convert[color->mode][0][1];
  208. const int c3 = color->yuv_convert[color->mode][0][2];
  209. const int c4 = color->yuv_convert[color->mode][1][1];
  210. const int c5 = color->yuv_convert[color->mode][1][2];
  211. const int c6 = color->yuv_convert[color->mode][2][1];
  212. const int c7 = color->yuv_convert[color->mode][2][2];
  213. int x, y;
  214. for (y = 0; y < height; y++) {
  215. for (x = 0; x < width; x += 2) {
  216. const int u = srcpU[x >> 1] - 128;
  217. const int v = srcpV[x >> 1] - 128;
  218. const int uvval = c2 * u + c3 * v + 1081344;
  219. dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
  220. dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
  221. dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
  222. dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
  223. }
  224. srcpY += src_pitchY;
  225. dstpY += dst_pitchY;
  226. srcpU += src_pitchUV;
  227. srcpV += src_pitchUV;
  228. dstpU += dst_pitchUV;
  229. dstpV += dst_pitchUV;
  230. }
  231. }
  232. static void process_frame_yuv420p(ColorMatrixContext *color,
  233. AVFilterBufferRef *dst, AVFilterBufferRef *src)
  234. {
  235. const unsigned char *srcpU = src->data[1];
  236. const unsigned char *srcpV = src->data[2];
  237. const unsigned char *srcpY = src->data[0];
  238. const unsigned char *srcpN = src->data[0] + src->linesize[0];
  239. const int src_pitchY = src->linesize[0];
  240. const int src_pitchUV = src->linesize[1];
  241. const int height = src->video->h;
  242. const int width = src->video->w;
  243. unsigned char *dstpU = dst->data[1];
  244. unsigned char *dstpV = dst->data[2];
  245. unsigned char *dstpY = dst->data[0];
  246. unsigned char *dstpN = dst->data[0] + dst->linesize[0];
  247. const int dst_pitchY = dst->linesize[0];
  248. const int dst_pitchUV = dst->linesize[1];
  249. const int c2 = color->yuv_convert[color->mode][0][1];
  250. const int c3 = color->yuv_convert[color->mode][0][2];
  251. const int c4 = color->yuv_convert[color->mode][1][1];
  252. const int c5 = color->yuv_convert[color->mode][1][2];
  253. const int c6 = color->yuv_convert[color->mode][2][1];
  254. const int c7 = color->yuv_convert[color->mode][2][2];
  255. int x, y;
  256. for (y = 0; y < height; y += 2) {
  257. for (x = 0; x < width; x += 2) {
  258. const int u = srcpU[x >> 1] - 128;
  259. const int v = srcpV[x >> 1] - 128;
  260. const int uvval = c2 * u + c3 * v + 1081344;
  261. dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
  262. dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
  263. dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
  264. dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
  265. dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
  266. dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
  267. }
  268. srcpY += src_pitchY << 1;
  269. dstpY += dst_pitchY << 1;
  270. srcpN += src_pitchY << 1;
  271. dstpN += dst_pitchY << 1;
  272. srcpU += src_pitchUV;
  273. srcpV += src_pitchUV;
  274. dstpU += dst_pitchUV;
  275. dstpV += dst_pitchUV;
  276. }
  277. }
  278. static int config_input(AVFilterLink *inlink)
  279. {
  280. AVFilterContext *ctx = inlink->dst;
  281. ColorMatrixContext *color = ctx->priv;
  282. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  283. color->hsub = pix_desc->log2_chroma_w;
  284. color->vsub = pix_desc->log2_chroma_h;
  285. av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
  286. return 0;
  287. }
  288. static int query_formats(AVFilterContext *ctx)
  289. {
  290. static const enum PixelFormat pix_fmts[] = {
  291. PIX_FMT_YUV422P,
  292. PIX_FMT_YUV420P,
  293. PIX_FMT_UYVY422,
  294. PIX_FMT_NONE
  295. };
  296. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  297. return 0;
  298. }
  299. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  300. {
  301. AVFilterBufferRef *picref =
  302. ff_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
  303. return picref;
  304. }
  305. static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  306. {
  307. AVFilterContext *ctx = link->dst;
  308. ColorMatrixContext *color = ctx->priv;
  309. AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
  310. color->outpicref = outpicref;
  311. return ff_start_frame(link->dst->outputs[0], outpicref);
  312. }
  313. static int end_frame(AVFilterLink *link)
  314. {
  315. AVFilterContext *ctx = link->dst;
  316. ColorMatrixContext *color = ctx->priv;
  317. AVFilterBufferRef *out = color->outpicref;
  318. if (link->cur_buf->format == PIX_FMT_YUV422P)
  319. process_frame_yuv422p(color, out, link->cur_buf);
  320. else if (link->cur_buf->format == PIX_FMT_YUV420P)
  321. process_frame_yuv420p(color, out, link->cur_buf);
  322. else
  323. process_frame_uyvy422(color, out, link->cur_buf);
  324. ff_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
  325. return ff_end_frame(ctx->outputs[0]);
  326. }
  327. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
  328. AVFilter avfilter_vf_colormatrix = {
  329. .name = "colormatrix",
  330. .description = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
  331. .priv_size = sizeof(ColorMatrixContext),
  332. .init = init,
  333. .query_formats = query_formats,
  334. .inputs = (AVFilterPad[]) {{ .name = "default",
  335. .type = AVMEDIA_TYPE_VIDEO,
  336. .config_props = config_input,
  337. .min_perms = AV_PERM_READ | AV_PERM_WRITE,
  338. .start_frame = start_frame,
  339. .get_video_buffer = get_video_buffer,
  340. .draw_slice = null_draw_slice,
  341. .end_frame = end_frame, },
  342. { .name = NULL }},
  343. .outputs = (AVFilterPad[]) {{ .name = "default",
  344. .type = AVMEDIA_TYPE_VIDEO, },
  345. { .name = NULL }},
  346. };