diff --git a/data/denoise.conf b/data/denoise.conf index ee726f3..21d4cde 100644 --- a/data/denoise.conf +++ b/data/denoise.conf @@ -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 diff --git a/meson.build b/meson.build index 781f9ea..96d618b 100644 --- a/meson.build +++ b/meson.build @@ -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]) @@ -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)) diff --git a/src/backend.h b/src/backend.h index 76adf2a..d84b22d 100644 --- a/src/backend.h +++ b/src/backend.h @@ -18,7 +18,12 @@ #include #include -#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. */ @@ -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; @@ -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; @@ -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 diff --git a/src/config.c b/src/config.c index 475e29c..e5cf205 100644 --- a/src/config.c +++ b/src/config.c @@ -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"); @@ -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; @@ -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); } diff --git a/src/config.h b/src/config.h index 630f7e2..9c2dcf8 100644 --- a/src/config.h +++ b/src/config.h @@ -6,6 +6,7 @@ #include #include "trigger.h" +#include "picture.h" struct cdn_framing_cfg { bool supported; @@ -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: or compatible, "" if none */ }; diff --git a/src/cpu/denoise_cpu.c b/src/cpu/denoise_cpu.c index dbd2b6f..2df3faa 100644 --- a/src/cpu/denoise_cpu.c +++ b/src/cpu/denoise_cpu.c @@ -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; @@ -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); } /* ---------------------------------------------------------------------- */ @@ -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; \ @@ -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); @@ -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 @@ -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; @@ -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) diff --git a/src/cpu/denoise_cpu.h b/src/cpu/denoise_cpu.h index dc5c6d4..74f587e 100644 --- a/src/cpu/denoise_cpu.h +++ b/src/cpu/denoise_cpu.h @@ -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 diff --git a/src/gpu/common.glsl b/src/gpu/common.glsl index 4127731..1abd75f 100644 --- a/src/gpu/common.glsl +++ b/src/gpu/common.glsl @@ -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) diff --git a/src/gpu/denoise_gpu.c b/src/gpu/denoise_gpu.c index 0b51d78..ab9705b 100644 --- a/src/gpu/denoise_gpu.c +++ b/src/gpu/denoise_gpu.c @@ -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; @@ -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"); @@ -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 || @@ -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); diff --git a/src/gpu/filter.comp b/src/gpu/filter.comp index 52337ce..02c3e96 100644 --- a/src/gpu/filter.comp +++ b/src/gpu/filter.comp @@ -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=pc.w-bo || y=height-dist) return c; if (!chroma) tt=max(mulq(tt,tmp[KY+y*pc.w+x]),(tt+3)>>2); @@ -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) { diff --git a/src/main.c b/src/main.c index 7bbfcca..29d8077 100644 --- a/src/main.c +++ b/src/main.c @@ -39,6 +39,7 @@ #include "config.h" #include "gain.h" #include "effects.h" +#include "picture.h" #include "pipewire-frame.h" #include "trigger.h" @@ -103,6 +104,7 @@ struct cam { const struct cdn_backend_ops *ops; void *bstate; struct cdn_effects *effects; + struct cdn_picture *picture; /* vignette and picture controls, last */ struct ce_config effect_cfg; /* the low-light stage on top of the temporal one, if a plugin runs */ const struct cdn_backend_ops *ml_ops; @@ -427,7 +429,11 @@ static void on_in_process(void *data) bool temporal = c->app->cfg.temporal && c->app->cfg.mode != CDN_MODE_OFF; if ((temporal || strength > 0) && c->bstate) { - struct cdn_frame_info info = { gain, exposure, ih ? (uint64_t)ih->pts : 0 }; + const struct cdn_config *cfg = &c->app->cfg; + struct cdn_frame_info info = { gain, exposure, ih ? (uint64_t)ih->pts : 0, 1, + (uint8_t)cfg->temporal_frames, + (uint16_t)(cfg->spatial * 256.0f + 0.5f) }; + float user = cfg->strength; bool boost = temporal && c->ml_state && strength > 0; double t0 = mono_ms(); size_t need = (size_t)w * h * 3 / 2; @@ -461,14 +467,14 @@ static void on_in_process(void *data) /* temporal result B, then the low-light stage blends a*M(B) over it */ struct cdn_frame sf = { c->stage, c->stage + (size_t)w * h, w, h, w, w }; - int rc = temporal_process(c, &fi, &sf, &info, 1.0f, dma_in); + int rc = temporal_process(c, &fi, &sf, &info, user, dma_in); if (rc == -EAGAIN) goto drop_frame; if (rc) cdn_frame_copy(&fi, &sf); - if (c->ml_ops->process(c->ml_state, &sf, &fo, &info, strength)) + if (c->ml_ops->process(c->ml_state, &sf, &fo, &info, strength * user)) cdn_frame_copy(&sf, &fo); c->boosted++; } else { - int rc = temporal_process(c, &fi, &fo, &info, temporal ? 1.0f : strength, dma_in); + int rc = temporal_process(c, &fi, &fo, &info, (temporal ? 1.0f : strength) * user, dma_in); if (rc == -EAGAIN) goto drop_frame; if (rc) cdn_frame_copy(&fi, &fo); } @@ -483,6 +489,14 @@ static void on_in_process(void *data) c->ops->reset(c->bstate); } + /* The picture stage (vignette, brightness ... sharpen) on the finished frame. */ + if (!cdn_picture_neutral(&c->app->cfg.picture)) { + if (!c->picture) + c->picture = cdn_picture_create(); + if (c->picture) + cdn_picture_apply(c->picture, &c->app->cfg.picture, &fo); + } + uint64_t result_pts = ih && ih->pts > 0 ? (uint64_t)ih->pts : 0; uint64_t result_seq = ih ? ih->seq : c->frames; bool ready = true; @@ -1021,6 +1035,7 @@ static void cam_destroy(struct cam *c) cdn_noise_tracker_free(&c->noise); free(c->copy); free(c->stage); + cdn_picture_destroy(c->picture); spa_list_remove(&c->link); free(c); } diff --git a/src/picture.c b/src/picture.c new file mode 100644 index 0000000..d752f1e --- /dev/null +++ b/src/picture.c @@ -0,0 +1,183 @@ +// SPDX-License-Identifier: MIT +#include +#include +#include +#include + +#include "picture.h" + +#define GAIN_ONE 4096 /* Q12 */ + +struct cdn_picture { + int w, h; + float vignette; + uint16_t *gy; /* luma gain map, w * h, Q12 */ + uint16_t *guv; /* chroma gain map, (w / 2) * (h / 2), Q12 */ + struct cdn_picture_cfg lut_cfg; + bool lut_valid; + uint8_t ylut[256]; + int16_t sat_q8; /* saturation, Q8 */ + int16_t u_off, v_off; /* warmth / tint, chroma levels */ + uint8_t *rows; /* sharpen: 3 luma rows before the pass */ + int rows_w; +}; + +void cdn_picture_defaults(struct cdn_picture_cfg *p) +{ + *p = (struct cdn_picture_cfg){ .contrast = 1.0f, .saturation = 1.0f }; +} + +bool cdn_picture_neutral(const struct cdn_picture_cfg *p) +{ + return p->vignette == 0 && p->brightness == 0 && p->contrast == 1 && p->saturation == 1 && + p->warmth == 0 && p->tint == 0 && p->sharpen == 0; +} + +struct cdn_picture *cdn_picture_create(void) +{ + return calloc(1, sizeof(struct cdn_picture)); +} + +void cdn_picture_destroy(struct cdn_picture *s) +{ + if (!s) + return; + free(s->gy); + free(s->guv); + free(s->rows); + free(s); +} + +static inline uint8_t clamp8(int v) +{ + return v < 0 ? 0 : v > 255 ? 255 : (uint8_t)v; +} + +/* Gain 1 + vignette * r^2, r = 1 in the corners: a cos^4 falloff to first order. */ +static int build_gain(struct cdn_picture *s, int w, int h, float vignette) +{ + int cw = w / 2, ch = h / 2; + uint16_t *gy = malloc((size_t)w * h * sizeof(*gy)); + uint16_t *guv = malloc((size_t)(cw ? cw : 1) * (ch ? ch : 1) * sizeof(*guv)); + float cx = (w - 1) * 0.5f, cy = (h - 1) * 0.5f, norm = 1.0f / (cx * cx + cy * cy + 1e-6f); + + if (!gy || !guv) { + free(gy); + free(guv); + return -ENOMEM; + } + for (int y = 0; y < h; y++) + for (int x = 0; x < w; x++) { + float dx = x - cx, dy = y - cy; + gy[(size_t)y * w + x] = (uint16_t)lrintf(GAIN_ONE * (1.0f + vignette * (dx * dx + dy * dy) * norm)); + } + for (int y = 0; y < ch; y++) + for (int x = 0; x < cw; x++) { + float dx = 2 * x + 0.5f - cx, dy = 2 * y + 0.5f - cy; + guv[(size_t)y * cw + x] = (uint16_t)lrintf(GAIN_ONE * (1.0f + vignette * (dx * dx + dy * dy) * norm)); + } + free(s->gy); + free(s->guv); + s->gy = gy; + s->guv = guv; + s->w = w; + s->h = h; + s->vignette = vignette; + return 0; +} + +static void build_lut(struct cdn_picture *s, const struct cdn_picture_cfg *p) +{ + for (int i = 0; i < 256; i++) + s->ylut[i] = clamp8((int)lrintf(128.0f + (i - 128) * p->contrast + p->brightness * 64.0f)); + s->sat_q8 = (int16_t)lrintf(p->saturation * 256.0f); + /* warmer = more red (Cr), less blue (Cb); magenta = more of both */ + s->u_off = (int16_t)lrintf(-p->warmth * 10.0f + p->tint * 8.0f); + s->v_off = (int16_t)lrintf(p->warmth * 10.0f + p->tint * 8.0f); + s->lut_cfg = *p; + s->lut_valid = true; +} + +/* Unsharp mask with a 3x3 box, on the luma as the earlier passes left it. */ +static int sharpen(struct cdn_picture *s, struct cdn_frame *f, float amount) +{ + int w = f->width, h = f->height, k = (int)lrintf(amount * 1.5f * 256.0f); + uint8_t *r[3]; + + if (h < 3 || w < 3) + return 0; + if (s->rows_w < w) { + free(s->rows); + s->rows = malloc((size_t)3 * w); + s->rows_w = s->rows ? w : 0; + if (!s->rows) + return -ENOMEM; + } + r[0] = s->rows; + r[1] = s->rows + w; + r[2] = s->rows + 2 * (size_t)w; + memcpy(r[0], f->y, w); + memcpy(r[1], f->y + f->stride_y, w); + for (int y = 1; y < h - 1; y++) { + uint8_t *above = r[(y - 1) % 3], *mid = r[y % 3], *below = r[(y + 1) % 3]; + uint8_t *out = f->y + (size_t)y * f->stride_y; + + memcpy(below, f->y + (size_t)(y + 1) * f->stride_y, w); + for (int x = 1; x < w - 1; x++) { + int sum = above[x - 1] + above[x] + above[x + 1] + mid[x - 1] + mid[x] + mid[x + 1] + + below[x - 1] + below[x] + below[x + 1]; + int detail = mid[x] * 9 - sum; /* 9 * (pixel - mean) */ + out[x] = clamp8(mid[x] + (detail * k) / (9 * 256)); + } + } + return 0; +} + +int cdn_picture_apply(struct cdn_picture *s, const struct cdn_picture_cfg *p, struct cdn_frame *f) +{ + int w = f->width, h = f->height, cw = w / 2, ch = h / 2; + bool vig = p->vignette > 0, lut, chroma; + + if (cdn_picture_neutral(p)) + return 0; + if (vig && (s->w != w || s->h != h || s->vignette != p->vignette || !s->gy) && + build_gain(s, w, h, p->vignette)) + return -ENOMEM; + if (!s->lut_valid || memcmp(&s->lut_cfg, p, sizeof(*p))) + build_lut(s, p); + lut = p->brightness != 0 || p->contrast != 1; + chroma = vig || p->saturation != 1 || p->warmth != 0 || p->tint != 0; + + if (vig || lut) + for (int y = 0; y < h; y++) { + uint8_t *row = f->y + (size_t)y * f->stride_y; + const uint16_t *g = vig ? s->gy + (size_t)y * w : NULL; + + for (int x = 0; x < w; x++) { + int v = row[x]; + + if (g) + v = clamp8(16 + (((v - 16) * g[x]) >> 12)); + row[x] = s->ylut[v]; + } + } + if (chroma) + for (int y = 0; y < ch; y++) { + uint8_t *row = f->uv + (size_t)y * f->stride_uv; + const uint16_t *g = vig ? s->guv + (size_t)y * cw : NULL; + + for (int x = 0; x < cw; x++) { + int u = row[2 * x] - 128, v = row[2 * x + 1] - 128; + + if (g) { + u = (u * g[x]) >> 12; + v = (v * g[x]) >> 12; + } + row[2 * x] = clamp8(128 + ((u * s->sat_q8) >> 8) + s->u_off); + row[2 * x + 1] = clamp8(128 + ((v * s->sat_q8) >> 8) + s->v_off); + } + } + if (p->sharpen > 0) + return sharpen(s, f, p->sharpen); + return 0; +} diff --git a/src/picture.h b/src/picture.h new file mode 100644 index 0000000..d8737f8 --- /dev/null +++ b/src/picture.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: MIT */ +#ifndef CDN_PICTURE_H +#define CDN_PICTURE_H +/* + * The picture stage: the last pass over each NV12 output frame, after the + * denoiser (and on frames it passes through). Lens-shading compensation + * for the dark corners the camera's own processing leaves, then the usual + * picture controls. Every setting at its default leaves the frame as it is + * and the pass is skipped. + */ +#include +#include + +#include "backend.h" + +struct cdn_picture_cfg { + float vignette; /* 0..1: corner brightening, 1 = twice the light in the corners */ + float brightness; /* -1..1: luma offset, 1 = +64 levels */ + float contrast; /* 0.5..2: around mid grey */ + float saturation; /* 0..2 */ + float warmth; /* -1..1: blue <-> orange */ + float tint; /* -1..1: green <-> magenta */ + float sharpen; /* 0..1: unsharp mask on luma, 1 = strong */ +}; + +void cdn_picture_defaults(struct cdn_picture_cfg *p); +bool cdn_picture_neutral(const struct cdn_picture_cfg *p); + +struct cdn_picture; +struct cdn_picture *cdn_picture_create(void); +void cdn_picture_destroy(struct cdn_picture *s); +/* In place. 0, or -ENOMEM (the frame is then left as it is). */ +int cdn_picture_apply(struct cdn_picture *s, const struct cdn_picture_cfg *p, struct cdn_frame *f); + +#endif diff --git a/tests/backend_fixture.c b/tests/backend_fixture.c index 8ddb348..e95f8d7 100644 --- a/tests/backend_fixture.c +++ b/tests/backend_fixture.c @@ -9,7 +9,7 @@ static int process(void *s, const struct cdn_frame *i, struct cdn_frame *o, static void destroy(void *s) { (void)s; } static const struct cdn_backend_ops ops = { #ifdef BAD_ABI - .abi = 0, + .abi = CDN_BACKEND_ABI - 1, /* a plugin built for the previous ABI */ #else .abi = CDN_BACKEND_ABI, #endif diff --git a/tests/test_config.c b/tests/test_config.c index 3b3a2fe..7b0d5a6 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -85,6 +85,49 @@ int main(int argc, char **argv) fclose(f); CHECK(cdn_config_load(&c, compat, boards, s1, 2) != 0); + /* the tuning keys, and their ranges */ + f = fopen(user, "w"); + fputs("[denoise]\nstrength = 0.5\nspatial = 0.25\ntemporal-frames = 4\n", f); + fclose(f); + CHECK(cdn_config_load(&c, compat, boards, s1, 2) == 0); + CHECK(c.strength == 0.5f && c.spatial == 0.25f && c.temporal_frames == 4); + { + static const char *const out_of_range[] = { + "strength = 1.5", "strength = -0.1", "spatial = 2", "temporal-frames = 1", + "temporal-frames = 9", "temporal-frames = 3.5", "strength = much", + }; + + for (unsigned int i = 0; i < sizeof(out_of_range) / sizeof(out_of_range[0]); i++) { + f = fopen(user, "w"); + fprintf(f, "[denoise]\n%s\n", out_of_range[i]); + fclose(f); + CHECK(cdn_config_load(&c, compat, boards, s1, 2) != 0); + } + } + + /* the picture keys, and their ranges */ + f = fopen(user, "w"); + fputs("[denoise]\nvignette = 0.5\nbrightness = -0.25\ncontrast = 1.5\nsaturation = 2\n" + "warmth = 0.5\ntint = -1\nsharpen = 0.5\n", f); + fclose(f); + CHECK(cdn_config_load(&c, compat, boards, s1, 2) == 0); + CHECK(c.picture.vignette == 0.5f && c.picture.brightness == -0.25f && + c.picture.contrast == 1.5f && c.picture.saturation == 2 && c.picture.warmth == 0.5f && + c.picture.tint == -1 && c.picture.sharpen == 0.5f); + { + static const char *const out_of_range[] = { + "vignette = 1.1", "brightness = -2", "contrast = 0.4", "contrast = 2.5", + "saturation = -0.1", "warmth = 1.5", "tint = -1.5", "sharpen = 2", + }; + + for (unsigned int i = 0; i < sizeof(out_of_range) / sizeof(out_of_range[0]); i++) { + f = fopen(user, "w"); + fprintf(f, "[denoise]\n%s\n", out_of_range[i]); + fclose(f); + CHECK(cdn_config_load(&c, compat, boards, s1, 2) != 0); + } + } + const char *s2[] = { bad }; CHECK(cdn_config_load(&c, compat, boards, s2, 1) != 0); diff --git a/tests/test_cpu.c b/tests/test_cpu.c index 98dd3fa..691ffe2 100644 --- a/tests/test_cpu.c +++ b/tests/test_cpu.c @@ -98,6 +98,37 @@ int main(void) fprintf(stderr, "ghosting: trail mse %.1f > raw %.1f\n", ghost_den / 25, ghost_raw / 25); fails++; } + /* The user's tuning: NEON and scalar still agree, and a lighter setting + * (3-frame mean, quarter spatial smoothing) removes less than the default. */ + { + void *ta = cdn_cpu_create(W, H, 1), *tb = cdn_cpu_create(W, H, 3); + struct cdn_frame_info ti = { .tuned = 1, .temporal_frames = 3, .spatial_q8 = 64 }; + double tuned_mse = 0; + for (int i = 0; i < 40; i++) { + struct cdn_frame fi = { n, n + W * H, W, H, W, W }; + struct cdn_frame fa = { o1, o1 + W * H, W, H, W, W }; + struct cdn_frame fb = { o2, o2 + W * H, W, H, W, W }; + truth(t, i); + noisy(n, t, 8.0); + cdn_cpu_process_tuned(ta, &fi, &fa, &ti, 1.0f, false); + cdn_cpu_process_tuned(tb, &fi, &fb, &ti, 1.0f, true); + if (memcmp(o1, o2, fs)) { + fprintf(stderr, "tuned frame %d: scalar and NEON differ\n", i); + fails++; + break; + } + if (i >= 15) + tuned_mse += mse_y(o1, t, 0, W, 0, 50); + } + double tuned_db = 10 * log10(raw_mse / tuned_mse); + printf("tuned (3 frames, spatial 0.25): %.2f dB better (default %.2f dB)\n", tuned_db, gain_db); + if (!(tuned_db > 1 && tuned_db < gain_db - 1)) { + fprintf(stderr, "tuning did not lighten the filter: %.2f dB vs %.2f dB\n", tuned_db, gain_db); + fails++; + } + cdn_cpu_destroy(ta); + cdn_cpu_destroy(tb); + } cdn_cpu_destroy(sa); cdn_cpu_destroy(sb); free(t); free(n); free(o1); free(o2); diff --git a/tests/test_gpu.c b/tests/test_gpu.c index 4313e2e..ad9340f 100644 --- a/tests/test_gpu.c +++ b/tests/test_gpu.c @@ -47,6 +47,10 @@ static int run(const struct cdn_backend_ops *gpu, const struct cdn_backend_dmabu float strength = frame < 16 ? 1 : frame < 24 ? 0 : frame < 32 ? 0.375f : 1; if (frame == 33) strength = -0.5f; if (frame == 34) strength = 1.5f; + // The user's tuning (denoise.conf strength/spatial/temporal-frames) must stay bit-exact too. + if (frame >= 40) + info = (struct cdn_frame_info){ .tuned = 1, .temporal_frames = frame < 56 ? 3 : 5, + .spatial_q8 = frame < 56 ? 0 : 160 }; if (frame == 16) { struct cdn_dmabuf unavailable = { w,h,{-1,0,(uint32_t)w},{-1,0,(uint32_t)w} }; if (!dma || dma->abi != CDN_DMABUF_ABI || diff --git a/tests/test_picture.c b/tests/test_picture.c new file mode 100644 index 0000000..020b385 --- /dev/null +++ b/tests/test_picture.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: MIT +#include +#include +#include + +#include "../src/picture.h" + +static int fails; +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #c); fails++; } } while (0) + +#define W 64 +#define H 48 + +static void fill(uint8_t *y, uint8_t *uv, int luma, int u, int v) +{ + memset(y, luma, W * H); + for (int i = 0; i < W * H / 2; i += 2) { + uv[i] = (uint8_t)u; + uv[i + 1] = (uint8_t)v; + } +} + +int main(void) +{ + static uint8_t y[W * H], uv[W * H / 2]; + struct cdn_frame f = { y, uv, W, H, W, W }; + struct cdn_picture_cfg p; + struct cdn_picture *s = cdn_picture_create(); + + CHECK(s); + cdn_picture_defaults(&p); + CHECK(cdn_picture_neutral(&p)); + + /* neutral: untouched */ + fill(y, uv, 100, 140, 120); + CHECK(cdn_picture_apply(s, &p, &f) == 0); + CHECK(y[0] == 100 && y[W * H / 2 + W / 2] == 100 && uv[0] == 140 && uv[1] == 120); + + /* vignette: centre about unchanged, corners brighter, twice the signal above black at 1 */ + p.vignette = 1; + CHECK(!cdn_picture_neutral(&p)); + CHECK(cdn_picture_apply(s, &p, &f) == 0); + CHECK(abs(y[(H / 2) * W + W / 2] - 100) <= 1); + CHECK(y[0] >= 180 && y[0] <= 184); /* 16 + 84 * 2 */ + CHECK(y[W * H - 1] == y[0]); /* symmetric */ + CHECK(uv[0] > 140 && uv[1] < 120); /* chroma scaled with the light */ + + /* contrast / brightness / saturation / warmth */ + cdn_picture_defaults(&p); + p.brightness = 0.25f; + fill(y, uv, 100, 128, 128); + cdn_picture_apply(s, &p, &f); + CHECK(y[5] == 116); + cdn_picture_defaults(&p); + p.contrast = 2; + fill(y, uv, 100, 128, 128); + cdn_picture_apply(s, &p, &f); + CHECK(y[5] == 72); + cdn_picture_defaults(&p); + p.saturation = 0; + fill(y, uv, 100, 160, 90); + cdn_picture_apply(s, &p, &f); + CHECK(uv[0] == 128 && uv[1] == 128 && y[5] == 100); + cdn_picture_defaults(&p); + p.warmth = 1; + fill(y, uv, 100, 128, 128); + cdn_picture_apply(s, &p, &f); + CHECK(uv[0] < 128 && uv[1] > 128); + + /* sharpen: a flat frame stays flat, an edge gets steeper */ + cdn_picture_defaults(&p); + p.sharpen = 1; + fill(y, uv, 100, 128, 128); + cdn_picture_apply(s, &p, &f); + CHECK(y[10 * W + 10] == 100); + for (int r = 0; r < H; r++) + for (int c = 0; c < W; c++) + y[r * W + c] = c < W / 2 ? 80 : 160; + cdn_picture_apply(s, &p, &f); + CHECK(y[10 * W + W / 2 - 1] < 80 && y[10 * W + W / 2] > 160); + CHECK(y[10 * W + 5] == 80); + + cdn_picture_destroy(s); + if (fails) + return 1; + printf("picture: ok\n"); + return 0; +}