00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00039 #include "avfilter.h"
00040 #include "formats.h"
00041 #include "internal.h"
00042 #include "video.h"
00043 #include "libavutil/common.h"
00044 #include "libavutil/mem.h"
00045 #include "libavutil/pixdesc.h"
00046
00047 #define MIN_SIZE 3
00048 #define MAX_SIZE 13
00049
00050
00051 #define SHIFTUP(x,shift) (-((-(x))>>(shift)))
00052
00053 typedef struct FilterParam {
00054 int msize_x;
00055 int msize_y;
00056 int amount;
00057 int steps_x;
00058 int steps_y;
00059 int scalebits;
00060 int32_t halfscale;
00061 uint32_t *sc[(MAX_SIZE * MAX_SIZE) - 1];
00062 } FilterParam;
00063
00064 typedef struct {
00065 FilterParam luma;
00066 FilterParam chroma;
00067 int hsub, vsub;
00068 } UnsharpContext;
00069
00070 static void apply_unsharp( uint8_t *dst, int dst_stride,
00071 const uint8_t *src, int src_stride,
00072 int width, int height, FilterParam *fp)
00073 {
00074 uint32_t **sc = fp->sc;
00075 uint32_t sr[(MAX_SIZE * MAX_SIZE) - 1], tmp1, tmp2;
00076
00077 int32_t res;
00078 int x, y, z;
00079 const uint8_t *src2 = NULL;
00080
00081 if (!fp->amount) {
00082 if (dst_stride == src_stride)
00083 memcpy(dst, src, src_stride * height);
00084 else
00085 for (y = 0; y < height; y++, dst += dst_stride, src += src_stride)
00086 memcpy(dst, src, width);
00087 return;
00088 }
00089
00090 for (y = 0; y < 2 * fp->steps_y; y++)
00091 memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * fp->steps_x));
00092
00093 for (y = -fp->steps_y; y < height + fp->steps_y; y++) {
00094 if (y < height)
00095 src2 = src;
00096
00097 memset(sr, 0, sizeof(sr[0]) * (2 * fp->steps_x - 1));
00098 for (x = -fp->steps_x; x < width + fp->steps_x; x++) {
00099 tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x];
00100 for (z = 0; z < fp->steps_x * 2; z += 2) {
00101 tmp2 = sr[z + 0] + tmp1; sr[z + 0] = tmp1;
00102 tmp1 = sr[z + 1] + tmp2; sr[z + 1] = tmp2;
00103 }
00104 for (z = 0; z < fp->steps_y * 2; z += 2) {
00105 tmp2 = sc[z + 0][x + fp->steps_x] + tmp1; sc[z + 0][x + fp->steps_x] = tmp1;
00106 tmp1 = sc[z + 1][x + fp->steps_x] + tmp2; sc[z + 1][x + fp->steps_x] = tmp2;
00107 }
00108 if (x >= fp->steps_x && y >= fp->steps_y) {
00109 const uint8_t *srx = src - fp->steps_y * src_stride + x - fp->steps_x;
00110 uint8_t *dsx = dst - fp->steps_y * dst_stride + x - fp->steps_x;
00111
00112 res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + fp->halfscale) >> fp->scalebits)) * fp->amount) >> 16);
00113 *dsx = av_clip_uint8(res);
00114 }
00115 }
00116 if (y >= 0) {
00117 dst += dst_stride;
00118 src += src_stride;
00119 }
00120 }
00121 }
00122
00123 static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double amount)
00124 {
00125 fp->msize_x = msize_x;
00126 fp->msize_y = msize_y;
00127 fp->amount = amount * 65536.0;
00128
00129 fp->steps_x = msize_x / 2;
00130 fp->steps_y = msize_y / 2;
00131 fp->scalebits = (fp->steps_x + fp->steps_y) * 2;
00132 fp->halfscale = 1 << (fp->scalebits - 1);
00133 }
00134
00135 static av_cold int init(AVFilterContext *ctx, const char *args)
00136 {
00137 UnsharpContext *unsharp = ctx->priv;
00138 int lmsize_x = 5, cmsize_x = 5;
00139 int lmsize_y = 5, cmsize_y = 5;
00140 double lamount = 1.0f, camount = 0.0f;
00141
00142 if (args)
00143 sscanf(args, "%d:%d:%lf:%d:%d:%lf", &lmsize_x, &lmsize_y, &lamount,
00144 &cmsize_x, &cmsize_y, &camount);
00145
00146 if ((lamount && (lmsize_x < 2 || lmsize_y < 2)) ||
00147 (camount && (cmsize_x < 2 || cmsize_y < 2))) {
00148 av_log(ctx, AV_LOG_ERROR,
00149 "Invalid value <2 for lmsize_x:%d or lmsize_y:%d or cmsize_x:%d or cmsize_y:%d\n",
00150 lmsize_x, lmsize_y, cmsize_x, cmsize_y);
00151 return AVERROR(EINVAL);
00152 }
00153
00154 set_filter_param(&unsharp->luma, lmsize_x, lmsize_y, lamount);
00155 set_filter_param(&unsharp->chroma, cmsize_x, cmsize_y, camount);
00156
00157 return 0;
00158 }
00159
00160 static int query_formats(AVFilterContext *ctx)
00161 {
00162 enum AVPixelFormat pix_fmts[] = {
00163 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
00164 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
00165 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
00166 };
00167
00168 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00169
00170 return 0;
00171 }
00172
00173 static void init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width)
00174 {
00175 int z;
00176 const char *effect;
00177
00178 effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
00179
00180 av_log(ctx, AV_LOG_VERBOSE, "effect:%s type:%s msize_x:%d msize_y:%d amount:%0.2f\n",
00181 effect, effect_type, fp->msize_x, fp->msize_y, fp->amount / 65535.0);
00182
00183 for (z = 0; z < 2 * fp->steps_y; z++)
00184 fp->sc[z] = av_malloc(sizeof(*(fp->sc[z])) * (width + 2 * fp->steps_x));
00185 }
00186
00187 static int config_props(AVFilterLink *link)
00188 {
00189 UnsharpContext *unsharp = link->dst->priv;
00190 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
00191
00192 unsharp->hsub = desc->log2_chroma_w;
00193 unsharp->vsub = desc->log2_chroma_h;
00194
00195 init_filter_param(link->dst, &unsharp->luma, "luma", link->w);
00196 init_filter_param(link->dst, &unsharp->chroma, "chroma", SHIFTUP(link->w, unsharp->hsub));
00197
00198 return 0;
00199 }
00200
00201 static void free_filter_param(FilterParam *fp)
00202 {
00203 int z;
00204
00205 for (z = 0; z < 2 * fp->steps_y; z++)
00206 av_free(fp->sc[z]);
00207 }
00208
00209 static av_cold void uninit(AVFilterContext *ctx)
00210 {
00211 UnsharpContext *unsharp = ctx->priv;
00212
00213 free_filter_param(&unsharp->luma);
00214 free_filter_param(&unsharp->chroma);
00215 }
00216
00217 static int filter_frame(AVFilterLink *link, AVFilterBufferRef *in)
00218 {
00219 UnsharpContext *unsharp = link->dst->priv;
00220 AVFilterLink *outlink = link->dst->outputs[0];
00221 AVFilterBufferRef *out;
00222 int cw = SHIFTUP(link->w, unsharp->hsub);
00223 int ch = SHIFTUP(link->h, unsharp->vsub);
00224
00225 out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
00226 if (!out) {
00227 avfilter_unref_bufferp(&in);
00228 return AVERROR(ENOMEM);
00229 }
00230 avfilter_copy_buffer_ref_props(out, in);
00231
00232 apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma);
00233 apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, &unsharp->chroma);
00234 apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, &unsharp->chroma);
00235
00236 avfilter_unref_bufferp(&in);
00237 return ff_filter_frame(outlink, out);
00238 }
00239
00240 static const AVFilterPad avfilter_vf_unsharp_inputs[] = {
00241 {
00242 .name = "default",
00243 .type = AVMEDIA_TYPE_VIDEO,
00244 .filter_frame = filter_frame,
00245 .config_props = config_props,
00246 .min_perms = AV_PERM_READ,
00247 },
00248 { NULL }
00249 };
00250
00251 static const AVFilterPad avfilter_vf_unsharp_outputs[] = {
00252 {
00253 .name = "default",
00254 .type = AVMEDIA_TYPE_VIDEO,
00255 },
00256 { NULL }
00257 };
00258
00259 AVFilter avfilter_vf_unsharp = {
00260 .name = "unsharp",
00261 .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
00262
00263 .priv_size = sizeof(UnsharpContext),
00264
00265 .init = init,
00266 .uninit = uninit,
00267 .query_formats = query_formats,
00268
00269 .inputs = avfilter_vf_unsharp_inputs,
00270
00271 .outputs = avfilter_vf_unsharp_outputs,
00272 };