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

Go to the source code of this file.

Data Structures

struct  ascii_temporal_cell_t
struct  ascii_stabilizer_t

Macros

#define ASCII_TEMPORAL_MOTION_LIMIT   8
#define ASCII_TEMPORAL_MAX_INTERVAL_NS   250000000ULL
#define ASCII_TEMPORAL_COLOR_TIME_NS   50000000ULL

Functions

asciichat_error_t ascii_stabilizer_prepare (ascii_stabilizer_t *state, size_t width, size_t height, uint64_t configuration)
void ascii_stabilizer_reset (ascii_stabilizer_t *state)
ascii_stabilizer_t * ascii_stabilizer_set_target (ascii_stabilizer_t *state)
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)
ascii_stabilizer_t * ascii_stabilizer_target (void)
uint32_t ascii_stabilizer_blend_color (uint32_t previous, uint32_t current, unsigned weight)
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)

Macro Definition Documentation

◆ ASCII_TEMPORAL_COLOR_TIME_NS

#define ASCII_TEMPORAL_COLOR_TIME_NS   50000000ULL

Definition at line 10 of file stabilize.h.

◆ ASCII_TEMPORAL_MAX_INTERVAL_NS

#define ASCII_TEMPORAL_MAX_INTERVAL_NS   250000000ULL

Definition at line 9 of file stabilize.h.

◆ ASCII_TEMPORAL_MOTION_LIMIT

#define ASCII_TEMPORAL_MOTION_LIMIT   8

Definition at line 8 of file stabilize.h.

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_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}
unsigned char uint8_t
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_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
ascii_temporal_cell_t * cells
Definition stabilize.h:25
uint64_t elapsed_ns
Definition stabilize.h:28
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().