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.

177 lines
4.7KB

  1. /*
  2. * SSA/ASS demuxer
  3. * Copyright (c) 2008 Michael Niedermayer
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "subtitles.h"
  25. #include "libavcodec/internal.h"
  26. #include "libavutil/bprint.h"
  27. typedef struct ASSContext {
  28. FFDemuxSubtitlesQueue q;
  29. } ASSContext;
  30. static int ass_probe(AVProbeData *p)
  31. {
  32. char buf[13];
  33. FFTextReader tr;
  34. ff_text_init_buf(&tr, p->buf, p->buf_size);
  35. ff_text_read(&tr, buf, sizeof(buf));
  36. if (!memcmp(buf, "[Script Info]", 13))
  37. return AVPROBE_SCORE_MAX;
  38. return 0;
  39. }
  40. static int ass_read_close(AVFormatContext *s)
  41. {
  42. ASSContext *ass = s->priv_data;
  43. ff_subtitles_queue_clean(&ass->q);
  44. return 0;
  45. }
  46. static int read_ts(const uint8_t *p, int64_t *start, int *duration)
  47. {
  48. int64_t end;
  49. int hh1, mm1, ss1, ms1;
  50. int hh2, mm2, ss2, ms2;
  51. if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
  52. &hh1, &mm1, &ss1, &ms1,
  53. &hh2, &mm2, &ss2, &ms2) == 8) {
  54. end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  55. *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  56. *duration = end - *start;
  57. return 0;
  58. }
  59. return -1;
  60. }
  61. static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
  62. {
  63. int64_t pos = ff_text_pos(tr);
  64. av_bprint_clear(buf);
  65. for (;;) {
  66. char c = ff_text_r8(tr);
  67. if (!c)
  68. break;
  69. av_bprint_chars(buf, c, 1);
  70. if (c == '\n')
  71. break;
  72. }
  73. return pos;
  74. }
  75. static int ass_read_header(AVFormatContext *s)
  76. {
  77. ASSContext *ass = s->priv_data;
  78. AVBPrint header, line;
  79. int header_remaining, res = 0;
  80. AVStream *st;
  81. FFTextReader tr;
  82. ff_text_init_avio(&tr, s->pb);
  83. st = avformat_new_stream(s, NULL);
  84. if (!st)
  85. return AVERROR(ENOMEM);
  86. avpriv_set_pts_info(st, 64, 1, 100);
  87. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  88. st->codec->codec_id = AV_CODEC_ID_SSA;
  89. header_remaining = INT_MAX;
  90. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  91. av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
  92. for (;;) {
  93. int64_t pos = get_line(&line, &tr);
  94. if (!line.str[0]) // EOF
  95. break;
  96. if (!memcmp(line.str, "[Events]", 8))
  97. header_remaining = 2;
  98. else if (line.str[0] == '[')
  99. header_remaining = INT_MAX;
  100. if (header_remaining) {
  101. av_bprintf(&header, "%s", line.str);
  102. header_remaining--;
  103. } else {
  104. int64_t ts_start = AV_NOPTS_VALUE;
  105. int duration = -1;
  106. AVPacket *sub;
  107. if (read_ts(line.str, &ts_start, &duration) < 0)
  108. continue;
  109. sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0);
  110. if (!sub) {
  111. res = AVERROR(ENOMEM);
  112. goto end;
  113. }
  114. sub->pos = pos;
  115. sub->pts = ts_start;
  116. sub->duration = duration;
  117. }
  118. }
  119. av_bprint_finalize(&line, NULL);
  120. res = avpriv_bprint_to_extradata(st->codec, &header);
  121. if (res < 0)
  122. goto end;
  123. ff_subtitles_queue_finalize(&ass->q);
  124. end:
  125. return res;
  126. }
  127. static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
  128. {
  129. ASSContext *ass = s->priv_data;
  130. return ff_subtitles_queue_read_packet(&ass->q, pkt);
  131. }
  132. static int ass_read_seek(AVFormatContext *s, int stream_index,
  133. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  134. {
  135. ASSContext *ass = s->priv_data;
  136. return ff_subtitles_queue_seek(&ass->q, s, stream_index,
  137. min_ts, ts, max_ts, flags);
  138. }
  139. AVInputFormat ff_ass_demuxer = {
  140. .name = "ass",
  141. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  142. .priv_data_size = sizeof(ASSContext),
  143. .read_probe = ass_probe,
  144. .read_header = ass_read_header,
  145. .read_packet = ass_read_packet,
  146. .read_close = ass_read_close,
  147. .read_seek2 = ass_read_seek,
  148. };