Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org> 00003 * Copyright (c) 2010 Baptiste Coudurier 00004 * 00005 * This file is part of Libav, ported from MPlayer. 00006 * 00007 * Libav is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License along 00018 * with Libav; if not, write to the Free Software Foundation, Inc., 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00020 */ 00021 00028 #include "libavutil/pixdesc.h" 00029 #include "avfilter.h" 00030 00031 typedef struct { 00032 int Coefs[4][512*16]; 00033 unsigned int *Line; 00034 unsigned short *Frame[3]; 00035 int hsub, vsub; 00036 } HQDN3DContext; 00037 00038 static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef) 00039 { 00040 // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF); 00041 int dMul= PrevMul-CurrMul; 00042 unsigned int d=((dMul+0x10007FF)>>12); 00043 return CurrMul + Coef[d]; 00044 } 00045 00046 static void deNoiseTemporal(unsigned char *FrameSrc, 00047 unsigned char *FrameDest, 00048 unsigned short *FrameAnt, 00049 int W, int H, int sStride, int dStride, 00050 int *Temporal) 00051 { 00052 long X, Y; 00053 unsigned int PixelDst; 00054 00055 for (Y = 0; Y < H; Y++) { 00056 for (X = 0; X < W; X++) { 00057 PixelDst = LowPassMul(FrameAnt[X]<<8, FrameSrc[X]<<16, Temporal); 00058 FrameAnt[X] = ((PixelDst+0x1000007F)>>8); 00059 FrameDest[X]= ((PixelDst+0x10007FFF)>>16); 00060 } 00061 FrameSrc += sStride; 00062 FrameDest += dStride; 00063 FrameAnt += W; 00064 } 00065 } 00066 00067 static void deNoiseSpacial(unsigned char *Frame, 00068 unsigned char *FrameDest, 00069 unsigned int *LineAnt, 00070 int W, int H, int sStride, int dStride, 00071 int *Horizontal, int *Vertical) 00072 { 00073 long X, Y; 00074 long sLineOffs = 0, dLineOffs = 0; 00075 unsigned int PixelAnt; 00076 unsigned int PixelDst; 00077 00078 /* First pixel has no left nor top neighbor. */ 00079 PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16; 00080 FrameDest[0]= ((PixelDst+0x10007FFF)>>16); 00081 00082 /* First line has no top neighbor, only left. */ 00083 for (X = 1; X < W; X++) { 00084 PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal); 00085 FrameDest[X]= ((PixelDst+0x10007FFF)>>16); 00086 } 00087 00088 for (Y = 1; Y < H; Y++) { 00089 unsigned int PixelAnt; 00090 sLineOffs += sStride, dLineOffs += dStride; 00091 /* First pixel on each line doesn't have previous pixel */ 00092 PixelAnt = Frame[sLineOffs]<<16; 00093 PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical); 00094 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16); 00095 00096 for (X = 1; X < W; X++) { 00097 unsigned int PixelDst; 00098 /* The rest are normal */ 00099 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal); 00100 PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical); 00101 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16); 00102 } 00103 } 00104 } 00105 00106 static void deNoise(unsigned char *Frame, 00107 unsigned char *FrameDest, 00108 unsigned int *LineAnt, 00109 unsigned short **FrameAntPtr, 00110 int W, int H, int sStride, int dStride, 00111 int *Horizontal, int *Vertical, int *Temporal) 00112 { 00113 long X, Y; 00114 long sLineOffs = 0, dLineOffs = 0; 00115 unsigned int PixelAnt; 00116 unsigned int PixelDst; 00117 unsigned short* FrameAnt=(*FrameAntPtr); 00118 00119 if (!FrameAnt) { 00120 (*FrameAntPtr) = FrameAnt = av_malloc(W*H*sizeof(unsigned short)); 00121 for (Y = 0; Y < H; Y++) { 00122 unsigned short* dst=&FrameAnt[Y*W]; 00123 unsigned char* src=Frame+Y*sStride; 00124 for (X = 0; X < W; X++) dst[X]=src[X]<<8; 00125 } 00126 } 00127 00128 if (!Horizontal[0] && !Vertical[0]) { 00129 deNoiseTemporal(Frame, FrameDest, FrameAnt, 00130 W, H, sStride, dStride, Temporal); 00131 return; 00132 } 00133 if (!Temporal[0]) { 00134 deNoiseSpacial(Frame, FrameDest, LineAnt, 00135 W, H, sStride, dStride, Horizontal, Vertical); 00136 return; 00137 } 00138 00139 /* First pixel has no left nor top neighbor. Only previous frame */ 00140 LineAnt[0] = PixelAnt = Frame[0]<<16; 00141 PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal); 00142 FrameAnt[0] = ((PixelDst+0x1000007F)>>8); 00143 FrameDest[0]= ((PixelDst+0x10007FFF)>>16); 00144 00145 /* First line has no top neighbor. Only left one for each pixel and 00146 * last frame */ 00147 for (X = 1; X < W; X++) { 00148 LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal); 00149 PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal); 00150 FrameAnt[X] = ((PixelDst+0x1000007F)>>8); 00151 FrameDest[X]= ((PixelDst+0x10007FFF)>>16); 00152 } 00153 00154 for (Y = 1; Y < H; Y++) { 00155 unsigned int PixelAnt; 00156 unsigned short* LinePrev=&FrameAnt[Y*W]; 00157 sLineOffs += sStride, dLineOffs += dStride; 00158 /* First pixel on each line doesn't have previous pixel */ 00159 PixelAnt = Frame[sLineOffs]<<16; 00160 LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical); 00161 PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal); 00162 LinePrev[0] = ((PixelDst+0x1000007F)>>8); 00163 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16); 00164 00165 for (X = 1; X < W; X++) { 00166 unsigned int PixelDst; 00167 /* The rest are normal */ 00168 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal); 00169 LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical); 00170 PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal); 00171 LinePrev[X] = ((PixelDst+0x1000007F)>>8); 00172 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16); 00173 } 00174 } 00175 } 00176 00177 static void PrecalcCoefs(int *Ct, double Dist25) 00178 { 00179 int i; 00180 double Gamma, Simil, C; 00181 00182 Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001); 00183 00184 for (i = -255*16; i <= 255*16; i++) { 00185 Simil = 1.0 - FFABS(i) / (16*255.0); 00186 C = pow(Simil, Gamma) * 65536.0 * i / 16.0; 00187 Ct[16*256+i] = lrint(C); 00188 } 00189 00190 Ct[0] = !!Dist25; 00191 } 00192 00193 #define PARAM1_DEFAULT 4.0 00194 #define PARAM2_DEFAULT 3.0 00195 #define PARAM3_DEFAULT 6.0 00196 00197 static int init(AVFilterContext *ctx, const char *args, void *opaque) 00198 { 00199 HQDN3DContext *hqdn3d = ctx->priv; 00200 double LumSpac, LumTmp, ChromSpac, ChromTmp; 00201 double Param1, Param2, Param3, Param4; 00202 00203 LumSpac = PARAM1_DEFAULT; 00204 ChromSpac = PARAM2_DEFAULT; 00205 LumTmp = PARAM3_DEFAULT; 00206 ChromTmp = LumTmp * ChromSpac / LumSpac; 00207 00208 if (args) { 00209 switch (sscanf(args, "%lf:%lf:%lf:%lf", 00210 &Param1, &Param2, &Param3, &Param4)) { 00211 case 1: 00212 LumSpac = Param1; 00213 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT; 00214 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT; 00215 ChromTmp = LumTmp * ChromSpac / LumSpac; 00216 break; 00217 case 2: 00218 LumSpac = Param1; 00219 ChromSpac = Param2; 00220 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT; 00221 ChromTmp = LumTmp * ChromSpac / LumSpac; 00222 break; 00223 case 3: 00224 LumSpac = Param1; 00225 ChromSpac = Param2; 00226 LumTmp = Param3; 00227 ChromTmp = LumTmp * ChromSpac / LumSpac; 00228 break; 00229 case 4: 00230 LumSpac = Param1; 00231 ChromSpac = Param2; 00232 LumTmp = Param3; 00233 ChromTmp = Param4; 00234 break; 00235 } 00236 } 00237 00238 av_log(ctx, AV_LOG_INFO, "ls:%lf cs:%lf lt:%lf ct:%lf\n", 00239 LumSpac, ChromSpac, LumTmp, ChromTmp); 00240 if (LumSpac < 0 || ChromSpac < 0 || isnan(ChromTmp)) { 00241 av_log(ctx, AV_LOG_ERROR, 00242 "Invalid negative value for luma or chroma spatial strength, " 00243 "or resulting value for chroma temporal strength is nan.\n"); 00244 return AVERROR(EINVAL); 00245 } 00246 00247 PrecalcCoefs(hqdn3d->Coefs[0], LumSpac); 00248 PrecalcCoefs(hqdn3d->Coefs[1], LumTmp); 00249 PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac); 00250 PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp); 00251 00252 return 0; 00253 } 00254 00255 static void uninit(AVFilterContext *ctx) 00256 { 00257 HQDN3DContext *hqdn3d = ctx->priv; 00258 00259 av_freep(&hqdn3d->Line); 00260 av_freep(&hqdn3d->Frame[0]); 00261 av_freep(&hqdn3d->Frame[1]); 00262 av_freep(&hqdn3d->Frame[2]); 00263 } 00264 00265 static int query_formats(AVFilterContext *ctx) 00266 { 00267 static const enum PixelFormat pix_fmts[] = { 00268 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE 00269 }; 00270 00271 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); 00272 00273 return 0; 00274 } 00275 00276 static int config_input(AVFilterLink *inlink) 00277 { 00278 HQDN3DContext *hqdn3d = inlink->dst->priv; 00279 00280 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w; 00281 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h; 00282 00283 hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line)); 00284 if (!hqdn3d->Line) 00285 return AVERROR(ENOMEM); 00286 00287 return 0; 00288 } 00289 00290 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { } 00291 00292 static void end_frame(AVFilterLink *inlink) 00293 { 00294 HQDN3DContext *hqdn3d = inlink->dst->priv; 00295 AVFilterLink *outlink = inlink->dst->outputs[0]; 00296 AVFilterBufferRef *inpic = inlink ->cur_buf; 00297 AVFilterBufferRef *outpic = outlink->out_buf; 00298 int cw = inpic->video->w >> hqdn3d->hsub; 00299 int ch = inpic->video->h >> hqdn3d->vsub; 00300 00301 deNoise(inpic->data[0], outpic->data[0], 00302 hqdn3d->Line, &hqdn3d->Frame[0], inpic->video->w, inpic->video->h, 00303 inpic->linesize[0], outpic->linesize[0], 00304 hqdn3d->Coefs[0], 00305 hqdn3d->Coefs[0], 00306 hqdn3d->Coefs[1]); 00307 deNoise(inpic->data[1], outpic->data[1], 00308 hqdn3d->Line, &hqdn3d->Frame[1], cw, ch, 00309 inpic->linesize[1], outpic->linesize[1], 00310 hqdn3d->Coefs[2], 00311 hqdn3d->Coefs[2], 00312 hqdn3d->Coefs[3]); 00313 deNoise(inpic->data[2], outpic->data[2], 00314 hqdn3d->Line, &hqdn3d->Frame[2], cw, ch, 00315 inpic->linesize[2], outpic->linesize[2], 00316 hqdn3d->Coefs[2], 00317 hqdn3d->Coefs[2], 00318 hqdn3d->Coefs[3]); 00319 00320 avfilter_draw_slice(outlink, 0, inpic->video->h, 1); 00321 avfilter_end_frame(outlink); 00322 avfilter_unref_buffer(inpic); 00323 avfilter_unref_buffer(outpic); 00324 } 00325 00326 AVFilter avfilter_vf_hqdn3d = { 00327 .name = "hqdn3d", 00328 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."), 00329 00330 .priv_size = sizeof(HQDN3DContext), 00331 .init = init, 00332 .uninit = uninit, 00333 .query_formats = query_formats, 00334 00335 .inputs = (AVFilterPad[]) {{ .name = "default", 00336 .type = AVMEDIA_TYPE_VIDEO, 00337 .draw_slice = null_draw_slice, 00338 .config_props = config_input, 00339 .end_frame = end_frame }, 00340 { .name = NULL}}, 00341 00342 .outputs = (AVFilterPad[]) {{ .name = "default", 00343 .type = AVMEDIA_TYPE_VIDEO }, 00344 { .name = NULL}}, 00345 };