]> git.sesse.net Git - ffmpeg/blob - libavformat/srtdec.c
Merge commit 'cb45553f577f8e0ebfe05d3287e1b6fa5859b967'
[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 "libavutil/bprint.h"
25 #include "libavutil/intreadwrite.h"
26
27 static int srt_probe(AVProbeData *p)
28 {
29     unsigned char *ptr = p->buf;
30     int i, v, num = 0;
31
32     if (AV_RB24(ptr) == 0xEFBBBF)
33         ptr += 3;  /* skip UTF-8 BOM */
34
35     for (i=0; i<2; i++) {
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;
39         num = atoi(ptr);
40         ptr += strcspn(ptr, "\n") + 1;
41     }
42     return 0;
43 }
44
45 static int srt_read_header(AVFormatContext *s)
46 {
47     AVStream *st = avformat_new_stream(s, NULL);
48     if (!st)
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;
53     return 0;
54 }
55
56 static int64_t get_pts(const char **buf, int *duration,
57                        int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
58 {
59     int i;
60
61     for (i=0; i<2; i++) {
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;
73             return start;
74         }
75         *buf += strcspn(*buf, "\n") + 1;
76     }
77     return AV_NOPTS_VALUE;
78 }
79
80 static inline int is_eol(char c)
81 {
82     return c == '\r' || c == '\n';
83 }
84
85 static void read_chunk(AVIOContext *pb, AVBPrint *buf)
86 {
87     char eol_buf[5];
88     int n = 0, i = 0, nb_eol = 0;
89
90     for (;;) {
91         char c = avio_r8(pb);
92
93         if (!c)
94             break;
95
96         /* ignore all initial line breaks */
97         if (n == 0 && is_eol(c))
98             continue;
99
100         /* line break buffering: we don't want to add the trailing \r\n */
101         if (is_eol(c)) {
102             nb_eol += c == '\n';
103             if (nb_eol == 2)
104                 break;
105             eol_buf[i++] = c;
106             if (i == sizeof(eol_buf) - 1)
107                 break;
108             continue;
109         }
110
111         /* only one line break followed by data: we flush the line breaks
112          * buffer */
113         if (i) {
114             eol_buf[i] = 0;
115             av_bprintf(buf, "%s", eol_buf);
116             i = nb_eol = 0;
117         }
118
119         av_bprint_chars(buf, c, 1);
120         n++;
121     }
122
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) */
126     if (buf->len)
127         av_bprintf(buf, "\n");
128 }
129
130 static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
131 {
132     AVBPrint buf;
133     int64_t pos = avio_tell(s->pb);
134     int res = AVERROR_EOF;
135
136     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
137     read_chunk(s->pb, &buf);
138
139     if (buf.len) {
140         int64_t pts;
141         int duration, pkt_size;
142         const char *ptr = buf.str;
143         int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
144
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;
150             pkt->pos = pos;
151             pkt->pts = pkt->dts = pts;
152             pkt->duration = duration;
153             if (x1 != -1) {
154                 uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, 16);
155                 if (p) {
156                     AV_WL32(p,      x1);
157                     AV_WL32(p +  4, y1);
158                     AV_WL32(p +  8, x2);
159                     AV_WL32(p + 12, y2);
160                 }
161             }
162         }
163     }
164     av_bprint_finalize(&buf, NULL);
165     return res;
166 }
167
168 AVInputFormat ff_srt_demuxer = {
169     .name        = "srt",
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,
175 };