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.

162 lines
4.9KB

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