Commit | Line | Data |
---|---|---|
2ba45a60 DM |
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 | int i, v, num = 0; | |
35 | FFTextReader tr; | |
36 | ||
37 | ff_text_init_buf(&tr, p->buf, p->buf_size); | |
38 | ||
39 | while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n') | |
40 | ff_text_r8(&tr); | |
41 | for (i=0; i<2; i++) { | |
42 | char buf[128]; | |
43 | if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0) | |
44 | break; | |
45 | if ((num == i || num + 1 == i) | |
46 | && buf[0] >= '0' && buf[1] <= '9' && strstr(buf, " --> ") | |
47 | && sscanf(buf, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1) | |
48 | return AVPROBE_SCORE_MAX; | |
49 | num = atoi(buf); | |
50 | } | |
51 | return 0; | |
52 | } | |
53 | ||
54 | static int64_t get_pts(const char **buf, int *duration, | |
55 | int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2) | |
56 | { | |
57 | int i; | |
58 | ||
59 | for (i=0; i<2; i++) { | |
60 | int hh1, mm1, ss1, ms1; | |
61 | int hh2, mm2, ss2, ms2; | |
62 | if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d" | |
63 | "%*[ ]X1:%u X2:%u Y1:%u Y2:%u", | |
64 | &hh1, &mm1, &ss1, &ms1, | |
65 | &hh2, &mm2, &ss2, &ms2, | |
66 | x1, x2, y1, y2) >= 8) { | |
67 | int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1; | |
68 | int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2; | |
69 | *duration = end - start; | |
70 | *buf += ff_subtitles_next_line(*buf); | |
71 | return start; | |
72 | } | |
73 | *buf += ff_subtitles_next_line(*buf); | |
74 | } | |
75 | return AV_NOPTS_VALUE; | |
76 | } | |
77 | ||
78 | static int srt_read_header(AVFormatContext *s) | |
79 | { | |
80 | SRTContext *srt = s->priv_data; | |
81 | AVBPrint buf; | |
82 | AVStream *st = avformat_new_stream(s, NULL); | |
83 | int res = 0; | |
84 | FFTextReader tr; | |
85 | ff_text_init_avio(&tr, s->pb); | |
86 | ||
87 | if (!st) | |
88 | return AVERROR(ENOMEM); | |
89 | avpriv_set_pts_info(st, 64, 1, 1000); | |
90 | st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE; | |
91 | st->codec->codec_id = AV_CODEC_ID_SUBRIP; | |
92 | ||
93 | av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED); | |
94 | ||
95 | while (!ff_text_eof(&tr)) { | |
96 | ff_subtitles_read_text_chunk(&tr, &buf); | |
97 | ||
98 | if (buf.len) { | |
99 | int64_t pos = ff_text_pos(&tr); | |
100 | int64_t pts; | |
101 | int duration; | |
102 | const char *ptr = buf.str; | |
103 | int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1; | |
104 | AVPacket *sub; | |
105 | ||
106 | pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2); | |
107 | if (pts != AV_NOPTS_VALUE) { | |
108 | int len = buf.len - (ptr - buf.str); | |
109 | if (len <= 0) | |
110 | continue; | |
111 | sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0); | |
112 | if (!sub) { | |
113 | res = AVERROR(ENOMEM); | |
114 | goto end; | |
115 | } | |
116 | sub->pos = pos; | |
117 | sub->pts = pts; | |
118 | sub->duration = duration; | |
119 | if (x1 != -1) { | |
120 | uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16); | |
121 | if (p) { | |
122 | AV_WL32(p, x1); | |
123 | AV_WL32(p + 4, y1); | |
124 | AV_WL32(p + 8, x2); | |
125 | AV_WL32(p + 12, y2); | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | ff_subtitles_queue_finalize(&srt->q); | |
133 | ||
134 | end: | |
135 | av_bprint_finalize(&buf, NULL); | |
136 | return res; | |
137 | } | |
138 | ||
139 | static int srt_read_packet(AVFormatContext *s, AVPacket *pkt) | |
140 | { | |
141 | SRTContext *srt = s->priv_data; | |
142 | return ff_subtitles_queue_read_packet(&srt->q, pkt); | |
143 | } | |
144 | ||
145 | static int srt_read_seek(AVFormatContext *s, int stream_index, | |
146 | int64_t min_ts, int64_t ts, int64_t max_ts, int flags) | |
147 | { | |
148 | SRTContext *srt = s->priv_data; | |
149 | return ff_subtitles_queue_seek(&srt->q, s, stream_index, | |
150 | min_ts, ts, max_ts, flags); | |
151 | } | |
152 | ||
153 | static int srt_read_close(AVFormatContext *s) | |
154 | { | |
155 | SRTContext *srt = s->priv_data; | |
156 | ff_subtitles_queue_clean(&srt->q); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | AVInputFormat ff_srt_demuxer = { | |
161 | .name = "srt", | |
162 | .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"), | |
163 | .priv_data_size = sizeof(SRTContext), | |
164 | .read_probe = srt_probe, | |
165 | .read_header = srt_read_header, | |
166 | .read_packet = srt_read_packet, | |
167 | .read_seek2 = srt_read_seek, | |
168 | .read_close = srt_read_close, | |
169 | }; |