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...
34 #include "libavutil/pixdesc.h"
35 #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)
40 static const double yuv_coeff[4][3][3] = {
41 { { +0.7152, +0.0722, +0.2126 }, // Rec.709 (0)
42 { -0.3850, +0.5000, -0.1150 },
43 { -0.4540, -0.0460, +0.5000 } },
44 { { +0.5900, +0.1100, +0.3000 }, // FCC (1)
45 { -0.3310, +0.5000, -0.1690 },
46 { -0.4210, -0.0790, +0.5000 } },
47 { { +0.5870, +0.1140, +0.2990 }, // Rec.601 (ITU-R BT.470-2/SMPTE 170M) (2)
48 { -0.3313, +0.5000, -0.1687 },
49 { -0.4187, -0.0813, +0.5000 } },
50 { { +0.7010, +0.0870, +0.2120 }, // SMPTE 240M (3)
51 { -0.3840, +0.5000, -0.1160 },
52 { -0.4450, -0.0550, +0.5000 } },
56 int yuv_convert[16][3][3];
58 int source, dest, mode;
84 static void inverse3x3(double im[3][3], const double m[3][3])
86 double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
88 ima = det * (me * mi - mf * mh);
89 imb = det * (mc * mh - mb * mi);
90 imc = det * (mb * mf - mc * me);
91 imd = det * (mf * mg - md * mi);
92 ime = det * (ma * mi - mc * mg);
93 imf = det * (mc * md - ma * mf);
94 img = det * (md * mh - me * mg);
95 imh = det * (mb * mg - ma * mh);
96 imi = det * (ma * me - mb * md);
99 static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
102 for (i = 0; i < 3; i++)
103 for (j = 0; j < 3; j++)
104 cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
107 static void calc_coefficients(AVFilterContext *ctx)
109 ColorMatrixContext *color = ctx->priv;
110 double rgb_coeffd[4][3][3];
111 double yuv_convertd[16][3][3];
115 for (i = 0; i < 4; i++)
116 inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
117 for (i = 0; i < 4; i++) {
118 for (j = 0; j < 4; j++) {
119 solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
120 for (k = 0; k < 3; k++) {
121 color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
122 color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
123 color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
125 if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
126 color->yuv_convert[v][2][0] != 0) {
127 av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
134 static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
136 static int get_color_mode_index(const char *name)
140 for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
141 if (!av_strcasecmp(color_modes[i], name))
146 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
148 ColorMatrixContext *color = ctx->priv;
152 if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
154 av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
155 av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
159 color->source = get_color_mode_index(color->src);
160 if (color->source < 0) {
161 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
162 return AVERROR(EINVAL);
165 color->dest = get_color_mode_index(color->dst);
166 if (color->dest < 0) {
167 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
168 return AVERROR(EINVAL);
171 if (color->source == color->dest) {
172 av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
173 return AVERROR(EINVAL);
176 color->mode = color->source * 4 + color->dest;
178 calc_coefficients(ctx);
183 static void process_frame_uyvy422(ColorMatrixContext *color,
184 AVFilterBufferRef *dst, AVFilterBufferRef *src)
186 const unsigned char *srcp = src->data[0];
187 const int src_pitch = src->linesize[0];
188 const int height = src->video->h;
189 const int width = src->video->w*2;
190 unsigned char *dstp = dst->data[0];
191 const int dst_pitch = dst->linesize[0];
192 const int c2 = color->yuv_convert[color->mode][0][1];
193 const int c3 = color->yuv_convert[color->mode][0][2];
194 const int c4 = color->yuv_convert[color->mode][1][1];
195 const int c5 = color->yuv_convert[color->mode][1][2];
196 const int c6 = color->yuv_convert[color->mode][2][1];
197 const int c7 = color->yuv_convert[color->mode][2][2];
200 for (y = 0; y < height; y++) {
201 for (x = 0; x < width; x += 4) {
202 const int u = srcp[x + 0] - 128;
203 const int v = srcp[x + 2] - 128;
204 const int uvval = c2 * u + c3 * v + 1081344;
205 dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
206 dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
207 dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
208 dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
215 static void process_frame_yuv422p(ColorMatrixContext *color,
216 AVFilterBufferRef *dst, AVFilterBufferRef *src)
218 const unsigned char *srcpU = src->data[1];
219 const unsigned char *srcpV = src->data[2];
220 const unsigned char *srcpY = src->data[0];
221 const int src_pitchY = src->linesize[0];
222 const int src_pitchUV = src->linesize[1];
223 const int height = src->video->h;
224 const int width = src->video->w;
225 unsigned char *dstpU = dst->data[1];
226 unsigned char *dstpV = dst->data[2];
227 unsigned char *dstpY = dst->data[0];
228 const int dst_pitchY = dst->linesize[0];
229 const int dst_pitchUV = dst->linesize[1];
230 const int c2 = color->yuv_convert[color->mode][0][1];
231 const int c3 = color->yuv_convert[color->mode][0][2];
232 const int c4 = color->yuv_convert[color->mode][1][1];
233 const int c5 = color->yuv_convert[color->mode][1][2];
234 const int c6 = color->yuv_convert[color->mode][2][1];
235 const int c7 = color->yuv_convert[color->mode][2][2];
238 for (y = 0; y < height; y++) {
239 for (x = 0; x < width; x += 2) {
240 const int u = srcpU[x >> 1] - 128;
241 const int v = srcpV[x >> 1] - 128;
242 const int uvval = c2 * u + c3 * v + 1081344;
243 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
244 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
245 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
246 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
250 srcpU += src_pitchUV;
251 srcpV += src_pitchUV;
252 dstpU += dst_pitchUV;
253 dstpV += dst_pitchUV;
257 static void process_frame_yuv420p(ColorMatrixContext *color,
258 AVFilterBufferRef *dst, AVFilterBufferRef *src)
260 const unsigned char *srcpU = src->data[1];
261 const unsigned char *srcpV = src->data[2];
262 const unsigned char *srcpY = src->data[0];
263 const unsigned char *srcpN = src->data[0] + src->linesize[0];
264 const int src_pitchY = src->linesize[0];
265 const int src_pitchUV = src->linesize[1];
266 const int height = src->video->h;
267 const int width = src->video->w;
268 unsigned char *dstpU = dst->data[1];
269 unsigned char *dstpV = dst->data[2];
270 unsigned char *dstpY = dst->data[0];
271 unsigned char *dstpN = dst->data[0] + dst->linesize[0];
272 const int dst_pitchY = dst->linesize[0];
273 const int dst_pitchUV = dst->linesize[1];
274 const int c2 = color->yuv_convert[color->mode][0][1];
275 const int c3 = color->yuv_convert[color->mode][0][2];
276 const int c4 = color->yuv_convert[color->mode][1][1];
277 const int c5 = color->yuv_convert[color->mode][1][2];
278 const int c6 = color->yuv_convert[color->mode][2][1];
279 const int c7 = color->yuv_convert[color->mode][2][2];
282 for (y = 0; y < height; y += 2) {
283 for (x = 0; x < width; x += 2) {
284 const int u = srcpU[x >> 1] - 128;
285 const int v = srcpV[x >> 1] - 128;
286 const int uvval = c2 * u + c3 * v + 1081344;
287 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
288 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
289 dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
290 dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
291 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
292 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
294 srcpY += src_pitchY << 1;
295 dstpY += dst_pitchY << 1;
296 srcpN += src_pitchY << 1;
297 dstpN += dst_pitchY << 1;
298 srcpU += src_pitchUV;
299 srcpV += src_pitchUV;
300 dstpU += dst_pitchUV;
301 dstpV += dst_pitchUV;
305 static int config_input(AVFilterLink *inlink)
307 AVFilterContext *ctx = inlink->dst;
308 ColorMatrixContext *color = ctx->priv;
309 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
311 color->hsub = pix_desc->log2_chroma_w;
312 color->vsub = pix_desc->log2_chroma_h;
314 av_log(ctx, AV_LOG_INFO, "%s -> %s\n", color->src, color->dst);
319 static int query_formats(AVFilterContext *ctx)
321 static const enum PixelFormat pix_fmts[] = {
328 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
333 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
335 AVFilterBufferRef *picref =
336 avfilter_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
340 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
342 AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
344 link->dst->outputs[0]->out_buf = outpicref;
346 avfilter_start_frame(link->dst->outputs[0], outpicref);
349 static void end_frame(AVFilterLink *link)
351 AVFilterContext *ctx = link->dst;
352 ColorMatrixContext *color = ctx->priv;
353 AVFilterBufferRef *out = link->dst->outputs[0]->out_buf;
355 if (link->cur_buf->format == PIX_FMT_YUV422P)
356 process_frame_yuv422p(color, out, link->cur_buf);
357 else if (link->cur_buf->format == PIX_FMT_YUV420P)
358 process_frame_yuv420p(color, out, link->cur_buf);
360 process_frame_uyvy422(color, out, link->cur_buf);
362 avfilter_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
363 avfilter_end_frame(ctx->outputs[0]);
364 avfilter_unref_buffer(link->cur_buf);
367 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
369 AVFilter avfilter_vf_colormatrix = {
370 .name = "colormatrix",
371 .description = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
373 .priv_size = sizeof(ColorMatrixContext),
375 .query_formats = query_formats,
377 .inputs = (AVFilterPad[]) {{ .name = "default",
378 .type = AVMEDIA_TYPE_VIDEO,
379 .config_props = config_input,
380 .start_frame = start_frame,
381 .get_video_buffer = get_video_buffer,
382 .draw_slice = null_draw_slice,
383 .end_frame = end_frame, },
386 .outputs = (AVFilterPad[]) {{ .name = "default",
387 .type = AVMEDIA_TYPE_VIDEO, },