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.

138 lines
3.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. * PJS (Phoenix Japanimation Society) subtitles format demuxer
  23. *
  24. * @see http://subs.com.ru/page.php?al=pjs
  25. */
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #include "subtitles.h"
  29. typedef struct {
  30. FFDemuxSubtitlesQueue q;
  31. } PJSContext;
  32. static int pjs_probe(AVProbeData *p)
  33. {
  34. char c;
  35. int64_t start, end;
  36. const unsigned char *ptr = p->buf;
  37. if (sscanf(ptr, "%"SCNd64",%"SCNd64",%c", &start, &end, &c) == 3) {
  38. size_t q1pos = strcspn(ptr, "\"");
  39. size_t q2pos = q1pos + strcspn(ptr + q1pos + 1, "\"") + 1;
  40. if (strcspn(ptr, "\r\n") > q2pos)
  41. return AVPROBE_SCORE_MAX;
  42. }
  43. return 0;
  44. }
  45. static int64_t read_ts(char **line, int *duration)
  46. {
  47. int64_t start, end;
  48. if (sscanf(*line, "%"SCNd64",%"SCNd64, &start, &end) == 2) {
  49. *line += strcspn(*line, "\"") + 1;
  50. *duration = end - start;
  51. return start;
  52. }
  53. return AV_NOPTS_VALUE;
  54. }
  55. static int pjs_read_header(AVFormatContext *s)
  56. {
  57. PJSContext *pjs = s->priv_data;
  58. AVStream *st = avformat_new_stream(s, NULL);
  59. int res = 0;
  60. if (!st)
  61. return AVERROR(ENOMEM);
  62. avpriv_set_pts_info(st, 64, 1, 10);
  63. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  64. st->codec->codec_id = AV_CODEC_ID_PJS;
  65. while (!url_feof(s->pb)) {
  66. char line[4096];
  67. char *p = line;
  68. const int64_t pos = avio_tell(s->pb);
  69. int len = ff_get_line(s->pb, line, sizeof(line));
  70. int64_t pts_start;
  71. int duration;
  72. if (!len)
  73. break;
  74. line[strcspn(line, "\r\n")] = 0;
  75. pts_start = read_ts(&p, &duration);
  76. if (pts_start != AV_NOPTS_VALUE) {
  77. AVPacket *sub;
  78. p[strcspn(p, "\"")] = 0;
  79. sub = ff_subtitles_queue_insert(&pjs->q, p, strlen(p), 0);
  80. if (!sub)
  81. return AVERROR(ENOMEM);
  82. sub->pos = pos;
  83. sub->pts = pts_start;
  84. sub->duration = duration;
  85. }
  86. }
  87. ff_subtitles_queue_finalize(&pjs->q);
  88. return res;
  89. }
  90. static int pjs_read_packet(AVFormatContext *s, AVPacket *pkt)
  91. {
  92. PJSContext *pjs = s->priv_data;
  93. return ff_subtitles_queue_read_packet(&pjs->q, pkt);
  94. }
  95. static int pjs_read_seek(AVFormatContext *s, int stream_index,
  96. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  97. {
  98. PJSContext *pjs = s->priv_data;
  99. return ff_subtitles_queue_seek(&pjs->q, s, stream_index,
  100. min_ts, ts, max_ts, flags);
  101. }
  102. static int pjs_read_close(AVFormatContext *s)
  103. {
  104. PJSContext *pjs = s->priv_data;
  105. ff_subtitles_queue_clean(&pjs->q);
  106. return 0;
  107. }
  108. AVInputFormat ff_pjs_demuxer = {
  109. .name = "pjs",
  110. .long_name = NULL_IF_CONFIG_SMALL("PJS (Phoenix Japanimation Society) subtitles"),
  111. .priv_data_size = sizeof(PJSContext),
  112. .read_probe = pjs_probe,
  113. .read_header = pjs_read_header,
  114. .read_packet = pjs_read_packet,
  115. .read_seek2 = pjs_read_seek,
  116. .read_close = pjs_read_close,
  117. .extensions = "pjs",
  118. };