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.

187 lines
5.2KB

  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. unsigned readorder;
  30. } ASSContext;
  31. static int ass_probe(AVProbeData *p)
  32. {
  33. char buf[13];
  34. FFTextReader tr;
  35. ff_text_init_buf(&tr, p->buf, p->buf_size);
  36. ff_text_read(&tr, buf, sizeof(buf));
  37. if (!memcmp(buf, "[Script Info]", 13))
  38. return AVPROBE_SCORE_MAX;
  39. return 0;
  40. }
  41. static int ass_read_close(AVFormatContext *s)
  42. {
  43. ASSContext *ass = s->priv_data;
  44. ff_subtitles_queue_clean(&ass->q);
  45. return 0;
  46. }
  47. static int read_dialogue(ASSContext *ass, AVBPrint *dst, const uint8_t *p,
  48. int64_t *start, int *duration)
  49. {
  50. int pos;
  51. int64_t end;
  52. int hh1, mm1, ss1, ms1;
  53. int hh2, mm2, ss2, ms2;
  54. if (sscanf(p, "Dialogue: %*[^,],%d:%d:%d%*c%d,%d:%d:%d%*c%d,%n",
  55. &hh1, &mm1, &ss1, &ms1,
  56. &hh2, &mm2, &ss2, &ms2, &pos) >= 8) {
  57. /* This is not part of the sscanf itself in order to handle an actual
  58. * number (which would be the Layer) or the form "Marked=N" (which is
  59. * the old SSA field, now replaced by Layer, and will be lead to Layer
  60. * being 0 here). */
  61. const int layer = atoi(p + 10);
  62. end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  63. *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  64. *duration = end - *start;
  65. av_bprint_clear(dst);
  66. av_bprintf(dst, "%u,%d,%s", ass->readorder++, layer, p + pos);
  67. /* right strip the buffer */
  68. while (dst->len > 0 &&
  69. dst->str[dst->len - 1] == '\r' ||
  70. dst->str[dst->len - 1] == '\n')
  71. dst->str[--dst->len] = 0;
  72. return 0;
  73. }
  74. return -1;
  75. }
  76. static int64_t get_line(AVBPrint *buf, FFTextReader *tr)
  77. {
  78. int64_t pos = ff_text_pos(tr);
  79. av_bprint_clear(buf);
  80. for (;;) {
  81. char c = ff_text_r8(tr);
  82. if (!c)
  83. break;
  84. av_bprint_chars(buf, c, 1);
  85. if (c == '\n')
  86. break;
  87. }
  88. return pos;
  89. }
  90. static int ass_read_header(AVFormatContext *s)
  91. {
  92. ASSContext *ass = s->priv_data;
  93. AVBPrint header, line, rline;
  94. int res = 0;
  95. AVStream *st;
  96. FFTextReader tr;
  97. ff_text_init_avio(&tr, s->pb);
  98. st = avformat_new_stream(s, NULL);
  99. if (!st)
  100. return AVERROR(ENOMEM);
  101. avpriv_set_pts_info(st, 64, 1, 100);
  102. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  103. st->codec->codec_id = AV_CODEC_ID_ASS;
  104. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  105. av_bprint_init(&line, 0, AV_BPRINT_SIZE_UNLIMITED);
  106. av_bprint_init(&rline, 0, AV_BPRINT_SIZE_UNLIMITED);
  107. for (;;) {
  108. int64_t pos = get_line(&line, &tr);
  109. int64_t ts_start = AV_NOPTS_VALUE;
  110. int duration = -1;
  111. AVPacket *sub;
  112. if (!line.str[0]) // EOF
  113. break;
  114. if (read_dialogue(ass, &rline, line.str, &ts_start, &duration) < 0) {
  115. av_bprintf(&header, "%s", line.str);
  116. continue;
  117. }
  118. sub = ff_subtitles_queue_insert(&ass->q, rline.str, rline.len, 0);
  119. if (!sub) {
  120. res = AVERROR(ENOMEM);
  121. goto end;
  122. }
  123. sub->pos = pos;
  124. sub->pts = ts_start;
  125. sub->duration = duration;
  126. }
  127. av_bprint_finalize(&line, NULL);
  128. av_bprint_finalize(&rline, NULL);
  129. res = avpriv_bprint_to_extradata(st->codec, &header);
  130. if (res < 0)
  131. goto end;
  132. ff_subtitles_queue_finalize(&ass->q);
  133. end:
  134. return res;
  135. }
  136. static int ass_read_packet(AVFormatContext *s, AVPacket *pkt)
  137. {
  138. ASSContext *ass = s->priv_data;
  139. return ff_subtitles_queue_read_packet(&ass->q, pkt);
  140. }
  141. static int ass_read_seek(AVFormatContext *s, int stream_index,
  142. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  143. {
  144. ASSContext *ass = s->priv_data;
  145. return ff_subtitles_queue_seek(&ass->q, s, stream_index,
  146. min_ts, ts, max_ts, flags);
  147. }
  148. AVInputFormat ff_ass_demuxer = {
  149. .name = "ass",
  150. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  151. .priv_data_size = sizeof(ASSContext),
  152. .read_probe = ass_probe,
  153. .read_header = ass_read_header,
  154. .read_packet = ass_read_packet,
  155. .read_close = ass_read_close,
  156. .read_seek2 = ass_read_seek,
  157. };