]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_colormatrix.c
Merge commit 'd3810c47fe8c9509c28c65c0244e743c1d353daf'
[ffmpeg] / libavfilter / vf_colormatrix.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * @file
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...
29  */
30
31 #include <strings.h>
32 #include <float.h>
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "video.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/avstring.h"
38
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)
41
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 } },
55 };
56
57 typedef struct {
58     int yuv_convert[16][3][3];
59     int interlaced;
60     int source, dest, mode;
61     char src[256];
62     char dst[256];
63     int hsub, vsub;
64     AVFilterBufferRef *outpicref;
65 } ColorMatrixContext;
66
67 #define ma m[0][0]
68 #define mb m[0][1]
69 #define mc m[0][2]
70 #define md m[1][0]
71 #define me m[1][1]
72 #define mf m[1][2]
73 #define mg m[2][0]
74 #define mh m[2][1]
75 #define mi m[2][2]
76
77 #define ima im[0][0]
78 #define imb im[0][1]
79 #define imc im[0][2]
80 #define imd im[1][0]
81 #define ime im[1][1]
82 #define imf im[1][2]
83 #define img im[2][0]
84 #define imh im[2][1]
85 #define imi im[2][2]
86
87 static void inverse3x3(double im[3][3], const double m[3][3])
88 {
89     double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
90     det = 1.0 / det;
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);
100 }
101
102 static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
103 {
104     int i, j;
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];
108 }
109
110 static void calc_coefficients(AVFilterContext *ctx)
111 {
112     ColorMatrixContext *color = ctx->priv;
113     double rgb_coeffd[4][3][3];
114     double yuv_convertd[16][3][3];
115     int v = 0;
116     int i, j, k;
117
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]);
127             }
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");
131             }
132             v++;
133         }
134     }
135 }
136
137 static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
138
139 static int get_color_mode_index(const char *name)
140 {
141     int i;
142
143     for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
144         if (!av_strcasecmp(color_modes[i], name))
145             return i;
146     return -1;
147 }
148
149 static av_cold int init(AVFilterContext *ctx, const char *args)
150 {
151     ColorMatrixContext *color = ctx->priv;
152
153     if (!args)
154         goto usage;
155     if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
156     usage:
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");
159         return -1;
160     }
161
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);
166     }
167
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);
172     }
173
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);
177     }
178
179     color->mode = color->source * 4 + color->dest;
180
181     calc_coefficients(ctx);
182
183     return 0;
184 }
185
186 static void process_frame_uyvy422(ColorMatrixContext *color,
187                                   AVFilterBufferRef *dst, AVFilterBufferRef *src)
188 {
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];
201     int x, y;
202
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);
212         }
213         srcp += src_pitch;
214         dstp += dst_pitch;
215     }
216 }
217
218 static void process_frame_yuv422p(ColorMatrixContext *color,
219                                   AVFilterBufferRef *dst, AVFilterBufferRef *src)
220 {
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];
239     int x, y;
240
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);
250         }
251         srcpY += src_pitchY;
252         dstpY += dst_pitchY;
253         srcpU += src_pitchUV;
254         srcpV += src_pitchUV;
255         dstpU += dst_pitchUV;
256         dstpV += dst_pitchUV;
257     }
258 }
259
260 static void process_frame_yuv420p(ColorMatrixContext *color,
261                                   AVFilterBufferRef *dst, AVFilterBufferRef *src)
262 {
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];
283     int x, y;
284
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);
296         }
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;
305     }
306 }
307
308 static int config_input(AVFilterLink *inlink)
309 {
310     AVFilterContext *ctx = inlink->dst;
311     ColorMatrixContext *color = ctx->priv;
312     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
313
314     color->hsub = pix_desc->log2_chroma_w;
315     color->vsub = pix_desc->log2_chroma_h;
316
317     av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
318
319     return 0;
320 }
321
322 static int query_formats(AVFilterContext *ctx)
323 {
324     static const enum PixelFormat pix_fmts[] = {
325         PIX_FMT_YUV422P,
326         PIX_FMT_YUV420P,
327         PIX_FMT_UYVY422,
328         PIX_FMT_NONE
329     };
330
331     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
332
333     return 0;
334 }
335
336 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
337 {
338     AVFilterBufferRef *picref =
339         ff_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
340     return picref;
341 }
342
343 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
344 {
345     AVFilterContext *ctx = link->dst;
346     ColorMatrixContext *color = ctx->priv;
347     AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
348
349     color->outpicref = outpicref;
350
351     return ff_start_frame(link->dst->outputs[0], outpicref);
352 }
353
354 static int end_frame(AVFilterLink *link)
355 {
356     AVFilterContext *ctx = link->dst;
357     ColorMatrixContext *color = ctx->priv;
358     AVFilterBufferRef *out = color->outpicref;
359
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);
364     else
365         process_frame_uyvy422(color, out, link->cur_buf);
366
367     ff_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
368     return ff_end_frame(ctx->outputs[0]);
369 }
370
371 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
372
373 AVFilter avfilter_vf_colormatrix = {
374     .name          = "colormatrix",
375     .description   = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
376
377     .priv_size     = sizeof(ColorMatrixContext),
378     .init          = init,
379     .query_formats = query_formats,
380
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, },
389                                   { .name = NULL }},
390
391     .outputs   = (AVFilterPad[]) {{ .name             = "default",
392                                     .type             = AVMEDIA_TYPE_VIDEO, },
393                                   { .name = NULL }},
394 };