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] = (float) inlink->w / inlink->h;
00158 var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
00159 (float) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
00160 var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
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 if (outfmt == PIX_FMT_PAL8) outfmt = PIX_FMT_BGR8;
00217
00218 if (scale->sws)
00219 sws_freeContext(scale->sws);
00220 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
00221 outlink->w, outlink->h, outfmt,
00222 scale->flags, NULL, NULL, NULL);
00223 if (scale->isws[0])
00224 sws_freeContext(scale->isws[0]);
00225 scale->isws[0] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
00226 outlink->w, outlink->h/2, outfmt,
00227 scale->flags, NULL, NULL, NULL);
00228 if (scale->isws[1])
00229 sws_freeContext(scale->isws[1]);
00230 scale->isws[1] = sws_getContext(inlink ->w, inlink ->h/2, inlink ->format,
00231 outlink->w, outlink->h/2, outfmt,
00232 scale->flags, NULL, NULL, NULL);
00233 if (!scale->sws || !scale->isws[0] || !scale->isws[1])
00234 return AVERROR(EINVAL);
00235
00236 if (inlink->sample_aspect_ratio.num){
00237 outlink->sample_aspect_ratio = av_mul_q((AVRational){outlink->h * inlink->w, outlink->w * inlink->h}, inlink->sample_aspect_ratio);
00238 } else
00239 outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
00240
00241 return 0;
00242
00243 fail:
00244 av_log(NULL, AV_LOG_ERROR,
00245 "Error when evaluating the expression '%s'.\n"
00246 "Maybe the expression for out_w:'%s' or for out_h:'%s' is self-referencing.\n",
00247 expr, scale->w_expr, scale->h_expr);
00248 return ret;
00249 }
00250
00251 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00252 {
00253 ScaleContext *scale = link->dst->priv;
00254 AVFilterLink *outlink = link->dst->outputs[0];
00255 AVFilterBufferRef *outpicref;
00256
00257 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
00258 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00259
00260 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_ALIGN, outlink->w, outlink->h);
00261 avfilter_copy_buffer_ref_props(outpicref, picref);
00262 outpicref->video->w = outlink->w;
00263 outpicref->video->h = outlink->h;
00264
00265 outlink->out_buf = outpicref;
00266
00267 av_reduce(&outpicref->video->sample_aspect_ratio.num, &outpicref->video->sample_aspect_ratio.den,
00268 (int64_t)picref->video->sample_aspect_ratio.num * outlink->h * link->w,
00269 (int64_t)picref->video->sample_aspect_ratio.den * outlink->w * link->h,
00270 INT_MAX);
00271
00272 scale->slice_y = 0;
00273 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
00274 }
00275
00276 static int scale_slice(AVFilterLink *link, struct SwsContext *sws, int y, int h, int mul, int field)
00277 {
00278 ScaleContext *scale = link->dst->priv;
00279 AVFilterBufferRef *cur_pic = link->cur_buf;
00280 AVFilterBufferRef *out_buf = link->dst->outputs[0]->out_buf;
00281 const uint8_t *in[4];
00282 uint8_t *out[4];
00283 int in_stride[4],out_stride[4];
00284 int i;
00285
00286 for(i=0; i<4; i++){
00287 int vsub= ((i+1)&2) ? scale->vsub : 0;
00288 in_stride[i] = cur_pic->linesize[i] * mul;
00289 out_stride[i] = out_buf->linesize[i] * mul;
00290 in[i] = cur_pic->data[i] + ((y>>vsub)+field) * cur_pic->linesize[i];
00291 out[i] = out_buf->data[i] + field * out_buf->linesize[i];
00292 }
00293 if(scale->input_is_pal){
00294 in[1] = cur_pic->data[1];
00295 out[1] = out_buf->data[1];
00296 }
00297
00298 return sws_scale(sws, in, in_stride, y/mul, h,
00299 out,out_stride);
00300 }
00301
00302 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00303 {
00304 ScaleContext *scale = link->dst->priv;
00305 int out_h;
00306
00307 if (scale->slice_y == 0 && slice_dir == -1)
00308 scale->slice_y = link->dst->outputs[0]->h;
00309
00310 if(scale->interlaced>0 || (scale->interlaced<0 && link->cur_buf->video->interlaced)){
00311 av_assert0(y%(2<<scale->vsub) == 0);
00312 out_h = scale_slice(link, scale->isws[0], y, (h+1)/2, 2, 0);
00313 out_h+= scale_slice(link, scale->isws[1], y, h /2, 2, 1);
00314 }else{
00315 out_h = scale_slice(link, scale->sws, y, h, 1, 0);
00316 }
00317
00318 if (slice_dir == -1)
00319 scale->slice_y -= out_h;
00320 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
00321 if (slice_dir == 1)
00322 scale->slice_y += out_h;
00323 }
00324
00325 AVFilter avfilter_vf_scale = {
00326 .name = "scale",
00327 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
00328
00329 .init = init,
00330 .uninit = uninit,
00331
00332 .query_formats = query_formats,
00333
00334 .priv_size = sizeof(ScaleContext),
00335
00336 .inputs = (const AVFilterPad[]) {{ .name = "default",
00337 .type = AVMEDIA_TYPE_VIDEO,
00338 .start_frame = start_frame,
00339 .draw_slice = draw_slice,
00340 .min_perms = AV_PERM_READ, },
00341 { .name = NULL}},
00342 .outputs = (const AVFilterPad[]) {{ .name = "default",
00343 .type = AVMEDIA_TYPE_VIDEO,
00344 .config_props = config_props, },
00345 { .name = NULL}},
00346 };