• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavfilter/vf_crop.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007 Bobby Bingham
00003  *
00004  * This file is part of FFmpeg.
00005  *
00006  * FFmpeg is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * FFmpeg is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with FFmpeg; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00026 /* #define DEBUG */
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 #include "libavutil/mathematics.h"
00034 
00035 static const char * const var_names[] = {
00036     "in_w", "iw",   
00037     "in_h", "ih",   
00038     "out_w", "ow",  
00039     "out_h", "oh",  
00040     "a",
00041     "sar",
00042     "dar",
00043     "hsub",
00044     "vsub",
00045     "x",
00046     "y",
00047     "n",            
00048     "pos",          
00049     "t",            
00050     NULL
00051 };
00052 
00053 enum var_name {
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_A,
00059     VAR_SAR,
00060     VAR_DAR,
00061     VAR_HSUB,
00062     VAR_VSUB,
00063     VAR_X,
00064     VAR_Y,
00065     VAR_N,
00066     VAR_POS,
00067     VAR_T,
00068     VAR_VARS_NB
00069 };
00070 
00071 typedef struct {
00072     int  x;             
00073     int  y;             
00074     int  w;             
00075     int  h;             
00076 
00077     int max_step[4];    
00078     int hsub, vsub;     
00079     char x_expr[256], y_expr[256], ow_expr[256], oh_expr[256];
00080     AVExpr *x_pexpr, *y_pexpr;  /* parsed expressions for x and y */
00081     double var_values[VAR_VARS_NB];
00082 } CropContext;
00083 
00084 static int query_formats(AVFilterContext *ctx)
00085 {
00086     static const enum PixelFormat pix_fmts[] = {
00087         PIX_FMT_RGB48BE,      PIX_FMT_RGB48LE,
00088         PIX_FMT_BGR48BE,      PIX_FMT_BGR48LE,
00089         PIX_FMT_ARGB,         PIX_FMT_RGBA,
00090         PIX_FMT_ABGR,         PIX_FMT_BGRA,
00091         PIX_FMT_RGB24,        PIX_FMT_BGR24,
00092         PIX_FMT_RGB565BE,     PIX_FMT_RGB565LE,
00093         PIX_FMT_RGB555BE,     PIX_FMT_RGB555LE,
00094         PIX_FMT_BGR565BE,     PIX_FMT_BGR565LE,
00095         PIX_FMT_BGR555BE,     PIX_FMT_BGR555LE,
00096         PIX_FMT_GRAY16BE,     PIX_FMT_GRAY16LE,
00097         PIX_FMT_YUV420P16LE,  PIX_FMT_YUV420P16BE,
00098         PIX_FMT_YUV422P16LE,  PIX_FMT_YUV422P16BE,
00099         PIX_FMT_YUV444P16LE,  PIX_FMT_YUV444P16BE,
00100         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
00101         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
00102         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
00103         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
00104         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
00105         PIX_FMT_YUVA420P,
00106         PIX_FMT_RGB8,         PIX_FMT_BGR8,
00107         PIX_FMT_RGB4_BYTE,    PIX_FMT_BGR4_BYTE,
00108         PIX_FMT_PAL8,         PIX_FMT_GRAY8,
00109         PIX_FMT_NONE
00110     };
00111 
00112     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00113 
00114     return 0;
00115 }
00116 
00117 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00118 {
00119     CropContext *crop = ctx->priv;
00120 
00121     av_strlcpy(crop->ow_expr, "iw", sizeof(crop->ow_expr));
00122     av_strlcpy(crop->oh_expr, "ih", sizeof(crop->oh_expr));
00123     av_strlcpy(crop->x_expr, "(in_w-out_w)/2", sizeof(crop->x_expr));
00124     av_strlcpy(crop->y_expr, "(in_h-out_h)/2", sizeof(crop->y_expr));
00125 
00126     if (args)
00127         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->ow_expr, crop->oh_expr, crop->x_expr, crop->y_expr);
00128 
00129     return 0;
00130 }
00131 
00132 static av_cold void uninit(AVFilterContext *ctx)
00133 {
00134     CropContext *crop = ctx->priv;
00135 
00136     av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
00137     av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
00138 }
00139 
00140 static inline int normalize_double(int *n, double d)
00141 {
00142     int ret = 0;
00143 
00144     if (isnan(d)) {
00145         ret = AVERROR(EINVAL);
00146     } else if (d > INT_MAX || d < INT_MIN) {
00147         *n = d > INT_MAX ? INT_MAX : INT_MIN;
00148         ret = AVERROR(EINVAL);
00149     } else
00150         *n = round(d);
00151 
00152     return ret;
00153 }
00154 
00155 static int config_input(AVFilterLink *link)
00156 {
00157     AVFilterContext *ctx = link->dst;
00158     CropContext *crop = ctx->priv;
00159     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
00160     int ret;
00161     const char *expr;
00162     double res;
00163 
00164     crop->var_values[VAR_IN_W]  = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
00165     crop->var_values[VAR_IN_H]  = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
00166     crop->var_values[VAR_A]     = (float) link->w / link->h;
00167     crop->var_values[VAR_SAR]   = link->sample_aspect_ratio.num ? av_q2d(link->sample_aspect_ratio) : 1;
00168     crop->var_values[VAR_DAR]   = crop->var_values[VAR_A] * crop->var_values[VAR_SAR];
00169     crop->var_values[VAR_HSUB]  = 1<<pix_desc->log2_chroma_w;
00170     crop->var_values[VAR_VSUB]  = 1<<pix_desc->log2_chroma_h;
00171     crop->var_values[VAR_X]     = NAN;
00172     crop->var_values[VAR_Y]     = NAN;
00173     crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
00174     crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
00175     crop->var_values[VAR_N]     = 0;
00176     crop->var_values[VAR_T]     = NAN;
00177     crop->var_values[VAR_POS]   = NAN;
00178 
00179     av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
00180     crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
00181     crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00182 
00183     if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
00184                                       var_names, crop->var_values,
00185                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00186     crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
00187     if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_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_H] = crop->var_values[VAR_OH] = res;
00191     /* evaluate again ow as it may depend on oh */
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 (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
00197         normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
00198         av_log(ctx, AV_LOG_ERROR,
00199                "Too big value or invalid expression for out_w/ow or out_h/oh. "
00200                "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
00201                crop->ow_expr, crop->oh_expr);
00202         return AVERROR(EINVAL);
00203     }
00204     crop->w &= ~((1 << crop->hsub) - 1);
00205     crop->h &= ~((1 << crop->vsub) - 1);
00206 
00207     if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
00208                              NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
00209         (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
00210                              NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00211         return AVERROR(EINVAL);
00212 
00213     av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d\n",
00214            link->w, link->h, crop->w, crop->h);
00215 
00216     if (crop->w <= 0 || crop->h <= 0 ||
00217         crop->w > link->w || crop->h > link->h) {
00218         av_log(ctx, AV_LOG_ERROR,
00219                "Invalid too big or non positive size for width '%d' or height '%d'\n",
00220                crop->w, crop->h);
00221         return AVERROR(EINVAL);
00222     }
00223 
00224     /* set default, required in the case the first computed value for x/y is NAN */
00225     crop->x = (link->w - crop->w) / 2;
00226     crop->y = (link->h - crop->h) / 2;
00227     crop->x &= ~((1 << crop->hsub) - 1);
00228     crop->y &= ~((1 << crop->vsub) - 1);
00229     return 0;
00230 
00231 fail_expr:
00232     av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
00233     return ret;
00234 }
00235 
00236 static int config_output(AVFilterLink *link)
00237 {
00238     CropContext *crop = link->src->priv;
00239 
00240     link->w = crop->w;
00241     link->h = crop->h;
00242 
00243     return 0;
00244 }
00245 
00246 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00247 {
00248     AVFilterContext *ctx = link->dst;
00249     CropContext *crop = ctx->priv;
00250     AVFilterBufferRef *ref2;
00251     int i;
00252 
00253     ref2 = avfilter_ref_buffer(picref, ~0);
00254     ref2->video->w = crop->w;
00255     ref2->video->h = crop->h;
00256 
00257     crop->var_values[VAR_T] = picref->pts == AV_NOPTS_VALUE ?
00258         NAN : picref->pts * av_q2d(link->time_base);
00259     crop->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
00260     crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
00261     crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
00262     crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
00263 
00264     normalize_double(&crop->x, crop->var_values[VAR_X]);
00265     normalize_double(&crop->y, crop->var_values[VAR_Y]);
00266 
00267     if (crop->x < 0) crop->x = 0;
00268     if (crop->y < 0) crop->y = 0;
00269     if ((unsigned)crop->x + (unsigned)crop->w > link->w) crop->x = link->w - crop->w;
00270     if ((unsigned)crop->y + (unsigned)crop->h > link->h) crop->y = link->h - crop->h;
00271     crop->x &= ~((1 << crop->hsub) - 1);
00272     crop->y &= ~((1 << crop->vsub) - 1);
00273 
00274     av_dlog(ctx, "n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
00275             (int)crop->var_values[VAR_N], crop->var_values[VAR_T], crop->x,
00276             crop->y, crop->x+crop->w, crop->y+crop->h);
00277 
00278     ref2->data[0] += crop->y * ref2->linesize[0];
00279     ref2->data[0] += crop->x * crop->max_step[0];
00280 
00281     if (!(av_pix_fmt_descriptors[link->format].flags & PIX_FMT_PAL)) {
00282         for (i = 1; i < 3; i ++) {
00283             if (ref2->data[i]) {
00284                 ref2->data[i] += (crop->y >> crop->vsub) * ref2->linesize[i];
00285                 ref2->data[i] += (crop->x * crop->max_step[i]) >> crop->hsub;
00286             }
00287         }
00288     }
00289 
00290     /* alpha plane */
00291     if (ref2->data[3]) {
00292         ref2->data[3] += crop->y * ref2->linesize[3];
00293         ref2->data[3] += crop->x * crop->max_step[3];
00294     }
00295 
00296     avfilter_start_frame(link->dst->outputs[0], ref2);
00297 }
00298 
00299 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00300 {
00301     AVFilterContext *ctx = link->dst;
00302     CropContext *crop = ctx->priv;
00303 
00304     if (y >= crop->y + crop->h || y + h <= crop->y)
00305         return;
00306 
00307     if (y < crop->y) {
00308         h -= crop->y - y;
00309         y  = crop->y;
00310     }
00311     if (y + h > crop->y + crop->h)
00312         h = crop->y + crop->h - y;
00313 
00314     avfilter_draw_slice(ctx->outputs[0], y - crop->y, h, slice_dir);
00315 }
00316 
00317 static void end_frame(AVFilterLink *link)
00318 {
00319     CropContext *crop = link->dst->priv;
00320 
00321     crop->var_values[VAR_N] += 1.0;
00322     avfilter_unref_buffer(link->cur_buf);
00323     avfilter_end_frame(link->dst->outputs[0]);
00324 }
00325 
00326 AVFilter avfilter_vf_crop = {
00327     .name      = "crop",
00328     .description = NULL_IF_CONFIG_SMALL("Crop the input video to width:height:x:y."),
00329 
00330     .priv_size = sizeof(CropContext),
00331 
00332     .query_formats = query_formats,
00333     .init          = init,
00334     .uninit        = uninit,
00335 
00336     .inputs    = (const AVFilterPad[]) {{ .name       = "default",
00337                                     .type             = AVMEDIA_TYPE_VIDEO,
00338                                     .start_frame      = start_frame,
00339                                     .draw_slice       = draw_slice,
00340                                     .end_frame        = end_frame,
00341                                     .get_video_buffer = avfilter_null_get_video_buffer,
00342                                     .config_props     = config_input, },
00343                                   { .name = NULL}},
00344     .outputs   = (const AVFilterPad[]) {{ .name       = "default",
00345                                     .type             = AVMEDIA_TYPE_VIDEO,
00346                                     .config_props     = config_output, },
00347                                   { .name = NULL}},
00348 };
Generated on Fri Feb 1 2013 14:34:49 for FFmpeg by doxygen 1.7.1