00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include "libavutil/avstring.h"
00030 #include "libavutil/eval.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/pixdesc.h"
00033 #include "internal.h"
00034
00035 typedef struct {
00036 const AVClass *class;
00037 AVExpr *e[3];
00038 char *expr_str[3];
00039 int framenum;
00040 AVFilterBufferRef *picref;
00041 int hsub, vsub;
00042 } GEQContext;
00043
00044 #define OFFSET(x) offsetof(GEQContext, x)
00045 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00046
00047 static const AVOption geq_options[] = {
00048 { "lum_expr", "set luminance expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
00049 { "cb_expr", "set chroma blue expression", OFFSET(expr_str) + sizeof(char*), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
00050 { "cr_expr", "set chroma red expression", OFFSET(expr_str) + 2*sizeof(char*), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
00051 {NULL},
00052 };
00053
00054 AVFILTER_DEFINE_CLASS(geq);
00055
00056 static inline double getpix(void *priv, double x, double y, int plane)
00057 {
00058 int xi, yi;
00059 GEQContext *geq = priv;
00060 AVFilterBufferRef *picref = geq->picref;
00061 const uint8_t *src = picref->data[plane];
00062 const int linesize = picref->linesize[plane];
00063 const int w = picref->video->w >> (plane ? geq->hsub : 0);
00064 const int h = picref->video->h >> (plane ? geq->vsub : 0);
00065
00066 xi = x = av_clipf(x, 0, w - 2);
00067 yi = y = av_clipf(y, 0, h - 2);
00068
00069 x -= xi;
00070 y -= yi;
00071
00072 return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
00073 + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
00074 }
00075
00076
00077
00078 static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
00079 static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
00080 static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
00081
00082 static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
00083 enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
00084
00085 static av_cold int geq_init(AVFilterContext *ctx, const char *args)
00086 {
00087 GEQContext *geq = ctx->priv;
00088 int plane, ret = 0;
00089 static const char *shorthand[] = { "lum_expr", "cb_expr", "cr_expr", NULL };
00090
00091 geq->class = &geq_class;
00092 av_opt_set_defaults(geq);
00093
00094 if ((ret = av_opt_set_from_string(geq, args, shorthand, "=", ":")) < 0)
00095 return ret;
00096
00097 if (!geq->expr_str[0]) {
00098 av_log(ctx, AV_LOG_ERROR, "Luminance expression is mandatory\n");
00099 ret = AVERROR(EINVAL);
00100 goto end;
00101 }
00102
00103 if (!geq->expr_str[1] && !geq->expr_str[2]) {
00104
00105 geq->expr_str[1] = av_strdup(geq->expr_str[0]);
00106 geq->expr_str[2] = av_strdup(geq->expr_str[0]);
00107 } else {
00108
00109 if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
00110 if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
00111 }
00112
00113 if (!geq->expr_str[1] || !geq->expr_str[2]) {
00114 ret = AVERROR(ENOMEM);
00115 goto end;
00116 }
00117
00118 for (plane = 0; plane < 3; plane++) {
00119 static double (*p[])(void *, double, double) = { lum, cb, cr };
00120 static const char *const func2_names[] = { "lum", "cb", "cr", "p", NULL };
00121 double (*func2[])(void *, double, double) = { lum, cb, cr, p[plane], NULL };
00122
00123 ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane], var_names,
00124 NULL, NULL, func2_names, func2, 0, ctx);
00125 if (ret < 0)
00126 break;
00127 }
00128
00129 end:
00130 return ret;
00131 }
00132
00133 static int geq_query_formats(AVFilterContext *ctx)
00134 {
00135 static const enum PixelFormat pix_fmts[] = {
00136 AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
00137 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
00138 AV_PIX_FMT_YUVA420P,
00139 AV_PIX_FMT_NONE
00140 };
00141 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00142 return 0;
00143 }
00144
00145 static int geq_config_props(AVFilterLink *inlink)
00146 {
00147 GEQContext *geq = inlink->dst->priv;
00148 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
00149
00150 geq->hsub = desc->log2_chroma_w;
00151 geq->vsub = desc->log2_chroma_h;
00152 return 0;
00153 }
00154
00155 static int geq_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
00156 {
00157 int plane;
00158 GEQContext *geq = inlink->dst->priv;
00159 AVFilterLink *outlink = inlink->dst->outputs[0];
00160 AVFilterBufferRef *out;
00161 double values[VAR_VARS_NB] = {
00162 [VAR_N] = geq->framenum++,
00163 [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
00164 };
00165
00166 geq->picref = in;
00167 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00168 if (!out) {
00169 avfilter_unref_bufferp(&in);
00170 return AVERROR(ENOMEM);
00171 }
00172 avfilter_copy_buffer_ref_props(out, in);
00173
00174 for (plane = 0; plane < 3; plane++) {
00175 int x, y;
00176 uint8_t *dst = out->data[plane];
00177 const int linesize = out->linesize[plane];
00178 const int w = inlink->w >> (plane ? geq->hsub : 0);
00179 const int h = inlink->h >> (plane ? geq->vsub : 0);
00180
00181 values[VAR_W] = w;
00182 values[VAR_H] = h;
00183 values[VAR_SW] = w / (double)inlink->w;
00184 values[VAR_SH] = h / (double)inlink->h;
00185
00186 for (y = 0; y < h; y++) {
00187 values[VAR_Y] = y;
00188 for (x = 0; x < w; x++) {
00189 values[VAR_X] = x;
00190 dst[x] = av_expr_eval(geq->e[plane], values, geq);
00191 }
00192 dst += linesize;
00193 }
00194 }
00195
00196 avfilter_unref_bufferp(&geq->picref);
00197 return ff_filter_frame(outlink, out);
00198 }
00199
00200 static av_cold void geq_uninit(AVFilterContext *ctx)
00201 {
00202 int i;
00203 GEQContext *geq = ctx->priv;
00204
00205 for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
00206 av_expr_free(geq->e[i]);
00207 av_opt_free(geq);
00208 }
00209
00210 static const AVFilterPad geq_inputs[] = {
00211 {
00212 .name = "default",
00213 .type = AVMEDIA_TYPE_VIDEO,
00214 .config_props = geq_config_props,
00215 .filter_frame = geq_filter_frame,
00216 .min_perms = AV_PERM_READ,
00217 },
00218 { NULL }
00219 };
00220
00221 static const AVFilterPad geq_outputs[] = {
00222 {
00223 .name = "default",
00224 .type = AVMEDIA_TYPE_VIDEO,
00225 },
00226 { NULL }
00227 };
00228
00229 AVFilter avfilter_vf_geq = {
00230 .name = "geq",
00231 .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
00232 .priv_size = sizeof(GEQContext),
00233 .init = geq_init,
00234 .uninit = geq_uninit,
00235 .query_formats = geq_query_formats,
00236 .inputs = geq_inputs,
00237 .outputs = geq_outputs,
00238 .priv_class = &geq_class,
00239 };