FFmpeg  4.4.4
tedcaptionsdec.c
Go to the documentation of this file.
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;
34 
35 static const AVOption tedcaptions_options[] = {
36  { "start_time", "set the start time (offset) of the subtitles, in ms",
38  { .i64 = 15000 }, INT64_MIN, INT64_MAX,
40  { NULL },
41 };
42 
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  ret = expect_byte(pb, cur_byte, '"');
98  if (ret < 0)
99  return ret;
100  while (*cur_byte > 0 && *cur_byte != '"') {
101  if (*cur_byte == '\\') {
102  next_byte(pb, cur_byte);
103  if (*cur_byte < 0)
104  return AVERROR_INVALIDDATA;
105  if ((*cur_byte | 32) == 'u') {
106  unsigned chr = 0, i;
107  for (i = 0; i < 4; i++) {
108  next_byte(pb, cur_byte);
109  if (!HEX_DIGIT_TEST(*cur_byte))
110  return ERR_CODE(*cur_byte);
111  chr = chr * 16 + HEX_DIGIT_VAL(*cur_byte);
112  }
113  av_bprint_utf8(bp, chr);
114  } else {
115  av_bprint_chars(bp, *cur_byte, 1);
116  }
117  } else {
118  av_bprint_chars(bp, *cur_byte, 1);
119  }
120  next_byte(pb, cur_byte);
121  }
122  ret = expect_byte(pb, cur_byte, '"');
123  if (ret < 0)
124  return ret;
125  if (full && !av_bprint_is_complete(bp))
126  return AVERROR(ENOMEM);
127 
128  return 0;
129 }
130 
131 static int parse_label(AVIOContext *pb, int *cur_byte, AVBPrint *bp)
132 {
133  int ret;
134 
136  ret = parse_string(pb, cur_byte, bp, 0);
137  if (ret < 0)
138  return ret;
139  ret = expect_byte(pb, cur_byte, ':');
140  if (ret < 0)
141  return ret;
142  return 0;
143 }
144 
145 static int parse_boolean(AVIOContext *pb, int *cur_byte, int *result)
146 {
147  static const char * const text[] = { "false", "true" };
148  const char *p;
149  int i;
150 
151  skip_spaces(pb, cur_byte);
152  for (i = 0; i < 2; i++) {
153  p = text[i];
154  if (*cur_byte != *p)
155  continue;
156  for (; *p; p++, next_byte(pb, cur_byte))
157  if (*cur_byte != *p)
158  return AVERROR_INVALIDDATA;
159  if (BETWEEN(*cur_byte | 32, 'a', 'z'))
160  return AVERROR_INVALIDDATA;
161  *result = i;
162  return 0;
163  }
164  return AVERROR_INVALIDDATA;
165 }
166 
167 static int parse_int(AVIOContext *pb, int *cur_byte, int64_t *result)
168 {
169  int64_t val = 0;
170 
171  skip_spaces(pb, cur_byte);
172  if ((unsigned)*cur_byte - '0' > 9)
173  return AVERROR_INVALIDDATA;
174  while (BETWEEN(*cur_byte, '0', '9')) {
175  if (val > INT_MAX/10 - (*cur_byte - '0'))
176  return AVERROR_INVALIDDATA;
177  val = val * 10 + (*cur_byte - '0');
178  next_byte(pb, cur_byte);
179  }
180  *result = val;
181  return 0;
182 }
183 
185 {
186  int ret, cur_byte, start_of_par;
187  AVBPrint label, content;
188  int64_t pos, start, duration;
189  AVPacket *pkt;
190 
192 
193  next_byte(pb, &cur_byte);
194  ret = expect_byte(pb, &cur_byte, '{');
195  if (ret < 0)
196  return AVERROR_INVALIDDATA;
197  ret = parse_label(pb, &cur_byte, &label);
198  if (ret < 0 || strcmp(label.str, "captions"))
199  return AVERROR_INVALIDDATA;
200  ret = expect_byte(pb, &cur_byte, '[');
201  if (ret < 0)
202  return AVERROR_INVALIDDATA;
203  while (1) {
204  start = duration = AV_NOPTS_VALUE;
205  ret = expect_byte(pb, &cur_byte, '{');
206  if (ret < 0)
207  goto fail;
208  pos = avio_tell(pb) - 1;
209  while (1) {
210  ret = parse_label(pb, &cur_byte, &label);
211  if (ret < 0)
212  goto fail;
213  if (!strcmp(label.str, "startOfParagraph")) {
214  ret = parse_boolean(pb, &cur_byte, &start_of_par);
215  if (ret < 0)
216  goto fail;
217  } else if (!strcmp(label.str, "content")) {
218  ret = parse_string(pb, &cur_byte, &content, 1);
219  if (ret < 0)
220  goto fail;
221  } else if (!strcmp(label.str, "startTime")) {
222  ret = parse_int(pb, &cur_byte, &start);
223  if (ret < 0)
224  goto fail;
225  } else if (!strcmp(label.str, "duration")) {
226  ret = parse_int(pb, &cur_byte, &duration);
227  if (ret < 0)
228  goto fail;
229  } else {
230  ret = AVERROR_INVALIDDATA;
231  goto fail;
232  }
233  skip_spaces(pb, &cur_byte);
234  if (cur_byte != ',')
235  break;
236  next_byte(pb, &cur_byte);
237  }
238  ret = expect_byte(pb, &cur_byte, '}');
239  if (ret < 0)
240  goto fail;
241 
242  if (!content.size || start == AV_NOPTS_VALUE ||
244  ret = AVERROR_INVALIDDATA;
245  goto fail;
246  }
247  pkt = ff_subtitles_queue_insert(subs, content.str, content.len, 0);
248  if (!pkt) {
249  ret = AVERROR(ENOMEM);
250  goto fail;
251  }
252  pkt->pos = pos;
253  pkt->pts = start;
254  pkt->duration = duration;
255  av_bprint_clear(&content);
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  goto fail;
265  ret = expect_byte(pb, &cur_byte, '}');
266  if (ret < 0)
267  goto fail;
268  skip_spaces(pb, &cur_byte);
269  if (cur_byte != AVERROR_EOF)
270  ret = ERR_CODE(cur_byte);
271 fail:
272  av_bprint_finalize(&content, NULL);
273  return ret;
274 }
275 
277 {
279  AVStream *st = avformat_new_stream(avf, NULL);
280  int ret, i;
281  AVPacket *last;
282 
283  if (!st)
284  return AVERROR(ENOMEM);
285 
286  ret = parse_file(avf->pb, &tc->subs);
287  if (ret < 0) {
288  if (ret == AVERROR_INVALIDDATA)
289  av_log(avf, AV_LOG_ERROR, "Syntax error near offset %"PRId64".\n",
290  avio_tell(avf->pb));
292  return ret;
293  }
294  ff_subtitles_queue_finalize(avf, &tc->subs);
295  for (i = 0; i < tc->subs.nb_subs; i++)
296  tc->subs.subs[i]->pts += tc->start_time;
297 
298  last = tc->subs.subs[tc->subs.nb_subs - 1];
301  avpriv_set_pts_info(st, 64, 1, 1000);
302  st->probe_packets = 0;
303  st->start_time = 0;
304  st->duration = last->pts + last->duration;
305  st->cur_dts = 0;
306 
307  return 0;
308 }
309 
311 {
313 
314  return ff_subtitles_queue_read_packet(&tc->subs, packet);
315 }
316 
318 {
320 
322  return 0;
323 }
324 
326 {
327  static const char *const tags[] = {
328  "\"captions\"", "\"duration\"", "\"content\"",
329  "\"startOfParagraph\"", "\"startTime\"",
330  };
331  unsigned i, count = 0;
332  const char *t;
333 
334  if (p->buf[strspn(p->buf, " \t\r\n")] != '{')
335  return 0;
336  for (i = 0; i < FF_ARRAY_ELEMS(tags); i++) {
337  if (!(t = strstr(p->buf, tags[i])))
338  continue;
339  t += strlen(tags[i]);
340  t += strspn(t, " \t\r\n");
341  if (*t == ':')
342  count++;
343  }
344  return count == FF_ARRAY_ELEMS(tags) ? AVPROBE_SCORE_MAX :
345  count ? AVPROBE_SCORE_EXTENSION : 0;
346 }
347 
348 static int tedcaptions_read_seek(AVFormatContext *avf, int stream_index,
349  int64_t min_ts, int64_t ts, int64_t max_ts,
350  int flags)
351 {
353  return ff_subtitles_queue_seek(&tc->subs, avf, stream_index,
354  min_ts, ts, max_ts, flags);
355 }
356 
358  .name = "tedcaptions",
359  .long_name = NULL_IF_CONFIG_SMALL("TED Talks captions"),
360  .priv_data_size = sizeof(TEDCaptionsDemuxer),
361  .priv_class = &tedcaptions_demuxer_class,
366  .read_seek2 = tedcaptions_read_seek,
367 };
static double val(void *priv, double ch)
Definition: aeval.c:76
#define av_cold
Definition: attributes.h:88
uint8_t
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
#define AV_BPRINT_SIZE_UNLIMITED
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
#define AV_BPRINT_SIZE_AUTOMATIC
#define flags(name, subs,...)
Definition: cbs_av1.c:561
#define fail()
Definition: checkasm.h:133
#define NULL
Definition: coverity.c:32
static int64_t start_time
Definition: ffplay.c:332
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
@ AV_OPT_TYPE_INT64
Definition: opt.h:226
@ AV_CODEC_ID_TEXT
raw UTF-8 text
Definition: codec_id.h:525
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
int i
Definition: input.c:407
#define av_log2
Definition: intmath.h:83
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
AVOptions.
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:279
#define AV_OPT_FLAG_SUBTITLE_PARAM
Definition: opt.h:282
#define tc
Definition: regdef.h:69
#define FF_ARRAY_ELEMS(a)
unsigned int pos
Definition: spdifenc.c:412
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
Format I/O context.
Definition: avformat.h:1232
AVIOContext * pb
I/O context.
Definition: avformat.h:1274
void * priv_data
Format private data.
Definition: avformat.h:1260
Bytestream IO Context.
Definition: avio.h:161
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int probe_packets
Number of packets to buffer for codec probing.
Definition: avformat.h:1073
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
int64_t cur_dts
Definition: avformat.h:1066
FFDemuxSubtitlesQueue subs
int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Update current_sub_idx to emulate a seek.
Definition: subtitles.c:255
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
Generic read_packet() callback for subtitles demuxers using this queue system.
Definition: subtitles.c:216
void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
Set missing durations, sort subtitles by PTS (and then byte position), and drop duplicated events.
Definition: subtitles.c:198
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:307
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge)
Insert a new subtitle event.
Definition: subtitles.c:111
#define av_log(a,...)
AVInputFormat ff_tedcaptions_demuxer
#define BETWEEN(a, amin, amax)
static int tedcaptions_read_close(AVFormatContext *avf)
static int tedcaptions_read_packet(AVFormatContext *avf, AVPacket *packet)
static void skip_spaces(AVIOContext *pb, int *cur_byte)
#define HEX_DIGIT_VAL(c)
#define ERR_CODE(c)
static const AVClass tedcaptions_demuxer_class
static void next_byte(AVIOContext *pb, int *cur_byte)
static int parse_string(AVIOContext *pb, int *cur_byte, AVBPrint *bp, int full)
static int parse_boolean(AVIOContext *pb, int *cur_byte, int *result)
static int parse_int(AVIOContext *pb, int *cur_byte, int64_t *result)
static int parse_label(AVIOContext *pb, int *cur_byte, AVBPrint *bp)
static const AVOption tedcaptions_options[]
static int expect_byte(AVIOContext *pb, int *cur_byte, uint8_t c)
static int tedcaptions_read_seek(AVFormatContext *avf, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
static av_cold int tedcaptions_read_header(AVFormatContext *avf)
static av_cold int tedcaptions_read_probe(const AVProbeData *p)
#define HEX_DIGIT_TEST(c)
static void av_bprint_utf8(AVBPrint *bp, unsigned c)
static int parse_file(AVIOContext *pb, FFDemuxSubtitlesQueue *subs)
int64_t duration
Definition: movenc.c:64
AVPacket * pkt
Definition: movenc.c:59
const char * b
Definition: vf_curves.c:118
static double c[64]