00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #include "avfilter.h"
00027 #include "libavutil/avstring.h"
00028 #include "libavutil/eval.h"
00029 #include "libavutil/mathematics.h"
00030 #include "libavutil/pixdesc.h"
00031 #include "libswscale/swscale.h"
00032
00033 static const char *var_names[] = {
00034 "PI",
00035 "PHI",
00036 "E",
00037 "in_w", "iw",
00038 "in_h", "ih",
00039 "out_w", "ow",
00040 "out_h", "oh",
00041 "a", "dar",
00042 "sar",
00043 "hsub",
00044 "vsub",
00045 NULL
00046 };
00047
00048 enum var_name {
00049 VAR_PI,
00050 VAR_PHI,
00051 VAR_E,
00052 VAR_IN_W, VAR_IW,
00053 VAR_IN_H, VAR_IH,
00054 VAR_OUT_W, VAR_OW,
00055 VAR_OUT_H, VAR_OH,
00056 VAR_A, VAR_DAR,
00057 VAR_SAR,
00058 VAR_HSUB,
00059 VAR_VSUB,
00060 VARS_NB
00061 };
00062
00063 typedef struct {
00064 struct SwsContext *sws;
00065
00071 int w, h;
00072 unsigned int flags;
00073
00074 int hsub, vsub;
00075 int slice_y;
00076 int input_is_pal;
00077
00078 char w_expr[256];
00079 char h_expr[256];
00080 } ScaleContext;
00081
00082 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00083 {
00084 ScaleContext *scale = ctx->priv;
00085 const char *p;
00086
00087 av_strlcpy(scale->w_expr, "iw", sizeof(scale->w_expr));
00088 av_strlcpy(scale->h_expr, "ih", sizeof(scale->h_expr));
00089
00090 scale->flags = SWS_BILINEAR;
00091 if (args) {
00092 sscanf(args, "%255[^:]:%255[^:]", scale->w_expr, scale->h_expr);
00093 p = strstr(args,"flags=");
00094 if (p) scale->flags = strtoul(p+6, NULL, 0);
00095 }
00096
00097 return 0;
00098 }
00099
00100 static av_cold void uninit(AVFilterContext *ctx)
00101 {
00102 ScaleContext *scale = ctx->priv;
00103 sws_freeContext(scale->sws);
00104 scale->sws = NULL;
00105 }
00106
00107 static int query_formats(AVFilterContext *ctx)
00108 {
00109 AVFilterFormats *formats;
00110 enum PixelFormat pix_fmt;
00111 int ret;
00112
00113 if (ctx->inputs[0]) {
00114 formats = NULL;
00115 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
00116 if ( sws_isSupportedInput(pix_fmt)
00117 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
00118 avfilter_formats_unref(&formats);
00119 return ret;
00120 }
00121 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
00122 }
00123 if (ctx->outputs[0]) {
00124 formats = NULL;
00125 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
00126 if ( sws_isSupportedOutput(pix_fmt)
00127 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
00128 avfilter_formats_unref(&formats);
00129 return ret;
00130 }
00131 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
00132 }
00133
00134 return 0;
00135 }
00136
00137 static int config_props(AVFilterLink *outlink)
00138 {
00139 AVFilterContext *ctx = outlink->src;
00140 AVFilterLink *inlink = outlink->src->inputs[0];
00141 ScaleContext *scale = ctx->priv;
00142 int64_t w, h;
00143 double var_values[VARS_NB], res;
00144 char *expr;
00145 int ret;
00146
00147 var_values[VAR_PI] = M_PI;
00148 var_values[VAR_PHI] = M_PHI;
00149 var_values[VAR_E] = M_E;
00150 var_values[VAR_IN_W] = var_values[VAR_IW] = inlink->w;
00151 var_values[VAR_IN_H] = var_values[VAR_IH] = inlink->h;
00152 var_values[VAR_OUT_W] = var_values[VAR_OW] = NAN;
00153 var_values[VAR_OUT_H] = var_values[VAR_OH] = NAN;
00154 var_values[VAR_DAR] = var_values[VAR_A] = (float) inlink->w / inlink->h;
00155 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
00156 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
00157 var_values[VAR_HSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00158 var_values[VAR_VSUB] = 1<<av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00159
00160
00161 av_expr_parse_and_eval(&res, (expr = scale->w_expr),
00162 var_names, var_values,
00163 NULL, NULL, NULL, NULL, NULL, 0, ctx);
00164 scale->w = var_values[VAR_OUT_W] = var_values[VAR_OW] = res;
00165 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->h_expr),
00166 var_names, var_values,
00167 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00168 goto fail;
00169 scale->h = var_values[VAR_OUT_H] = var_values[VAR_OH] = res;
00170
00171 if ((ret = av_expr_parse_and_eval(&res, (expr = scale->w_expr),
00172 var_names, var_values,
00173 NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
00174 goto fail;
00175 scale->w = res;
00176
00177 w = scale->w;
00178 h = scale->h;
00179
00180
00181 if (w < -1 || h < -1) {
00182 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
00183 return AVERROR(EINVAL);
00184 }
00185 if (w == -1 && h == -1)
00186 scale->w = scale->h = 0;
00187
00188 if (!(w = scale->w))
00189 w = inlink->w;
00190 if (!(h = scale->h))
00191 h = inlink->h;
00192 if (w == -1)
00193 w = av_rescale(h, inlink->w, inlink->h);
00194 if (h == -1)
00195 h = av_rescale(w, inlink->h, inlink->w);
00196
00197 if (w > INT_MAX || h > INT_MAX ||
00198 (h * inlink->w) > INT_MAX ||
00199 (w * inlink->h) > INT_MAX)
00200 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
00201
00202 outlink->w = w;
00203 outlink->h = h;
00204
00205
00206 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
00207 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
00208 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
00209 scale->flags);
00210
00211 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
00212
00213 if (scale->sws)
00214 sws_freeContext(scale->sws);
00215 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
00216 outlink->w, outlink->h, outlink->format,
00217 scale->flags, NULL, NULL, NULL);
00218 if (!scale->sws)
00219 return AVERROR(EINVAL);
00220
00221
00222 if (inlink->sample_aspect_ratio.num)
00223 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h*inlink->w,
00224 outlink->w*inlink->h},
00225 inlink->sample_aspect_ratio);
00226 else
00227 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
00228
00229 return 0;
00230
00231 fail:
00232 av_log(NULL, AV_LOG_ERROR,
00233 "Error when evaluating the expression '%s'\n", expr);
00234 return ret;
00235 }
00236
00237 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00238 {
00239 ScaleContext *scale = link->dst->priv;
00240 AVFilterLink *outlink = link->dst->outputs[0];
00241 AVFilterBufferRef *outpicref;
00242
00243 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
00244 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00245
00246 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00247 avfilter_copy_buffer_ref_props(outpicref, picref);
00248 outpicref->video->w = outlink->w;
00249 outpicref->video->h = outlink->h;
00250
00251 outlink->out_buf = outpicref;
00252
00253 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
00254 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
00255 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
00256 INT_MAX);
00257
00258 scale->slice_y = 0;
00259 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
00260 }
00261
00262 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00263 {
00264 ScaleContext *scale = link->dst->priv;
00265 int out_h;
00266 AVFilterBufferRef *cur_pic = link->cur_buf;
00267 const uint8_t *data[4];
00268
00269 if (scale->slice_y == 0 && slice_dir == -1)
00270 scale->slice_y = link->dst->outputs[0]->h;
00271
00272 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
00273 data[1] = scale->input_is_pal ?
00274 cur_pic->data[1] :
00275 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
00276 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
00277 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
00278
00279 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
00280 link->dst->outputs[0]->out_buf->data,
00281 link->dst->outputs[0]->out_buf->linesize);
00282
00283 if (slice_dir == -1)
00284 scale->slice_y -= out_h;
00285 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
00286 if (slice_dir == 1)
00287 scale->slice_y += out_h;
00288 }
00289
00290 AVFilter avfilter_vf_scale = {
00291 .name = "scale",
00292 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
00293
00294 .init = init,
00295 .uninit = uninit,
00296
00297 .query_formats = query_formats,
00298
00299 .priv_size = sizeof(ScaleContext),
00300
00301 .inputs = (AVFilterPad[]) {{ .name = "default",
00302 .type = AVMEDIA_TYPE_VIDEO,
00303 .start_frame = start_frame,
00304 .draw_slice = draw_slice,
00305 .min_perms = AV_PERM_READ, },
00306 { .name = NULL}},
00307 .outputs = (AVFilterPad[]) {{ .name = "default",
00308 .type = AVMEDIA_TYPE_VIDEO,
00309 .config_props = config_props, },
00310 { .name = NULL}},
00311 };