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.

270 lines
7.4KB

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