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