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 File Reference

Go to the source code of this file.

Functions

ascii_stabilizer_t * ascii_stabilizer_target (void)
ascii_stabilizer_t * ascii_stabilizer_set_target (ascii_stabilizer_t *state)
void ascii_stabilizer_reset (ascii_stabilizer_t *state)
asciichat_error_t ascii_stabilizer_prepare (ascii_stabilizer_t *state, size_t width, size_t height, uint64_t configuration)
uint32_t ascii_stabilizer_blend_color (uint32_t previous, uint32_t current, unsigned weight)
uint32_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, uint32_t *stabilized_color)
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)
void ascii_stabilizer_frame_row (ascii_frame_t *frame, ascii_color_t mode, bool background, const simd_cells_t *ops)
void ascii_stabilizer_halfrow (ascii_frame_t *frame, ascii_color_t mode, const simd_cells_t *ops)

Function Documentation

◆ ascii_stabilizer_blend_color()

uint32_t ascii_stabilizer_blend_color ( uint32_t previous,
uint32_t current,
unsigned weight )

Definition at line 44 of file stabilize.c.

44 {
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}
unsigned int uint32_t

Referenced by ascii_stabilizer_choose(), ascii_stabilizer_frame_row(), and ascii_stabilizer_glyph().

◆ ascii_stabilizer_choose()

uint32_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,
uint32_t * stabilized_color )

Choose a glyph using current and previous candidate errors and patch motion.

Definition at line 61 of file stabilize.c.

62 {
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}
unsigned long long uint64_t
#define ASCII_TEMPORAL_COLOR_TIME_NS
#define ASCII_TEMPORAL_MAX_INTERVAL_NS
#define ASCII_TEMPORAL_MOTION_LIMIT
uint32_t ascii_stabilizer_blend_color(uint32_t previous, uint32_t current, unsigned weight)
Definition stabilize.c:44
uint8_t samples[16]
Definition stabilize.h:14

References ascii_stabilizer_blend_color(), ASCII_TEMPORAL_COLOR_TIME_NS, ASCII_TEMPORAL_MAX_INTERVAL_NS, ASCII_TEMPORAL_MOTION_LIMIT, ascii_temporal_cell_t::color, ascii_temporal_cell_t::glyph, ascii_temporal_cell_t::samples, and ascii_temporal_cell_t::valid.

◆ ascii_stabilizer_frame_row()

void ascii_stabilizer_frame_row ( ascii_frame_t * frame,
ascii_color_t mode,
bool background,
const simd_cells_t * ops )

Definition at line 123 of file stabilize.c.

123 {
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}
unsigned char uint8_t
static bool size_t index
Definition queries.h:8
ascii_temporal_cell_t * cells
Definition stabilize.h:25
uint64_t elapsed_ns
Definition stabilize.h:28
void(* quantize)(const uint32_t *rgb, uint32_t *out, size_t count, unsigned colors)
Definition cells.h:18

References ASCII_COLOR_16, ASCII_COLOR_256, ASCII_COLOR_NONE, ascii_stabilizer_blend_color(), ASCII_TEMPORAL_COLOR_TIME_NS, ASCII_TEMPORAL_MAX_INTERVAL_NS, ASCII_TEMPORAL_MOTION_LIMIT, ascii_stabilizer_t::cells, ascii_temporal_cell_t::color, ascii_frame_t::dithered, ascii_stabilizer_t::elapsed_ns, ascii_frame_t::glyph, ascii_temporal_cell_t::glyph, ascii_frame_t::index, index, ascii_frame_t::luma, ascii_frame_t::packed, simd_cells_t::quantize, ascii_frame_t::row, ascii_temporal_cell_t::samples, ascii_temporal_cell_t::source_color, ascii_frame_t::stabilizer, ascii_frame_t::table, ascii_frame_t::temporal_preselected, ascii_temporal_cell_t::valid, and ascii_frame_t::width.

Referenced by ascii_frame_emit_row().

◆ ascii_stabilizer_glyph()

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 at line 88 of file stabilize.c.

89 {
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}
uint8_t rgb_samples[3][16]
Definition stabilize.h:15

References ascii_stabilizer_blend_color(), ASCII_TEMPORAL_COLOR_TIME_NS, ASCII_TEMPORAL_MAX_INTERVAL_NS, ASCII_TEMPORAL_MOTION_LIMIT, ascii_temporal_cell_t::background, ascii_temporal_cell_t::color, ascii_temporal_cell_t::glyph, ascii_temporal_cell_t::rgb_samples, ascii_temporal_cell_t::samples, and ascii_temporal_cell_t::valid.

Referenced by ascii_glyph_render().

◆ ascii_stabilizer_halfrow()

void ascii_stabilizer_halfrow ( ascii_frame_t * frame,
ascii_color_t mode,
const simd_cells_t * ops )

Definition at line 175 of file stabilize.c.

175 {
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 ascii_stabilizer_choose
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

References ASCII_COLOR_16, ASCII_COLOR_NONE, ASCII_COLOR_TRUE, ascii_stabilizer_choose, ASCII_TEMPORAL_MOTION_LIMIT, ascii_frame_t::bg, ascii_stabilizer_t::cells, ascii_stabilizer_t::elapsed_ns, ascii_frame_t::fg, ascii_frame_t::glyph, simd_cells_t::halfblock, ascii_frame_t::index, ascii_frame_t::packed, simd_cells_t::quantize, ascii_frame_t::row, ascii_temporal_cell_t::source_color, ascii_frame_t::source_height, ascii_frame_t::stabilizer, ascii_frame_t::table, ascii_temporal_cell_t::valid, and ascii_frame_t::width.

Referenced by ascii_frame_emit_halfrow().

◆ ascii_stabilizer_prepare()

asciichat_error_t ascii_stabilizer_prepare ( ascii_stabilizer_t * state,
size_t width,
size_t height,
uint64_t configuration )

Prepare history for a viewport and rendering contract, resetting it on change.

Definition at line 26 of file stabilize.c.

26 {
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}
#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
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
@ ERROR_MEMORY
Definition error_codes.h:55
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
uint64_t last_timestamp_ns
Definition stabilize.h:29
uint64_t configuration
Definition stabilize.h:27

References ASCIICHAT_OK, ascii_stabilizer_t::cells, cells, ascii_stabilizer_t::configuration, ascii_stabilizer_t::elapsed_ns, ERROR_INVALID_PARAM, ERROR_MEMORY, ascii_stabilizer_t::height, height, ascii_stabilizer_t::last_timestamp_ns, SAFE_CALLOC, SAFE_FREE, SET_ERRNO, ascii_stabilizer_t::width, and width.

◆ ascii_stabilizer_reset()

void ascii_stabilizer_reset ( ascii_stabilizer_t * state)

Definition at line 19 of file stabilize.c.

19 {
20 if (!state)
21 return;
22 SAFE_FREE(state->cells);
23 memset(state, 0, sizeof(*state));
24}

References ascii_stabilizer_t::cells, and SAFE_FREE.

◆ ascii_stabilizer_set_target()

ascii_stabilizer_t * ascii_stabilizer_set_target ( ascii_stabilizer_t * state)

Select history for the current thread until the previous target is restored.

Definition at line 13 of file stabilize.c.

13 {
14 ascii_stabilizer_t *previous = g_stabilizer_target;
15 g_stabilizer_target = state;
16 return previous;
17}

◆ ascii_stabilizer_target()

ascii_stabilizer_t * ascii_stabilizer_target ( void )

Definition at line 9 of file stabilize.c.

9 {
10 return g_stabilizer_target;
11}

Referenced by ascii_frame_init(), ascii_half_frame_init(), and ascii_legacy_frame_init().