• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00005  *
00006  * Libav 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  * Libav 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 Libav; 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 *var_names[] = {
00036     "E",
00037     "PHI",
00038     "PI",
00039     "in_w", "iw",   
00040     "in_h", "ih",   
00041     "out_w", "ow",  
00042     "out_h", "oh",  
00043     "x",
00044     "y",
00045     "n",            
00046     "pos",          
00047     "t",            
00048     NULL
00049 };
00050 
00051 enum var_name {
00052     VAR_E,
00053     VAR_PHI,
00054     VAR_PI,
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_X,
00060     VAR_Y,
00061     VAR_N,
00062     VAR_POS,
00063     VAR_T,
00064     VAR_VARS_NB
00065 };
00066 
00067 typedef struct {
00068     int  x;             
00069     int  y;             
00070     int  w;             
00071     int  h;             
00072 
00073     int max_step[4];    
00074     int hsub, vsub;     
00075     char x_expr[256], y_expr[256], ow_expr[256], oh_expr[256];
00076     AVExpr *x_pexpr, *y_pexpr;  /* parsed expressions for x and y */
00077     double var_values[VAR_VARS_NB];
00078 } CropContext;
00079 
00080 static int query_formats(AVFilterContext *ctx)
00081 {
00082     static const enum PixelFormat pix_fmts[] = {
00083         PIX_FMT_RGB48BE,      PIX_FMT_RGB48LE,
00084         PIX_FMT_BGR48BE,      PIX_FMT_BGR48LE,
00085         PIX_FMT_ARGB,         PIX_FMT_RGBA,
00086         PIX_FMT_ABGR,         PIX_FMT_BGRA,
00087         PIX_FMT_RGB24,        PIX_FMT_BGR24,
00088         PIX_FMT_RGB565BE,     PIX_FMT_RGB565LE,
00089         PIX_FMT_RGB555BE,     PIX_FMT_RGB555LE,
00090         PIX_FMT_BGR565BE,     PIX_FMT_BGR565LE,
00091         PIX_FMT_BGR555BE,     PIX_FMT_BGR555LE,
00092         PIX_FMT_GRAY16BE,     PIX_FMT_GRAY16LE,
00093         PIX_FMT_YUV420P16LE,  PIX_FMT_YUV420P16BE,
00094         PIX_FMT_YUV422P16LE,  PIX_FMT_YUV422P16BE,
00095         PIX_FMT_YUV444P16LE,  PIX_FMT_YUV444P16BE,
00096         PIX_FMT_YUV444P,      PIX_FMT_YUV422P,
00097         PIX_FMT_YUV420P,      PIX_FMT_YUV411P,
00098         PIX_FMT_YUV410P,      PIX_FMT_YUV440P,
00099         PIX_FMT_YUVJ444P,     PIX_FMT_YUVJ422P,
00100         PIX_FMT_YUVJ420P,     PIX_FMT_YUVJ440P,
00101         PIX_FMT_YUVA420P,
00102         PIX_FMT_RGB8,         PIX_FMT_BGR8,
00103         PIX_FMT_RGB4_BYTE,    PIX_FMT_BGR4_BYTE,
00104         PIX_FMT_PAL8,         PIX_FMT_GRAY8,
00105         PIX_FMT_NONE
00106     };
00107 
00108     avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00109 
00110     return 0;
00111 }
00112 
00113 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00114 {
00115     CropContext *crop = ctx->priv;
00116 
00117     av_strlcpy(crop->ow_expr, "iw", sizeof(crop->ow_expr));
00118     av_strlcpy(crop->oh_expr, "ih", sizeof(crop->oh_expr));
00119     av_strlcpy(crop->x_expr, "(in_w-out_w)/2", sizeof(crop->x_expr));
00120     av_strlcpy(crop->y_expr, "(in_h-out_h)/2", sizeof(crop->y_expr));
00121 
00122     if (args)
00123         sscanf(args, "%255[^:]:%255[^:]:%255[^:]:%255[^:]", crop->ow_expr, crop->oh_expr, crop->x_expr, crop->y_expr);
00124 
00125     return 0;
00126 }
00127 
00128 static av_cold void uninit(AVFilterContext *ctx)
00129 {
00130     CropContext *crop = ctx->priv;
00131 
00132     av_expr_free(crop->x_pexpr); crop->x_pexpr = NULL;
00133     av_expr_free(crop->y_pexpr); crop->y_pexpr = NULL;
00134 }
00135 
00136 static inline int normalize_double(int *n, double d)
00137 {
00138     int ret = 0;
00139 
00140     if (isnan(d)) {
00141         ret = AVERROR(EINVAL);
00142     } else if (d > INT_MAX || d < INT_MIN) {
00143         *n = d > INT_MAX ? INT_MAX : INT_MIN;
00144         ret = AVERROR(EINVAL);
00145     } else
00146         *n = round(d);
00147 
00148     return ret;
00149 }
00150 
00151 static int config_input(AVFilterLink *link)
00152 {
00153     AVFilterContext *ctx = link->dst;
00154     CropContext *crop = ctx->priv;
00155     const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[link->format];
00156     int ret;
00157     const char *expr;
00158     double res;
00159 
00160     crop->var_values[VAR_E]     = M_E;
00161     crop->var_values[VAR_PHI]   = M_PHI;
00162     crop->var_values[VAR_PI]    = M_PI;
00163     crop->var_values[VAR_IN_W]  = crop->var_values[VAR_IW] = ctx->inputs[0]->w;
00164     crop->var_values[VAR_IN_H]  = crop->var_values[VAR_IH] = ctx->inputs[0]->h;
00165     crop->var_values[VAR_X]     = NAN;
00166     crop->var_values[VAR_Y]     = NAN;
00167     crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = NAN;
00168     crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = NAN;
00169     crop->var_values[VAR_N]     = 0;
00170     crop->var_values[VAR_T]     = NAN;
00171     crop->var_values[VAR_POS]   = NAN;
00172 
00173     av_image_fill_max_pixsteps(crop->max_step, NULL, pix_desc);
00174     crop->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
00175     crop->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00176 
00177     if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
00178                                       var_names, crop->var_values,
00179                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00180     crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
00181     if ((ret = av_expr_parse_and_eval(&res, (expr = crop->oh_expr),
00182                                       var_names, crop->var_values,
00183                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00184     crop->var_values[VAR_OUT_H] = crop->var_values[VAR_OH] = res;
00185     /* evaluate again ow as it may depend on oh */
00186     if ((ret = av_expr_parse_and_eval(&res, (expr = crop->ow_expr),
00187                                       var_names, crop->var_values,
00188                                       NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) goto fail_expr;
00189     crop->var_values[VAR_OUT_W] = crop->var_values[VAR_OW] = res;
00190     if (normalize_double(&crop->w, crop->var_values[VAR_OUT_W]) < 0 ||
00191         normalize_double(&crop->h, crop->var_values[VAR_OUT_H]) < 0) {
00192         av_log(ctx, AV_LOG_ERROR,
00193                "Too big value or invalid expression for out_w/ow or out_h/oh. "
00194                "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
00195                crop->ow_expr, crop->oh_expr);
00196         return AVERROR(EINVAL);
00197     }
00198     crop->w &= ~((1 << crop->hsub) - 1);
00199     crop->h &= ~((1 << crop->vsub) - 1);
00200 
00201     if ((ret = av_expr_parse(&crop->x_pexpr, crop->x_expr, var_names,
00202                              NULL, NULL, NULL, NULL, 0, ctx)) < 0 ||
00203         (ret = av_expr_parse(&crop->y_pexpr, crop->y_expr, var_names,
00204                              NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00205         return AVERROR(EINVAL);
00206 
00207     av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d\n",
00208            link->w, link->h, crop->w, crop->h);
00209 
00210     if (crop->w <= 0 || crop->h <= 0 ||
00211         crop->w > link->w || crop->h > link->h) {
00212         av_log(ctx, AV_LOG_ERROR,
00213                "Invalid too big or non positive size for width '%d' or height '%d'\n",
00214                crop->w, crop->h);
00215         return AVERROR(EINVAL);
00216     }
00217 
00218     /* set default, required in the case the first computed value for x/y is NAN */
00219     crop->x = (link->w - crop->w) / 2;
00220     crop->y = (link->h - crop->h) / 2;
00221     crop->x &= ~((1 << crop->hsub) - 1);
00222     crop->y &= ~((1 << crop->vsub) - 1);
00223     return 0;
00224 
00225 fail_expr:
00226     av_log(NULL, AV_LOG_ERROR, "Error when evaluating the expression '%s'\n", expr);
00227     return ret;
00228 }
00229 
00230 static int config_output(AVFilterLink *link)
00231 {
00232     CropContext *crop = link->src->priv;
00233 
00234     link->w = crop->w;
00235     link->h = crop->h;
00236 
00237     return 0;
00238 }
00239 
00240 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00241 {
00242     AVFilterContext *ctx = link->dst;
00243     CropContext *crop = ctx->priv;
00244     AVFilterBufferRef *ref2;
00245     int i;
00246 
00247     ref2 = avfilter_ref_buffer(picref, ~0);
00248     ref2->video->w = crop->w;
00249     ref2->video->h = crop->h;
00250 
00251     crop->var_values[VAR_T] = picref->pts == AV_NOPTS_VALUE ?
00252         NAN : picref->pts * av_q2d(link->time_base);
00253     crop->var_values[VAR_POS] = picref->pos == -1 ? NAN : picref->pos;
00254     crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
00255     crop->var_values[VAR_Y] = av_expr_eval(crop->y_pexpr, crop->var_values, NULL);
00256     crop->var_values[VAR_X] = av_expr_eval(crop->x_pexpr, crop->var_values, NULL);
00257 
00258     normalize_double(&crop->x, crop->var_values[VAR_X]);
00259     normalize_double(&crop->y, crop->var_values[VAR_Y]);
00260 
00261     if (crop->x < 0) crop->x = 0;
00262     if (crop->y < 0) crop->y = 0;
00263     if ((unsigned)crop->x + (unsigned)crop->w > link->w) crop->x = link->w - crop->w;
00264     if ((unsigned)crop->y + (unsigned)crop->h > link->h) crop->y = link->h - crop->h;
00265     crop->x &= ~((1 << crop->hsub) - 1);
00266     crop->y &= ~((1 << crop->vsub) - 1);
00267 
00268     av_dlog(ctx, "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,
00270             crop->y, crop->x+crop->w, crop->y+crop->h);
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     /* alpha plane */
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 };
Generated on Thu Jul 11 2013 15:38:23 for Libav by doxygen 1.7.1