Browse code

libavfilter: add colormatrix filter

Ported by: Baptiste Coudurier
cleanup+fate by ubitux
For detailed authorship of the original code please see avisynth

multiple authors authored on 2012/03/10 12:04:19
Showing 6 changed files
... ...
@@ -1670,6 +1670,7 @@ aresample_filter_deps="swresample"
1670 1670
 ass_filter_deps="libass"
1671 1671
 blackframe_filter_deps="gpl"
1672 1672
 boxblur_filter_deps="gpl"
1673
+colormatrix_filter_deps="gpl"
1673 1674
 cropdetect_filter_deps="gpl"
1674 1675
 delogo_filter_deps="gpl"
1675 1676
 drawtext_filter_deps="libfreetype"
... ...
@@ -918,6 +918,18 @@ boxblur=min(h\,w)/10:1:min(cw\,ch)/10:1
918 918
 
919 919
 @end itemize
920 920
 
921
+@section colormatrix
922
+
923
+The colormatrix filter allows conversion between any of the following color
924
+space: BT.709 (@var{bt709}), BT.601 (@var{bt601}), SMPTE-240M (@var{smpte240m})
925
+and FCC (@var{fcc}).
926
+
927
+The syntax of the parameters is @var{source}:@var{destination}:
928
+
929
+@example
930
+colormatrix=bt601:smpte240m
931
+@end example
932
+
921 933
 @section copy
922 934
 
923 935
 Copy the input source unchanged to the output. Mainly useful for
... ...
@@ -52,6 +52,7 @@ OBJS-$(CONFIG_BBOX_FILTER)                   += bbox.o vf_bbox.o
52 52
 OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
53 53
 OBJS-$(CONFIG_BLACKFRAME_FILTER)             += vf_blackframe.o
54 54
 OBJS-$(CONFIG_BOXBLUR_FILTER)                += vf_boxblur.o
55
+OBJS-$(CONFIG_COLORMATRIX_FILTER)            += vf_colormatrix.o
55 56
 OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
56 57
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
57 58
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
... ...
@@ -60,6 +60,7 @@ void avfilter_register_all(void)
60 60
     REGISTER_FILTER (BLACKDETECT, blackdetect, vf);
61 61
     REGISTER_FILTER (BLACKFRAME,  blackframe,  vf);
62 62
     REGISTER_FILTER (BOXBLUR,     boxblur,     vf);
63
+    REGISTER_FILTER (COLORMATRIX, colormatrix, vf);
63 64
     REGISTER_FILTER (COPY,        copy,        vf);
64 65
     REGISTER_FILTER (CROP,        crop,        vf);
65 66
     REGISTER_FILTER (CROPDETECT,  cropdetect,  vf);
66 67
new file mode 100644
... ...
@@ -0,0 +1,389 @@
0
+/*
1
+ * ColorMatrix v2.2 for Avisynth 2.5.x
2
+ *
3
+ * Copyright (C) 2006-2007 Kevin Stone
4
+ *
5
+ * ColorMatrix 1.x is Copyright (C) Wilbert Dijkhof
6
+ *
7
+ * This program is free software; you can redistribute it and/or modify it
8
+ * under the terms of the GNU General Public License as published by the
9
+ * Free Software Foundation; either version 2 of the License, or (at your
10
+ * option) any later version.
11
+ *
12
+ * This program is distributed in the hope that it will be useful, but
13
+ * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15
+ * License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with this program; if not, write to the Free Software Foundation,
19
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
+ */
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
+
30
+#include <strings.h>
31
+#include <float.h>
32
+#include "avfilter.h"
33
+#include "libavutil/pixdesc.h"
34
+#include "libavutil/avstring.h"
35
+
36
+#define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
37
+#define CB(n) av_clip_uint8(n)
38
+
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
+
54
+typedef struct {
55
+    int yuv_convert[16][3][3];
56
+    int interlaced;
57
+    int source, dest, mode;
58
+    char src[256];
59
+    char dst[256];
60
+    int hsub, vsub;
61
+} ColorMatrixContext;
62
+
63
+#define ma m[0][0]
64
+#define mb m[0][1]
65
+#define mc m[0][2]
66
+#define md m[1][0]
67
+#define me m[1][1]
68
+#define mf m[1][2]
69
+#define mg m[2][0]
70
+#define mh m[2][1]
71
+#define mi m[2][2]
72
+
73
+#define ima im[0][0]
74
+#define imb im[0][1]
75
+#define imc im[0][2]
76
+#define imd im[1][0]
77
+#define ime im[1][1]
78
+#define imf im[1][2]
79
+#define img im[2][0]
80
+#define imh im[2][1]
81
+#define imi im[2][2]
82
+
83
+static void inverse3x3(double im[3][3], const double m[3][3])
84
+{
85
+    double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
86
+    det = 1.0 / det;
87
+    ima = det * (me * mi - mf * mh);
88
+    imb = det * (mc * mh - mb * mi);
89
+    imc = det * (mb * mf - mc * me);
90
+    imd = det * (mf * mg - md * mi);
91
+    ime = det * (ma * mi - mc * mg);
92
+    imf = det * (mc * md - ma * mf);
93
+    img = det * (md * mh - me * mg);
94
+    imh = det * (mb * mg - ma * mh);
95
+    imi = det * (ma * me - mb * md);
96
+}
97
+
98
+static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
99
+{
100
+    int i, j;
101
+    for (i = 0; i < 3; i++)
102
+        for (j = 0; j < 3; j++)
103
+            cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
104
+}
105
+
106
+static void calc_coefficients(AVFilterContext *ctx)
107
+{
108
+    ColorMatrixContext *color = ctx->priv;
109
+    double rgb_coeffd[4][3][3];
110
+    double yuv_convertd[16][3][3];
111
+    int v = 0;
112
+    int i, j, k;
113
+
114
+    for (i = 0; i < 4; i++)
115
+        inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
116
+    for (i = 0; i < 4; i++) {
117
+        for (j = 0; j < 4; j++) {
118
+            solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
119
+            for (k = 0; k < 3; k++) {
120
+                color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
121
+                color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
122
+                color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
123
+            }
124
+            if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
125
+                color->yuv_convert[v][2][0] != 0) {
126
+                av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
127
+            }
128
+            v++;
129
+        }
130
+    }
131
+}
132
+
133
+static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
134
+
135
+static int get_color_mode_index(const char *name)
136
+{
137
+    int i;
138
+
139
+    for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
140
+        if (!av_strcasecmp(color_modes[i], name))
141
+            return i;
142
+    return -1;
143
+}
144
+
145
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
146
+{
147
+    ColorMatrixContext *color = ctx->priv;
148
+
149
+    if (!args)
150
+        goto usage;
151
+    if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
152
+    usage:
153
+        av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
154
+        av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
155
+        return -1;
156
+    }
157
+
158
+    color->source = get_color_mode_index(color->src);
159
+    if (color->source < 0) {
160
+        av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
161
+        return AVERROR(EINVAL);
162
+    }
163
+
164
+    color->dest = get_color_mode_index(color->dst);
165
+    if (color->dest < 0) {
166
+        av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
167
+        return AVERROR(EINVAL);
168
+    }
169
+
170
+    if (color->source == color->dest) {
171
+        av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
172
+        return AVERROR(EINVAL);
173
+    }
174
+
175
+    color->mode = color->source * 4 + color->dest;
176
+
177
+    calc_coefficients(ctx);
178
+
179
+    return 0;
180
+}
181
+
182
+static void process_frame_uyvy422(ColorMatrixContext *color,
183
+                                  AVFilterBufferRef *dst, AVFilterBufferRef *src)
184
+{
185
+    const unsigned char *srcp = src->data[0];
186
+    const int src_pitch = src->linesize[0];
187
+    const int height = src->video->h;
188
+    const int width = src->video->w*2;
189
+    unsigned char *dstp = dst->data[0];
190
+    const int dst_pitch = dst->linesize[0];
191
+    const int c2 = color->yuv_convert[color->mode][0][1];
192
+    const int c3 = color->yuv_convert[color->mode][0][2];
193
+    const int c4 = color->yuv_convert[color->mode][1][1];
194
+    const int c5 = color->yuv_convert[color->mode][1][2];
195
+    const int c6 = color->yuv_convert[color->mode][2][1];
196
+    const int c7 = color->yuv_convert[color->mode][2][2];
197
+    int x, y;
198
+
199
+    for (y = 0; y < height; y++) {
200
+        for (x = 0; x < width; x += 4) {
201
+            const int u = srcp[x + 0] - 128;
202
+            const int v = srcp[x + 2] - 128;
203
+            const int uvval = c2 * u + c3 * v + 1081344;
204
+            dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
205
+            dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
206
+            dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
207
+            dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
208
+        }
209
+        srcp += src_pitch;
210
+        dstp += dst_pitch;
211
+    }
212
+}
213
+
214
+static void process_frame_yuv422p(ColorMatrixContext *color,
215
+                                  AVFilterBufferRef *dst, AVFilterBufferRef *src)
216
+{
217
+    const unsigned char *srcpU = src->data[1];
218
+    const unsigned char *srcpV = src->data[2];
219
+    const unsigned char *srcpY = src->data[0];
220
+    const int src_pitchY  = src->linesize[0];
221
+    const int src_pitchUV = src->linesize[1];
222
+    const int height = src->video->h;
223
+    const int width = src->video->w;
224
+    unsigned char *dstpU = dst->data[1];
225
+    unsigned char *dstpV = dst->data[2];
226
+    unsigned char *dstpY = dst->data[0];
227
+    const int dst_pitchY  = dst->linesize[0];
228
+    const int dst_pitchUV = dst->linesize[1];
229
+    const int c2 = color->yuv_convert[color->mode][0][1];
230
+    const int c3 = color->yuv_convert[color->mode][0][2];
231
+    const int c4 = color->yuv_convert[color->mode][1][1];
232
+    const int c5 = color->yuv_convert[color->mode][1][2];
233
+    const int c6 = color->yuv_convert[color->mode][2][1];
234
+    const int c7 = color->yuv_convert[color->mode][2][2];
235
+    int x, y;
236
+
237
+    for (y = 0; y < height; y++) {
238
+        for (x = 0; x < width; x += 2) {
239
+            const int u = srcpU[x >> 1] - 128;
240
+            const int v = srcpV[x >> 1] - 128;
241
+            const int uvval = c2 * u + c3 * v + 1081344;
242
+            dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
243
+            dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
244
+            dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
245
+            dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
246
+        }
247
+        srcpY += src_pitchY;
248
+        dstpY += dst_pitchY;
249
+        srcpU += src_pitchUV;
250
+        srcpV += src_pitchUV;
251
+        dstpU += dst_pitchUV;
252
+        dstpV += dst_pitchUV;
253
+    }
254
+}
255
+
256
+static void process_frame_yuv420p(ColorMatrixContext *color,
257
+                                  AVFilterBufferRef *dst, AVFilterBufferRef *src)
258
+{
259
+    const unsigned char *srcpU = src->data[1];
260
+    const unsigned char *srcpV = src->data[2];
261
+    const unsigned char *srcpY = src->data[0];
262
+    const unsigned char *srcpN = src->data[0] + src->linesize[0];
263
+    const int src_pitchY  = src->linesize[0];
264
+    const int src_pitchUV = src->linesize[1];
265
+    const int height = src->video->h;
266
+    const int width = src->video->w;
267
+    unsigned char *dstpU = dst->data[1];
268
+    unsigned char *dstpV = dst->data[2];
269
+    unsigned char *dstpY = dst->data[0];
270
+    unsigned char *dstpN = dst->data[0] + dst->linesize[0];
271
+    const int dst_pitchY  = dst->linesize[0];
272
+    const int dst_pitchUV = dst->linesize[1];
273
+    const int c2 = color->yuv_convert[color->mode][0][1];
274
+    const int c3 = color->yuv_convert[color->mode][0][2];
275
+    const int c4 = color->yuv_convert[color->mode][1][1];
276
+    const int c5 = color->yuv_convert[color->mode][1][2];
277
+    const int c6 = color->yuv_convert[color->mode][2][1];
278
+    const int c7 = color->yuv_convert[color->mode][2][2];
279
+    int x, y;
280
+
281
+    for (y = 0; y < height; y += 2) {
282
+        for (x = 0; x < width; x += 2) {
283
+            const int u = srcpU[x >> 1] - 128;
284
+            const int v = srcpV[x >> 1] - 128;
285
+            const int uvval = c2 * u + c3 * v + 1081344;
286
+            dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
287
+            dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
288
+            dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
289
+            dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
290
+            dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
291
+            dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
292
+        }
293
+        srcpY += src_pitchY << 1;
294
+        dstpY += dst_pitchY << 1;
295
+        srcpN += src_pitchY << 1;
296
+        dstpN += dst_pitchY << 1;
297
+        srcpU += src_pitchUV;
298
+        srcpV += src_pitchUV;
299
+        dstpU += dst_pitchUV;
300
+        dstpV += dst_pitchUV;
301
+    }
302
+}
303
+
304
+static int config_input(AVFilterLink *inlink)
305
+{
306
+    AVFilterContext *ctx = inlink->dst;
307
+    ColorMatrixContext *color = ctx->priv;
308
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
309
+
310
+    color->hsub = pix_desc->log2_chroma_w;
311
+    color->vsub = pix_desc->log2_chroma_h;
312
+
313
+    av_log(ctx, AV_LOG_INFO, "%s -> %s\n", color->src, color->dst);
314
+
315
+    return 0;
316
+}
317
+
318
+static int query_formats(AVFilterContext *ctx)
319
+{
320
+    static const enum PixelFormat pix_fmts[] = {
321
+        PIX_FMT_YUV422P,
322
+        PIX_FMT_YUV420P,
323
+        PIX_FMT_UYVY422,
324
+        PIX_FMT_NONE
325
+    };
326
+
327
+    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
328
+
329
+    return 0;
330
+}
331
+
332
+static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
333
+{
334
+    AVFilterBufferRef *picref =
335
+        avfilter_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
336
+    return picref;
337
+}
338
+
339
+static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
340
+{
341
+    AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
342
+
343
+    link->dst->outputs[0]->out_buf = outpicref;
344
+
345
+    avfilter_start_frame(link->dst->outputs[0], outpicref);
346
+}
347
+
348
+static void end_frame(AVFilterLink *link)
349
+{
350
+    AVFilterContext *ctx = link->dst;
351
+    ColorMatrixContext *color = ctx->priv;
352
+    AVFilterBufferRef *out = link->dst->outputs[0]->out_buf;
353
+
354
+    if (link->cur_buf->format == PIX_FMT_YUV422P)
355
+        process_frame_yuv422p(color, out, link->cur_buf);
356
+    else if (link->cur_buf->format == PIX_FMT_YUV420P)
357
+        process_frame_yuv420p(color, out, link->cur_buf);
358
+    else
359
+        process_frame_uyvy422(color, out, link->cur_buf);
360
+
361
+    avfilter_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
362
+    avfilter_end_frame(ctx->outputs[0]);
363
+    avfilter_unref_buffer(link->cur_buf);
364
+}
365
+
366
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
367
+
368
+AVFilter avfilter_vf_colormatrix = {
369
+    .name          = "colormatrix",
370
+    .description   = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
371
+
372
+    .priv_size     = sizeof(ColorMatrixContext),
373
+    .init          = init,
374
+    .query_formats = query_formats,
375
+
376
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
377
+                                    .type             = AVMEDIA_TYPE_VIDEO,
378
+                                    .config_props     = config_input,
379
+                                    .start_frame      = start_frame,
380
+                                    .get_video_buffer = get_video_buffer,
381
+                                    .draw_slice       = null_draw_slice,
382
+                                    .end_frame        = end_frame, },
383
+                                  { .name = NULL }},
384
+
385
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
386
+                                    .type             = AVMEDIA_TYPE_VIDEO, },
387
+                                  { .name = NULL }},
388
+};
... ...
@@ -28,6 +28,11 @@ do_lavfi() {
28 28
     fi
29 29
 }
30 30
 
31
+do_lavfi_colormatrix() {
32
+    do_lavfi "${1}1" "$1=$4:$5,$1=$5:$3,$1=$3:$4,$1=$4:$3,$1=$3:$5,$1=$5:$2"
33
+    do_lavfi "${1}2" "$1=$2:$3,$1=$3:$2,$1=$2:$4,$1=$4:$2,$1=$2:$5,$1=$5:$4"
34
+}
35
+
31 36
 do_lavfi "crop"               "crop=iw-100:ih-100:100:100"
32 37
 do_lavfi "crop_scale"         "crop=iw-100:ih-100:100:100,scale=400:-1"
33 38
 do_lavfi "crop_scale_vflip"   "null,null,crop=iw-200:ih-200:200:200,crop=iw-20:ih-20:20:20,scale=200:200,scale=250:250,vflip,vflip,null,scale=200:200,crop=iw-100:ih-100:100:100,vflip,scale=200:200,null,vflip,crop=iw-100:ih-100:100:100,null"
... ...
@@ -55,6 +60,8 @@ do_lavfi "vflip"              "vflip"
55 55
 do_lavfi "vflip_crop"         "vflip,crop=iw-100:ih-100:100:100"
56 56
 do_lavfi "vflip_vflip"        "vflip,vflip"
57 57
 
58
+do_lavfi_colormatrix "colormatrix" bt709 fcc bt601 smpte240m
59
+
58 60
 do_lavfi_pixfmts(){
59 61
     test ${test%_[bl]e} = pixfmts_$1 || return 0
60 62
     filter=$1