vf_colormatrix.c 15 KB

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