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.

195 lines
6.1KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  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. * SubViewer subtitle demuxer
  23. * @see https://en.wikipedia.org/wiki/SubViewer
  24. */
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "subtitles.h"
  28. #include "libavcodec/internal.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/bprint.h"
  31. #include "libavutil/intreadwrite.h"
  32. typedef struct {
  33. FFDemuxSubtitlesQueue q;
  34. } SubViewerContext;
  35. static int subviewer_probe(AVProbeData *p)
  36. {
  37. char c;
  38. const unsigned char *ptr = p->buf;
  39. if (AV_RB24(ptr) == 0xEFBBBF)
  40. ptr += 3; /* skip UTF-8 BOM */
  41. if (sscanf(ptr, "%*u:%*u:%*u.%*u,%*u:%*u:%*u.%*u%c", &c) == 1)
  42. return AVPROBE_SCORE_EXTENSION;
  43. if (!strncmp(ptr, "[INFORMATION]", 13))
  44. return AVPROBE_SCORE_MAX/3;
  45. return 0;
  46. }
  47. static int read_ts(const char *s, int64_t *start, int *duration)
  48. {
  49. int64_t end;
  50. int hh1, mm1, ss1, ms1;
  51. int hh2, mm2, ss2, ms2;
  52. if (sscanf(s, "%u:%u:%u.%u,%u:%u:%u.%u",
  53. &hh1, &mm1, &ss1, &ms1, &hh2, &mm2, &ss2, &ms2) == 8) {
  54. end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  55. *start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  56. *duration = end - *start;
  57. return 0;
  58. }
  59. return -1;
  60. }
  61. static int subviewer_read_header(AVFormatContext *s)
  62. {
  63. SubViewerContext *subviewer = s->priv_data;
  64. AVStream *st = avformat_new_stream(s, NULL);
  65. AVBPrint header;
  66. int res = 0, new_event = 1;
  67. int64_t pts_start = AV_NOPTS_VALUE;
  68. int duration = -1;
  69. AVPacket *sub = NULL;
  70. if (!st)
  71. return AVERROR(ENOMEM);
  72. avpriv_set_pts_info(st, 64, 1, 100);
  73. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  74. st->codec->codec_id = AV_CODEC_ID_SUBVIEWER;
  75. av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED);
  76. while (!url_feof(s->pb)) {
  77. char line[2048];
  78. int64_t pos = 0;
  79. int len = ff_get_line(s->pb, line, sizeof(line));
  80. if (!len)
  81. break;
  82. line[strcspn(line, "\r\n")] = 0;
  83. if (line[0] == '[' && strncmp(line, "[br]", 4)) {
  84. /* ignore event style, XXX: add to side_data? */
  85. if (strstr(line, "[COLF]") || strstr(line, "[SIZE]") ||
  86. strstr(line, "[FONT]") || strstr(line, "[STYLE]"))
  87. continue;
  88. if (!st->codec->extradata) { // header not finalized yet
  89. av_bprintf(&header, "%s\n", line);
  90. if (!strncmp(line, "[END INFORMATION]", 17) || !strncmp(line, "[SUBTITLE]", 10)) {
  91. /* end of header */
  92. res = avpriv_bprint_to_extradata(st->codec, &header);
  93. if (res < 0)
  94. goto end;
  95. } else if (strncmp(line, "[INFORMATION]", 13)) {
  96. /* assume file metadata at this point */
  97. int i, j = 0;
  98. char key[32], value[128];
  99. for (i = 1; i < sizeof(key) - 1 && line[i] && line[i] != ']'; i++)
  100. key[i - 1] = av_tolower(line[i]);
  101. key[i - 1] = 0;
  102. if (line[i] == ']')
  103. i++;
  104. while (line[i] == ' ')
  105. i++;
  106. while (j < sizeof(value) - 1 && line[i] && line[i] != ']')
  107. value[j++] = line[i++];
  108. value[j] = 0;
  109. av_dict_set(&s->metadata, key, value, 0);
  110. }
  111. }
  112. } else if (read_ts(line, &pts_start, &duration) >= 0) {
  113. new_event = 1;
  114. pos = avio_tell(s->pb);
  115. } else if (*line) {
  116. if (!new_event) {
  117. sub = ff_subtitles_queue_insert(&subviewer->q, "\n", 1, 1);
  118. if (!sub) {
  119. res = AVERROR(ENOMEM);
  120. goto end;
  121. }
  122. }
  123. sub = ff_subtitles_queue_insert(&subviewer->q, line, strlen(line), !new_event);
  124. if (!sub) {
  125. res = AVERROR(ENOMEM);
  126. goto end;
  127. }
  128. if (new_event) {
  129. sub->pos = pos;
  130. sub->pts = pts_start;
  131. sub->duration = duration;
  132. }
  133. new_event = 0;
  134. }
  135. }
  136. ff_subtitles_queue_finalize(&subviewer->q);
  137. end:
  138. av_bprint_finalize(&header, NULL);
  139. return res;
  140. }
  141. static int subviewer_read_packet(AVFormatContext *s, AVPacket *pkt)
  142. {
  143. SubViewerContext *subviewer = s->priv_data;
  144. return ff_subtitles_queue_read_packet(&subviewer->q, pkt);
  145. }
  146. static int subviewer_read_seek(AVFormatContext *s, int stream_index,
  147. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  148. {
  149. SubViewerContext *subviewer = s->priv_data;
  150. return ff_subtitles_queue_seek(&subviewer->q, s, stream_index,
  151. min_ts, ts, max_ts, flags);
  152. }
  153. static int subviewer_read_close(AVFormatContext *s)
  154. {
  155. SubViewerContext *subviewer = s->priv_data;
  156. ff_subtitles_queue_clean(&subviewer->q);
  157. return 0;
  158. }
  159. AVInputFormat ff_subviewer_demuxer = {
  160. .name = "subviewer",
  161. .long_name = NULL_IF_CONFIG_SMALL("SubViewer subtitle format"),
  162. .priv_data_size = sizeof(SubViewerContext),
  163. .read_probe = subviewer_probe,
  164. .read_header = subviewer_read_header,
  165. .read_packet = subviewer_read_packet,
  166. .read_seek2 = subviewer_read_seek,
  167. .read_close = subviewer_read_close,
  168. .extensions = "sub",
  169. };