ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
stabilize.c
Go to the documentation of this file.
1#include <ascii-chat/common.h>
4#include <string.h>
5#include <stdlib.h>
6
7static _Thread_local ascii_stabilizer_t *g_stabilizer_target;
8
10 return g_stabilizer_target;
11}
12
14 ascii_stabilizer_t *previous = g_stabilizer_target;
15 g_stabilizer_target = state;
16 return previous;
17}
18
20 if (!state)
21 return;
22 SAFE_FREE(state->cells);
23 memset(state, 0, sizeof(*state));
24}
25
27 if (!state || !width || !height || width > SIZE_MAX / height || width * height > SIZE_MAX / sizeof(ascii_temporal_cell_t))
28 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid stabilization viewport");
29 if (state->cells && state->width == width && state->height == height && state->configuration == configuration)
30 return ASCIICHAT_OK;
32 if (!cells)
33 return SET_ERRNO(ERROR_MEMORY, "Cannot allocate stabilization history");
34 SAFE_FREE(state->cells);
35 state->cells = cells;
36 state->width = width;
37 state->height = height;
38 state->configuration = configuration;
39 state->last_timestamp_ns = 0;
40 state->elapsed_ns = UINT64_MAX;
41 return ASCIICHAT_OK;
42}
43
44uint32_t ascii_stabilizer_blend_color(uint32_t previous, uint32_t current, unsigned weight) {
45 uint32_t result = 0;
46 for (unsigned shift = 0; shift <= 16; shift += 8) {
47 unsigned old_channel = (previous >> shift) & 255;
48 unsigned new_channel = (current >> shift) & 255;
49 result |= (uint32_t)((old_channel * (256 - weight) + new_channel * weight + 128) / 256) << shift;
50 }
51 return result;
52}
53
54static unsigned source_color_motion(uint32_t previous, uint32_t current) {
55 unsigned difference = 0;
56 for (unsigned shift = 0; shift <= 16; shift += 8)
57 difference += (unsigned)abs((int)((previous >> shift) & 255) - (int)((current >> shift) & 255));
58 return difference / 3;
59}
60
61uint32_t ascii_stabilizer_choose(ascii_temporal_cell_t *cell, const uint8_t samples[16], uint32_t candidate, uint64_t candidate_error, uint64_t previous_error, uint32_t color, uint64_t elapsed_ns,
62 uint32_t *stabilized_color) {
63 unsigned motion = 0;
64 if (cell->valid) {
65 for (size_t i = 0; i < 16; ++i)
66 motion += (unsigned)abs((int)samples[i] - (int)cell->samples[i]);
67 motion /= 16;
68 }
69 uint32_t result = candidate;
70 uint32_t output_color = color;
71 if (cell->valid && motion <= ASCII_TEMPORAL_MOTION_LIMIT && elapsed_ns <= ASCII_TEMPORAL_MAX_INTERVAL_NS) {
72 uint64_t margin = candidate_error / 8 + 128;
73 if (previous_error != UINT64_MAX && (previous_error <= candidate_error || previous_error - candidate_error <= margin))
74 result = cell->glyph;
75 // Weight depends on elapsed time, avoiding a different response at each FPS.
76 unsigned weight = (unsigned)(elapsed_ns * 256 / (elapsed_ns + ASCII_TEMPORAL_COLOR_TIME_NS));
77 output_color = ascii_stabilizer_blend_color(cell->color, color, weight);
78 }
79 memcpy(cell->samples, samples, sizeof(cell->samples));
80 cell->glyph = result;
81 cell->color = output_color;
82 cell->valid = true;
83 if (stabilized_color)
84 *stabilized_color = output_color;
85 return result;
86}
87
88uint32_t ascii_stabilizer_glyph(ascii_temporal_cell_t *cell, const uint8_t samples[16], const int16_t rgb_samples[3][16], bool colored, uint32_t candidate, uint64_t candidate_error,
89 uint64_t previous_error, uint32_t previous_color, uint64_t elapsed_ns, uint32_t *color, uint32_t *background) {
90 unsigned motion = 0, color_motion = 0;
91 if (cell->valid)
92 for (int i = 0; i < 16; ++i)
93 motion += (unsigned)abs((int)samples[i] - (int)cell->samples[i]);
94 if (cell->valid && colored) {
95 for (int channel = 0; channel < 3; ++channel)
96 for (int i = 0; i < 16; ++i)
97 color_motion += (unsigned)abs(rgb_samples[channel][i] - (int)cell->rgb_samples[channel][i]);
98 color_motion /= 48;
99 }
100 uint32_t selected = candidate;
101 if (cell->valid && motion / 16 <= ASCII_TEMPORAL_MOTION_LIMIT && color_motion <= ASCII_TEMPORAL_MOTION_LIMIT && elapsed_ns <= ASCII_TEMPORAL_MAX_INTERVAL_NS) {
102 uint64_t margin = candidate_error / 8 + 128;
103 if (previous_error != UINT64_MAX && (previous_error <= candidate_error || previous_error - candidate_error <= margin))
104 selected = cell->glyph;
105 // Glyph choice is independent of color. Select the matching foreground
106 // before blending, instead of updating and restoring the whole cell.
107 uint32_t foreground = selected != candidate ? previous_color : *color;
108 unsigned weight = (unsigned)(elapsed_ns * 256 / (elapsed_ns + ASCII_TEMPORAL_COLOR_TIME_NS));
109 *color = ascii_stabilizer_blend_color(cell->color, foreground, weight);
110 *background = ascii_stabilizer_blend_color(cell->background, *background, weight);
111 }
112 memcpy(cell->samples, samples, sizeof(cell->samples));
113 for (int channel = 0; channel < 3; ++channel)
114 for (int i = 0; i < 16; ++i)
115 cell->rgb_samples[channel][i] = (uint8_t)rgb_samples[channel][i];
116 cell->background = *background;
117 cell->glyph = selected;
118 cell->color = *color;
119 cell->valid = true;
120 return selected;
121}
122
123void ascii_stabilizer_frame_row(ascii_frame_t *frame, ascii_color_t mode, bool background, const simd_cells_t *ops) {
124 if (frame->stabilizer && !frame->temporal_preselected) {
125 const uint64_t elapsed = frame->stabilizer->elapsed_ns;
126 const bool recent = elapsed <= ASCII_TEMPORAL_MAX_INTERVAL_NS;
127 const unsigned weight = recent ? (unsigned)(elapsed * 256 / (elapsed + ASCII_TEMPORAL_COLOR_TIME_NS)) : 256;
128 for (size_t x = 0; x < frame->width; ++x) {
129 ascii_temporal_cell_t *cell = &frame->stabilizer->cells[frame->row * frame->width + x];
130 uint8_t luma = frame->luma[x];
131 uint32_t glyph = frame->glyph[x];
132 uint32_t color = frame->packed[x];
133 if (cell->valid && recent && abs((int)luma - (int)cell->samples[0]) <= ASCII_TEMPORAL_MOTION_LIMIT) {
134 if (cell->glyph != glyph) {
135 // Only nearby brightness bins can win the hysteresis test. Searching
136 // outward avoids scanning the entire 64-entry palette for every cell.
137 unsigned bin = luma >> 2;
138 int best_delta = (int)luma - ((int)bin * 4 + 2);
139 uint64_t best_error = (uint64_t)(best_delta * best_delta);
140 uint64_t margin = best_error / 8 + 128;
141 for (unsigned distance = 1; distance < 64; ++distance) {
142 bool found = false;
143 for (int direction = -1; direction <= 1; direction += 2) {
144 int index = (int)bin + direction * (int)distance;
145 if (index < 0 || index >= 64 || frame->table[index] != cell->glyph)
146 continue;
147 int delta = (int)luma - (index * 4 + 2);
148 uint64_t error = (uint64_t)(delta * delta);
149 if (error <= best_error || error - best_error <= margin)
150 glyph = cell->glyph;
151 found = true;
152 }
153 // Bins farther than this cannot fall within the error margin.
154 int nearest_delta = (int)distance * 4 - 3;
155 if (found || (uint64_t)(nearest_delta * nearest_delta) > best_error + margin)
156 break;
157 }
158 }
159 if (mode == ASCII_COLOR_NONE || source_color_motion(cell->source_color, color) <= ASCII_TEMPORAL_MOTION_LIMIT)
160 color = ascii_stabilizer_blend_color(cell->color, color, weight);
161 }
162 cell->source_color = frame->packed[x];
163 cell->samples[0] = luma;
164 cell->glyph = frame->glyph[x] = glyph;
165 cell->color = frame->packed[x] = color;
166 cell->valid = true;
167 if (background)
168 frame->luma[x] = (uint8_t)((77u * (color >> 16) + 150u * ((color >> 8) & 255) + 29u * (color & 255) + 128u) >> 8);
169 }
170 if (!frame->dithered && (mode == ASCII_COLOR_16 || mode == ASCII_COLOR_256))
171 ops->quantize(frame->packed, frame->index, frame->width, mode == ASCII_COLOR_16 ? 16 : 256);
172 }
173}
174
176 if (frame->stabilizer) {
177 size_t top_row = frame->row * 2;
178 bool has_bottom = top_row + 1 < frame->source_height;
179 for (size_t x = 0; x < frame->width; ++x) {
180 uint32_t *pixels[] = {&frame->packed[x], &frame->index[x]};
181 for (size_t half = 0; half < (has_bottom ? 2u : 1u); ++half) {
182 uint32_t color = *pixels[half];
183 uint8_t luma = (uint8_t)((76u * (color >> 16) + 150u * ((color >> 8) & 255) + 29u * (color & 255)) >> 8);
184 uint8_t samples[16];
185 memset(samples, luma, sizeof(samples));
186 ascii_temporal_cell_t *cell = &frame->stabilizer->cells[(top_row + half) * frame->width + x];
187 uint64_t elapsed = frame->stabilizer->elapsed_ns;
188 if (cell->valid && mode != ASCII_COLOR_NONE && source_color_motion(cell->source_color, color) > ASCII_TEMPORAL_MOTION_LIMIT)
189 elapsed = UINT64_MAX;
190 cell->source_color = color;
191 ascii_stabilizer_choose(cell, samples, 0, 0, UINT64_MAX, color, elapsed, pixels[half]);
192 }
193 if (!has_bottom)
194 frame->index[x] = frame->packed[x];
195 }
196 if (mode == ASCII_COLOR_TRUE) {
197 memcpy(frame->fg, frame->packed, frame->width * sizeof(uint32_t));
198 memcpy(frame->bg, frame->index, frame->width * sizeof(uint32_t));
199 } else if (mode != ASCII_COLOR_NONE) {
200 ops->quantize(frame->packed, frame->fg, frame->width, mode == ASCII_COLOR_16 ? 16 : 256);
201 ops->quantize(frame->index, frame->bg, frame->width, mode == ASCII_COLOR_16 ? 16 : 256);
202 }
203 ops->halfblock(frame->packed, frame->index, frame->glyph, frame->fg, frame->bg, frame->width, (unsigned)mode, frame->table);
204 }
205}
#define SET_ERRNO(code,...)
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define ascii_stabilizer_prepare
#define ascii_stabilizer_reset
#define ASCII_TEMPORAL_COLOR_TIME_NS
#define ascii_stabilizer_choose
#define ascii_stabilizer_set_target
#define ASCII_TEMPORAL_MAX_INTERVAL_NS
#define ASCII_TEMPORAL_MOTION_LIMIT
static bool size_t index
Definition queries.h:8
uint32_t ascii_stabilizer_glyph(ascii_temporal_cell_t *cell, const uint8_t samples[16], const int16_t rgb_samples[3][16], bool colored, uint32_t candidate, uint64_t candidate_error, uint64_t previous_error, uint32_t previous_color, uint64_t elapsed_ns, uint32_t *color, uint32_t *background)
Definition stabilize.c:88
uint32_t ascii_stabilizer_blend_color(uint32_t previous, uint32_t current, unsigned weight)
Definition stabilize.c:44
void ascii_stabilizer_halfrow(ascii_frame_t *frame, ascii_color_t mode, const simd_cells_t *ops)
Definition stabilize.c:175
void ascii_stabilizer_frame_row(ascii_frame_t *frame, ascii_color_t mode, bool background, const simd_cells_t *ops)
Definition stabilize.c:123
ascii_stabilizer_t * ascii_stabilizer_target(void)
Definition stabilize.c:9
🔤 String Manipulation and Shell Escaping Utilities
When code needs to work with ascii frame, it keeps output, output_target, width, source_height here i...
uint64_t last_timestamp_ns
Definition stabilize.h:29
ascii_temporal_cell_t * cells
Definition stabilize.h:25
uint64_t elapsed_ns
Definition stabilize.h:28
uint64_t configuration
Definition stabilize.h:27
uint8_t samples[16]
Definition stabilize.h:14
uint8_t rgb_samples[3][16]
Definition stabilize.h:15
The useful part of simd cells is in luminance: together they describe what is pending,...
Definition cells.h:14
void(* halfblock)(const uint32_t *top, const uint32_t *bottom, uint32_t *glyph, uint32_t *fg, uint32_t *bg, size_t count, unsigned mode, const uint32_t keys[5])
Definition cells.h:19
void(* quantize)(const uint32_t *rgb, uint32_t *out, size_t count, unsigned colors)
Definition cells.h:18