]> git.sesse.net Git - ffmpeg/blob - libavformat/tedcaptionsdec.c
Merge commit 'c93ccf5a4cca722b39f05e9f5660b4cb75bc1740'
[ffmpeg] / libavformat / tedcaptionsdec.c
1 /*
2  * TED Talks captions format decoder
3  * Copyright (c) 2012 Nicolas George
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 "libavutil/bprint.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
25 #include "avformat.h"
26 #include "internal.h"
27 #include "subtitles.h"
28
29 typedef struct {
30     AVClass *class;
31     int64_t start_time;
32     FFDemuxSubtitlesQueue subs;
33 } TEDCaptionsDemuxer;
34
35 static const AVOption tedcaptions_options[] = {
36     { "start_time", "set the start time (offset) of the subtitles, in ms",
37       offsetof(TEDCaptionsDemuxer, start_time), FF_OPT_TYPE_INT64,
38       { .i64 = 15000 }, INT64_MIN, INT64_MAX,
39       AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM },
40     { NULL },
41 };
42
43 static const AVClass tedcaptions_demuxer_class = {
44     .class_name = "tedcaptions_demuxer",
45     .item_name  = av_default_item_name,
46     .option     = tedcaptions_options,
47     .version    = LIBAVUTIL_VERSION_INT,
48 };
49
50 #define BETWEEN(a, amin, amax) ((unsigned)((a) - (amin)) <= (amax) - (amin))
51
52 #define HEX_DIGIT_TEST(c) (BETWEEN(c, '0', '9') || BETWEEN((c) | 32, 'a', 'z'))
53 #define HEX_DIGIT_VAL(c) ((c) <= '9' ? (c) - '0' : ((c) | 32) - 'a' + 10)
54 #define ERR_CODE(c) (c < 0 ? c : AVERROR_INVALIDDATA)
55
56 static void av_bprint_utf8(AVBPrint *bp, unsigned c)
57 {
58     int bytes, i;
59
60     if (c <= 0x7F) {
61         av_bprint_chars(bp, c, 1);
62         return;
63     }
64     bytes = (av_log2(c) - 2) / 5;
65     av_bprint_chars(bp, (c >> (bytes * 6)) | ((0xFF80 >> bytes) & 0xFF), 1);
66     for (i = bytes - 1; i >= 0; i--)
67         av_bprint_chars(bp, ((c >> (i * 6)) & 0x3F) | 0x80, 1);
68 }
69
70 static void next_byte(AVIOContext *pb, int *cur_byte)
71 {
72     uint8_t b;
73     int ret = avio_read(pb, &b, 1);
74     *cur_byte = ret > 0 ? b : ret == 0 ? AVERROR_EOF : ret;
75 }
76
77 static void skip_spaces(AVIOContext *pb, int *cur_byte)
78 {
79     while (*cur_byte == ' '  || *cur_byte == '\t' ||
80            *cur_byte == '\n' || *cur_byte == '\r')
81         next_byte(pb, cur_byte);
82 }
83
84 static int expect_byte(AVIOContext *pb, int *cur_byte, uint8_t c)
85 {
86     skip_spaces(pb, cur_byte);
87     if (*cur_byte != c)
88         return ERR_CODE(*cur_byte);
89     next_byte(pb, cur_byte);
90     return 0;
91 }
92
93 static int parse_string(AVIOContext *pb, int *cur_byte, AVBPrint *bp, int full)
94 {
95     int ret;
96
97     av_bprint_init(bp, 0, full ? -1 : 1);
98     ret = expect_byte(pb, cur_byte, '"');
99     if (ret < 0)
100         goto fail;
101     while (*cur_byte > 0 && *cur_byte != '"') {
102         if (*cur_byte == '\\') {
103             next_byte(pb, cur_byte);
104             if (*cur_byte < 0) {
105                 ret = AVERROR_INVALIDDATA;
106                 goto fail;
107             }
108             if ((*cur_byte | 32) == 'u') {
109                 unsigned chr = 0, i;
110                 for (i = 0; i < 4; i++) {
111                     next_byte(pb, cur_byte);
112                     if (!HEX_DIGIT_TEST(*cur_byte)) {
113                         ret = ERR_CODE(*cur_byte);
114                         goto fail;
115                     }
116                     chr = chr * 16 + HEX_DIGIT_VAL(*cur_byte);
117                 }
118                 av_bprint_utf8(bp, chr);
119             } else {
120                 av_bprint_chars(bp, *cur_byte, 1);
121             }
122         } else {
123             av_bprint_chars(bp, *cur_byte, 1);
124         }
125         next_byte(pb, cur_byte);
126     }
127     ret = expect_byte(pb, cur_byte, '"');
128     if (ret < 0)
129         goto fail;
130     if (full && !av_bprint_is_complete(bp)) {
131         ret = AVERROR(ENOMEM);
132         goto fail;
133     }
134     return 0;
135
136 fail:
137     av_bprint_finalize(bp, NULL);
138     return ret;
139 }
140
141 static int parse_label(AVIOContext *pb, int *cur_byte, AVBPrint *bp)
142 {
143     int ret;
144
145     ret = parse_string(pb, cur_byte, bp, 0);
146     if (ret < 0)
147         return ret;
148     ret = expect_byte(pb, cur_byte, ':');
149     if (ret < 0)
150         return ret;
151     return 0;
152 }
153
154 static int parse_boolean(AVIOContext *pb, int *cur_byte, int *result)
155 {
156     const char *text[] = { "false", "true" }, *p;
157     int i;
158
159     skip_spaces(pb, cur_byte);
160     for (i = 0; i < 2; i++) {
161         p = text[i];
162         if (*cur_byte != *p)
163             continue;
164         for (; *p; p++, next_byte(pb, cur_byte))
165             if (*cur_byte != *p)
166                 return AVERROR_INVALIDDATA;
167         if (BETWEEN(*cur_byte | 32, 'a', 'z'))
168             return AVERROR_INVALIDDATA;
169         *result = i;
170         return 0;
171     }
172     return AVERROR_INVALIDDATA;
173 }
174
175 static int parse_int(AVIOContext *pb, int *cur_byte, int64_t *result)
176 {
177     int64_t val = 0;
178
179     skip_spaces(pb, cur_byte);
180     if ((unsigned)*cur_byte - '0' > 9)
181         return AVERROR_INVALIDDATA;
182     while (BETWEEN(*cur_byte, '0', '9')) {
183         val = val * 10 + (*cur_byte - '0');
184         next_byte(pb, cur_byte);
185     }
186     *result = val;
187     return 0;
188 }
189
190 static int parse_file(AVIOContext *pb, FFDemuxSubtitlesQueue *subs)
191 {
192     int ret, cur_byte, start_of_par;
193     AVBPrint label, content;
194     int64_t pos, start, duration;
195     AVPacket *pkt;
196
197     next_byte(pb, &cur_byte);
198     ret = expect_byte(pb, &cur_byte, '{');
199     if (ret < 0)
200         return AVERROR_INVALIDDATA;
201     ret = parse_label(pb, &cur_byte, &label);
202     if (ret < 0 || strcmp(label.str, "captions"))
203         return AVERROR_INVALIDDATA;
204     ret = expect_byte(pb, &cur_byte, '[');
205     if (ret < 0)
206         return AVERROR_INVALIDDATA;
207     while (1) {
208         content.size = 0;
209         start = duration = AV_NOPTS_VALUE;
210         ret = expect_byte(pb, &cur_byte, '{');
211         if (ret < 0)
212             return ret;
213         pos = avio_tell(pb) - 1;
214         while (1) {
215             ret = parse_label(pb, &cur_byte, &label);
216             if (ret < 0)
217                 return ret;
218             if (!strcmp(label.str, "startOfParagraph")) {
219                 ret = parse_boolean(pb, &cur_byte, &start_of_par);
220                 if (ret < 0)
221                     return ret;
222             } else if (!strcmp(label.str, "content")) {
223                 ret = parse_string(pb, &cur_byte, &content, 1);
224                 if (ret < 0)
225                     return ret;
226             } else if (!strcmp(label.str, "startTime")) {
227                 ret = parse_int(pb, &cur_byte, &start);
228                 if (ret < 0)
229                     return ret;
230             } else if (!strcmp(label.str, "duration")) {
231                 ret = parse_int(pb, &cur_byte, &duration);
232                 if (ret < 0)
233                     return ret;
234             } else {
235                 return AVERROR_INVALIDDATA;
236             }
237             skip_spaces(pb, &cur_byte);
238             if (cur_byte != ',')
239                 break;
240             next_byte(pb, &cur_byte);
241         }
242         ret = expect_byte(pb, &cur_byte, '}');
243         if (ret < 0)
244             return ret;
245
246         if (!content.size || start == AV_NOPTS_VALUE ||
247             duration == AV_NOPTS_VALUE)
248             return AVERROR_INVALIDDATA;
249         pkt = ff_subtitles_queue_insert(subs, content.str, content.len, 0);
250         if (!pkt)
251             return AVERROR(ENOMEM);
252         pkt->pos      = pos;
253         pkt->pts      = start;
254         pkt->duration = duration;
255         av_bprint_finalize(&content, NULL);
256
257         skip_spaces(pb, &cur_byte);
258         if (cur_byte != ',')
259             break;
260         next_byte(pb, &cur_byte);
261     }
262     ret = expect_byte(pb, &cur_byte, ']');
263     if (ret < 0)
264         return ret;
265     ret = expect_byte(pb, &cur_byte, '}');
266     if (ret < 0)
267         return ret;
268     skip_spaces(pb, &cur_byte);
269     if (cur_byte != AVERROR_EOF)
270         return ERR_CODE(cur_byte);
271     return 0;
272 }
273
274 static av_cold int tedcaptions_read_header(AVFormatContext *avf)
275 {
276     TEDCaptionsDemuxer *tc = avf->priv_data;
277     AVStream *st;
278     int ret, i;
279     AVPacket *last;
280
281     ret = parse_file(avf->pb, &tc->subs);
282     if (ret < 0) {
283         if (ret == AVERROR_INVALIDDATA)
284             av_log(avf, AV_LOG_ERROR, "Syntax error near offset %"PRId64".\n",
285                    avio_tell(avf->pb));
286         ff_subtitles_queue_clean(&tc->subs);
287         return ret;
288     }
289     ff_subtitles_queue_finalize(&tc->subs);
290     for (i = 0; i < tc->subs.nb_subs; i++)
291         tc->subs.subs[i].pts += tc->start_time;
292
293     last = &tc->subs.subs[tc->subs.nb_subs - 1];
294     st = avformat_new_stream(avf, NULL);
295     if (!st)
296         return AVERROR(ENOMEM);
297     st->codec->codec_type     = AVMEDIA_TYPE_SUBTITLE;
298     st->codec->codec_id       = AV_CODEC_ID_TEXT;
299     avpriv_set_pts_info(st, 64, 1, 1000);
300     st->probe_packets = 0;
301     st->start_time    = 0;
302     st->duration      = last->pts + last->duration;
303     st->cur_dts       = 0;
304
305     return 0;
306 }
307
308 static int tedcaptions_read_packet(AVFormatContext *avf, AVPacket *packet)
309 {
310     TEDCaptionsDemuxer *tc = avf->priv_data;
311
312     return ff_subtitles_queue_read_packet(&tc->subs, packet);
313 }
314
315 static int tedcaptions_read_close(AVFormatContext *avf)
316 {
317     TEDCaptionsDemuxer *tc = avf->priv_data;
318
319     ff_subtitles_queue_clean(&tc->subs);
320     return 0;
321 }
322
323 static av_cold int tedcaptions_read_probe(AVProbeData *p)
324 {
325     static const char *const tags[] = {
326         "\"captions\"", "\"duration\"", "\"content\"",
327         "\"startOfParagraph\"", "\"startTime\"",
328     };
329     unsigned i, count = 0;
330     const char *t;
331
332     if (p->buf[strspn(p->buf, " \t\r\n")] != '{')
333         return 0;
334     for (i = 0; i < FF_ARRAY_ELEMS(tags); i++) {
335         if (!(t = strstr(p->buf, tags[i])))
336             continue;
337         t += strlen(tags[i]);
338         t += strspn(t, " \t\r\n");
339         if (*t == ':')
340             count++;
341     }
342     return count == FF_ARRAY_ELEMS(tags) ? AVPROBE_SCORE_MAX :
343            count                         ? AVPROBE_SCORE_EXTENSION : 0;
344 }
345
346 static int tedcaptions_read_seek(AVFormatContext *avf, int stream_index,
347                                  int64_t min_ts, int64_t ts, int64_t max_ts,
348                                  int flags)
349 {
350     TEDCaptionsDemuxer *tc = avf->priv_data;
351     return ff_subtitles_queue_seek(&tc->subs, avf, stream_index,
352                                    min_ts, ts, max_ts, flags);
353 }
354
355 AVInputFormat ff_tedcaptions_demuxer = {
356     .name           = "tedcaptions",
357     .long_name      = NULL_IF_CONFIG_SMALL("TED Talks captions"),
358     .priv_data_size = sizeof(TEDCaptionsDemuxer),
359     .priv_class     = &tedcaptions_demuxer_class,
360     .read_header    = tedcaptions_read_header,
361     .read_packet    = tedcaptions_read_packet,
362     .read_close     = tedcaptions_read_close,
363     .read_probe     = tedcaptions_read_probe,
364     .read_seek2     = tedcaptions_read_seek,
365 };