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.

129 lines
3.5KB

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