2 * Copyright (c) 2012-2013 Clément Bœsch <u pkh me>
4 * This file is part of FFmpeg.
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.
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.
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
22 #include "subtitles.h"
23 #include "avio_internal.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
27 void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
31 r->buf_pos = r->buf_len = 0;
33 for (i = 0; i < 2; i++)
34 r->buf[r->buf_len++] = avio_r8(r->pb);
35 if (strncmp("\xFF\xFE", r->buf, 2) == 0) {
38 } else if (strncmp("\xFE\xFF", r->buf, 2) == 0) {
42 r->buf[r->buf_len++] = avio_r8(r->pb);
43 if (strncmp("\xEF\xBB\xBF", r->buf, 3) == 0) {
48 if (s && (r->type == FF_UTF16LE || r->type == FF_UTF16BE))
49 av_log(s, AV_LOG_INFO,
50 "UTF16 is automatically converted to UTF8, do not specify a character encoding\n");
53 void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
55 memset(&r->buf_pb, 0, sizeof(r->buf_pb));
56 ffio_init_context(&r->buf_pb, buf, size, 0, NULL, NULL, NULL, NULL);
57 ff_text_init_avio(NULL, r, &r->buf_pb);
60 int64_t ff_text_pos(FFTextReader *r)
62 return avio_tell(r->pb) - r->buf_len + r->buf_pos;
65 int ff_text_r8(FFTextReader *r)
69 if (r->buf_pos < r->buf_len)
70 return r->buf[r->buf_pos++];
71 if (r->type == FF_UTF16LE) {
72 GET_UTF16(val, avio_rl16(r->pb), return 0;)
73 } else if (r->type == FF_UTF16BE) {
74 GET_UTF16(val, avio_rb16(r->pb), return 0;)
76 return avio_r8(r->pb);
82 PUT_UTF8(val, tmp, r->buf[r->buf_len++] = tmp;)
83 return r->buf[r->buf_pos++]; // buf_len is at least 1
86 void ff_text_read(FFTextReader *r, char *buf, size_t size)
88 for ( ; size > 0; size--)
89 *buf++ = ff_text_r8(r);
92 int ff_text_eof(FFTextReader *r)
94 return r->buf_pos >= r->buf_len && avio_feof(r->pb);
97 int ff_text_peek_r8(FFTextReader *r)
100 if (r->buf_pos < r->buf_len)
101 return r->buf[r->buf_pos];
103 if (!avio_feof(r->pb)) {
111 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
112 const uint8_t *event, size_t len, int merge)
114 AVPacket *subs, *sub;
116 if (merge && q->nb_subs > 0) {
117 /* merge with previous event */
120 sub = &q->subs[q->nb_subs - 1];
122 if (av_grow_packet(sub, len) < 0)
124 memcpy(sub->data + old_len, event, len);
128 if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
130 subs = av_fast_realloc(q->subs, &q->allocated_size,
131 (q->nb_subs + 1) * sizeof(*q->subs));
135 sub = &subs[q->nb_subs];
136 if (av_new_packet(sub, len) < 0)
139 sub->flags |= AV_PKT_FLAG_KEY;
140 sub->pts = sub->dts = 0;
141 memcpy(sub->data, event, len);
146 static int cmp_pkt_sub_ts_pos(const void *a, const void *b)
148 const AVPacket *s1 = a;
149 const AVPacket *s2 = b;
150 if (s1->pts == s2->pts)
151 return FFDIFFSIGN(s1->pos, s2->pos);
152 return FFDIFFSIGN(s1->pts , s2->pts);
155 static int cmp_pkt_sub_pos_ts(const void *a, const void *b)
157 const AVPacket *s1 = a;
158 const AVPacket *s2 = b;
159 if (s1->pos == s2->pos) {
160 if (s1->pts == s2->pts)
162 return s1->pts > s2->pts ? 1 : -1;
164 return s1->pos > s2->pos ? 1 : -1;
167 static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q)
171 for (i = 1; i < q->nb_subs; i++) {
172 const int last_id = i - 1 - drop;
173 const AVPacket *last = &q->subs[last_id];
175 if (q->subs[i].pts == last->pts &&
176 q->subs[i].duration == last->duration &&
177 q->subs[i].stream_index == last->stream_index &&
178 !strcmp(q->subs[i].data, last->data)) {
180 av_packet_unref(&q->subs[i]);
183 q->subs[last_id + 1] = q->subs[i];
184 memset(&q->subs[i], 0, sizeof(q->subs[i])); // for safety
190 av_log(log_ctx, AV_LOG_WARNING, "Dropping %d duplicated subtitle events\n", drop);
194 void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
201 qsort(q->subs, q->nb_subs, sizeof(*q->subs),
202 q->sort == SUB_SORT_TS_POS ? cmp_pkt_sub_ts_pos
203 : cmp_pkt_sub_pos_ts);
204 for (i = 0; i < q->nb_subs; i++)
205 if (q->subs[i].duration < 0 && i < q->nb_subs - 1)
206 q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
208 if (!q->keep_duplicates)
209 drop_dups(log_ctx, q);
212 int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
214 AVPacket *sub = q->subs + q->current_sub_idx;
217 if (q->current_sub_idx == q->nb_subs)
219 if ((ret = av_packet_ref(pkt, sub)) < 0) {
224 q->current_sub_idx++;
228 static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts)
230 int s1 = 0, s2 = q->nb_subs - 1;
233 return AVERROR(ERANGE);
241 return q->subs[s1].pts <= q->subs[s2].pts ? s1 : s2;
243 if (q->subs[mid].pts <= ts)
250 int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
251 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
253 if (flags & AVSEEK_FLAG_BYTE) {
254 return AVERROR(ENOSYS);
255 } else if (flags & AVSEEK_FLAG_FRAME) {
256 if (ts < 0 || ts >= q->nb_subs)
257 return AVERROR(ERANGE);
258 q->current_sub_idx = ts;
260 int i, idx = search_sub_ts(q, ts);
265 for (i = idx; i < q->nb_subs && q->subs[i].pts < min_ts; i++)
266 if (stream_index == -1 || q->subs[i].stream_index == stream_index)
268 for (i = idx; i > 0 && q->subs[i].pts > max_ts; i--)
269 if (stream_index == -1 || q->subs[i].stream_index == stream_index)
272 ts_selected = q->subs[idx].pts;
273 if (ts_selected < min_ts || ts_selected > max_ts)
274 return AVERROR(ERANGE);
276 /* look back in the latest subtitles for overlapping subtitles */
277 for (i = idx - 1; i >= 0; i--) {
278 int64_t pts = q->subs[i].pts;
279 if (q->subs[i].duration <= 0 ||
280 (stream_index != -1 && q->subs[i].stream_index != stream_index))
282 if (pts >= min_ts && pts > ts_selected - q->subs[i].duration)
288 /* If the queue is used to store multiple subtitles streams (like with
289 * VobSub) and the stream index is not specified, we need to make sure
290 * to focus on the smallest file position offset for a same timestamp;
291 * queue is ordered by pts and then filepos, so we can take the first
292 * entry for a given timestamp. */
293 if (stream_index == -1)
294 while (idx > 0 && q->subs[idx - 1].pts == q->subs[idx].pts)
297 q->current_sub_idx = idx;
302 void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
306 for (i = 0; i < q->nb_subs; i++)
307 av_packet_unref(&q->subs[i]);
309 q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
312 int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c)
317 if (!*c) // cached char?
322 end_chr = *c == '<' ? '>' : '<';
324 av_bprint_chars(buf, *c, 1);
327 } while (*c != end_chr && *c);
328 if (end_chr == '>') {
329 av_bprint_chars(buf, '>', 1);
335 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
338 const size_t len = strlen(attr);
342 if (!in_quotes && av_isspace(*s))
344 in_quotes ^= *s == '"'; // XXX: support escaping?
347 while (av_isspace(*s))
349 if (!av_strncasecmp(s, attr, len) && s[len] == '=')
350 return s + len + 1 + (s[len + 1] == '"');
355 static inline int is_eol(char c)
357 return c == '\r' || c == '\n';
360 void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf)
362 char eol_buf[5], last_was_cr = 0;
363 int n = 0, i = 0, nb_eol = 0;
365 av_bprint_clear(buf);
368 char c = ff_text_r8(tr);
373 /* ignore all initial line breaks */
374 if (n == 0 && is_eol(c))
377 /* line break buffering: we don't want to add the trailing \r\n */
379 nb_eol += c == '\n' || last_was_cr;
383 if (i == sizeof(eol_buf) - 1)
385 last_was_cr = c == '\r';
389 /* only one line break followed by data: we flush the line breaks
393 av_bprintf(buf, "%s", eol_buf);
397 av_bprint_chars(buf, c, 1);
402 void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
405 tr.buf_pos = tr.buf_len = 0;
408 ff_subtitles_read_text_chunk(&tr, buf);
411 ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
416 while (cur + 1 < size) {
417 unsigned char c = ff_text_r8(tr);
419 return ff_text_eof(tr) ? cur : AVERROR_INVALIDDATA;
420 if (c == '\r' || c == '\n')
425 while (ff_text_peek_r8(tr) == '\r')
427 if (ff_text_peek_r8(tr) == '\n')