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.

176 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 "libavutil/bprint.h"
  24. #include "libavutil/intreadwrite.h"
  25. static int srt_probe(AVProbeData *p)
  26. {
  27. unsigned char *ptr = p->buf;
  28. int i, v, num = 0;
  29. if (AV_RB24(ptr) == 0xEFBBBF)
  30. ptr += 3; /* skip UTF-8 BOM */
  31. for (i=0; i<2; i++) {
  32. if ((num == i || num + 1 == i)
  33. && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
  34. return AVPROBE_SCORE_MAX;
  35. num = atoi(ptr);
  36. ptr += strcspn(ptr, "\n") + 1;
  37. }
  38. return 0;
  39. }
  40. static int srt_read_header(AVFormatContext *s)
  41. {
  42. AVStream *st = avformat_new_stream(s, NULL);
  43. if (!st)
  44. return AVERROR(ENOMEM);
  45. avpriv_set_pts_info(st, 64, 1, 1000);
  46. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  47. st->codec->codec_id = AV_CODEC_ID_SUBRIP;
  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 += strcspn(*buf, "\n") + 1;
  66. return start;
  67. }
  68. *buf += strcspn(*buf, "\n") + 1;
  69. }
  70. return AV_NOPTS_VALUE;
  71. }
  72. static inline int is_eol(char c)
  73. {
  74. return c == '\r' || c == '\n';
  75. }
  76. static void read_chunk(AVIOContext *pb, AVBPrint *buf)
  77. {
  78. char eol_buf[5];
  79. int n = 0, i = 0, nb_eol = 0;
  80. for (;;) {
  81. char c = avio_r8(pb);
  82. if (!c)
  83. break;
  84. /* ignore all initial line breaks */
  85. if (n == 0 && is_eol(c))
  86. continue;
  87. /* line break buffering: we don't want to add the trailing \r\n */
  88. if (is_eol(c)) {
  89. nb_eol += c == '\n';
  90. if (nb_eol == 2)
  91. break;
  92. eol_buf[i++] = c;
  93. if (i == sizeof(eol_buf) - 1)
  94. break;
  95. continue;
  96. }
  97. /* only one line break followed by data: we flush the line breaks
  98. * buffer */
  99. if (i) {
  100. eol_buf[i] = 0;
  101. av_bprintf(buf, "%s", eol_buf);
  102. i = nb_eol = 0;
  103. }
  104. av_bprint_chars(buf, c, 1);
  105. n++;
  106. }
  107. /* FIXME: remove the following when the lavc SubRip decoder is fixed
  108. * (trailing tags are not correctly flushed, see what happens to FATE when
  109. * you disable this code) */
  110. if (buf->len)
  111. av_bprintf(buf, "\n");
  112. }
  113. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  114. {
  115. AVBPrint buf;
  116. int64_t pos = avio_tell(s->pb);
  117. int res = AVERROR_EOF;
  118. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  119. read_chunk(s->pb, &buf);
  120. if (buf.len) {
  121. int64_t pts;
  122. int duration, pkt_size;
  123. const char *ptr = buf.str;
  124. int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  125. pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
  126. pkt_size = buf.len - (ptr - buf.str);
  127. if (pts != AV_NOPTS_VALUE && !(res = av_new_packet(pkt, pkt_size))) {
  128. memcpy(pkt->data, ptr, pkt->size);
  129. pkt->flags |= AV_PKT_FLAG_KEY;
  130. pkt->pos = pos;
  131. pkt->pts = pkt->dts = pts;
  132. pkt->duration = duration;
  133. if (x1 != -1) {
  134. uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, 16);
  135. if (p) {
  136. AV_WL32(p, x1);
  137. AV_WL32(p + 4, y1);
  138. AV_WL32(p + 8, x2);
  139. AV_WL32(p + 12, y2);
  140. }
  141. }
  142. }
  143. }
  144. av_bprint_finalize(&buf, NULL);
  145. return res;
  146. }
  147. AVInputFormat ff_srt_demuxer = {
  148. .name = "srt",
  149. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  150. .read_probe = srt_probe,
  151. .read_header = srt_read_header,
  152. .read_packet = srt_read_packet,
  153. .flags = AVFMT_GENERIC_INDEX,
  154. };