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.

201 lines
6.3KB

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