char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenmtmd: propagate video ID to bitmap (#28601)6de9cdb26

char/aliceai-t5-moe
34.9 KiB1044 linesraw
1// fix problem with std::min and std::max
2#if defined(_WIN32)
3#define WIN32_LEAN_AND_MEAN
4#ifndef NOMINMAX
5#   define NOMINMAX
6#endif
7#include <windows.h>
8#endif
9
10#include "mtmd.h"
11#include "mtmd-helper.h"
12#include "mtmd-helper-common.h"
13#include "llama.h"
14
15#include "hash/hash.h"
16
17#include <algorithm>
18#include <cinttypes>
19#include <vector>
20
21//#define MTMD_AUDIO_DEBUG
22
23#define MINIAUDIO_IMPLEMENTATION
24#ifndef MTMD_AUDIO_DEBUG
25#   define MA_NO_ENCODING
26#endif
27#define MA_NO_DEVICE_IO
28#define MA_NO_RESOURCE_MANAGER
29#define MA_NO_NODE_GRAPH
30#define MA_NO_ENGINE
31#define MA_NO_GENERATION
32#define MA_API static
33#include "miniaudio/miniaudio.h"
34
35#define STB_IMAGE_IMPLEMENTATION
36#include "stb/stb_image.h"
37
38#ifdef MTMD_INTERNAL_HEADER
39#error "mtmd-helper is a public library outside of mtmd. it must not include internal headers"
40#endif
41
42#ifdef MTMD_VIDEO
43#include "sheredom/subprocess.h"
44#include <thread>
45#ifndef _WIN32
46#include <csignal>
47#include <fcntl.h>
48#include <pthread.h>
49#endif
50#endif
51
52//
53// internal logging functions
54//
55
56void mtmd_helper_log_set(ggml_log_callback log_callback, void * user_data) {
57    if (log_callback == nullptr) {
58        log_callback = g_logger.default_callback;
59    }
60    g_logger.log_callback = log_callback;
61    g_logger.log_callback_user_data = user_data;
62    mtmd_log_set(log_callback, user_data);
63}
64
65//
66// helper functions
67//
68
69size_t mtmd_helper_get_n_tokens(const mtmd_input_chunks * chunks) {
70    size_t n_tokens = 0;
71    for (size_t i = 0; i < mtmd_input_chunks_size(chunks); i++) {
72        auto chunk = mtmd_input_chunks_get(chunks, i);
73        n_tokens += mtmd_input_chunk_get_n_tokens(chunk);
74    }
75    return n_tokens;
76}
77
78llama_pos mtmd_helper_get_n_pos(const mtmd_input_chunks * chunks) {
79    llama_pos n_pos = 0;
80    for (size_t i = 0; i < mtmd_input_chunks_size(chunks); i++) {
81        auto chunk = mtmd_input_chunks_get(chunks, i);
82        n_pos += mtmd_input_chunk_get_n_pos(chunk);
83    }
84    return n_pos;
85}
86
87void mtmd_helper_image_get_decoder_pos(const mtmd_image_tokens * chunks, llama_pos pos_0, mtmd_decoder_pos * out_pos) {
88    size_t n_tokens = mtmd_image_tokens_get_n_tokens(chunks);
89    for (size_t i = 0; i < n_tokens; i++) {
90        out_pos[i] = mtmd_image_tokens_get_decoder_pos(chunks, pos_0, i);
91    }
92}
93
94// Helper class to set non-causal attention via RAII
95class scope_non_causal {
96public:
97    scope_non_causal(llama_context * context, bool enabled) : context_(context), enabled_(enabled) {
98        if (enabled_) {
99            // TODO @ngxson : need to make sure only one image is processed at a time, and n_ubatch must be enough to hold the image
100            llama_set_causal_attn(context_, false);
101        }
102    }
103    ~scope_non_causal() {
104        if (enabled_) {
105            llama_set_causal_attn(context_, true);
106        }
107    }
108
109    scope_non_causal(const scope_non_causal &) = delete;
110    scope_non_causal & operator=(const scope_non_causal &) = delete;
111
112private:
113    llama_context * context_;
114    bool enabled_;
115};
116
117// Helper function for decoding an image whose embeddings have already been calculated
118int32_t mtmd_helper_decode_image_chunk(
119        mtmd_context * ctx,
120        struct llama_context * lctx,
121        const mtmd_input_chunk * chunk,
122        float * encoded_embd,
123        llama_pos n_past,
124        llama_seq_id seq_id,
125        int32_t n_batch,
126        llama_pos * new_n_past,
127        mtmd_helper_post_decode_callback callback,
128        void * user_data) {
129    GGML_ASSERT(n_batch > 0);
130    auto chunk_type = mtmd_input_chunk_get_type(chunk);
131    const char * name = chunk_type == MTMD_INPUT_CHUNK_TYPE_IMAGE ? "image" : "audio";
132    if (chunk_type == MTMD_INPUT_CHUNK_TYPE_TEXT) {
133        LOG_ERR("failed to decode chunk: input chunk not of image/audio type\n");
134        return -1;
135    }
136
137    const llama_model * model = llama_get_model(lctx);
138    int n_mmproj_embd = llama_model_n_embd_inp(model);
139    int n_pos_per_embd = mtmd_decode_use_mrope(ctx) ? 4 : 1;
140
141    int32_t n_tokens = mtmd_input_chunk_get_n_tokens(chunk);
142    int32_t i_batch = 0;
143    int32_t n_img_batches = (n_tokens + n_batch - 1) / n_batch;
144    decode_embd_batch batch_embd(encoded_embd, n_tokens, n_pos_per_embd, n_mmproj_embd);
145
146    if (mtmd_decode_use_mrope(ctx)) {
147        if (chunk_type == MTMD_INPUT_CHUNK_TYPE_IMAGE) {
148            const auto image_tokens = mtmd_input_chunk_get_tokens_image(chunk);
149            if (!image_tokens) {
150                LOG_ERR("failed to decode chunk: image tokens are null\n");
151                return -1;
152            }
153            const auto n_tokens = mtmd_image_tokens_get_n_tokens(image_tokens);
154            std::vector<mtmd_decoder_pos> rel_pos(n_tokens);
155            mtmd_helper_image_get_decoder_pos(image_tokens, n_past, rel_pos.data());
156            batch_embd.set_position_mrope_2d(rel_pos, seq_id);
157        } else if (chunk_type == MTMD_INPUT_CHUNK_TYPE_AUDIO) {
158            batch_embd.set_position_mrope_1d(n_past, seq_id);
159        } else {
160            GGML_ABORT("invalid chunk type for M-RoPE");
161        }
162    } else {
163        batch_embd.set_position_normal(n_past, seq_id);
164    }
165
166    const bool use_non_causal = mtmd_decode_use_non_causal(ctx, chunk);
167    const scope_non_causal non_causal(lctx, use_non_causal);
168
169    while (i_batch < n_img_batches) { // split into batches
170        int pos_offset = i_batch*n_batch;
171        int n_tokens_batch = std::min(n_batch, n_tokens - pos_offset);
172        llama_batch batch_embd_view = batch_embd.get_view(pos_offset, n_tokens_batch);
173
174        LOG_INF("decoding %s batch %d/%d, n_tokens_batch = %d\n", name, i_batch+1, n_img_batches, n_tokens_batch);
175
176        int64_t t1 = ggml_time_ms();
177        int32_t ret = llama_decode(lctx, batch_embd_view);
178        if (ret != 0) {
179            LOG_ERR("failed to decode %s\n", name);
180            return ret;
181        }
182
183        if (callback != nullptr) {
184            ret = callback(batch_embd_view, user_data);
185            if (ret != 0) {
186                LOG_ERR("post-decode callback failed\n");
187                return ret;
188            }
189        }
190
191        LOG_INF("%s decoded (batch %d/%d) in %" PRId64 " ms\n", name, i_batch+1, n_img_batches, ggml_time_ms() - t1);
192
193        i_batch++;
194    }
195
196    n_past += mtmd_input_chunk_get_n_pos(chunk);
197    *new_n_past = n_past;
198
199    return 0;
200}
201
202int32_t mtmd_helper_eval_chunk_single(mtmd_context * ctx,
203        struct llama_context * lctx,
204        const mtmd_input_chunk * chunk,
205        llama_pos n_past,
206        llama_seq_id seq_id,
207        int32_t n_batch,
208        bool logits_last,
209        llama_pos * new_n_past) {
210    GGML_ASSERT(n_batch > 0);
211    int32_t ret;
212    llama_batch text_batch = llama_batch_init(n_batch, 0, 1);
213    auto chunk_type = mtmd_input_chunk_get_type(chunk);
214
215    if (chunk_type == MTMD_INPUT_CHUNK_TYPE_TEXT) {
216        size_t n_tokens;
217        const auto tokens = mtmd_input_chunk_get_tokens_text(chunk, &n_tokens);
218        // LOG_INF("decoding text chunk, n_tokens = %zu\n", n_tokens);
219        size_t i = 0;
220        while (i < n_tokens) { // split into batches
221            text_batch.n_tokens = 0; // clear the batch
222            for (; i < n_tokens && text_batch.n_tokens < n_batch; i++) {
223                int32_t j = text_batch.n_tokens;
224                text_batch.token   [j]    = tokens[i];
225                text_batch.pos     [j]    = n_past++;
226                text_batch.n_seq_id[j]    = 1;
227                text_batch.seq_id  [j][0] = seq_id;
228                text_batch.logits  [j]    = false;
229
230                text_batch.n_tokens++;
231            }
232            bool is_last_token = (i == n_tokens);
233            if (logits_last && is_last_token) {
234                text_batch.logits[text_batch.n_tokens - 1] = true;
235            }
236            ret = llama_decode(lctx, text_batch);
237            if (ret != 0) {
238                LOG_ERR("failed to decode text\n");
239                llama_batch_free(text_batch);
240                return ret;
241            }
242            *new_n_past += text_batch.n_tokens;
243        }
244
245    } else if (chunk_type == MTMD_INPUT_CHUNK_TYPE_IMAGE || chunk_type == MTMD_INPUT_CHUNK_TYPE_AUDIO) {
246        const char * name = chunk_type == MTMD_INPUT_CHUNK_TYPE_IMAGE ? "image" : "audio";
247        int64_t t0 = ggml_time_ms();
248
249        LOG_INF("encoding %s slice...\n", name);
250
251        ret = mtmd_encode_chunk(ctx, chunk);
252        if (ret != 0) {
253            LOG_ERR("failed to encode %s slice\n", name);
254            llama_batch_free(text_batch);
255            return ret;
256        }
257
258        LOG_INF("%s slice encoded in %" PRId64 " ms\n", name, ggml_time_ms() - t0);
259
260        float * embd = mtmd_get_output_embd(ctx);
261        ret = mtmd_helper_decode_image_chunk(ctx, lctx, chunk, embd, n_past, seq_id, n_batch, new_n_past, nullptr, nullptr);
262        if (ret != 0) {
263            LOG_ERR("failed to decode %s\n", name);
264            llama_batch_free(text_batch);
265            return ret;
266        }
267    } else {
268        GGML_ABORT("chunk type not supported");
269    }
270
271    llama_batch_free(text_batch);
272    return 0;
273}
274
275int32_t mtmd_helper_eval_chunks(mtmd_context * ctx,
276                                struct llama_context * lctx,
277                                const mtmd_input_chunks * chunks,
278                                llama_pos n_past,
279                                llama_seq_id seq_id,
280                                int32_t n_batch,
281                                bool logits_last,
282                                llama_pos * new_n_past) {
283    size_t n_chunks = mtmd_input_chunks_size(chunks);
284    if (n_chunks == 0) {
285        LOG_WRN("no chunks to eval\n");
286        return 0;
287    }
288
289    for (size_t i = 0; i < n_chunks; i++) {
290        bool chunk_logits_last = (i == n_chunks - 1) && logits_last;
291        auto chunk = mtmd_input_chunks_get(chunks, i);
292
293        int32_t res = mtmd_helper_eval_chunk_single(ctx, lctx, chunk, n_past, seq_id, n_batch, chunk_logits_last, &n_past);
294        if (res != 0) {
295            LOG_ERR("failed to eval chunk %zu\n", i);
296            return res;
297        }
298        *new_n_past = n_past;
299    }
300
301    return 0;
302}
303
304namespace audio_helpers {
305
306static bool is_audio_file(const char * buf, size_t len) {
307    if (len < 12) {
308        return false;
309    }
310
311    // RIFF ref: https://en.wikipedia.org/wiki/Resource_Interchange_File_Format
312    // WAV ref: https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
313    bool is_wav = memcmp(buf, "RIFF", 4) == 0 && memcmp(buf + 8, "WAVE", 4) == 0;
314    bool is_mp3 = len >= 3 && (
315        memcmp(buf, "ID3", 3) == 0 ||
316        // Check for MPEG sync word (simplified check)
317        ((unsigned char)buf[0] == 0xFF && ((unsigned char)buf[1] & 0xE0) == 0xE0)
318    );
319    bool is_flac = memcmp(buf, "fLaC", 4) == 0;
320
321    return is_wav || is_mp3 || is_flac;
322}
323
324// returns true if the buffer is a valid audio file
325static bool decode_audio_from_buf(const unsigned char * buf_in, size_t len, int target_sampler_rate, std::vector<float> & pcmf32_mono) {
326    ma_result result;
327    const int channels = 1;
328    ma_decoder_config decoder_config = ma_decoder_config_init(ma_format_f32, channels, target_sampler_rate);
329    ma_decoder decoder;
330
331    result = ma_decoder_init_memory(buf_in, len, &decoder_config, &decoder);
332    if (result != MA_SUCCESS) {
333        return false;
334    }
335
336    ma_uint64 frame_count;
337    ma_uint64 frames_read;
338    result = ma_decoder_get_length_in_pcm_frames(&decoder, &frame_count);
339    if (result != MA_SUCCESS) {
340        ma_decoder_uninit(&decoder);
341        return false;
342    }
343
344    pcmf32_mono.resize(frame_count);
345    result = ma_decoder_read_pcm_frames(&decoder, pcmf32_mono.data(), frame_count, &frames_read);
346    if (result != MA_SUCCESS) {
347        ma_decoder_uninit(&decoder);
348        return false;
349    }
350
351#ifdef MTMD_AUDIO_DEBUG
352    // save audio to wav file
353    ma_encoder_config config = ma_encoder_config_init(ma_encoding_format_wav, ma_format_f32, 1, target_sampler_rate);
354    ma_encoder encoder;
355    ma_encoder_init_file("output.wav", &config, &encoder);
356    ma_encoder_write_pcm_frames(&encoder, pcmf32_mono.data(), pcmf32_mono.size(), &frames_read);
357    ma_encoder_uninit(&encoder);
358#endif
359
360    ma_decoder_uninit(&decoder);
361    return true;
362}
363
364} // namespace audio_helpers
365
366static bool is_webp_file(const unsigned char * buf, size_t len) {
367    // WEBP ref: https://developers.google.com/speed/webp/docs/riff_container
368    return len >= 12 && memcmp(buf, "RIFF", 4) == 0 && memcmp(buf + 8, "WEBP", 4) == 0;
369}
370
371#ifdef MTMD_VIDEO
372static mtmd_bitmap * decode_webp_with_ffmpeg(const mtmd_context * mctx, const unsigned char * buf, size_t len, bool placeholder,
373                                             const mtmd_helper_video_init_params & params);
374static void mtmd_helper_video_set_id(mtmd_helper_video * vctx, const std::string & id);
375#endif
376
377mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_buf(const mtmd_context * ctx, const unsigned char * buf, size_t len, bool placeholder,
378                                                            mtmd_helper_init_opt opt) {
379    // calculate the hash if needed
380    std::string id;
381    mtmd_bitmap * result = nullptr;
382
383    GGML_UNUSED(opt); // only used by video code paths
384
385    if (!placeholder) {
386        // use sha256 to prevent cache poisoning
387        id = hash_sha256_hex(buf, len);
388    }
389
390    if (audio_helpers::is_audio_file((const char *)buf, len)) {
391        std::vector<float> pcmf32;
392        const int sample_rate = mtmd_get_audio_sample_rate(ctx);
393        if (sample_rate < 0) {
394            LOG_ERR("This model does not support audio input\n");
395            return {nullptr, nullptr};
396        }
397        if (!audio_helpers::decode_audio_from_buf(buf, len, sample_rate, pcmf32)) {
398            LOG_ERR("Unable to read WAV audio file from buffer\n");
399            return {nullptr, nullptr};
400        }
401        result = mtmd_bitmap_init_from_audio(pcmf32.size(), placeholder ? nullptr : pcmf32.data());
402        mtmd_bitmap_set_id(result, id.empty() ? nullptr : id.c_str());
403        return {result, nullptr};
404    }
405
406    // otherwise, we assume it's an image
407    if (!result) {
408        int nx, ny, nc;
409        auto * data = stbi_load_from_memory(buf, len, &nx, &ny, &nc, 3);
410        if (data) {
411            result = mtmd_bitmap_init(nx, ny, placeholder ? nullptr : data);
412            mtmd_bitmap_set_id(result, id.empty() ? nullptr : id.c_str());
413            stbi_image_free(data);
414            return {result, nullptr};
415        }
416        // otherwise, fallthrough to video decoding (if supported)
417    }
418
419#ifdef MTMD_VIDEO
420    // stb_image does not support webp; decode it with ffmpeg as a single frame
421    if (!result && is_webp_file(buf, len)) {
422        result = decode_webp_with_ffmpeg(ctx, buf, len, placeholder, opt.video_params);
423        if (!result) {
424            LOG_ERR("%s: failed to decode webp buffer\n", __func__);
425            return {nullptr, nullptr};
426        }
427        mtmd_bitmap_set_id(result, id.empty() ? nullptr : id.c_str());
428        return {result, nullptr};
429    }
430#endif
431
432    // last try: load as video
433#ifdef MTMD_VIDEO
434    if (!result) {
435        auto video_ctx = mtmd_helper_video_init_from_buf(ctx, buf, len, opt.video_params);
436        if (!video_ctx) {
437            LOG_ERR("%s: failed to decode buffer as either image/audio/video\n", __func__);
438            return {nullptr, nullptr};
439        }
440        mtmd_helper_video_set_id(video_ctx, id); // propagate the hash to the frames
441        result = mtmd_bitmap_init_lazy(ctx,
442            id.empty() ? nullptr : id.c_str(),
443            video_ctx,
444            [](size_t, void * user_data, mtmd_bitmap ** out_bitmap, char ** out_text) -> int {
445                auto * vctx = static_cast<mtmd_helper_video *>(user_data);
446                char * text = nullptr;
447                int ret = mtmd_helper_video_read_next(vctx, out_bitmap, &text);
448                *out_text = text; // heap-allocated by read_next; freed automatically by mtmd
449                return ret;
450            });
451         return {result, video_ctx};
452    }
453#else
454    if (!result) {
455        LOG_ERR("%s: failed to decode buffer as either image or audio (video support not compiled in)\n", __func__);
456        return {nullptr, nullptr};
457    }
458#endif
459
460    // should not reach here
461    return {nullptr, nullptr};
462}
463
464mtmd_helper_bitmap_wrapper mtmd_helper_bitmap_init_from_file(const mtmd_context * ctx, const char * fname, bool placeholder,
465                                                             mtmd_helper_init_opt opt) {
466#ifdef _WIN32
467    int wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, NULL, 0);
468    if (!wlen) {
469        LOG_ERR("Unable to convert filename to UTF-16: %s\n", fname);
470        return {nullptr, nullptr};
471    }
472    std::vector<wchar_t> wfname(wlen);
473    wlen = MultiByteToWideChar(CP_UTF8, 0, fname, -1, wfname.data(), wlen);
474    if (!wlen) {
475        LOG_ERR("Unable to convert filename to UTF-16: %s\n", fname);
476        return {nullptr, nullptr};
477    }
478    FILE * f = _wfopen(wfname.data(), L"rb");
479#else
480    FILE * f = fopen(fname, "rb");
481#endif
482    if (!f) {
483        LOG_ERR("Unable to open file %s: %s\n", fname, strerror(errno));
484        return {nullptr, nullptr};
485    }
486
487    std::vector<unsigned char> buf;
488
489    fseek(f, 0, SEEK_END);
490    long file_size = ftell(f);
491    fseek(f, 0, SEEK_SET);
492    if (file_size < 0) {
493        LOG_ERR("Failed to get file size of %s\n", fname);
494        fclose(f);
495        return {nullptr, nullptr};
496    }
497    buf.resize(file_size);
498
499    size_t n_read = fread(buf.data(), 1, file_size, f);
500    fclose(f);
501    if (n_read != (size_t)file_size) {
502        LOG_ERR("Failed to read entire file %s", fname);
503        return {nullptr, nullptr};
504    }
505
506    return mtmd_helper_bitmap_init_from_buf(ctx, buf.data(), buf.size(), placeholder, opt);
507}
508
509bool mtmd_helper_support_video(const mtmd_context * ctx) {
510#ifdef MTMD_VIDEO
511    return mtmd_support_vision(ctx);
512#else
513    GGML_UNUSED(ctx);
514    return false;
515#endif
516}
517
518//
519// Video input helpers
520//
521
522#ifdef MTMD_VIDEO
523
524struct mtmd_helper_video {
525    const mtmd_context * mctx;
526    std::string path;
527    std::vector<uint8_t> input_buf; // non-empty when initialized from buffer
528    std::string ffmpeg_bin;
529    std::string ffprobe_bin;
530    float fps_target = 0.0f;
531    mtmd_helper_video_info info = {};
532    std::string id; // hash of the input video
533
534    // RAII wrapper for managing subprocess
535    struct subprocess_handle {
536        struct subprocess_s proc = {};
537        bool created = false; // process exists and must be cleaned up
538        bool alive   = false; // process can still give us data
539        std::thread feeder;
540
541        subprocess_handle() = default;
542        subprocess_handle(const subprocess_handle &) = delete;
543        subprocess_handle & operator=(const subprocess_handle &) = delete;
544        ~subprocess_handle() { stop(); }
545
546        void stop() {
547            // note: alive becomes false on stdout EOF, but the process still needs cleanup
548            if (!created) {
549                return;
550            }
551            subprocess_terminate(&proc);
552#ifdef _WIN32
553            // no SIGPIPE on windows: a blocked feeder only gets a broken pipe once we close our read end of the child stdin
554            if (proc.hStdInput) {
555                CloseHandle(proc.hStdInput);
556                proc.hStdInput = nullptr;
557            }
558#endif
559            // join before destroy: feeder holds a FILE* from subprocess_stdin;
560            // subprocess_destroy closes it, so the thread must finish first
561            if (feeder.joinable()) {
562                feeder.join();
563            }
564            subprocess_join(&proc, nullptr); // reap the child, or else it stays a zombie
565            subprocess_destroy(&proc);
566            created = false;
567            alive   = false;
568        }
569
570        FILE * stdout_pipe() {
571            return subprocess_stdout(&proc);
572        }
573
574        // buf is tied to lifetime of mtmd_helper_video, so it's guaranteed to outlive the feeder thread
575        void start_feeder(const std::vector<uint8_t> & buf) {
576            feeder = std::thread([this, &buf]() {
577#ifndef _WIN32
578                // ffmpeg can exit before it reads all the input, for example when ffprobe already got the metadata.
579                // the write below must then fail with EPIPE, instead of killing the process with SIGPIPE
580                sigset_t sigpipe_set;
581                sigemptyset(&sigpipe_set);
582                sigaddset(&sigpipe_set, SIGPIPE);
583                pthread_sigmask(SIG_BLOCK, &sigpipe_set, nullptr); // linux sends the signal to the writing thread
584#endif
585                FILE * f = subprocess_stdin(&proc);
586                if (!f) {
587                    return;
588                }
589#ifdef F_SETNOSIGPIPE
590                fcntl(fileno(f), F_SETNOSIGPIPE, 1); // macos/bsd send it to the process, so turn it off per fd
591#endif
592                fwrite(buf.data(), 1, buf.size(), f);
593                fclose(f);
594                proc.stdin_file = nullptr; // prevent double-close in subprocess_destroy
595            });
596        }
597    };
598
599    subprocess_handle sp;
600    int32_t current_frame = 0;
601
602    std::string prompt_start         = "Video:";
603    int32_t     timestamp_interval_ms = 5000; // emit a timestamp text every N ms (0 = disabled)
604    float       next_timestamp_ms     = 0.0f; // next elapsed-ms threshold at which to emit
605
606    std::vector<uint8_t> frame_buf;
607    std::string pending_text; // text queued to be returned before the next frame
608    bool        start_emitted = false;
609
610    bool is_buf_input() const {
611        return !input_buf.empty();
612    }
613
614    bool probe(float fps_target_arg) {
615        const char * input_arg = is_buf_input() ? "pipe:0" : path.c_str();
616        const char * cmd[] = {
617            ffprobe_bin.c_str(),
618            "-v", "quiet",
619            "-show_entries", "stream=width,height,r_frame_rate,nb_frames,duration",
620            "-select_streams", "v:0",
621            "-of", "default=noprint_wrappers=1",
622            input_arg,
623            nullptr,
624        };
625
626        LOG_DBG("%s: launching:", __func__);
627        for (size_t i = 0; cmd[i]; i++) { LOG_DBG(" %s", cmd[i]); }
628        LOG_DBG("\n");
629
630        subprocess_handle probe_sp;
631        if (subprocess_create(cmd,
632                subprocess_option_search_user_path | subprocess_option_inherit_environment,
633                &probe_sp.proc) != 0) {
634            LOG_ERR("%s: failed to launch ffprobe\n", __func__);
635            return false;
636        }
637        probe_sp.created = true;
638        probe_sp.alive   = true;
639
640        if (is_buf_input()) {
641            probe_sp.start_feeder(input_buf);
642        }
643
644        uint32_t width  = 0;
645        uint32_t height = 0;
646        float orig_fps = 0.0f;
647        float duration = -1.0f;
648        int32_t n_frames_orig = -1;
649        char line[256];
650        FILE * fp = probe_sp.stdout_pipe();
651
652        while (fgets(line, sizeof(line), fp)) {
653            char * eq = strchr(line, '=');
654            if (!eq) continue;
655            *eq = '\0';
656            const char * key = line;
657            const char * val = eq + 1;
658            char * nl = (char *)strchr(val, '\n');
659            if (nl) *nl = '\0';
660
661            if (strcmp(key, "width") == 0) {
662                width = (uint32_t)atoi(val);
663            } else if (strcmp(key, "height") == 0) {
664                height = (uint32_t)atoi(val);
665            } else if (strcmp(key, "r_frame_rate") == 0) {
666                orig_fps = parse_rational(val);
667            } else if (strcmp(key, "nb_frames") == 0 && strcmp(val, "N/A") != 0) {
668                n_frames_orig = atoi(val);
669            } else if (strcmp(key, "duration") == 0 && strcmp(val, "N/A") != 0) {
670                duration = (float)atof(val);
671            }
672        }
673
674        probe_sp.stop();
675
676        if (width == 0 || height == 0 || orig_fps <= 0.0f) {
677            return false;
678        }
679
680        if (duration < 0.0f && n_frames_orig > 0) {
681            duration = (float)n_frames_orig / orig_fps;
682        }
683
684        fps_target = fps_target_arg > 0.0f ? fps_target_arg : orig_fps;
685        info.width    = width;
686        info.height   = height;
687        info.fps      = fps_target;
688        LOG_DBG("%s: %ux%u fps=%.2f duration=%.2fs n_frames=%d\n",
689                __func__, width, height, fps_target, duration, info.n_frames);
690        info.n_frames = duration > 0.0f ? (int32_t)(duration * fps_target + 0.5f) : -1;
691        frame_buf.resize((size_t)width * height * 3);
692        return true;
693    }
694
695    bool start_ffmpeg(float seek_seconds) {
696        char seek_buf[64];
697        char fps_buf[64];
698
699        std::vector<const char *> cmd;
700        cmd.push_back(ffmpeg_bin.c_str());
701
702        if (!is_buf_input() && seek_seconds > 0.0f) {
703            // input-side seek: fast, keyframe-accurate; only valid for seekable file inputs
704            snprintf(seek_buf, sizeof(seek_buf), "%.6f", seek_seconds);
705            cmd.push_back("-ss");
706            cmd.push_back(seek_buf);
707        }
708
709        cmd.push_back("-nostdin");
710        if (is_buf_input()) {
711            // remove the 64KB read-ahead limit of cache:, or else ffmpeg cannot reach a moov atom at end of file
712            cmd.push_back("-read_ahead_limit");
713            cmd.push_back("-1");
714        }
715        cmd.push_back("-i");
716        // cache:pipe:0 wraps stdin with a seekable in-memory cache, letting ffmpeg seek
717        // backwards for container headers (e.g. MP4 moov atom at end of file)
718        cmd.push_back(is_buf_input() ? "cache:pipe:0" : path.c_str());
719
720        if (seek_seconds > 0.0f && is_buf_input()) {
721            // output-side seek: frame-accurate but decodes and discards frames up to seek point
722            snprintf(seek_buf, sizeof(seek_buf), "%.6f", seek_seconds);
723            cmd.push_back("-ss");
724            cmd.push_back(seek_buf);
725        }
726
727        if (fps_target > 0.0f) {
728            snprintf(fps_buf, sizeof(fps_buf), "fps=%.6f", fps_target);
729            cmd.push_back("-vf");
730            cmd.push_back(fps_buf);
731        }
732
733        cmd.push_back("-f");
734        cmd.push_back("rawvideo");
735        cmd.push_back("-pix_fmt");
736        cmd.push_back("rgb24");
737        cmd.push_back("pipe:1");
738        cmd.push_back("-loglevel");
739        cmd.push_back("error");
740        cmd.push_back(nullptr);
741
742        LOG_DBG("%s: launching:", __func__);
743        for (size_t i = 0; cmd[i]; i++) {
744            LOG_DBG(" %s", cmd[i]);
745        }
746        LOG_DBG("\n");
747
748        int ret = subprocess_create(
749            cmd.data(),
750            subprocess_option_search_user_path | subprocess_option_inherit_environment,
751            &sp.proc);
752
753        sp.created = (ret == 0);
754        sp.alive   = (ret == 0);
755        LOG_DBG("%s: subprocess_create ret=%d proc_alive=%d\n", __func__, ret, (int)sp.alive);
756
757        if (sp.alive && is_buf_input()) {
758            LOG_DBG("%s: starting feeder thread for %zu-byte buffer\n", __func__, input_buf.size());
759            sp.start_feeder(input_buf);
760        }
761
762        return sp.alive;
763    }
764
765    void stop_ffmpeg() {
766        sp.stop();
767    }
768
769    mtmd_bitmap * read_next_frame() {
770        if (!sp.alive) return nullptr;
771
772        FILE * fp = sp.stdout_pipe();
773        const size_t frame_size = (size_t)info.width * info.height * 3;
774        LOG_DBG("%s: reading frame %d, expecting %zu bytes (%ux%u)\n",
775                __func__, current_frame, frame_size, info.width, info.height);
776
777        size_t total_read = 0;
778        while (total_read < frame_size) {
779            size_t n = fread(frame_buf.data() + total_read, 1, frame_size - total_read, fp);
780            if (n == 0) {
781                // clean EOF only if no bytes read yet; partial frame is an error
782                LOG_DBG("%s: fread returned 0 after %zu/%zu bytes (ferror=%d)\n",
783                        __func__, total_read, frame_size, ferror(fp));
784                sp.alive = false;
785                return nullptr;
786            }
787            total_read += n;
788        }
789
790        LOG_DBG("%s: frame %d read OK\n", __func__, current_frame);
791        mtmd_bitmap * frame = mtmd_bitmap_init(info.width, info.height, frame_buf.data());
792        mtmd_bitmap_set_mergeable(frame, true);
793        if (!id.empty()) {
794            // each frame gets a unique id in the form of {hash}+{frame}, so that it can be identified in cache
795            std::string frame_id = id + "+" + std::to_string(current_frame);
796            mtmd_bitmap_set_id(frame, frame_id.c_str());
797        }
798        current_frame++;
799        return frame;
800    }
801
802    int32_t read_next(mtmd_bitmap ** out_bitmap, char ** out_text) {
803        *out_bitmap = nullptr;
804        *out_text   = nullptr;
805
806        if (!pending_text.empty()) {
807            *out_text = strdup(pending_text.c_str());
808            pending_text.clear();
809            return *out_text ? 0 : -2;
810        }
811
812        LOG_DBG("%s: proc_alive=%d start_emitted=%d current_frame=%d\n",
813                __func__, (int)sp.alive, (int)start_emitted, current_frame);
814
815        if (!sp.alive) {
816            return (current_frame == 0) ? -2 : -1;
817        }
818
819        if (!start_emitted) {
820            start_emitted = true;
821            if (!prompt_start.empty()) {
822                *out_text = strdup(prompt_start.c_str());
823                return *out_text ? 0 : -2;
824            }
825        }
826
827        mtmd_bitmap * frame = read_next_frame();
828        if (!frame) return -1;
829        *out_bitmap = frame;
830
831        if (timestamp_interval_ms > 0) {
832            // current_frame was already incremented by read_next_frame(); undo for elapsed calc
833            float elapsed_ms = (float)(current_frame - 1) / info.fps * 1000.0f;
834            if (elapsed_ms >= next_timestamp_ms) {
835                char ts_buf[32];
836                float elapsed_s = elapsed_ms / 1000.0f;
837                int   minutes   = (int)(elapsed_s / 60);
838                float seconds   = elapsed_s - minutes * 60.0f;
839                snprintf(ts_buf, sizeof(ts_buf), "[%dm%.2fs]", minutes, seconds);
840                pending_text = ts_buf;
841                next_timestamp_ms += (float)timestamp_interval_ms;
842            }
843        }
844
845        return 0;
846    }
847
848    static float parse_rational(const char * s) {
849        int num = 0, den = 1;
850        if (sscanf(s, "%d/%d", &num, &den) == 2 && den > 0) {
851            return (float)num / (float)den;
852        }
853        float val;
854        if (sscanf(s, "%f", &val) == 1) {
855            return val;
856        }
857        return 0.0f;
858    }
859};
860#endif
861
862mtmd_helper_video_init_params mtmd_helper_video_init_params_default() {
863    return {
864        /* fps_target             */ 4.0f,
865        /* ffmpeg_bin_dir         */ nullptr,
866        /* timestamp_interval_ms  */ 5000,
867    };
868}
869
870mtmd_helper_init_opt mtmd_helper_init_opt_default() {
871    return {
872        /* video_params */ mtmd_helper_video_init_params_default(),
873    };
874}
875
876static std::string video_resolve_bin(const char * bin_dir, const char * name) {
877    if (!bin_dir || bin_dir[0] == '\0') {
878        return name; // rely on PATH
879    }
880    std::string result = bin_dir;
881    char last = result.back();
882    if (last != '/' && last != '\\') {
883#ifdef _WIN32
884        result += '\\';
885#else
886        result += '/';
887#endif
888    }
889    result += name;
890#ifdef _WIN32
891    result += ".exe";
892#endif
893    return result;
894}
895
896#ifdef MTMD_VIDEO
897static void mtmd_helper_video_set_id(mtmd_helper_video * vctx, const std::string & id) {
898    vctx->id = id;
899}
900
901static mtmd_bitmap * decode_webp_with_ffmpeg(const mtmd_context * mctx, const unsigned char * buf, size_t len, bool placeholder,
902                                             const mtmd_helper_video_init_params & params) {
903    mtmd_helper_video vctx;
904    vctx.mctx        = mctx;
905    vctx.input_buf.assign(buf, buf + len);
906    vctx.ffmpeg_bin  = video_resolve_bin(params.ffmpeg_bin_dir, "ffmpeg");
907    vctx.ffprobe_bin = video_resolve_bin(params.ffmpeg_bin_dir, "ffprobe");
908    if (!vctx.probe(0.0f)) {
909        return nullptr;
910    }
911    if (placeholder) {
912        return mtmd_bitmap_init(vctx.info.width, vctx.info.height, nullptr);
913    }
914    // still image: the fps filter would output no frame, so disable it
915    vctx.fps_target = 0.0f;
916    if (!vctx.start_ffmpeg(0.0f)) {
917        return nullptr;
918    }
919    mtmd_bitmap * frame = vctx.read_next_frame();
920    if (frame) {
921        mtmd_bitmap_set_mergeable(frame, false);
922    }
923    return frame;
924}
925#endif
926
927mtmd_helper_video * mtmd_helper_video_init(
928        const mtmd_context * mctx,
929        const char * path,
930        mtmd_helper_video_init_params params) {
931#ifdef MTMD_VIDEO
932    auto * ctx = new mtmd_helper_video();
933
934    ctx->mctx                 = mctx;
935    ctx->path                 = path;
936    ctx->ffmpeg_bin           = video_resolve_bin(params.ffmpeg_bin_dir, "ffmpeg");
937    ctx->ffprobe_bin          = video_resolve_bin(params.ffmpeg_bin_dir, "ffprobe");
938    ctx->timestamp_interval_ms = params.timestamp_interval_ms;
939
940    if (!ctx->probe(params.fps_target)) {
941        LOG_ERR("%s: ffprobe failed for '%s' (is ffprobe in PATH?)\n", __func__, path);
942        delete ctx;
943        return nullptr;
944    }
945
946    if (!ctx->start_ffmpeg(0.0f)) {
947        LOG_ERR("%s: failed to start ffmpeg for '%s' (is ffmpeg in PATH?)\n", __func__, path);
948        delete ctx;
949        return nullptr;
950    }
951
952    return ctx;
953#else
954    GGML_UNUSED(mctx);
955    GGML_UNUSED(path);
956    GGML_UNUSED(params);
957    LOG_ERR("%s: video is not supported in this build (MTMD_VIDEO is set to OFF)\n", __func__);
958    return nullptr;
959#endif
960}
961
962mtmd_helper_video * mtmd_helper_video_init_from_buf(
963        const mtmd_context * mctx,
964        const unsigned char * buf, size_t len,
965        mtmd_helper_video_init_params params) {
966#ifdef MTMD_VIDEO
967    auto * ctx = new mtmd_helper_video();
968
969    ctx->mctx                  = mctx;
970    ctx->input_buf.assign(buf, buf + len);
971    ctx->ffmpeg_bin            = video_resolve_bin(params.ffmpeg_bin_dir, "ffmpeg");
972    ctx->ffprobe_bin           = video_resolve_bin(params.ffmpeg_bin_dir, "ffprobe");
973    ctx->timestamp_interval_ms = params.timestamp_interval_ms;
974
975    if (!ctx->probe(params.fps_target)) {
976        LOG_ERR("%s: ffprobe failed on buffer (is ffprobe in PATH?)\n", __func__);
977        delete ctx;
978        return nullptr;
979    }
980
981    if (!ctx->start_ffmpeg(0.0f)) {
982        LOG_ERR("%s: failed to start ffmpeg on buffer (is ffmpeg in PATH?)\n", __func__);
983        delete ctx;
984        return nullptr;
985    }
986
987    return ctx;
988#else
989    GGML_UNUSED(mctx);
990    GGML_UNUSED(buf);
991    GGML_UNUSED(len);
992    GGML_UNUSED(params);
993    LOG_ERR("%s: video is not supported in this build (MTMD_VIDEO is set to OFF)\n", __func__);
994    return nullptr;
995#endif
996}
997
998void mtmd_helper_video_free(mtmd_helper_video * ctx) {
999#ifdef MTMD_VIDEO
1000    if (!ctx) return;
1001    ctx->stop_ffmpeg();
1002    delete ctx;
1003#else
1004    GGML_UNUSED(ctx);
1005    LOG_ERR("%s: video is not supported in this build (MTMD_VIDEO is set to OFF)\n", __func__);
1006#endif
1007}
1008
1009mtmd_helper_video_info mtmd_helper_video_get_info(const mtmd_helper_video * ctx) {
1010#ifdef MTMD_VIDEO
1011    return ctx->info;
1012#else
1013    GGML_UNUSED(ctx);
1014    GGML_ASSERT(false && "video is not supported in this build (MTMD_VIDEO is set to OFF)");
1015#endif
1016}
1017
1018int32_t mtmd_helper_video_read_next(mtmd_helper_video * ctx,
1019        mtmd_bitmap ** out_bitmap, char ** out_text) {
1020#ifdef MTMD_VIDEO
1021    if (!ctx) return -2;
1022    return ctx->read_next(out_bitmap, out_text);
1023#else
1024    GGML_UNUSED(ctx);
1025    GGML_UNUSED(out_bitmap);
1026    GGML_UNUSED(out_text);
1027    GGML_ASSERT(false && "video is not supported in this build (MTMD_VIDEO is set to OFF)");
1028#endif
1029}
1030
1031bool mtmd_helper_model_can_chat(const llama_context * lctx, const mtmd_context * mctx) {
1032    if (!mctx) {
1033        return true;
1034    }
1035
1036    auto * model = llama_get_model(lctx);
1037    auto * tmpl = llama_model_chat_template(model, nullptr);
1038    auto info = mtmd_gen_audio_get_info(mctx);
1039
1040    // tts-only model cannot be used for chat (no chat template)
1041    bool is_tts_only = info.type != MTMD_GEN_AUDIO_TYPE_NONE && tmpl == nullptr;
1042
1043    return !is_tts_only;
1044}