webvttdec.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  21. * @file
  22. * WebVTT subtitle demuxer
  23. * @see http://dev.w3.org/html5/webvtt/
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "subtitles.h"
  28. #include "libavutil/bprint.h"
  29. #include "libavutil/intreadwrite.h"
  30. typedef struct {
  31. FFDemuxSubtitlesQueue q;
  32. } WebVTTContext;
  33. static int webvtt_probe(AVProbeData *p)
  34. {
  35. const uint8_t *ptr = p->buf;
  36. if (AV_RB24(ptr) == 0xEFBBBF)
  37. ptr += 3; /* skip UTF-8 BOM */
  38. if (!strncmp(ptr, "WEBVTT", 6) &&
  39. (!ptr[6] || strchr("\n\r\t ", ptr[6])))
  40. return AVPROBE_SCORE_MAX;
  41. return 0;
  42. }
  43. static int64_t read_ts(const char *s)
  44. {
  45. int hh, mm, ss, ms;
  46. if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600 + mm*60 + ss) * 1000 + ms;
  47. if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60 + ss) * 1000 + ms;
  48. return AV_NOPTS_VALUE;
  49. }
  50. static int64_t extract_cue(AVBPrint *buf, AVIOContext *pb)
  51. {
  52. int prev_chr_is_eol = 0;
  53. int64_t pos = avio_tell(pb);
  54. av_bprint_clear(buf);
  55. for (;;) {
  56. char c = avio_r8(pb);
  57. if (!c)
  58. break;
  59. if (c == '\r' || c == '\n') {
  60. if (prev_chr_is_eol)
  61. break;
  62. prev_chr_is_eol = (c == '\n');
  63. } else
  64. prev_chr_is_eol = 0;
  65. if (c != '\r')
  66. av_bprint_chars(buf, c, 1);
  67. }
  68. av_bprint_chars(buf, '\0', 1);
  69. return pos;
  70. }
  71. static int webvtt_read_header(AVFormatContext *s)
  72. {
  73. WebVTTContext *webvtt = s->priv_data;
  74. AVBPrint header, cue;
  75. int res = 0;
  76. AVStream *st = avformat_new_stream(s, NULL);
  77. if (!st)
  78. return AVERROR(ENOMEM);
  79. avpriv_set_pts_info(st, 64, 1, 1000);
  80. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  81. st->codec->codec_id = AV_CODEC_ID_WEBVTT;
  82. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  83. av_bprint_init(&cue, 0, AV_BPRINT_SIZE_UNLIMITED);
  84. for (;;) {
  85. int i, len;
  86. int64_t pos = extract_cue(&cue, s->pb);
  87. AVPacket *sub;
  88. const char *p = cue.str;
  89. const char *identifier = p;
  90. //const char *settings = NULL;
  91. int64_t ts_start, ts_end;
  92. if (!*p) // EOF
  93. break;
  94. /* ignore header chunk */
  95. if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) ||
  96. !strncmp(p, "WEBVTT", 6))
  97. continue;
  98. /* optional cue identifier (can be a number like in SRT or some kind of
  99. * chaptering id), silently skip it */
  100. for (i = 0; p[i] && p[i] != '\n'; i++) {
  101. if (!strncmp(p + i, "-->", 3)) {
  102. identifier = NULL;
  103. break;
  104. }
  105. }
  106. if (identifier)
  107. p += strcspn(p, "\n");
  108. /* cue timestamps */
  109. if ((ts_start = read_ts(p)) == AV_NOPTS_VALUE)
  110. break;
  111. if (!(p = strstr(p, "-->")))
  112. break;
  113. p += 3;
  114. do p++; while (*p == ' ' || *p == '\t');
  115. if ((ts_end = read_ts(p)) == AV_NOPTS_VALUE)
  116. break;
  117. /* optional cue settings, TODO: store in side_data */
  118. p += strcspn(p, "\n\t ");
  119. while (*p == '\t' || *p == ' ')
  120. p++;
  121. if (*p != '\n') {
  122. //settings = p;
  123. p += strcspn(p, "\n");
  124. }
  125. if (*p == '\n')
  126. p++;
  127. /* create packet */
  128. len = cue.str + cue.len - p - 1;
  129. sub = ff_subtitles_queue_insert(&webvtt->q, p, len, 0);
  130. if (!sub) {
  131. res = AVERROR(ENOMEM);
  132. goto end;
  133. }
  134. sub->pos = pos;
  135. sub->pts = ts_start;
  136. sub->duration = ts_end - ts_start;
  137. }
  138. ff_subtitles_queue_finalize(&webvtt->q);
  139. end:
  140. av_bprint_finalize(&cue, NULL);
  141. av_bprint_finalize(&header, NULL);
  142. return res;
  143. }
  144. static int webvtt_read_packet(AVFormatContext *s, AVPacket *pkt)
  145. {
  146. WebVTTContext *webvtt = s->priv_data;
  147. return ff_subtitles_queue_read_packet(&webvtt->q, pkt);
  148. }
  149. static int webvtt_read_close(AVFormatContext *s)
  150. {
  151. WebVTTContext *webvtt = s->priv_data;
  152. ff_subtitles_queue_clean(&webvtt->q);
  153. return 0;
  154. }
  155. AVInputFormat ff_webvtt_demuxer = {
  156. .name = "webvtt",
  157. .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
  158. .priv_data_size = sizeof(WebVTTContext),
  159. .read_probe = webvtt_probe,
  160. .read_header = webvtt_read_header,
  161. .read_packet = webvtt_read_packet,
  162. .read_close = webvtt_read_close,
  163. .flags = AVFMT_GENERIC_INDEX,
  164. .extensions = "vtt",
  165. };