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...
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/avstring.h"
39 #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
40 #define CB(n) av_clip_uint8(n)
42 static const double yuv_coeff[4][3][3] = {
43 { { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
44 { -0.3850, +0.5000, -0.1150 },
45 { -0.4540, -0.0460, +0.5000 } },
46 { { +0.5900, +0.1100, +0.3000 }, // FCC (1)
47 { -0.3310, +0.5000, -0.1690 },
48 { -0.4210, -0.0790, +0.5000 } },
49 { { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
50 { -0.3313, +0.5000, -0.1687 },
51 { -0.4187, -0.0813, +0.5000 } },
52 { { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
53 { -0.3840, +0.5000, -0.1160 },
54 { -0.4450, -0.0550, +0.5000 } },
58 int yuv_convert[16][3][3];
60 int source, dest, mode;
64 AVFilterBufferRef *outpicref;
87 static void inverse3x3(double im[3][3], const double m[3][3])
89 double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
91 ima = det * (me * mi - mf * mh);
92 imb = det * (mc * mh - mb * mi);
93 imc = det * (mb * mf - mc * me);
94 imd = det * (mf * mg - md * mi);
95 ime = det * (ma * mi - mc * mg);
96 imf = det * (mc * md - ma * mf);
97 img = det * (md * mh - me * mg);
98 imh = det * (mb * mg - ma * mh);
99 imi = det * (ma * me - mb * md);
102 static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
105 for (i = 0; i < 3; i++)
106 for (j = 0; j < 3; j++)
107 cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
110 static void calc_coefficients(AVFilterContext *ctx)
112 ColorMatrixContext *color = ctx->priv;
113 double rgb_coeffd[4][3][3];
114 double yuv_convertd[16][3][3];
118 for (i = 0; i < 4; i++)
119 inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
120 for (i = 0; i < 4; i++) {
121 for (j = 0; j < 4; j++) {
122 solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
123 for (k = 0; k < 3; k++) {
124 color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
125 color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
126 color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
128 if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
129 color->yuv_convert[v][2][0] != 0) {
130 av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
137 static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
139 static int get_color_mode_index(const char *name)
143 for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
144 if (!av_strcasecmp(color_modes[i], name))
149 static av_cold int init(AVFilterContext *ctx, const char *args)
151 ColorMatrixContext *color = ctx->priv;
155 if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
157 av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
158 av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
162 color->source = get_color_mode_index(color->src);
163 if (color->source < 0) {
164 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
165 return AVERROR(EINVAL);
168 color->dest = get_color_mode_index(color->dst);
169 if (color->dest < 0) {
170 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
171 return AVERROR(EINVAL);
174 if (color->source == color->dest) {
175 av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
176 return AVERROR(EINVAL);
179 color->mode = color->source * 4 + color->dest;
181 calc_coefficients(ctx);
186 static void process_frame_uyvy422(ColorMatrixContext *color,
187 AVFilterBufferRef *dst, AVFilterBufferRef *src)
189 const unsigned char *srcp = src->data[0];
190 const int src_pitch = src->linesize[0];
191 const int height = src->video->h;
192 const int width = src->video->w*2;
193 unsigned char *dstp = dst->data[0];
194 const int dst_pitch = dst->linesize[0];
195 const int c2 = color->yuv_convert[color->mode][0][1];
196 const int c3 = color->yuv_convert[color->mode][0][2];
197 const int c4 = color->yuv_convert[color->mode][1][1];
198 const int c5 = color->yuv_convert[color->mode][1][2];
199 const int c6 = color->yuv_convert[color->mode][2][1];
200 const int c7 = color->yuv_convert[color->mode][2][2];
203 for (y = 0; y < height; y++) {
204 for (x = 0; x < width; x += 4) {
205 const int u = srcp[x + 0] - 128;
206 const int v = srcp[x + 2] - 128;
207 const int uvval = c2 * u + c3 * v + 1081344;
208 dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
209 dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
210 dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
211 dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
218 static void process_frame_yuv422p(ColorMatrixContext *color,
219 AVFilterBufferRef *dst, AVFilterBufferRef *src)
221 const unsigned char *srcpU = src->data[1];
222 const unsigned char *srcpV = src->data[2];
223 const unsigned char *srcpY = src->data[0];
224 const int src_pitchY = src->linesize[0];
225 const int src_pitchUV = src->linesize[1];
226 const int height = src->video->h;
227 const int width = src->video->w;
228 unsigned char *dstpU = dst->data[1];
229 unsigned char *dstpV = dst->data[2];
230 unsigned char *dstpY = dst->data[0];
231 const int dst_pitchY = dst->linesize[0];
232 const int dst_pitchUV = dst->linesize[1];
233 const int c2 = color->yuv_convert[color->mode][0][1];
234 const int c3 = color->yuv_convert[color->mode][0][2];
235 const int c4 = color->yuv_convert[color->mode][1][1];
236 const int c5 = color->yuv_convert[color->mode][1][2];
237 const int c6 = color->yuv_convert[color->mode][2][1];
238 const int c7 = color->yuv_convert[color->mode][2][2];
241 for (y = 0; y < height; y++) {
242 for (x = 0; x < width; x += 2) {
243 const int u = srcpU[x >> 1] - 128;
244 const int v = srcpV[x >> 1] - 128;
245 const int uvval = c2 * u + c3 * v + 1081344;
246 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
247 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
248 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
249 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
253 srcpU += src_pitchUV;
254 srcpV += src_pitchUV;
255 dstpU += dst_pitchUV;
256 dstpV += dst_pitchUV;
260 static void process_frame_yuv420p(ColorMatrixContext *color,
261 AVFilterBufferRef *dst, AVFilterBufferRef *src)
263 const unsigned char *srcpU = src->data[1];
264 const unsigned char *srcpV = src->data[2];
265 const unsigned char *srcpY = src->data[0];
266 const unsigned char *srcpN = src->data[0] + src->linesize[0];
267 const int src_pitchY = src->linesize[0];
268 const int src_pitchUV = src->linesize[1];
269 const int height = src->video->h;
270 const int width = src->video->w;
271 unsigned char *dstpU = dst->data[1];
272 unsigned char *dstpV = dst->data[2];
273 unsigned char *dstpY = dst->data[0];
274 unsigned char *dstpN = dst->data[0] + dst->linesize[0];
275 const int dst_pitchY = dst->linesize[0];
276 const int dst_pitchUV = dst->linesize[1];
277 const int c2 = color->yuv_convert[color->mode][0][1];
278 const int c3 = color->yuv_convert[color->mode][0][2];
279 const int c4 = color->yuv_convert[color->mode][1][1];
280 const int c5 = color->yuv_convert[color->mode][1][2];
281 const int c6 = color->yuv_convert[color->mode][2][1];
282 const int c7 = color->yuv_convert[color->mode][2][2];
285 for (y = 0; y < height; y += 2) {
286 for (x = 0; x < width; x += 2) {
287 const int u = srcpU[x >> 1] - 128;
288 const int v = srcpV[x >> 1] - 128;
289 const int uvval = c2 * u + c3 * v + 1081344;
290 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
291 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
292 dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
293 dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
294 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
295 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
297 srcpY += src_pitchY << 1;
298 dstpY += dst_pitchY << 1;
299 srcpN += src_pitchY << 1;
300 dstpN += dst_pitchY << 1;
301 srcpU += src_pitchUV;
302 srcpV += src_pitchUV;
303 dstpU += dst_pitchUV;
304 dstpV += dst_pitchUV;
308 static int config_input(AVFilterLink *inlink)
310 AVFilterContext *ctx = inlink->dst;
311 ColorMatrixContext *color = ctx->priv;
312 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
314 color->hsub = pix_desc->log2_chroma_w;
315 color->vsub = pix_desc->log2_chroma_h;
317 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
322 static int query_formats(AVFilterContext *ctx)
324 static const enum PixelFormat pix_fmts[] = {
331 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
336 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
338 AVFilterBufferRef *picref =
339 ff_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
343 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
345 AVFilterContext *ctx = link->dst;
346 ColorMatrixContext *color = ctx->priv;
347 AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
349 color->outpicref = outpicref;
351 return ff_start_frame(link->dst->outputs[0], outpicref);
354 static int end_frame(AVFilterLink *link)
356 AVFilterContext *ctx = link->dst;
357 ColorMatrixContext *color = ctx->priv;
358 AVFilterBufferRef *out = color->outpicref;
360 if (link->cur_buf->format == PIX_FMT_YUV422P)
361 process_frame_yuv422p(color, out, link->cur_buf);
362 else if (link->cur_buf->format == PIX_FMT_YUV420P)
363 process_frame_yuv420p(color, out, link->cur_buf);
365 process_frame_uyvy422(color, out, link->cur_buf);
367 ff_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
368 return ff_end_frame(ctx->outputs[0]);
371 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
373 AVFilter avfilter_vf_colormatrix = {
374 .name = "colormatrix",
375 .description = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
377 .priv_size = sizeof(ColorMatrixContext),
379 .query_formats = query_formats,
381 .inputs = (AVFilterPad[]) {{ .name = "default",
382 .type = AVMEDIA_TYPE_VIDEO,
383 .config_props = config_input,
384 .min_perms = AV_PERM_READ | AV_PERM_WRITE,
385 .start_frame = start_frame,
386 .get_video_buffer = get_video_buffer,
387 .draw_slice = null_draw_slice,
388 .end_frame = end_frame, },
391 .outputs = (AVFilterPad[]) {{ .name = "default",
392 .type = AVMEDIA_TYPE_VIDEO, },