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.

205 lines
6.3KB

  1. /*
  2. * MicroDVD subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. * Copyright (c) 2012 Clément Bœsch <u pkh me>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "subtitles.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/opt.h"
  27. #define MAX_LINESIZE 2048
  28. typedef struct {
  29. const AVClass *class;
  30. FFDemuxSubtitlesQueue q;
  31. AVRational frame_rate;
  32. } MicroDVDContext;
  33. static int microdvd_probe(AVProbeData *p)
  34. {
  35. unsigned char c;
  36. const uint8_t *ptr = p->buf;
  37. int i;
  38. if (AV_RB24(ptr) == 0xEFBBBF)
  39. ptr += 3; /* skip UTF-8 BOM */
  40. for (i=0; i<3; i++) {
  41. if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
  42. sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
  43. sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
  44. return 0;
  45. ptr += ff_subtitles_next_line(ptr);
  46. }
  47. return AVPROBE_SCORE_MAX;
  48. }
  49. static int64_t get_pts(const char *buf)
  50. {
  51. int frame;
  52. char c;
  53. if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
  54. return frame;
  55. return AV_NOPTS_VALUE;
  56. }
  57. static int get_duration(const char *buf)
  58. {
  59. int frame_start, frame_end;
  60. if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
  61. return frame_end - frame_start;
  62. return -1;
  63. }
  64. static const char *bom = "\xEF\xBB\xBF";
  65. static int microdvd_read_header(AVFormatContext *s)
  66. {
  67. AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
  68. MicroDVDContext *microdvd = s->priv_data;
  69. AVStream *st = avformat_new_stream(s, NULL);
  70. int i = 0;
  71. char line_buf[MAX_LINESIZE];
  72. int has_real_fps = 0;
  73. if (!st)
  74. return AVERROR(ENOMEM);
  75. while (!avio_feof(s->pb)) {
  76. char *p;
  77. AVPacket *sub;
  78. int64_t pos = avio_tell(s->pb);
  79. int len = ff_get_line(s->pb, line_buf, sizeof(line_buf));
  80. char *line = line_buf;
  81. if (!strncmp(line, bom, 3))
  82. line += 3;
  83. p = line;
  84. if (!len)
  85. break;
  86. line[strcspn(line, "\r\n")] = 0;
  87. if (i++ < 3) {
  88. int frame;
  89. double fps;
  90. char c;
  91. if ((sscanf(line, "{%d}{}%6lf", &frame, &fps) == 2 ||
  92. sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
  93. && frame <= 1 && fps > 3 && fps < 100) {
  94. pts_info = av_d2q(fps, 100000);
  95. has_real_fps = 1;
  96. continue;
  97. }
  98. if (!st->codec->extradata && sscanf(line, "{DEFAULT}{}%c", &c) == 1) {
  99. st->codec->extradata = av_strdup(line + 11);
  100. if (!st->codec->extradata)
  101. return AVERROR(ENOMEM);
  102. st->codec->extradata_size = strlen(st->codec->extradata) + 1;
  103. continue;
  104. }
  105. }
  106. #define SKIP_FRAME_ID \
  107. p = strchr(p, '}'); \
  108. if (!p) { \
  109. av_log(s, AV_LOG_WARNING, "Invalid event \"%s\"" \
  110. " at line %d\n", line, i); \
  111. continue; \
  112. } \
  113. p++
  114. SKIP_FRAME_ID;
  115. SKIP_FRAME_ID;
  116. if (!*p)
  117. continue;
  118. sub = ff_subtitles_queue_insert(&microdvd->q, p, strlen(p), 0);
  119. if (!sub)
  120. return AVERROR(ENOMEM);
  121. sub->pos = pos;
  122. sub->pts = get_pts(line);
  123. sub->duration = get_duration(line);
  124. }
  125. ff_subtitles_queue_finalize(&microdvd->q);
  126. if (has_real_fps) {
  127. /* export the FPS info only if set in the file */
  128. microdvd->frame_rate = pts_info;
  129. } else if (microdvd->frame_rate.num) {
  130. /* fallback on user specified frame rate */
  131. pts_info = microdvd->frame_rate;
  132. }
  133. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  134. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  135. st->codec->codec_id = AV_CODEC_ID_MICRODVD;
  136. return 0;
  137. }
  138. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  139. {
  140. MicroDVDContext *microdvd = s->priv_data;
  141. return ff_subtitles_queue_read_packet(&microdvd->q, pkt);
  142. }
  143. static int microdvd_read_seek(AVFormatContext *s, int stream_index,
  144. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  145. {
  146. MicroDVDContext *microdvd = s->priv_data;
  147. return ff_subtitles_queue_seek(&microdvd->q, s, stream_index,
  148. min_ts, ts, max_ts, flags);
  149. }
  150. static int microdvd_read_close(AVFormatContext *s)
  151. {
  152. MicroDVDContext *microdvd = s->priv_data;
  153. ff_subtitles_queue_clean(&microdvd->q);
  154. return 0;
  155. }
  156. #define OFFSET(x) offsetof(MicroDVDContext, x)
  157. #define SD AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_DECODING_PARAM
  158. static const AVOption microdvd_options[] = {
  159. { "subfps", "set the movie frame rate fallback", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, SD },
  160. { NULL }
  161. };
  162. static const AVClass microdvd_class = {
  163. .class_name = "microdvddec",
  164. .item_name = av_default_item_name,
  165. .option = microdvd_options,
  166. .version = LIBAVUTIL_VERSION_INT,
  167. };
  168. AVInputFormat ff_microdvd_demuxer = {
  169. .name = "microdvd",
  170. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  171. .priv_data_size = sizeof(MicroDVDContext),
  172. .read_probe = microdvd_probe,
  173. .read_header = microdvd_read_header,
  174. .read_packet = microdvd_read_packet,
  175. .read_seek2 = microdvd_read_seek,
  176. .read_close = microdvd_read_close,
  177. .priv_class = &microdvd_class,
  178. };