FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
srtdec.c
Go to the documentation of this file.
1 /*
2  * SubRip subtitle demuxer
3  * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #include "avformat.h"
23 #include "internal.h"
24 #include "subtitles.h"
25 #include "libavutil/bprint.h"
26 #include "libavutil/intreadwrite.h"
27 
28 typedef struct {
30 } SRTContext;
31 
32 static int srt_probe(AVProbeData *p)
33 {
34  int v;
35  char buf[64], *pbuf;
36  FFTextReader tr;
37 
38  ff_text_init_buf(&tr, p->buf, p->buf_size);
39 
40  while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
41  ff_text_r8(&tr);
42 
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  if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
46  strtol(buf, &pbuf, 10) < 0 || *pbuf)
47  return 0;
48 
49  /* Check if the next line matches a SRT timestamp */
50  if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
51  return 0;
52  if (buf[0] >= '0' && buf[1] <= '9' && strstr(buf, " --> ")
53  && sscanf(buf, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
54  return AVPROBE_SCORE_MAX;
55 
56  return 0;
57 }
58 
59 static int64_t get_pts(const char **buf, int *duration,
60  int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
61 {
62  int i;
63 
64  for (i=0; i<2; i++) {
65  int hh1, mm1, ss1, ms1;
66  int hh2, mm2, ss2, ms2;
67  if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
68  "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
69  &hh1, &mm1, &ss1, &ms1,
70  &hh2, &mm2, &ss2, &ms2,
71  x1, x2, y1, y2) >= 8) {
72  int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
73  int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
74  *duration = end - start;
75  *buf += ff_subtitles_next_line(*buf);
76  return start;
77  }
78  *buf += ff_subtitles_next_line(*buf);
79  }
80  return AV_NOPTS_VALUE;
81 }
82 
84 {
85  SRTContext *srt = s->priv_data;
86  AVBPrint buf;
87  AVStream *st = avformat_new_stream(s, NULL);
88  int res = 0;
89  FFTextReader tr;
90  ff_text_init_avio(&tr, s->pb);
91 
92  if (!st)
93  return AVERROR(ENOMEM);
94  avpriv_set_pts_info(st, 64, 1, 1000);
97 
99 
100  while (!ff_text_eof(&tr)) {
101  ff_subtitles_read_text_chunk(&tr, &buf);
102 
103  if (buf.len) {
104  int64_t pos = ff_text_pos(&tr);
105  int64_t pts;
106  int duration;
107  const char *ptr = buf.str;
108  int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
109  AVPacket *sub;
110 
111  pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
112  if (pts != AV_NOPTS_VALUE) {
113  int len = buf.len - (ptr - buf.str);
114  if (len <= 0)
115  continue;
116  sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
117  if (!sub) {
118  res = AVERROR(ENOMEM);
119  goto end;
120  }
121  sub->pos = pos;
122  sub->pts = pts;
123  sub->duration = duration;
124  if (x1 != -1) {
126  if (p) {
127  AV_WL32(p, x1);
128  AV_WL32(p + 4, y1);
129  AV_WL32(p + 8, x2);
130  AV_WL32(p + 12, y2);
131  }
132  }
133  }
134  }
135  }
136 
138 
139 end:
140  av_bprint_finalize(&buf, NULL);
141  return res;
142 }
143 
145 {
146  SRTContext *srt = s->priv_data;
147  return ff_subtitles_queue_read_packet(&srt->q, pkt);
148 }
149 
150 static int srt_read_seek(AVFormatContext *s, int stream_index,
151  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
152 {
153  SRTContext *srt = s->priv_data;
154  return ff_subtitles_queue_seek(&srt->q, s, stream_index,
155  min_ts, ts, max_ts, flags);
156 }
157 
159 {
160  SRTContext *srt = s->priv_data;
162  return 0;
163 }
164 
166  .name = "srt",
167  .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
168  .priv_data_size = sizeof(SRTContext),
172  .read_seek2 = srt_read_seek,
174 };