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.

125 lines
3.7KB

  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 v1 subtitle demuxer
  23. */
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "subtitles.h"
  27. typedef struct {
  28. FFDemuxSubtitlesQueue q;
  29. } SubViewer1Context;
  30. static int subviewer1_probe(AVProbeData *p)
  31. {
  32. const unsigned char *ptr = p->buf;
  33. if (strstr(ptr, "******** START SCRIPT ********"))
  34. return AVPROBE_SCORE_EXTENSION;
  35. return 0;
  36. }
  37. static int subviewer1_read_header(AVFormatContext *s)
  38. {
  39. int delay = 0;
  40. AVPacket *sub = NULL;
  41. SubViewer1Context *subviewer1 = s->priv_data;
  42. AVStream *st = avformat_new_stream(s, NULL);
  43. if (!st)
  44. return AVERROR(ENOMEM);
  45. avpriv_set_pts_info(st, 64, 1, 1);
  46. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  47. st->codec->codec_id = AV_CODEC_ID_SUBVIEWER1;
  48. while (!url_feof(s->pb)) {
  49. char line[4096];
  50. int len = ff_get_line(s->pb, line, sizeof(line));
  51. int hh, mm, ss;
  52. if (!len)
  53. break;
  54. if (!strncmp(line, "[DELAY]", 7)) {
  55. ff_get_line(s->pb, line, sizeof(line));
  56. sscanf(line, "%d", &delay);
  57. }
  58. if (sscanf(line, "[%d:%d:%d]", &hh, &mm, &ss) == 3) {
  59. const int64_t pos = avio_tell(s->pb);
  60. int64_t pts_start = hh*3600LL + mm*60LL + ss + delay;
  61. len = ff_get_line(s->pb, line, sizeof(line));
  62. line[strcspn(line, "\r\n")] = 0;
  63. if (!*line) {
  64. if (sub)
  65. sub->duration = pts_start - sub->pts;
  66. } else {
  67. sub = ff_subtitles_queue_insert(&subviewer1->q, line, len, 0);
  68. if (!sub)
  69. return AVERROR(ENOMEM);
  70. sub->pos = pos;
  71. sub->pts = pts_start;
  72. sub->duration = -1;
  73. }
  74. }
  75. }
  76. ff_subtitles_queue_finalize(&subviewer1->q);
  77. return 0;
  78. }
  79. static int subviewer1_read_packet(AVFormatContext *s, AVPacket *pkt)
  80. {
  81. SubViewer1Context *subviewer1 = s->priv_data;
  82. return ff_subtitles_queue_read_packet(&subviewer1->q, pkt);
  83. }
  84. static int subviewer1_read_seek(AVFormatContext *s, int stream_index,
  85. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  86. {
  87. SubViewer1Context *subviewer1 = s->priv_data;
  88. return ff_subtitles_queue_seek(&subviewer1->q, s, stream_index,
  89. min_ts, ts, max_ts, flags);
  90. }
  91. static int subviewer1_read_close(AVFormatContext *s)
  92. {
  93. SubViewer1Context *subviewer1 = s->priv_data;
  94. ff_subtitles_queue_clean(&subviewer1->q);
  95. return 0;
  96. }
  97. AVInputFormat ff_subviewer1_demuxer = {
  98. .name = "subviewer1",
  99. .long_name = NULL_IF_CONFIG_SMALL("SubViewer v1 subtitle format"),
  100. .priv_data_size = sizeof(SubViewer1Context),
  101. .read_probe = subviewer1_probe,
  102. .read_header = subviewer1_read_header,
  103. .read_packet = subviewer1_read_packet,
  104. .read_seek2 = subviewer1_read_seek,
  105. .read_close = subviewer1_read_close,
  106. .extensions = "sub",
  107. };