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
25 #define MAX_LINESIZE 2000
27 typedef struct ASSContext{
28 uint8_t *event_buffer;
30 unsigned int event_count;
31 unsigned int event_index;
34 static int probe(AVProbeData *p)
36 const char *header= "[Script Info]";
38 if( !memcmp(p->buf , header, strlen(header))
39 || !memcmp(p->buf+3, header, strlen(header)))
40 return AVPROBE_SCORE_MAX;
45 static int read_close(AVFormatContext *s)
47 ASSContext *ass = s->priv_data;
49 av_freep(&ass->event_buffer);
50 av_freep(&ass->event);
55 static int64_t get_pts(const uint8_t *p)
57 int hour, min, sec, hsec;
59 if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
60 return AV_NOPTS_VALUE;
62 // av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
70 static int event_cmp(uint8_t **a, uint8_t **b)
72 return get_pts(*a) - get_pts(*b);
75 static int read_header(AVFormatContext *s, AVFormatParameters *ap)
77 int i, len, header_remaining;
78 ASSContext *ass = s->priv_data;
79 ByteIOContext *pb = s->pb;
82 uint8_t *p, **dst[2]={0};
85 st = av_new_stream(s, 0);
88 av_set_pts_info(st, 64, 1, 100);
89 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
90 st->codec->codec_id= CODEC_ID_SSA;
92 header_remaining= INT_MAX;
93 dst[0] = &st->codec->extradata;
94 dst[1] = &ass->event_buffer;
96 uint8_t line[MAX_LINESIZE];
98 len = ff_get_line(pb, line, sizeof(line));
100 if(!memcmp(line, "[Events]", 8))
102 else if(line[0]=='[')
103 header_remaining= INT_MAX;
105 i= header_remaining==0;
107 if(i && get_pts(line) == AV_NOPTS_VALUE)
110 p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
114 memcpy(p + pos[i], line, len+1);
116 if(i) ass->event_count++;
117 else header_remaining--;
119 st->codec->extradata_size= pos[0];
121 if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
124 ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
125 p= ass->event_buffer;
126 for(i=0; i<ass->event_count; i++){
128 while(*p && *p != '\n')
133 qsort(ass->event, ass->event_count, sizeof(*ass->event), (void*)event_cmp);
143 static int read_packet(AVFormatContext *s, AVPacket *pkt)
145 ASSContext *ass = s->priv_data;
148 if(ass->event_index >= ass->event_count)
151 p= ass->event[ ass->event_index ];
153 end= strchr(p, '\n');
154 av_new_packet(pkt, end ? end-p+1 : strlen(p));
155 pkt->flags |= AV_PKT_FLAG_KEY;
156 pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
157 pkt->pts= pkt->dts= get_pts(p);
158 memcpy(pkt->data, p, pkt->size);
165 AVInputFormat ass_demuxer = {
167 NULL_IF_CONFIG_SMALL("Advanced SubStation Alpha subtitle format"),