00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00031 #include <float.h>
00032 #include "avfilter.h"
00033 #include "formats.h"
00034 #include "internal.h"
00035 #include "video.h"
00036 #include "libavutil/pixdesc.h"
00037 #include "libavutil/avstring.h"
00038
00039 #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
00040 #define CB(n) av_clip_uint8(n)
00041
00042 static const double yuv_coeff[4][3][3] = {
00043 { { +0.7152, +0.0722, +0.2126 },
00044 { -0.3850, +0.5000, -0.1150 },
00045 { -0.4540, -0.0460, +0.5000 } },
00046 { { +0.5900, +0.1100, +0.3000 },
00047 { -0.3310, +0.5000, -0.1690 },
00048 { -0.4210, -0.0790, +0.5000 } },
00049 { { +0.5870, +0.1140, +0.2990 },
00050 { -0.3313, +0.5000, -0.1687 },
00051 { -0.4187, -0.0813, +0.5000 } },
00052 { { +0.7010, +0.0870, +0.2120 },
00053 { -0.3840, +0.5000, -0.1160 },
00054 { -0.4450, -0.0550, +0.5000 } },
00055 };
00056
00057 typedef struct {
00058 int yuv_convert[16][3][3];
00059 int interlaced;
00060 int source, dest, mode;
00061 char src[256];
00062 char dst[256];
00063 int hsub, vsub;
00064 } ColorMatrixContext;
00065
00066 #define ma m[0][0]
00067 #define mb m[0][1]
00068 #define mc m[0][2]
00069 #define md m[1][0]
00070 #define me m[1][1]
00071 #define mf m[1][2]
00072 #define mg m[2][0]
00073 #define mh m[2][1]
00074 #define mi m[2][2]
00075
00076 #define ima im[0][0]
00077 #define imb im[0][1]
00078 #define imc im[0][2]
00079 #define imd im[1][0]
00080 #define ime im[1][1]
00081 #define imf im[1][2]
00082 #define img im[2][0]
00083 #define imh im[2][1]
00084 #define imi im[2][2]
00085
00086 static void inverse3x3(double im[3][3], const double m[3][3])
00087 {
00088 double det = ma * (me * mi - mf * mh) - mb * (md * mi - mf * mg) + mc * (md * mh - me * mg);
00089 det = 1.0 / det;
00090 ima = det * (me * mi - mf * mh);
00091 imb = det * (mc * mh - mb * mi);
00092 imc = det * (mb * mf - mc * me);
00093 imd = det * (mf * mg - md * mi);
00094 ime = det * (ma * mi - mc * mg);
00095 imf = det * (mc * md - ma * mf);
00096 img = det * (md * mh - me * mg);
00097 imh = det * (mb * mg - ma * mh);
00098 imi = det * (ma * me - mb * md);
00099 }
00100
00101 static void solve_coefficients(double cm[3][3], double rgb[3][3], const double yuv[3][3])
00102 {
00103 int i, j;
00104 for (i = 0; i < 3; i++)
00105 for (j = 0; j < 3; j++)
00106 cm[i][j] = yuv[i][0] * rgb[0][j] + yuv[i][1] * rgb[1][j] + yuv[i][2] * rgb[2][j];
00107 }
00108
00109 static void calc_coefficients(AVFilterContext *ctx)
00110 {
00111 ColorMatrixContext *color = ctx->priv;
00112 double rgb_coeffd[4][3][3];
00113 double yuv_convertd[16][3][3];
00114 int v = 0;
00115 int i, j, k;
00116
00117 for (i = 0; i < 4; i++)
00118 inverse3x3(rgb_coeffd[i], yuv_coeff[i]);
00119 for (i = 0; i < 4; i++) {
00120 for (j = 0; j < 4; j++) {
00121 solve_coefficients(yuv_convertd[v], rgb_coeffd[i], yuv_coeff[j]);
00122 for (k = 0; k < 3; k++) {
00123 color->yuv_convert[v][k][0] = NS(yuv_convertd[v][k][0]);
00124 color->yuv_convert[v][k][1] = NS(yuv_convertd[v][k][1]);
00125 color->yuv_convert[v][k][2] = NS(yuv_convertd[v][k][2]);
00126 }
00127 if (color->yuv_convert[v][0][0] != 65536 || color->yuv_convert[v][1][0] != 0 ||
00128 color->yuv_convert[v][2][0] != 0) {
00129 av_log(ctx, AV_LOG_ERROR, "error calculating conversion coefficients\n");
00130 }
00131 v++;
00132 }
00133 }
00134 }
00135
00136 static const char *color_modes[] = {"bt709", "FCC", "bt601", "smpte240m"};
00137
00138 static int get_color_mode_index(const char *name)
00139 {
00140 int i;
00141
00142 for (i = 0; i < FF_ARRAY_ELEMS(color_modes); i++)
00143 if (!av_strcasecmp(color_modes[i], name))
00144 return i;
00145 return -1;
00146 }
00147
00148 static av_cold int init(AVFilterContext *ctx, const char *args)
00149 {
00150 ColorMatrixContext *color = ctx->priv;
00151
00152 if (!args)
00153 goto usage;
00154 if (sscanf(args, "%255[^:]:%255[^:]", color->src, color->dst) != 2) {
00155 usage:
00156 av_log(ctx, AV_LOG_ERROR, "usage: <src>:<dst>\n");
00157 av_log(ctx, AV_LOG_ERROR, "possible options: bt709,bt601,smpte240m,fcc\n");
00158 return -1;
00159 }
00160
00161 color->source = get_color_mode_index(color->src);
00162 if (color->source < 0) {
00163 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->src);
00164 return AVERROR(EINVAL);
00165 }
00166
00167 color->dest = get_color_mode_index(color->dst);
00168 if (color->dest < 0) {
00169 av_log(ctx, AV_LOG_ERROR, "unknown color space %s\n", color->dst);
00170 return AVERROR(EINVAL);
00171 }
00172
00173 if (color->source == color->dest) {
00174 av_log(ctx, AV_LOG_ERROR, "source and destination color space are identical\n");
00175 return AVERROR(EINVAL);
00176 }
00177
00178 color->mode = color->source * 4 + color->dest;
00179
00180 calc_coefficients(ctx);
00181
00182 return 0;
00183 }
00184
00185 static void process_frame_uyvy422(ColorMatrixContext *color,
00186 AVFilterBufferRef *dst, AVFilterBufferRef *src)
00187 {
00188 const unsigned char *srcp = src->data[0];
00189 const int src_pitch = src->linesize[0];
00190 const int height = src->video->h;
00191 const int width = src->video->w*2;
00192 unsigned char *dstp = dst->data[0];
00193 const int dst_pitch = dst->linesize[0];
00194 const int c2 = color->yuv_convert[color->mode][0][1];
00195 const int c3 = color->yuv_convert[color->mode][0][2];
00196 const int c4 = color->yuv_convert[color->mode][1][1];
00197 const int c5 = color->yuv_convert[color->mode][1][2];
00198 const int c6 = color->yuv_convert[color->mode][2][1];
00199 const int c7 = color->yuv_convert[color->mode][2][2];
00200 int x, y;
00201
00202 for (y = 0; y < height; y++) {
00203 for (x = 0; x < width; x += 4) {
00204 const int u = srcp[x + 0] - 128;
00205 const int v = srcp[x + 2] - 128;
00206 const int uvval = c2 * u + c3 * v + 1081344;
00207 dstp[x + 0] = CB((c4 * u + c5 * v + 8421376) >> 16);
00208 dstp[x + 1] = CB((65536 * (srcp[x + 1] - 16) + uvval) >> 16);
00209 dstp[x + 2] = CB((c6 * u + c7 * v + 8421376) >> 16);
00210 dstp[x + 3] = CB((65536 * (srcp[x + 3] - 16) + uvval) >> 16);
00211 }
00212 srcp += src_pitch;
00213 dstp += dst_pitch;
00214 }
00215 }
00216
00217 static void process_frame_yuv422p(ColorMatrixContext *color,
00218 AVFilterBufferRef *dst, AVFilterBufferRef *src)
00219 {
00220 const unsigned char *srcpU = src->data[1];
00221 const unsigned char *srcpV = src->data[2];
00222 const unsigned char *srcpY = src->data[0];
00223 const int src_pitchY = src->linesize[0];
00224 const int src_pitchUV = src->linesize[1];
00225 const int height = src->video->h;
00226 const int width = src->video->w;
00227 unsigned char *dstpU = dst->data[1];
00228 unsigned char *dstpV = dst->data[2];
00229 unsigned char *dstpY = dst->data[0];
00230 const int dst_pitchY = dst->linesize[0];
00231 const int dst_pitchUV = dst->linesize[1];
00232 const int c2 = color->yuv_convert[color->mode][0][1];
00233 const int c3 = color->yuv_convert[color->mode][0][2];
00234 const int c4 = color->yuv_convert[color->mode][1][1];
00235 const int c5 = color->yuv_convert[color->mode][1][2];
00236 const int c6 = color->yuv_convert[color->mode][2][1];
00237 const int c7 = color->yuv_convert[color->mode][2][2];
00238 int x, y;
00239
00240 for (y = 0; y < height; y++) {
00241 for (x = 0; x < width; x += 2) {
00242 const int u = srcpU[x >> 1] - 128;
00243 const int v = srcpV[x >> 1] - 128;
00244 const int uvval = c2 * u + c3 * v + 1081344;
00245 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
00246 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
00247 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
00248 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
00249 }
00250 srcpY += src_pitchY;
00251 dstpY += dst_pitchY;
00252 srcpU += src_pitchUV;
00253 srcpV += src_pitchUV;
00254 dstpU += dst_pitchUV;
00255 dstpV += dst_pitchUV;
00256 }
00257 }
00258
00259 static void process_frame_yuv420p(ColorMatrixContext *color,
00260 AVFilterBufferRef *dst, AVFilterBufferRef *src)
00261 {
00262 const unsigned char *srcpU = src->data[1];
00263 const unsigned char *srcpV = src->data[2];
00264 const unsigned char *srcpY = src->data[0];
00265 const unsigned char *srcpN = src->data[0] + src->linesize[0];
00266 const int src_pitchY = src->linesize[0];
00267 const int src_pitchUV = src->linesize[1];
00268 const int height = src->video->h;
00269 const int width = src->video->w;
00270 unsigned char *dstpU = dst->data[1];
00271 unsigned char *dstpV = dst->data[2];
00272 unsigned char *dstpY = dst->data[0];
00273 unsigned char *dstpN = dst->data[0] + dst->linesize[0];
00274 const int dst_pitchY = dst->linesize[0];
00275 const int dst_pitchUV = dst->linesize[1];
00276 const int c2 = color->yuv_convert[color->mode][0][1];
00277 const int c3 = color->yuv_convert[color->mode][0][2];
00278 const int c4 = color->yuv_convert[color->mode][1][1];
00279 const int c5 = color->yuv_convert[color->mode][1][2];
00280 const int c6 = color->yuv_convert[color->mode][2][1];
00281 const int c7 = color->yuv_convert[color->mode][2][2];
00282 int x, y;
00283
00284 for (y = 0; y < height; y += 2) {
00285 for (x = 0; x < width; x += 2) {
00286 const int u = srcpU[x >> 1] - 128;
00287 const int v = srcpV[x >> 1] - 128;
00288 const int uvval = c2 * u + c3 * v + 1081344;
00289 dstpY[x + 0] = CB((65536 * (srcpY[x + 0] - 16) + uvval) >> 16);
00290 dstpY[x + 1] = CB((65536 * (srcpY[x + 1] - 16) + uvval) >> 16);
00291 dstpN[x + 0] = CB((65536 * (srcpN[x + 0] - 16) + uvval) >> 16);
00292 dstpN[x + 1] = CB((65536 * (srcpN[x + 1] - 16) + uvval) >> 16);
00293 dstpU[x >> 1] = CB((c4 * u + c5 * v + 8421376) >> 16);
00294 dstpV[x >> 1] = CB((c6 * u + c7 * v + 8421376) >> 16);
00295 }
00296 srcpY += src_pitchY << 1;
00297 dstpY += dst_pitchY << 1;
00298 srcpN += src_pitchY << 1;
00299 dstpN += dst_pitchY << 1;
00300 srcpU += src_pitchUV;
00301 srcpV += src_pitchUV;
00302 dstpU += dst_pitchUV;
00303 dstpV += dst_pitchUV;
00304 }
00305 }
00306
00307 static int config_input(AVFilterLink *inlink)
00308 {
00309 AVFilterContext *ctx = inlink->dst;
00310 ColorMatrixContext *color = ctx->priv;
00311 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
00312
00313 color->hsub = pix_desc->log2_chroma_w;
00314 color->vsub = pix_desc->log2_chroma_h;
00315
00316 av_log(ctx, AV_LOG_VERBOSE, "%s -> %s\n", color->src, color->dst);
00317
00318 return 0;
00319 }
00320
00321 static int query_formats(AVFilterContext *ctx)
00322 {
00323 static const enum AVPixelFormat pix_fmts[] = {
00324 AV_PIX_FMT_YUV422P,
00325 AV_PIX_FMT_YUV420P,
00326 AV_PIX_FMT_UYVY422,
00327 AV_PIX_FMT_NONE
00328 };
00329
00330 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00331
00332 return 0;
00333 }
00334
00335 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
00336 {
00337 AVFilterContext *ctx = link->dst;
00338 ColorMatrixContext *color = ctx->priv;
00339 AVFilterLink *outlink = ctx->outputs[0];
00340 AVFilterBufferRef *out;
00341
00342 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00343 if (!out) {
00344 avfilter_unref_bufferp(&in);
00345 return AVERROR(ENOMEM);
00346 }
00347 avfilter_copy_buffer_ref_props(out, in);
00348
00349 if (in->format == AV_PIX_FMT_YUV422P)
00350 process_frame_yuv422p(color, out, in);
00351 else if (in->format == AV_PIX_FMT_YUV420P)
00352 process_frame_yuv420p(color, out, in);
00353 else
00354 process_frame_uyvy422(color, out, in);
00355
00356 avfilter_unref_bufferp(&in);
00357 return ff_filter_frame(outlink, out);
00358 }
00359
00360 static const AVFilterPad colormatrix_inputs[] = {
00361 {
00362 .name = "default",
00363 .type = AVMEDIA_TYPE_VIDEO,
00364 .config_props = config_input,
00365 .min_perms = AV_PERM_READ,
00366 .filter_frame = filter_frame,
00367 },
00368 { NULL }
00369 };
00370
00371 static const AVFilterPad colormatrix_outputs[] = {
00372 {
00373 .name = "default",
00374 .type = AVMEDIA_TYPE_VIDEO,
00375 },
00376 { NULL }
00377 };
00378
00379 AVFilter avfilter_vf_colormatrix = {
00380 .name = "colormatrix",
00381 .description = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
00382
00383 .priv_size = sizeof(ColorMatrixContext),
00384 .init = init,
00385 .query_formats = query_formats,
00386 .inputs = colormatrix_inputs,
00387 .outputs = colormatrix_outputs,
00388 };