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.

245 lines
7.6KB

  1. /*
  2. * SubRip subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. * Copyright (c) 2015 Clément Bœsch <u pkh me>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "subtitles.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/intreadwrite.h"
  27. typedef struct {
  28. FFDemuxSubtitlesQueue q;
  29. } SRTContext;
  30. static int srt_probe(AVProbeData *p)
  31. {
  32. int v;
  33. char buf[64], *pbuf;
  34. FFTextReader tr;
  35. ff_text_init_buf(&tr, p->buf, p->buf_size);
  36. while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
  37. ff_text_r8(&tr);
  38. /* Check if the first non-empty line is a number. We do not check what the
  39. * number is because in practice it can be anything.
  40. * Also, that number can be followed by random garbage, so we can not
  41. * unfortunately check that we only have a number. */
  42. if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0 ||
  43. strtol(buf, &pbuf, 10) < 0 || pbuf == buf)
  44. return 0;
  45. /* Check if the next line matches a SRT timestamp */
  46. if (ff_subtitles_read_line(&tr, buf, sizeof(buf)) < 0)
  47. return 0;
  48. pbuf = buf;
  49. if (buf[0] == '-')
  50. pbuf++;
  51. if (pbuf[0] >= '0' && pbuf[0] <= '9' && strstr(buf, " --> ")
  52. && sscanf(buf, "%*d:%*d:%*d%*1[,.]%*d --> %*d:%*d:%*d%*1[,.]%d", &v) == 1)
  53. return AVPROBE_SCORE_MAX;
  54. return 0;
  55. }
  56. struct event_info {
  57. int32_t x1, x2, y1, y2;
  58. int duration;
  59. int64_t pts;
  60. int64_t pos;
  61. };
  62. static int get_event_info(const char *line, struct event_info *ei)
  63. {
  64. int hh1, mm1, ss1, ms1;
  65. int hh2, mm2, ss2, ms2;
  66. ei->x1 = ei->x2 = ei->y1 = ei->y2 = ei->duration = -1;
  67. ei->pts = AV_NOPTS_VALUE;
  68. ei->pos = -1;
  69. if (sscanf(line, "%d:%d:%d%*1[,.]%d --> %d:%d:%d%*1[,.]%d"
  70. "%*[ ]X1:%u X2:%u Y1:%u Y2:%u",
  71. &hh1, &mm1, &ss1, &ms1,
  72. &hh2, &mm2, &ss2, &ms2,
  73. &ei->x1, &ei->x2, &ei->y1, &ei->y2) >= 8) {
  74. const int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 1000LL + ms1;
  75. const int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 1000LL + ms2;
  76. ei->duration = end - start;
  77. ei->pts = start;
  78. return 0;
  79. }
  80. return -1;
  81. }
  82. static int add_event(FFDemuxSubtitlesQueue *q, AVBPrint *buf, char *line_cache,
  83. const struct event_info *ei, int append_cache)
  84. {
  85. if (append_cache && line_cache[0])
  86. av_bprintf(buf, "%s\n", line_cache);
  87. line_cache[0] = 0;
  88. while (buf->len > 0 && buf->str[buf->len - 1] == '\n')
  89. buf->str[--buf->len] = 0;
  90. if (buf->len) {
  91. AVPacket *sub = ff_subtitles_queue_insert(q, buf->str, buf->len, 0);
  92. if (!sub)
  93. return AVERROR(ENOMEM);
  94. av_bprint_clear(buf);
  95. sub->pos = ei->pos;
  96. sub->pts = ei->pts;
  97. sub->duration = ei->duration;
  98. if (ei->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, ei->x1);
  102. AV_WL32(p + 4, ei->y1);
  103. AV_WL32(p + 8, ei->x2);
  104. AV_WL32(p + 12, ei->y2);
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. static int srt_read_header(AVFormatContext *s)
  111. {
  112. SRTContext *srt = s->priv_data;
  113. AVBPrint buf;
  114. AVStream *st = avformat_new_stream(s, NULL);
  115. int res = 0;
  116. char line[4096], line_cache[4096];
  117. int has_event_info = 0;
  118. struct event_info ei;
  119. FFTextReader tr;
  120. ff_text_init_avio(s, &tr, s->pb);
  121. if (!st)
  122. return AVERROR(ENOMEM);
  123. avpriv_set_pts_info(st, 64, 1, 1000);
  124. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  125. st->codecpar->codec_id = AV_CODEC_ID_SUBRIP;
  126. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  127. line_cache[0] = 0;
  128. while (!ff_text_eof(&tr)) {
  129. struct event_info tmp_ei;
  130. const int64_t pos = ff_text_pos(&tr);
  131. ptrdiff_t len = ff_subtitles_read_line(&tr, line, sizeof(line));
  132. if (len < 0)
  133. break;
  134. if (!len || !line[0])
  135. continue;
  136. if (get_event_info(line, &tmp_ei) < 0) {
  137. char *pline;
  138. if (!has_event_info)
  139. continue;
  140. if (line_cache[0]) {
  141. /* We got some cache and a new line so we assume the cached
  142. * line was actually part of the payload */
  143. av_bprintf(&buf, "%s\n", line_cache);
  144. line_cache[0] = 0;
  145. }
  146. /* If the line doesn't start with a number, we assume it's part of
  147. * the payload, otherwise is likely an event number preceding the
  148. * timing information... but we can't be sure of this yet, so we
  149. * cache it */
  150. if (strtol(line, &pline, 10) < 0 || line == pline)
  151. av_bprintf(&buf, "%s\n", line);
  152. else
  153. strcpy(line_cache, line);
  154. } else {
  155. if (has_event_info) {
  156. /* We have the information of previous event, append it to the
  157. * queue. We insert the cached line if and only if the payload
  158. * is empty and the cached line is not a standalone number. */
  159. char *pline = NULL;
  160. const int standalone_number = strtol(line_cache, &pline, 10) >= 0 && pline && !*pline;
  161. res = add_event(&srt->q, &buf, line_cache, &ei, !buf.len && !standalone_number);
  162. if (res < 0)
  163. goto end;
  164. } else {
  165. has_event_info = 1;
  166. }
  167. tmp_ei.pos = pos;
  168. ei = tmp_ei;
  169. }
  170. }
  171. /* Append the last event. Here we force the cache to be flushed, because a
  172. * trailing number is more likely to be geniune (for example a copyright
  173. * date) and not the event index of an inexistant event */
  174. if (has_event_info) {
  175. res = add_event(&srt->q, &buf, line_cache, &ei, 1);
  176. if (res < 0)
  177. goto end;
  178. }
  179. ff_subtitles_queue_finalize(s, &srt->q);
  180. end:
  181. av_bprint_finalize(&buf, NULL);
  182. return res;
  183. }
  184. static int srt_read_packet(AVFormatContext *s, AVPacket *pkt)
  185. {
  186. SRTContext *srt = s->priv_data;
  187. return ff_subtitles_queue_read_packet(&srt->q, pkt);
  188. }
  189. static int srt_read_seek(AVFormatContext *s, int stream_index,
  190. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  191. {
  192. SRTContext *srt = s->priv_data;
  193. return ff_subtitles_queue_seek(&srt->q, s, stream_index,
  194. min_ts, ts, max_ts, flags);
  195. }
  196. static int srt_read_close(AVFormatContext *s)
  197. {
  198. SRTContext *srt = s->priv_data;
  199. ff_subtitles_queue_clean(&srt->q);
  200. return 0;
  201. }
  202. AVInputFormat ff_srt_demuxer = {
  203. .name = "srt",
  204. .long_name = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
  205. .priv_data_size = sizeof(SRTContext),
  206. .read_probe = srt_probe,
  207. .read_header = srt_read_header,
  208. .read_packet = srt_read_packet,
  209. .read_seek2 = srt_read_seek,
  210. .read_close = srt_read_close,
  211. };