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.

284 lines
7.8KB

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