vf_colormatrix.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "internal.h"
  33. #include "video.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/avstring.h"
  37. #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
  38. #define CB(n) av_clip_uint8(n)
  39. static const double yuv_coeff[4][3][3] = {
  40. { { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
  41. { -0.3850, +0.5000, -0.1150 },
  42. { -0.4540, -0.0460, +0.5000 } },
  43. { { +0.5900, +0.1100, +0.3000 }, // FCC (1)
  44. { -0.3310, +0.5000, -0.1690 },
  45. { -0.4210, -0.0790, +0.5000 } },
  46. { { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
  47. { -0.3313, +0.5000, -0.1687 },
  48. { -0.4187, -0.0813, +0.5000 } },
  49. { { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
  50. { -0.3840, +0.5000, -0.1160 },
  51. { -0.4450, -0.0550, +0.5000 } },
  52. };
  53. enum ColorMode {
  54. COLOR_MODE_NONE = -1,
  55. COLOR_MODE_BT709,
  56. COLOR_MODE_FCC,
  57. COLOR_MODE_BT601,
  58. COLOR_MODE_SMPTE240M,
  59. COLOR_MODE_COUNT
  60. };
  61. typedef struct {
  62. const AVClass *class;
  63. int yuv_convert[16][3][3];
  64. int interlaced;
  65. enum ColorMode source, dest;
  66. int mode;
  67. int hsub, vsub;
  68. } ColorMatrixContext;
  69. #define OFFSET(x) offsetof(ColorMatrixContext, x)
  70. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  71. static const AVOption colormatrix_options[] = {
  72. { "src", "set source color matrix", OFFSET(source), AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
  73. { "dst", "set destination color matrix", OFFSET(dest), AV_OPT_TYPE_INT, {.i64=COLOR_MODE_NONE}, COLOR_MODE_NONE, COLOR_MODE_COUNT-1, .flags=FLAGS, .unit="color_mode" },
  74. { "bt709", "set BT.709 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT709}, .flags=FLAGS, .unit="color_mode" },
  75. { "fcc", "set FCC colorspace ", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_FCC}, .flags=FLAGS, .unit="color_mode" },
  76. { "bt601", "set BT.601 colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_BT601}, .flags=FLAGS, .unit="color_mode" },
  77. { "smpte240m", "set SMPTE-240M colorspace", 0, AV_OPT_TYPE_CONST, {.i64=COLOR_MODE_SMPTE240M}, .flags=FLAGS, .unit="color_mode" },
  78. { NULL }
  79. };
  80. AVFILTER_DEFINE_CLASS(colormatrix);
  81. #define ma m[0][0]
  82. #define mb m[0][1]
  83. #define mc m[0][2]
  84. #define md m[1][0]
  85. #define me m[1][1]
  86. #define mf m[1][2]
  87. #define mg m[2][0]
  88. #define mh m[2][1]
  89. #define mi m[2][2]
  90. #define ima im[0][0]
  91. #define imb im[0][1]
  92. #define imc im[0][2]
  93. #define imd im[1][0]
  94. #define ime im[1][1]
  95. #define imf im[1][2]
  96. #define img im[2][0]
  97. #define imh im[2][1]
  98. #define imi im[2][2]
  99. static void inverse3x3(double im[3][3], const double m[3][3])
  100. {
  101. double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
  102. det = 1.0 / det;
  103. ima = det * (me * mi - mf * mh);
  104. imb = det * (mc * mh - mb * mi);
  105. imc = det * (mb * mf - mc * me);
  106. imd = det * (mf * mg - md * mi);
  107. ime = det * (ma * mi - mc * mg);
  108. imf = det * (mc * md - ma * mf);
  109. img = det * (md * mh - me * mg);
  110. imh = det * (mb * mg - ma * mh);
  111. imi = det * (ma * me - mb * md);
  112. }
  113. static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
  114. {
  115. int i, j;
  116. for (i = 0; i < 3; i++)
  117. for (j = 0; j < 3; j++)
  118. cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
  119. }
  120. static void calc_coefficients(AVFilterContext *ctx)
  121. {
  122. ColorMatrixContext *color = ctx->priv;
  123. double rgb_coeffd[4][3][3];
  124. double yuv_convertd[16][3][3];
  125. int v = 0;
  126. int i, j, k;
  127. for (i = 0; i < 4; i++)
  128. inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
  129. for (i = 0; i < 4; i++) {
  130. for (j = 0; j < 4; j++) {
  131. solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
  132. for (k = 0; k < 3; k++) {
  133. color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
  134. color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
  135. color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
  136. }
  137. if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
  138. color->yuv_convert[v][2][0] != 0) {
  139. av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
  140. }
  141. v++;
  142. }
  143. }
  144. }
  145. static const char *color_modes[] = {"bt709", "fcc", "bt601", "smpte240m"};
  146. static av_cold int init(AVFilterContext *ctx)
  147. {
  148. ColorMatrixContext *color = ctx->priv;
  149. if (color->dest == COLOR_MODE_NONE) {
  150. av_log(ctx, AV_LOG_ERROR, "Unspecified destination color space\n");
  151. return AVERROR(EINVAL);
  152. }
  153. if (color->source == color->dest) {
  154. av_log(ctx, AV_LOG_ERROR, "Source and destination color space must not be identical\n");
  155. return AVERROR(EINVAL);
  156. }
  157. return 0;
  158. }
  159. static void process_frame_uyvy422(ColorMatrixContext *color,
  160. AVFrame *dst, AVFrame *src)
  161. {
  162. const unsigned char *srcp = src->data[0];
  163. const int src_pitch = src->linesize[0];
  164. const int height = src->height;
  165. const int width = src->width*2;
  166. unsigned char *dstp = dst->data[0];
  167. const int dst_pitch = dst->linesize[0];
  168. const int c2 = color->yuv_convert[color->mode][0][1];
  169. const int c3 = color->yuv_convert[color->mode][0][2];
  170. const int c4 = color->yuv_convert[color->mode][1][1];
  171. const int c5 = color->yuv_convert[color->mode][1][2];
  172. const int c6 = color->yuv_convert[color->mode][2][1];
  173. const int c7 = color->yuv_convert[color->mode][2][2];
  174. int x, y;
  175. for (y = 0; y < height; y++) {
  176. for (x = 0; x < width; x += 4) {
  177. const int u = srcp[x + 0] - 128;
  178. const int v = srcp[x + 2] - 128;
  179. const int uvval = c2 * u + c3 * v + 1081344;
  180. dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
  181. dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
  182. dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
  183. dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
  184. }
  185. srcp += src_pitch;
  186. dstp += dst_pitch;
  187. }
  188. }
  189. static void process_frame_yuv422p(ColorMatrixContext *color,
  190. AVFrame *dst, AVFrame *src)
  191. {
  192. const unsigned char *srcpU = src->data[1];
  193. const unsigned char *srcpV = src->data[2];
  194. const unsigned char *srcpY = src->data[0];
  195. const int src_pitchY = src->linesize[0];
  196. const int src_pitchUV = src->linesize[1];
  197. const int height = src->height;
  198. const int width = src->width;
  199. unsigned char *dstpU = dst->data[1];
  200. unsigned char *dstpV = dst->data[2];
  201. unsigned char *dstpY = dst->data[0];
  202. const int dst_pitchY = dst->linesize[0];
  203. const int dst_pitchUV = dst->linesize[1];
  204. const int c2 = color->yuv_convert[color->mode][0][1];
  205. const int c3 = color->yuv_convert[color->mode][0][2];
  206. const int c4 = color->yuv_convert[color->mode][1][1];
  207. const int c5 = color->yuv_convert[color->mode][1][2];
  208. const int c6 = color->yuv_convert[color->mode][2][1];
  209. const int c7 = color->yuv_convert[color->mode][2][2];
  210. int x, y;
  211. for (y = 0; y < height; y++) {
  212. for (x = 0; x < width; x += 2) {
  213. const int u = srcpU[x >> 1] - 128;
  214. const int v = srcpV[x >> 1] - 128;
  215. const int uvval = c2 * u + c3 * v + 1081344;
  216. dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
  217. dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
  218. dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
  219. dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
  220. }
  221. srcpY += src_pitchY;
  222. dstpY += dst_pitchY;
  223. srcpU += src_pitchUV;
  224. srcpV += src_pitchUV;
  225. dstpU += dst_pitchUV;
  226. dstpV += dst_pitchUV;
  227. }
  228. }
  229. static void process_frame_yuv420p(ColorMatrixContext *color,
  230. AVFrame *dst, AVFrame *src)
  231. {
  232. const unsigned char *srcpU = src->data[1];
  233. const unsigned char *srcpV = src->data[2];
  234. const unsigned char *srcpY = src->data[0];
  235. const unsigned char *srcpN = src->data[0] + src->linesize[0];
  236. const int src_pitchY = src->linesize[0];
  237. const int src_pitchUV = src->linesize[1];
  238. const int height = src->height;
  239. const int width = src->width;
  240. unsigned char *dstpU = dst->data[1];
  241. unsigned char *dstpV = dst->data[2];
  242. unsigned char *dstpY = dst->data[0];
  243. unsigned char *dstpN = dst->data[0] + dst->linesize[0];
  244. const int dst_pitchY = dst->linesize[0];
  245. const int dst_pitchUV = dst->linesize[1];
  246. const int c2 = color->yuv_convert[color->mode][0][1];
  247. const int c3 = color->yuv_convert[color->mode][0][2];
  248. const int c4 = color->yuv_convert[color->mode][1][1];
  249. const int c5 = color->yuv_convert[color->mode][1][2];
  250. const int c6 = color->yuv_convert[color->mode][2][1];
  251. const int c7 = color->yuv_convert[color->mode][2][2];
  252. int x, y;
  253. for (y = 0; y < height; y += 2) {
  254. for (x = 0; x < width; x += 2) {
  255. const int u = srcpU[x >> 1] - 128;
  256. const int v = srcpV[x >> 1] - 128;
  257. const int uvval = c2 * u + c3 * v + 1081344;
  258. dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
  259. dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
  260. dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
  261. dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
  262. dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
  263. dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
  264. }
  265. srcpY += src_pitchY << 1;
  266. dstpY += dst_pitchY << 1;
  267. srcpN += src_pitchY << 1;
  268. dstpN += dst_pitchY << 1;
  269. srcpU += src_pitchUV;
  270. srcpV += src_pitchUV;
  271. dstpU += dst_pitchUV;
  272. dstpV += dst_pitchUV;
  273. }
  274. }
  275. static int config_input(AVFilterLink *inlink)
  276. {
  277. AVFilterContext *ctx = inlink->dst;
  278. ColorMatrixContext *color = ctx->priv;
  279. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
  280. color->hsub = pix_desc->log2_chroma_w;
  281. color->vsub = pix_desc->log2_chroma_h;
  282. av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n",
  283. color_modes[color->source], color_modes[color->dest]);
  284. return 0;
  285. }
  286. static int query_formats(AVFilterContext *ctx)
  287. {
  288. static const enum AVPixelFormat pix_fmts[] = {
  289. AV_PIX_FMT_YUV422P,
  290. AV_PIX_FMT_YUV420P,
  291. AV_PIX_FMT_UYVY422,
  292. AV_PIX_FMT_NONE
  293. };
  294. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  295. return 0;
  296. }
  297. static int filter_frame(AVFilterLink *link, AVFrame *in)
  298. {
  299. AVFilterContext *ctx = link->dst;
  300. ColorMatrixContext *color = ctx->priv;
  301. AVFilterLink *outlink = ctx->outputs[0];
  302. AVFrame *out;
  303. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  304. if (!out) {
  305. av_frame_free(&in);
  306. return AVERROR(ENOMEM);
  307. }
  308. av_frame_copy_props(out, in);
  309. if (color->source == COLOR_MODE_NONE) {
  310. enum AVColorSpace cs = av_frame_get_colorspace(in);
  311. enum ColorMode source;
  312. switch(cs) {
  313. case AVCOL_SPC_BT709 : source = COLOR_MODE_BT709 ; break;
  314. case AVCOL_SPC_FCC : source = COLOR_MODE_FCC ; break;
  315. case AVCOL_SPC_SMPTE240M : source = COLOR_MODE_SMPTE240M ; break;
  316. case AVCOL_SPC_BT470BG : source = COLOR_MODE_BT601 ; break;
  317. default :
  318. av_log(ctx, AV_LOG_ERROR, "Input frame does not specify a supported colorspace, and none has been specified as source either\n");
  319. return AVERROR(EINVAL);
  320. }
  321. color->mode = source * 4 + color->dest;
  322. } else
  323. color->mode = color->source * 4 + color->dest;
  324. switch(color->dest) {
  325. case COLOR_MODE_BT709 : av_frame_set_colorspace(out, AVCOL_SPC_BT709) ; break;
  326. case COLOR_MODE_FCC : av_frame_set_colorspace(out, AVCOL_SPC_FCC) ; break;
  327. case COLOR_MODE_SMPTE240M: av_frame_set_colorspace(out, AVCOL_SPC_SMPTE240M); break;
  328. case COLOR_MODE_BT601 : av_frame_set_colorspace(out, AVCOL_SPC_BT470BG) ; break;
  329. }
  330. calc_coefficients(ctx);
  331. if (in->format == AV_PIX_FMT_YUV422P)
  332. process_frame_yuv422p(color, out, in);
  333. else if (in->format == AV_PIX_FMT_YUV420P)
  334. process_frame_yuv420p(color, out, in);
  335. else
  336. process_frame_uyvy422(color, out, in);
  337. av_frame_free(&in);
  338. return ff_filter_frame(outlink, out);
  339. }
  340. static const AVFilterPad colormatrix_inputs[] = {
  341. {
  342. .name = "default",
  343. .type = AVMEDIA_TYPE_VIDEO,
  344. .config_props = config_input,
  345. .filter_frame = filter_frame,
  346. },
  347. { NULL }
  348. };
  349. static const AVFilterPad colormatrix_outputs[] = {
  350. {
  351. .name = "default",
  352. .type = AVMEDIA_TYPE_VIDEO,
  353. },
  354. { NULL }
  355. };
  356. AVFilter ff_vf_colormatrix = {
  357. .name = "colormatrix",
  358. .description = NULL_IF_CONFIG_SMALL("Convert color matrix."),
  359. .priv_size = sizeof(ColorMatrixContext),
  360. .init = init,
  361. .query_formats = query_formats,
  362. .inputs = colormatrix_inputs,
  363. .outputs = colormatrix_outputs,
  364. .priv_class = &colormatrix_class,
  365. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  366. };