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.

175 lines
5.4KB

  1. /*
  2. * SubRip subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  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. #include "libavutil/intreadwrite.h"
  26. typedef struct {
  27. FFDemuxSubtitlesQueue q;
  28. } SRTContext;
  29. static int srt_probe(AVProbeData *p)
  30. {
  31. int v;
  32. char buf[64], *pbuf;
  33. FFTextReader tr;
  34. ff_text_init_buf(&tr, p->buf, p->buf_size);
  35. while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
  36. ff_text_r8(&tr);
  37. /* Check if the first non-empty line is a number. We do not check what the
  38. * number is because in practice it can be anything. */
  39. if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
  40. strtol(buf, &pbuf, 10) < 0 || *pbuf)
  41. return 0;
  42. /* Check if the next line matches a SRT timestamp */
  43. if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
  44. return 0;
  45. if (buf[0] >= '0' && buf[1] <= '9' && strstr(buf, " --> ")
  46. && sscanf(buf, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
  47. return AVPROBE_SCORE_MAX;
  48. return 0;
  49. }
  50. static int64_t get_pts(const char **buf, int *duration,
  51. int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2)
  52. {
  53. int i;
  54. for (i=0; i<2; i++) {
  55. int hh1, mm1, ss1, ms1;
  56. int hh2, mm2, ss2, ms2;
  57. if (sscanf(*buf, "%d:%2d:%2d%*1[,.]%3d --> %d:%2d:%2d%*1[,.]%3d"
  58. "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
  59. &hh1, &mm1, &ss1, &ms1,
  60. &hh2, &mm2, &ss2, &ms2,
  61. x1, x2, y1, y2) >= 8) {
  62. int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
  63. int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
  64. *duration = end - start;
  65. *buf += ff_subtitles_next_line(*buf);
  66. return start;
  67. }
  68. *buf += ff_subtitles_next_line(*buf);
  69. }
  70. return AV_NOPTS_VALUE;
  71. }
  72. static int srt_read_header(AVFormatContext *s)
  73. {
  74. SRTContext *srt = s->priv_data;
  75. AVBPrint buf;
  76. AVStream *st = avformat_new_stream(s, NULL);
  77. int res = 0;
  78. FFTextReader tr;
  79. ff_text_init_avio(s, &tr, s->pb);
  80. if (!st)
  81. return AVERROR(ENOMEM);
  82. avpriv_set_pts_info(st, 64, 1, 1000);
  83. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  84. st->codec->codec_id = AV_CODEC_ID_SUBRIP;
  85. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  86. while (!ff_text_eof(&tr)) {
  87. ff_subtitles_read_text_chunk(&tr, &buf);
  88. if (buf.len) {
  89. int64_t pos = ff_text_pos(&tr);
  90. int64_t pts;
  91. int duration;
  92. const char *ptr = buf.str;
  93. int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  94. AVPacket *sub;
  95. pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
  96. if (pts != AV_NOPTS_VALUE) {
  97. int len = buf.len - (ptr - buf.str);
  98. if (len <= 0)
  99. continue;
  100. sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
  101. if (!sub) {
  102. res = AVERROR(ENOMEM);
  103. goto end;
  104. }
  105. sub->pos = pos;
  106. sub->pts = pts;
  107. sub->duration = duration;
  108. if (x1 != -1) {
  109. uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
  110. if (p) {
  111. AV_WL32(p, x1);
  112. AV_WL32(p + 4, y1);
  113. AV_WL32(p + 8, x2);
  114. AV_WL32(p + 12, y2);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. ff_subtitles_queue_finalize(&srt->q);
  121. end:
  122. av_bprint_finalize(&buf, NULL);
  123. return res;
  124. }
  125. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  126. {
  127. SRTContext *srt = s->priv_data;
  128. return ff_subtitles_queue_read_packet(&srt->q, pkt);
  129. }
  130. static int srt_read_seek(AVFormatContext *s, int stream_index,
  131. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  132. {
  133. SRTContext *srt = s->priv_data;
  134. return ff_subtitles_queue_seek(&srt->q, s, stream_index,
  135. min_ts, ts, max_ts, flags);
  136. }
  137. static int srt_read_close(AVFormatContext *s)
  138. {
  139. SRTContext *srt = s->priv_data;
  140. ff_subtitles_queue_clean(&srt->q);
  141. return 0;
  142. }
  143. AVInputFormat ff_srt_demuxer = {
  144. .name = "srt",
  145. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  146. .priv_data_size = sizeof(SRTContext),
  147. .read_probe = srt_probe,
  148. .read_header = srt_read_header,
  149. .read_packet = srt_read_packet,
  150. .read_seek2 = srt_read_seek,
  151. .read_close = srt_read_close,
  152. };