Skip to content

Commit 0249509

Browse files
authored
refactor: add user data pointer to the image preview callback (#1001)
1 parent 52b67c5 commit 0249509

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

examples/cli/main.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ struct SDParams {
151151
preview_t preview_method = PREVIEW_NONE;
152152
int preview_interval = 1;
153153
std::string preview_path = "preview.png";
154+
float preview_fps = 16;
154155
bool taesd_preview = false;
155156
bool preview_noisy = false;
156157

@@ -1638,25 +1639,22 @@ bool load_images_from_dir(const std::string dir,
16381639
return true;
16391640
}
16401641

1641-
std::string preview_path;
1642-
float preview_fps;
1643-
1644-
void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy) {
1642+
void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy, void* data) {
16451643
(void)step;
16461644
(void)is_noisy;
1645+
SDParams* params = (SDParams*)data;
16471646
// is_noisy is set to true if the preview corresponds to noisy latents, false if it's denoised latents
16481647
// unused in this app, it will either be always noisy or always denoised here
16491648
if (frame_count == 1) {
1650-
stbi_write_png(preview_path.c_str(), image->width, image->height, image->channel, image->data, 0);
1649+
stbi_write_png(params->preview_path.c_str(), image->width, image->height, image->channel, image->data, 0);
16511650
} else {
1652-
create_mjpg_avi_from_sd_images(preview_path.c_str(), image, frame_count, preview_fps);
1651+
create_mjpg_avi_from_sd_images(params->preview_path.c_str(), image, frame_count, params->preview_fps);
16531652
}
16541653
}
16551654

16561655
int main(int argc, const char* argv[]) {
16571656
SDParams params;
16581657
parse_args(argc, argv, params);
1659-
preview_path = params.preview_path;
16601658
if (params.video_frames > 4) {
16611659
size_t last_dot_pos = params.preview_path.find_last_of(".");
16621660
std::string base_path = params.preview_path;
@@ -1667,20 +1665,20 @@ int main(int argc, const char* argv[]) {
16671665
std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(), ::tolower);
16681666
}
16691667
if (file_ext == ".png") {
1670-
preview_path = base_path + ".avi";
1668+
params.preview_path = base_path + ".avi";
16711669
}
16721670
}
1673-
preview_fps = params.fps;
1671+
params.preview_fps = params.fps;
16741672
if (params.preview_method == PREVIEW_PROJ)
1675-
preview_fps /= 4.0f;
1673+
params.preview_fps /= 4.0f;
16761674

16771675
params.sample_params.guidance.slg.layers = params.skip_layers.data();
16781676
params.sample_params.guidance.slg.layer_count = params.skip_layers.size();
16791677
params.high_noise_sample_params.guidance.slg.layers = params.high_noise_skip_layers.data();
16801678
params.high_noise_sample_params.guidance.slg.layer_count = params.high_noise_skip_layers.size();
16811679

16821680
sd_set_log_callback(sd_log_cb, (void*)&params);
1683-
sd_set_preview_callback((sd_preview_cb_t)step_callback, params.preview_method, params.preview_interval, !params.preview_noisy, params.preview_noisy);
1681+
sd_set_preview_callback(step_callback, params.preview_method, params.preview_interval, !params.preview_noisy, params.preview_noisy, (void*)&params);
16841682

16851683
if (params.verbose) {
16861684
print_params(params);

stable-diffusion.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,8 @@ class StableDiffusionGGML {
13071307
enum SDVersion version,
13081308
preview_t preview_mode,
13091309
ggml_tensor* result,
1310-
std::function<void(int, int, sd_image_t*, bool)> step_callback,
1310+
std::function<void(int, int, sd_image_t*, bool, void*)> step_callback,
1311+
void* step_callback_data,
13111312
bool is_noisy) {
13121313
const uint32_t channel = 3;
13131314
uint32_t width = latents->ne[0];
@@ -1378,7 +1379,7 @@ class StableDiffusionGGML {
13781379
for (int i = 0; i < frames; i++) {
13791380
images[i] = {width, height, channel, data + i * width * height * channel};
13801381
}
1381-
step_callback(step, frames, images, is_noisy);
1382+
step_callback(step, frames, images, is_noisy, step_callback_data);
13821383
free(data);
13831384
free(images);
13841385
} else {
@@ -1432,7 +1433,7 @@ class StableDiffusionGGML {
14321433
images[i].data = ggml_tensor_to_sd_image(result, i, ggml_n_dims(latents) == 4);
14331434
}
14341435

1435-
step_callback(step, frames, images, is_noisy);
1436+
step_callback(step, frames, images, is_noisy, step_callback_data);
14361437

14371438
ggml_ext_tensor_scale_inplace(result, 0);
14381439
for (int i = 0; i < frames; i++) {
@@ -1581,8 +1582,9 @@ class StableDiffusionGGML {
15811582
}
15821583

15831584
auto denoise = [&](ggml_tensor* input, float sigma, int step) -> ggml_tensor* {
1584-
auto sd_preview_cb = sd_get_preview_callback();
1585-
auto sd_preview_mode = sd_get_preview_mode();
1585+
auto sd_preview_cb = sd_get_preview_callback();
1586+
auto sd_preview_cb_data = sd_get_preview_callback_data();
1587+
auto sd_preview_mode = sd_get_preview_mode();
15861588
if (step == 1 || step == -1) {
15871589
pretty_progress(0, (int)steps, 0);
15881590
}
@@ -1651,7 +1653,7 @@ class StableDiffusionGGML {
16511653
}
16521654
if (sd_preview_cb != nullptr && sd_should_preview_noisy()) {
16531655
if (step % sd_get_preview_interval() == 0) {
1654-
preview_image(work_ctx, step, noised_input, version, sd_preview_mode, preview_tensor, sd_preview_cb, true);
1656+
preview_image(work_ctx, step, noised_input, version, sd_preview_mode, preview_tensor, sd_preview_cb, sd_preview_cb_data, true);
16551657
}
16561658
}
16571659

@@ -1799,7 +1801,7 @@ class StableDiffusionGGML {
17991801

18001802
if (sd_preview_cb != nullptr && sd_should_preview_denoised()) {
18011803
if (step % sd_get_preview_interval() == 0) {
1802-
preview_image(work_ctx, step, denoised, version, sd_preview_mode, preview_tensor, sd_preview_cb, false);
1804+
preview_image(work_ctx, step, denoised, version, sd_preview_mode, preview_tensor, sd_preview_cb, sd_preview_cb_data, false);
18031805
}
18041806
}
18051807

stable-diffusion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ typedef struct sd_ctx_t sd_ctx_t;
283283

284284
typedef void (*sd_log_cb_t)(enum sd_log_level_t level, const char* text, void* data);
285285
typedef void (*sd_progress_cb_t)(int step, int steps, float time, void* data);
286-
typedef void (*sd_preview_cb_t)(int step, int frame_count, sd_image_t* frames, bool is_noisy);
286+
typedef void (*sd_preview_cb_t)(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data);
287287

288288
SD_API void sd_set_log_callback(sd_log_cb_t sd_log_cb, void* data);
289289
SD_API void sd_set_progress_callback(sd_progress_cb_t cb, void* data);
290-
SD_API void sd_set_preview_callback(sd_preview_cb_t cb, enum preview_t mode, int interval, bool denoised, bool noisy);
290+
SD_API void sd_set_preview_callback(sd_preview_cb_t cb, enum preview_t mode, int interval, bool denoised, bool noisy, void* data);
291291
SD_API int32_t get_num_physical_cores();
292292
SD_API const char* sd_get_system_info();
293293

util.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static sd_progress_cb_t sd_progress_cb = nullptr;
187187
void* sd_progress_cb_data = nullptr;
188188

189189
static sd_preview_cb_t sd_preview_cb = nullptr;
190+
static void* sd_preview_cb_data = nullptr;
190191
preview_t sd_preview_mode = PREVIEW_NONE;
191192
int sd_preview_interval = 1;
192193
bool sd_preview_denoised = true;
@@ -335,8 +336,9 @@ void sd_set_progress_callback(sd_progress_cb_t cb, void* data) {
335336
sd_progress_cb = cb;
336337
sd_progress_cb_data = data;
337338
}
338-
void sd_set_preview_callback(sd_preview_cb_t cb, preview_t mode = PREVIEW_PROJ, int interval = 1, bool denoised = true, bool noisy = false) {
339+
void sd_set_preview_callback(sd_preview_cb_t cb, preview_t mode, int interval, bool denoised, bool noisy, void* data) {
339340
sd_preview_cb = cb;
341+
sd_preview_cb_data = data;
340342
sd_preview_mode = mode;
341343
sd_preview_interval = interval;
342344
sd_preview_denoised = denoised;
@@ -346,6 +348,9 @@ void sd_set_preview_callback(sd_preview_cb_t cb, preview_t mode = PREVIEW_PROJ,
346348
sd_preview_cb_t sd_get_preview_callback() {
347349
return sd_preview_cb;
348350
}
351+
void* sd_get_preview_callback_data() {
352+
return sd_preview_cb_data;
353+
}
349354

350355
preview_t sd_get_preview_mode() {
351356
return sd_preview_mode;

util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ sd_progress_cb_t sd_get_progress_callback();
5858
void* sd_get_progress_callback_data();
5959

6060
sd_preview_cb_t sd_get_preview_callback();
61+
void* sd_get_preview_callback_data();
6162
preview_t sd_get_preview_mode();
6263
int sd_get_preview_interval();
6364
bool sd_should_preview_denoised();

0 commit comments

Comments
 (0)