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.

166 lines
5.0KB

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