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.

130 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. * VPlayer subtitles format demuxer
  23. */
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "subtitles.h"
  27. typedef struct {
  28. FFDemuxSubtitlesQueue q;
  29. } VPlayerContext;
  30. static int vplayer_probe(AVProbeData *p)
  31. {
  32. char c;
  33. const unsigned char *ptr = p->buf;
  34. if ((sscanf(ptr, "%*3d:%*2d:%*2d.%*2d%c", &c) == 1 ||
  35. sscanf(ptr, "%*3d:%*2d:%*2d%c", &c) == 1) && strchr(": =", c))
  36. return AVPROBE_SCORE_MAX;
  37. return 0;
  38. }
  39. static int64_t read_ts(char **line)
  40. {
  41. char c;
  42. int hh, mm, ss, ms, n, len;
  43. if (((n = sscanf(*line, "%d:%d:%d.%d%c%n", &hh, &mm, &ss, &ms, &c, &len)) >= 5 ||
  44. (n = sscanf(*line, "%d:%d:%d%c%n", &hh, &mm, &ss, &c, &len)) >= 4) && strchr(": =", c)) {
  45. *line += len;
  46. return (hh*3600LL + mm*60LL + ss) * 100LL + (n < 5 ? 0 : ms);
  47. }
  48. return AV_NOPTS_VALUE;
  49. }
  50. static int vplayer_read_header(AVFormatContext *s)
  51. {
  52. VPlayerContext *vplayer = s->priv_data;
  53. AVStream *st = avformat_new_stream(s, NULL);
  54. if (!st)
  55. return AVERROR(ENOMEM);
  56. avpriv_set_pts_info(st, 64, 1, 100);
  57. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  58. st->codecpar->codec_id = AV_CODEC_ID_VPLAYER;
  59. while (!avio_feof(s->pb)) {
  60. char line[4096];
  61. char *p = line;
  62. const int64_t pos = avio_tell(s->pb);
  63. int len = ff_get_line(s->pb, line, sizeof(line));
  64. int64_t pts_start;
  65. if (!len)
  66. break;
  67. line[strcspn(line, "\r\n")] = 0;
  68. pts_start = read_ts(&p);
  69. if (pts_start != AV_NOPTS_VALUE) {
  70. AVPacket *sub;
  71. sub = ff_subtitles_queue_insert(&vplayer->q, p, strlen(p), 0);
  72. if (!sub)
  73. return AVERROR(ENOMEM);
  74. sub->pos = pos;
  75. sub->pts = pts_start;
  76. sub->duration = -1;
  77. }
  78. }
  79. ff_subtitles_queue_finalize(s, &vplayer->q);
  80. return 0;
  81. }
  82. static int vplayer_read_packet(AVFormatContext *s, AVPacket *pkt)
  83. {
  84. VPlayerContext *vplayer = s->priv_data;
  85. return ff_subtitles_queue_read_packet(&vplayer->q, pkt);
  86. }
  87. static int vplayer_read_seek(AVFormatContext *s, int stream_index,
  88. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  89. {
  90. VPlayerContext *vplayer = s->priv_data;
  91. return ff_subtitles_queue_seek(&vplayer->q, s, stream_index,
  92. min_ts, ts, max_ts, flags);
  93. }
  94. static int vplayer_read_close(AVFormatContext *s)
  95. {
  96. VPlayerContext *vplayer = s->priv_data;
  97. ff_subtitles_queue_clean(&vplayer->q);
  98. return 0;
  99. }
  100. AVInputFormat ff_vplayer_demuxer = {
  101. .name = "vplayer",
  102. .long_name = NULL_IF_CONFIG_SMALL("VPlayer subtitles"),
  103. .priv_data_size = sizeof(VPlayerContext),
  104. .read_probe = vplayer_probe,
  105. .read_header = vplayer_read_header,
  106. .read_packet = vplayer_read_packet,
  107. .read_seek2 = vplayer_read_seek,
  108. .read_close = vplayer_read_close,
  109. .extensions = "txt",
  110. };