Commit 868e5557 for whisper.cpp

commit 868e55578ea5f69550c046da22aefe11dc6e04c0
Author: Jürgen Fleiß <118339672+JuergenFleiss@users.noreply.github.com>
Date:   Wed Nov 25 18:23:27 2026 +0100

    whisper : add correct dtw start time for tokens, map dtw timings to non vad timings (#4170)

diff --git a/examples/cli/cli.cpp b/examples/cli/cli.cpp
index 85eed32b..dea33fa0 100644
--- a/examples/cli/cli.cpp
+++ b/examples/cli/cli.cpp
@@ -762,12 +762,15 @@ static void output_json(
                             std::string        text;
                             whisper_token_data data;
                             int64_t            t1;
+                            int64_t            dtw_t0;
+                            int64_t            dtw_t1;
                         };
                         std::vector<merged_token> merged;
                         merged.reserve(n);
                         for (int j = 0; j < n; ) {
                             auto tok = whisper_full_get_token_data(ctx, i, j);
-                            merged_token m{ whisper_token_to_str(ctx, tok.id), tok, tok.t1 };
+                            merged_token m{ whisper_token_to_str(ctx, tok.id), tok, tok.t1,
+                                whisper_full_get_token_dtw_t0(ctx, i, j), whisper_full_get_token_dtw_t1(ctx, i, j) };
                             ++j;
                             while (j < n && utf8_trailing_bytes_needed(m.text) > 0) {
                                 auto tok_next = whisper_full_get_token_data(ctx, i, j);
@@ -775,6 +778,9 @@ static void output_json(
                                 if (tok_next.t1 > -1) {
                                     m.t1 = tok_next.t1;
                                 }
+                                if (tok_next.t_dtw > -1) {
+                                    m.dtw_t1 = whisper_full_get_token_dtw_t1(ctx, i, j);
+                                }
                                 ++j;
                             }
                             merged.push_back(std::move(m));
@@ -791,7 +797,8 @@ static void output_json(
                                 }
                                 value_i("id", mt.data.id, false);
                                 value_f("p", mt.data.p, false);
-                                value_f("t_dtw", mt.data.t_dtw, true);
+                                value_f("t_dtw_start", mt.dtw_t0, false);
+                                value_f("t_dtw", mt.dtw_t1, true);
                             end_obj(j == (nm - 1));
                         }
                         end_arr(!params.diarize && !params.tinydiarize);
diff --git a/include/whisper.h b/include/whisper.h
index 545084ef..717f9bd0 100644
--- a/include/whisper.h
+++ b/include/whisper.h
@@ -144,10 +144,14 @@ extern "C" {

         // [EXPERIMENTAL] Token-level timestamps with DTW
         // do not use if you haven't computed token-level timestamps with dtw
-        // Roughly corresponds to the moment in audio in which the token was output
+        // The moment in audio at which the token ends (the token was output)
         int64_t t_dtw;

         float vlen;        // voice length of the token
+
+        // [EXPERIMENTAL] The moment in audio at which the token starts, from DTW:
+        // the end of the previous text token, or the start of the decoding window
+        int64_t t_dtw_start;
     } whisper_token_data;

     typedef struct whisper_model_loader {
@@ -677,6 +681,13 @@ extern "C" {
     WHISPER_API int64_t whisper_full_get_token_t1           (struct whisper_context * ctx, int i_segment, int i_token);
     WHISPER_API int64_t whisper_full_get_token_t1_from_state(struct whisper_state * state, int i_segment, int i_token);

+    // Same as above for the DTW token times (t_dtw_start/t_dtw), in centiseconds,
+    // mapped back to the original audio when VAD is enabled. -1 if not computed.
+    WHISPER_API int64_t whisper_full_get_token_dtw_t0           (struct whisper_context * ctx, int i_segment, int i_token);
+    WHISPER_API int64_t whisper_full_get_token_dtw_t0_from_state(struct whisper_state * state, int i_segment, int i_token);
+    WHISPER_API int64_t whisper_full_get_token_dtw_t1           (struct whisper_context * ctx, int i_segment, int i_token);
+    WHISPER_API int64_t whisper_full_get_token_dtw_t1_from_state(struct whisper_state * state, int i_segment, int i_token);
+
     // Get the probability of the specified token in the specified segment
     WHISPER_API float whisper_full_get_token_p           (struct whisper_context * ctx, int i_segment, int i_token);
     WHISPER_API float whisper_full_get_token_p_from_state(struct whisper_state * state, int i_segment, int i_token);
diff --git a/src/whisper.cpp b/src/whisper.cpp
index c978b6e6..7043a6df 100644
--- a/src/whisper.cpp
+++ b/src/whisper.cpp
@@ -6636,7 +6636,7 @@ static whisper_token_data whisper_sample_token(
       const whisper_decoder & decoder,
                        bool   best) {
     whisper_token_data result = {
-        0, 0, 0.0f, 0.0f, 0.0f, 0.0f, -1, -1, -1, 0.0f,
+        0, 0, 0.0f, 0.0f, 0.0f, 0.0f, -1, -1, -1, 0.0f, -1,
     };

     const auto & vocab = ctx.vocab;
@@ -6735,7 +6735,7 @@ static std::vector<whisper_token_data> whisper_sample_token_topk(
         const auto id = dist(decoder.rng);
         //printf("XXX %d %d %f %f %f %f\n", id, tid, probs[id], logprobs[id], pt, ptsum);

-        result.push_back({ id, tid, probs[id], logprobs[id], pt, ptsum, -1, -1, -1, 0.0f, });
+        result.push_back({ id, tid, probs[id], logprobs[id], pt, ptsum, -1, -1, -1, 0.0f, -1, });

         if (result[i].id >= vocab.token_beg) {
             result[i].tid = result[i].id;
@@ -8317,6 +8317,30 @@ int64_t whisper_full_get_token_t1(struct whisper_context * ctx, int i_segment, i
     return whisper_full_get_token_t1_from_state(ctx->state, i_segment, i_token);
 }

+int64_t whisper_full_get_token_dtw_t0_from_state(struct whisper_state * state, int i_segment, int i_token) {
+    const int64_t t0 = state->result_all[i_segment].tokens[i_token].t_dtw_start;
+    if (t0 < 0 || !state->has_vad_segments || state->vad_segments.empty()) {
+        return t0;
+    }
+    return whisper_map_token_time_segment_aware(t0, state->vad_segments);
+}
+
+int64_t whisper_full_get_token_dtw_t0(struct whisper_context * ctx, int i_segment, int i_token) {
+    return whisper_full_get_token_dtw_t0_from_state(ctx->state, i_segment, i_token);
+}
+
+int64_t whisper_full_get_token_dtw_t1_from_state(struct whisper_state * state, int i_segment, int i_token) {
+    const int64_t t1 = state->result_all[i_segment].tokens[i_token].t_dtw;
+    if (t1 < 0 || !state->has_vad_segments || state->vad_segments.empty()) {
+        return t1;
+    }
+    return whisper_map_token_time_segment_aware(t1, state->vad_segments);
+}
+
+int64_t whisper_full_get_token_dtw_t1(struct whisper_context * ctx, int i_segment, int i_token) {
+    return whisper_full_get_token_dtw_t1_from_state(ctx->state, i_segment, i_token);
+}
+
 int whisper_full_n_vad_segments_from_state(struct whisper_state * state) {
     return (int) state->vad_segments.size();
 }
@@ -9291,8 +9315,10 @@ static void whisper_exp_compute_token_level_timestamps_dtw(

     ggml_tensor * alignment = dtw_and_backtrace(gctx, w);

-    // Place timestamps on segments
+    // Place timestamps on segments. Each step of the path to the next row ends the
+    // current text token and starts the next one; the first one starts at seek.
     int32_t last_v = 0;
+    int64_t last_t = seek;
     auto seg_i = state->result_all.begin() + i_segment;
     auto tok_i = seg_i->tokens.begin();
     for (int i = 0; i < alignment->ne[1]; ++i) {
@@ -9311,7 +9337,9 @@ static void whisper_exp_compute_token_level_timestamps_dtw(
                 }
             }

+            tok_i->t_dtw_start = last_t;
             tok_i->t_dtw = timestamp;
+            last_t = timestamp;
             ++tok_i;
             if (tok_i == seg_i->tokens.end()) {
                 ++seg_i;