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.

170 lines
4.5KB

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