3 * Copyright (c) 2008 Michael Niedermayer
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/mathematics.h"
26 #define MAX_LINESIZE 2000
28 typedef struct ASSContext{
29 uint8_t *event_buffer;
31 unsigned int event_count;
32 unsigned int event_index;
35 static int probe(AVProbeData *p)
37 const char *header= "[Script Info]";
39 if( !memcmp(p->buf , header, strlen(header))
40 || !memcmp(p->buf+3, header, strlen(header)))
41 return AVPROBE_SCORE_MAX;
46 static int read_close(AVFormatContext *s)
48 ASSContext *ass = s->priv_data;
50 av_freep(&ass->event_buffer);
51 av_freep(&ass->event);
56 static int64_t get_pts(const uint8_t *p)
58 int hour, min, sec, hsec;
60 if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
61 return AV_NOPTS_VALUE;
63 // av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
71 static int event_cmp(uint8_t **a, uint8_t **b)
73 return get_pts(*a) - get_pts(*b);
76 static int read_header(AVFormatContext *s)
78 int i, len, header_remaining;
79 ASSContext *ass = s->priv_data;
80 AVIOContext *pb = s->pb;
83 uint8_t *p, **dst[2]={0};
86 st = avformat_new_stream(s, NULL);
89 avpriv_set_pts_info(st, 64, 1, 100);
90 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
91 st->codec->codec_id= CODEC_ID_SSA;
93 header_remaining= INT_MAX;
94 dst[0] = &st->codec->extradata;
95 dst[1] = &ass->event_buffer;
97 uint8_t line[MAX_LINESIZE];
99 len = ff_get_line(pb, line, sizeof(line));
101 if(!memcmp(line, "[Events]", 8))
103 else if(line[0]=='[')
104 header_remaining= INT_MAX;
106 i= header_remaining==0;
108 if(i && get_pts(line) == AV_NOPTS_VALUE)
111 p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
115 memcpy(p + pos[i], line, len+1);
117 if(i) ass->event_count++;
118 else header_remaining--;
120 st->codec->extradata_size= pos[0];
122 if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
125 ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
126 p= ass->event_buffer;
127 for(i=0; i<ass->event_count; i++){
129 while(*p && *p != '\n')
134 qsort(ass->event, ass->event_count, sizeof(*ass->event), (void*)event_cmp);
144 static int read_packet(AVFormatContext *s, AVPacket *pkt)
146 ASSContext *ass = s->priv_data;
149 if(ass->event_index >= ass->event_count)
152 p= ass->event[ ass->event_index ];
154 end= strchr(p, '\n');
155 av_new_packet(pkt, end ? end-p+1 : strlen(p));
156 pkt->flags |= AV_PKT_FLAG_KEY;
157 pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
158 pkt->pts= pkt->dts= get_pts(p);
159 memcpy(pkt->data, p, pkt->size);
166 static int read_seek2(AVFormatContext *s, int stream_index,
167 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
169 ASSContext *ass = s->priv_data;
171 if (flags & AVSEEK_FLAG_BYTE) {
172 return AVERROR(ENOSYS);
173 } else if (flags & AVSEEK_FLAG_FRAME) {
174 if (ts < 0 || ts >= ass->event_count)
175 return AVERROR(ERANGE);
176 ass->event_index = ts;
179 int64_t min_ts_diff = INT64_MAX;
180 if (stream_index == -1) {
181 AVRational time_base = s->streams[0]->time_base;
182 ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
183 min_ts = av_rescale_rnd(min_ts, time_base.den,
184 time_base.num * (int64_t)AV_TIME_BASE,
186 max_ts = av_rescale_rnd(max_ts, time_base.den,
187 time_base.num * (int64_t)AV_TIME_BASE,
190 /* TODO: ass->event[] is sorted by pts so we could do a binary search */
191 for (i=0; i<ass->event_count; i++) {
192 int64_t pts = get_pts(ass->event[i]);
193 int64_t ts_diff = FFABS(pts - ts);
194 if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
195 min_ts_diff = ts_diff;
200 return AVERROR(ERANGE);
201 ass->event_index = idx;
206 AVInputFormat ff_ass_demuxer = {
208 .long_name = NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle format"),
209 .priv_data_size = sizeof(ASSContext),
211 .read_header = read_header,
212 .read_packet = read_packet,
213 .read_close = read_close,
214 .read_seek2 = read_seek2,