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