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.

142 lines
4.1KB

  1. /*
  2. * Copyright (c) 2014 Eejya Singh
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * STL subtitles format demuxer
  23. * @see https://documentation.apple.com/en/dvdstudiopro/usermanual/index.html#chapter=19%26section=13%26tasks=true
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "subtitles.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/avstring.h"
  30. typedef struct {
  31. FFDemuxSubtitlesQueue q;
  32. } STLContext;
  33. static int stl_probe(AVProbeData *p)
  34. {
  35. char c;
  36. const unsigned char *ptr = p->buf;
  37. if (AV_RB24(ptr) == 0xEFBBBF)
  38. ptr += 3; /* skip UTF-8 BOM */
  39. while (*ptr == '\r' || *ptr == '\n' || *ptr == '$' || !strncmp(ptr, "//" , 2))
  40. ptr += ff_subtitles_next_line(ptr);
  41. if (sscanf(ptr, "%*d:%*d:%*d:%*d , %*d:%*d:%*d:%*d , %c", &c) == 1)
  42. return AVPROBE_SCORE_MAX;
  43. return 0;
  44. }
  45. static int64_t get_pts(char **buf, int *duration)
  46. {
  47. int hh1, mm1, ss1, ms1;
  48. int hh2, mm2, ss2, ms2;
  49. int len = 0;
  50. if (sscanf(*buf, "%2d:%2d:%2d:%2d , %2d:%2d:%2d:%2d , %n",
  51. &hh1, &mm1, &ss1, &ms1,
  52. &hh2, &mm2, &ss2, &ms2, &len) >= 8 && len > 0) {
  53. int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  54. int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  55. *duration = end - start;
  56. *buf += len;
  57. return start;
  58. }
  59. return AV_NOPTS_VALUE;
  60. }
  61. static int stl_read_header(AVFormatContext *s)
  62. {
  63. STLContext *stl = s->priv_data;
  64. AVStream *st = avformat_new_stream(s, NULL);
  65. if (!st)
  66. return AVERROR(ENOMEM);
  67. avpriv_set_pts_info(st, 64, 1, 100);
  68. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  69. st->codec->codec_id = AV_CODEC_ID_STL;
  70. while (!avio_feof(s->pb)) {
  71. char line[4096];
  72. char *p = line;
  73. const int64_t pos = avio_tell(s->pb);
  74. int len = ff_get_line(s->pb, line, sizeof(line));
  75. int64_t pts_start;
  76. int duration;
  77. if (!len)
  78. break;
  79. line[strcspn(line, "\r\n")] = 0;
  80. pts_start = get_pts(&p , &duration);
  81. if (pts_start != AV_NOPTS_VALUE) {
  82. AVPacket *sub;
  83. sub = ff_subtitles_queue_insert(&stl->q, p, strlen(p), 0);
  84. if (!sub)
  85. return AVERROR(ENOMEM);
  86. sub->pos = pos;
  87. sub->pts = pts_start;
  88. sub->duration = duration;
  89. }
  90. }
  91. ff_subtitles_queue_finalize(&stl->q);
  92. return 0;
  93. }
  94. static int stl_read_packet(AVFormatContext *s, AVPacket *pkt)
  95. {
  96. STLContext *stl = s->priv_data;
  97. return ff_subtitles_queue_read_packet(&stl->q, pkt);
  98. }
  99. static int stl_read_seek(AVFormatContext *s, int stream_index,
  100. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  101. {
  102. STLContext *stl = s->priv_data;
  103. return ff_subtitles_queue_seek(&stl->q, s, stream_index,
  104. min_ts, ts, max_ts, flags);
  105. }
  106. static int stl_read_close(AVFormatContext *s)
  107. {
  108. STLContext *stl = s->priv_data;
  109. ff_subtitles_queue_clean(&stl->q);
  110. return 0;
  111. }
  112. AVInputFormat ff_stl_demuxer = {
  113. .name = "stl",
  114. .long_name = NULL_IF_CONFIG_SMALL("Spruce subtitle format"),
  115. .priv_data_size = sizeof(STLContext),
  116. .read_probe = stl_probe,
  117. .read_header = stl_read_header,
  118. .read_packet = stl_read_packet,
  119. .read_seek2 = stl_read_seek,
  120. .read_close = stl_read_close,
  121. .extensions = "stl",
  122. };