FFmpeg  1.2.4
subtitles.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Clément Bœsch
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 "libavutil/avstring.h"
24 
26  const uint8_t *event, int len, int merge)
27 {
28  AVPacket *subs, *sub;
29 
30  if (merge && q->nb_subs > 0) {
31  /* merge with previous event */
32 
33  int old_len;
34  sub = &q->subs[q->nb_subs - 1];
35  old_len = sub->size;
36  if (av_grow_packet(sub, len) < 0)
37  return NULL;
38  memcpy(sub->data + old_len, event, len);
39  } else {
40  /* new event */
41 
42  if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
43  return NULL;
44  subs = av_fast_realloc(q->subs, &q->allocated_size,
45  (q->nb_subs + 1) * sizeof(*q->subs));
46  if (!subs)
47  return NULL;
48  q->subs = subs;
49  sub = &subs[q->nb_subs++];
50  if (av_new_packet(sub, len) < 0)
51  return NULL;
52  sub->destruct = NULL;
53  sub->flags |= AV_PKT_FLAG_KEY;
54  sub->pts = sub->dts = 0;
55  memcpy(sub->data, event, len);
56  }
57  return sub;
58 }
59 
60 static int cmp_pkt_sub(const void *a, const void *b)
61 {
62  const AVPacket *s1 = a;
63  const AVPacket *s2 = b;
64  if (s1->pts == s2->pts) {
65  if (s1->pos == s2->pos)
66  return 0;
67  return s1->pos > s2->pos ? 1 : -1;
68  }
69  return s1->pts > s2->pts ? 1 : -1;
70 }
71 
73 {
74  int i;
75 
76  qsort(q->subs, q->nb_subs, sizeof(*q->subs), cmp_pkt_sub);
77  for (i = 0; i < q->nb_subs; i++)
78  if (q->subs[i].duration == -1 && i < q->nb_subs - 1)
79  q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
80 }
81 
83 {
84  AVPacket *sub = q->subs + q->current_sub_idx;
85 
86  if (q->current_sub_idx == q->nb_subs)
87  return AVERROR_EOF;
88  *pkt = *sub;
89  pkt->dts = pkt->pts;
90  q->current_sub_idx++;
91  return 0;
92 }
93 
95  int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
96 {
97  if (flags & AVSEEK_FLAG_BYTE) {
98  return AVERROR(ENOSYS);
99  } else if (flags & AVSEEK_FLAG_FRAME) {
100  if (ts < 0 || ts >= q->nb_subs)
101  return AVERROR(ERANGE);
102  q->current_sub_idx = ts;
103  } else {
104  int i, idx = -1;
105  int64_t min_ts_diff = INT64_MAX;
106  int64_t ts_selected;
107  /* TODO: q->subs[] is sorted by pts so we could do a binary search */
108  for (i = 0; i < q->nb_subs; i++) {
109  int64_t pts = q->subs[i].pts;
110  uint64_t ts_diff = FFABS(pts - ts);
111  if ((stream_index == -1 || q->subs[i].stream_index == stream_index) &&
112  pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
113  min_ts_diff = ts_diff;
114  idx = i;
115  }
116  }
117  if (idx < 0)
118  return AVERROR(ERANGE);
119  /* look back in the latest subtitles for overlapping subtitles */
120  ts_selected = q->subs[idx].pts;
121  for (i = idx - 1; i >= 0; i--) {
122  int64_t pts = q->subs[i].pts;
123  if (q->subs[i].duration <= 0 ||
124  (stream_index != -1 && q->subs[i].stream_index != stream_index))
125  continue;
126  if (pts >= min_ts && pts > ts_selected - q->subs[i].duration)
127  idx = i;
128  else
129  break;
130  }
131 
132  /* If the queue is used to store multiple subtitles streams (like with
133  * VobSub) and the stream index is not specified, we need to make sure
134  * to focus on the smallest file position offset for a same timestamp;
135  * queue is ordered by pts and then filepos, so we can take the first
136  * entry for a given timestamp. */
137  if (stream_index == -1)
138  while (idx > 0 && q->subs[idx - 1].pts == q->subs[idx].pts)
139  idx--;
140 
141  q->current_sub_idx = idx;
142  }
143  return 0;
144 }
145 
147 {
148  int i;
149 
150  for (i = 0; i < q->nb_subs; i++)
151  av_destruct_packet(&q->subs[i]);
152  av_freep(&q->subs);
153  q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
154 }
155 
157 {
158  int i = 0;
159  char end_chr;
160 
161  if (!*c) // cached char?
162  *c = avio_r8(pb);
163  if (!*c)
164  return 0;
165 
166  end_chr = *c == '<' ? '>' : '<';
167  do {
168  av_bprint_chars(buf, *c, 1);
169  *c = avio_r8(pb);
170  i++;
171  } while (*c != end_chr && *c);
172  if (end_chr == '>') {
173  av_bprint_chars(buf, '>', 1);
174  *c = 0;
175  }
176  return i;
177 }
178 
179 const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
180 {
181  int in_quotes = 0;
182  const int len = strlen(attr);
183 
184  while (*s) {
185  while (*s) {
186  if (!in_quotes && av_isspace(*s))
187  break;
188  in_quotes ^= *s == '"'; // XXX: support escaping?
189  s++;
190  }
191  while (av_isspace(*s))
192  s++;
193  if (!av_strncasecmp(s, attr, len) && s[len] == '=')
194  return s + len + 1 + (s[len + 1] == '"');
195  }
196  return NULL;
197 }
198 
199 static inline int is_eol(char c)
200 {
201  return c == '\r' || c == '\n';
202 }
203 
205 {
206  char eol_buf[5];
207  int n = 0, i = 0, nb_eol = 0;
208 
209  av_bprint_clear(buf);
210 
211  for (;;) {
212  char c = avio_r8(pb);
213 
214  if (!c)
215  break;
216 
217  /* ignore all initial line breaks */
218  if (n == 0 && is_eol(c))
219  continue;
220 
221  /* line break buffering: we don't want to add the trailing \r\n */
222  if (is_eol(c)) {
223  nb_eol += c == '\n';
224  if (nb_eol == 2)
225  break;
226  eol_buf[i++] = c;
227  if (i == sizeof(eol_buf) - 1)
228  break;
229  continue;
230  }
231 
232  /* only one line break followed by data: we flush the line breaks
233  * buffer */
234  if (i) {
235  eol_buf[i] = 0;
236  av_bprintf(buf, "%s", eol_buf);
237  i = nb_eol = 0;
238  }
239 
240  av_bprint_chars(buf, c, 1);
241  n++;
242  }
243 }