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.

215 lines
6.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. 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 inline int is_eol(char c)
  67. {
  68. return c == '\r' || c == '\n';
  69. }
  70. static void read_chunk(AVIOContext *pb, AVBPrint *buf)
  71. {
  72. char eol_buf[5];
  73. int n = 0, i = 0, nb_eol = 0;
  74. av_bprint_clear(buf);
  75. for (;;) {
  76. char c = avio_r8(pb);
  77. if (!c)
  78. break;
  79. /* ignore all initial line breaks */
  80. if (n == 0 && is_eol(c))
  81. continue;
  82. /* line break buffering: we don't want to add the trailing \r\n */
  83. if (is_eol(c)) {
  84. nb_eol += c == '\n';
  85. if (nb_eol == 2)
  86. break;
  87. eol_buf[i++] = c;
  88. if (i == sizeof(eol_buf) - 1)
  89. break;
  90. continue;
  91. }
  92. /* only one line break followed by data: we flush the line breaks
  93. * buffer */
  94. if (i) {
  95. eol_buf[i] = 0;
  96. av_bprintf(buf, "%s", eol_buf);
  97. i = nb_eol = 0;
  98. }
  99. av_bprint_chars(buf, c, 1);
  100. n++;
  101. }
  102. /* FIXME: remove the following when the lavc SubRip decoder is fixed
  103. * (trailing tags are not correctly flushed, see what happens to FATE when
  104. * you disable this code) */
  105. if (buf->len)
  106. av_bprintf(buf, "\n");
  107. }
  108. static int srt_read_header(AVFormatContext *s)
  109. {
  110. SRTContext *srt = s->priv_data;
  111. AVBPrint buf;
  112. AVStream *st = avformat_new_stream(s, NULL);
  113. int res = 0;
  114. if (!st)
  115. return AVERROR(ENOMEM);
  116. avpriv_set_pts_info(st, 64, 1, 1000);
  117. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  118. st->codec->codec_id = AV_CODEC_ID_SUBRIP;
  119. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  120. while (!url_feof(s->pb)) {
  121. read_chunk(s->pb, &buf);
  122. if (buf.len) {
  123. int64_t pos = avio_tell(s->pb);
  124. int64_t pts;
  125. int duration;
  126. const char *ptr = buf.str;
  127. int32_t x1 = -1, y1 = -1, x2 = -1, y2 = -1;
  128. AVPacket *sub;
  129. pts = get_pts(&ptr, &duration, &x1, &y1, &x2, &y2);
  130. if (pts != AV_NOPTS_VALUE) {
  131. int len = buf.len - (ptr - buf.str);
  132. sub = ff_subtitles_queue_insert(&srt->q, ptr, len, 0);
  133. if (!sub) {
  134. res = AVERROR(ENOMEM);
  135. goto end;
  136. }
  137. sub->pos = pos;
  138. sub->pts = pts;
  139. sub->duration = duration;
  140. if (x1 != -1) {
  141. uint8_t *p = av_packet_new_side_data(sub, AV_PKT_DATA_SUBTITLE_POSITION, 16);
  142. if (p) {
  143. AV_WL32(p, x1);
  144. AV_WL32(p + 4, y1);
  145. AV_WL32(p + 8, x2);
  146. AV_WL32(p + 12, y2);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. ff_subtitles_queue_finalize(&srt->q);
  153. end:
  154. av_bprint_finalize(&buf, NULL);
  155. return res;
  156. }
  157. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  158. {
  159. SRTContext *srt = s->priv_data;
  160. return ff_subtitles_queue_read_packet(&srt->q, pkt);
  161. }
  162. static int srt_read_seek(AVFormatContext *s, int stream_index,
  163. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  164. {
  165. SRTContext *srt = s->priv_data;
  166. return ff_subtitles_queue_seek(&srt->q, s, stream_index,
  167. min_ts, ts, max_ts, flags);
  168. }
  169. static int srt_read_close(AVFormatContext *s)
  170. {
  171. SRTContext *srt = s->priv_data;
  172. ff_subtitles_queue_clean(&srt->q);
  173. return 0;
  174. }
  175. AVInputFormat ff_srt_demuxer = {
  176. .name = "srt",
  177. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  178. .priv_data_size = sizeof(SRTContext),
  179. .read_probe = srt_probe,
  180. .read_header = srt_read_header,
  181. .read_packet = srt_read_packet,
  182. .read_seek2 = srt_read_seek,
  183. .read_close = srt_read_close,
  184. .flags = AVFMT_GENERIC_INDEX,
  185. };