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.

193 lines
5.4KB

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