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
28
29 typedef struct {
32
34 {
35 int v;
36 char buf[64], *pbuf;
38
40
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. */
49 strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
50 return 0;
51
52 /* Check if the next line matches a SRT timestamp */
54 return 0;
55 pbuf = buf;
56 if (buf[0] == '-')
57 pbuf++;
58 if (pbuf[0] >= '0' && pbuf[0] <= '9' && strstr(buf, " --> ")
59 && sscanf(buf, "%*d:%*d:%*d%*1[,.]%*d --> %*d:%*d:%*d%*1[,.]%d", &v) == 1)
61
62 return 0;
63 }
64
70 };
71
73 {
74 int hh1, mm1, ss1, ms1;
75 int hh2, mm2, ss2, ms2;
76
80 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 const int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
86 const int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
89 return 0;
90 }
91 return -1;
92 }
93
96 {
97 if (append_cache && line_cache[0])
99 line_cache[0] = 0;
100
101 while (buf->len > 0 && buf->str[buf->len - 1] == '\n')
102 buf->str[--buf->len] = 0;
103
104 if (buf->len) {
114 if (p) {
119 }
120 }
121 }
122
123 return 0;
124 }
125
127 {
129 AVBPrint buf;
131 int res = 0;
132 char line[4096], line_cache[4096];
133 int has_event_info = 0;
137
138 if (!st)
143
145
146 line_cache[0] = 0;
147
152
154 break;
155
157 continue;
158
160 char *pline;
161
162 if (!has_event_info)
163 continue;
164
165 if (line_cache[0]) {
166 /* We got some cache and a new line so we assume the cached
167 * line was actually part of the payload */
169 line_cache[0] = 0;
170 }
171
172 /* If the line doesn't start with a number, we assume it's part of
173 * the payload, otherwise is likely an event number preceding the
174 * timing information... but we can't be sure of this yet, so we
175 * cache it */
176 if (strtol(
line, &pline, 10) < 0 ||
line == pline)
178 else
179 strcpy(line_cache,
line);
180 } else {
181 if (has_event_info) {
182 /* We have the information of previous event, append it to the
183 * queue. We insert the cached line if and only if the payload
184 * is empty and the cached line is not a standalone number. */
186 const int standalone_number = strtol(line_cache, &pline, 10) >= 0 && pline && !*pline;
187 res =
add_event(&srt->
q, &buf, line_cache, &ei, !buf.len && !standalone_number);
188 if (res < 0)
189 goto end;
190 } else {
191 has_event_info = 1;
192 }
194 ei = tmp_ei;
195 }
196 }
197
198 /* Append the last event. Here we force the cache to be flushed, because a
199 * trailing number is more likely to be geniune (for example a copyright
200 * date) and not the event index of an inexistant event */
201 if (has_event_info) {
202 res =
add_event(&srt->
q, &buf, line_cache, &ei, 1);
203 if (res < 0)
204 goto end;
205 }
206
208
209 end:
211 return res;
212 }
213
224 };