You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

396 lines
10KB

  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. #include "avformat.h"
  21. #include "subtitles.h"
  22. #include "avio_internal.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. void ff_text_init_avio(FFTextReader *r, AVIOContext *pb)
  26. {
  27. int i;
  28. r->pb = pb;
  29. r->buf_pos = r->buf_len = 0;
  30. r->type = FF_UTF_8;
  31. for (i = 0; i < 2; i++)
  32. r->buf[r->buf_len++] = avio_r8(r->pb);
  33. if (strncmp("\xFF\xFE", r->buf, 2) == 0) {
  34. r->type = FF_UTF16LE;
  35. r->buf_pos += 2;
  36. } else if (strncmp("\xFE\xFF", r->buf, 2) == 0) {
  37. r->type = FF_UTF16BE;
  38. r->buf_pos += 2;
  39. } else {
  40. r->buf[r->buf_len++] = avio_r8(r->pb);
  41. if (strncmp("\xEF\xBB\xBF", r->buf, 3) == 0) {
  42. // UTF8
  43. r->buf_pos += 3;
  44. }
  45. }
  46. }
  47. void ff_text_init_buf(FFTextReader *r, void *buf, size_t size)
  48. {
  49. memset(&r->buf_pb, 0, sizeof(r->buf_pb));
  50. ffio_init_context(&r->buf_pb, buf, size, 0, NULL, NULL, NULL, NULL);
  51. ff_text_init_avio(r, &r->buf_pb);
  52. }
  53. int64_t ff_text_pos(FFTextReader *r)
  54. {
  55. return avio_tell(r->pb) - r->buf_len + r->buf_pos;
  56. }
  57. int ff_text_r8(FFTextReader *r)
  58. {
  59. uint32_t val;
  60. uint8_t tmp;
  61. if (r->buf_pos < r->buf_len)
  62. return r->buf[r->buf_pos++];
  63. if (r->type == FF_UTF16LE) {
  64. GET_UTF16(val, avio_rl16(r->pb), return 0;)
  65. } else if (r->type == FF_UTF16BE) {
  66. GET_UTF16(val, avio_rb16(r->pb), return 0;)
  67. } else {
  68. return avio_r8(r->pb);
  69. }
  70. if (!val)
  71. return 0;
  72. r->buf_pos = 0;
  73. r->buf_len = 0;
  74. PUT_UTF8(val, tmp, r->buf[r->buf_len++] = tmp;)
  75. return r->buf[r->buf_pos++]; // buf_len is at least 1
  76. }
  77. void ff_text_read(FFTextReader *r, char *buf, size_t size)
  78. {
  79. for ( ; size > 0; size--)
  80. *buf++ = ff_text_r8(r);
  81. }
  82. int ff_text_eof(FFTextReader *r)
  83. {
  84. return r->buf_pos >= r->buf_len && avio_feof(r->pb);
  85. }
  86. int ff_text_peek_r8(FFTextReader *r)
  87. {
  88. int c;
  89. if (r->buf_pos < r->buf_len)
  90. return r->buf[r->buf_pos];
  91. c = ff_text_r8(r);
  92. if (!avio_feof(r->pb)) {
  93. r->buf_pos = 0;
  94. r->buf_len = 1;
  95. r->buf[0] = c;
  96. }
  97. return c;
  98. }
  99. AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
  100. const uint8_t *event, int len, int merge)
  101. {
  102. AVPacket *subs, *sub;
  103. if (merge && q->nb_subs > 0) {
  104. /* merge with previous event */
  105. int old_len;
  106. sub = &q->subs[q->nb_subs - 1];
  107. old_len = sub->size;
  108. if (av_grow_packet(sub, len) < 0)
  109. return NULL;
  110. memcpy(sub->data + old_len, event, len);
  111. } else {
  112. /* new event */
  113. if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
  114. return NULL;
  115. subs = av_fast_realloc(q->subs, &q->allocated_size,
  116. (q->nb_subs + 1) * sizeof(*q->subs));
  117. if (!subs)
  118. return NULL;
  119. q->subs = subs;
  120. sub = &subs[q->nb_subs++];
  121. if (av_new_packet(sub, len) < 0)
  122. return NULL;
  123. sub->flags |= AV_PKT_FLAG_KEY;
  124. sub->pts = sub->dts = 0;
  125. memcpy(sub->data, event, len);
  126. }
  127. return sub;
  128. }
  129. static int cmp_pkt_sub_ts_pos(const void *a, const void *b)
  130. {
  131. const AVPacket *s1 = a;
  132. const AVPacket *s2 = b;
  133. if (s1->pts == s2->pts) {
  134. if (s1->pos == s2->pos)
  135. return 0;
  136. return s1->pos > s2->pos ? 1 : -1;
  137. }
  138. return s1->pts > s2->pts ? 1 : -1;
  139. }
  140. static int cmp_pkt_sub_pos_ts(const void *a, const void *b)
  141. {
  142. const AVPacket *s1 = a;
  143. const AVPacket *s2 = b;
  144. if (s1->pos == s2->pos) {
  145. if (s1->pts == s2->pts)
  146. return 0;
  147. return s1->pts > s2->pts ? 1 : -1;
  148. }
  149. return s1->pos > s2->pos ? 1 : -1;
  150. }
  151. void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
  152. {
  153. int i;
  154. qsort(q->subs, q->nb_subs, sizeof(*q->subs),
  155. q->sort == SUB_SORT_TS_POS ? cmp_pkt_sub_ts_pos
  156. : cmp_pkt_sub_pos_ts);
  157. for (i = 0; i < q->nb_subs; i++)
  158. if (q->subs[i].duration == -1 && i < q->nb_subs - 1)
  159. q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
  160. }
  161. int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
  162. {
  163. AVPacket *sub = q->subs + q->current_sub_idx;
  164. if (q->current_sub_idx == q->nb_subs)
  165. return AVERROR_EOF;
  166. if (av_copy_packet(pkt, sub) < 0) {
  167. return AVERROR(ENOMEM);
  168. }
  169. pkt->dts = pkt->pts;
  170. q->current_sub_idx++;
  171. return 0;
  172. }
  173. static int search_sub_ts(const FFDemuxSubtitlesQueue *q, int64_t ts)
  174. {
  175. int s1 = 0, s2 = q->nb_subs - 1;
  176. if (s2 < s1)
  177. return AVERROR(ERANGE);
  178. for (;;) {
  179. int mid;
  180. if (s1 == s2)
  181. return s1;
  182. if (s1 == s2 - 1)
  183. return q->subs[s1].pts <= q->subs[s2].pts ? s1 : s2;
  184. mid = (s1 + s2) / 2;
  185. if (q->subs[mid].pts <= ts)
  186. s1 = mid;
  187. else
  188. s2 = mid;
  189. }
  190. }
  191. int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
  192. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  193. {
  194. if (flags & AVSEEK_FLAG_BYTE) {
  195. return AVERROR(ENOSYS);
  196. } else if (flags & AVSEEK_FLAG_FRAME) {
  197. if (ts < 0 || ts >= q->nb_subs)
  198. return AVERROR(ERANGE);
  199. q->current_sub_idx = ts;
  200. } else {
  201. int i, idx = search_sub_ts(q, ts);
  202. int64_t ts_selected;
  203. if (idx < 0)
  204. return idx;
  205. for (i = idx; i < q->nb_subs && q->subs[i].pts < min_ts; i++)
  206. if (stream_index == -1 || q->subs[i].stream_index == stream_index)
  207. idx = i;
  208. for (i = idx; i > 0 && q->subs[i].pts > max_ts; i--)
  209. if (stream_index == -1 || q->subs[i].stream_index == stream_index)
  210. idx = i;
  211. ts_selected = q->subs[idx].pts;
  212. if (ts_selected < min_ts || ts_selected > max_ts)
  213. return AVERROR(ERANGE);
  214. /* look back in the latest subtitles for overlapping subtitles */
  215. for (i = idx - 1; i >= 0; i--) {
  216. int64_t pts = q->subs[i].pts;
  217. if (q->subs[i].duration <= 0 ||
  218. (stream_index != -1 && q->subs[i].stream_index != stream_index))
  219. continue;
  220. if (pts >= min_ts && pts > ts_selected - q->subs[i].duration)
  221. idx = i;
  222. else
  223. break;
  224. }
  225. /* If the queue is used to store multiple subtitles streams (like with
  226. * VobSub) and the stream index is not specified, we need to make sure
  227. * to focus on the smallest file position offset for a same timestamp;
  228. * queue is ordered by pts and then filepos, so we can take the first
  229. * entry for a given timestamp. */
  230. if (stream_index == -1)
  231. while (idx > 0 && q->subs[idx - 1].pts == q->subs[idx].pts)
  232. idx--;
  233. q->current_sub_idx = idx;
  234. }
  235. return 0;
  236. }
  237. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
  238. {
  239. int i;
  240. for (i = 0; i < q->nb_subs; i++)
  241. av_free_packet(&q->subs[i]);
  242. av_freep(&q->subs);
  243. q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
  244. }
  245. int ff_smil_extract_next_text_chunk(FFTextReader *tr, AVBPrint *buf, char *c)
  246. {
  247. int i = 0;
  248. char end_chr;
  249. if (!*c) // cached char?
  250. *c = ff_text_r8(tr);
  251. if (!*c)
  252. return 0;
  253. end_chr = *c == '<' ? '>' : '<';
  254. do {
  255. av_bprint_chars(buf, *c, 1);
  256. *c = ff_text_r8(tr);
  257. i++;
  258. } while (*c != end_chr && *c);
  259. if (end_chr == '>') {
  260. av_bprint_chars(buf, '>', 1);
  261. *c = 0;
  262. }
  263. return i;
  264. }
  265. const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
  266. {
  267. int in_quotes = 0;
  268. const int len = strlen(attr);
  269. while (*s) {
  270. while (*s) {
  271. if (!in_quotes && av_isspace(*s))
  272. break;
  273. in_quotes ^= *s == '"'; // XXX: support escaping?
  274. s++;
  275. }
  276. while (av_isspace(*s))
  277. s++;
  278. if (!av_strncasecmp(s, attr, len) && s[len] == '=')
  279. return s + len + 1 + (s[len + 1] == '"');
  280. }
  281. return NULL;
  282. }
  283. static inline int is_eol(char c)
  284. {
  285. return c == '\r' || c == '\n';
  286. }
  287. void ff_subtitles_read_text_chunk(FFTextReader *tr, AVBPrint *buf)
  288. {
  289. char eol_buf[5], last_was_cr = 0;
  290. int n = 0, i = 0, nb_eol = 0;
  291. av_bprint_clear(buf);
  292. for (;;) {
  293. char c = ff_text_r8(tr);
  294. if (!c)
  295. break;
  296. /* ignore all initial line breaks */
  297. if (n == 0 && is_eol(c))
  298. continue;
  299. /* line break buffering: we don't want to add the trailing \r\n */
  300. if (is_eol(c)) {
  301. nb_eol += c == '\n' || last_was_cr;
  302. if (nb_eol == 2)
  303. break;
  304. eol_buf[i++] = c;
  305. if (i == sizeof(eol_buf) - 1)
  306. break;
  307. last_was_cr = c == '\r';
  308. continue;
  309. }
  310. /* only one line break followed by data: we flush the line breaks
  311. * buffer */
  312. if (i) {
  313. eol_buf[i] = 0;
  314. av_bprintf(buf, "%s", eol_buf);
  315. i = nb_eol = 0;
  316. }
  317. av_bprint_chars(buf, c, 1);
  318. n++;
  319. }
  320. }
  321. void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
  322. {
  323. FFTextReader tr;
  324. tr.buf_pos = tr.buf_len = 0;
  325. tr.type = 0;
  326. tr.pb = pb;
  327. ff_subtitles_read_text_chunk(&tr, buf);
  328. }
  329. ptrdiff_t ff_subtitles_read_line(FFTextReader *tr, char *buf, size_t size)
  330. {
  331. size_t cur = 0;
  332. if (!size)
  333. return 0;
  334. while (cur + 1 < size) {
  335. unsigned char c = ff_text_r8(tr);
  336. if (!c)
  337. return ff_text_eof(tr) ? cur : AVERROR_INVALIDDATA;
  338. if (c == '\r' || c == '\n')
  339. break;
  340. buf[cur++] = c;
  341. buf[cur] = '\0';
  342. }
  343. if (ff_text_peek_r8(tr) == '\r')
  344. ff_text_r8(tr);
  345. if (ff_text_peek_r8(tr) == '\n')
  346. ff_text_r8(tr);
  347. return cur;
  348. }