Coverage Report

Created: 2026-11-30 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/player/sub.c
Line
Count
Source
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <stddef.h>
19
#include <stdbool.h>
20
#include <inttypes.h>
21
#include <math.h>
22
#include <assert.h>
23
24
#include "mpv_talloc.h"
25
26
#include "common/msg.h"
27
#include "options/options.h"
28
#include "common/common.h"
29
30
#include "stream/stream.h"
31
#include "sub/dec_sub.h"
32
#include "demux/demux.h"
33
#include "video/mp_image.h"
34
35
#include "core.h"
36
37
// 0: primary sub, 1: secondary sub, -1: not selected
38
static int get_order(struct MPContext *mpctx, struct track *track)
39
70.1k
{
40
70.1k
    for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) {
41
70.1k
        if (mpctx->current_track[n][STREAM_SUB] == track)
42
70.1k
            return n;
43
70.1k
    }
44
0
    return -1;
45
70.1k
}
46
47
static void reset_subtitles(struct MPContext *mpctx, struct track *track)
48
5.62M
{
49
5.62M
    if (track->d_sub) {
50
176k
        sub_reset(track->d_sub);
51
176k
        sub_set_play_dir(track->d_sub, mpctx->play_dir);
52
176k
    }
53
5.62M
}
54
55
// Only matters for subs on an image.
56
void redraw_subs(struct MPContext *mpctx)
57
3.18M
{
58
9.55M
    for (int n = 0; n < num_ptracks[STREAM_SUB]; n++) {
59
6.36M
        if (mpctx->current_track[n][STREAM_SUB] &&
60
170k
            mpctx->current_track[n][STREAM_SUB]->d_sub)
61
170k
        {
62
170k
            mpctx->current_track[n][STREAM_SUB]->redraw_subs = true;
63
170k
        }
64
6.36M
    }
65
3.18M
}
66
67
void reset_subtitle_state(struct MPContext *mpctx)
68
3.50M
{
69
9.11M
    for (int n = 0; n < mpctx->num_tracks; n++)
70
5.60M
        reset_subtitles(mpctx, mpctx->tracks[n]);
71
3.50M
    term_osd_clear_subs(mpctx);
72
3.50M
}
73
74
void uninit_sub(struct MPContext *mpctx, struct track *track)
75
324k
{
76
324k
    if (track && track->d_sub) {
77
14.4k
        int order = get_order(mpctx, track);
78
14.4k
        reset_subtitles(mpctx, track);
79
14.4k
        sub_select(track->d_sub, false);
80
14.4k
        if (order >= 0) {
81
14.4k
            term_osd_set_subs(mpctx, NULL, order);
82
14.4k
            osd_set_sub(mpctx->osd, order, NULL);
83
14.4k
        }
84
14.4k
        sub_destroy(track->d_sub);
85
14.4k
        track->d_sub = NULL;
86
14.4k
    }
87
324k
}
88
89
void uninit_sub_all(struct MPContext *mpctx)
90
801k
{
91
1.12M
    for (int n = 0; n < mpctx->num_tracks; n++)
92
318k
        uninit_sub(mpctx, mpctx->tracks[n]);
93
801k
}
94
95
static bool update_subtitle(struct MPContext *mpctx, double video_pts,
96
                            struct track *track)
97
23.7M
{
98
23.7M
    struct dec_sub *dec_sub = track ? track->d_sub : NULL;
99
100
23.7M
    if (!dec_sub)
101
22.5M
        return true;
102
103
1.21M
    if (mpctx->vo_chain) {
104
1.19M
        struct mp_image_params params = mpctx->vo_chain->filter->input_params;
105
1.19M
        if (params.imgfmt)
106
1.15M
            sub_control(dec_sub, SD_CTRL_SET_VIDEO_PARAMS, &params);
107
1.19M
    }
108
109
    // Checking if packets have special animations is relatively expensive.
110
    // This is only needed if we are rendering ASS subtitles with no video
111
    // being played.
112
1.21M
    bool still_image = mpctx->video_out && ((mpctx->video_status == STATUS_EOF &&
113
648k
                       mpctx->opts->subs_rend->sub_past_video_end) ||
114
1.19M
                       !mpctx->current_track[0][STREAM_VIDEO] ||
115
1.19M
                       mpctx->current_track[0][STREAM_VIDEO]->image);
116
1.21M
    sub_control(dec_sub, SD_CTRL_SET_ANIMATED_CHECK, &still_image);
117
118
1.21M
    if (track->demuxer->fully_read && sub_can_preload(dec_sub)) {
119
        // Assume fully_read implies no interleaved audio/video streams.
120
        // (Reading packets will change the demuxer position.)
121
7.11k
        demux_seek(track->demuxer, 0, 0);
122
7.11k
        sub_preload(dec_sub);
123
7.11k
    }
124
125
1.21M
    bool packets_read = false;
126
1.21M
    bool sub_updated = false;
127
1.21M
    sub_read_packets(dec_sub, video_pts, mpctx->paused, &packets_read, &sub_updated);
128
129
1.21M
    double osd_pts = osd_get_force_video_pts(mpctx->osd);
130
131
    // Check if we need to update subtitles for these special cases. Always
132
    // update on discontinuities like seeking or a new file.
133
1.21M
    if (sub_updated || track->redraw_subs || osd_pts == MP_NOPTS_VALUE) {
134
        // Always force a redecode of all packets if we seek on a still image.
135
1.21M
        if (track->redraw_subs && still_image)
136
736
            sub_redecode_cached_packets(dec_sub);
137
138
        // Handle displaying subtitles on terminal.
139
1.21M
        if (track->selected && track->type == STREAM_SUB && !mpctx->video_out) {
140
26.5k
            char *text = sub_get_text(dec_sub, video_pts, SD_TEXT_TYPE_PLAIN);
141
26.5k
            int order = get_order(mpctx, track);
142
26.5k
            if (order >= 0)
143
26.5k
                term_osd_set_subs(mpctx, text, order);
144
26.5k
            talloc_free(text);
145
26.5k
        }
146
147
        // Handle displaying subtitles on VO with no video being played. This is
148
        // quite different, because normally subtitles are redrawn on new video
149
        // frames, using the video frames' timestamps.
150
1.21M
        if (still_image) {
151
3.18k
            if (osd_pts != video_pts) {
152
1.69k
                osd_set_force_video_pts(mpctx->osd, video_pts);
153
1.69k
                osd_query_and_reset_want_redraw(mpctx->osd);
154
1.69k
                vo_redraw(mpctx->video_out);
155
1.69k
            }
156
3.18k
        }
157
1.21M
    }
158
159
1.21M
    track->redraw_subs = false;
160
1.21M
    return packets_read;
161
23.7M
}
162
163
// Return true if the subtitles for the given PTS are ready; false if the player
164
// should wait for new demuxer data, and then should retry.
165
bool update_subtitles(struct MPContext *mpctx, double video_pts)
166
11.8M
{
167
11.8M
    bool ok = true;
168
35.6M
    for (int n = 0; n < num_ptracks[STREAM_SUB]; n++)
169
23.7M
        ok &= update_subtitle(mpctx, video_pts, mpctx->current_track[n][STREAM_SUB]);
170
11.8M
    return ok;
171
11.8M
}
172
173
static struct attachment_list *get_all_attachments(struct MPContext *mpctx)
174
14.6k
{
175
14.6k
    struct attachment_list *list = talloc_zero(NULL, struct attachment_list);
176
14.6k
    struct demuxer *prev_demuxer = NULL;
177
124k
    for (int n = 0; n < mpctx->num_tracks; n++) {
178
109k
        struct track *t = mpctx->tracks[n];
179
109k
        if (!t->demuxer || prev_demuxer == t->demuxer)
180
27.8k
            continue;
181
81.4k
        prev_demuxer = t->demuxer;
182
82.1k
        for (int i = 0; i < t->demuxer->num_attachments; i++) {
183
641
            struct demux_attachment *att = &t->demuxer->attachments[i];
184
641
            struct demux_attachment copy = {
185
641
                .name = talloc_strdup(list, att->name),
186
641
                .type = talloc_strdup(list, att->type),
187
641
                .data = talloc_memdup(list, att->data, att->data_size),
188
641
                .data_size = att->data_size,
189
641
            };
190
641
            MP_TARRAY_APPEND(list, list->entries, list->num_entries, copy);
191
641
        }
192
81.4k
    }
193
14.6k
    return list;
194
14.6k
}
195
196
static bool init_subdec(struct MPContext *mpctx, struct track *track)
197
14.6k
{
198
14.6k
    mp_assert(!track->d_sub);
199
200
14.6k
    if (!track->demuxer || !track->stream)
201
0
        return false;
202
203
14.6k
    track->d_sub = sub_create(mpctx->global, track,
204
14.6k
                              get_all_attachments(mpctx),
205
14.6k
                              get_order(mpctx, track));
206
14.6k
    if (!track->d_sub)
207
233
        return false;
208
209
14.4k
    struct track *vtrack = mpctx->current_track[0][STREAM_VIDEO];
210
14.4k
    struct mp_codec_params *v_c =
211
14.4k
        vtrack && vtrack->stream ? vtrack->stream->codec : NULL;
212
14.4k
    double fps = v_c ? v_c->fps : 25;
213
14.4k
    sub_control(track->d_sub, SD_CTRL_SET_VIDEO_DEF_FPS, &fps);
214
215
14.4k
    return true;
216
14.6k
}
217
218
void reinit_sub(struct MPContext *mpctx, struct track *track)
219
465k
{
220
465k
    if (!track || !track->stream || track->stream->type != STREAM_SUB)
221
450k
        return;
222
223
14.6k
    mp_assert(!track->d_sub);
224
225
14.6k
    if (!init_subdec(mpctx, track)) {
226
233
        track->dec_failed = true;
227
233
        error_on_track(mpctx, track);
228
233
        return;
229
233
    }
230
231
14.4k
    sub_select(track->d_sub, true);
232
14.4k
    int order = get_order(mpctx, track);
233
14.4k
    osd_set_sub(mpctx->osd, order, track->d_sub);
234
235
    // When paused we have to wait for packets to be available.
236
    // Retry on a timeout until we get a packet. If still not successful,
237
    // then queue it for later in the playloop (but this will have a delay).
238
14.4k
    if (mpctx->playback_initialized) {
239
5.11k
        track->demuxer_ready = false;
240
5.11k
        int64_t end = mp_time_ns() + MP_TIME_MS_TO_NS(50);
241
10.2k
        while (!track->demuxer_ready && mp_time_ns() < end)
242
5.11k
            track->demuxer_ready = update_subtitles(mpctx, mpctx->playback_pts) ||
243
0
                                  !mpctx->paused;
244
5.11k
        if (!track->demuxer_ready)
245
0
            mp_wakeup_core(mpctx);
246
247
5.11k
    }
248
14.4k
}
249
250
void reinit_sub_all(struct MPContext *mpctx)
251
229k
{
252
689k
    for (int n = 0; n < num_ptracks[STREAM_SUB]; n++)
253
459k
        reinit_sub(mpctx, mpctx->current_track[n][STREAM_SUB]);
254
229k
}