3 * Copyright (c) 2008 Michael Niedermayer
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/mathematics.h"
29 #define MAX_LINESIZE 2000
31 typedef struct ASSContext {
32 uint8_t *event_buffer;
34 unsigned int event_count;
35 unsigned int event_index;
38 static int probe(AVProbeData *p)
40 const char *header = "[Script Info]";
42 if (!memcmp(p->buf, header, strlen(header)) ||
43 !memcmp(p->buf + 3, header, strlen(header)))
44 return AVPROBE_SCORE_MAX;
49 static int read_close(AVFormatContext *s)
51 ASSContext *ass = s->priv_data;
53 av_freep(&ass->event_buffer);
54 av_freep(&ass->event);
59 static int64_t get_pts(const uint8_t *p)
61 int hour, min, sec, hsec;
63 if (sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
64 return AV_NOPTS_VALUE;
66 av_log(NULL, AV_LOG_TRACE, "%d %d %d %d [%s]\n", hour, min, sec, hsec, p);
71 return sec * 100 + hsec;
74 static int event_cmp(const void *_a, const void *_b)
76 const uint8_t *const *a = _a, *const *b = _b;
77 return get_pts(*a) - get_pts(*b);
80 static int read_header(AVFormatContext *s)
82 int i, len, header_remaining;
83 ASSContext *ass = s->priv_data;
84 AVIOContext *pb = s->pb;
86 int allocated[2] = { 0 };
87 uint8_t *p, **dst[2] = { 0 };
90 st = avformat_new_stream(s, NULL);
93 avpriv_set_pts_info(st, 64, 1, 100);
94 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
95 st->codec->codec_id = AV_CODEC_ID_SSA;
97 header_remaining = INT_MAX;
98 dst[0] = &st->codec->extradata;
99 dst[1] = &ass->event_buffer;
100 while (!pb->eof_reached) {
101 uint8_t line[MAX_LINESIZE];
103 len = ff_get_line(pb, line, sizeof(line));
105 if (!memcmp(line, "[Events]", 8))
106 header_remaining = 2;
107 else if (line[0] == '[')
108 header_remaining = INT_MAX;
110 i = header_remaining == 0;
112 if (i && get_pts(line) == AV_NOPTS_VALUE)
115 p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i] + MAX_LINESIZE);
119 memcpy(p + pos[i], line, len + 1);
126 st->codec->extradata_size = pos[0];
128 if (ass->event_count >= UINT_MAX / sizeof(*ass->event))
131 ass->event = av_malloc(ass->event_count * sizeof(*ass->event));
132 p = ass->event_buffer;
133 for (i = 0; i < ass->event_count; i++) {
135 while (*p && *p != '\n')
140 qsort(ass->event, ass->event_count, sizeof(*ass->event), event_cmp);
150 static int read_packet(AVFormatContext *s, AVPacket *pkt)
152 ASSContext *ass = s->priv_data;
156 if (ass->event_index >= ass->event_count)
159 p = ass->event[ass->event_index];
161 end = strchr(p, '\n');
162 ret = av_new_packet(pkt, end ? end - p + 1 : strlen(p));
165 pkt->flags |= AV_PKT_FLAG_KEY;
166 pkt->pos = p - ass->event_buffer + s->streams[0]->codec->extradata_size;
167 pkt->pts = pkt->dts = get_pts(p);
168 memcpy(pkt->data, p, pkt->size);
175 static int read_seek2(AVFormatContext *s, int stream_index,
176 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
178 ASSContext *ass = s->priv_data;
180 if (flags & AVSEEK_FLAG_BYTE) {
181 return AVERROR(ENOSYS);
182 } else if (flags & AVSEEK_FLAG_FRAME) {
183 if (ts < 0 || ts >= ass->event_count)
184 return AVERROR(ERANGE);
185 ass->event_index = ts;
188 int64_t min_ts_diff = INT64_MAX;
189 if (stream_index == -1) {
190 AVRational time_base = s->streams[0]->time_base;
191 ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
192 min_ts = av_rescale_rnd(min_ts, time_base.den,
193 time_base.num * (int64_t) AV_TIME_BASE,
195 max_ts = av_rescale_rnd(max_ts, time_base.den,
196 time_base.num * (int64_t) AV_TIME_BASE,
199 /* TODO: ass->event[] is sorted by pts so we could do a binary search */
200 for (i = 0; i < ass->event_count; i++) {
201 int64_t pts = get_pts(ass->event[i]);
202 int64_t ts_diff = FFABS(pts - ts);
203 if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
204 min_ts_diff = ts_diff;
209 return AVERROR(ERANGE);
210 ass->event_index = idx;
215 AVInputFormat ff_ass_demuxer = {
217 .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
218 .priv_data_size = sizeof(ASSContext),
220 .read_header = read_header,
221 .read_packet = read_packet,
222 .read_close = read_close,
223 .read_seek2 = read_seek2,