2 * SubRip subtitle demuxer
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
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
24 #include "subtitles.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/intreadwrite.h"
29 FFDemuxSubtitlesQueue q;
32 static int srt_probe(AVProbeData *p)
38 ff_text_init_buf(&tr, p->buf, p->buf_size);
40 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43 /* Check if the first non-empty line is a number. We do not check what the
44 * number is because in practice it can be anything.
45 * Also, that number can be followed by random garbage, so we can not
46 * unfortunately check that we only have a number. */
47 if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
48 strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
51 /* Check if the next line matches a SRT timestamp */
52 if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
54 if (buf[0] >= '0' && buf[0] <= '9' && strstr(buf, " --> ")
55 && sscanf(buf, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
56 return AVPROBE_SCORE_MAX;
61 static int64_t get_pts(const char **buf, int *duration,
62 int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
67 int hh1, mm1, ss1, ms1;
68 int hh2, mm2, ss2, ms2;
69 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
70 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
71 &hh1, &mm1, &ss1, &ms1,
72 &hh2, &mm2, &ss2, &ms2,
73 x1, x2, y1, y2) >= 8) {
74 int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
75 int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
76 *duration = end - start;
77 *buf += ff_subtitles_next_line(*buf);
80 *buf += ff_subtitles_next_line(*buf);
82 return AV_NOPTS_VALUE;
85 static int srt_read_header(AVFormatContext *s)
87 SRTContext *srt = s->priv_data;
89 AVStream *st = avformat_new_stream(s, NULL);
92 ff_text_init_avio(s, &tr, s->pb);
95 return AVERROR(ENOMEM);
96 avpriv_set_pts_info(st, 64, 1, 1000);
97 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
98 st->codec->codec_id = AV_CODEC_ID_SUBRIP;
100 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
102 while (!ff_text_eof(&tr)) {
103 ff_subtitles_read_text_chunk(&tr, &buf);
106 int64_t pos = ff_text_pos(&tr);
109 const char *ptr = buf.str;
110 int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
113 pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
114 if (pts != AV_NOPTS_VALUE) {
115 int len = buf.len - (ptr - buf.str);
118 sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
120 res = AVERROR(ENOMEM);
125 sub->duration = duration;
127 uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
139 ff_subtitles_queue_finalize(s, &srt->q);
142 av_bprint_finalize(&buf, NULL);
146 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
148 SRTContext *srt = s->priv_data;
149 return ff_subtitles_queue_read_packet(&srt->q, pkt);
152 static int srt_read_seek(AVFormatContext *s, int stream_index,
153 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
155 SRTContext *srt = s->priv_data;
156 return ff_subtitles_queue_seek(&srt->q, s, stream_index,
157 min_ts, ts, max_ts, flags);
160 static int srt_read_close(AVFormatContext *s)
162 SRTContext *srt = s->priv_data;
163 ff_subtitles_queue_clean(&srt->q);
167 AVInputFormat ff_srt_demuxer = {
169 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
170 .priv_data_size = sizeof(SRTContext),
171 .read_probe = srt_probe,
172 .read_header = srt_read_header,
173 .read_packet = srt_read_packet,
174 .read_seek2 = srt_read_seek,
175 .read_close = srt_read_close,