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.

131 lines
4.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 "libavutil/intreadwrite.h"
  24. static int srt_probe(AVProbeData *p)
  25. {
  26. unsigned char *ptr = p->buf;
  27. int i, v, num = 0;
  28. if (AV_RB24(ptr) == 0xEFBBBF)
  29. ptr += 3; /* skip UTF-8 BOM */
  30. for (i=0; i<2; i++) {
  31. if (num == i && sscanf(ptr, "%*d:%*2d:%*2d%*1[,.]%*3d --> %*d:%*2d:%*2d%*1[,.]%3d", &v) == 1)
  32. return AVPROBE_SCORE_MAX;
  33. num = atoi(ptr);
  34. ptr += strcspn(ptr, "\n") + 1;
  35. }
  36. return 0;
  37. }
  38. static int srt_read_header(AVFormatContext *s)
  39. {
  40. AVStream *st = avformat_new_stream(s, NULL);
  41. if (!st)
  42. return AVERROR(ENOMEM);
  43. avpriv_set_pts_info(st, 64, 1, 1000);
  44. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  45. st->codec->codec_id = AV_CODEC_ID_SUBRIP;
  46. return 0;
  47. }
  48. static int64_t get_pts(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 += strcspn(*buf, "\n") + 1;
  64. return start;
  65. }
  66. *buf += strcspn(*buf, "\n") + 1;
  67. }
  68. return AV_NOPTS_VALUE;
  69. }
  70. static inline int is_eol(char c)
  71. {
  72. return c == '\r' || c == '\n';
  73. }
  74. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  75. {
  76. char buffer[2048], *ptr = buffer, *ptr2;
  77. int64_t pos = avio_tell(s->pb);
  78. int res = AVERROR_EOF;
  79. do {
  80. ptr2 = ptr;
  81. ptr += ff_get_line(s->pb, ptr, sizeof(buffer)+buffer-ptr);
  82. } while (!is_eol(*ptr2) && !url_feof(s->pb) && ptr-buffer<sizeof(buffer)-1);
  83. if (buffer[0]) {
  84. int64_t pts;
  85. int duration;
  86. const char *end = ptr;
  87. int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  88. ptr = buffer;
  89. pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
  90. if (pts != AV_NOPTS_VALUE &&
  91. !(res = av_new_packet(pkt, end - ptr))) {
  92. memcpy(pkt->data, ptr, pkt->size);
  93. pkt->flags |= AV_PKT_FLAG_KEY;
  94. pkt->pos = pos;
  95. pkt->pts = pkt->dts = pts;
  96. pkt->duration = duration;
  97. if (x1 != -1) {
  98. uint8_t *p = av_packet_new_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, 16);
  99. if (p) {
  100. AV_WL32(p, x1);
  101. AV_WL32(p + 4, y1);
  102. AV_WL32(p + 8, x2);
  103. AV_WL32(p + 12, y2);
  104. }
  105. }
  106. }
  107. }
  108. return res;
  109. }
  110. AVInputFormat ff_srt_demuxer = {
  111. .name = "srt",
  112. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  113. .read_probe = srt_probe,
  114. .read_header = srt_read_header,
  115. .read_packet = srt_read_packet,
  116. .flags = AVFMT_GENERIC_INDEX,
  117. };