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