📄 timgfilterdenoise3d.cpp
字号:
/*
* Copyright (c) 2003-2006 Milan Cutka
* based on mplayer denoise3d and hqdn3d filters (C) 2003 Daniel Moreno <comac@comac.darktech.org>
* by hqdn3d deNoiseTemporal and deNoiseSpacial by Loren Merritt
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "stdafx.h"
#include "TimgFilterDenoise3d.h"
#include "TblurSettings.h"
#include "Tconfig.h"
#include "simd.h"
//===================================== TimgFilterDenoise3d =====================================
TimgFilterDenoise3d::TimgFilterDenoise3d(IffdshowBase *Ideci,Tfilters *Iparent):TimgFilter(Ideci,Iparent)
{
oldLuma=oldChroma=oldTime=-2;
}
void TimgFilterDenoise3d::onSizeChange(void)
{
done();
}
bool TimgFilterDenoise3d::is(const TffPictBase &pict,const TfilterSettingsVideo *cfg0)
{
const TblurSettings *cfg=(const TblurSettings*)cfg0;
return super::is(pict,cfg) && cfg->isDenoise3d && (cfg->denoise3Dluma || cfg->denoise3Dchroma || cfg->denoise3Dtime);
}
HRESULT TimgFilterDenoise3d::process(TfilterQueue::iterator it,TffPict &pict,const TfilterSettingsVideo *cfg0)
{
if (is(pict,cfg0))
{
const TblurSettings *cfg=(const TblurSettings*)cfg0;
init(pict,cfg->full,cfg->half);
if (oldLuma!=cfg->denoise3Dluma || oldChroma!=cfg->denoise3Dchroma || oldTime!=cfg->denoise3Dtime)
{
oldLuma=cfg->denoise3Dluma;oldChroma=cfg->denoise3Dchroma;oldTime=cfg->denoise3Dtime;
double LumSpac=oldLuma/100.0;
double LumTmp=oldTime/100.0;
double ChromSpac=oldChroma/100.0;
double ChromTmp=LumTmp*ChromSpac/LumSpac;
precalcCoefs(0,LumSpac);
precalcCoefs(1,LumTmp);
precalcCoefs(2,ChromSpac);
precalcCoefs(3,ChromTmp);
}
bool cspChanged=false;
const unsigned char *src[4];
cspChanged|=getCur(FF_CSPS_MASK_YUV_PLANAR,pict,cfg->full,src);
unsigned char *dst[4];
cspChanged|=getNext(csp1,pict,cfg->full,dst);
if (cspChanged) done();
deNoise(pict,dx1[0],dy1[0],src,stride1,dst,stride2);
}
return parent->deliverSample(++it,pict);
}
void TimgFilterDenoise3d::onSeek(void)
{
done();
}
//================================== TimgFilterDenoise3dNormal =================================
TimgFilterDenoise3dNormal::TimgFilterDenoise3dNormal(IffdshowBase *Ideci,Tfilters *Iparent):TimgFilterDenoise3d(Ideci,Iparent)
{
Line=NULL;
Frame[0]=Frame[1]=Frame[2]=NULL;
}
void TimgFilterDenoise3dNormal::done(void)
{
if (Line) aligned_free(Line);Line=NULL;
if (Frame[0]) aligned_free(Frame[0]);Frame[0]=NULL;
if (Frame[1]) aligned_free(Frame[1]);Frame[1]=NULL;
if (Frame[2]) aligned_free(Frame[2]);Frame[2]=NULL;
}
void TimgFilterDenoise3dNormal::precalcCoefs(int nCt, double Dist25)
{
int *Ct=Coefs[nCt];
int i;
double Gamma = log(0.25) / log(1.0 - Dist25/255.0);
for (i = -256; i <= 255; i++)
{
double Simil = 1.0 - abs(i) / 255.0;
double C = pow(Simil, Gamma) * (double)i;
Ct[256+i] = int((C<0) ? (C-0.5) : (C+0.5));
}
}
void TimgFilterDenoise3dNormal::deNoise(const unsigned char *Frame,
const unsigned char *FramePrev,
unsigned char *FrameDest,
unsigned char *LineAnt,
int W, int H, stride_t sStride, stride_t pStride, stride_t dStride,
int *Horizontal, int *Vertical, int *Temporal)
{
stride_t sLineOffs = 0, pLineOffs = 0, dLineOffs = 0;
unsigned char PixelAnt;
/* First pixel has no left nor top neightbour. Only previous frame */
LineAnt[0] = PixelAnt = Frame[0];
FrameDest[0] = LowPass(FramePrev[0], LineAnt[0], Temporal);
// Fist line has no top neightbour. Only left one for each pixel and last frame
for (int X = 1; X < W; X++)
{
PixelAnt = LowPass(PixelAnt, Frame[X], Horizontal);
LineAnt[X] = PixelAnt;
FrameDest[X] = LowPass(FramePrev[X], LineAnt[X], Temporal);
}
for (int Y = 1; Y < H; Y++)
{
sLineOffs += sStride, pLineOffs += pStride, dLineOffs += dStride;
// First pixel on each line doesn't have previous pixel
PixelAnt = Frame[sLineOffs];
LineAnt[0] = LowPass(LineAnt[0], PixelAnt, Vertical);
FrameDest[dLineOffs] = LowPass(FramePrev[pLineOffs], LineAnt[0], Temporal);
for (int X = 1; X < W; X++)
{
// The rest are normal
PixelAnt = LowPass(PixelAnt, Frame[sLineOffs+X], Horizontal);
LineAnt[X] = LowPass(LineAnt[X], PixelAnt, Vertical);
FrameDest[dLineOffs+X] = LowPass(FramePrev[pLineOffs+X], LineAnt[X], Temporal);
}
}
}
void TimgFilterDenoise3dNormal::deNoise(TffPict &pict,unsigned int dx,unsigned int dy,const unsigned char *src[4],stride_t stride1[4],unsigned char *dst[4],stride_t stride2[4])
{
if (!Line) Line=(unsigned char*)aligned_malloc(dx);
if (!Frame[0])
for (unsigned int i=0;i<3;i++)
{
FrameStride[i]=((dx>>pict.cspInfo.shiftX[i])/16+2)*16;
Frame[i]=(unsigned char*)aligned_malloc(FrameStride[i]*(dy>>pict.cspInfo.shiftY[i]));
}
static const int coeffIdxs[3]={0,2,2};
for (unsigned int i=0;i<3;i++)
{
deNoise(src[i],Frame[i],dst[i],
Line,dx>>pict.cspInfo.shiftX[i],dy>>pict.cspInfo.shiftY[i],
stride1[i],FrameStride[i],stride2[i],
Coefs[coeffIdxs[i] ] + 256,
Coefs[coeffIdxs[i] ] + 256,
Coefs[coeffIdxs[i]+1] + 256);
TffPict::copy(Frame[i],FrameStride[i],dst[i],stride2[i],pict.cspInfo.Bpp*dx>>pict.cspInfo.shiftX[i],dy>>pict.cspInfo.shiftY[i]);
}
}
//================================== TimgFilterDenoise3dHQ ===================================
TimgFilterDenoise3dHQ::TimgFilterDenoise3dHQ(IffdshowBase *Ideci,Tfilters *Iparent):TimgFilterDenoise3d(Ideci,Iparent)
{
Line=NULL;
Frame[0]=Frame[1]=Frame[2]=NULL;
}
void TimgFilterDenoise3dHQ::done(void)
{
if (Line) aligned_free(Line);Line=NULL;
if (Frame[0]) aligned_free(Frame[0]);Frame[0]=NULL;
if (Frame[1]) aligned_free(Frame[1]);Frame[1]=NULL;
if (Frame[2]) aligned_free(Frame[2]);Frame[2]=NULL;
}
void TimgFilterDenoise3dHQ::precalcCoefs(int nCt, double Dist25)
{
int *Ct=Coefs[nCt];
int i;
double Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
for (i = -255*16; i <= 255*16; i++)
{
double Simil = 1.0 - abs(i) / (16*255.0);
double C = pow(Simil, Gamma) * 65536.0 * (double)i / 16.0;
Ct[16*256+i] = int((C<0) ? (C-0.5) : (C+0.5));
}
Ct[0] = (Dist25 != 0);
}
void TimgFilterDenoise3dHQ::deNoiseTemporal(
const unsigned char *Frame, // mpi->planes[x]
unsigned char *FrameDest, // dmpi->planes[x]
unsigned short *FrameAnt,
int W, int H, stride_t sStride, stride_t dStride,
int *Temporal)
{
int X, Y;
unsigned int PixelDst;
for (Y = 0; Y < H; Y++){
for (X = 0; X < W; X++){
PixelDst = LowPassMul(FrameAnt[X]<<8, Frame[X]<<16, Temporal);
FrameAnt[X] = (unsigned short)((PixelDst+0x1000007F)>>8);
FrameDest[X]= (unsigned char)((PixelDst+0x10007FFF)>>16);
}
Frame += sStride;
FrameDest += dStride;
FrameAnt += W;
}
}
void TimgFilterDenoise3dHQ::deNoiseSpacial(
const unsigned char *Frame, // mpi->planes[x]
unsigned char *FrameDest, // dmpi->planes[x]
unsigned int *LineAnt, // vf->priv->Line (width bytes)
int W, int H, stride_t sStride, stride_t dStride,
int *Horizontal, int *Vertical)
{
int X, Y;
stride_t sLineOffs = 0, dLineOffs = 0;
unsigned int PixelAnt;
unsigned int PixelDst;
/* First pixel has no left nor top neightbour. */
PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
FrameDest[0]= (unsigned char)((PixelDst+0x10007FFF)>>16);
/* Fist line has no top neightbour, only left. */
for (X = 1; X < W; X++){
PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
FrameDest[X]= (unsigned char)((PixelDst+0x10007FFF)>>16);
}
for (Y = 1; Y < H; Y++){
unsigned int PixelAnt;
sLineOffs += sStride, dLineOffs += dStride;
/* First pixel on each line doesn't have previous pixel */
PixelAnt = Frame[sLineOffs]<<16;
PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
FrameDest[dLineOffs]= (unsigned char)((PixelDst+0x10007FFF)>>16);
for (X = 1; X < W; X++){
int PixelDst;
/* The rest are normal */
PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
FrameDest[dLineOffs+X]= (unsigned char)((PixelDst+0x10007FFF)>>16);
}
}
}
void TimgFilterDenoise3dHQ::deNoise(const unsigned char *Frame, // mpi->planes[x]
unsigned char *FrameDest, // dmpi->planes[x]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -