2 * Copyright (c) 2012 Clément Bœsch
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 "libavutil/avstring.h"
25 AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
26 const uint8_t *event, int len, int merge)
30 if (merge && q->nb_subs > 0) {
31 /* merge with previous event */
34 sub = &q->subs[q->nb_subs - 1];
36 if (av_grow_packet(sub, len) < 0)
38 memcpy(sub->data + old_len, event, len);
42 if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
44 subs = av_fast_realloc(q->subs, &q->allocated_size,
45 (q->nb_subs + 1) * sizeof(*q->subs));
49 sub = &subs[q->nb_subs++];
50 if (av_new_packet(sub, len) < 0)
53 sub->flags |= AV_PKT_FLAG_KEY;
54 sub->pts = sub->dts = 0;
55 memcpy(sub->data, event, len);
60 static int cmp_pkt_sub(const void *a, const void *b)
62 const AVPacket *s1 = a;
63 const AVPacket *s2 = b;
64 if (s1->pts == s2->pts) {
65 if (s1->pos == s2->pos)
67 return s1->pos > s2->pos ? 1 : -1;
69 return s1->pts > s2->pts ? 1 : -1;
72 void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
76 qsort(q->subs, q->nb_subs, sizeof(*q->subs), cmp_pkt_sub);
77 for (i = 0; i < q->nb_subs; i++)
78 if (q->subs[i].duration == -1 && i < q->nb_subs - 1)
79 q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
82 int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
84 AVPacket *sub = q->subs + q->current_sub_idx;
86 if (q->current_sub_idx == q->nb_subs)
94 int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
95 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
97 if (flags & AVSEEK_FLAG_BYTE) {
98 return AVERROR(ENOSYS);
99 } else if (flags & AVSEEK_FLAG_FRAME) {
100 if (ts < 0 || ts >= q->nb_subs)
101 return AVERROR(ERANGE);
102 q->current_sub_idx = ts;
105 int64_t min_ts_diff = INT64_MAX;
107 if (stream_index == -1) {
108 AVRational time_base = s->streams[0]->time_base;
109 ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
110 min_ts = av_rescale_rnd(min_ts, time_base.den,
111 time_base.num * (int64_t)AV_TIME_BASE,
113 max_ts = av_rescale_rnd(max_ts, time_base.den,
114 time_base.num * (int64_t)AV_TIME_BASE,
117 /* TODO: q->subs[] is sorted by pts so we could do a binary search */
118 for (i = 0; i < q->nb_subs; i++) {
119 int64_t pts = q->subs[i].pts;
120 uint64_t ts_diff = FFABS(pts - ts);
121 if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
122 min_ts_diff = ts_diff;
127 return AVERROR(ERANGE);
128 /* look back in the latest subtitles for overlapping subtitles */
129 ts_selected = q->subs[idx].pts;
130 for (i = idx - 1; i >= 0; i--) {
131 if (q->subs[i].duration <= 0)
133 if (q->subs[i].pts > ts_selected - q->subs[i].duration)
138 q->current_sub_idx = idx;
143 void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
147 for (i = 0; i < q->nb_subs; i++)
148 av_destruct_packet(&q->subs[i]);
150 q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
153 int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
158 if (!*c) // cached char?
163 end_chr = *c == '<' ? '>' : '<';
165 av_bprint_chars(buf, *c, 1);
168 } while (*c != end_chr && *c);
169 if (end_chr == '>') {
170 av_bprint_chars(buf, '>', 1);
176 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
179 const int len = strlen(attr);
183 if (!in_quotes && isspace(*s))
185 in_quotes ^= *s == '"'; // XXX: support escaping?
190 if (!av_strncasecmp(s, attr, len) && s[len] == '=')
191 return s + len + 1 + (s[len + 1] == '"');