subtitles.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
  84. {
  85. int i;
  86. for (i = 0; i < q->nb_subs; i++)
  87. av_destruct_packet(&q->subs[i]);
  88. av_freep(&q->subs);
  89. q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
  90. }
  91. int ff_smil_extract_next_chunk(AVIOContext *pb, AVBPrint *buf, char *c)
  92. {
  93. int i = 0;
  94. char end_chr;
  95. if (!*c) // cached char?
  96. *c = avio_r8(pb);
  97. if (!*c)
  98. return 0;
  99. end_chr = *c == '<' ? '>' : '<';
  100. do {
  101. av_bprint_chars(buf, *c, 1);
  102. *c = avio_r8(pb);
  103. i++;
  104. } while (*c != end_chr && *c);
  105. if (end_chr == '>') {
  106. av_bprint_chars(buf, '>', 1);
  107. *c = 0;
  108. }
  109. return i;
  110. }
  111. const char *ff_smil_get_attr_ptr(const char *s, const char *attr)
  112. {
  113. int in_quotes = 0;
  114. const int len = strlen(attr);
  115. while (*s) {
  116. while (*s) {
  117. if (!in_quotes && isspace(*s))
  118. break;
  119. in_quotes ^= *s == '"'; // XXX: support escaping?
  120. s++;
  121. }
  122. while (isspace(*s))
  123. s++;
  124. if (!av_strncasecmp(s, attr, len) && s[len] == '=')
  125. return s + len + 1 + (s[len + 1] == '"');
  126. }
  127. return NULL;
  128. }