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 "video.h"
00035 #include "libavutil/pixdesc.h"
00036 #include "libavutil/avstring.h"
00037 
00038 #define NS(n) n < 0 ? (int)(n*65536.0-0.5+DBL_EPSILON) : (int)(n*65536.0+0.5)
00039 #define CB(n) av_clip_uint8(n)
00040 
00041 static const double yuv_coeff[4][3][3] = {
00042     { { +0.7152, +0.0722, +0.2126 }, 
00043       { -0.3850, +0.5000, -0.1150 },
00044       { -0.4540, -0.0460, +0.5000 } },
00045     { { +0.5900, +0.1100, +0.3000 }, 
00046       { -0.3310, +0.5000, -0.1690 },
00047       { -0.4210, -0.0790, +0.5000 } },
00048     { { +0.5870, +0.1140, +0.2990 }, 
00049       { -0.3313, +0.5000, -0.1687 },
00050       { -0.4187, -0.0813, +0.5000 } },
00051     { { +0.7010, +0.0870, +0.2120 }, 
00052       { -0.3840, +0.5000, -0.1160 },
00053       { -0.4450, -0.0550, +0.5000 } },
00054 };
00055 
00056 typedef struct {
00057     int yuv_convert[16][3][3];
00058     int interlaced;
00059     int source, dest, mode;
00060     char src[256];
00061     char dst[256];
00062     int hsub, vsub;
00063     AVFilterBufferRef *outpicref;
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_descriptors[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 PixelFormat pix_fmts[] = {
00324         PIX_FMT_YUV422P,
00325         PIX_FMT_YUV420P,
00326         PIX_FMT_UYVY422,
00327         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 AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00336 {
00337     AVFilterBufferRef *picref =
00338         ff_get_video_buffer(inlink->dst->outputs[0], perms, w, h);
00339     return picref;
00340 }
00341 
00342 static int start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00343 {
00344     AVFilterContext *ctx = link->dst;
00345     ColorMatrixContext *color = ctx->priv;
00346     AVFilterBufferRef *outpicref = avfilter_ref_buffer(picref, ~0);
00347 
00348     color->outpicref = outpicref;
00349 
00350     return ff_start_frame(link->dst->outputs[0], outpicref);
00351 }
00352 
00353 static int end_frame(AVFilterLink *link)
00354 {
00355     AVFilterContext *ctx = link->dst;
00356     ColorMatrixContext *color = ctx->priv;
00357     AVFilterBufferRef *out = color->outpicref;
00358 
00359     if (link->cur_buf->format == PIX_FMT_YUV422P)
00360         process_frame_yuv422p(color, out, link->cur_buf);
00361     else if (link->cur_buf->format == PIX_FMT_YUV420P)
00362         process_frame_yuv420p(color, out, link->cur_buf);
00363     else
00364         process_frame_uyvy422(color, out, link->cur_buf);
00365 
00366     ff_draw_slice(ctx->outputs[0], 0, link->dst->outputs[0]->h, 1);
00367     return ff_end_frame(ctx->outputs[0]);
00368 }
00369 
00370 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
00371 
00372 AVFilter avfilter_vf_colormatrix = {
00373     .name          = "colormatrix",
00374     .description   = NULL_IF_CONFIG_SMALL("Color matrix conversion"),
00375 
00376     .priv_size     = sizeof(ColorMatrixContext),
00377     .init          = init,
00378     .query_formats = query_formats,
00379 
00380     .inputs    = (AVFilterPad[]) {{ .name             = "default",
00381                                     .type             = AVMEDIA_TYPE_VIDEO,
00382                                     .config_props     = config_input,
00383                                     .min_perms        = AV_PERM_READ | AV_PERM_WRITE,
00384                                     .start_frame      = start_frame,
00385                                     .get_video_buffer = get_video_buffer,
00386                                     .draw_slice       = null_draw_slice,
00387                                     .end_frame        = end_frame, },
00388                                   { .name = NULL }},
00389 
00390     .outputs   = (AVFilterPad[]) {{ .name             = "default",
00391                                     .type             = AVMEDIA_TYPE_VIDEO, },
00392                                   { .name = NULL }},
00393 };