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.

141 lines
4.1KB

  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. * MPlayer subtitles format demuxer
  23. */
  24. #include "avformat.h"
  25. #include "internal.h"
  26. #include "subtitles.h"
  27. typedef struct {
  28. FFDemuxSubtitlesQueue q;
  29. } MPSubContext;
  30. static int mpsub_probe(AVProbeData *p)
  31. {
  32. const char *ptr = p->buf;
  33. const char *ptr_end = p->buf + p->buf_size;
  34. while (ptr < ptr_end) {
  35. int n;
  36. if (!memcmp(ptr, "FORMAT=TIME", 11) ||
  37. sscanf(ptr, "FORMAT=%d", &n) == 1)
  38. return AVPROBE_SCORE_MAX/2;
  39. ptr += strcspn(ptr, "\n") + 1;
  40. }
  41. return 0;
  42. }
  43. static int mpsub_read_header(AVFormatContext *s)
  44. {
  45. MPSubContext *mpsub = s->priv_data;
  46. AVStream *st;
  47. AVBPrint buf;
  48. AVRational pts_info = (AVRational){ 100, 1 }; // ts based by default
  49. int res = 0;
  50. float multiplier = 100.0;
  51. float current_pts = 0;
  52. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  53. while (!url_feof(s->pb)) {
  54. char line[1024];
  55. float start, duration;
  56. int fps, len = ff_get_line(s->pb, line, sizeof(line));
  57. if (!len)
  58. break;
  59. line[strcspn(line, "\r\n")] = 0;
  60. if (sscanf(line, "FORMAT=%d", &fps) == 1 && fps > 3 && fps < 100) {
  61. /* frame based timing */
  62. pts_info = (AVRational){ fps, 1 };
  63. multiplier = 1.0;
  64. } else if (sscanf(line, "%f %f", &start, &duration) == 2) {
  65. AVPacket *sub;
  66. const int64_t pos = avio_tell(s->pb);
  67. ff_subtitles_read_chunk(s->pb, &buf);
  68. if (buf.len) {
  69. sub = ff_subtitles_queue_insert(&mpsub->q, buf.str, buf.len, 0);
  70. if (!sub) {
  71. res = AVERROR(ENOMEM);
  72. goto end;
  73. }
  74. sub->pts = (int64_t)(current_pts + start*multiplier);
  75. sub->duration = (int)(duration * multiplier);
  76. current_pts += (start + duration) * multiplier;
  77. sub->pos = pos;
  78. }
  79. }
  80. }
  81. st = avformat_new_stream(s, NULL);
  82. if (!st)
  83. return AVERROR(ENOMEM);
  84. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  85. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  86. st->codec->codec_id = AV_CODEC_ID_TEXT;
  87. ff_subtitles_queue_finalize(&mpsub->q);
  88. end:
  89. av_bprint_finalize(&buf, NULL);
  90. return res;
  91. }
  92. static int mpsub_read_packet(AVFormatContext *s, AVPacket *pkt)
  93. {
  94. MPSubContext *mpsub = s->priv_data;
  95. return ff_subtitles_queue_read_packet(&mpsub->q, pkt);
  96. }
  97. static int mpsub_read_seek(AVFormatContext *s, int stream_index,
  98. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  99. {
  100. MPSubContext *mpsub = s->priv_data;
  101. return ff_subtitles_queue_seek(&mpsub->q, s, stream_index,
  102. min_ts, ts, max_ts, flags);
  103. }
  104. static int mpsub_read_close(AVFormatContext *s)
  105. {
  106. MPSubContext *mpsub = s->priv_data;
  107. ff_subtitles_queue_clean(&mpsub->q);
  108. return 0;
  109. }
  110. AVInputFormat ff_mpsub_demuxer = {
  111. .name = "mpsub",
  112. .long_name = NULL_IF_CONFIG_SMALL("MPlayer subtitles"),
  113. .priv_data_size = sizeof(MPSubContext),
  114. .read_probe = mpsub_probe,
  115. .read_header = mpsub_read_header,
  116. .read_packet = mpsub_read_packet,
  117. .read_seek2 = mpsub_read_seek,
  118. .read_close = mpsub_read_close,
  119. .extensions = "sub",
  120. };