Libav
|
00001 /* 00002 * Ported to FFmpeg from MPlayer libmpcodecs/unsharp.c 00003 * Original copyright (C) 2002 Remi Guyomarch <rguyom@pobox.com> 00004 * Port copyright (C) 2010 Daniel G. Taylor <dan@programmer-art.org> 00005 * Relicensed to the LGPL with permission from Remi Guyomarch. 00006 * 00007 * This file is part of FFmpeg. 00008 * 00009 * FFmpeg is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * FFmpeg is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with FFmpeg; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00039 #include "avfilter.h" 00040 #include "libavutil/common.h" 00041 #include "libavutil/mem.h" 00042 #include "libavutil/pixdesc.h" 00043 00044 #define MIN_SIZE 3 00045 #define MAX_SIZE 13 00046 00047 #define CHROMA_WIDTH(link) -((-link->w) >> av_pix_fmt_descriptors[link->format].log2_chroma_w) 00048 #define CHROMA_HEIGHT(link) -((-link->h) >> av_pix_fmt_descriptors[link->format].log2_chroma_h) 00049 00050 typedef struct FilterParam { 00051 int msize_x; 00052 int msize_y; 00053 int amount; 00054 int steps_x; 00055 int steps_y; 00056 int scalebits; 00057 int32_t halfscale; 00058 uint32_t *sc[(MAX_SIZE * MAX_SIZE) - 1]; 00059 } FilterParam; 00060 00061 typedef struct { 00062 FilterParam luma; 00063 FilterParam chroma; 00064 } UnsharpContext; 00065 00066 static void unsharpen(uint8_t *dst, uint8_t *src, int dst_stride, int src_stride, int width, int height, FilterParam *fp) 00067 { 00068 uint32_t **sc = fp->sc; 00069 uint32_t sr[(MAX_SIZE * MAX_SIZE) - 1], tmp1, tmp2; 00070 00071 int32_t res; 00072 int x, y, z; 00073 00074 if (!fp->amount) { 00075 if (dst_stride == src_stride) 00076 memcpy(dst, src, src_stride * height); 00077 else 00078 for (y = 0; y < height; y++, dst += dst_stride, src += src_stride) 00079 memcpy(dst, src, width); 00080 return; 00081 } 00082 00083 for (y = 0; y < 2 * fp->steps_y; y++) 00084 memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * fp->steps_x)); 00085 00086 for (y =- fp->steps_y; y < height + fp->steps_y; y++) { 00087 memset(sr, 0, sizeof(sr[0]) * (2 * fp->steps_x - 1)); 00088 for (x =- fp->steps_x; x < width + fp->steps_x; x++) { 00089 tmp1 = x <= 0 ? src[0] : x >= width ? src[width-1] : src[x]; 00090 for (z = 0; z < fp->steps_x * 2; z += 2) { 00091 tmp2 = sr[z + 0] + tmp1; sr[z + 0] = tmp1; 00092 tmp1 = sr[z + 1] + tmp2; sr[z + 1] = tmp2; 00093 } 00094 for (z = 0; z < fp->steps_y * 2; z += 2) { 00095 tmp2 = sc[z + 0][x + fp->steps_x] + tmp1; sc[z + 0][x + fp->steps_x] = tmp1; 00096 tmp1 = sc[z + 1][x + fp->steps_x] + tmp2; sc[z + 1][x + fp->steps_x] = tmp2; 00097 } 00098 if (x >= fp->steps_x && y >= fp->steps_y) { 00099 uint8_t* srx = src - fp->steps_y * src_stride + x - fp->steps_x; 00100 uint8_t* dsx = dst - fp->steps_y * dst_stride + x - fp->steps_x; 00101 00102 res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + fp->halfscale) >> fp->scalebits)) * fp->amount) >> 16); 00103 *dsx = av_clip_uint8(res); 00104 } 00105 } 00106 if (y >= 0) { 00107 dst += dst_stride; 00108 src += src_stride; 00109 } 00110 } 00111 } 00112 00113 static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double amount) 00114 { 00115 fp->msize_x = msize_x; 00116 fp->msize_y = msize_y; 00117 fp->amount = amount * 65536.0; 00118 00119 fp->steps_x = msize_x / 2; 00120 fp->steps_y = msize_y / 2; 00121 fp->scalebits = (fp->steps_x + fp->steps_y) * 2; 00122 fp->halfscale = 1 << (fp->scalebits - 1); 00123 } 00124 00125 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) 00126 { 00127 UnsharpContext *unsharp = ctx->priv; 00128 int lmsize_x = 5, cmsize_x = 0; 00129 int lmsize_y = 5, cmsize_y = 0; 00130 double lamount = 1.0f, camount = 0.0f; 00131 00132 if (args) 00133 sscanf(args, "%d:%d:%lf:%d:%d:%lf", &lmsize_x, &lmsize_y, &lamount, 00134 &cmsize_x, &cmsize_y, &camount); 00135 00136 set_filter_param(&unsharp->luma, lmsize_x, lmsize_y, lamount); 00137 set_filter_param(&unsharp->chroma, cmsize_x, cmsize_y, camount); 00138 00139 return 0; 00140 } 00141 00142 static int query_formats(AVFilterContext *ctx) 00143 { 00144 enum PixelFormat pix_fmts[] = { 00145 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_YUV410P, 00146 PIX_FMT_YUV411P, PIX_FMT_YUV440P, PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, 00147 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ440P, PIX_FMT_NONE 00148 }; 00149 00150 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); 00151 00152 return 0; 00153 } 00154 00155 static void init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width) 00156 { 00157 int z; 00158 const char *effect; 00159 00160 effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen"; 00161 00162 av_log(ctx, AV_LOG_INFO, "effect:%s type:%s msize_x:%d msize_y:%d amount:%0.2f\n", 00163 effect, effect_type, fp->msize_x, fp->msize_y, fp->amount / 65535.0); 00164 00165 for (z = 0; z < 2 * fp->steps_y; z++) 00166 fp->sc[z] = av_malloc(sizeof(*(fp->sc[z])) * (width + 2 * fp->steps_x)); 00167 } 00168 00169 static int config_props(AVFilterLink *link) 00170 { 00171 UnsharpContext *unsharp = link->dst->priv; 00172 00173 init_filter_param(link->dst, &unsharp->luma, "luma", link->w); 00174 init_filter_param(link->dst, &unsharp->chroma, "chroma", CHROMA_WIDTH(link)); 00175 00176 return 0; 00177 } 00178 00179 static void free_filter_param(FilterParam *fp) 00180 { 00181 int z; 00182 00183 for (z = 0; z < 2 * fp->steps_y; z++) 00184 av_free(fp->sc[z]); 00185 } 00186 00187 static av_cold void uninit(AVFilterContext *ctx) 00188 { 00189 UnsharpContext *unsharp = ctx->priv; 00190 00191 free_filter_param(&unsharp->luma); 00192 free_filter_param(&unsharp->chroma); 00193 } 00194 00195 static void end_frame(AVFilterLink *link) 00196 { 00197 UnsharpContext *unsharp = link->dst->priv; 00198 AVFilterPicRef *in = link->cur_pic; 00199 AVFilterPicRef *out = link->dst->outputs[0]->outpic; 00200 00201 unsharpen(out->data[0], in->data[0], out->linesize[0], in->linesize[0], link->w, link->h, &unsharp->luma); 00202 unsharpen(out->data[1], in->data[1], out->linesize[1], in->linesize[1], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), &unsharp->chroma); 00203 unsharpen(out->data[2], in->data[2], out->linesize[2], in->linesize[2], CHROMA_WIDTH(link), CHROMA_HEIGHT(link), &unsharp->chroma); 00204 00205 avfilter_unref_pic(in); 00206 avfilter_draw_slice(link->dst->outputs[0], 0, link->h, 1); 00207 avfilter_end_frame(link->dst->outputs[0]); 00208 avfilter_unref_pic(out); 00209 } 00210 00211 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir) 00212 { 00213 } 00214 00215 AVFilter avfilter_vf_unsharp = { 00216 .name = "unsharp", 00217 .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."), 00218 00219 .priv_size = sizeof(UnsharpContext), 00220 00221 .init = init, 00222 .uninit = uninit, 00223 .query_formats = query_formats, 00224 00225 .inputs = (AVFilterPad[]) {{ .name = "default", 00226 .type = AVMEDIA_TYPE_VIDEO, 00227 .draw_slice = draw_slice, 00228 .end_frame = end_frame, 00229 .config_props = config_props, 00230 .min_perms = AV_PERM_READ, }, 00231 { .name = NULL}}, 00232 00233 .outputs = (AVFilterPad[]) {{ .name = "default", 00234 .type = AVMEDIA_TYPE_VIDEO, }, 00235 { .name = NULL}}, 00236 };