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.

231 lines
6.2KB

  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. #include "avformat.h"
  21. #include "subtitles.h"
  22. #include "libavutil/avstring.h"
  23. AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
  24. const uint8_t *event, int len, int merge)
  25. {
  26. AVPacket *subs, *sub;
  27. if (merge && q->nb_subs > 0) {
  28. /* merge with previous event */
  29. int old_len;
  30. sub = &q->subs[q->nb_subs - 1];
  31. old_len = sub->size;
  32. if (av_grow_packet(sub, len) < 0)
  33. return NULL;
  34. memcpy(sub->data + old_len, event, len);
  35. } else {
  36. /* new event */
  37. if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
  38. return NULL;
  39. subs = av_fast_realloc(q->subs, &q->allocated_size,
  40. (q->nb_subs + 1) * sizeof(*q->subs));
  41. if (!subs)
  42. return NULL;
  43. q->subs = subs;
  44. sub = &subs[q->nb_subs++];
  45. if (av_new_packet(sub, len) < 0)
  46. return NULL;
  47. sub->destruct = 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. *pkt = *sub;
  79. pkt->dts = pkt->pts;
  80. q->current_sub_idx++;
  81. return 0;
  82. }
  83. int ff_subtitles_queue_seek(FFDemuxSubtitlesQueue *q, AVFormatContext *s, int stream_index,
  84. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  85. {
  86. if (flags & AVSEEK_FLAG_BYTE) {
  87. return AVERROR(ENOSYS);
  88. } else if (flags & AVSEEK_FLAG_FRAME) {
  89. if (ts < 0 || ts >= q->nb_subs)
  90. return AVERROR(ERANGE);
  91. q->current_sub_idx = ts;
  92. } else {
  93. int i, idx = -1;
  94. int64_t min_ts_diff = INT64_MAX;
  95. int64_t ts_selected;
  96. /* TODO: q->subs[] is sorted by pts so we could do a binary search */
  97. for (i = 0; i < q->nb_subs; i++) {
  98. int64_t pts = q->subs[i].pts;
  99. uint64_t ts_diff = FFABS(pts - ts);
  100. if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
  101. min_ts_diff = ts_diff;
  102. idx = i;
  103. }
  104. }
  105. if (idx < 0)
  106. return AVERROR(ERANGE);
  107. /* look back in the latest subtitles for overlapping subtitles */
  108. ts_selected = q->subs[idx].pts;
  109. for (i = idx - 1; i >= 0; i--) {
  110. if (q->subs[i].duration <= 0)
  111. continue;
  112. if (q->subs[i].pts > ts_selected - q->subs[i].duration)
  113. idx = i;
  114. else
  115. break;
  116. }
  117. q->current_sub_idx = idx;
  118. }
  119. return 0;
  120. }
  121. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
  122. {
  123. int i;
  124. for (i = 0; i < q->nb_subs; i++)
  125. av_destruct_packet(&q->subs[i]);
  126. av_freep(&q->subs);
  127. q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
  128. }
  129. int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
  130. {
  131. int i = 0;
  132. char end_chr;
  133. if (!*c) // cached char?
  134. *c = avio_r8(pb);
  135. if (!*c)
  136. return 0;
  137. end_chr = *c == '<' ? '>' : '<';
  138. do {
  139. av_bprint_chars(buf, *c, 1);
  140. *c = avio_r8(pb);
  141. i++;
  142. } while (*c != end_chr && *c);
  143. if (end_chr == '>') {
  144. av_bprint_chars(buf, '>', 1);
  145. *c = 0;
  146. }
  147. return i;
  148. }
  149. const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
  150. {
  151. int in_quotes = 0;
  152. const int len = strlen(attr);
  153. while (*s) {
  154. while (*s) {
  155. if (!in_quotes && isspace(*s))
  156. break;
  157. in_quotes ^= *s == '"'; // XXX: support escaping?
  158. s++;
  159. }
  160. while (isspace(*s))
  161. s++;
  162. if (!av_strncasecmp(s, attr, len) && s[len] == '=')
  163. return s + len + 1 + (s[len + 1] == '"');
  164. }
  165. return NULL;
  166. }
  167. static inline int is_eol(char c)
  168. {
  169. return c == '\r' || c == '\n';
  170. }
  171. void ff_subtitles_read_chunk(AVIOContext *pb, AVBPrint *buf)
  172. {
  173. char eol_buf[5];
  174. int n = 0, i = 0, nb_eol = 0;
  175. av_bprint_clear(buf);
  176. for (;;) {
  177. char c = avio_r8(pb);
  178. if (!c)
  179. break;
  180. /* ignore all initial line breaks */
  181. if (n == 0 && is_eol(c))
  182. continue;
  183. /* line break buffering: we don't want to add the trailing \r\n */
  184. if (is_eol(c)) {
  185. nb_eol += c == '\n';
  186. if (nb_eol == 2)
  187. break;
  188. eol_buf[i++] = c;
  189. if (i == sizeof(eol_buf) - 1)
  190. break;
  191. continue;
  192. }
  193. /* only one line break followed by data: we flush the line breaks
  194. * buffer */
  195. if (i) {
  196. eol_buf[i] = 0;
  197. av_bprintf(buf, "%s", eol_buf);
  198. i = nb_eol = 0;
  199. }
  200. av_bprint_chars(buf, c, 1);
  201. n++;
  202. }
  203. }