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
5.2KB

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