realtextdec.c 4.7 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. /**
  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. const unsigned char *ptr = p->buf;
  37. if (AV_RB24(ptr) == 0xEFBBBF)
  38. ptr += 3; /* skip UTF-8 BOM */
  39. return !av_strncasecmp(ptr, "<window", 7) ? AVPROBE_SCORE_MAX/2 : 0;
  40. }
  41. static int read_ts(const char *s)
  42. {
  43. int hh, mm, ss, ms;
  44. if (sscanf(s, "%u:%u:%u.%u", &hh, &mm, &ss, &ms) == 4) return (hh*3600 + mm*60 + ss) * 100 + ms;
  45. if (sscanf(s, "%u:%u:%u" , &hh, &mm, &ss ) == 3) return (hh*3600 + mm*60 + ss) * 100;
  46. if (sscanf(s, "%u:%u.%u", &mm, &ss, &ms) == 3) return ( mm*60 + ss) * 100 + ms;
  47. if (sscanf(s, "%u:%u" , &mm, &ss ) == 2) return ( mm*60 + ss) * 100;
  48. if (sscanf(s, "%u.%u", &ss, &ms) == 2) return ( ss) * 100 + ms;
  49. return strtol(s, NULL, 10) * 100;
  50. }
  51. static int realtext_read_header(AVFormatContext *s)
  52. {
  53. RealTextContext *rt = s->priv_data;
  54. AVStream *st = avformat_new_stream(s, NULL);
  55. AVBPrint buf;
  56. char c = 0;
  57. int res = 0, duration = read_ts("60"); // default duration is 60 seconds
  58. if (!st)
  59. return AVERROR(ENOMEM);
  60. avpriv_set_pts_info(st, 64, 1, 100);
  61. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  62. st->codec->codec_id = AV_CODEC_ID_REALTEXT;
  63. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  64. while (!url_feof(s->pb)) {
  65. AVPacket *sub;
  66. const int64_t pos = avio_tell(s->pb) - (c != 0);
  67. int n = ff_smil_extract_next_chunk(s->pb, &buf, &c);
  68. if (n == 0)
  69. break;
  70. if (!av_strncasecmp(buf.str, "<window", 7)) {
  71. /* save header to extradata */
  72. const char *p = ff_smil_get_attr_ptr(buf.str, "duration");
  73. if (p)
  74. duration = read_ts(p);
  75. st->codec->extradata = av_strdup(buf.str);
  76. if (!st->codec->extradata) {
  77. res = AVERROR(ENOMEM);
  78. goto end;
  79. }
  80. st->codec->extradata_size = buf.len + 1;
  81. } else {
  82. /* if we just read a <time> tag, introduce a new event, otherwise merge
  83. * with the previous one */
  84. int merge = !av_strncasecmp(buf.str, "<time", 5) ? 0 : 1;
  85. sub = ff_subtitles_queue_insert(&rt->q, buf.str, buf.len, merge);
  86. if (!sub) {
  87. res = AVERROR(ENOMEM);
  88. goto end;
  89. }
  90. if (!merge) {
  91. const char *begin = ff_smil_get_attr_ptr(buf.str, "begin");
  92. const char *end = ff_smil_get_attr_ptr(buf.str, "end");
  93. sub->pos = pos;
  94. sub->pts = begin ? read_ts(begin) : 0;
  95. sub->duration = end ? (read_ts(end) - sub->pts) : duration;
  96. }
  97. }
  98. av_bprint_clear(&buf);
  99. }
  100. ff_subtitles_queue_finalize(&rt->q);
  101. end:
  102. av_bprint_finalize(&buf, NULL);
  103. return res;
  104. }
  105. static int realtext_read_packet(AVFormatContext *s, AVPacket *pkt)
  106. {
  107. RealTextContext *rt = s->priv_data;
  108. return ff_subtitles_queue_read_packet(&rt->q, pkt);
  109. }
  110. static int realtext_read_close(AVFormatContext *s)
  111. {
  112. RealTextContext *rt = s->priv_data;
  113. ff_subtitles_queue_clean(&rt->q);
  114. return 0;
  115. }
  116. AVInputFormat ff_realtext_demuxer = {
  117. .name = "realtext",
  118. .long_name = NULL_IF_CONFIG_SMALL("RealText subtitle format"),
  119. .priv_data_size = sizeof(RealTextContext),
  120. .read_probe = realtext_probe,
  121. .read_header = realtext_read_header,
  122. .read_packet = realtext_read_packet,
  123. .read_close = realtext_read_close,
  124. .flags = AVFMT_GENERIC_INDEX,
  125. .extensions = "rt",
  126. };