66#include <json-glib/json-glib.h>
84#define DT_RAWDENOISEAI_MODEL_LEN 128
120#define DT_RAWDENOISEAI_NUM_VERSIONS 1
121#define DT_RAWDENOISEAI_NUM_SIZES 3
122#define DT_RAWDENOISEAI_NUM_SCALES 2
235 if(!gd->
tried[ver][sz][sc])
247 if(!gd->
models[ver][sz][sc])
253 if(gd->
models[ver][sz][sc])
269 if(!base || !*base || strchr(base,
'/') || strchr(base,
'\\'))
return NULL;
272 if(!gd->
custom) gd->
custom = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, NULL);
274 if(!g_hash_table_lookup_extended(gd->
custom, base, NULL, (gpointer *)&
m))
282 g_hash_table_insert(gd->
custom, g_strdup(base),
m);
299 GDir *
d = g_dir_open(dir, 0, NULL);
303 while((fn = g_dir_read_name(
d)))
305 if(!g_str_has_suffix(fn,
".anselnn"))
continue;
306 gboolean shipped =
FALSE;
313 shipped = !g_strcmp0(fn,
name);
315 if(!shipped)
out = g_list_prepend(
out, g_strdup(fn));
318 return g_list_sort(
out, (GCompareFunc)g_strcmp0);
323 return _(
"raw denoise (AI)");
329 _(
"denoise the raw picture with a neural network conditioned "
330 "on the camera noise profile"),
331 _(
"corrective"), _(
"linear, raw, scene-referred"), _(
"linear, raw"),
332 _(
"linear, raw, scene-referred"));
377#define NN_REGION_MAX_BLOCKS 32
381#define NN_REGION_SLACK 1.02f
398 bytes = (bytes + 63) & ~(
size_t)63;
400 size_t off = (size_t)-1;
406 for(
int i = 0;
i <=
r->n_blocks;
i++)
408 const size_t end = (
i <
r->n_blocks) ?
r->blocks[
i].off :
r->size;
409 if(end - gap >= bytes)
415 if(
i ==
r->n_blocks)
break;
416 gap =
r->blocks[
i].off +
r->blocks[
i].len;
424 size_t gap_end =
r->size;
425 for(
int i =
r->n_blocks;
i >= 0;
i--)
427 const size_t gap_start = (
i > 0) ?
r->blocks[
i - 1].off +
r->blocks[
i - 1].len : 0;
428 if(gap_end - gap_start >= bytes)
430 off = gap_end - bytes;
434 if(
i > 0) gap_end =
r->blocks[
i - 1].off;
437 if(off == (
size_t)-1)
439 size_t live = 0, largest_gap = 0, gap = 0;
440 for(
int i = 0;
i <=
r->n_blocks;
i++)
442 const size_t end = (
i <
r->n_blocks) ?
r->blocks[
i].off :
r->size;
443 if(end - gap > largest_gap) largest_gap = end - gap;
444 if(
i ==
r->n_blocks)
break;
445 live +=
r->blocks[
i].len;
446 gap =
r->blocks[
i].off +
r->blocks[
i].len;
449 "[rawdenoiseai] region alloc failed: %" G_GSIZE_FORMAT
" bytes (%s), region %" G_GSIZE_FORMAT
450 ", live %" G_GSIZE_FORMAT
" in %d blocks, largest gap %" G_GSIZE_FORMAT
"\n",
451 bytes, long_lived ?
"long-lived" :
"churn",
r->size, live,
r->n_blocks, largest_gap);
454 for(
int i =
r->n_blocks;
i > at;
i--)
r->blocks[
i] =
r->blocks[
i - 1];
455 r->blocks[at].off = off;
456 r->blocks[at].len = bytes;
458 return r->base + off;
463 const size_t off = (size_t)((
char *)
p -
r->base);
464 for(
int i = 0;
i <
r->n_blocks;
i++)
465 if(
r->blocks[
i].off == off)
467 for(
int j =
i; j <
r->n_blocks - 1; j++)
r->blocks[j] =
r->blocks[j + 1];
482 if(
r && (
char *)
p >=
r->base && (
char *)
p < r->base +
r->size)
554 g_hash_table_iter_init(&it, gd->
custom);
555 while(g_hash_table_iter_next(&it, &
k, &
v))
557 g_hash_table_destroy(gd->
custom);
611 module->hide_enable_button = !_rawdenoiseai_supported(module);
612 module->default_enabled = 0;
636 interpolated = *first;
637 for(GList *iter = profiles; iter; iter = g_list_next(iter))
640 if(current->
iso == iso)
642 interpolated = *current;
645 if(last && last->
iso < iso && current->
iso > iso)
647 interpolated.
iso = iso;
651 interpolated = *current;
655 for(
int k = 0;
k < 3;
k++)
657 d->a[
k] = interpolated.
a[
k];
658 d->b[
k] = interpolated.
b[
k];
669 d->strength =
p->strength;
670 d->noise_level =
p->noise_level;
671 d->sigma_scale[0] =
p->sigma_red;
672 d->sigma_scale[1] =
p->sigma_green;
673 d->sigma_scale[2] =
p->sigma_blue;
686 const gboolean want_custom = gd &&
p->custom_model[0];
692 "[rawdenoiseai] commit: camera '%s' iso %.0f model %s-%s-%s%s -> "
693 "a=(%.3g %.3g %.3g) b=(%.3g %.3g %.3g) strength %.2f noise level %.2f "
694 "channel correction (%.2f %.2f %.2f)\n",
699 d->model ? (want_custom ?
" (user model)" :
"") :
" (missing)",
700 d->a[0],
d->a[1],
d->a[2],
d->b[0],
d->b[1],
d->b[2],
p->strength,
p->noise_level,
p->sigma_red,
701 p->sigma_green,
p->sigma_blue);
714 "[rawdenoiseai] model plane layout unsupported "
715 "(fine_in %d coarse_out %d) — module disabled\n",
720 if(!
d->model || !(
p->strength > 0.0f))
piece->enabled = 0;
723static unsigned _align_lcm(
const unsigned a,
const unsigned b)
725 if(a == 0 || b == 0)
return a > b ? a : b;
726 unsigned x = a, y = b;
729 const unsigned t =
x % y;
743 float extra_cl = 6.0f;
744 unsigned overlap = 0;
750 const gboolean xtrans_cfa =
piece->dsc_in.filters == 9u;
753 + (bin > 1 ? 12.0f / (float)(bin * bin) : 0.0f);
797 overlap = (bin > 1) ? 192 : 96;
799 tiling->factor = 2.0f + extra;
815 tiling->factor_cl = 2.0f + extra_cl * 1.4f;
818 tiling->overhead = 64u * 1024u * 1024u;
819 tiling->overlap = overlap;
840 const gboolean xtrans =
piece->dsc_in.filters == 9u;
841 unsigned align = xtrans ? 6u : 2u;
857 if(
v >=
n)
v = 2 *
n - 2 -
v;
867 for(
int c = 0; c < 3; c++) scale[c] =
d->noise_level *
d->sigma_scale[c];
877static void _k_bin_planes(
const float *
const nn_in,
float *
const coarse_in,
float *
const cnt,
878 const int pw,
const int ph,
const int bin,
881 const int cw = pw / bin, chh = ph / bin;
882 const size_t cplane = (size_t)cw * chh;
887 for(
int c = 0; c < 3; c++)
889 const float *
const mean = coarse_in + (size_t)c * cplane;
890 const float *
const n_c = cnt + (size_t)c * cplane;
891 float *
const sigma = coarse_in + (size_t)(3 + c) * cplane;
892 for(
size_t i = 0;
i < cplane;
i++)
894 const float n = n_c[
i] > 1.0f ? n_c[
i] : 1.0f;
895 const float var = (
d->a[c] *
MAX(mean[
i], 0.0f) +
d->b[c]) /
n;
896 sigma[
i] = scale[c] * sqrtf(
MAX(var, 1e-12f));
907 const uint32_t filters,
const uint8_t (*
const xtrans)[6],
910 const size_t plane = (size_t)pw * ph;
914 for(
int y = 0; y < ph; y++)
916 const int sy = _reflect(y,
height);
917 const float *
const irow =
in + (size_t)sy *
width;
918 float *
const mosaic = nn_in + (size_t)y * pw;
919 float *
const onehot_r = nn_in + plane + (size_t)y * pw;
920 float *
const onehot_g = nn_in + 2 * plane + (size_t)y * pw;
921 float *
const onehot_b = nn_in + 3 * plane + (size_t)y * pw;
922 float *
const sigma = nn_in + 4 * plane + (size_t)y * pw;
923 for(
int x = 0;
x < pw;
x++)
925 const int sx = _reflect(
x,
width);
926 const float v = irow[sx];
927 int c = (filters == 9u) ?
FCxtrans(sy, sx, roi, xtrans) : (int)
FC(sy, sx, filters);
928 if(c < 0 || c > 2) c = 1;
930 onehot_r[
x] = (c == 0) ? 1.0f : 0.0f;
931 onehot_g[
x] = (c == 1) ? 1.0f : 0.0f;
932 onehot_b[
x] = (c == 2) ? 1.0f : 0.0f;
933 const float var =
d->a[c] *
MAX(
v, 0.0f) +
d->b[c];
934 sigma[
x] = scale[c] * sqrtf(
MAX(var, 1e-12f));
954#define DT_NN_FUSION_T_CHI2 2.5
960static void _k_residual(
const float *
const in,
const float *
const head,
float *
const out,
const size_t n)
974 for(
int y = 0; y <
height; y++)
976 const float *
const src_in =
in + (size_t)y *
width;
977 const float *
const src_nn = den + (size_t)y * pw;
978 float *
const dst =
out + (size_t)y *
width;
986static void _k_bin16_mdv(
const float *
const nn_in,
const float *
const denoised,
float *
const M,
987 float *
const D,
float *
const V,
const int pw,
const int ph)
989 const size_t plane = (size_t)pw * ph;
990 const float *
const sig = nn_in + 4 * plane;
992 const size_t p0 = (size_t)cw * chh;
993 for(
int c = 0; c < 3; c++)
995 const float *
const oh = nn_in + (size_t)(1 + c) * plane;
997 for(
int cy = 0; cy < chh; cy++)
998 for(
int cx = 0; cx < cw; cx++)
1000 float sm = 0.f, sd = 0.f, sv = 0.f, cnt = 0.f;
1004 const size_t i = (size_t)y * pw +
x;
1005 sm += nn_in[
i] * oh[
i];
1006 sd += denoised[
i] * oh[
i];
1007 sv += sig[
i] * sig[
i] * oh[
i];
1010 const float n = cnt > 1.f ? cnt : 1.f;
1011 const size_t o = (size_t)c * p0 + (
size_t)cy * cw + cx;
1023static void _k_avg2x2(
const float *
const in,
float *
const out,
const int sw,
const int sh,
const size_t p0)
1025 const int w2 = sw / 2, h2 = sh / 2;
1026 for(
int c = 0; c < 3; c++)
1028 const float *
const src =
in + (size_t)c * p0;
1029 float *
const dst =
out + (size_t)c * p0;
1030 for(
int y = 0; y < h2; y++)
1031 for(
int x = 0;
x <
w2;
x++)
1032 dst[(
size_t)y *
w2 +
x]
1034 * (src[(size_t)(2 * y) * sw + 2 *
x] + src[(size_t)(2 * y) * sw + 2 *
x + 1]
1035 + src[(size_t)(2 * y + 1) * sw + 2 *
x] + src[(size_t)(2 * y + 1) * sw + 2 *
x + 1]);
1051static inline __attribute__((always_inline))
float _bilerp_tap(
const float *
const p,
const int w,
1052 const int h,
const float fx,
1055 const float cx =
fx < 0.f ? 0.f : (
fx > w - 1.f ? w - 1.f :
fx);
1056 const float cy = fy < 0.f ? 0.f : (fy > h - 1.f ? h - 1.f : fy);
1057 const int x0 = (int)cx, y0 = (
int)cy;
1058 const int x1 = x0 + 1 < w ? x0 + 1 : x0;
1059 const int y1 = y0 + 1 < h ? y0 + 1 : y0;
1060 const float ax = cx - x0, ay = cy - y0;
1061 const float top =
p[(size_t)y0 * w + x0] * (1.f - ax) +
p[(size_t)y0 * w + x1] * ax;
1062 const float bot =
p[(size_t)y1 * w + x0] * (1.f - ax) +
p[(size_t)y1 * w + x1] * ax;
1063 return top * (1.f - ay) + bot * ay;
1068static void _upsample_bilinear(
const float *
const src,
const int sw,
const int sh,
const int f,
float *
const dst)
1070 const int fw = sw *
f, fh = sh *
f;
1071 for(
int y = 0; y < fh; y++)
1073 const float sy = (y + 0.5f) /
f - 0.5f;
1074 for(
int x = 0;
x < fw;
x++) dst[(
size_t)y * fw +
x] = _bilerp_tap(src, sw, sh, (
x + 0.5f) /
f - 0.5f, sy);
1101static void _k_floor_fuse(
const float *
const M,
const float *
const D,
const float *
const V,
1102 float *
const fused,
const int sw,
const int sh,
const size_t p0,
const int S)
1104 for(
int c = 0; c < 3; c++)
1107 const float *
const Mp =
M + (size_t)c * p0;
1108 const float *
const Dp = D + (size_t)c * p0;
1109 const float *
const Vp =
V + (size_t)c * p0;
1110 float *
const fs = fused + (size_t)c * p0;
1111 for(
int y = 0; y < sh; y++)
1112 for(
int x = 0;
x < sw;
x++)
1116 double structure = 0.0;
1117 for(
int ny = -1; ny <= 1; ny++)
1118 for(
int nx = -1; nx <= 1; nx++)
1120 const int cy2 = CLAMP(y + ny, 0, sh - 1), cx2 = CLAMP(
x + nx, 0, sw - 1);
1122 for(
int dy = -1; dy <= 1; dy++)
1123 for(
int dx = -1; dx <= 1; dx++)
1125 const int yy = CLAMP(cy2 + dy, 0, sh - 1), xx = CLAMP(cx2 + dx, 0, sw - 1);
1126 mean += Mp[(size_t)yy * sw + xx];
1128 const double mloc = Mp[(size_t)cy2 * sw + cx2] - mean / 9.0;
1129 structure += mloc * mloc;
1131 const size_t i = (size_t)y * sw +
x;
1133 const double vn = (
double)Vp[
i] * vscale;
1135 if(structure < 0.0) structure = 0.0;
1136 const float w = (float)(structure / (structure + vn + 1e-20));
1137 fs[
i] = w * Dp[
i] + (1.f - w) * Mp[
i];
1146static void _k_fuse_step(
const float *
const fused_c,
const float *
const Mf,
const float *
const Df,
1147 const float *
const Vf,
const float *
const Mc,
const float *
const Dc,
1148 float *
const fused_f,
float *
const ups,
const int sw,
const int sh,
1149 const size_t p0,
const int sc)
1151 const int fw = sw * 2, fh = sh * 2;
1152 for(
int c = 0; c < 3; c++)
1154 const float *
const Dfp = Df + (size_t)c * p0;
1155 const float *
const Mfp = Mf + (size_t)c * p0;
1156 const float *
const Vfp = Vf + (size_t)c * p0;
1157 float *
const upD = ups, *
const upM = ups + p0;
1166 float *
const fs = fused_f + (size_t)c * p0;
1167 for(
int y = 0; y < fh; y++)
1168 for(
int x = 0;
x < fw;
x++)
1173 for(
int dy = -1; dy <= 1; dy++)
1174 for(
int dx = -1; dx <= 1; dx++)
1176 const int yy = CLAMP(y + dy, 0, fh - 1), xx = CLAMP(
x + dx, 0, fw - 1);
1177 const size_t j = (size_t)yy * fw + xx;
1178 const double d = (
double)(Dfp[j] - upD[j]) - (
double)(Mfp[j] - upM[j]);
1182 const size_t i = (size_t)y * fw +
x;
1183 const double vn = (
double)Vfp[
i] * vscale;
1185 if(vm < 0.0) vm = 0.0;
1186 const float w = (float)(vn / (vn + vm + 1e-20));
1187 fs[
i] += w * (Dfp[
i] - upD[
i]) + (1.f - w) * (Mfp[
i] - upM[
i]);
1196static void _k_bilerp_add(
const float *
const fused,
const float *
const D16,
float *
const scratch,
1197 const float *
const nn_in,
float *
const denoised,
const int pw,
const int ph,
1198 const size_t p0,
const int cw0,
const int ch0)
1200 const size_t plane = (size_t)pw * ph;
1202 for(
int c = 0; c < 3; c++)
1204 float *
const cs = scratch + (size_t)c * p0;
1205 const float *
const fa = fused + (size_t)c * p0;
1206 const float *
const d0 = D16 + (size_t)c * p0;
1207 for(
size_t i = 0;
i < p0;
i++) cs[
i] = fa[
i] -
d0[
i];
1210 for(
int y = 0; y < ph; y++)
1212 const float sy = (y + 0.5f) *
inv - 0.5f;
1213 for(
int x = 0;
x < pw;
x++)
1215 const size_t i = (size_t)y * pw +
x;
1216 for(
int c = 0; c < 3; c++)
1217 if(nn_in[(
size_t)(1 + c) * plane +
i] > 0.0f)
1219 denoised[
i] += _bilerp_tap(scratch + (
size_t)c * p0, cw0, ch0, (
x + 0.5f) *
inv - 0.5f, sy);
1229 if(scale <= 0)
return 0;
1236 if(pw %
S || ph %
S)
return 0;
1237 const int cw0 = pw / 16, ch0 = ph / 16;
1238 const size_t p0 = (size_t)cw0 * ch0;
1243 for(
int k = 0;
k < 9;
k++) lv[
k] = buf + (
size_t)
k * p0 * 3;
1244 float *fusedA = buf + p0 * 3 * 9, *fusedB = fusedA + p0 * 3, *ups = fusedB + p0 * 3;
1246 _k_bin16_mdv(nn_in, denoised, lv[0], lv[1], lv[2], pw, ph);
1248 int w_l = cw0, h_l = ch0;
1249 for(
int k = 1;
k < nlev;
k++)
1251 for(
int md = 0; md < 3; md++)
_k_avg2x2(lv[3 * (
k - 1) + md], lv[3 *
k + md], w_l, h_l, p0);
1264 int sw = w_l, sh = h_l;
1272 _k_floor_fuse(lv[3 * (nlev - 1)], lv[3 * (nlev - 1) + 1], lv[3 * (nlev - 1) + 2], fusedA, sw, sh, p0,
1274 for(
int k = nlev - 2;
k >= 0;
k--)
1276 _k_fuse_step(fusedA, lv[3 *
k], lv[3 *
k + 1], lv[3 *
k + 2], lv[3 * (
k + 1)], lv[3 * (
k + 1) + 1], fusedB,
1278 float *tmp = fusedA;
1285 _k_bilerp_add(fusedA, lv[1], fusedB, nn_in, denoised, pw, ph, p0, cw0, ch0);
1292 const void *
const ivoid,
void *
const ovoid)
1299 const float *
const in = (
const float *)ivoid;
1302 if(!
model || !(
d->strength > 0.0f))
1314 const uint8_t(*
const xtrans)[6] = (
const uint8_t(*
const)[6])
piece->dsc_in.xtrans;
1318 const int pw = (
width + align - 1) / align * align;
1319 const int ph = (
height + align - 1) / align * align;
1320 const size_t plane = (size_t)pw * ph;
1329 region.
size = ((plane * (in_ch + 1) *
sizeof(float) + 63) & ~(size_t)63)
1331 region.
size = (region.
size + 63) & ~(
size_t)63;
1336 region.
size / 1048576.0);
1342 float *
const nn_in =
_region_alloc(®ion, plane * in_ch *
sizeof(
float), 0);
1343 float *
const nn_out =
_region_alloc(®ion, plane *
sizeof(
float), 0);
1355 const int cw = pw / bin, chh = ph / bin;
1356 const size_t cplane = (size_t)cw * chh;
1367 if(!rc)
_k_residual(coarse_in, coarse_out, coarse_out, cplane * 3);
1378 if(!rc)
_k_residual(nn_in, nn_out, nn_out, plane);
1384 dt_print(
DT_DEBUG_ALWAYS,
"[rawdenoiseai] inference failed (%d) on %dx%d tile, scratch %.1f MB\n", rc, pw, ph,
1407 cl_mem dev_in, cl_mem dev_out)
1413 const int devid =
pipe->devid;
1421 const int is_xtrans = filters == 9u;
1424 const int pw = (
width + align - 1) / align * align;
1425 const int ph = (
height + align - 1) / align * align;
1426 const size_t plane = (size_t)pw * ph;
1429 const int cw = pw / bin, chh = ph / bin;
1430 const size_t cplane = (size_t)cw * chh;
1432 const int gw = pw / 16,
gh = ph / 16;
1433 const size_t p16 = (size_t)gw *
gh;
1443 gboolean success =
FALSE;
1444 int err = CL_SUCCESS;
1458 cl_mem dev_cin = NULL, dev_chead = NULL, dev_cden = NULL;
1460 cl_mem grids[11] = { NULL };
1461 if(!dev_planes || !dev_noise || !dev_den || !dev_xtrans || !dev_in_buf || !dev_out_buf)
goto cleanup;
1463 size_t origin[3] = { 0, 0, 0 };
1464 size_t region[3] = { (size_t)
width, (
size_t)
height, 1 };
1466 if(err != CL_SUCCESS)
goto cleanup;
1473 if(!dev_cin || !dev_chead || !dev_cden)
goto cleanup;
1480 for(
int k = 0;
k < 11;
k++)
1488 unsigned char xtrans_host[36];
1489 memcpy(xtrans_host,
piece->dsc_in.xtrans,
sizeof(xtrans_host));
1497 const unsigned int f = filters;
1514 if(err != CL_SUCCESS)
goto cleanup;
1531 if(err != CL_SUCCESS)
goto cleanup;
1534 if(err != CL_SUCCESS)
goto cleanup;
1536 const int n3 = (int)(cplane * 3);
1541 size_t sz[3] = {
ROUNDUPDWD(n3, devid), 1, 1 };
1543 if(err != CL_SUCCESS)
goto cleanup;
1544 cl_mem guide_src = dev_cden;
1546 const int three = 3;
1547 const cl_long dst_off = (cl_long)plane * 5;
1557 if(err != CL_SUCCESS)
goto cleanup;
1565 if(err != CL_SUCCESS)
1567 dt_print(
DT_DEBUG_OPENCL,
"[rawdenoiseai] GPU inference failed on %dx%d tile, falling back to CPU\n", pw, ph);
1572 const int n1 = (int)plane;
1577 size_t sz[3] = {
ROUNDUPDWD(n1, devid), 1, 1 };
1579 if(err != CL_SUCCESS)
goto cleanup;
1596 if(err != CL_SUCCESS)
goto cleanup;
1599 int lw = gw, lh =
gh;
1600 for(
int k = 1;
k < nlev;
k++)
1602 for(
int md = 0; md < 3; md++)
1611 if(err != CL_SUCCESS)
goto cleanup;
1617 int fA = 9, fB = 10;
1620 const int S = 16 << (nlev - 1);
1631 if(err != CL_SUCCESS)
goto cleanup;
1633 for(
int k = nlev - 2;
k >= 0;
k--)
1635 const int fw =
lw * 2, fh = lh * 2, sc = 16 <<
k;
1651 if(err != CL_SUCCESS)
goto cleanup;
1669 if(err != CL_SUCCESS)
goto cleanup;
1685 if(err != CL_SUCCESS)
goto cleanup;
1689 size_t origin[3] = { 0, 0, 0 };
1690 size_t region[3] = { (size_t)
width, (
size_t)
height, 1 };
1692 if(err != CL_SUCCESS)
goto cleanup;
1706 for(
int k = 0;
k < 11;
k++)
1744 g_strlcpy(
p->custom_model, txt,
sizeof(
p->custom_model));
1746 p->custom_model[0] =
'\0';
1758 for(GList *l = files; l; l = g_list_next(l))
1762 if(!g_strcmp0((
const char *)l->data,
p->custom_model)) sel =
i;
1765 if(
p->custom_model[0] && !sel)
1770 g_list_free_full(files,
g_free);
1773 const gboolean shipped = !
p->custom_model[0];
1774 gtk_widget_set_sensitive(
g->version, shipped);
1775 gtk_widget_set_sensitive(
g->size, shipped);
1776 gtk_widget_set_sensitive(
g->scale_variant, shipped);
1791 const gboolean have_model = gd && (
p->custom_model[0] ?
_get_custom_model(gd,
p->custom_model)
1794 gchar *prof = profiles
1797 : g_strdup(_(
"no noise profile for this camera — using the generic profile"));
1798 gchar *label = have_model
1800 : g_strdup_printf(_(
"selected model (%s %s, %s) is not installed — module inactive\n%s"),
1804 gtk_label_set_text(GTK_LABEL(
g->profile_label), label);
1820 gtk_widget_set_tooltip_text(
g->strength, _(
"opacity of the noise removal: blends between the original\n"
1821 "image (0%) and the fully denoised result (100%).\n"
1822 "lower it to keep some residual grain"));
1825 gtk_widget_set_tooltip_text(
g->version, _(
"neural model version. Older edits keep their original\n"
1826 "version so their result never changes across updates."));
1829 gtk_widget_set_tooltip_text(
g->size, _(
"network width. large: reference quality, practical on GPU\n"
1830 "(OpenCL) and the default there. half: ~4x faster.\n"
1831 "quarter: ~4x faster again, the default without OpenCL\n"
1832 "and the choice for weak hardware or near-realtime editing."));
1835 gtk_widget_set_tooltip_text(
g->scale_variant, _(
"single-scale: the fine full-resolution pass only — fast,\n"
1836 "no low-frequency chroma handling. multiscale: adds the\n"
1837 "coarse chroma pass and the low-band fusion — high quality,\n"
1838 "recommended for high ISO."));
1842 gtk_box_pack_start(GTK_BOX(box_raw),
g->custom_model,
TRUE,
TRUE, 0);
1843 gtk_widget_set_tooltip_text(
g->custom_model,
1844 _(
"use a neural model of your own instead of the shipped ones.\n"
1845 "drop a .anselnn file into your Ansel config directory and it\n"
1846 "appears here; the edit records the file NAME, so it keeps\n"
1847 "pointing at the same model as the folder changes."));
1853 gtk_label_set_line_wrap(GTK_LABEL(
g->profile_label),
TRUE);
1854 gtk_box_pack_start(GTK_BOX(box_raw),
g->profile_label,
FALSE,
FALSE, 0);
1859 gtk_widget_set_tooltip_text(
g->noise_level, _(
"global scale on the assumed noise amplitude, relative to the\n"
1860 "calibrated noise for this camera at this ISO (100% trusts the\n"
1861 "calibration). raise it if noise remains, lower it if fine\n"
1862 "detail is eaten"));
1865 _(
"per-channel corrections"), GTK_BOX(box_raw), GTK_PACK_START);
1866 self->
gui->
widget = GTK_WIDGET(
g->cs.container);
1868 const char *sigma_tooltip = _(
"per-channel correction of the camera noise profile, applied on top\n"
1869 "of the global correction. Profiles are measured after demosaicing,\n"
1870 "which averages away part of the noise — most on the dense green\n"
1871 "lattice — while this module sees the raw sensor noise at full\n"
1872 "strength. The defaults are calibrated against raw-mosaic\n"
1873 "measurements over 253 cameras.");
1877 gtk_widget_set_tooltip_text(
g->sigma_red, sigma_tooltip);
1882 gtk_widget_set_tooltip_text(
g->sigma_green, sigma_tooltip);
1887 gtk_widget_set_tooltip_text(
g->sigma_blue, sigma_tooltip);
1892 "and an installed rawdenoiseai model file."));
1896 gtk_stack_add_named(GTK_STACK(self->
gui->
widget), label_unsupported,
"unsupported");
1897 gtk_stack_add_named(GTK_STACK(self->
gui->
widget), box_raw,
"raw");
void cleanup(dt_imageio_module_format_t *self)
void dt_bauhaus_slider_set_digits(GtkWidget *widget, int val)
void dt_bauhaus_combobox_clear(GtkWidget *widget)
int dt_bauhaus_combobox_get(GtkWidget *widget)
const char * dt_bauhaus_combobox_get_text(GtkWidget *widget)
void dt_bauhaus_combobox_set(GtkWidget *widget, const int pos)
void dt_bauhaus_widget_set_label(GtkWidget *widget, const char *label)
GtkWidget * dt_bauhaus_combobox_new(dt_bauhaus_t *bh, dt_gui_module_t *self)
void dt_bauhaus_slider_set_format(GtkWidget *widget, const char *format)
void dt_bauhaus_combobox_add(GtkWidget *widget, const char *text)
void dt_gui_new_collapsible_section(dt_gui_collapsible_section_t *cs, const char *confname, const char *label, GtkBox *parent, GtkPackType pack)
Create a collapsible section and pack it into the parent box.
void dt_gui_update_collapsible_section(dt_gui_collapsible_section_t *cs)
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
const dt_colormatrix_t dt_aligned_pixel_t out
static const dt_colormatrix_t M
static float strength(float value, float strength)
gboolean dt_image_needs_demosaic(const dt_image_t *img)
struct dt_bauhaus_t * dt_bauhaus_get_global(void)
static int FCxtrans(const int row, const int col, global const unsigned char(*const xtrans)[6])
static int FC(const int row, const int col, const unsigned int filters)
#define dt_dev_add_history_item(dev, module, enable, redraw)
static int dt_pthread_mutex_unlock(dt_pthread_mutex_t *mutex) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
static int dt_pthread_mutex_init(dt_pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
Initialise a mutex. With mutexattr NULL – which is how 54 of the 56 call sites in this tree spell it ...
static int dt_pthread_mutex_destroy(dt_pthread_mutex_t *mutex)
static int dt_pthread_mutex_lock(dt_pthread_mutex_t *mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
void dt_loc_get_datadir(char *datadir, size_t bufsize)
void dt_loc_get_user_config_dir(char *configdir, size_t bufsize)
memcpy(roundtrip, origin, sizeof(roundtrip))
static void dt_iop_image_copy_by_size(float *const __restrict__ out, const float *const __restrict__ in, const size_t width, const size_t height, const size_t ch)
uint32_t dt_dev_get_roi_filters(const dt_dev_pixelpipe_iop_t *const piece, const dt_iop_roi_t *const roi_in)
void dt_iop_default_init(dt_iop_module_t *module)
const char ** dt_iop_set_description(dt_iop_module_t *module, const char *main_text, const char *purpose, const char *input, const char *process, const char *output)
void dt_iop_request_focus(dt_iop_module_t *module)
Move darkroom focus to module, or clear it with NULL.
@ IOP_FLAGS_SUPPORTS_BLENDING
GtkWidget * dt_bauhaus_slider_from_params(dt_iop_module_t *self, const char *param)
GtkWidget * dt_bauhaus_combobox_from_params(dt_iop_module_t *self, const char *param)
static dt_iop_gui_data_t * dt_iop_gui_data(const struct dt_iop_module_t *m)
The module's GUI data blob, NULL-safe for headless callers: IOP process() implementations read it for...
#define IOP_GUI_ALLOC(module)
GtkWidget * dt_ui_section_label_new(const gchar *str)
GtkWidget * dt_ui_label_new(const gchar *str)
static const dt_dev_pixelpipe_t * pipe
static const dt_dev_pixelpipe_t const dt_dev_pixelpipe_iop_t * piece
static const dt_iop_drawlayer_params_t * params
void dt_print(dt_debug_thread_t thread, const char *msg,...) __attribute__((format(printf
Print to stdout when thread is enabled, prefixed with seconds since startup.
float *const restrict const size_t k
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
#define dt_free_align(ptr)
Release memory from dt_alloc_align() and set ptr to NULL.
static void * dt_calloc_align(size_t size)
dt_alloc_align() followed by a zero fill.
#define DT_MODULE_INTROSPECTION(MODVER, PARAMSTYPE)
DT_MODULE() for a module whose params struct is introspected.
static float gh(const float f)
void dt_nn_cl_destroy(dt_nn_cl_t *cl)
int dt_nn_unet_apply_stage_cl(const dt_nn_model_t *m, int stage, dt_nn_cl_t *cl, int devid, cl_mem dev_in, cl_mem dev_out, int width, int height)
int dt_nn_model_anchor(const dt_nn_model_t *m)
void dt_nn_model_free(dt_nn_model_t *m)
int dt_nn_model_in_channels(const dt_nn_model_t *m)
int dt_nn_model_coarse_in_channels(const dt_nn_model_t *m)
dt_nn_cl_t * dt_nn_cl_create(int program)
size_t dt_nn_unet_scratch_bytes(const dt_nn_model_t *m, int width, int height)
int dt_nn_unet_apply_stage(const dt_nn_model_t *m, int stage, const float *in, float *out, int width, int height, int apply_residual)
void dt_nn_set_cancel(dt_nn_cancel_f cancel_fn)
float dt_nn_unet_scratch_per_px(const dt_nn_model_t *m)
dt_nn_model_t * dt_nn_model_load(const char *path, char *err, size_t err_len)
__DT_CLONE_TARGETS__ void dt_nn_upsample_nearest(const float *in, int ch, int w, int h, int factor, float *out)
int dt_nn_model_coarse_out_channels(const dt_nn_model_t *m)
float dt_nn_unet_scratch_per_px_cl(const dt_nn_model_t *m)
int dt_nn_model_alignment(const dt_nn_model_t *m)
__DT_CLONE_TARGETS__ void dt_nn_bin_planes(const float *planes, int pw, int ph, int bin, float *out_rgb, float *out_cnt)
int dt_nn_model_bin(const dt_nn_model_t *m, const int is_xtrans)
void dt_nn_set_allocator(dt_nn_alloc_f alloc_fn, dt_nn_free_f free_fn)
#define DT_NN_FUSION_COARSEST
#define DT_NN_FUSION_FINEST
const dt_noiseprofile_t dt_noiseprofile_generic
void dt_noiseprofile_interpolate(const dt_noiseprofile_t *const p1, const dt_noiseprofile_t *const p2, dt_noiseprofile_t *out)
void dt_noiseprofile_free(gpointer data)
GList * dt_noiseprofile_get_matching(const dt_image_t *cimg)
int dt_opencl_enqueue_kernel_2d(const int dev, const int kernel, const size_t *sizes)
void * dt_opencl_alloc_device_buffer(const int devid, const size_t size)
int dt_opencl_enqueue_copy_buffer_to_image(const int devid, cl_mem src_buffer, cl_mem dst_image, size_t offset, size_t *origin, size_t *region)
int dt_opencl_create_kernel(const int prog, const char *name)
int dt_opencl_write_buffer_to_device(const int devid, void *host, void *device, const size_t offset, const size_t size, const int blocking)
int dt_opencl_is_enabled(void)
void dt_opencl_free_kernel(const int kernel)
int dt_opencl_set_kernel_arg(const int dev, const int kernel, const int num, const size_t size, const void *arg)
int dt_opencl_enqueue_copy_image_to_buffer(const int devid, cl_mem src_image, cl_mem dst_buffer, size_t *origin, size_t *region, size_t offset)
void dt_opencl_release_mem_object(cl_mem mem)
#define __OMP_PARALLEL_FOR__(...)
#define DT_PATH_MAX
Buffer size for a filesystem path anywhere in Ansel.
void dt_concat_path_file(char destination[4096], const char path[4096], const char *const file)
Append a constant filename to a variable, stack-based, fixed-sized, directory, and add a / in-between...
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
#define dt_pixelpipe_cache_alloc_align_cache(size, id)
#define dt_pixelpipe_cache_free_align(mem)
static gboolean dt_dev_pixelpipe_has_shutdown(const dt_dev_pixelpipe_t *pipe)
static dt_nn_model_t * _get_custom_model(dt_iop_rawdenoiseai_global_data_t *gd, const char *base)
void init(dt_iop_module_t *module)
const char ** description(struct dt_iop_module_t *self)
static const char *const _scale_tag[2]
__DT_CLONE_TARGETS__ int process(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid)
static void _region_free(nn_region_t *r, void *p)
#define DT_NN_FUSION_T_CHI2
static __DT_CLONE_TARGETS__ void _k_bin16_mdv(const float *const nn_in, const float *const denoised, float *const M, float *const D, float *const V, const int pw, const int ph)
void reload_defaults(dt_iop_module_t *module)
static int _apply_low_band_anchor(const float *const nn_in, float *const denoised, const int pw, const int ph, const int scale)
static void _sigma_scale(const dt_iop_rawdenoiseai_data_t *d, float scale[3])
void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *params, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
#define NN_REGION_MAX_BLOCKS
static __thread const dt_dev_pixelpipe_t * _nn_pipe
dt_iop_rawdenoiseai_scale_t
void gui_update(dt_iop_module_t *self)
Refresh GUI controls from current params and configuration.
static int _nn_cancelled(void)
static __DT_CLONE_TARGETS__ void _upsample_bilinear(const float *const src, const int sw, const int sh, const int f, float *const dst)
static void _nn_arena_free(void *p)
static __DT_CLONE_TARGETS__ void _k_residual(const float *const in, const float *const head, float *const out, const size_t n)
void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
static gboolean _rawdenoiseai_supported(dt_iop_module_t *module)
static GList * _list_custom_models(void)
void gui_init(dt_iop_module_t *self)
static void _custom_model_populate(dt_iop_module_t *self)
static __DT_CLONE_TARGETS__ void _k_bin_planes(const float *const nn_in, float *const coarse_in, float *const cnt, const int pw, const int ph, const int bin, const dt_iop_rawdenoiseai_data_t *const d)
#define DT_RAWDENOISEAI_MODEL_LEN
static __DT_CLONE_TARGETS__ void _k_assemble(const float *const in, float *const nn_in, const int width, const int height, const int pw, const int ph, const dt_iop_rawdenoiseai_data_t *const d, const uint32_t filters, const uint8_t(*const xtrans)[6], const dt_iop_roi_t *const roi)
static __thread nn_region_t * _nn_region
void tiling_callback(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe, const struct dt_dev_pixelpipe_iop_t *piece, struct dt_develop_tiling_t *tiling)
static __DT_CLONE_TARGETS__ void _k_bilerp_add(const float *const fused, const float *const D16, float *const scratch, const float *const nn_in, float *const denoised, const int pw, const int ph, const size_t p0, const int cw0, const int ch0)
void gui_cleanup(dt_iop_module_t *self)
static void _fetch_noise_profile(dt_iop_module_t *self, dt_iop_rawdenoiseai_data_t *d)
void cleanup_global(dt_iop_module_so_t *module)
__DT_CLONE_TARGETS__ static __DT_CLONE_TARGETS__ void _k_floor_fuse(const float *const M, const float *const D, const float *const V, float *const fused, const int sw, const int sh, const size_t p0, const int S)
static void _custom_model_callback(GtkWidget *w, dt_iop_module_t *self)
int default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece)
void input_format(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_iop_buffer_dsc_t *dsc)
static const char *const _version_tag[1]
dt_iop_rawdenoiseai_size_t
@ DT_RAWDENOISEAI_QUARTER
static const char *const _size_tag[3]
static void * _nn_arena_alloc(size_t bytes, int long_lived)
gboolean force_enable(struct dt_iop_module_t *self, const gboolean current_state)
void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
static unsigned _align_lcm(const unsigned a, const unsigned b)
static void * _region_alloc(nn_region_t *r, size_t bytes, int long_lived)
#define DT_RAWDENOISEAI_NUM_SCALES
void init_global(dt_iop_module_so_t *module)
static __DT_CLONE_TARGETS__ void _k_avg2x2(const float *const in, float *const out, const int sw, const int sh, const size_t p0)
static __DT_CLONE_TARGETS__ void _k_blend_crop(const float *const in, const float *const den, float *const out, const int width, const int height, const int pw, const float strength)
dt_iop_rawdenoiseai_version_t
int process_cl(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out)
#define DT_RAWDENOISEAI_NUM_SIZES
static dt_nn_model_t * _get_model(dt_iop_rawdenoiseai_global_data_t *gd, dt_iop_rawdenoiseai_version_t ver, dt_iop_rawdenoiseai_size_t sz, dt_iop_rawdenoiseai_scale_t sc)
static int _fusion_levels(void)
static const float DT_NN_FUSION_DENS[3]
static __DT_CLONE_TARGETS__ void _k_fuse_step(const float *const fused_c, const float *const Mf, const float *const Df, const float *const Vf, const float *const Mc, const float *const Dc, float *const fused_f, float *const ups, const int sw, const int sh, const size_t p0, const int sc)
#define DT_RAWDENOISEAI_NUM_VERSIONS
static dt_iop_rawdenoiseai_size_t _default_size(void)
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
char camera_makermodel[128]
dt_iop_global_data_t * data
int32_t hide_enable_button
dt_iop_params_t * default_params
struct dt_iop_module_gui_t * gui
struct dt_develop_t * dev
dt_iop_global_data_t * global_data
dt_nn_model_t * models[1][3][2]
dt_gui_collapsible_section_t cs
GtkWidget * profile_label
GtkWidget * scale_variant
dt_iop_rawdenoiseai_version_t version
dt_iop_rawdenoiseai_size_t size
dt_iop_rawdenoiseai_scale_t scale_variant
Region of interest passed through the pixelpipe.
struct nn_region_t::@204005242210332105254067302307172354074061017313 blocks[32]
#define __DT_CLONE_TARGETS__
typedef double((*spd)(unsigned long int wavelength, double TempK))