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.

213 lines
6.8KB

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