2 * ColorMatrix v2.2 for Avisynth 2.5.x
4 * Copyright (C) 2006-2007 Kevin Stone
6 * ColorMatrix 1.x is Copyright (C) Wilbert Dijkhof
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.
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.
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
25 * ColorMatrix 2.0 is based on the original ColorMatrix filter by Wilbert
26 * Dijkhof. It adds the ability to convert between any of: Rec.709, FCC,
27 * Rec.601, and SMPTE 240M. It also makes pre and post clipping optional,
28 * adds an option to use scaled or non-scaled coefficients, and more...
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/avstring.h"
38 #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
39 #define CB(n) av_clip_uint8(n)
41 static const double yuv_coeff[4][3][3] = {
42 { { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
43 { -0.3850, +0.5000, -0.1150 },
44 { -0.4540, -0.0460, +0.5000 } },
45 { { +0.5900, +0.1100, +0.3000 }, // FCC (1)
46 { -0.3310, +0.5000, -0.1690 },
47 { -0.4210, -0.0790, +0.5000 } },
48 { { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
49 { -0.3313, +0.5000, -0.1687 },
50 { -0.4187, -0.0813, +0.5000 } },
51 { { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
52 { -0.3840, +0.5000, -0.1160 },
53 { -0.4450, -0.0550, +0.5000 } },
57 int yuv_convert[16][3][3];
59 int source, dest, mode;
63 AVFilterBufferRef *outpicref;
86 static void inverse3x3(double im[3][3], const double m[3][3])
88 double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
90 ima = det * (me * mi - mf * mh);
91 imb = det * (mc * mh - mb * mi);
92 imc = det * (mb * mf - mc * me);
93 imd = det * (mf * mg - md * mi);
94 ime = det * (ma * mi - mc * mg);
95 imf = det * (mc * md - ma * mf);
96 img = det * (md * mh - me * mg);
97 imh = det * (mb * mg - ma * mh);
98 imi = det * (ma * me - mb * md);
101 static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
104 for (i = 0; i < 3; i++)
105 for (j = 0; j < 3; j++)
106 cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
109 static void calc_coefficients(AVFilterContext *ctx)
111 ColorMatrixContext *color = ctx->priv;
112 double rgb_coeffd[4][3][3];
113 double yuv_convertd[16][3][3];
117 for (i = 0; i < 4; i++)
118 inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
119 for (i = 0; i < 4; i++) {
120 for (j = 0; j < 4; j++) {
121 solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
122 for (k = 0; k < 3; k++) {
123 color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
124 color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
125 color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
127 if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
128 color->yuv_convert[v][2][0] != 0) {
129 av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
136 static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
138 static int get_color_mode_index(const char *name)
142 for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
143 if (!av_strcasecmp(color_modes[i], name))
148 static av_cold int init(AVFilterContext *ctx, const char *args)
150 ColorMatrixContext *color = ctx->priv;
154 if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
156 av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
157 av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
161 color->source = get_color_mode_index(color->src);
162 if (color->source < 0) {
163 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
164 return AVERROR(EINVAL);
167 color->dest = get_color_mode_index(color->dst);
168 if (color->dest < 0) {
169 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
170 return AVERROR(EINVAL);
173 if (color->source == color->dest) {
174 av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
175 return AVERROR(EINVAL);
178 color->mode = color->source * 4 + color->dest;
180 calc_coefficients(ctx);
185 static void process_frame_uyvy422(ColorMatrixContext *color,
186 AVFilterBufferRef *dst, AVFilterBufferRef *src)
188 const unsigned char *srcp = src->data[0];
189 const int src_pitch = src->linesize[0];
190 const int height = src->video->h;
191 const int width = src->video->w*2;
192 unsigned char *dstp = dst->data[0];
193 const int dst_pitch = dst->linesize[0];
194 const int c2 = color->yuv_convert[color->mode][0][1];
195 const int c3 = color->yuv_convert[color->mode][0][2];
196 const int c4 = color->yuv_convert[color->mode][1][1];
197 const int c5 = color->yuv_convert[color->mode][1][2];
198 const int c6 = color->yuv_convert[color->mode][2][1];
199 const int c7 = color->yuv_convert[color->mode][2][2];
202 for (y = 0; y < height; y++) {
203 for (x = 0; x < width; x += 4) {
204 const int u = srcp[x + 0] - 128;
205 const int v = srcp[x + 2] - 128;
206 const int uvval = c2 * u + c3 * v + 1081344;
207 dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
208 dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
209 dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
210 dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
217 static void process_frame_yuv422p(ColorMatrixContext *color,
218 AVFilterBufferRef *dst, AVFilterBufferRef *src)
220 const unsigned char *srcpU = src->data[1];
221 const unsigned char *srcpV = src->data[2];
222 const unsigned char *srcpY = src->data[0];
223 const int src_pitchY = src->linesize[0];
224 const int src_pitchUV = src->linesize[1];
225 const int height = src->video->h;
226 const int width = src->video->w;
227 unsigned char *dstpU = dst->data[1];
228 unsigned char *dstpV = dst->data[2];
229 unsigned char *dstpY = dst->data[0];
230 const int dst_pitchY = dst->linesize[0];
231 const int dst_pitchUV = dst->linesize[1];
232 const int c2 = color->yuv_convert[color->mode][0][1];
233 const int c3 = color->yuv_convert[color->mode][0][2];
234 const int c4 = color->yuv_convert[color->mode][1][1];
235 const int c5 = color->yuv_convert[color->mode][1][2];
236 const int c6 = color->yuv_convert[color->mode][2][1];
237 const int c7 = color->yuv_convert[color->mode][2][2];
240 for (y = 0; y < height; y++) {
241 for (x = 0; x < width; x += 2) {
242 const int u = srcpU[x >> 1] - 128;
243 const int v = srcpV[x >> 1] - 128;
244 const int uvval = c2 * u + c3 * v + 1081344;
245 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
246 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
247 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
248 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
252 srcpU += src_pitchUV;
253 srcpV += src_pitchUV;
254 dstpU += dst_pitchUV;
255 dstpV += dst_pitchUV;
259 static void process_frame_yuv420p(ColorMatrixContext *color,
260 AVFilterBufferRef *dst, AVFilterBufferRef *src)
262 const unsigned char *srcpU = src->data[1];
263 const unsigned char *srcpV = src->data[2];
264 const unsigned char *srcpY = src->data[0];
265 const unsigned char *srcpN = src->data[0] + src->linesize[0];
266 const int src_pitchY = src->linesize[0];
267 const int src_pitchUV = src->linesize[1];
268 const int height = src->video->h;
269 const int width = src->video->w;
270 unsigned char *dstpU = dst->data[1];
271 unsigned char *dstpV = dst->data[2];
272 unsigned char *dstpY = dst->data[0];
273 unsigned char *dstpN = dst->data[0] + dst->linesize[0];
274 const int dst_pitchY = dst->linesize[0];
275 const int dst_pitchUV = dst->linesize[1];
276 const int c2 = color->yuv_convert[color->mode][0][1];
277 const int c3 = color->yuv_convert[color->mode][0][2];
278 const int c4 = color->yuv_convert[color->mode][1][1];
279 const int c5 = color->yuv_convert[color->mode][1][2];
280 const int c6 = color->yuv_convert[color->mode][2][1];
281 const int c7 = color->yuv_convert[color->mode][2][2];
284 for (y = 0; y < height; y += 2) {
285 for (x = 0; x < width; x += 2) {
286 const int u = srcpU[x >> 1] - 128;
287 const int v = srcpV[x >> 1] - 128;
288 const int uvval = c2 * u + c3 * v + 1081344;
289 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
290 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
291 dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
292 dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
293 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
294 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
296 srcpY += src_pitchY << 1;
297 dstpY += dst_pitchY << 1;
298 srcpN += src_pitchY << 1;
299 dstpN += dst_pitchY << 1;
300 srcpU += src_pitchUV;
301 srcpV += src_pitchUV;
302 dstpU += dst_pitchUV;
303 dstpV += dst_pitchUV;
307 static int config_input(AVFilterLink *inlink)
309 AVFilterContext *ctx = inlink->dst;
310 ColorMatrixContext *color = ctx->priv;
311 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
313 color->hsub = pix_desc->log2_chroma_w;
314 color->vsub = pix_desc->log2_chroma_h;
316 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
321 static int query_formats(AVFilterContext *ctx)
323 static const enum PixelFormat pix_fmts[] = {
330 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
335 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
337 AVFilterBufferRef *picref =
338 ff_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
342 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
344 AVFilterContext *ctx = link->dst;
345 ColorMatrixContext *color = ctx->priv;
346 AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
348 color->outpicref = outpicref;
350 return ff_start_frame(link->dst->outputs[0], outpicref);
353 static int end_frame(AVFilterLink *link)
355 AVFilterContext *ctx = link->dst;
356 ColorMatrixContext *color = ctx->priv;
357 AVFilterBufferRef *out = color->outpicref;
359 if (link->cur_buf->format == PIX_FMT_YUV422P)
360 process_frame_yuv422p(color, out, link->cur_buf);
361 else if (link->cur_buf->format == PIX_FMT_YUV420P)
362 process_frame_yuv420p(color, out, link->cur_buf);
364 process_frame_uyvy422(color, out, link->cur_buf);
366 ff_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
367 return ff_end_frame(ctx->outputs[0]);
370 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
372 AVFilter avfilter_vf_colormatrix = {
373 .name = "colormatrix",
374 .description = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
376 .priv_size = sizeof(ColorMatrixContext),
378 .query_formats = query_formats,
380 .inputs = (AVFilterPad[]) {{ .name = "default",
381 .type = AVMEDIA_TYPE_VIDEO,
382 .config_props = config_input,
383 .min_perms = AV_PERM_READ | AV_PERM_WRITE,
384 .start_frame = start_frame,
385 .get_video_buffer = get_video_buffer,
386 .draw_slice = null_draw_slice,
387 .end_frame = end_frame, },
390 .outputs = (AVFilterPad[]) {{ .name = "default",
391 .type = AVMEDIA_TYPE_VIDEO, },