FFmpeg
srtdec.c
Go to the documentation of this file.
1 /*
2  * SubRip subtitle demuxer
3  * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4  * Copyright (c) 2015 Clément Bœsch <u pkh me>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 #include "avformat.h"
24 #include "demux.h"
25 #include "internal.h"
26 #include "subtitles.h"
27 #include "libavutil/bprint.h"
28 #include "libavutil/intreadwrite.h"
29 
30 typedef struct {
32 } SRTContext;
33 
34 static int srt_probe(const AVProbeData *p)
35 {
36  int v;
37  char buf[64], *pbuf;
38  FFTextReader tr;
39 
40  ff_text_init_buf(&tr, p->buf, p->buf_size);
41 
42  while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
43  ff_text_r8(&tr);
44 
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)
51  return 0;
52 
53  /* Check if the next line matches a SRT timestamp */
54  if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
55  return 0;
56  pbuf = buf;
57  if (buf[0] == '-')
58  pbuf++;
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;
62 
63  return 0;
64 }
65 
66 struct event_info {
68  int duration;
71 };
72 
73 static int get_event_info(const char *line, struct event_info *ei)
74 {
75  int hh1, mm1, ss1, ms1;
76  int hh2, mm2, ss2, ms2;
77 
78  ei->x1 = ei->x2 = ei->y1 = ei->y2 = ei->duration = -1;
79  ei->pts = AV_NOPTS_VALUE;
80  ei->pos = -1;
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;
89  ei->pts = start;
90  return 0;
91  }
92  return -1;
93 }
94 
95 static int add_event(FFDemuxSubtitlesQueue *q, AVBPrint *buf, char *line_cache,
96  const struct event_info *ei, int append_cache)
97 {
98  if (append_cache && line_cache[0])
99  av_bprintf(buf, "%s\n", line_cache);
100  line_cache[0] = 0;
101  if (!av_bprint_is_complete(buf))
102  return AVERROR(ENOMEM);
103 
104  while (buf->len > 0 && buf->str[buf->len - 1] == '\n')
105  buf->str[--buf->len] = 0;
106 
107  if (buf->len) {
108  AVPacket *sub = ff_subtitles_queue_insert_bprint(q, buf, 0);
109  if (!sub)
110  return AVERROR(ENOMEM);
111  av_bprint_clear(buf);
112  sub->pos = ei->pos;
113  sub->pts = ei->pts;
114  sub->duration = ei->duration;
115  if (ei->x1 != -1) {
117  if (p) {
118  AV_WL32(p, ei->x1);
119  AV_WL32(p + 4, ei->y1);
120  AV_WL32(p + 8, ei->x2);
121  AV_WL32(p + 12, ei->y2);
122  }
123  }
124  }
125 
126  return 0;
127 }
128 
130 {
131  SRTContext *srt = s->priv_data;
132  AVBPrint buf;
134  int res = 0;
135  char line[4096], line_cache[4096];
136  int has_event_info = 0;
137  struct event_info ei;
138  FFTextReader tr;
139  ff_text_init_avio(s, &tr, s->pb);
140 
141  if (!st)
142  return AVERROR(ENOMEM);
143  avpriv_set_pts_info(st, 64, 1, 1000);
146 
148 
149  line_cache[0] = 0;
150 
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));
155 
156  if (len < 0)
157  break;
158 
159  if (!len || !line[0])
160  continue;
161 
162  if (get_event_info(line, &tmp_ei) < 0) {
163  char *pline;
164 
165  if (!has_event_info)
166  continue;
167 
168  if (line_cache[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);
172  line_cache[0] = 0;
173  }
174 
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
178  * cache it */
179  if (strtol(line, &pline, 10) < 0 || line == pline)
180  av_bprintf(&buf, "%s\n", line);
181  else
182  strcpy(line_cache, line);
183  } else {
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. */
188  char *pline = NULL;
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);
191  if (res < 0)
192  goto end;
193  } else {
194  has_event_info = 1;
195  }
196  tmp_ei.pos = pos;
197  ei = tmp_ei;
198  }
199  }
200 
201  /* Append the last event. Here we force the cache to be flushed, because a
202  * trailing number is more likely to be geniune (for example a copyright
203  * date) and not the event index of an inexistant event */
204  if (has_event_info) {
205  res = add_event(&srt->q, &buf, line_cache, &ei, 1);
206  if (res < 0)
207  goto end;
208  }
209 
211 
212 end:
213  av_bprint_finalize(&buf, NULL);
214  return res;
215 }
216 
218  .p.name = "srt",
219  .p.long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
220  .priv_data_size = sizeof(SRTContext),
221  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
225  .read_seek2 = ff_subtitles_read_seek,
227 };
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_subtitles_read_close
int ff_subtitles_read_close(AVFormatContext *s)
Definition: subtitles.c:337
AV_BPRINT_SIZE_UNLIMITED
#define AV_BPRINT_SIZE_UNLIMITED
ff_text_r8
int ff_text_r8(FFTextReader *r)
Return the next byte.
Definition: subtitles.c:63
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:218
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
av_bprint_init
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
get_event_info
static int get_event_info(const char *line, struct event_info *ei)
Definition: srtdec.c:73
int64_t
long long int64_t
Definition: coverity.c:34
event_info
Definition: srtdec.c:66
SRTContext
Definition: srtenc.c:34
add_event
static int add_event(FFDemuxSubtitlesQueue *q, AVBPrint *buf, char *line_cache, const struct event_info *ei, int append_cache)
Definition: srtdec.c:95
SRTContext::q
FFDemuxSubtitlesQueue q
Definition: srtdec.c:31
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
FFTextReader
Definition: subtitles.h:41
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
ff_subtitles_read_line
ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
Read a line of text.
Definition: subtitles.c:446
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:853
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
event_info::pts
int64_t pts
Definition: srtdec.c:69
ff_subtitles_read_packet
int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: subtitles.c:323
event_info::y1
int32_t y1
Definition: srtdec.c:67
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
srt_probe
static int srt_probe(const AVProbeData *p)
Definition: srtdec.c:34
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AV_PKT_DATA_SUBTITLE_POSITION
@ AV_PKT_DATA_SUBTITLE_POSITION
Subtitle event position.
Definition: packet.h:184
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
event_info::duration
int duration
Definition: srtdec.c:68
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
ff_text_init_avio
void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
Initialize the FFTextReader from the given AVIOContext.
Definition: subtitles.c:26
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:240
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
ff_subtitles_queue_finalize
void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
Set missing durations, sort subtitles by PTS (and then byte position), and drop duplicated events.
Definition: subtitles.c:204
FFDemuxSubtitlesQueue
Definition: subtitles.h:103
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
line
Definition: graph2dot.c:48
event_info::pos
int64_t pos
Definition: srtdec.c:70
ff_subtitles_queue_insert_bprint
AVPacket * ff_subtitles_queue_insert_bprint(FFDemuxSubtitlesQueue *q, const AVBPrint *event, int merge)
Same as ff_subtitles_queue_insert but takes an AVBPrint input.
Definition: subtitles.c:148
bprint.h
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
ff_text_pos
int64_t ff_text_pos(FFTextReader *r)
Return the byte position of the next byte returned by ff_text_r8().
Definition: subtitles.c:58
AV_CODEC_ID_SUBRIP
@ AV_CODEC_ID_SUBRIP
Definition: codec_id.h:566
ff_subtitles_read_seek
int ff_subtitles_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: subtitles.c:329
demux.h
len
int len
Definition: vorbis_enc_data.h:426
AVStream
Stream structure.
Definition: avformat.h:743
event_info::x2
int32_t x2
Definition: srtdec.c:67
srt_read_header
static int srt_read_header(AVFormatContext *s)
Definition: srtdec.c:129
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
av_bprintf
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:99
ff_text_peek_r8
int ff_text_peek_r8(FFTextReader *r)
Like ff_text_r8(), but don't remove the byte from the buffer.
Definition: subtitles.c:95
event_info::y2
int32_t y2
Definition: srtdec.c:67
subtitles.h
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:231
av_bprint_clear
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:232
ff_text_init_buf
void ff_text_init_buf(FFTextReader *r, const void *buf, size_t size)
Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.
Definition: subtitles.c:52
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
ff_text_eof
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition: subtitles.c:90
event_info::x1
int32_t x1
Definition: srtdec.c:67
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:542
FFInputFormat
Definition: demux.h:37
int32_t
int32_t
Definition: audioconvert.c:56
ff_srt_demuxer
const FFInputFormat ff_srt_demuxer
Definition: srtdec.c:217