FFmpeg
subtitles.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2013 Clément Bœsch <u pkh me>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "avformat.h"
22 #include "subtitles.h"
23 #include "avio_internal.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/avstring.h"
26 
28 {
29  int i;
30  r->pb = pb;
31  r->buf_pos = r->buf_len = 0;
32  r->type = FF_UTF_8;
33  for (i = 0; i < 2; i++)
34  r->buf[r->buf_len++] = avio_r8(r->pb);
35  if (strncmp("\xFF\xFE", r->buf, 2) == 0) {
36  r->type = FF_UTF16LE;
37  r->buf_pos += 2;
38  } else if (strncmp("\xFE\xFF", r->buf, 2) == 0) {
39  r->type = FF_UTF16BE;
40  r->buf_pos += 2;
41  } else {
42  r->buf[r->buf_len++] = avio_r8(r->pb);
43  if (strncmp("\xEF\xBB\xBF", r->buf, 3) == 0) {
44  // UTF8
45  r->buf_pos += 3;
46  }
47  }
48  if (s && (r->type == FF_UTF16LE || r->type == FF_UTF16BE))
50  "UTF16 is automatically converted to UTF8, do not specify a character encoding\n");
51 }
52 
53 void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
54 {
55  memset(&r->buf_pb, 0, sizeof(r->buf_pb));
56  ffio_init_context(&r->buf_pb, buf, size, 0, NULL, NULL, NULL, NULL);
57  ff_text_init_avio(NULL, r, &r->buf_pb);
58 }
59 
61 {
62  return avio_tell(r->pb) - r->buf_len + r->buf_pos;
63 }
64 
66 {
67  uint32_t val;
68  uint8_t tmp;
69  if (r->buf_pos < r->buf_len)
70  return r->buf[r->buf_pos++];
71  if (r->type == FF_UTF16LE) {
72  GET_UTF16(val, avio_rl16(r->pb), return 0;)
73  } else if (r->type == FF_UTF16BE) {
74  GET_UTF16(val, avio_rb16(r->pb), return 0;)
75  } else {
76  return avio_r8(r->pb);
77  }
78  if (!val)
79  return 0;
80  r->buf_pos = 0;
81  r->buf_len = 0;
82  PUT_UTF8(val, tmp, r->buf[r->buf_len++] = tmp;)
83  return r->buf[r->buf_pos++]; // buf_len is at least 1
84 }
85 
86 void ff_text_read(FFTextReader *r, char *buf, size_t size)
87 {
88  for ( ; size > 0; size--)
89  *buf++ = ff_text_r8(r);
90 }
91 
93 {
94  return r->buf_pos >= r->buf_len && avio_feof(r->pb);
95 }
96 
98 {
99  int c;
100  if (r->buf_pos < r->buf_len)
101  return r->buf[r->buf_pos];
102  c = ff_text_r8(r);
103  if (!avio_feof(r->pb)) {
104  r->buf_pos = 0;
105  r->buf_len = 1;
106  r->buf[0] = c;
107  }
108  return c;
109 }
110 
112  const uint8_t *event, size_t len, int merge)
113 {
114  AVPacket *subs, *sub;
115 
116  if (merge && q->nb_subs > 0) {
117  /* merge with previous event */
118 
119  int old_len;
120  sub = &q->subs[q->nb_subs - 1];
121  old_len = sub->size;
122  if (av_grow_packet(sub, len) < 0)
123  return NULL;
124  memcpy(sub->data + old_len, event, len);
125  } else {
126  /* new event */
127 
128  if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
129  return NULL;
130  subs = av_fast_realloc(q->subs, &q->allocated_size,
131  (q->nb_subs + 1) * sizeof(*q->subs));
132  if (!subs)
133  return NULL;
134  q->subs = subs;
135  sub = &subs[q->nb_subs++];
136  if (av_new_packet(sub, len) < 0)
137  return NULL;
138  sub->flags |= AV_PKT_FLAG_KEY;
139  sub->pts = sub->dts = 0;
140  memcpy(sub->data, event, len);
141  }
142  return sub;
143 }
144 
145 static int cmp_pkt_sub_ts_pos(const void *a, const void *b)
146 {
147  const AVPacket *s1 = a;
148  const AVPacket *s2 = b;
149  if (s1->pts == s2->pts)
150  return FFDIFFSIGN(s1->pos, s2->pos);
151  return FFDIFFSIGN(s1->pts , s2->pts);
152 }
153 
154 static int cmp_pkt_sub_pos_ts(const void *a, const void *b)
155 {
156  const AVPacket *s1 = a;
157  const AVPacket *s2 = b;
158  if (s1->pos == s2->pos) {
159  if (s1->pts == s2->pts)
160  return 0;
161  return s1->pts > s2->pts ? 1 : -1;
162  }
163  return s1->pos > s2->pos ? 1 : -1;
164 }
165 
166 static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q)
167 {
168  int i, drop = 0;
169 
170  for (i = 1; i < q->nb_subs; i++) {
171  const int last_id = i - 1 - drop;
172  const AVPacket *last = &q->subs[last_id];
173 
174  if (q->subs[i].pts == last->pts &&
175  q->subs[i].duration == last->duration &&
176  q->subs[i].stream_index == last->stream_index &&
177  !strcmp(q->subs[i].data, last->data)) {
178 
179  av_packet_unref(&q->subs[i]);
180  drop++;
181  } else if (drop) {
182  q->subs[last_id + 1] = q->subs[i];
183  memset(&q->subs[i], 0, sizeof(q->subs[i])); // for safety
184  }
185  }
186 
187  if (drop) {
188  q->nb_subs -= drop;
189  av_log(log_ctx, AV_LOG_WARNING, "Dropping %d duplicated subtitle events\n", drop);
190  }
191 }
192 
194 {
195  int i;
196 
197  if (!q->nb_subs)
198  return;
199 
200  qsort(q->subs, q->nb_subs, sizeof(*q->subs),
203  for (i = 0; i < q->nb_subs; i++)
204  if (q->subs[i].duration < 0 && i < q->nb_subs - 1)
205  q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
206 
207  if (!q->keep_duplicates)
208  drop_dups(log_ctx, q);
209 }
210 
212 {
213  AVPacket *sub = q->subs + q->current_sub_idx;
214 
215  if (q->current_sub_idx == q->nb_subs)
216  return AVERROR_EOF;
217  if (av_packet_ref(pkt, sub) < 0) {
218  return AVERROR(ENOMEM);
219  }
220 
221  pkt->dts = pkt->pts;
222  q->current_sub_idx++;
223  return 0;
224 }
225 
226 static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts)
227 {
228  int s1 = 0, s2 = q->nb_subs - 1;
229 
230  if (s2 < s1)
231  return AVERROR(ERANGE);
232 
233  for (;;) {
234  int mid;
235 
236  if (s1 == s2)
237  return s1;
238  if (s1 == s2 - 1)
239  return q->subs[s1].pts <= q->subs[s2].pts ? s1 : s2;
240  mid = (s1 + s2) / 2;
241  if (q->subs[mid].pts <= ts)
242  s1 = mid;
243  else
244  s2 = mid;
245  }
246 }
247 
249  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
250 {
251  if (flags & AVSEEK_FLAG_BYTE) {
252  return AVERROR(ENOSYS);
253  } else if (flags & AVSEEK_FLAG_FRAME) {
254  if (ts < 0 || ts >= q->nb_subs)
255  return AVERROR(ERANGE);
256  q->current_sub_idx = ts;
257  } else {
258  int i, idx = search_sub_ts(q, ts);
259  int64_t ts_selected;
260 
261  if (idx < 0)
262  return idx;
263  for (i = idx; i < q->nb_subs && q->subs[i].pts < min_ts; i++)
264  if (stream_index == -1 || q->subs[i].stream_index == stream_index)
265  idx = i;
266  for (i = idx; i > 0 && q->subs[i].pts > max_ts; i--)
267  if (stream_index == -1 || q->subs[i].stream_index == stream_index)
268  idx = i;
269 
270  ts_selected = q->subs[idx].pts;
271  if (ts_selected < min_ts || ts_selected > max_ts)
272  return AVERROR(ERANGE);
273 
274  /* look back in the latest subtitles for overlapping subtitles */
275  for (i = idx - 1; i >= 0; i--) {
276  int64_t pts = q->subs[i].pts;
277  if (q->subs[i].duration <= 0 ||
278  (stream_index != -1 && q->subs[i].stream_index != stream_index))
279  continue;
280  if (pts >= min_ts && pts > ts_selected - q->subs[i].duration)
281  idx = i;
282  else
283  break;
284  }
285 
286  /* If the queue is used to store multiple subtitles streams (like with
287  * VobSub) and the stream index is not specified, we need to make sure
288  * to focus on the smallest file position offset for a same timestamp;
289  * queue is ordered by pts and then filepos, so we can take the first
290  * entry for a given timestamp. */
291  if (stream_index == -1)
292  while (idx > 0 && q->subs[idx - 1].pts == q->subs[idx].pts)
293  idx--;
294 
295  q->current_sub_idx = idx;
296  }
297  return 0;
298 }
299 
301 {
302  int i;
303 
304  for (i = 0; i < q->nb_subs; i++)
305  av_packet_unref(&q->subs[i]);
306  av_freep(&q->subs);
307  q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
308 }
309 
311 {
312  int i = 0;
313  char end_chr;
314 
315  if (!*c) // cached char?
316  *c = ff_text_r8(tr);
317  if (!*c)
318  return 0;
319 
320  end_chr = *c == '<' ? '>' : '<';
321  do {
322  av_bprint_chars(buf, *c, 1);
323  *c = ff_text_r8(tr);
324  i++;
325  } while (*c != end_chr && *c);
326  if (end_chr == '>') {
327  av_bprint_chars(buf, '>', 1);
328  *c = 0;
329  }
330  return i;
331 }
332 
333 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
334 {
335  int in_quotes = 0;
336  const size_t len = strlen(attr);
337 
338  while (*s) {
339  while (*s) {
340  if (!in_quotes && av_isspace(*s))
341  break;
342  in_quotes ^= *s == '"'; // XXX: support escaping?
343  s++;
344  }
345  while (av_isspace(*s))
346  s++;
347  if (!av_strncasecmp(s, attr, len) && s[len] == '=')
348  return s + len + 1 + (s[len + 1] == '"');
349  }
350  return NULL;
351 }
352 
353 static inline int is_eol(char c)
354 {
355  return c == '\r' || c == '\n';
356 }
357 
359 {
360  char eol_buf[5], last_was_cr = 0;
361  int n = 0, i = 0, nb_eol = 0;
362 
363  av_bprint_clear(buf);
364 
365  for (;;) {
366  char c = ff_text_r8(tr);
367 
368  if (!c)
369  break;
370 
371  /* ignore all initial line breaks */
372  if (n == 0 && is_eol(c))
373  continue;
374 
375  /* line break buffering: we don't want to add the trailing \r\n */
376  if (is_eol(c)) {
377  nb_eol += c == '\n' || last_was_cr;
378  if (nb_eol == 2)
379  break;
380  eol_buf[i++] = c;
381  if (i == sizeof(eol_buf) - 1)
382  break;
383  last_was_cr = c == '\r';
384  continue;
385  }
386 
387  /* only one line break followed by data: we flush the line breaks
388  * buffer */
389  if (i) {
390  eol_buf[i] = 0;
391  av_bprintf(buf, "%s", eol_buf);
392  i = nb_eol = 0;
393  }
394 
395  av_bprint_chars(buf, c, 1);
396  n++;
397  }
398 }
399 
401 {
402  FFTextReader tr;
403  tr.buf_pos = tr.buf_len = 0;
404  tr.type = 0;
405  tr.pb = pb;
407 }
408 
409 ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
410 {
411  size_t cur = 0;
412  if (!size)
413  return 0;
414  while (cur + 1 < size) {
415  unsigned char c = ff_text_r8(tr);
416  if (!c)
417  return ff_text_eof(tr) ? cur : AVERROR_INVALIDDATA;
418  if (c == '\r' || c == '\n')
419  break;
420  buf[cur++] = c;
421  buf[cur] = '\0';
422  }
423  while (ff_text_peek_r8(tr) == '\r')
424  ff_text_r8(tr);
425  if (ff_text_peek_r8(tr) == '\n')
426  ff_text_r8(tr);
427  return cur;
428 }
int keep_duplicates
set to 1 to keep duplicated subtitle events
Definition: subtitles.h:108
#define NULL
Definition: coverity.c:32
const char const char void * val
Definition: avisynth_c.h:863
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1500
static int cmp_pkt_sub_pos_ts(const void *a, const void *b)
Definition: subtitles.c:154
static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts)
Definition: subtitles.c:226
void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
Same as ff_subtitles_read_text_chunk(), but read from an AVIOContext.
Definition: subtitles.c:400
int size
Definition: avcodec.h:1481
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
Remove and destroy all the subtitles packets.
Definition: subtitles.c:300
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:223
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
int ff_text_peek_r8(FFTextReader *r)
Like ff_text_r8(), but don&#39;t remove the byte from the buffer.
Definition: subtitles.c:97
#define PUT_UTF8(val, tmp, PUT_BYTE)
Convert a 32-bit Unicode character to its UTF-8 encoded form (up to 4 bytes long).
Definition: common.h:438
int allocated_size
allocated size for subs
Definition: subtitles.h:105
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:788
Format I/O context.
Definition: avformat.h:1358
const char * ff_smil_get_attr_ptr(const char *s, const char *attr)
SMIL helper to point on the value of an attribute in the given tag.
Definition: subtitles.c:333
uint8_t
int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
Generic read_packet() callback for subtitles demuxers using this queue system.
Definition: subtitles.c:211
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1498
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVIOContext * pb
Definition: subtitles.h:42
enum sub_sort sort
sort method to use when finalizing subtitles
Definition: subtitles.h:107
uint8_t * data
Definition: avcodec.h:1480
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
#define av_log(a,...)
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:608
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1512
#define GET_UTF16(val, GET_16BIT, ERROR)
Convert a UTF-16 character (2 or 4 bytes) to its 32-bit UCS-4 encoded form.
Definition: common.h:410
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
static void drop_dups(void *log_ctx, FFDemuxSubtitlesQueue *q)
Definition: subtitles.c:166
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define s2
Definition: regdef.h:39
static int is_eol(char c)
Definition: subtitles.c:353
int ff_text_eof(FFTextReader *r)
Return non-zero if EOF was reached.
Definition: subtitles.c:92
ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
Read a line of text.
Definition: subtitles.c:409
const char * r
Definition: vf_curves.c:114
simple assert() macros that are a bit more flexible than ISO C assert().
void ff_text_init_avio(void *s, FFTextReader *r, AVIOContext *pb)
Initialize the FFTextReader from the given AVIOContext.
Definition: subtitles.c:27
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1486
unsigned char buf[8]
Definition: subtitles.h:43
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:641
#define FFDIFFSIGN(x, y)
Comparator.
Definition: common.h:92
int64_t ff_text_pos(FFTextReader *r)
Return the byte position of the next byte returned by ff_text_r8().
Definition: subtitles.c:60
AVIOContext buf_pb
Definition: subtitles.h:45
#define b
Definition: input.c:41
static int cmp_pkt_sub_ts_pos(const void *a, const void *b)
Definition: subtitles.c:145
#define s(width, name)
Definition: cbs_vp9.c:257
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:476
int n
Definition: avisynth_c.h:760
void ff_text_read(FFTextReader *r, char *buf, size_t size)
Read the given number of bytes (in UTF-8).
Definition: subtitles.c:86
int ff_text_r8(FFTextReader *r)
Return the next byte.
Definition: subtitles.c:65
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
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:248
int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c)
SMIL helper to load next chunk ("<...>" or untagged content) in buf.
Definition: subtitles.c:310
void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf)
Read a subtitles chunk from FFTextReader.
Definition: subtitles.c:358
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
void * buf
Definition: avisynth_c.h:766
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2512
#define s1
Definition: regdef.h:38
static void merge(GetBitContext *gb, uint8_t *dst, uint8_t *src, int size)
Merge two consequent lists of equal size depending on bits read.
Definition: bink.c:217
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:561
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:756
Main libavformat public API header.
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
AVPacket * subs
array of subtitles packets
Definition: subtitles.h:103
void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
Similar to ff_text_init_avio(), but sets it up to read from a bounded buffer.
Definition: subtitles.c:53
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
int current_sub_idx
current position for the read packet callback
Definition: subtitles.h:106
int len
#define AVSEEK_FLAG_FRAME
seeking based on frame number
Definition: avformat.h:2514
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1479
#define av_freep(p)
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:361
int stream_index
Definition: avcodec.h:1482
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
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
This structure stores compressed data.
Definition: avcodec.h:1457
int nb_subs
number of subtitles packets
Definition: subtitles.h:104
sort by timestamps, then position
Definition: subtitles.h:30
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:193
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1473
void av_bprint_chars(AVBPrint *buf, char c, unsigned n)
Append char c n times to a print buffer.
Definition: bprint.c:140
static uint8_t tmp[11]
Definition: aes_ctr.c:26