1 /*
2 * Copyright (c) 2012-2013 Clément Bœsch <u pkh me>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "avformat.h"
22 #include "subtitles.h"
23 #include "avio_internal.h"
24 #include "libavutil/avstring.h"
25
ff_text_init_avio(void * s,FFTextReader * r,AVIOContext * pb)26 void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
27 {
28 int i;
29 r->pb = pb;
30 r->buf_pos = r->buf_len = 0;
31 r->type = FF_UTF_8;
32 for (i = 0; i < 2; i++)
33 r->buf[r->buf_len++] = avio_r8(r->pb);
34 if (strncmp("\xFF\xFE", r->buf, 2) == 0) {
35 r->type = FF_UTF16LE;
36 r->buf_pos += 2;
37 } else if (strncmp("\xFE\xFF", r->buf, 2) == 0) {
38 r->type = FF_UTF16BE;
39 r->buf_pos += 2;
40 } else {
41 r->buf[r->buf_len++] = avio_r8(r->pb);
42 if (strncmp("\xEF\xBB\xBF", r->buf, 3) == 0) {
43 // UTF8
44 r->buf_pos += 3;
45 }
46 }
47 if (s && (r->type == FF_UTF16LE || r->type == FF_UTF16BE))
48 av_log(s, AV_LOG_INFO,
49 "UTF16 is automatically converted to UTF8, do not specify a character encoding\n");
50 }
51
ff_text_init_buf(FFTextReader * r,void * buf,size_t size)52 void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
53 {
54 ffio_init_context(&r->buf_pb, buf, size, 0, NULL, NULL, NULL, NULL);
55 ff_text_init_avio(NULL, r, &r->buf_pb.pub);
56 }
57
ff_text_pos(FFTextReader * r)58 int64_t ff_text_pos(FFTextReader *r)
59 {
60 return avio_tell(r->pb) - r->buf_len + r->buf_pos;
61 }
62
ff_text_r8(FFTextReader * r)63 int ff_text_r8(FFTextReader *r)
64 {
65 uint32_t val;
66 uint8_t tmp;
67 if (r->buf_pos < r->buf_len)
68 return r->buf[r->buf_pos++];
69 if (r->type == FF_UTF16LE) {
70 GET_UTF16(val, avio_rl16(r->pb), return 0;)
71 } else if (r->type == FF_UTF16BE) {
72 GET_UTF16(val, avio_rb16(r->pb), return 0;)
73 } else {
74 return avio_r8(r->pb);
75 }
76 if (!val)
77 return 0;
78 r->buf_pos = 0;
79 r->buf_len = 0;
80 PUT_UTF8(val, tmp, r->buf[r->buf_len++] = tmp;)
81 return r->buf[r->buf_pos++]; // buf_len is at least 1
82 }
83
ff_text_read(FFTextReader * r,char * buf,size_t size)84 void ff_text_read(FFTextReader *r, char *buf, size_t size)
85 {
86 for ( ; size > 0; size--)
87 *buf++ = ff_text_r8(r);
88 }
89
ff_text_eof(FFTextReader * r)90 int ff_text_eof(FFTextReader *r)
91 {
92 return r->buf_pos >= r->buf_len && avio_feof(r->pb);
93 }
94
ff_text_peek_r8(FFTextReader * r)95 int ff_text_peek_r8(FFTextReader *r)
96 {
97 int c;
98 if (r->buf_pos < r->buf_len)
99 return r->buf[r->buf_pos];
100 c = ff_text_r8(r);
101 if (!avio_feof(r->pb)) {
102 r->buf_pos = 0;
103 r->buf_len = 1;
104 r->buf[0] = c;
105 }
106 return c;
107 }
108
ff_subtitles_queue_insert(FFDemuxSubtitlesQueue * q,const uint8_t * event,size_t len,int merge)109 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
110 const uint8_t *event, size_t len, int merge)
111 {
112 AVPacket **subs, *sub;
113
114 if (merge && q->nb_subs > 0) {
115 /* merge with previous event */
116
117 int old_len;
118 sub = q->subs[q->nb_subs - 1];
119 old_len = sub->size;
120 if (av_grow_packet(sub, len) < 0)
121 return NULL;
122 memcpy(sub->data + old_len, event, len);
123 } else {
124 /* new event */
125
126 if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
127 return NULL;
128 subs = av_fast_realloc(q->subs, &q->allocated_size,
129 (q->nb_subs + 1) * sizeof(*q->subs));
130 if (!subs)
131 return NULL;
132 q->subs = subs;
133 sub = av_packet_alloc();
134 if (!sub)
135 return NULL;
136 if (av_new_packet(sub, len) < 0) {
137 av_packet_free(&sub);
138 return NULL;
139 }
140 subs[q->nb_subs++] = sub;
141 sub->flags |= AV_PKT_FLAG_KEY;
142 sub->pts = sub->dts = 0;
143 memcpy(sub->data, event, len);
144 }
145 return sub;
146 }
147
cmp_pkt_sub_ts_pos(const void * a,const void * b)148 static int cmp_pkt_sub_ts_pos(const void *a, const void *b)
149 {
150 const AVPacket *s1 = *(const AVPacket **)a;
151 const AVPacket *s2 = *(const AVPacket **)b;
152 if (s1->pts == s2->pts)
153 return FFDIFFSIGN(s1->pos, s2->pos);
154 return FFDIFFSIGN(s1->pts , s2->pts);
155 }
156
cmp_pkt_sub_pos_ts(const void * a,const void * b)157 static int cmp_pkt_sub_pos_ts(const void *a, const void *b)
158 {
159 const AVPacket *s1 = *(const AVPacket **)a;
160 const AVPacket *s2 = *(const AVPacket **)b;
161 if (s1->pos == s2->pos) {
162 if (s1->pts == s2->pts)
163 return 0;
164 return s1->pts > s2->pts ? 1 : -1;
165 }
166 return s1->pos > s2->pos ? 1 : -1;
167 }
168
drop_dups(void * log_ctx,FFDemuxSubtitlesQueue * q)169 static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q)
170 {
171 int i, drop = 0;
172
173 for (i = 1; i < q->nb_subs; i++) {
174 const int last_id = i - 1 - drop;
175 const AVPacket *last = q->subs[last_id];
176
177 if (q->subs[i]->pts == last->pts &&
178 q->subs[i]->duration == last->duration &&
179 q->subs[i]->stream_index == last->stream_index &&
180 !strcmp(q->subs[i]->data, last->data)) {
181
182 av_packet_free(&q->subs[i]);
183 drop++;
184 } else if (drop) {
185 q->subs[last_id + 1] = q->subs[i];
186 q->subs[i] = NULL;
187 }
188 }
189
190 if (drop) {
191 q->nb_subs -= drop;
192 av_log(log_ctx, AV_LOG_WARNING, "Dropping %d duplicated subtitle events\n", drop);
193 }
194 }
195
ff_subtitles_queue_finalize(void * log_ctx,FFDemuxSubtitlesQueue * q)196 void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
197 {
198 int i;
199
200 if (!q->nb_subs)
201 return;
202
203 qsort(q->subs, q->nb_subs, sizeof(*q->subs),
204 q->sort == SUB_SORT_TS_POS ? cmp_pkt_sub_ts_pos
205 : cmp_pkt_sub_pos_ts);
206 for (i = 0; i < q->nb_subs; i++)
207 if (q->subs[i]->duration < 0 && i < q->nb_subs - 1 && q->subs[i + 1]->pts - (uint64_t)q->subs[i]->pts <= INT64_MAX)
208 q->subs[i]->duration = q->subs[i + 1]->pts - q->subs[i]->pts;
209
210 if (!q->keep_duplicates)
211 drop_dups(log_ctx, q);
212 }
213
ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue * q,AVPacket * pkt)214 int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
215 {
216 AVPacket *sub;
217 int ret;
218
219 if (q->current_sub_idx == q->nb_subs)
220 return AVERROR_EOF;
221 sub = q->subs[q->current_sub_idx];
222 if ((ret = av_packet_ref(pkt, sub)) < 0) {
223 return ret;
224 }
225
226 pkt->dts = pkt->pts;
227 q->current_sub_idx++;
228 return 0;
229 }
230
search_sub_ts(const FFDemuxSubtitlesQueue * q,int64_t ts)231 static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts)
232 {
233 int s1 = 0, s2 = q->nb_subs - 1;
234
235 if (s2 < s1)
236 return AVERROR(ERANGE);
237
238 for (;;) {
239 int mid;
240
241 if (s1 == s2)
242 return s1;
243 if (s1 == s2 - 1)
244 return q->subs[s1]->pts <= q->subs[s2]->pts ? s1 : s2;
245 mid = (s1 + s2) / 2;
246 if (q->subs[mid]->pts <= ts)
247 s1 = mid;
248 else
249 s2 = mid;
250 }
251 }
252
ff_subtitles_queue_seek(FFDemuxSubtitlesQueue * q,AVFormatContext * s,int stream_index,int64_t min_ts,int64_t ts,int64_t max_ts,int flags)253 int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
254 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
255 {
256 if (flags & AVSEEK_FLAG_BYTE) {
257 return AVERROR(ENOSYS);
258 } else if (flags & AVSEEK_FLAG_FRAME) {
259 if (ts < 0 || ts >= q->nb_subs)
260 return AVERROR(ERANGE);
261 q->current_sub_idx = ts;
262 } else {
263 int i, idx = search_sub_ts(q, ts);
264 int64_t ts_selected;
265
266 if (idx < 0)
267 return idx;
268 for (i = idx; i < q->nb_subs && q->subs[i]->pts < min_ts; i++)
269 if (stream_index == -1 || q->subs[i]->stream_index == stream_index)
270 idx = i;
271 for (i = idx; i > 0 && q->subs[i]->pts > max_ts; i--)
272 if (stream_index == -1 || q->subs[i]->stream_index == stream_index)
273 idx = i;
274
275 ts_selected = q->subs[idx]->pts;
276 if (ts_selected < min_ts || ts_selected > max_ts)
277 return AVERROR(ERANGE);
278
279 /* look back in the latest subtitles for overlapping subtitles */
280 for (i = idx - 1; i >= 0; i--) {
281 int64_t pts = q->subs[i]->pts;
282 if (q->subs[i]->duration <= 0 ||
283 (stream_index != -1 && q->subs[i]->stream_index != stream_index))
284 continue;
285 if (pts >= min_ts && pts > ts_selected - q->subs[i]->duration)
286 idx = i;
287 else
288 break;
289 }
290
291 /* If the queue is used to store multiple subtitles streams (like with
292 * VobSub) and the stream index is not specified, we need to make sure
293 * to focus on the smallest file position offset for a same timestamp;
294 * queue is ordered by pts and then filepos, so we can take the first
295 * entry for a given timestamp. */
296 if (stream_index == -1)
297 while (idx > 0 && q->subs[idx - 1]->pts == q->subs[idx]->pts)
298 idx--;
299
300 q->current_sub_idx = idx;
301 }
302 return 0;
303 }
304
ff_subtitles_queue_clean(FFDemuxSubtitlesQueue * q)305 void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
306 {
307 int i;
308
309 for (i = 0; i < q->nb_subs; i++)
310 av_packet_free(&q->subs[i]);
311 av_freep(&q->subs);
312 q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
313 }
314
ff_subtitles_read_packet(AVFormatContext * s,AVPacket * pkt)315 int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt)
316 {
317 FFDemuxSubtitlesQueue *q = s->priv_data;
318 return ff_subtitles_queue_read_packet(q, pkt);
319 }
320
ff_subtitles_read_seek(AVFormatContext * s,int stream_index,int64_t min_ts,int64_t ts,int64_t max_ts,int flags)321 int ff_subtitles_read_seek(AVFormatContext *s, int stream_index,
322 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
323 {
324 FFDemuxSubtitlesQueue *q = s->priv_data;
325 return ff_subtitles_queue_seek(q, s, stream_index,
326 min_ts, ts, max_ts, flags);
327 }
328
ff_subtitles_read_close(AVFormatContext * s)329 int ff_subtitles_read_close(AVFormatContext *s)
330 {
331 FFDemuxSubtitlesQueue *q = s->priv_data;
332 ff_subtitles_queue_clean(q);
333 return 0;
334 }
335
ff_smil_extract_next_text_chunk(FFTextReader * tr,AVBPrint * buf,char * c)336 int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c)
337 {
338 int i = 0;
339 char end_chr;
340
341 if (!*c) // cached char?
342 *c = ff_text_r8(tr);
343 if (!*c)
344 return 0;
345
346 end_chr = *c == '<' ? '>' : '<';
347 do {
348 av_bprint_chars(buf, *c, 1);
349 *c = ff_text_r8(tr);
350 i++;
351 } while (*c != end_chr && *c);
352 if (end_chr == '>') {
353 av_bprint_chars(buf, '>', 1);
354 *c = 0;
355 }
356 return i;
357 }
358
ff_smil_get_attr_ptr(const char * s,const char * attr)359 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
360 {
361 int in_quotes = 0;
362 const size_t len = strlen(attr);
363
364 while (*s) {
365 while (*s) {
366 if (!in_quotes && av_isspace(*s))
367 break;
368 in_quotes ^= *s == '"'; // XXX: support escaping?
369 s++;
370 }
371 while (av_isspace(*s))
372 s++;
373 if (!av_strncasecmp(s, attr, len) && s[len] == '=')
374 return s + len + 1 + (s[len + 1] == '"');
375 }
376 return NULL;
377 }
378
is_eol(char c)379 static inline int is_eol(char c)
380 {
381 return c == '\r' || c == '\n';
382 }
383
ff_subtitles_read_text_chunk(FFTextReader * tr,AVBPrint * buf)384 void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf)
385 {
386 char eol_buf[5], last_was_cr = 0;
387 int n = 0, i = 0, nb_eol = 0;
388
389 av_bprint_clear(buf);
390
391 for (;;) {
392 char c = ff_text_r8(tr);
393
394 if (!c)
395 break;
396
397 /* ignore all initial line breaks */
398 if (n == 0 && is_eol(c))
399 continue;
400
401 /* line break buffering: we don't want to add the trailing \r\n */
402 if (is_eol(c)) {
403 nb_eol += c == '\n' || last_was_cr;
404 if (nb_eol == 2)
405 break;
406 eol_buf[i++] = c;
407 if (i == sizeof(eol_buf) - 1)
408 break;
409 last_was_cr = c == '\r';
410 continue;
411 }
412
413 /* only one line break followed by data: we flush the line breaks
414 * buffer */
415 if (i) {
416 eol_buf[i] = 0;
417 av_bprintf(buf, "%s", eol_buf);
418 i = nb_eol = 0;
419 }
420
421 av_bprint_chars(buf, c, 1);
422 n++;
423 }
424 }
425
ff_subtitles_read_chunk(AVIOContext * pb,AVBPrint * buf)426 void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
427 {
428 FFTextReader tr;
429 tr.buf_pos = tr.buf_len = 0;
430 tr.type = 0;
431 tr.pb = pb;
432 ff_subtitles_read_text_chunk(&tr, buf);
433 }
434
ff_subtitles_read_line(FFTextReader * tr,char * buf,size_t size)435 ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
436 {
437 size_t cur = 0;
438 if (!size)
439 return 0;
440 buf[0] = '\0';
441 while (cur + 1 < size) {
442 unsigned char c = ff_text_r8(tr);
443 if (!c)
444 return ff_text_eof(tr) ? cur : AVERROR_INVALIDDATA;
445 if (c == '\r' || c == '\n')
446 break;
447 buf[cur++] = c;
448 buf[cur] = '\0';
449 }
450 while (ff_text_peek_r8(tr) == '\r')
451 ff_text_r8(tr);
452 if (ff_text_peek_r8(tr) == '\n')
453 ff_text_r8(tr);
454 return cur;
455 }
456