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)
34 const unsigned char *ptr = p->buf;
37 if (AV_RB24(ptr) == 0xEFBBBF)
38 ptr += 3; /* skip UTF-8 BOM */
41 if ((num == i || num + 1 == i)
42 && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
43 return AVPROBE_SCORE_MAX;
45 ptr += strcspn(ptr, "\n") + 1;
50 static int64_t get_pts(const char **buf, int *duration,
51 int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
56 int hh1, mm1, ss1, ms1;
57 int hh2, mm2, ss2, ms2;
58 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
59 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
60 &hh1, &mm1, &ss1, &ms1,
61 &hh2, &mm2, &ss2, &ms2,
62 x1, x2, y1, y2) >= 8) {
63 int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
64 int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
65 *duration = end - start;
66 *buf += strcspn(*buf, "\n") + 1;
69 *buf += strcspn(*buf, "\n") + 1;
71 return AV_NOPTS_VALUE;
74 static int srt_read_header(AVFormatContext *s)
76 SRTContext *srt = s->priv_data;
78 AVStream *st = avformat_new_stream(s, NULL);
82 return AVERROR(ENOMEM);
83 avpriv_set_pts_info(st, 64, 1, 1000);
84 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
85 st->codec->codec_id = AV_CODEC_ID_SUBRIP;
87 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
89 while (!url_feof(s->pb)) {
90 ff_subtitles_read_chunk(s->pb, &buf);
93 int64_t pos = avio_tell(s->pb);
96 const char *ptr = buf.str;
97 int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
100 pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
101 if (pts != AV_NOPTS_VALUE) {
102 int len = buf.len - (ptr - buf.str);
105 sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
107 res = AVERROR(ENOMEM);
112 sub->duration = duration;
114 uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
126 ff_subtitles_queue_finalize(&srt->q);
129 av_bprint_finalize(&buf, NULL);
133 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
135 SRTContext *srt = s->priv_data;
136 return ff_subtitles_queue_read_packet(&srt->q, pkt);
139 static int srt_read_seek(AVFormatContext *s, int stream_index,
140 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
142 SRTContext *srt = s->priv_data;
143 return ff_subtitles_queue_seek(&srt->q, s, stream_index,
144 min_ts, ts, max_ts, flags);
147 static int srt_read_close(AVFormatContext *s)
149 SRTContext *srt = s->priv_data;
150 ff_subtitles_queue_clean(&srt->q);
154 AVInputFormat ff_srt_demuxer = {
156 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
157 .priv_data_size = sizeof(SRTContext),
158 .read_probe = srt_probe,
159 .read_header = srt_read_header,
160 .read_packet = srt_read_packet,
161 .read_seek2 = srt_read_seek,
162 .read_close = srt_read_close,