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/pixdesc.h"
00028 #include "libswscale/swscale.h"
00029
00030 typedef struct {
00031 struct SwsContext *sws;
00032
00038 int w, h;
00039 unsigned int flags;
00040
00041 int hsub, vsub;
00042 int slice_y;
00043 int input_is_pal;
00044 } ScaleContext;
00045
00046 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00047 {
00048 ScaleContext *scale = ctx->priv;
00049 const char *p;
00050
00051 scale->flags = SWS_BILINEAR;
00052 if (args) {
00053 sscanf(args, "%d:%d", &scale->w, &scale->h);
00054 p = strstr(args,"flags=");
00055 if (p) scale->flags = strtoul(p+6, NULL, 0);
00056 }
00057
00058
00059 if (scale->w < -1 || scale->h < -1) {
00060 av_log(ctx, AV_LOG_ERROR, "Size values less than -1 are not acceptable.\n");
00061 return AVERROR(EINVAL);
00062 }
00063 if (scale->w == -1 && scale->h == -1)
00064 scale->w = scale->h = 0;
00065
00066 return 0;
00067 }
00068
00069 static av_cold void uninit(AVFilterContext *ctx)
00070 {
00071 ScaleContext *scale = ctx->priv;
00072 sws_freeContext(scale->sws);
00073 scale->sws = NULL;
00074 }
00075
00076 static int query_formats(AVFilterContext *ctx)
00077 {
00078 AVFilterFormats *formats;
00079 enum PixelFormat pix_fmt;
00080 int ret;
00081
00082 if (ctx->inputs[0]) {
00083 formats = NULL;
00084 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
00085 if ( sws_isSupportedInput(pix_fmt)
00086 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
00087 avfilter_formats_unref(&formats);
00088 return ret;
00089 }
00090 avfilter_formats_ref(formats, &ctx->inputs[0]->out_formats);
00091 }
00092 if (ctx->outputs[0]) {
00093 formats = NULL;
00094 for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++)
00095 if ( sws_isSupportedOutput(pix_fmt)
00096 && (ret = avfilter_add_format(&formats, pix_fmt)) < 0) {
00097 avfilter_formats_unref(&formats);
00098 return ret;
00099 }
00100 avfilter_formats_ref(formats, &ctx->outputs[0]->in_formats);
00101 }
00102
00103 return 0;
00104 }
00105
00106 static int config_props(AVFilterLink *outlink)
00107 {
00108 AVFilterContext *ctx = outlink->src;
00109 AVFilterLink *inlink = outlink->src->inputs[0];
00110 ScaleContext *scale = ctx->priv;
00111 int64_t w, h;
00112
00113 if (!(w = scale->w))
00114 w = inlink->w;
00115 if (!(h = scale->h))
00116 h = inlink->h;
00117 if (w == -1)
00118 w = av_rescale(h, inlink->w, inlink->h);
00119 if (h == -1)
00120 h = av_rescale(w, inlink->h, inlink->w);
00121
00122 if (w > INT_MAX || h > INT_MAX ||
00123 (h * inlink->w) > INT_MAX ||
00124 (w * inlink->h) > INT_MAX)
00125 av_log(ctx, AV_LOG_ERROR, "Rescaled value for width or height is too big.\n");
00126
00127 outlink->w = w;
00128 outlink->h = h;
00129
00130
00131 av_log(ctx, AV_LOG_INFO, "w:%d h:%d fmt:%s -> w:%d h:%d fmt:%s flags:0x%0x\n",
00132 inlink ->w, inlink ->h, av_pix_fmt_descriptors[ inlink->format].name,
00133 outlink->w, outlink->h, av_pix_fmt_descriptors[outlink->format].name,
00134 scale->flags);
00135
00136 scale->input_is_pal = av_pix_fmt_descriptors[inlink->format].flags & PIX_FMT_PAL;
00137
00138 scale->sws = sws_getContext(inlink ->w, inlink ->h, inlink ->format,
00139 outlink->w, outlink->h, outlink->format,
00140 scale->flags, NULL, NULL, NULL);
00141 if (!scale->sws)
00142 return AVERROR(EINVAL);
00143
00144 return 0;
00145 }
00146
00147 static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
00148 {
00149 ScaleContext *scale = link->dst->priv;
00150 AVFilterLink *outlink = link->dst->outputs[0];
00151 AVFilterBufferRef *outpicref;
00152
00153 scale->hsub = av_pix_fmt_descriptors[link->format].log2_chroma_w;
00154 scale->vsub = av_pix_fmt_descriptors[link->format].log2_chroma_h;
00155
00156 outpicref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00157 avfilter_copy_buffer_ref_props(outpicref, picref);
00158 outpicref->video->w = outlink->w;
00159 outpicref->video->h = outlink->h;
00160
00161 outlink->out_buf = outpicref;
00162
00163 av_reduce(&outpicref->video->pixel_aspect.num, &outpicref->video->pixel_aspect.den,
00164 (int64_t)picref->video->pixel_aspect.num * outlink->h * link->w,
00165 (int64_t)picref->video->pixel_aspect.den * outlink->w * link->h,
00166 INT_MAX);
00167
00168 scale->slice_y = 0;
00169 avfilter_start_frame(outlink, avfilter_ref_buffer(outpicref, ~0));
00170 }
00171
00172 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00173 {
00174 ScaleContext *scale = link->dst->priv;
00175 int out_h;
00176 AVFilterBufferRef *cur_pic = link->cur_buf;
00177 const uint8_t *data[4];
00178
00179 if (scale->slice_y == 0 && slice_dir == -1)
00180 scale->slice_y = link->dst->outputs[0]->h;
00181
00182 data[0] = cur_pic->data[0] + y * cur_pic->linesize[0];
00183 data[1] = scale->input_is_pal ?
00184 cur_pic->data[1] :
00185 cur_pic->data[1] + (y>>scale->vsub) * cur_pic->linesize[1];
00186 data[2] = cur_pic->data[2] + (y>>scale->vsub) * cur_pic->linesize[2];
00187 data[3] = cur_pic->data[3] + y * cur_pic->linesize[3];
00188
00189 out_h = sws_scale(scale->sws, data, cur_pic->linesize, y, h,
00190 link->dst->outputs[0]->out_buf->data,
00191 link->dst->outputs[0]->out_buf->linesize);
00192
00193 if (slice_dir == -1)
00194 scale->slice_y -= out_h;
00195 avfilter_draw_slice(link->dst->outputs[0], scale->slice_y, out_h, slice_dir);
00196 if (slice_dir == 1)
00197 scale->slice_y += out_h;
00198 }
00199
00200 AVFilter avfilter_vf_scale = {
00201 .name = "scale",
00202 .description = NULL_IF_CONFIG_SMALL("Scale the input video to width:height size and/or convert the image format."),
00203
00204 .init = init,
00205 .uninit = uninit,
00206
00207 .query_formats = query_formats,
00208
00209 .priv_size = sizeof(ScaleContext),
00210
00211 .inputs = (AVFilterPad[]) {{ .name = "default",
00212 .type = AVMEDIA_TYPE_VIDEO,
00213 .start_frame = start_frame,
00214 .draw_slice = draw_slice,
00215 .min_perms = AV_PERM_READ, },
00216 { .name = NULL}},
00217 .outputs = (AVFilterPad[]) {{ .name = "default",
00218 .type = AVMEDIA_TYPE_VIDEO,
00219 .config_props = config_props, },
00220 { .name = NULL}},
00221 };