2 * SubRip subtitle demuxer
3 * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4 * Copyright (c) 2015 Clément Bœsch <u pkh me>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "subtitles.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/intreadwrite.h"
31 FFDemuxSubtitlesQueue q
;
34 static int srt_probe(const AVProbeData
*p
)
40 ff_text_init_buf(&tr
, p
->buf
, p
->buf_size
);
42 while (ff_text_peek_r8(&tr
) == '\r' || ff_text_peek_r8(&tr
) == '\n')
45 /* Check if the first non-empty line is a number. We do not check what the
46 * number is because in practice it can be anything.
47 * Also, that number can be followed by random garbage, so we can not
48 * unfortunately check that we only have a number. */
49 if (ff_subtitles_read_line(&tr
, buf
, sizeof(buf
)) < 0 ||
50 strtol(buf
, &pbuf
, 10) < 0 || pbuf
== buf
)
53 /* Check if the next line matches a SRT timestamp */
54 if (ff_subtitles_read_line(&tr
, buf
, sizeof(buf
)) < 0)
59 if (pbuf
[0] >= '0' && pbuf
[0] <= '9' && strstr(buf
, " --> ")
60 && sscanf(buf
, "%*d:%*d:%*d%*1[,.]%*d --> %*d:%*d:%*d%*1[,.]%d", &v
) == 1)
61 return AVPROBE_SCORE_MAX
;
67 int32_t x1
, x2
, y1
, y2
;
73 static int get_event_info(const char *line
, struct event_info
*ei
)
75 int hh1
, mm1
, ss1
, ms1
;
76 int hh2
, mm2
, ss2
, ms2
;
78 ei
->x1
= ei
->x2
= ei
->y1
= ei
->y2
= ei
->duration
= -1;
79 ei
->pts
= AV_NOPTS_VALUE
;
81 if (sscanf(line
, "%d:%d:%d%*1[,.]%d --> %d:%d:%d%*1[,.]%d"
82 "%*[ ]X1:%"PRId32
" X2:%"PRId32
" Y1:%"PRId32
" Y2:%"PRId32
,
83 &hh1
, &mm1
, &ss1
, &ms1
,
84 &hh2
, &mm2
, &ss2
, &ms2
,
85 &ei
->x1
, &ei
->x2
, &ei
->y1
, &ei
->y2
) >= 8) {
86 const int64_t start
= (hh1
*3600LL + mm1
*60LL + ss1
) * 1000LL + ms1
;
87 const int64_t end
= (hh2
*3600LL + mm2
*60LL + ss2
) * 1000LL + ms2
;
88 ei
->duration
= end
- start
;
95 static int add_event(FFDemuxSubtitlesQueue
*q
, AVBPrint
*buf
, char *line_cache
,
96 const struct event_info
*ei
, int append_cache
)
98 if (append_cache
&& line_cache
[0])
99 av_bprintf(buf
, "%s\n", line_cache
);
101 if (!av_bprint_is_complete(buf
))
102 return AVERROR(ENOMEM
);
104 while (buf
->len
> 0 && buf
->str
[buf
->len
- 1] == '\n')
105 buf
->str
[--buf
->len
] = 0;
108 AVPacket
*sub
= ff_subtitles_queue_insert_bprint(q
, buf
, 0);
110 return AVERROR(ENOMEM
);
111 av_bprint_clear(buf
);
114 sub
->duration
= ei
->duration
;
116 uint8_t *p
= av_packet_new_side_data(sub
, AV_PKT_DATA_SUBTITLE_POSITION
, 16);
119 AV_WL32(p
+ 4, ei
->y1
);
120 AV_WL32(p
+ 8, ei
->x2
);
121 AV_WL32(p
+ 12, ei
->y2
);
129 static int srt_read_header(AVFormatContext
*s
)
131 SRTContext
*srt
= s
->priv_data
;
133 AVStream
*st
= avformat_new_stream(s
, NULL
);
135 char line
[4096], line_cache
[4096];
136 int has_event_info
= 0;
137 struct event_info ei
;
139 ff_text_init_avio(s
, &tr
, s
->pb
);
142 return AVERROR(ENOMEM
);
143 avpriv_set_pts_info(st
, 64, 1, 1000);
144 st
->codecpar
->codec_type
= AVMEDIA_TYPE_SUBTITLE
;
145 st
->codecpar
->codec_id
= AV_CODEC_ID_SUBRIP
;
147 av_bprint_init(&buf
, 0, AV_BPRINT_SIZE_UNLIMITED
);
151 while (!ff_text_eof(&tr
)) {
152 struct event_info tmp_ei
;
153 const int64_t pos
= ff_text_pos(&tr
);
154 ptrdiff_t len
= ff_subtitles_read_line(&tr
, line
, sizeof(line
));
159 if (!len
|| !line
[0])
162 if (get_event_info(line
, &tmp_ei
) < 0) {
169 /* We got some cache and a new line so we assume the cached
170 * line was actually part of the payload */
171 av_bprintf(&buf
, "%s\n", line_cache
);
175 /* If the line doesn't start with a number, we assume it's part of
176 * the payload, otherwise is likely an event number preceding the
177 * timing information... but we can't be sure of this yet, so we
179 if (strtol(line
, &pline
, 10) < 0 || line
== pline
)
180 av_bprintf(&buf
, "%s\n", line
);
182 strcpy(line_cache
, line
);
184 if (has_event_info
) {
185 /* We have the information of previous event, append it to the
186 * queue. We insert the cached line if and only if the payload
187 * is empty and the cached line is not a standalone number. */
189 const int standalone_number
= strtol(line_cache
, &pline
, 10) >= 0 && pline
&& !*pline
;
190 res
= add_event(&srt
->q
, &buf
, line_cache
, &ei
, !buf
.len
&& !standalone_number
);
201 /* Append the last event. Here we force the cache to be flushed, because a
202 * trailing number is more likely to be genuine (for example a copyright
203 * date) and not the event index of an inexistent event */
204 if (has_event_info
) {
205 res
= add_event(&srt
->q
, &buf
, line_cache
, &ei
, 1);
210 ff_subtitles_queue_finalize(s
, &srt
->q
);
213 av_bprint_finalize(&buf
, NULL
);
217 const FFInputFormat ff_srt_demuxer
= {
219 .p
.long_name
= NULL_IF_CONFIG_SMALL("SubRip subtitle"),
220 .priv_data_size
= sizeof(SRTContext
),
221 .flags_internal
= FF_INFMT_FLAG_INIT_CLEANUP
,
222 .read_probe
= srt_probe
,
223 .read_header
= srt_read_header
,
224 .read_packet
= ff_subtitles_read_packet
,
225 .read_seek2
= ff_subtitles_read_seek
,
226 .read_close
= ff_subtitles_read_close
,