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.

188 lines
5.3KB

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