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.

169 lines
5.1KB

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