From 287b04196dcac864270c09da199f5d381e65c0d2 Mon Sep 17 00:00:00 2001 From: Yucheng Du <256645790+yucheng-du@users.noreply.github.com> Date: Tue, 1 Dec 2026 04:00:06 -0800 Subject: [PATCH 1/2] vo_kitty: finish inline frames that fit in one chunk Mark the first chunk as final when it contains the whole image. Small frames previously left the terminal waiting for a continuation. --- video/out/vo_kitty.c | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/video/out/vo_kitty.c b/video/out/vo_kitty.c index 2009f27020e8b..ee9687bf80d4e 100644 --- a/video/out/vo_kitty.c +++ b/video/out/vo_kitty.c @@ -48,7 +48,7 @@ #define DEFAULT_WIDTH 80 #define DEFAULT_HEIGHT 25 -#define KITTY_ESC_IMG "\033_Ga=T,f=24,s=%d,v=%d,C=1,q=2,m=1;" +#define KITTY_ESC_IMG "\033_Ga=T,f=24,s=%d,v=%d,C=1,q=2,m=%d;" #define KITTY_ESC_IMG_SHM "\033_Ga=T,t=s,f=24,s=%d,v=%d,C=1,q=2,m=1;%s" #define KITTY_ESC_CONTINUE "\033_Gm=%d;" static const bstr KITTY_ESC_END = bstr0_lit("\033\\"); @@ -312,10 +312,9 @@ static void flip_page(struct vo *vo) return; } - append_asprintf_passthrough(p, cmd, KITTY_ESC_IMG, - vo->dwidth, vo->dheight); - int output_size = p->output_size - 1; + append_asprintf_passthrough(p, cmd, KITTY_ESC_IMG, + vo->dwidth, vo->dheight, output_size > 4096); int offset = 0; for (; offset < output_size; ) { @@ -330,15 +329,6 @@ static void flip_page(struct vo *vo) append_passthrough(p, cmd, KITTY_ESC_END); offset += chunk; } - - // When the data is less than or equal to chunk size the final packet - // isn't sent, i.e. an escape sequence with `m=0`. - // This ensures that an escape sequence with `m=0` is sent and - // terminals stay happy - if (offset == 0) { - append_asprintf_passthrough(p, cmd, KITTY_ESC_CONTINUE, 0); - append_passthrough(p, cmd, KITTY_ESC_END); - } } terminal_swapchain_present(p->swapchain, cmd); From 2b8365dbef3145bcf9ab3d21df4c19b9bdba0510 Mon Sep 17 00:00:00 2001 From: Yucheng Du <256645790+yucheng-du@users.noreply.github.com> Date: Tue, 1 Dec 2026 04:01:35 -0800 Subject: [PATCH 2/2] vo_kitty: keep shared frames intact until the terminal reads them Opening the same object for each frame could overwrite an unread image. On macOS, sizing that object again failed and discarded every other frame while the terminal still had the name. Create up to three objects exclusively and send a frame inline when all three are pending. This keeps memory use bounded even if the terminal never unlinks a name. --- DOCS/man/vo.rst | 3 +- video/out/vo_kitty.c | 169 ++++++++++++++++++++----------------------- 2 files changed, 81 insertions(+), 91 deletions(-) diff --git a/DOCS/man/vo.rst b/DOCS/man/vo.rst index 06413021c9358..9ee2de535a9c8 100644 --- a/DOCS/man/vo.rst +++ b/DOCS/man/vo.rst @@ -432,7 +432,8 @@ Available video output drivers are: Use shared memory objects to transfer image data to the terminal. This is much faster than sending the data as escape codes, but is not supported by as many terminals. It also only works on the local machine - and not via e.g. SSH connections. + and not via e.g. SSH connections. If the terminal falls behind, mpv + sends frames inline until a shared memory object is available again. This option is not implemented on Windows. diff --git a/video/out/vo_kitty.c b/video/out/vo_kitty.c index ee9687bf80d4e..579b9c0a8e616 100644 --- a/video/out/vo_kitty.c +++ b/video/out/vo_kitty.c @@ -24,6 +24,7 @@ #include "config.h" #if HAVE_POSIX +#include #include #include #include @@ -65,14 +66,19 @@ struct vo_kitty_opts { bool auto_multiplexer_passthrough; }; +struct shm_slot { + char *path, *encoded_path; + bool owned; +}; + struct priv { struct vo_kitty_opts opts; uint8_t *buffer; char *output; - char *shm_path, *shm_path_b64; + struct shm_slot shm_slots[3]; + struct shm_slot *shm_frame; int buffer_size, output_size; - int shm_fd; struct terminal_swapchain *swapchain; bstr dcs_prefix; bstr dcs_suffix; @@ -108,36 +114,13 @@ static inline void append_asprintf_passthrough(struct priv *p, bstr *bs, bstr_xappend(NULL, bs, p->dcs_suffix); } -static void close_shm(struct priv *p) -{ -#if HAVE_POSIX_SHM - if (p->buffer != NULL) { - munmap(p->buffer, p->buffer_size); - p->buffer = NULL; - } - if (p->shm_fd != -1) { - close(p->shm_fd); - p->shm_fd = -1; - } -#endif -} - -static void free_bufs(struct vo* vo) +static void free_bufs(struct vo *vo) { - struct priv* p = vo->priv; - - talloc_free(p->frame); - talloc_free(p->output); - - if (p->opts.use_shm) { - close_shm(p); -#if HAVE_POSIX_SHM - if (p->shm_path) - shm_unlink(p->shm_path); -#endif - } else { - talloc_free(p->buffer); - } + struct priv *p = vo->priv; + TA_FREEP(&p->frame); + TA_FREEP(&p->buffer); + TA_FREEP(&p->output); + p->shm_frame = NULL; } static void get_win_size(struct vo *vo, int *out_rows, int *out_cols, @@ -207,44 +190,53 @@ static int reconfig(struct vo *vo, struct mp_image_params *params) if (mp_sws_reinit(p->sws) < 0) return -1; - if (!p->opts.use_shm) { - p->buffer = talloc_array(NULL, uint8_t, p->buffer_size); - p->output = talloc_array(NULL, char, p->output_size); - } - return 0; } -static int create_shm(struct vo *vo) +static void copy_frame(struct priv *p, uint8_t *buffer) +{ + int stride = p->frame->w * BYTES_PER_PX; + memcpy_pic(buffer, p->frame->planes[0], stride, p->frame->h, + stride, p->frame->stride[0]); +} + +static bool write_shm(struct vo *vo) { #if HAVE_POSIX_SHM struct priv *p = vo->priv; - p->shm_fd = shm_open(p->shm_path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); - if (p->shm_fd == -1) { - MP_ERR(vo, "Failed to create shared memory object"); - return 0; - } - - if (ftruncate(p->shm_fd, p->buffer_size) == -1) { - MP_ERR(vo, "Failed to truncate shared memory object"); - shm_unlink(p->shm_path); - close(p->shm_fd); - return 0; - } - - p->buffer = mmap(NULL, p->buffer_size, - PROT_READ | PROT_WRITE, MAP_SHARED, p->shm_fd, 0); + // The terminal unlinks a name after reading it. Leave any pending frame + // alone, and use the inline protocol if all slots are still in use. + for (int i = 0; i < MP_ARRAY_SIZE(p->shm_slots); i++) { + struct shm_slot *slot = &p->shm_slots[i]; + int fd = shm_open(slot->path, O_CREAT | O_EXCL | O_RDWR, 0600); + if (fd < 0) { + if (errno == EEXIST) + continue; + MP_ERR(vo, "Failed to create shared memory object: %s\n", mp_strerror(errno)); + break; + } - if (p->buffer == MAP_FAILED) { - MP_ERR(vo, "Failed to mmap shared memory object"); - shm_unlink(p->shm_path); - close(p->shm_fd); - return 0; + uint8_t *buffer = MAP_FAILED; + if (ftruncate(fd, p->buffer_size) < 0) { + MP_ERR(vo, "Failed to size shared memory object: %s\n", mp_strerror(errno)); + } else { + buffer = mmap(NULL, p->buffer_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (buffer == MAP_FAILED) + MP_ERR(vo, "Failed to map shared memory object: %s\n", mp_strerror(errno)); + } + close(fd); + if (buffer == MAP_FAILED) { + shm_unlink(slot->path); + break; + } + copy_frame(p, buffer); + munmap(buffer, p->buffer_size); + slot->owned = true; + p->shm_frame = slot; + return true; } - return 1; -#else - return 0; #endif + return false; } static bool draw_frame(struct vo *vo, struct vo_frame *frame) @@ -277,16 +269,16 @@ static bool draw_frame(struct vo *vo, struct vo_frame *frame) osd_draw_on_image(vo->osd, p->osd, mpi ? mpi->pts : 0, 0, p->frame); - if (p->opts.use_shm && !create_shm(vo)) - goto done; - - memcpy_pic(p->buffer, p->frame->planes[0], p->frame->w * BYTES_PER_PX, - p->frame->h, p->frame->w * BYTES_PER_PX, p->frame->stride[0]); - - if (!p->opts.use_shm) + p->shm_frame = NULL; + if (!p->opts.use_shm || !write_shm(vo)) { + if (!p->buffer) { + p->buffer = talloc_array(NULL, uint8_t, p->buffer_size); + p->output = talloc_array(NULL, char, p->output_size); + } + copy_frame(p, p->buffer); av_base64_encode(p->output, p->output_size, p->buffer, p->buffer_size); + } -done: talloc_free(mpi); return VO_TRUE; } @@ -294,7 +286,7 @@ static bool draw_frame(struct vo *vo, struct vo_frame *frame) static void flip_page(struct vo *vo) { struct priv *p = vo->priv; - if (!p->buffer) + if (!p->shm_frame && !p->output) return; bstr *cmd = terminal_swapchain_acquire(p->swapchain); @@ -302,16 +294,11 @@ static void flip_page(struct vo *vo) // Start with ESC to position the cursor append_asprintf_passthrough(p, cmd, TERM_ESC_GOTO_YX, p->top, p->left); - if (p->opts.use_shm) { + if (p->shm_frame) { append_asprintf_passthrough(p, cmd, KITTY_ESC_IMG_SHM, - vo->dwidth, vo->dheight, p->shm_path_b64); + vo->dwidth, vo->dheight, p->shm_frame->encoded_path); append_passthrough(p, cmd, KITTY_ESC_END); } else { - if (!p->output) { - terminal_swapchain_present(p->swapchain, cmd); - return; - } - int output_size = p->output_size - 1; append_asprintf_passthrough(p, cmd, KITTY_ESC_IMG, vo->dwidth, vo->dheight, output_size > 4096); @@ -332,15 +319,8 @@ static void flip_page(struct vo *vo) } terminal_swapchain_present(p->swapchain, cmd); - -#if HAVE_POSIX - // The terminal reads the image from the shared memory when it gets the - // command, so that has to be written before the next frame replaces it. - if (p->opts.use_shm) { + if (p->shm_frame) terminal_swapchain_wait(p->swapchain); - close_shm(p); - } -#endif } static int preinit(struct vo *vo) @@ -353,11 +333,15 @@ static int preinit(struct vo *vo) #if HAVE_POSIX_SHM if (p->opts.use_shm) { - p->shm_path = talloc_asprintf(vo, "/mpv-kitty-%p", vo); - int p_size = strlen(p->shm_path); - int b64_size = AV_BASE64_SIZE(p_size); - p->shm_path_b64 = talloc_array(vo, char, b64_size); - av_base64_encode(p->shm_path_b64, b64_size, p->shm_path, p_size); + for (int i = 0; i < MP_ARRAY_SIZE(p->shm_slots); i++) { + struct shm_slot *slot = &p->shm_slots[i]; + slot->path = talloc_asprintf(vo, "/mpv%x-%"PRIxPTR"-%d", + (unsigned)getpid(), (uintptr_t)vo, i); + int len = strlen(slot->path); + int size = AV_BASE64_SIZE(len); + slot->encoded_path = talloc_array(vo, char, size); + av_base64_encode(slot->encoded_path, size, slot->path, len); + } } #else if (p->opts.use_shm) { @@ -423,6 +407,12 @@ static void uninit(struct vo *vo) terminal_set_mouse_input(false); terminal_swapchain_destroy(p->swapchain); +#if HAVE_POSIX_SHM + for (int i = 0; i < MP_ARRAY_SIZE(p->shm_slots); i++) { + if (p->shm_slots[i].owned) + shm_unlink(p->shm_slots[i].path); + } +#endif free_bufs(vo); } @@ -440,7 +430,6 @@ const struct vo_driver video_out_kitty = { .uninit = uninit, .priv_size = sizeof(struct priv), .priv_defaults = &(const struct priv) { - .shm_fd = -1, .opts.config_clear = true, .opts.alt_screen = true, },