2 * SubRip subtitle demuxer
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
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/intreadwrite.h"
26 static int srt_probe(AVProbeData *p)
28 unsigned char *ptr = p->buf;
31 if (AV_RB24(ptr) == 0xEFBBBF)
32 ptr += 3; /* skip UTF-8 BOM */
35 if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
36 return AVPROBE_SCORE_MAX;
38 ptr += strcspn(ptr, "\n") + 1;
43 static int srt_read_header(AVFormatContext *s)
45 AVStream *st = avformat_new_stream(s, NULL);
48 avpriv_set_pts_info(st, 64, 1, 1000);
49 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
50 st->codec->codec_id = CODEC_ID_SRT;
54 static int64_t get_pts(const char *buf)
56 int i, v, hour, min, sec, hsec;
59 if (sscanf(buf, "%d:%2d:%2d%*1[,.]%3d --> %*d:%*2d:%*2d%*1[,.]%3d",
60 &hour, &min, &sec, &hsec, &v) == 5) {
65 buf += strcspn(buf, "\n") + 1;
67 return AV_NOPTS_VALUE;
70 static inline int is_eol(char c)
72 return c == '\r' || c == '\n';
75 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
77 char buffer[2048], *ptr = buffer, *ptr2;
78 int64_t pos = avio_tell(s->pb);
79 int res = AVERROR_EOF;
83 ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
84 } while (!is_eol(*ptr2) && !s->pb->eof_reached && ptr-buffer<sizeof(buffer)-1);
86 if (buffer[0] && !(res = av_new_packet(pkt, ptr-buffer))) {
87 memcpy(pkt->data, buffer, pkt->size);
88 pkt->flags |= AV_PKT_FLAG_KEY;
90 pkt->pts = pkt->dts = get_pts(pkt->data);
95 AVInputFormat ff_srt_demuxer = {
97 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
98 .read_probe = srt_probe,
99 .read_header = srt_read_header,
100 .read_packet = srt_read_packet,
101 .flags = AVFMT_GENERIC_INDEX,