Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions data/denoise.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ temporal-backend = auto
# for both stages
backend = auto

# How strong the filter is (live with: systemctl --user reload aurora-camera-denoise)
# strength: the filtered picture mixed with the camera's, 0..1 (1 = filtered
# only, the default; 0.5 = half each: half the grain comes back, and half
# the softness goes)
# spatial: the 3x3 smoothing after the temporal filter, 0..1 (1 = default,
# 0 = none: finest detail and texture kept, some fine grain left)
# temporal-frames: the longest running mean, 2..8 frames (8 = default: the
# smoothest still picture; fewer = less smear on motion, more grain)
strength = 1
spatial = 1
temporal-frames = 8

# The picture after the filter, live too. All neutral by default:
# vignette 0..1 brightens the corners (lens shading; 1 = twice the light in
# the corners); brightness -1..1; contrast 0.5..2; saturation 0..2;
# warmth -1..1 (blue..orange); tint -1..1 (green..magenta); sharpen 0..1
#vignette = 0
#brightness = 0
#contrast = 1
#saturation = 1
#warmth = 0
#tint = 0
#sharpen = 0

# CPU worker threads; 0 = one, which costs the least CPU. More threads lower
# the latency a little for more total CPU time.
threads = 0
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_project_arguments(

core = static_library('cdn-core',
'src/backend.c', 'src/cpu/denoise_cpu.c', 'src/trigger.c', 'src/config.c', 'src/gain.c', 'src/effects.c',
'src/picture.c',
dependencies : [threads, dl, m])
core_dep = declare_dependency(link_with : core, dependencies : [threads, dl, m])

Expand Down Expand Up @@ -125,3 +126,4 @@ test('config', executable('test_config', 'tests/test_config.c', dependencies : c
args : [meson.current_build_dir(), files('data/boards.conf')])

test('effects', executable('test_effects', 'tests/test_effects.c', dependencies : core_dep))
test('picture', executable('test_picture', 'tests/test_picture.c', dependencies : core_dep))
32 changes: 28 additions & 4 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
#include <stdint.h>
#include <stddef.h>

#define CDN_BACKEND_ABI 1
/*
* 2: struct cdn_frame_info gained tuned, temporal_frames and spatial_q8, which
* plugins read; an ABI 1 caller's info is too short for them, so a plugin
* and its caller must agree on 2.
*/
#define CDN_BACKEND_ABI 2
#define CDN_BACKEND_ENTRY "cdn_backend_entry"

/* One NV12 frame: a luma plane and an interleaved half-height CbCr plane. */
Expand All @@ -39,8 +44,26 @@ struct cdn_frame_info {
uint32_t exposure_us;
/* presentation time in nanoseconds */
uint64_t pts_ns;
/* the user's tuning (denoise.conf); tuned = 0: unset, backend defaults */
uint8_t tuned;
/* longest running mean of the temporal filter, 2..8 frames (default 8) */
uint8_t temporal_frames;
/* spatial smoothing level in 1/256 (0 = off, 256 = default) */
uint16_t spatial_q8;
};

/* The tuning in info, or the defaults when info is NULL or untuned. */
static inline int cdn_info_age_max(const struct cdn_frame_info *info)
{
int f = info && info->tuned ? info->temporal_frames : 8;
return (f < 2 ? 2 : f > 8 ? 8 : f) - 1;
}
static inline int cdn_info_spatial_q8(const struct cdn_frame_info *info)
{
int v = info && info->tuned ? info->spatial_q8 : 256;
return v > 256 ? 256 : v;
}

struct cdn_backend_ops {
/* CDN_BACKEND_ABI */
uint32_t abi;
Expand Down Expand Up @@ -69,13 +92,14 @@ struct cdn_backend_ops {

typedef const struct cdn_backend_ops *(*cdn_backend_entry_fn)(void);

/* Optional ABI v1 companion symbol; the original ops/frame layouts stay intact.
/* Optional companion symbol, versioned with CDN_DMABUF_ABI (its process takes
* the same struct cdn_frame_info, so it moved to 2 with CDN_BACKEND_ABI).
* Borrowed, linear NV12 DMA-BUFs; offsets are from the start of each fd,
* including the PipeWire mapoffset and chunk offset. The caller owns the
* buffers until this synchronous call returns. No explicit-sync streams.
*/
#define CDN_DMABUF_ENTRY "cdn_backend_dmabuf_entry"
#define CDN_DMABUF_ABI 1
#define CDN_DMABUF_ABI 2
#define CDN_BACKEND_SINGLE_UPLOAD 1u
struct cdn_dmabuf_plane {
int fd;
Expand All @@ -97,7 +121,7 @@ struct cdn_backend_dmabuf_ops {
typedef const struct cdn_backend_dmabuf_ops *(*cdn_backend_dmabuf_entry_fn)(void);
const struct cdn_backend_dmabuf_ops *cdn_backend_dmabuf(const struct cdn_backend_ops *ops);

/* Optional ABI v1 companion symbol: answers questions about one instance.
/* Optional companion symbol: answers questions about one instance.
* Returns 1 for yes, 0 for no, negative for unknown questions. */
#define CDN_QUERY_ENTRY "cdn_backend_query"
/* Yes when this instance runs the backend's full hardware path, so
Expand Down
23 changes: 21 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ static void defaults(struct cdn_config *c)
c->temporal = true;
c->low_light_frame_rate = true;
snprintf(c->temporal_backend, sizeof(c->temporal_backend), "auto");
c->strength = 1.0f;
c->spatial = 1.0f;
c->temporal_frames = 8;
cdn_picture_defaults(&c->picture);
snprintf(c->backend, sizeof(c->backend), "auto");
snprintf(c->prefer, sizeof(c->prefer), "ane");
snprintf(c->drivers, sizeof(c->drivers), "apple-isp");
Expand Down Expand Up @@ -81,6 +85,17 @@ static int set_key(struct cdn_config *c, const char *k, const char *v, bool sett
else if (settings && !strcmp(k, "backend")) snprintf(c->backend, sizeof(c->backend), "%s", v);
else if (settings && !strcmp(k, "prefer")) snprintf(c->prefer, sizeof(c->prefer), "%s", v);
else if (settings && !strcmp(k, "threads") && num) c->threads = (int)d;
else if (settings && !strcmp(k, "strength") && num && d >= 0 && d <= 1) c->strength = (float)d;
else if (settings && !strcmp(k, "spatial") && num && d >= 0 && d <= 1) c->spatial = (float)d;
else if (settings && !strcmp(k, "temporal-frames") && num && d >= 2 && d <= 8 && d == (int)d)
c->temporal_frames = (int)d;
else if (settings && !strcmp(k, "vignette") && num && d >= 0 && d <= 1) c->picture.vignette = (float)d;
else if (settings && !strcmp(k, "brightness") && num && d >= -1 && d <= 1) c->picture.brightness = (float)d;
else if (settings && !strcmp(k, "contrast") && num && d >= 0.5 && d <= 2) c->picture.contrast = (float)d;
else if (settings && !strcmp(k, "saturation") && num && d >= 0 && d <= 2) c->picture.saturation = (float)d;
else if (settings && !strcmp(k, "warmth") && num && d >= -1 && d <= 1) c->picture.warmth = (float)d;
else if (settings && !strcmp(k, "tint") && num && d >= -1 && d <= 1) c->picture.tint = (float)d;
else if (settings && !strcmp(k, "sharpen") && num && d >= 0 && d <= 1) c->picture.sharpen = (float)d;
else if (settings && !strcmp(k, "drivers")) snprintf(c->drivers, sizeof(c->drivers), "%s", v);
else return -1;
return 0;
Expand Down Expand Up @@ -321,10 +336,14 @@ void cdn_config_print(const struct cdn_config *c)
fprintf(stderr,
"camera-denoise: board %s, mode %s, temporal %s (%s), low-light frame rate %s, "
"backend %s (prefer %s), "
"enter gain >= %u, exit < %u, noise %.2f/%.2f, fps <= %.0f, ramp %d/%d frames\n",
"enter gain >= %u, exit < %u, noise %.2f/%.2f, fps <= %.0f, ramp %d/%d frames, "
"strength %.2f, spatial %.2f, temporal frames %d; picture: vignette %.2f, brightness %.2f, "
"contrast %.2f, saturation %.2f, warmth %.2f, tint %.2f, sharpen %.2f\n",
*c->board ? c->board : "(no board data)", cdn_mode_name(c->mode),
c->temporal ? "on" : "off", cdn_temporal_auto(c) ? "auto" : cdn_temporal_backend(c), c->low_light_frame_rate ? "on" : "off", c->backend,
c->prefer, c->trig.enter_gain, c->trig.exit_gain, c->trig.enter_noise,
c->trig.exit_noise, c->trig.fps_threshold, c->trig.ramp_up_frames,
c->trig.ramp_down_frames);
c->trig.ramp_down_frames, c->strength, c->spatial, c->temporal_frames,
c->picture.vignette, c->picture.brightness, c->picture.contrast, c->picture.saturation,
c->picture.warmth, c->picture.tint, c->picture.sharpen);
}
13 changes: 13 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stddef.h>

#include "trigger.h"
#include "picture.h"

struct cdn_framing_cfg {
bool supported;
Expand Down Expand Up @@ -36,6 +37,18 @@ struct cdn_config {
char backend[32]; /* auto, cpu or a plugin name */
char prefer[64]; /* plugins "auto" tries before cpu */
int threads; /* 0: backend default */
/*
* Tuning: strength mixes the filtered picture with the camera's
* (1 = filtered only, 0.5 = half of each), spatial scales the 3x3
* smoothing that follows the temporal filter (0 = none, 1 = full),
* temporal_frames caps the running mean (fewer = less smear on motion,
* more grain left).
*/
float strength;
float spatial;
int temporal_frames;
/* the picture stage after the denoiser (picture.h): all neutral by default */
struct cdn_picture_cfg picture;
char drivers[128]; /* V4L2 drivers whose sources get a filter */
char board[64]; /* matched sensor:<name> or compatible, "" if none */
};
Expand Down
23 changes: 19 additions & 4 deletions src/cpu/denoise_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ struct cdn_cpu {
int shift, ashift;
int16_t ts_y, ts_c; /* spatial clip levels */
int strength8; /* 0..256 */
int age_max; /* 1..AGE_MAX: longest running mean - 1 (tuning) */
int spatial_q8; /* 0..256: spatial clip scale (tuning) */

const struct cdn_frame *in;
struct cdn_frame *out;
Expand Down Expand Up @@ -238,6 +240,9 @@ static void set_params(struct cdn_cpu *s)
/* spatial clip: about 2 sigma on luma, 3 on chroma (sigma = 1.75 med) */
s->ts_y = clamp16((med * 7 + 16) >> 5, 1, 64);
s->ts_c = clamp16((medc * 21 + 32) >> 6, 1, 64);
/* tuning: 0 turns the spatial smoothing off (every clip level 0) */
s->ts_y = (int16_t)((s->ts_y * s->spatial_q8) >> 8);
s->ts_c = (int16_t)((s->ts_c * s->spatial_q8) >> 8);
}

/* ---------------------------------------------------------------------- */
Expand Down Expand Up @@ -362,7 +367,7 @@ static void pass_temporal_y(struct cdn_cpu *s, int y0, int y1)
uint16_t km = weight_scalar(s, m, av); \
uint16_t ka = age_weight[age[X]]; \
uint16_t kk = km > ka ? km : ka; \
age[X] = km > KMAX / 2 ? 1 : age[X] + 1 > AGE_MAX ? AGE_MAX : age[X] + 1; \
age[X] = km > KMAX / 2 ? 1 : age[X] + 1 > s->age_max ? s->age_max : age[X] + 1; \
int16_t delta = (int16_t)((src[X] << 4) - hist[X]); \
int16_t nh = (int16_t)(hist[X] + qrdmulh(delta, (int16_t)kk)); \
hist[X] = nh; \
Expand All @@ -387,7 +392,7 @@ static void pass_temporal_y(struct cdn_cpu *s, int y0, int y1)
}
uint8x16_t rlo = vld1q_u8(tlo), rhi = vld1q_u8(thi);
uint16x8_t half = vdupq_n_u16(KMAX / 2);
uint8x8_t one = vdup_n_u8(1), amax = vdup_n_u8(AGE_MAX);
uint8x8_t one = vdup_n_u8(1), amax = vdup_n_u8((uint8_t)s->age_max);
for (; x + 8 <= w - 1; x += 8) {
/* 3x3 sum in u16 */
uint16x8_t m = vdupq_n_u16(0);
Expand Down Expand Up @@ -723,6 +728,8 @@ static void *cpu_create(int width, int height, int threads)
s->sig_y16 = 2 * 16;
s->sig_c16 = 1 * 16;
s->strength8 = 256;
s->age_max = AGE_MAX;
s->spatial_q8 = 256;

/*
* One thread by default: it costs the least CPU time. A 1080p frame
Expand Down Expand Up @@ -816,9 +823,18 @@ static void update_noise(struct cdn_cpu *s, const struct cdn_frame *in)

int cdn_cpu_process_ex(void *p, const struct cdn_frame *in, struct cdn_frame *out,
float strength, bool neon)
{
return cdn_cpu_process_tuned(p, in, out, NULL, strength, neon);
}

int cdn_cpu_process_tuned(void *p, const struct cdn_frame *in, struct cdn_frame *out,
const struct cdn_frame_info *info, float strength, bool neon)
{
struct cdn_cpu *s = p;

s->age_max = cdn_info_age_max(info);
s->spatial_q8 = cdn_info_spatial_q8(info);

if (in->width != s->w || in->height != s->h || out->width != s->w ||
out->height != s->h)
return -EINVAL;
Expand All @@ -845,8 +861,7 @@ int cdn_cpu_process_ex(void *p, const struct cdn_frame *in, struct cdn_frame *ou
static int cpu_process(void *p, const struct cdn_frame *in, struct cdn_frame *out,
const struct cdn_frame_info *info, float strength)
{
(void)info;
return cdn_cpu_process_ex(p, in, out, strength, true);
return cdn_cpu_process_tuned(p, in, out, info, strength, true);
}

static float cpu_noise_sigma(void *p)
Expand Down
3 changes: 3 additions & 0 deletions src/cpu/denoise_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ void cdn_cpu_destroy(void *state);
/* process with the NEON path on or off (tests compare the two) */
int cdn_cpu_process_ex(void *state, const struct cdn_frame *in,
struct cdn_frame *out, float strength, bool neon);
/* the same with the user's tuning from info (NULL: defaults) */
int cdn_cpu_process_tuned(void *state, const struct cdn_frame *in, struct cdn_frame *out,
const struct cdn_frame_info *info, float strength, bool neon);

#endif
2 changes: 1 addition & 1 deletion src/gpu/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout(set=0,binding=2,std430) buffer History { int hist[]; };
layout(set=0,binding=3,std430) buffer Scratch { int tmp[]; };
layout(set=0,binding=4,std430) writeonly buffer Output { uint dst[]; };
layout(set=0,binding=5,std430) buffer Params { int par[]; };
layout(push_constant) uniform Push { int w, h, sy, sc, oy, oc, strength, seed; } pc;
layout(push_constant) uniform Push { int w, h, sy, sc, oy, oc, strength, seed, spatial, agemax; } pc;
// History: Y, UV (Q4), age. Scratch: diff, horizontal signed sum, weight,
// temporal Y/UV, first spatial UV. All offsets are in 32-bit words.
#define N (pc.w * pc.h)
Expand Down
12 changes: 5 additions & 7 deletions src/gpu/denoise_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define PARAM_BYTES ((16 + 288) * sizeof(int32_t))
struct buffer { VkBuffer buf; VkDeviceMemory mem; VkDeviceSize size; void *map; bool coherent; };
struct imported { struct buffer b; dev_t dev; ino_t ino; };
struct push { int32_t w, h, sy, sc, oy, oc, strength, seed; };
struct push { int32_t w, h, sy, sc, oy, oc, strength, seed, spatial, agemax; };
struct gpu {
int w, h;
bool valid, dead, dma, can_dma;
Expand Down Expand Up @@ -392,27 +392,26 @@ static bool valid_output(struct gpu *s, struct cdn_frame *out)
out->stride_y >= s->w && out->stride_uv >= s->w;
}

static struct push parameters(struct gpu *s, float strength)
static struct push parameters(struct gpu *s, const struct cdn_frame_info *info, float strength)
{
if (!isfinite(strength)) strength = 0;
if (strength < 0) strength = 0;
if (strength > 1) strength = 1;
return (struct push){ s->w, s->h, s->w, s->w, 0, s->w*s->h,
(int32_t)(strength*256+0.5f), !s->valid };
(int32_t)(strength*256+0.5f), !s->valid, cdn_info_spatial_q8(info), cdn_info_age_max(info) };
}

static int gpu_process(void *p, const struct cdn_frame *in, struct cdn_frame *out,
const struct cdn_frame_info *info, float strength)
{
struct gpu *s = p;
(void)info;
if (!valid_output(s, out) || !in || !in->y || !in->uv || in->width != s->w ||
in->height != s->h || in->stride_y < s->w || in->stride_uv < s->w) return -EINVAL;
struct cdn_frame upload = { s->b[0].map, (uint8_t *)s->b[0].map+(size_t)s->w*s->h,
s->w, s->h, s->w, s->w };
copy_frame(in, &upload);
int rc = cache_sync(s, &s->b[0], false);
struct push pc = parameters(s, strength);
struct push pc = parameters(s, info, strength);
if (!rc) rc = execute(s, &s->b[0], &s->b[0], &pc, out, false);
if (!rc && !s->said_upload) {
fprintf(stderr, "camera-denoise: gpu: using one upload for mapped NV12 input\n");
Expand Down Expand Up @@ -457,7 +456,6 @@ static int gpu_dmabuf(void *p, const struct cdn_dmabuf *in, struct cdn_frame *ou
const struct cdn_frame_info *info, float strength)
{
struct gpu *s = p;
(void)info;
if (!valid_output(s, out)) return -EINVAL;
if (!s->dma || !in || in->y.fd < 0 || in->uv.fd < 0 || in->width != s->w || in->height != s->h ||
in->y.stride < (uint32_t)s->w || in->uv.stride < (uint32_t)s->w ||
Expand All @@ -478,7 +476,7 @@ static int gpu_dmabuf(void *p, const struct cdn_dmabuf *in, struct cdn_frame *ou
int rc = wait_producer(in->y.fd);
if (!rc && in->uv.fd != in->y.fd) rc = wait_producer(in->uv.fd);
if (rc) return -EAGAIN; // leave history intact; do not read an unfinished frame
struct push pc = parameters(s, strength);
struct push pc = parameters(s, info, strength);
pc.sy = (int32_t)in->y.stride; pc.sc = (int32_t)in->uv.stride;
pc.oy = (int32_t)in->y.offset; pc.oc = (int32_t)in->uv.offset;
if (!rc) rc = execute(s, y, uv, &pc, out, true);
Expand Down
4 changes: 2 additions & 2 deletions src/gpu/filter.comp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout(local_size_x=128) in;
layout(constant_id=0) const int PASS=0;

int spatial(int base, int x, int y, int dist, bool chroma) {
int c=tmp[base+y*pc.w+x], tt=par[chroma ? 11 : 10];
int c=tmp[base+y*pc.w+x], tt=(par[chroma ? 11 : 10]*pc.spatial)>>8;
int bo=chroma ? 2*dist : dist, height=chroma ? pc.h/2 : pc.h;
if (x<bo || x>=pc.w-bo || y<dist || y>=height-dist) return c;
if (!chroma) tt=max(mulq(tt,tmp[KY+y*pc.w+x]),(tt+3)>>2);
Expand Down Expand Up @@ -79,7 +79,7 @@ void main() {
for(int dy=-2;dy<=2;dy++) a+=tmp[HS+at(x,y+dy)];
int km=4095+max(ramp(m,par[2],par[3],par[4],par[5]),ramp(abs(a),par[6],par[7],par[8],par[9]));
int ag=hist[AGE+i], kk=max(km,32767/(ag+1));
hist[AGE+i]=km>16383 ? 1 : min(ag+1,7);
hist[AGE+i]=km>16383 ? 1 : min(ag+1,pc.agemax);
int nh=hist[HY+i]+mulq((src(x,y,false)<<4)-hist[HY+i],kk);
hist[HY+i]=nh; tmp[KY+i]=kk; tmp[TY+i]=sat((nh+8)>>4);
} else if(PASS==4) {
Expand Down
Loading