00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00029 #include "libavutil/common.h"
00030 #include "libavutil/pixdesc.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "avfilter.h"
00033 #include "formats.h"
00034 #include "internal.h"
00035 #include "video.h"
00036
00037 typedef struct {
00038 int16_t *coefs[4];
00039 uint16_t *line;
00040 uint16_t *frame_prev[3];
00041 double strength[4];
00042 int hsub, vsub;
00043 int depth;
00044 void (*denoise_row[17])(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
00045 } HQDN3DContext;
00046
00047 void ff_hqdn3d_row_8_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
00048 void ff_hqdn3d_row_9_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
00049 void ff_hqdn3d_row_10_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
00050 void ff_hqdn3d_row_16_x86(uint8_t *src, uint8_t *dst, uint16_t *line_ant, uint16_t *frame_ant, ptrdiff_t w, int16_t *spatial, int16_t *temporal);
00051
00052 #define LUT_BITS (depth==16 ? 8 : 4)
00053 #define LOAD(x) (((depth==8 ? src[x] : AV_RN16A(src+(x)*2)) << (16-depth)) + (((1<<(16-depth))-1)>>1))
00054 #define STORE(x,val) (depth==8 ? dst[x] = (val) >> (16-depth)\
00055 : AV_WN16A(dst+(x)*2, (val) >> (16-depth)))
00056
00057 av_always_inline
00058 static uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
00059 {
00060 int d = (prev - cur) >> (8 - LUT_BITS);
00061 return cur + coef[d];
00062 }
00063
00064 av_always_inline
00065 static void denoise_temporal(uint8_t *src, uint8_t *dst,
00066 uint16_t *frame_ant,
00067 int w, int h, int sstride, int dstride,
00068 int16_t *temporal, int depth)
00069 {
00070 long x, y;
00071 uint32_t tmp;
00072
00073 temporal += 256 << LUT_BITS;
00074
00075 for (y = 0; y < h; y++) {
00076 for (x = 0; x < w; x++) {
00077 frame_ant[x] = tmp = lowpass(frame_ant[x], LOAD(x), temporal, depth);
00078 STORE(x, tmp);
00079 }
00080 src += sstride;
00081 dst += dstride;
00082 frame_ant += w;
00083 }
00084 }
00085
00086 av_always_inline
00087 static void denoise_spatial(HQDN3DContext *hqdn3d,
00088 uint8_t *src, uint8_t *dst,
00089 uint16_t *line_ant, uint16_t *frame_ant,
00090 int w, int h, int sstride, int dstride,
00091 int16_t *spatial, int16_t *temporal, int depth)
00092 {
00093 long x, y;
00094 uint32_t pixel_ant;
00095 uint32_t tmp;
00096
00097 spatial += 256 << LUT_BITS;
00098 temporal += 256 << LUT_BITS;
00099
00100
00101
00102 pixel_ant = LOAD(0);
00103 for (x = 0; x < w; x++) {
00104 line_ant[x] = tmp = pixel_ant = lowpass(pixel_ant, LOAD(x), spatial, depth);
00105 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
00106 STORE(x, tmp);
00107 }
00108
00109 for (y = 1; y < h; y++) {
00110 src += sstride;
00111 dst += dstride;
00112 frame_ant += w;
00113 if (hqdn3d->denoise_row[depth]) {
00114 hqdn3d->denoise_row[depth](src, dst, line_ant, frame_ant, w, spatial, temporal);
00115 continue;
00116 }
00117 pixel_ant = LOAD(0);
00118 for (x = 0; x < w-1; x++) {
00119 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
00120 pixel_ant = lowpass(pixel_ant, LOAD(x+1), spatial, depth);
00121 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
00122 STORE(x, tmp);
00123 }
00124 line_ant[x] = tmp = lowpass(line_ant[x], pixel_ant, spatial, depth);
00125 frame_ant[x] = tmp = lowpass(frame_ant[x], tmp, temporal, depth);
00126 STORE(x, tmp);
00127 }
00128 }
00129
00130 av_always_inline
00131 static void denoise_depth(HQDN3DContext *hqdn3d,
00132 uint8_t *src, uint8_t *dst,
00133 uint16_t *line_ant, uint16_t **frame_ant_ptr,
00134 int w, int h, int sstride, int dstride,
00135 int16_t *spatial, int16_t *temporal, int depth)
00136 {
00137
00138
00139 long x, y;
00140 uint16_t *frame_ant = *frame_ant_ptr;
00141 if (!frame_ant) {
00142 uint8_t *frame_src = src;
00143 *frame_ant_ptr = frame_ant = av_malloc(w*h*sizeof(uint16_t));
00144 for (y = 0; y < h; y++, src += sstride, frame_ant += w)
00145 for (x = 0; x < w; x++)
00146 frame_ant[x] = LOAD(x);
00147 src = frame_src;
00148 frame_ant = *frame_ant_ptr;
00149 }
00150
00151 if (spatial[0])
00152 denoise_spatial(hqdn3d, src, dst, line_ant, frame_ant,
00153 w, h, sstride, dstride, spatial, temporal, depth);
00154 else
00155 denoise_temporal(src, dst, frame_ant,
00156 w, h, sstride, dstride, temporal, depth);
00157 }
00158
00159 #define denoise(...) \
00160 switch (hqdn3d->depth) {\
00161 case 8: denoise_depth(__VA_ARGS__, 8); break;\
00162 case 9: denoise_depth(__VA_ARGS__, 9); break;\
00163 case 10: denoise_depth(__VA_ARGS__, 10); break;\
00164 case 16: denoise_depth(__VA_ARGS__, 16); break;\
00165 }
00166
00167 static int16_t *precalc_coefs(double dist25, int depth)
00168 {
00169 int i;
00170 double gamma, simil, C;
00171 int16_t *ct = av_malloc((512<<LUT_BITS)*sizeof(int16_t));
00172 if (!ct)
00173 return NULL;
00174
00175 gamma = log(0.25) / log(1.0 - FFMIN(dist25,252.0)/255.0 - 0.00001);
00176
00177 for (i = -255<<LUT_BITS; i <= 255<<LUT_BITS; i++) {
00178 double f = ((i<<(9-LUT_BITS)) + (1<<(8-LUT_BITS)) - 1) / 512.0;
00179 simil = 1.0 - FFABS(f) / 255.0;
00180 C = pow(simil, gamma) * 256.0 * f;
00181 ct[(256<<LUT_BITS)+i] = lrint(C);
00182 }
00183
00184 ct[0] = !!dist25;
00185 return ct;
00186 }
00187
00188 #define PARAM1_DEFAULT 4.0
00189 #define PARAM2_DEFAULT 3.0
00190 #define PARAM3_DEFAULT 6.0
00191
00192 static int init(AVFilterContext *ctx, const char *args)
00193 {
00194 HQDN3DContext *hqdn3d = ctx->priv;
00195 double lum_spac, lum_tmp, chrom_spac, chrom_tmp;
00196 double param1, param2, param3, param4;
00197
00198 lum_spac = PARAM1_DEFAULT;
00199 chrom_spac = PARAM2_DEFAULT;
00200 lum_tmp = PARAM3_DEFAULT;
00201 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
00202
00203 if (args) {
00204 switch (sscanf(args, "%lf:%lf:%lf:%lf",
00205 ¶m1, ¶m2, ¶m3, ¶m4)) {
00206 case 1:
00207 lum_spac = param1;
00208 chrom_spac = PARAM2_DEFAULT * param1 / PARAM1_DEFAULT;
00209 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
00210 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
00211 break;
00212 case 2:
00213 lum_spac = param1;
00214 chrom_spac = param2;
00215 lum_tmp = PARAM3_DEFAULT * param1 / PARAM1_DEFAULT;
00216 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
00217 break;
00218 case 3:
00219 lum_spac = param1;
00220 chrom_spac = param2;
00221 lum_tmp = param3;
00222 chrom_tmp = lum_tmp * chrom_spac / lum_spac;
00223 break;
00224 case 4:
00225 lum_spac = param1;
00226 chrom_spac = param2;
00227 lum_tmp = param3;
00228 chrom_tmp = param4;
00229 break;
00230 }
00231 }
00232
00233 hqdn3d->strength[0] = lum_spac;
00234 hqdn3d->strength[1] = lum_tmp;
00235 hqdn3d->strength[2] = chrom_spac;
00236 hqdn3d->strength[3] = chrom_tmp;
00237
00238 av_log(ctx, AV_LOG_VERBOSE, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
00239 lum_spac, chrom_spac, lum_tmp, chrom_tmp);
00240 if (lum_spac < 0 || chrom_spac < 0 || isnan(chrom_tmp)) {
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 return 0;
00248 }
00249
00250 static void uninit(AVFilterContext *ctx)
00251 {
00252 HQDN3DContext *hqdn3d = ctx->priv;
00253
00254 av_freep(&hqdn3d->coefs[0]);
00255 av_freep(&hqdn3d->coefs[1]);
00256 av_freep(&hqdn3d->coefs[2]);
00257 av_freep(&hqdn3d->coefs[3]);
00258 av_freep(&hqdn3d->line);
00259 av_freep(&hqdn3d->frame_prev[0]);
00260 av_freep(&hqdn3d->frame_prev[1]);
00261 av_freep(&hqdn3d->frame_prev[2]);
00262 }
00263
00264 static int query_formats(AVFilterContext *ctx)
00265 {
00266 static const enum PixelFormat pix_fmts[] = {
00267 PIX_FMT_YUV420P,
00268 PIX_FMT_YUV422P,
00269 PIX_FMT_YUV444P,
00270 PIX_FMT_YUV410P,
00271 PIX_FMT_YUV411P,
00272 PIX_FMT_YUV440P,
00273 PIX_FMT_YUVJ420P,
00274 PIX_FMT_YUVJ422P,
00275 PIX_FMT_YUVJ444P,
00276 PIX_FMT_YUVJ440P,
00277 AV_NE( PIX_FMT_YUV420P9BE, PIX_FMT_YUV420P9LE ),
00278 AV_NE( PIX_FMT_YUV422P9BE, PIX_FMT_YUV422P9LE ),
00279 AV_NE( PIX_FMT_YUV444P9BE, PIX_FMT_YUV444P9LE ),
00280 AV_NE( PIX_FMT_YUV420P10BE, PIX_FMT_YUV420P10LE ),
00281 AV_NE( PIX_FMT_YUV422P10BE, PIX_FMT_YUV422P10LE ),
00282 AV_NE( PIX_FMT_YUV444P10BE, PIX_FMT_YUV444P10LE ),
00283 AV_NE( PIX_FMT_YUV420P16BE, PIX_FMT_YUV420P16LE ),
00284 AV_NE( PIX_FMT_YUV422P16BE, PIX_FMT_YUV422P16LE ),
00285 AV_NE( PIX_FMT_YUV444P16BE, PIX_FMT_YUV444P16LE ),
00286 PIX_FMT_NONE
00287 };
00288
00289 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00290
00291 return 0;
00292 }
00293
00294 static int config_input(AVFilterLink *inlink)
00295 {
00296 HQDN3DContext *hqdn3d = inlink->dst->priv;
00297 int i;
00298
00299 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
00300 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
00301 hqdn3d->depth = av_pix_fmt_descriptors[inlink->format].comp[0].depth_minus1+1;
00302
00303 hqdn3d->line = av_malloc(inlink->w * sizeof(*hqdn3d->line));
00304 if (!hqdn3d->line)
00305 return AVERROR(ENOMEM);
00306
00307 for (i = 0; i < 4; i++) {
00308 hqdn3d->coefs[i] = precalc_coefs(hqdn3d->strength[i], hqdn3d->depth);
00309 if (!hqdn3d->coefs[i])
00310 return AVERROR(ENOMEM);
00311 }
00312
00313 #if HAVE_YASM
00314 hqdn3d->denoise_row[ 8] = ff_hqdn3d_row_8_x86;
00315 hqdn3d->denoise_row[ 9] = ff_hqdn3d_row_9_x86;
00316 hqdn3d->denoise_row[10] = ff_hqdn3d_row_10_x86;
00317 hqdn3d->denoise_row[16] = ff_hqdn3d_row_16_x86;
00318 #endif
00319
00320 return 0;
00321 }
00322
00323 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00324 {
00325 return 0;
00326 }
00327
00328 static int end_frame(AVFilterLink *inlink)
00329 {
00330 HQDN3DContext *hqdn3d = inlink->dst->priv;
00331 AVFilterLink *outlink = inlink->dst->outputs[0];
00332 AVFilterBufferRef *inpic = inlink ->cur_buf;
00333 AVFilterBufferRef *outpic = outlink->out_buf;
00334 int ret, c;
00335
00336 for (c = 0; c < 3; c++) {
00337 denoise(hqdn3d, inpic->data[c], outpic->data[c],
00338 hqdn3d->line, &hqdn3d->frame_prev[c],
00339 inpic->video->w >> (!!c * hqdn3d->hsub),
00340 inpic->video->h >> (!!c * hqdn3d->vsub),
00341 inpic->linesize[c], outpic->linesize[c],
00342 hqdn3d->coefs[c?2:0], hqdn3d->coefs[c?3:1]);
00343 }
00344
00345 if ((ret = ff_draw_slice(outlink, 0, inpic->video->h, 1)) < 0 ||
00346 (ret = ff_end_frame(outlink)) < 0)
00347 return ret;
00348 return 0;
00349 }
00350
00351 AVFilter avfilter_vf_hqdn3d = {
00352 .name = "hqdn3d",
00353 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
00354
00355 .priv_size = sizeof(HQDN3DContext),
00356 .init = init,
00357 .uninit = uninit,
00358 .query_formats = query_formats,
00359
00360 .inputs = (const AVFilterPad[]) {{ .name = "default",
00361 .type = AVMEDIA_TYPE_VIDEO,
00362 .start_frame = ff_inplace_start_frame,
00363 .draw_slice = null_draw_slice,
00364 .config_props = config_input,
00365 .end_frame = end_frame },
00366 { .name = NULL}},
00367
00368 .outputs = (const AVFilterPad[]) {{ .name = "default",
00369 .type = AVMEDIA_TYPE_VIDEO },
00370 { .name = NULL}},
00371 };