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.

195 lines
5.7KB

  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. if (stream_index == -1) {
  97. AVRational time_base = s->streams[0]->time_base;
  98. ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
  99. min_ts = av_rescale_rnd(min_ts, time_base.den,
  100. time_base.num * (int64_t)AV_TIME_BASE,
  101. AV_ROUND_UP);
  102. max_ts = av_rescale_rnd(max_ts, time_base.den,
  103. time_base.num * (int64_t)AV_TIME_BASE,
  104. AV_ROUND_DOWN);
  105. }
  106. /* TODO: q->subs[] is sorted by pts so we could do a binary search */
  107. for (i = 0; i < q->nb_subs; i++) {
  108. int64_t pts = q->subs[i].pts;
  109. uint64_t ts_diff = FFABS(pts - ts);
  110. if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
  111. min_ts_diff = ts_diff;
  112. idx = i;
  113. }
  114. }
  115. if (idx < 0)
  116. return AVERROR(ERANGE);
  117. /* look back in the latest subtitles for overlapping subtitles */
  118. ts_selected = q->subs[idx].pts;
  119. for (i = idx - 1; i >= 0; i--) {
  120. if (q->subs[i].duration <= 0)
  121. continue;
  122. if (q->subs[i].pts > ts_selected - q->subs[i].duration)
  123. idx = i;
  124. else
  125. break;
  126. }
  127. q->current_sub_idx = idx;
  128. }
  129. return 0;
  130. }
  131. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
  132. {
  133. int i;
  134. for (i = 0; i < q->nb_subs; i++)
  135. av_destruct_packet(&q->subs[i]);
  136. av_freep(&q->subs);
  137. q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
  138. }
  139. int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
  140. {
  141. int i = 0;
  142. char end_chr;
  143. if (!*c) // cached char?
  144. *c = avio_r8(pb);
  145. if (!*c)
  146. return 0;
  147. end_chr = *c == '<' ? '>' : '<';
  148. do {
  149. av_bprint_chars(buf, *c, 1);
  150. *c = avio_r8(pb);
  151. i++;
  152. } while (*c != end_chr && *c);
  153. if (end_chr == '>') {
  154. av_bprint_chars(buf, '>', 1);
  155. *c = 0;
  156. }
  157. return i;
  158. }
  159. const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
  160. {
  161. int in_quotes = 0;
  162. const int len = strlen(attr);
  163. while (*s) {
  164. while (*s) {
  165. if (!in_quotes && isspace(*s))
  166. break;
  167. in_quotes ^= *s == '"'; // XXX: support escaping?
  168. s++;
  169. }
  170. while (isspace(*s))
  171. s++;
  172. if (!av_strncasecmp(s, attr, len) && s[len] == '=')
  173. return s + len + 1 + (s[len + 1] == '"');
  174. }
  175. return NULL;
  176. }