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.

172 lines
4.6KB

  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 "avformat.h"
  22. #include "internal.h"
  23. #include "subtitles.h"
  24. #include "libavutil/bprint.h"
  25. typedef struct ASSContext{
  26. FFDemuxSubtitlesQueue q;
  27. }ASSContext;
  28. static int ass_probe(AVProbeData *p)
  29. {
  30. const char *header= "[Script Info]";
  31. if( !memcmp(p->buf , header, strlen(header))
  32. || !memcmp(p->buf+3, header, strlen(header)))
  33. return AVPROBE_SCORE_MAX;
  34. return 0;
  35. }
  36. static int ass_read_close(AVFormatContext *s)
  37. {
  38. ASSContext *ass = s->priv_data;
  39. ff_subtitles_queue_clean(&ass->q);
  40. return 0;
  41. }
  42. static int read_ts(const uint8_t *p, int64_t *start, int *duration)
  43. {
  44. int64_t end;
  45. int hh1, mm1, ss1, ms1;
  46. int hh2, mm2, ss2, ms2;
  47. if (sscanf(p, "%*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d",
  48. &hh1, &mm1, &ss1, &ms1,
  49. &hh2, &mm2, &ss2, &ms2) == 8) {
  50. end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  51. *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  52. *duration = end - *start;
  53. return 0;
  54. }
  55. return -1;
  56. }
  57. static int64_t get_line(AVBPrint *buf, AVIOContext *pb)
  58. {
  59. int64_t pos = avio_tell(pb);
  60. av_bprint_clear(buf);
  61. for (;;) {
  62. char c = avio_r8(pb);
  63. if (!c)
  64. break;
  65. av_bprint_chars(buf, c, 1);
  66. if (c == '\n')
  67. break;
  68. }
  69. return pos;
  70. }
  71. static int ass_read_header(AVFormatContext *s)
  72. {
  73. ASSContext *ass = s->priv_data;
  74. AVBPrint header, line;
  75. int header_remaining, res = 0;
  76. AVStream *st;
  77. st = avformat_new_stream(s, NULL);
  78. if (!st)
  79. return AVERROR(ENOMEM);
  80. avpriv_set_pts_info(st, 64, 1, 100);
  81. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  82. st->codec->codec_id= AV_CODEC_ID_SSA;
  83. header_remaining= INT_MAX;
  84. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  85. av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
  86. for (;;) {
  87. int64_t pos = get_line(&line, s->pb);
  88. if (!line.str[0]) // EOF
  89. break;
  90. if (!memcmp(line.str, "[Events]", 8))
  91. header_remaining= 2;
  92. else if (line.str[0]=='[')
  93. header_remaining= INT_MAX;
  94. if (header_remaining) {
  95. av_bprintf(&header, "%s", line.str);
  96. header_remaining--;
  97. } else {
  98. int64_t ts_start = AV_NOPTS_VALUE;
  99. int duration = -1;
  100. AVPacket *sub;
  101. if (read_ts(line.str, &ts_start, &duration) < 0)
  102. continue;
  103. sub = ff_subtitles_queue_insert(&ass->q, line.str, line.len, 0);
  104. if (!sub) {
  105. res = AVERROR(ENOMEM);
  106. goto end;
  107. }
  108. sub->pos = pos;
  109. sub->pts = ts_start;
  110. sub->duration = duration;
  111. }
  112. }
  113. av_bprint_finalize(&line, NULL);
  114. av_bprint_finalize(&header, (char **)&st->codec->extradata);
  115. if (!st->codec->extradata) {
  116. res = AVERROR(ENOMEM);
  117. goto end;
  118. }
  119. st->codec->extradata_size = header.len + 1;
  120. ff_subtitles_queue_finalize(&ass->q);
  121. end:
  122. return res;
  123. }
  124. static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
  125. {
  126. ASSContext *ass = s->priv_data;
  127. return ff_subtitles_queue_read_packet(&ass->q, pkt);
  128. }
  129. static int ass_read_seek(AVFormatContext *s, int stream_index,
  130. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  131. {
  132. ASSContext *ass = s->priv_data;
  133. return ff_subtitles_queue_seek(&ass->q, s, stream_index,
  134. min_ts, ts, max_ts, flags);
  135. }
  136. AVInputFormat ff_ass_demuxer = {
  137. .name = "ass",
  138. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  139. .priv_data_size = sizeof(ASSContext),
  140. .read_probe = ass_probe,
  141. .read_header = ass_read_header,
  142. .read_packet = ass_read_packet,
  143. .read_close = ass_read_close,
  144. .read_seek2 = ass_read_seek,
  145. };