Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/srtdec.c
Line
Count
Source
1
/*
2
 * SubRip subtitle demuxer
3
 * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
4
 * Copyright (c) 2015  Clément Bœsch <u pkh me>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
#include "avformat.h"
24
#include "demux.h"
25
#include "internal.h"
26
#include "subtitles.h"
27
#include "libavutil/bprint.h"
28
#include "libavutil/intreadwrite.h"
29
30
typedef struct {
31
    FFDemuxSubtitlesQueue q;
32
} SRTContext;
33
34
static int srt_probe(const AVProbeData *p)
35
936k
{
36
936k
    int v;
37
936k
    char buf[64], *pbuf;
38
936k
    FFTextReader tr;
39
40
936k
    ff_text_init_buf(&tr, p->buf, p->buf_size);
41
42
2.89M
    while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43
1.96M
        ff_text_r8(&tr);
44
45
    /* Check if the first non-empty line is a number. We do not check what the
46
     * number is because in practice it can be anything.
47
     * Also, that number can be followed by random garbage, so we can not
48
     * unfortunately check that we only have a number. */
49
936k
    if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
50
233k
        strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
51
917k
        return 0;
52
53
    /* Check if the next line matches a SRT timestamp */
54
19.3k
    if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
55
3.44k
        return 0;
56
15.8k
    pbuf = buf;
57
15.8k
    if (buf[0] == '-')
58
787
        pbuf++;
59
15.8k
    if (pbuf[0] >= '0' && pbuf[0] <= '9' && strstr(buf, " --> ")
60
318
        && sscanf(buf, "%*d:%*d:%*d%*1[,.]%*d --> %*d:%*d:%*d%*1[,.]%d", &v) == 1)
61
107
        return AVPROBE_SCORE_MAX;
62
63
15.7k
    return 0;
64
15.8k
}
65
66
struct event_info {
67
    int32_t x1, x2, y1, y2;
68
    int duration;
69
    int64_t pts;
70
    int64_t pos;
71
};
72
73
static int get_event_info(const char *line, struct event_info *ei)
74
1.22M
{
75
1.22M
    int hh1, mm1, ss1, ms1;
76
1.22M
    int hh2, mm2, ss2, ms2;
77
78
1.22M
    ei->x1 = ei->x2 = ei->y1 = ei->y2 = ei->duration = -1;
79
1.22M
    ei->pts = AV_NOPTS_VALUE;
80
1.22M
    ei->pos = -1;
81
1.22M
    if (sscanf(line, "%d:%d:%d%*1[,.]%d --> %d:%d:%d%*1[,.]%d"
82
1.22M
               "%*[ ]X1:%"PRId32" X2:%"PRId32" Y1:%"PRId32" Y2:%"PRId32,
83
1.22M
               &hh1, &mm1, &ss1, &ms1,
84
1.22M
               &hh2, &mm2, &ss2, &ms2,
85
1.22M
               &ei->x1, &ei->x2, &ei->y1, &ei->y2) >= 8) {
86
549k
        const int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
87
549k
        const int64_t end   = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
88
549k
        ei->duration = end - start;
89
549k
        ei->pts = start;
90
549k
        return 0;
91
549k
    }
92
673k
    return -1;
93
1.22M
}
94
95
static int add_event(FFDemuxSubtitlesQueue *q, AVBPrint *buf, char *line_cache,
96
                     const struct event_info *ei, int append_cache)
97
549k
{
98
549k
    if (append_cache && line_cache[0])
99
984
        av_bprintf(buf, "%s\n", line_cache);
100
549k
    line_cache[0] = 0;
101
549k
    if (!av_bprint_is_complete(buf))
102
0
        return AVERROR(ENOMEM);
103
104
1.09M
    while (buf->len > 0 && buf->str[buf->len - 1] == '\n')
105
548k
        buf->str[--buf->len] = 0;
106
107
549k
    if (buf->len) {
108
548k
        AVPacket *sub = ff_subtitles_queue_insert_bprint(q, buf, 0);
109
548k
        if (!sub)
110
0
            return AVERROR(ENOMEM);
111
548k
        av_bprint_clear(buf);
112
548k
        sub->pos = ei->pos;
113
548k
        sub->pts = ei->pts;
114
548k
        sub->duration = ei->duration;
115
548k
        if (ei->x1 != -1) {
116
677
            uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
117
677
            if (p) {
118
677
                AV_WL32(p,      ei->x1);
119
677
                AV_WL32(p +  4, ei->y1);
120
677
                AV_WL32(p +  8, ei->x2);
121
677
                AV_WL32(p + 12, ei->y2);
122
677
            }
123
677
        }
124
548k
    }
125
126
549k
    return 0;
127
549k
}
128
129
static int srt_read_header(AVFormatContext *s)
130
2.07k
{
131
2.07k
    SRTContext *srt = s->priv_data;
132
2.07k
    AVBPrint buf;
133
2.07k
    AVStream *st = avformat_new_stream(s, NULL);
134
2.07k
    int res = 0;
135
2.07k
    char line[4096], line_cache[4096];
136
2.07k
    int has_event_info = 0;
137
2.07k
    struct event_info ei;
138
2.07k
    FFTextReader tr;
139
2.07k
    ff_text_init_avio(s, &tr, s->pb);
140
141
2.07k
    if (!st)
142
0
        return AVERROR(ENOMEM);
143
2.07k
    avpriv_set_pts_info(st, 64, 1, 1000);
144
2.07k
    st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
145
2.07k
    st->codecpar->codec_id   = AV_CODEC_ID_SUBRIP;
146
147
2.07k
    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
148
149
2.07k
    line_cache[0] = 0;
150
151
1.22M
    while (!ff_text_eof(&tr)) {
152
1.22M
        struct event_info tmp_ei;
153
1.22M
        const int64_t pos = ff_text_pos(&tr);
154
1.22M
        ptrdiff_t len = ff_subtitles_read_line(&tr, line, sizeof(line));
155
156
1.22M
        if (len < 0)
157
298
            break;
158
159
1.22M
        if (!len || !line[0])
160
3.37k
            continue;
161
162
1.22M
        if (get_event_info(line, &tmp_ei) < 0) {
163
673k
            char *pline;
164
165
673k
            if (!has_event_info)
166
1.95k
                continue;
167
168
671k
            if (line_cache[0]) {
169
                /* We got some cache and a new line so we assume the cached
170
                 * line was actually part of the payload */
171
23.0k
                av_bprintf(&buf, "%s\n", line_cache);
172
23.0k
                line_cache[0] = 0;
173
23.0k
            }
174
175
            /* If the line doesn't start with a number, we assume it's part of
176
             * the payload, otherwise is likely an event number preceding the
177
             * timing information... but we can't be sure of this yet, so we
178
             * cache it */
179
671k
            if (strtol(line, &pline, 10) < 0 || line == pline)
180
645k
                av_bprintf(&buf, "%s\n", line);
181
25.6k
            else
182
25.6k
                strcpy(line_cache, line);
183
671k
        } else {
184
549k
            if (has_event_info) {
185
                /* We have the information of previous event, append it to the
186
                 * queue. We insert the cached line if and only if the payload
187
                 * is empty and the cached line is not a standalone number. */
188
548k
                char *pline = NULL;
189
548k
                const int standalone_number = strtol(line_cache, &pline, 10) >= 0 && pline && !*pline;
190
548k
                res = add_event(&srt->q, &buf, line_cache, &ei, !buf.len && !standalone_number);
191
548k
                if (res < 0)
192
0
                    goto end;
193
548k
            } else {
194
1.45k
                has_event_info = 1;
195
1.45k
            }
196
549k
            tmp_ei.pos = pos;
197
549k
            ei = tmp_ei;
198
549k
        }
199
1.22M
    }
200
201
    /* Append the last event. Here we force the cache to be flushed, because a
202
     * trailing number is more likely to be genuine (for example a copyright
203
     * date) and not the event index of an inexistent event */
204
2.07k
    if (has_event_info) {
205
1.45k
        res = add_event(&srt->q, &buf, line_cache, &ei, 1);
206
1.45k
        if (res < 0)
207
0
            goto end;
208
1.45k
    }
209
210
2.07k
    ff_subtitles_queue_finalize(s, &srt->q);
211
212
2.07k
end:
213
    av_bprint_finalize(&buf, NULL);
214
2.07k
    return res;
215
2.07k
}
216
217
const FFInputFormat ff_srt_demuxer = {
218
    .p.name      = "srt",
219
    .p.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
220
    .priv_data_size = sizeof(SRTContext),
221
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
222
    .read_probe  = srt_probe,
223
    .read_header = srt_read_header,
224
    .read_packet = ff_subtitles_read_packet,
225
    .read_seek2  = ff_subtitles_read_seek,
226
    .read_close  = ff_subtitles_read_close,
227
};