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.

157 lines
5.1KB

  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. * RealText subtitle demuxer
  23. * @see http://service.real.com/help/library/guides/ProductionGuide/prodguide/htmfiles/realtext.htm
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "subtitles.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/bprint.h"
  30. #include "libavutil/intreadwrite.h"
  31. typedef struct {
  32. FFDemuxSubtitlesQueue q;
  33. } RealTextContext;
  34. static int realtext_probe(AVProbeData *p)
  35. {
  36. char buf[7];
  37. FFTextReader tr;
  38. ff_text_init_buf(&tr, p->buf, p->buf_size);
  39. ff_text_read(&tr, buf, sizeof(buf));
  40. return !av_strncasecmp(buf, "<window", 7) ? AVPROBE_SCORE_EXTENSION : 0;
  41. }
  42. static int read_ts(const char *s)
  43. {
  44. int hh, mm, ss, ms;
  45. if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600 + mm*60 + ss) * 100 + ms;
  46. if (sscanf(s, "%u:%u:%u" , &hh, &mm, &ss ) == 3) return (hh*3600 + mm*60 + ss) * 100;
  47. if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60 + ss) * 100 + ms;
  48. if (sscanf(s, "%u:%u" , &mm, &ss ) == 2) return ( mm*60 + ss) * 100;
  49. if (sscanf(s, "%u.%u", &ss, &ms) == 2) return ( ss) * 100 + ms;
  50. return strtol(s, NULL, 10) * 100;
  51. }
  52. static int realtext_read_header(AVFormatContext *s)
  53. {
  54. RealTextContext *rt = s->priv_data;
  55. AVStream *st = avformat_new_stream(s, NULL);
  56. AVBPrint buf;
  57. char c = 0;
  58. int res = 0, duration = read_ts("60"); // default duration is 60 seconds
  59. FFTextReader tr;
  60. ff_text_init_avio(s, &tr, s->pb);
  61. if (!st)
  62. return AVERROR(ENOMEM);
  63. avpriv_set_pts_info(st, 64, 1, 100);
  64. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  65. st->codec->codec_id = AV_CODEC_ID_REALTEXT;
  66. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  67. while (!ff_text_eof(&tr)) {
  68. AVPacket *sub;
  69. const int64_t pos = ff_text_pos(&tr) - (c != 0);
  70. int n = ff_smil_extract_next_text_chunk(&tr, &buf, &c);
  71. if (n == 0)
  72. break;
  73. if (!av_strncasecmp(buf.str, "<window", 7)) {
  74. /* save header to extradata */
  75. const char *p = ff_smil_get_attr_ptr(buf.str, "duration");
  76. if (p)
  77. duration = read_ts(p);
  78. st->codec->extradata = av_strdup(buf.str);
  79. if (!st->codec->extradata) {
  80. res = AVERROR(ENOMEM);
  81. goto end;
  82. }
  83. st->codec->extradata_size = buf.len + 1;
  84. } else {
  85. /* if we just read a <time> tag, introduce a new event, otherwise merge
  86. * with the previous one */
  87. int merge = !av_strncasecmp(buf.str, "<time", 5) ? 0 : 1;
  88. sub = ff_subtitles_queue_insert(&rt->q, buf.str, buf.len, merge);
  89. if (!sub) {
  90. res = AVERROR(ENOMEM);
  91. goto end;
  92. }
  93. if (!merge) {
  94. const char *begin = ff_smil_get_attr_ptr(buf.str, "begin");
  95. const char *end = ff_smil_get_attr_ptr(buf.str, "end");
  96. sub->pos = pos;
  97. sub->pts = begin ? read_ts(begin) : 0;
  98. sub->duration = end ? (read_ts(end) - sub->pts) : duration;
  99. }
  100. }
  101. av_bprint_clear(&buf);
  102. }
  103. ff_subtitles_queue_finalize(&rt->q);
  104. end:
  105. av_bprint_finalize(&buf, NULL);
  106. return res;
  107. }
  108. static int realtext_read_packet(AVFormatContext *s, AVPacket *pkt)
  109. {
  110. RealTextContext *rt = s->priv_data;
  111. return ff_subtitles_queue_read_packet(&rt->q, pkt);
  112. }
  113. static int realtext_read_seek(AVFormatContext *s, int stream_index,
  114. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  115. {
  116. RealTextContext *rt = s->priv_data;
  117. return ff_subtitles_queue_seek(&rt->q, s, stream_index,
  118. min_ts, ts, max_ts, flags);
  119. }
  120. static int realtext_read_close(AVFormatContext *s)
  121. {
  122. RealTextContext *rt = s->priv_data;
  123. ff_subtitles_queue_clean(&rt->q);
  124. return 0;
  125. }
  126. AVInputFormat ff_realtext_demuxer = {
  127. .name = "realtext",
  128. .long_name = NULL_IF_CONFIG_SMALL("RealText subtitle format"),
  129. .priv_data_size = sizeof(RealTextContext),
  130. .read_probe = realtext_probe,
  131. .read_header = realtext_read_header,
  132. .read_packet = realtext_read_packet,
  133. .read_seek2 = realtext_read_seek,
  134. .read_close = realtext_read_close,
  135. .extensions = "rt",
  136. };