1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* vfetch - neofetch but the ascii part is a video
*
* Copyright (C) 2026 Doan Luu Duc Tai
*
*/
#include "video.h"
#include <chafa/chafa.h>
#include <glib.h>
#include <sys/ioctl.h>
#include <math.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "pl_mpeg.h"
#include "utils.h"
static void apply_pixel_mode(ChafaCanvasConfig *config,
const VfetchArgs *args) {
if (strcmp(args->format_arg, "symbols") == 0) {
chafa_canvas_config_set_pixel_mode(config, CHAFA_PIXEL_MODE_SYMBOLS);
} else if (strcmp(args->format_arg, "sixels") == 0) {
chafa_canvas_config_set_pixel_mode(config, CHAFA_PIXEL_MODE_SIXELS);
} else if (strcmp(args->format_arg, "kitty") == 0) {
chafa_canvas_config_set_pixel_mode(config, CHAFA_PIXEL_MODE_KITTY);
} else if (strcmp(args->format_arg, "iterm2") == 0) {
chafa_canvas_config_set_pixel_mode(config, CHAFA_PIXEL_MODE_ITERM2);
}
}
static void apply_color_settings(ChafaCanvasConfig *config,
const VfetchArgs *args) {
if (args->colors_arg) {
if (strcmp(args->colors_arg, "16") == 0) {
chafa_canvas_config_set_canvas_mode(config, CHAFA_CANVAS_MODE_INDEXED_16);
} else if (strcmp(args->colors_arg, "240") == 0) {
chafa_canvas_config_set_canvas_mode(config,
CHAFA_CANVAS_MODE_INDEXED_240);
} else if (strcmp(args->colors_arg, "256") == 0) {
chafa_canvas_config_set_canvas_mode(config,
CHAFA_CANVAS_MODE_INDEXED_256);
} else if (strcmp(args->colors_arg, "full") == 0) {
chafa_canvas_config_set_canvas_mode(config, CHAFA_CANVAS_MODE_TRUECOLOR);
}
}
if (args->fg_color_arg) {
chafa_canvas_config_set_fg_color(config,
parse_hex_color(args->fg_color_arg));
}
if (args->bg_color_arg) {
chafa_canvas_config_set_bg_color(config,
parse_hex_color(args->bg_color_arg));
}
}
#define K_FALLBACK_CELL_WIDTH 10.0
static VfetchError configure_chafa_canvas(uint32_t width,
uint32_t height,
ChafaCanvas **canvas_out,
const VfetchArgs *args) {
ChafaCanvasConfig *config = chafa_canvas_config_new();
chafa_canvas_config_set_geometry(config, (int)width, (int)height);
struct winsize window_size = {0};
int cell_px_w = 0;
int cell_px_h = 0;
if (isatty(STDOUT_FILENO) &&
ioctl(STDOUT_FILENO, TIOCGWINSZ, &window_size) == 0) {
if (window_size.ws_xpixel > 0 && window_size.ws_ypixel > 0 &&
window_size.ws_col > 0 && window_size.ws_row > 0) {
cell_px_w = window_size.ws_xpixel / window_size.ws_col;
cell_px_h = window_size.ws_ypixel / window_size.ws_row;
}
}
if (cell_px_w <= 0 || cell_px_h <= 0) {
cell_px_w = K_FALLBACK_CELL_WIDTH;
double h_val = round(K_FALLBACK_CELL_WIDTH / args->cell_aspect_ratio);
cell_px_h = (int)h_val;
}
chafa_canvas_config_set_cell_geometry(config, cell_px_w, cell_px_h);
apply_pixel_mode(config, args);
apply_color_settings(config, args);
ChafaSymbolMap *symbol_map = chafa_symbol_map_new();
char *symbols = (char *)g_hash_table_lookup(args->chafa_options, "--symbols");
chafa_symbol_map_apply_selectors(symbol_map, symbols ? symbols : "all", NULL);
chafa_canvas_config_set_symbol_map(config, symbol_map);
chafa_symbol_map_unref(symbol_map);
if (g_hash_table_lookup(args->chafa_options, "--fg-only")) {
chafa_canvas_config_set_fg_only_enabled(config, TRUE);
}
char *colorspace =
(char *)g_hash_table_lookup(args->chafa_options, "--color-space");
if (colorspace) {
if (strcmp(colorspace, "rgb") == 0) {
chafa_canvas_config_set_color_space(config, CHAFA_COLOR_SPACE_RGB);
} else if (strcmp(colorspace, "din99d") == 0) {
chafa_canvas_config_set_color_space(config, CHAFA_COLOR_SPACE_DIN99D);
}
}
char *fill = (char *)g_hash_table_lookup(args->chafa_options, "--fill");
if (fill && *fill) {
ChafaSymbolMap *fill_map = chafa_symbol_map_new();
for (guint i = 0; i < strlen(fill); i++) {
chafa_symbol_map_add_by_range(
fill_map, (gunichar)fill[i], (gunichar)fill[i]);
}
chafa_canvas_config_set_fill_symbol_map(config, fill_map);
chafa_symbol_map_unref(fill_map);
}
*canvas_out = chafa_canvas_new(config);
chafa_canvas_config_unref(config);
return (*canvas_out) ? VFETCH_SUCCESS : VFETCH_ERROR_CHAFA_CONFIG;
}
#undef K_FALLBACK_CELL_WIDTH
static plm_t *probe_video(const char *filename) {
plm_t *plm = plm_create_with_filename(filename);
if (!plm) {
log_vfetch_error(VFETCH_ERROR_VIDEO_OPEN_FAILED, filename);
}
return plm;
}
VfetchError resolve_geometry(VfetchArgs *args) {
if (args->height > 0) {
return VFETCH_SUCCESS;
}
plm_t *plm = probe_video(args->filename);
if (!plm) {
return VFETCH_ERROR_VIDEO_OPEN_FAILED;
}
int v_w = plm_get_width(plm);
int v_h = plm_get_height(plm);
plm_destroy(plm);
double video_ratio = (double)v_h / (double)v_w;
double target_h = (double)args->width * args->cell_aspect_ratio * video_ratio;
double final_h = fmax(1.0, round(target_h));
args->height = (uint32_t)final_h;
return VFETCH_SUCCESS;
}
VfetchError get_video_frame_count(const char *filename,
double fps,
size_t *frame_count_out) {
plm_t *plm = probe_video(filename);
if (!plm) {
return VFETCH_ERROR_VIDEO_OPEN_FAILED;
}
double total_f = ceil(plm_get_duration(plm) * fps);
*frame_count_out = (size_t)total_f;
plm_destroy(plm);
return VFETCH_SUCCESS;
}
VfetchError validate_fps(VfetchArgs *args) {
plm_t *plm = probe_video(args->filename);
if (!plm) {
return VFETCH_ERROR_VIDEO_OPEN_FAILED;
}
if (args->fps <= 0.0) {
args->fps = plm_get_framerate(plm);
}
plm_destroy(plm);
return VFETCH_SUCCESS;
}
VfetchError initialize_worker_resources(WorkerResources *res,
const VfetchArgs *args) {
res->plm = plm_create_with_filename(args->filename);
if (!res->plm) {
return VFETCH_ERROR_FILE_IO;
}
res->video_width = plm_get_width(res->plm);
res->video_height = plm_get_height(res->plm);
res->rgb_buffer =
malloc((size_t)res->video_width * (size_t)res->video_height * 3);
return configure_chafa_canvas(args->width, args->height, &res->canvas, args);
}
void cleanup_worker_resources(WorkerResources *res) {
if (res->plm) {
plm_destroy(res->plm);
}
if (res->canvas) {
chafa_canvas_unref(res->canvas);
}
free(res->rgb_buffer);
}
static VfetchError cache_single_frame(WorkerResources *res,
plm_frame_t *frame,
CacheFiles *files,
unsigned long *current_offset) {
plm_frame_to_rgb(frame, res->rgb_buffer, res->video_width * 3);
chafa_canvas_draw_all_pixels(res->canvas,
CHAFA_PIXEL_RGB8,
res->rgb_buffer,
res->video_width,
res->video_height,
res->video_width * 3);
GString *gstr = chafa_canvas_print(res->canvas, NULL);
if (!gstr) {
return VFETCH_ERROR_MEMORY_ALLOCATION;
}
FrameMetadata meta = {
.offset = *current_offset, .length = gstr->len, .pts = frame->time};
VfetchError err = VFETCH_SUCCESS;
if (fwrite(gstr->str, 1, gstr->len, files->data_file) != gstr->len ||
fwrite(&meta, sizeof(FrameMetadata), 1, files->meta_file) != 1) {
err = VFETCH_ERROR_FILE_IO;
} else {
*current_offset += gstr->len;
}
g_string_free(gstr, TRUE);
return err;
}
VfetchError populate_frame_cache_loop(WorkerResources *res,
AppState *state,
CacheFiles *files,
unsigned long *current_offset) {
while (!atomic_load(&state->exit_requested)) {
plm_frame_t *frame = plm_decode_video(res->plm);
if (!frame) {
break;
}
if (cache_single_frame(res, frame, files, current_offset) !=
VFETCH_SUCCESS) {
return VFETCH_ERROR_FILE_IO;
}
atomic_fetch_add(&state->ascii_frames_completed, 1);
}
return VFETCH_SUCCESS;
}
|