]> git.sesse.net Git - ffmpeg/blob - libavformat/srtdec.c
Merge commit '6906b19346ae8a330bfaa1c16ce535be10789723'
[ffmpeg] / libavformat / srtdec.c
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 {
29     FFDemuxSubtitlesQueue q;
30 } SRTContext;
31
32 static int srt_probe(AVProbeData *p)
33 {
34     unsigned char *ptr = p->buf;
35     int i, v, num = 0;
36
37     if (AV_RB24(ptr) == 0xEFBBBF)
38         ptr += 3;  /* skip UTF-8 BOM */
39
40     for (i=0; i<2; i++) {
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;
44         num = atoi(ptr);
45         ptr += strcspn(ptr, "\n") + 1;
46     }
47     return 0;
48 }
49
50 static int64_t get_pts(const char **buf, int *duration,
51                        int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
52 {
53     int i;
54
55     for (i=0; i<2; i++) {
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;
67             return start;
68         }
69         *buf += strcspn(*buf, "\n") + 1;
70     }
71     return AV_NOPTS_VALUE;
72 }
73
74 static inline int is_eol(char c)
75 {
76     return c == '\r' || c == '\n';
77 }
78
79 static void read_chunk(AVIOContext *pb, AVBPrint *buf)
80 {
81     char eol_buf[5];
82     int n = 0, i = 0, nb_eol = 0;
83
84     av_bprint_clear(buf);
85
86     for (;;) {
87         char c = avio_r8(pb);
88
89         if (!c)
90             break;
91
92         /* ignore all initial line breaks */
93         if (n == 0 && is_eol(c))
94             continue;
95
96         /* line break buffering: we don't want to add the trailing \r\n */
97         if (is_eol(c)) {
98             nb_eol += c == '\n';
99             if (nb_eol == 2)
100                 break;
101             eol_buf[i++] = c;
102             if (i == sizeof(eol_buf) - 1)
103                 break;
104             continue;
105         }
106
107         /* only one line break followed by data: we flush the line breaks
108          * buffer */
109         if (i) {
110             eol_buf[i] = 0;
111             av_bprintf(buf, "%s", eol_buf);
112             i = nb_eol = 0;
113         }
114
115         av_bprint_chars(buf, c, 1);
116         n++;
117     }
118
119     /* FIXME: remove the following when the lavc SubRip decoder is fixed
120      * (trailing tags are not correctly flushed, see what happens to FATE when
121      * you disable this code) */
122     if (buf->len)
123         av_bprintf(buf, "\n");
124 }
125
126 static int srt_read_header(AVFormatContext *s)
127 {
128     SRTContext *srt = s->priv_data;
129     AVBPrint buf;
130     AVStream *st = avformat_new_stream(s, NULL);
131     int res = 0;
132
133     if (!st)
134         return AVERROR(ENOMEM);
135     avpriv_set_pts_info(st, 64, 1, 1000);
136     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
137     st->codec->codec_id   = AV_CODEC_ID_SUBRIP;
138
139     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
140
141     while (!url_feof(s->pb)) {
142         read_chunk(s->pb, &buf);
143
144         if (buf.len) {
145             int64_t pos = avio_tell(s->pb);
146             int64_t pts;
147             int duration;
148             const char *ptr = buf.str;
149             int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
150             AVPacket *sub;
151
152             pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
153             if (pts != AV_NOPTS_VALUE) {
154                 int len = buf.len - (ptr - buf.str);
155                 sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
156                 if (!sub) {
157                     res = AVERROR(ENOMEM);
158                     goto end;
159                 }
160                 sub->pos = pos;
161                 sub->pts = pts;
162                 sub->duration = duration;
163                 if (x1 != -1) {
164                     uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
165                     if (p) {
166                         AV_WL32(p,      x1);
167                         AV_WL32(p +  4, y1);
168                         AV_WL32(p +  8, x2);
169                         AV_WL32(p + 12, y2);
170                     }
171                 }
172             }
173         }
174     }
175
176     ff_subtitles_queue_finalize(&srt->q);
177
178 end:
179     av_bprint_finalize(&buf, NULL);
180     return res;
181 }
182
183 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
184 {
185     SRTContext *srt = s->priv_data;
186     return ff_subtitles_queue_read_packet(&srt->q, pkt);
187 }
188
189 static int srt_read_seek(AVFormatContext *s, int stream_index,
190                          int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
191 {
192     SRTContext *srt = s->priv_data;
193     return ff_subtitles_queue_seek(&srt->q, s, stream_index,
194                                    min_ts, ts, max_ts, flags);
195 }
196
197 static int srt_read_close(AVFormatContext *s)
198 {
199     SRTContext *srt = s->priv_data;
200     ff_subtitles_queue_clean(&srt->q);
201     return 0;
202 }
203
204 AVInputFormat ff_srt_demuxer = {
205     .name        = "srt",
206     .long_name   = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
207     .priv_data_size = sizeof(SRTContext),
208     .read_probe  = srt_probe,
209     .read_header = srt_read_header,
210     .read_packet = srt_read_packet,
211     .read_seek2  = srt_read_seek,
212     .read_close  = srt_read_close,
213     .flags       = AVFMT_GENERIC_INDEX,
214 };