]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_colormatrix.c
aacdec: fix "may be used uninitialized" warning
[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 "libavutil/pixdesc.h"
35 #include "libavutil/avstring.h"
36
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)
39
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 } },
53 };
54
55 typedef struct {
56     int yuv_convert[16][3][3];
57     int interlaced;
58     int source, dest, mode;
59     char src[256];
60     char dst[256];
61     int hsub, vsub;
62 } ColorMatrixContext;
63
64 #define ma m[0][0]
65 #define mb m[0][1]
66 #define mc m[0][2]
67 #define md m[1][0]
68 #define me m[1][1]
69 #define mf m[1][2]
70 #define mg m[2][0]
71 #define mh m[2][1]
72 #define mi m[2][2]
73
74 #define ima im[0][0]
75 #define imb im[0][1]
76 #define imc im[0][2]
77 #define imd im[1][0]
78 #define ime im[1][1]
79 #define imf im[1][2]
80 #define img im[2][0]
81 #define imh im[2][1]
82 #define imi im[2][2]
83
84 static void inverse3x3(double im[3][3], const double m[3][3])
85 {
86     double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
87     det = 1.0 / det;
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);
97 }
98
99 static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
100 {
101     int i, j;
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];
105 }
106
107 static void calc_coefficients(AVFilterContext *ctx)
108 {
109     ColorMatrixContext *color = ctx->priv;
110     double rgb_coeffd[4][3][3];
111     double yuv_convertd[16][3][3];
112     int v = 0;
113     int i, j, k;
114
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]);
124             }
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");
128             }
129             v++;
130         }
131     }
132 }
133
134 static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
135
136 static int get_color_mode_index(const char *name)
137 {
138     int i;
139
140     for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
141         if (!av_strcasecmp(color_modes[i], name))
142             return i;
143     return -1;
144 }
145
146 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
147 {
148     ColorMatrixContext *color = ctx->priv;
149
150     if (!args)
151         goto usage;
152     if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
153     usage:
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");
156         return -1;
157     }
158
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);
163     }
164
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);
169     }
170
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);
174     }
175
176     color->mode = color->source * 4 + color->dest;
177
178     calc_coefficients(ctx);
179
180     return 0;
181 }
182
183 static void process_frame_uyvy422(ColorMatrixContext *color,
184                                   AVFilterBufferRef *dst, AVFilterBufferRef *src)
185 {
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];
198     int x, y;
199
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);
209         }
210         srcp += src_pitch;
211         dstp += dst_pitch;
212     }
213 }
214
215 static void process_frame_yuv422p(ColorMatrixContext *color,
216                                   AVFilterBufferRef *dst, AVFilterBufferRef *src)
217 {
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];
236     int x, y;
237
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);
247         }
248         srcpY += src_pitchY;
249         dstpY += dst_pitchY;
250         srcpU += src_pitchUV;
251         srcpV += src_pitchUV;
252         dstpU += dst_pitchUV;
253         dstpV += dst_pitchUV;
254     }
255 }
256
257 static void process_frame_yuv420p(ColorMatrixContext *color,
258                                   AVFilterBufferRef *dst, AVFilterBufferRef *src)
259 {
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];
280     int x, y;
281
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);
293         }
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;
302     }
303 }
304
305 static int config_input(AVFilterLink *inlink)
306 {
307     AVFilterContext *ctx = inlink->dst;
308     ColorMatrixContext *color = ctx->priv;
309     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
310
311     color->hsub = pix_desc->log2_chroma_w;
312     color->vsub = pix_desc->log2_chroma_h;
313
314     av_log(ctx, AV_LOG_INFO, "%s -> %s\n", color->src, color->dst);
315
316     return 0;
317 }
318
319 static int query_formats(AVFilterContext *ctx)
320 {
321     static const enum PixelFormat pix_fmts[] = {
322         PIX_FMT_YUV422P,
323         PIX_FMT_YUV420P,
324         PIX_FMT_UYVY422,
325         PIX_FMT_NONE
326     };
327
328     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
329
330     return 0;
331 }
332
333 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
334 {
335     AVFilterBufferRef *picref =
336         avfilter_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
337     return picref;
338 }
339
340 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
341 {
342     AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
343
344     link->dst->outputs[0]->out_buf = outpicref;
345
346     avfilter_start_frame(link->dst->outputs[0], outpicref);
347 }
348
349 static void end_frame(AVFilterLink *link)
350 {
351     AVFilterContext *ctx = link->dst;
352     ColorMatrixContext *color = ctx->priv;
353     AVFilterBufferRef *out = link->dst->outputs[0]->out_buf;
354
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);
359     else
360         process_frame_uyvy422(color, out, link->cur_buf);
361
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);
365 }
366
367 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
368
369 AVFilter avfilter_vf_colormatrix = {
370     .name          = "colormatrix",
371     .description   = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
372
373     .priv_size     = sizeof(ColorMatrixContext),
374     .init          = init,
375     .query_formats = query_formats,
376
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, },
384                                   { .name = NULL }},
385
386     .outputs   = (AVFilterPad[]) {{ .name             = "default",
387                                     .type             = AVMEDIA_TYPE_VIDEO, },
388                                   { .name = NULL }},
389 };