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