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