FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/srtdec.c
Date: 2024-02-29 09:57:37
Exec Total Coverage
Lines: 89 97 91.8%
Functions: 4 4 100.0%
Branches: 63 80 78.8%

Line Branch Exec 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 "internal.h"
25 #include "subtitles.h"
26 #include "libavutil/bprint.h"
27 #include "libavutil/intreadwrite.h"
28
29 typedef struct {
30 FFDemuxSubtitlesQueue q;
31 } SRTContext;
32
33 7056 static int srt_probe(const AVProbeData *p)
34 {
35 int v;
36 char buf[64], *pbuf;
37 FFTextReader tr;
38
39 7056 ff_text_init_buf(&tr, p->buf, p->buf_size);
40
41
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7059 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 7056 times.
7063 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
42 7 ff_text_r8(&tr);
43
44 /* Check if the first non-empty line is a number. We do not check what the
45 * number is because in practice it can be anything.
46 * Also, that number can be followed by random garbage, so we can not
47 * unfortunately check that we only have a number. */
48
3/4
✓ Branch 1 taken 1457 times.
✓ Branch 2 taken 5599 times.
✓ Branch 3 taken 1457 times.
✗ Branch 4 not taken.
8513 if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
49
2/2
✓ Branch 1 taken 1447 times.
✓ Branch 2 taken 10 times.
2914 strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
50 7046 return 0;
51
52 /* Check if the next line matches a SRT timestamp */
53
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
54 return 0;
55 10 pbuf = buf;
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (buf[0] == '-')
57 pbuf++;
58
4/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 2 times.
10 if (pbuf[0] >= '0' && pbuf[0] <= '9' && strstr(buf, " --> ")
59
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 && sscanf(buf, "%*d:%*d:%*d%*1[,.]%*d --> %*d:%*d:%*d%*1[,.]%d", &v) == 1)
60 8 return AVPROBE_SCORE_MAX;
61
62 2 return 0;
63 }
64
65 struct event_info {
66 int32_t x1, x2, y1, y2;
67 int duration;
68 int64_t pts;
69 int64_t pos;
70 };
71
72 2806 static int get_event_info(const char *line, struct event_info *ei)
73 {
74 int hh1, mm1, ss1, ms1;
75 int hh2, mm2, ss2, ms2;
76
77 2806 ei->x1 = ei->x2 = ei->y1 = ei->y2 = ei->duration = -1;
78 2806 ei->pts = AV_NOPTS_VALUE;
79 2806 ei->pos = -1;
80
2/2
✓ Branch 0 taken 700 times.
✓ Branch 1 taken 2106 times.
2806 if (sscanf(line, "%d:%d:%d%*1[,.]%d --> %d:%d:%d%*1[,.]%d"
81 "%*[ ]X1:%"PRId32" X2:%"PRId32" Y1:%"PRId32" Y2:%"PRId32,
82 &hh1, &mm1, &ss1, &ms1,
83 &hh2, &mm2, &ss2, &ms2,
84 &ei->x1, &ei->x2, &ei->y1, &ei->y2) >= 8) {
85 700 const int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
86 700 const int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
87 700 ei->duration = end - start;
88 700 ei->pts = start;
89 700 return 0;
90 }
91 2106 return -1;
92 }
93
94 700 static int add_event(FFDemuxSubtitlesQueue *q, AVBPrint *buf, char *line_cache,
95 const struct event_info *ei, int append_cache)
96 {
97
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 689 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9 times.
700 if (append_cache && line_cache[0])
98 2 av_bprintf(buf, "%s\n", line_cache);
99 700 line_cache[0] = 0;
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 700 times.
700 if (!av_bprint_is_complete(buf))
101 return AVERROR(ENOMEM);
102
103
4/4
✓ Branch 0 taken 1392 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 696 times.
✓ Branch 3 taken 696 times.
1396 while (buf->len > 0 && buf->str[buf->len - 1] == '\n')
104 696 buf->str[--buf->len] = 0;
105
106
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 4 times.
700 if (buf->len) {
107 696 AVPacket *sub = ff_subtitles_queue_insert_bprint(q, buf, 0);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
696 if (!sub)
109 return AVERROR(ENOMEM);
110 696 av_bprint_clear(buf);
111 696 sub->pos = ei->pos;
112 696 sub->pts = ei->pts;
113 696 sub->duration = ei->duration;
114
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 690 times.
696 if (ei->x1 != -1) {
115 6 uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
116
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (p) {
117 6 AV_WL32(p, ei->x1);
118 6 AV_WL32(p + 4, ei->y1);
119 6 AV_WL32(p + 8, ei->x2);
120 6 AV_WL32(p + 12, ei->y2);
121 }
122 }
123 }
124
125 700 return 0;
126 }
127
128 10 static int srt_read_header(AVFormatContext *s)
129 {
130 10 SRTContext *srt = s->priv_data;
131 AVBPrint buf;
132 10 AVStream *st = avformat_new_stream(s, NULL);
133 10 int res = 0;
134 char line[4096], line_cache[4096];
135 10 int has_event_info = 0;
136 struct event_info ei;
137 FFTextReader tr;
138 10 ff_text_init_avio(s, &tr, s->pb);
139
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!st)
141 return AVERROR(ENOMEM);
142 10 avpriv_set_pts_info(st, 64, 1, 1000);
143 10 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
144 10 st->codecpar->codec_id = AV_CODEC_ID_SUBRIP;
145
146 10 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
147
148 10 line_cache[0] = 0;
149
150
2/2
✓ Branch 1 taken 3529 times.
✓ Branch 2 taken 10 times.
3539 while (!ff_text_eof(&tr)) {
151 struct event_info tmp_ei;
152 3529 const int64_t pos = ff_text_pos(&tr);
153 3529 ptrdiff_t len = ff_subtitles_read_line(&tr, line, sizeof(line));
154
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3529 times.
3529 if (len < 0)
156 break;
157
158
3/4
✓ Branch 0 taken 2806 times.
✓ Branch 1 taken 723 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2806 times.
3529 if (!len || !line[0])
159 733 continue;
160
161
2/2
✓ Branch 1 taken 2106 times.
✓ Branch 2 taken 700 times.
2806 if (get_event_info(line, &tmp_ei) < 0) {
162 char *pline;
163
164
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2096 times.
2106 if (!has_event_info)
165 10 continue;
166
167
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2079 times.
2096 if (line_cache[0]) {
168 /* We got some cache and a new line so we assume the cached
169 * line was actually part of the payload */
170 17 av_bprintf(&buf, "%s\n", line_cache);
171 17 line_cache[0] = 0;
172 }
173
174 /* If the line doesn't start with a number, we assume it's part of
175 * the payload, otherwise is likely an event number preceding the
176 * timing information... but we can't be sure of this yet, so we
177 * cache it */
178
4/4
✓ Branch 1 taken 2095 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1388 times.
✓ Branch 4 taken 707 times.
2096 if (strtol(line, &pline, 10) < 0 || line == pline)
179 1389 av_bprintf(&buf, "%s\n", line);
180 else
181 707 strcpy(line_cache, line);
182 } else {
183
2/2
✓ Branch 0 taken 690 times.
✓ Branch 1 taken 10 times.
700 if (has_event_info) {
184 /* We have the information of previous event, append it to the
185 * queue. We insert the cached line if and only if the payload
186 * is empty and the cached line is not a standalone number. */
187 690 char *pline = NULL;
188
4/6
✓ Branch 1 taken 690 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 690 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 686 times.
✓ Branch 6 taken 4 times.
690 const int standalone_number = strtol(line_cache, &pline, 10) >= 0 && pline && !*pline;
189
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 687 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
690 res = add_event(&srt->q, &buf, line_cache, &ei, !buf.len && !standalone_number);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 690 times.
690 if (res < 0)
191 goto end;
192 } else {
193 10 has_event_info = 1;
194 }
195 700 tmp_ei.pos = pos;
196 700 ei = tmp_ei;
197 }
198 }
199
200 /* Append the last event. Here we force the cache to be flushed, because a
201 * trailing number is more likely to be geniune (for example a copyright
202 * date) and not the event index of an inexistant event */
203
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (has_event_info) {
204 10 res = add_event(&srt->q, &buf, line_cache, &ei, 1);
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (res < 0)
206 goto end;
207 }
208
209 10 ff_subtitles_queue_finalize(s, &srt->q);
210
211 10 end:
212 10 av_bprint_finalize(&buf, NULL);
213 10 return res;
214 }
215
216 const AVInputFormat ff_srt_demuxer = {
217 .name = "srt",
218 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
219 .priv_data_size = sizeof(SRTContext),
220 .flags_internal = FF_FMT_INIT_CLEANUP,
221 .read_probe = srt_probe,
222 .read_header = srt_read_header,
223 .read_packet = ff_subtitles_read_packet,
224 .read_seek2 = ff_subtitles_read_seek,
225 .read_close = ff_subtitles_read_close,
226 };
227