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.

139 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, "\"");
  50. *line += !!**line;
  51. *duration = end - start;
  52. return start;
  53. }
  54. return AV_NOPTS_VALUE;
  55. }
  56. static int pjs_read_header(AVFormatContext *s)
  57. {
  58. PJSContext *pjs = s->priv_data;
  59. AVStream *st = avformat_new_stream(s, NULL);
  60. int res = 0;
  61. if (!st)
  62. return AVERROR(ENOMEM);
  63. avpriv_set_pts_info(st, 64, 1, 10);
  64. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  65. st->codec->codec_id = AV_CODEC_ID_PJS;
  66. while (!url_feof(s->pb)) {
  67. char line[4096];
  68. char *p = line;
  69. const int64_t pos = avio_tell(s->pb);
  70. int len = ff_get_line(s->pb, line, sizeof(line));
  71. int64_t pts_start;
  72. int duration;
  73. if (!len)
  74. break;
  75. line[strcspn(line, "\r\n")] = 0;
  76. pts_start = read_ts(&p, &duration);
  77. if (pts_start != AV_NOPTS_VALUE) {
  78. AVPacket *sub;
  79. p[strcspn(p, "\"")] = 0;
  80. sub = ff_subtitles_queue_insert(&pjs->q, p, strlen(p), 0);
  81. if (!sub)
  82. return AVERROR(ENOMEM);
  83. sub->pos = pos;
  84. sub->pts = pts_start;
  85. sub->duration = duration;
  86. }
  87. }
  88. ff_subtitles_queue_finalize(&pjs->q);
  89. return res;
  90. }
  91. static int pjs_read_packet(AVFormatContext *s, AVPacket *pkt)
  92. {
  93. PJSContext *pjs = s->priv_data;
  94. return ff_subtitles_queue_read_packet(&pjs->q, pkt);
  95. }
  96. static int pjs_read_seek(AVFormatContext *s, int stream_index,
  97. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  98. {
  99. PJSContext *pjs = s->priv_data;
  100. return ff_subtitles_queue_seek(&pjs->q, s, stream_index,
  101. min_ts, ts, max_ts, flags);
  102. }
  103. static int pjs_read_close(AVFormatContext *s)
  104. {
  105. PJSContext *pjs = s->priv_data;
  106. ff_subtitles_queue_clean(&pjs->q);
  107. return 0;
  108. }
  109. AVInputFormat ff_pjs_demuxer = {
  110. .name = "pjs",
  111. .long_name = NULL_IF_CONFIG_SMALL("PJS (Phoenix Japanimation Society) subtitles"),
  112. .priv_data_size = sizeof(PJSContext),
  113. .read_probe = pjs_probe,
  114. .read_header = pjs_read_header,
  115. .read_packet = pjs_read_packet,
  116. .read_seek2 = pjs_read_seek,
  117. .read_close = pjs_read_close,
  118. .extensions = "pjs",
  119. };