00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00027 #include "libavutil/eval.h"
00028 #include "libavutil/mathematics.h"
00029 #include "libavutil/opt.h"
00030 #include "libavutil/pixdesc.h"
00031 #include "avfilter.h"
00032 #include "internal.h"
00033
00034 static const char *var_names[] = {
00035 "E",
00036 "PHI",
00037 "PI",
00038 "w",
00039 "h",
00040 "val",
00041 "maxval",
00042 "minval",
00043 "negval",
00044 "clipval",
00045 NULL
00046 };
00047
00048 enum var_name {
00049 VAR_E,
00050 VAR_PHI,
00051 VAR_PI,
00052 VAR_W,
00053 VAR_H,
00054 VAR_VAL,
00055 VAR_MAXVAL,
00056 VAR_MINVAL,
00057 VAR_NEGVAL,
00058 VAR_CLIPVAL,
00059 VAR_VARS_NB
00060 };
00061
00062 typedef struct {
00063 const AVClass *class;
00064 uint8_t lut[4][256];
00065 char *comp_expr_str[4];
00066 AVExpr *comp_expr[4];
00067 int hsub, vsub;
00068 double var_values[VAR_VARS_NB];
00069 int is_rgb, is_yuv;
00070 int rgba_map[4];
00071 int step;
00072 int negate_alpha;
00073 } LutContext;
00074
00075 #define Y 0
00076 #define U 1
00077 #define V 2
00078 #define R 0
00079 #define G 1
00080 #define B 2
00081 #define A 3
00082
00083 #define OFFSET(x) offsetof(LutContext, x)
00084
00085 static const AVOption lut_options[] = {
00086 {"c0", "set component #0 expression", OFFSET(comp_expr_str[0]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00087 {"c1", "set component #1 expression", OFFSET(comp_expr_str[1]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00088 {"c2", "set component #2 expression", OFFSET(comp_expr_str[2]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00089 {"c3", "set component #3 expression", OFFSET(comp_expr_str[3]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00090 {"y", "set Y expression", OFFSET(comp_expr_str[Y]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00091 {"u", "set U expression", OFFSET(comp_expr_str[U]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00092 {"v", "set V expression", OFFSET(comp_expr_str[V]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00093 {"r", "set R expression", OFFSET(comp_expr_str[R]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00094 {"g", "set G expression", OFFSET(comp_expr_str[G]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00095 {"b", "set B expression", OFFSET(comp_expr_str[B]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00096 {"a", "set A expression", OFFSET(comp_expr_str[A]), FF_OPT_TYPE_STRING, {.str="val"}, CHAR_MIN, CHAR_MAX},
00097 {NULL},
00098 };
00099
00100 static const char *lut_get_name(void *ctx)
00101 {
00102 return "lut";
00103 }
00104
00105 static const AVClass lut_class = {
00106 "LutContext",
00107 lut_get_name,
00108 lut_options
00109 };
00110
00111 static int init(AVFilterContext *ctx, const char *args, void *opaque)
00112 {
00113 LutContext *lut = ctx->priv;
00114 int ret;
00115
00116 lut->class = &lut_class;
00117 av_opt_set_defaults(lut);
00118
00119 lut->var_values[VAR_PHI] = M_PHI;
00120 lut->var_values[VAR_PI] = M_PI;
00121 lut->var_values[VAR_E ] = M_E;
00122
00123 lut->is_rgb = !strcmp(ctx->filter->name, "lutrgb");
00124 lut->is_yuv = !strcmp(ctx->filter->name, "lutyuv");
00125 if (args && (ret = av_set_options_string(lut, args, "=", ":")) < 0)
00126 return ret;
00127
00128 return 0;
00129 }
00130
00131 static av_cold void uninit(AVFilterContext *ctx)
00132 {
00133 LutContext *lut = ctx->priv;
00134 int i;
00135
00136 for (i = 0; i < 4; i++) {
00137 av_expr_free(lut->comp_expr[i]);
00138 lut->comp_expr[i] = NULL;
00139 av_freep(&lut->comp_expr_str[i]);
00140 }
00141 }
00142
00143 #define YUV_FORMATS \
00144 PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, \
00145 PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUV440P, \
00146 PIX_FMT_YUVA420P, \
00147 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P, \
00148 PIX_FMT_YUVJ440P
00149
00150 #define RGB_FORMATS \
00151 PIX_FMT_ARGB, PIX_FMT_RGBA, \
00152 PIX_FMT_ABGR, PIX_FMT_BGRA, \
00153 PIX_FMT_RGB24, PIX_FMT_BGR24
00154
00155 static enum PixelFormat yuv_pix_fmts[] = { YUV_FORMATS, PIX_FMT_NONE };
00156 static enum PixelFormat rgb_pix_fmts[] = { RGB_FORMATS, PIX_FMT_NONE };
00157 static enum PixelFormat all_pix_fmts[] = { RGB_FORMATS, YUV_FORMATS, PIX_FMT_NONE };
00158
00159 static int query_formats(AVFilterContext *ctx)
00160 {
00161 LutContext *lut = ctx->priv;
00162
00163 enum PixelFormat *pix_fmts = lut->is_rgb ? rgb_pix_fmts :
00164 lut->is_yuv ? yuv_pix_fmts : all_pix_fmts;
00165
00166 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00167 return 0;
00168 }
00169
00173 static double clip(void *opaque, double val)
00174 {
00175 LutContext *lut = opaque;
00176 double minval = lut->var_values[VAR_MINVAL];
00177 double maxval = lut->var_values[VAR_MAXVAL];
00178
00179 return av_clip(val, minval, maxval);
00180 }
00181
00186 static double compute_gammaval(void *opaque, double gamma)
00187 {
00188 LutContext *lut = opaque;
00189 double val = lut->var_values[VAR_CLIPVAL];
00190 double minval = lut->var_values[VAR_MINVAL];
00191 double maxval = lut->var_values[VAR_MAXVAL];
00192
00193 return pow((val-minval)/(maxval-minval), gamma) * (maxval-minval)+minval;
00194 }
00195
00196 static double (* const funcs1[])(void *, double) = {
00197 clip,
00198 compute_gammaval,
00199 NULL
00200 };
00201
00202 static const char * const funcs1_names[] = {
00203 "clip",
00204 "gammaval",
00205 NULL
00206 };
00207
00208 static int config_props(AVFilterLink *inlink)
00209 {
00210 AVFilterContext *ctx = inlink->dst;
00211 LutContext *lut = ctx->priv;
00212 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[inlink->format];
00213 int min[4], max[4];
00214 int val, comp, ret;
00215
00216 lut->hsub = desc->log2_chroma_w;
00217 lut->vsub = desc->log2_chroma_h;
00218
00219 lut->var_values[VAR_W] = inlink->w;
00220 lut->var_values[VAR_H] = inlink->h;
00221
00222 switch (inlink->format) {
00223 case PIX_FMT_YUV410P:
00224 case PIX_FMT_YUV411P:
00225 case PIX_FMT_YUV420P:
00226 case PIX_FMT_YUV422P:
00227 case PIX_FMT_YUV440P:
00228 case PIX_FMT_YUV444P:
00229 case PIX_FMT_YUVA420P:
00230 min[Y] = min[U] = min[V] = 16;
00231 max[Y] = 235;
00232 max[U] = max[V] = 240;
00233 min[A] = 0; max[A] = 255;
00234 break;
00235 default:
00236 min[0] = min[1] = min[2] = min[3] = 0;
00237 max[0] = max[1] = max[2] = max[3] = 255;
00238 }
00239
00240 lut->is_yuv = lut->is_rgb = 0;
00241 if (ff_fmt_is_in(inlink->format, yuv_pix_fmts)) lut->is_yuv = 1;
00242 else if (ff_fmt_is_in(inlink->format, rgb_pix_fmts)) lut->is_rgb = 1;
00243
00244 if (lut->is_rgb) {
00245 switch (inlink->format) {
00246 case PIX_FMT_ARGB: lut->rgba_map[A] = 0; lut->rgba_map[R] = 1; lut->rgba_map[G] = 2; lut->rgba_map[B] = 3; break;
00247 case PIX_FMT_ABGR: lut->rgba_map[A] = 0; lut->rgba_map[B] = 1; lut->rgba_map[G] = 2; lut->rgba_map[R] = 3; break;
00248 case PIX_FMT_RGBA:
00249 case PIX_FMT_RGB24: lut->rgba_map[R] = 0; lut->rgba_map[G] = 1; lut->rgba_map[B] = 2; lut->rgba_map[A] = 3; break;
00250 case PIX_FMT_BGRA:
00251 case PIX_FMT_BGR24: lut->rgba_map[B] = 0; lut->rgba_map[G] = 1; lut->rgba_map[R] = 2; lut->rgba_map[A] = 3; break;
00252 }
00253 lut->step = av_get_bits_per_pixel(desc) >> 3;
00254 }
00255
00256 for (comp = 0; comp < desc->nb_components; comp++) {
00257 double res;
00258
00259
00260 ret = av_expr_parse(&lut->comp_expr[comp], lut->comp_expr_str[comp],
00261 var_names, funcs1_names, funcs1, NULL, NULL, 0, ctx);
00262 if (ret < 0) {
00263 av_log(ctx, AV_LOG_ERROR,
00264 "Error when parsing the expression '%s' for the component %d.\n",
00265 lut->comp_expr_str[comp], comp);
00266 return AVERROR(EINVAL);
00267 }
00268
00269
00270 lut->var_values[VAR_MAXVAL] = max[comp];
00271 lut->var_values[VAR_MINVAL] = min[comp];
00272
00273 for (val = 0; val < 256; val++) {
00274 lut->var_values[VAR_VAL] = val;
00275 lut->var_values[VAR_CLIPVAL] = av_clip(val, min[comp], max[comp]);
00276 lut->var_values[VAR_NEGVAL] =
00277 av_clip(min[comp] + max[comp] - lut->var_values[VAR_VAL],
00278 min[comp], max[comp]);
00279
00280 res = av_expr_eval(lut->comp_expr[comp], lut->var_values, lut);
00281 if (isnan(res)) {
00282 av_log(ctx, AV_LOG_ERROR,
00283 "Error when evaluating the expression '%s' for the value %d for the component #%d.\n",
00284 lut->comp_expr_str[comp], val, comp);
00285 return AVERROR(EINVAL);
00286 }
00287 lut->lut[comp][val] = av_clip((int)res, min[comp], max[comp]);
00288 av_log(ctx, AV_LOG_DEBUG, "val[%d][%d] = %d\n", comp, val, lut->lut[comp][val]);
00289 }
00290 }
00291
00292 return 0;
00293 }
00294
00295 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
00296 {
00297 AVFilterContext *ctx = inlink->dst;
00298 LutContext *lut = ctx->priv;
00299 AVFilterLink *outlink = ctx->outputs[0];
00300 AVFilterBufferRef *inpic = inlink ->cur_buf;
00301 AVFilterBufferRef *outpic = outlink->out_buf;
00302 uint8_t *inrow, *outrow, *inrow0, *outrow0;
00303 int i, j, k, plane;
00304
00305 if (lut->is_rgb) {
00306
00307 inrow0 = inpic ->data[0] + y * inpic ->linesize[0];
00308 outrow0 = outpic->data[0] + y * outpic->linesize[0];
00309
00310 for (i = 0; i < h; i ++) {
00311 inrow = inrow0;
00312 outrow = outrow0;
00313 for (j = 0; j < inlink->w; j++) {
00314 for (k = 0; k < lut->step; k++)
00315 outrow[k] = lut->lut[lut->rgba_map[k]][inrow[k]];
00316 outrow += lut->step;
00317 inrow += lut->step;
00318 }
00319 inrow0 += inpic ->linesize[0];
00320 outrow0 += outpic->linesize[0];
00321 }
00322 } else {
00323
00324 for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
00325 int vsub = plane == 1 || plane == 2 ? lut->vsub : 0;
00326 int hsub = plane == 1 || plane == 2 ? lut->hsub : 0;
00327
00328 inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane];
00329 outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
00330
00331 for (i = 0; i < h>>vsub; i ++) {
00332 for (j = 0; j < inlink->w>>hsub; j++)
00333 outrow[j] = lut->lut[plane][inrow[j]];
00334 inrow += inpic ->linesize[plane];
00335 outrow += outpic->linesize[plane];
00336 }
00337 }
00338 }
00339
00340 avfilter_draw_slice(outlink, y, h, slice_dir);
00341 }
00342
00343 #define DEFINE_LUT_FILTER(name_, description_, init_) \
00344 AVFilter avfilter_vf_##name_ = { \
00345 .name = #name_, \
00346 .description = NULL_IF_CONFIG_SMALL(description_), \
00347 .priv_size = sizeof(LutContext), \
00348 \
00349 .init = init_, \
00350 .uninit = uninit, \
00351 .query_formats = query_formats, \
00352 \
00353 .inputs = (AVFilterPad[]) {{ .name = "default", \
00354 .type = AVMEDIA_TYPE_VIDEO, \
00355 .draw_slice = draw_slice, \
00356 .config_props = config_props, \
00357 .min_perms = AV_PERM_READ, }, \
00358 { .name = NULL}}, \
00359 .outputs = (AVFilterPad[]) {{ .name = "default", \
00360 .type = AVMEDIA_TYPE_VIDEO, }, \
00361 { .name = NULL}}, \
00362 }
00363
00364 #if CONFIG_LUT_FILTER
00365 DEFINE_LUT_FILTER(lut, "Compute and apply a lookup table to the RGB/YUV input video.", init);
00366 #endif
00367 #if CONFIG_LUTYUV_FILTER
00368 DEFINE_LUT_FILTER(lutyuv, "Compute and apply a lookup table to the YUV input video.", init);
00369 #endif
00370 #if CONFIG_LUTRGB_FILTER
00371 DEFINE_LUT_FILTER(lutrgb, "Compute and apply a lookup table to the RGB input video.", init);
00372 #endif
00373
00374 #if CONFIG_NEGATE_FILTER
00375
00376 static int negate_init(AVFilterContext *ctx, const char *args, void *opaque)
00377 {
00378 LutContext *lut = ctx->priv;
00379 char lut_params[64];
00380
00381 if (args)
00382 sscanf(args, "%d", &lut->negate_alpha);
00383
00384 av_log(ctx, AV_LOG_DEBUG, "negate_alpha:%d\n", lut->negate_alpha);
00385
00386 snprintf(lut_params, sizeof(lut_params), "c0=negval:c1=negval:c2=negval:a=%s",
00387 lut->negate_alpha ? "negval" : "val");
00388
00389 return init(ctx, lut_params, opaque);
00390 }
00391
00392 DEFINE_LUT_FILTER(negate, "Negate input video.", negate_init);
00393
00394 #endif