vf_colormatrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <strings.h>
  30. #include <float.h>
  31. #include "avfilter.h"
  32. #include "libavutil/pixdesc.h"
  33. #include "libavutil/avstring.h"
  34. #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
  35. #define CB(n) av_clip_uint8(n)
  36. static const double yuv_coeff[4][3][3] = {
  37. { { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
  38. { -0.3850, +0.5000, -0.1150 },
  39. { -0.4540, -0.0460, +0.5000 } },
  40. { { +0.5900, +0.1100, +0.3000 }, // FCC (1)
  41. { -0.3310, +0.5000, -0.1690 },
  42. { -0.4210, -0.0790, +0.5000 } },
  43. { { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
  44. { -0.3313, +0.5000, -0.1687 },
  45. { -0.4187, -0.0813, +0.5000 } },
  46. { { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
  47. { -0.3840, +0.5000, -0.1160 },
  48. { -0.4450, -0.0550, +0.5000 } },
  49. };
  50. typedef struct {
  51. int yuv_convert[16][3][3];
  52. int interlaced;
  53. int source, dest, mode;
  54. char src[256];
  55. char dst[256];
  56. int hsub, vsub;
  57. } ColorMatrixContext;
  58. #define ma m[0][0]
  59. #define mb m[0][1]
  60. #define mc m[0][2]
  61. #define md m[1][0]
  62. #define me m[1][1]
  63. #define mf m[1][2]
  64. #define mg m[2][0]
  65. #define mh m[2][1]
  66. #define mi m[2][2]
  67. #define ima im[0][0]
  68. #define imb im[0][1]
  69. #define imc im[0][2]
  70. #define imd im[1][0]
  71. #define ime im[1][1]
  72. #define imf im[1][2]
  73. #define img im[2][0]
  74. #define imh im[2][1]
  75. #define imi im[2][2]
  76. static void inverse3x3(double im[3][3], const double m[3][3])
  77. {
  78. double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
  79. det = 1.0 / det;
  80. ima = det * (me * mi - mf * mh);
  81. imb = det * (mc * mh - mb * mi);
  82. imc = det * (mb * mf - mc * me);
  83. imd = det * (mf * mg - md * mi);
  84. ime = det * (ma * mi - mc * mg);
  85. imf = det * (mc * md - ma * mf);
  86. img = det * (md * mh - me * mg);
  87. imh = det * (mb * mg - ma * mh);
  88. imi = det * (ma * me - mb * md);
  89. }
  90. static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
  91. {
  92. int i, j;
  93. for (i = 0; i < 3; i++)
  94. for (j = 0; j < 3; j++)
  95. cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
  96. }
  97. static void calc_coefficients(AVFilterContext *ctx)
  98. {
  99. ColorMatrixContext *color = ctx->priv;
  100. double rgb_coeffd[4][3][3];
  101. double yuv_convertd[16][3][3];
  102. int v = 0;
  103. int i, j, k;
  104. for (i = 0; i < 4; i++)
  105. inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
  106. for (i = 0; i < 4; i++) {
  107. for (j = 0; j < 4; j++) {
  108. solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
  109. for (k = 0; k < 3; k++) {
  110. color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
  111. color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
  112. color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
  113. }
  114. if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
  115. color->yuv_convert[v][2][0] != 0) {
  116. av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
  117. }
  118. v++;
  119. }
  120. }
  121. }
  122. static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
  123. static int get_color_mode_index(const char *name)
  124. {
  125. int i;
  126. for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
  127. if (!av_strcasecmp(color_modes[i], name))
  128. return i;
  129. return -1;
  130. }
  131. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  132. {
  133. ColorMatrixContext *color = ctx->priv;
  134. if (!args)
  135. goto usage;
  136. if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
  137. usage:
  138. av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
  139. av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
  140. return -1;
  141. }
  142. color->source = get_color_mode_index(color->src);
  143. if (color->source < 0) {
  144. av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
  145. return AVERROR(EINVAL);
  146. }
  147. color->dest = get_color_mode_index(color->dst);
  148. if (color->dest < 0) {
  149. av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
  150. return AVERROR(EINVAL);
  151. }
  152. if (color->source == color->dest) {
  153. av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
  154. return AVERROR(EINVAL);
  155. }
  156. color->mode = color->source * 4 + color->dest;
  157. calc_coefficients(ctx);
  158. return 0;
  159. }
  160. static void process_frame_uyvy422(ColorMatrixContext *color,
  161. AVFilterBufferRef *dst, AVFilterBufferRef *src)
  162. {
  163. const unsigned char *srcp = src->data[0];
  164. const int src_pitch = src->linesize[0];
  165. const int height = src->video->h;
  166. const int width = src->video->w*2;
  167. unsigned char *dstp = dst->data[0];
  168. const int dst_pitch = dst->linesize[0];
  169. const int c2 = color->yuv_convert[color->mode][0][1];
  170. const int c3 = color->yuv_convert[color->mode][0][2];
  171. const int c4 = color->yuv_convert[color->mode][1][1];
  172. const int c5 = color->yuv_convert[color->mode][1][2];
  173. const int c6 = color->yuv_convert[color->mode][2][1];
  174. const int c7 = color->yuv_convert[color->mode][2][2];
  175. int x, y;
  176. for (y = 0; y < height; y++) {
  177. for (x = 0; x < width; x += 4) {
  178. const int u = srcp[x + 0] - 128;
  179. const int v = srcp[x + 2] - 128;
  180. const int uvval = c2 * u + c3 * v + 1081344;
  181. dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
  182. dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
  183. dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
  184. dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
  185. }
  186. srcp += src_pitch;
  187. dstp += dst_pitch;
  188. }
  189. }
  190. static void process_frame_yuv422p(ColorMatrixContext *color,
  191. AVFilterBufferRef *dst, AVFilterBufferRef *src)
  192. {
  193. const unsigned char *srcpU = src->data[1];
  194. const unsigned char *srcpV = src->data[2];
  195. const unsigned char *srcpY = src->data[0];
  196. const int src_pitchY = src->linesize[0];
  197. const int src_pitchUV = src->linesize[1];
  198. const int height = src->video->h;
  199. const int width = src->video->w;
  200. unsigned char *dstpU = dst->data[1];
  201. unsigned char *dstpV = dst->data[2];
  202. unsigned char *dstpY = dst->data[0];
  203. const int dst_pitchY = dst->linesize[0];
  204. const int dst_pitchUV = dst->linesize[1];
  205. const int c2 = color->yuv_convert[color->mode][0][1];
  206. const int c3 = color->yuv_convert[color->mode][0][2];
  207. const int c4 = color->yuv_convert[color->mode][1][1];
  208. const int c5 = color->yuv_convert[color->mode][1][2];
  209. const int c6 = color->yuv_convert[color->mode][2][1];
  210. const int c7 = color->yuv_convert[color->mode][2][2];
  211. int x, y;
  212. for (y = 0; y < height; y++) {
  213. for (x = 0; x < width; x += 2) {
  214. const int u = srcpU[x >> 1] - 128;
  215. const int v = srcpV[x >> 1] - 128;
  216. const int uvval = c2 * u + c3 * v + 1081344;
  217. dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
  218. dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
  219. dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
  220. dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
  221. }
  222. srcpY += src_pitchY;
  223. dstpY += dst_pitchY;
  224. srcpU += src_pitchUV;
  225. srcpV += src_pitchUV;
  226. dstpU += dst_pitchUV;
  227. dstpV += dst_pitchUV;
  228. }
  229. }
  230. static void process_frame_yuv420p(ColorMatrixContext *color,
  231. AVFilterBufferRef *dst, AVFilterBufferRef *src)
  232. {
  233. const unsigned char *srcpU = src->data[1];
  234. const unsigned char *srcpV = src->data[2];
  235. const unsigned char *srcpY = src->data[0];
  236. const unsigned char *srcpN = src->data[0] + src->linesize[0];
  237. const int src_pitchY = src->linesize[0];
  238. const int src_pitchUV = src->linesize[1];
  239. const int height = src->video->h;
  240. const int width = src->video->w;
  241. unsigned char *dstpU = dst->data[1];
  242. unsigned char *dstpV = dst->data[2];
  243. unsigned char *dstpY = dst->data[0];
  244. unsigned char *dstpN = dst->data[0] + dst->linesize[0];
  245. const int dst_pitchY = dst->linesize[0];
  246. const int dst_pitchUV = dst->linesize[1];
  247. const int c2 = color->yuv_convert[color->mode][0][1];
  248. const int c3 = color->yuv_convert[color->mode][0][2];
  249. const int c4 = color->yuv_convert[color->mode][1][1];
  250. const int c5 = color->yuv_convert[color->mode][1][2];
  251. const int c6 = color->yuv_convert[color->mode][2][1];
  252. const int c7 = color->yuv_convert[color->mode][2][2];
  253. int x, y;
  254. for (y = 0; y < height; y += 2) {
  255. for (x = 0; x < width; x += 2) {
  256. const int u = srcpU[x >> 1] - 128;
  257. const int v = srcpV[x >> 1] - 128;
  258. const int uvval = c2 * u + c3 * v + 1081344;
  259. dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
  260. dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
  261. dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
  262. dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
  263. dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
  264. dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
  265. }
  266. srcpY += src_pitchY << 1;
  267. dstpY += dst_pitchY << 1;
  268. srcpN += src_pitchY << 1;
  269. dstpN += dst_pitchY << 1;
  270. srcpU += src_pitchUV;
  271. srcpV += src_pitchUV;
  272. dstpU += dst_pitchUV;
  273. dstpV += dst_pitchUV;
  274. }
  275. }
  276. static int config_input(AVFilterLink *inlink)
  277. {
  278. AVFilterContext *ctx = inlink->dst;
  279. ColorMatrixContext *color = ctx->priv;
  280. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  281. color->hsub = pix_desc->log2_chroma_w;
  282. color->vsub = pix_desc->log2_chroma_h;
  283. av_log(ctx, AV_LOG_INFO, "%s -> %s\n", color->src, color->dst);
  284. return 0;
  285. }
  286. static int query_formats(AVFilterContext *ctx)
  287. {
  288. static const enum PixelFormat pix_fmts[] = {
  289. PIX_FMT_YUV422P,
  290. PIX_FMT_YUV420P,
  291. PIX_FMT_UYVY422,
  292. PIX_FMT_NONE
  293. };
  294. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  295. return 0;
  296. }
  297. static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
  298. {
  299. AVFilterBufferRef *picref =
  300. avfilter_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
  301. return picref;
  302. }
  303. static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
  304. {
  305. AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
  306. link->dst->outputs[0]->out_buf = outpicref;
  307. avfilter_start_frame(link->dst->outputs[0], outpicref);
  308. }
  309. static void end_frame(AVFilterLink *link)
  310. {
  311. AVFilterContext *ctx = link->dst;
  312. ColorMatrixContext *color = ctx->priv;
  313. AVFilterBufferRef *out = link->dst->outputs[0]->out_buf;
  314. if (link->cur_buf->format == PIX_FMT_YUV422P)
  315. process_frame_yuv422p(color, out, link->cur_buf);
  316. else if (link->cur_buf->format == PIX_FMT_YUV420P)
  317. process_frame_yuv420p(color, out, link->cur_buf);
  318. else
  319. process_frame_uyvy422(color, out, link->cur_buf);
  320. avfilter_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
  321. avfilter_end_frame(ctx->outputs[0]);
  322. avfilter_unref_buffer(link->cur_buf);
  323. }
  324. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  325. AVFilter avfilter_vf_colormatrix = {
  326. .name = "colormatrix",
  327. .description = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
  328. .priv_size = sizeof(ColorMatrixContext),
  329. .init = init,
  330. .query_formats = query_formats,
  331. .inputs = (AVFilterPad[]) {{ .name = "default",
  332. .type = AVMEDIA_TYPE_VIDEO,
  333. .config_props = config_input,
  334. .start_frame = start_frame,
  335. .get_video_buffer = get_video_buffer,
  336. .draw_slice = null_draw_slice,
  337. .end_frame = end_frame, },
  338. { .name = NULL }},
  339. .outputs = (AVFilterPad[]) {{ .name = "default",
  340. .type = AVMEDIA_TYPE_VIDEO, },
  341. { .name = NULL }},
  342. };