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 "libavutil/bprint.h"
25 #include "libavutil/intreadwrite.h"
27 static int srt_probe(AVProbeData *p)
29 unsigned char *ptr = p->buf;
32 if (AV_RB24(ptr) == 0xEFBBBF)
33 ptr += 3; /* skip UTF-8 BOM */
36 if ((num == i || num + 1 == i)
37 && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
38 return AVPROBE_SCORE_MAX;
40 ptr += strcspn(ptr, "\n") + 1;
45 static int srt_read_header(AVFormatContext *s)
47 AVStream *st = avformat_new_stream(s, NULL);
49 return AVERROR(ENOMEM);
50 avpriv_set_pts_info(st, 64, 1, 1000);
51 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
52 st->codec->codec_id = AV_CODEC_ID_SUBRIP;
56 static int64_t get_pts(const char **buf, int *duration,
57 int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
62 int hh1, mm1, ss1, ms1;
63 int hh2, mm2, ss2, ms2;
64 if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
65 "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
66 &hh1, &mm1, &ss1, &ms1,
67 &hh2, &mm2, &ss2, &ms2,
68 x1, x2, y1, y2) >= 8) {
69 int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
70 int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
71 *duration = end - start;
72 *buf += strcspn(*buf, "\n") + 1;
75 *buf += strcspn(*buf, "\n") + 1;
77 return AV_NOPTS_VALUE;
80 static inline int is_eol(char c)
82 return c == '\r' || c == '\n';
85 static void read_chunk(AVIOContext *pb, AVBPrint *buf)
88 int n = 0, i = 0, nb_eol = 0;
96 /* ignore all initial line breaks */
97 if (n == 0 && is_eol(c))
100 /* line break buffering: we don't want to add the trailing \r\n */
106 if (i == sizeof(eol_buf) - 1)
111 /* only one line break followed by data: we flush the line breaks
115 av_bprintf(buf, "%s", eol_buf);
119 av_bprint_chars(buf, c, 1);
123 /* FIXME: remove the following when the lavc SubRip decoder is fixed
124 * (trailing tags are not correctly flushed, see what happens to FATE when
125 * you disable this code) */
127 av_bprintf(buf, "\n");
130 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
133 int64_t pos = avio_tell(s->pb);
134 int res = AVERROR_EOF;
136 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
137 read_chunk(s->pb, &buf);
141 int duration, pkt_size;
142 const char *ptr = buf.str;
143 int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
145 pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
146 pkt_size = buf.len - (ptr - buf.str);
147 if (pts != AV_NOPTS_VALUE && !(res = av_new_packet(pkt, pkt_size))) {
148 memcpy(pkt->data, ptr, pkt->size);
149 pkt->flags |= AV_PKT_FLAG_KEY;
151 pkt->pts = pkt->dts = pts;
152 pkt->duration = duration;
154 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, 16);
164 av_bprint_finalize(&buf, NULL);
168 AVInputFormat ff_srt_demuxer = {
170 .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
171 .read_probe = srt_probe,
172 .read_header = srt_read_header,
173 .read_packet = srt_read_packet,
174 .flags = AVFMT_GENERIC_INDEX,