• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavfilter/vf_unsharp.c

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