00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026
00027
00028 #include "avfilter.h"
00029 #include "libavutil/eval.h"
00030 #include "libavutil/avstring.h"
00031 #include "libavutil/libm.h"
00032 #include "libavutil/imgutils.h"
00033
00034 static const char *var_names[] = {
00035 "E",
00036 "PHI",
00037 "PI",
00038 "in_w", "iw",
00039 "in_h", "ih",
00040 "out_w", "ow",
00041 "out_h", "oh",
00042 "x",
00043 "y",
00044 "n",
00045 "pos",
00046 "t",
00047 NULL
00048 };
00049
00050 enum var_name {
00051 VAR_E,
00052 VAR_PHI,
00053 VAR_PI,
00054 VAR_IN_W, VAR_IW,
00055 VAR_IN_H, VAR_IH,
00056 VAR_OUT_W, VAR_OW,
00057 VAR_OUT_H, VAR_OH,
00058 VAR_X,
00059 VAR_Y,
00060 VAR_N,
00061 VAR_POS,
00062 VAR_T,
00063 VAR_VARS_NB
00064 };
00065
00066 typedef struct {
00067 int x;
00068 int y;
00069 int w;
00070 int h;
00071
00072 int max_step[4];
00073 int hsub, vsub;
00074 char x_expr[256], y_expr[256], ow_expr[256], oh_expr[256];
00075 AVExpr *x_pexpr, *y_pexpr;
00076 double var_values[VAR_VARS_NB];
00077 } CropContext;
00078
00079 static int query_formats(AVFilterContext *ctx)
00080 {
00081 static const enum PixelFormat pix_fmts[] = {
00082 PIX_FMT_RGB48BE, PIX_FMT_RGB48LE,
00083 PIX_FMT_ARGB, PIX_FMT_RGBA,
00084 PIX_FMT_ABGR, PIX_FMT_BGRA,
00085 PIX_FMT_RGB24, PIX_FMT_BGR24,
00086 PIX_FMT_RGB565BE, PIX_FMT_RGB565LE,
00087 PIX_FMT_RGB555BE, PIX_FMT_RGB555LE,
00088 PIX_FMT_BGR565BE, PIX_FMT_BGR565LE,
00089 PIX_FMT_BGR555BE, PIX_FMT_BGR555LE,
00090 PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE,
00091 PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE,
00092 PIX_FMT_YUV422P16LE, PIX_FMT_YUV422P16BE,
00093 PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE,
00094 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
00095 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
00096 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
00097 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
00098 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
00099 PIX_FMT_YUVA420P,
00100 PIX_FMT_RGB8, PIX_FMT_BGR8,
00101 PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
00102 PIX_FMT_PAL8, PIX_FMT_GRAY8,
00103 PIX_FMT_NONE
00104 };
00105
00106 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00107
00108 return 0;
00109 }
00110
00111 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00112 {
00113 CropContext *crop = ctx->priv;
00114
00115 av_strlcpy(crop->ow_expr, "iw", sizeof(crop->ow_expr));
00116 av_strlcpy(crop->oh_expr, "ih", sizeof(crop->oh_expr));
00117 av_strlcpy(crop->x_expr, "(in_w-out_w)/2", sizeof(crop->x_expr));
00118 av_strlcpy(crop->y_expr, "(in_h-out_h)/2", sizeof(crop->y_expr));
00119
00120 if (args)
00121 sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->ow_expr, crop->oh_expr, crop->x_expr, crop->y_expr);
00122
00123 return 0;
00124 }
00125
00126 static av_cold void uninit(AVFilterContext *ctx)
00127 {
00128 CropContext *crop = ctx->priv;
00129
00130 av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
00131 av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
00132 }
00133
00134 static inline int normalize_double(int *n, double d)
00135 {
00136 int ret = 0;
00137
00138 if (isnan(d)) {
00139 ret = AVERROR(EINVAL);
00140 } else if (d > INT_MAX || d < INT_MIN) {
00141 *n = d > INT_MAX ? INT_MAX : INT_MIN;
00142 ret = AVERROR(EINVAL);
00143 } else
00144 *n = round(d);
00145
00146 return ret;
00147 }
00148
00149 static int config_input(AVFilterLink *link)
00150 {
00151 AVFilterContext *ctx = link->dst;
00152 CropContext *crop = ctx->priv;
00153 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
00154 int ret;
00155 const char *expr;
00156 double res;
00157
00158 crop->var_values[VAR_E] = M_E;
00159 crop->var_values[VAR_PHI] = M_PHI;
00160 crop->var_values[VAR_PI] = M_PI;
00161 crop->var_values[VAR_IN_W] = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
00162 crop->var_values[VAR_IN_H] = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
00163 crop->var_values[VAR_X] = NAN;
00164 crop->var_values[VAR_Y] = NAN;
00165 crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
00166 crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
00167 crop->var_values[VAR_N] = 0;
00168 crop->var_values[VAR_T] = NAN;
00169 crop->var_values[VAR_POS] = NAN;
00170
00171 av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
00172 crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
00173 crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00174
00175 if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
00176 var_names, crop->var_values,
00177 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00178 crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
00179 if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
00180 var_names, crop->var_values,
00181 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00182 crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
00183
00184 if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
00185 var_names, crop->var_values,
00186 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00187 crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
00188 if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
00189 normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
00190 av_log(ctx, AV_LOG_ERROR,
00191 "Too big value or invalid expression for out_w/ow or out_h/oh. "
00192 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
00193 crop->ow_expr, crop->oh_expr);
00194 return AVERROR(EINVAL);
00195 }
00196 crop->w &= ~((1 << crop->hsub) - 1);
00197 crop->h &= ~((1 << crop->vsub) - 1);
00198
00199 if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
00200 NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
00201 (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
00202 NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00203 return AVERROR(EINVAL);
00204
00205 av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d\n",
00206 link->w, link->h, crop->w, crop->h);
00207
00208 if (crop->w <= 0 || crop->h <= 0 ||
00209 crop->w > link->w || crop->h > link->h) {
00210 av_log(ctx, AV_LOG_ERROR,
00211 "Invalid too big or non positive size for width '%d' or height '%d'\n",
00212 crop->w, crop->h);
00213 return AVERROR(EINVAL);
00214 }
00215
00216
00217 crop->x = (link->w - crop->w) / 2;
00218 crop->y = (link->h - crop->h) / 2;
00219 crop->x &= ~((1 << crop->hsub) - 1);
00220 crop->y &= ~((1 << crop->vsub) - 1);
00221 return 0;
00222
00223 fail_expr:
00224 av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
00225 return ret;
00226 }
00227
00228 static int config_output(AVFilterLink *link)
00229 {
00230 CropContext *crop = link->src->priv;
00231
00232 link->w = crop->w;
00233 link->h = crop->h;
00234
00235 return 0;
00236 }
00237
00238 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00239 {
00240 AVFilterContext *ctx = link->dst;
00241 CropContext *crop = ctx->priv;
00242 AVFilterBufferRef *ref2;
00243 int i;
00244
00245 ref2 = avfilter_ref_buffer(picref, ~0);
00246 ref2->video->w = crop->w;
00247 ref2->video->h = crop->h;
00248
00249 crop->var_values[VAR_T] = picref->pts == AV_NOPTS_VALUE ?
00250 NAN : picref->pts * av_q2d(link->time_base);
00251 crop->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
00252 crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
00253 crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
00254 crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
00255
00256 normalize_double(&crop->x, crop->var_values[VAR_X]);
00257 normalize_double(&crop->y, crop->var_values[VAR_Y]);
00258
00259 if (crop->x < 0) crop->x = 0;
00260 if (crop->y < 0) crop->y = 0;
00261 if ((unsigned)crop->x + (unsigned)crop->w > link->w) crop->x = link->w - crop->w;
00262 if ((unsigned)crop->y + (unsigned)crop->h > link->h) crop->y = link->h - crop->h;
00263 crop->x &= ~((1 << crop->hsub) - 1);
00264 crop->y &= ~((1 << crop->vsub) - 1);
00265
00266 #ifdef DEBUG
00267 av_log(ctx, AV_LOG_DEBUG,
00268 "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
00269 (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->x, crop->y, crop->x+crop->w, crop->y+crop->h);
00270 #endif
00271
00272 ref2->data[0] += crop->y * ref2->linesize[0];
00273 ref2->data[0] += crop->x * crop->max_step[0];
00274
00275 if (!(av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)) {
00276 for (i = 1; i < 3; i ++) {
00277 if (ref2->data[i]) {
00278 ref2->data[i] += (crop->y >> crop->vsub) * ref2->linesize[i];
00279 ref2->data[i] += (crop->x * crop->max_step[i]) >> crop->hsub;
00280 }
00281 }
00282 }
00283
00284
00285 if (ref2->data[3]) {
00286 ref2->data[3] += crop->y * ref2->linesize[3];
00287 ref2->data[3] += crop->x * crop->max_step[3];
00288 }
00289
00290 avfilter_start_frame(link->dst->outputs[0], ref2);
00291 }
00292
00293 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00294 {
00295 AVFilterContext *ctx = link->dst;
00296 CropContext *crop = ctx->priv;
00297
00298 if (y >= crop->y + crop->h || y + h <= crop->y)
00299 return;
00300
00301 if (y < crop->y) {
00302 h -= crop->y - y;
00303 y = crop->y;
00304 }
00305 if (y + h > crop->y + crop->h)
00306 h = crop->y + crop->h - y;
00307
00308 avfilter_draw_slice(ctx->outputs[0], y - crop->y, h, slice_dir);
00309 }
00310
00311 static void end_frame(AVFilterLink *link)
00312 {
00313 CropContext *crop = link->dst->priv;
00314
00315 crop->var_values[VAR_N] += 1.0;
00316 avfilter_unref_buffer(link->cur_buf);
00317 avfilter_end_frame(link->dst->outputs[0]);
00318 }
00319
00320 AVFilter avfilter_vf_crop = {
00321 .name = "crop",
00322 .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
00323
00324 .priv_size = sizeof(CropContext),
00325
00326 .query_formats = query_formats,
00327 .init = init,
00328 .uninit = uninit,
00329
00330 .inputs = (AVFilterPad[]) {{ .name = "default",
00331 .type = AVMEDIA_TYPE_VIDEO,
00332 .start_frame = start_frame,
00333 .draw_slice = draw_slice,
00334 .end_frame = end_frame,
00335 .get_video_buffer = avfilter_null_get_video_buffer,
00336 .config_props = config_input, },
00337 { .name = NULL}},
00338 .outputs = (AVFilterPad[]) {{ .name = "default",
00339 .type = AVMEDIA_TYPE_VIDEO,
00340 .config_props = config_output, },
00341 { .name = NULL}},
00342 };