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.

217 lines
6.5KB

  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(const 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, ret;
  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. int64_t pts;
  82. if (!strncmp(line, bom, 3))
  83. line += 3;
  84. p = line;
  85. if (!len)
  86. break;
  87. line[strcspn(line, "\r\n")] = 0;
  88. if (!*p)
  89. continue;
  90. if (i++ < 3) {
  91. int frame;
  92. double fps;
  93. char c;
  94. if ((sscanf(line, "{%d}{}%6lf", &frame, &fps) == 2 ||
  95. sscanf(line, "{%d}{%*d}%6lf", &frame, &fps) == 2)
  96. && frame <= 1 && fps > 3 && fps < 100) {
  97. pts_info = av_d2q(fps, 100000);
  98. has_real_fps = 1;
  99. continue;
  100. }
  101. if (!st->codecpar->extradata && sscanf(line, "{DEFAULT}{}%c", &c) == 1) {
  102. int size = strlen(line + 11);
  103. ret = ff_alloc_extradata(st->codecpar, size);
  104. if (ret < 0)
  105. goto fail;
  106. memcpy(st->codecpar->extradata, line + 11, size);
  107. continue;
  108. }
  109. }
  110. #define SKIP_FRAME_ID \
  111. p = strchr(p, '}'); \
  112. if (!p) { \
  113. av_log(s, AV_LOG_WARNING, "Invalid event \"%s\"" \
  114. " at line %d\n", line, i); \
  115. continue; \
  116. } \
  117. p++
  118. SKIP_FRAME_ID;
  119. SKIP_FRAME_ID;
  120. if (!*p)
  121. continue;
  122. pts = get_pts(line);
  123. if (pts == AV_NOPTS_VALUE)
  124. continue;
  125. sub = ff_subtitles_queue_insert(&microdvd->q, p, strlen(p), 0);
  126. if (!sub) {
  127. ret = AVERROR(ENOMEM);
  128. goto fail;
  129. }
  130. sub->pos = pos;
  131. sub->pts = pts;
  132. sub->duration = get_duration(line);
  133. }
  134. ff_subtitles_queue_finalize(s, &microdvd->q);
  135. if (has_real_fps) {
  136. /* export the FPS info only if set in the file */
  137. microdvd->frame_rate = pts_info;
  138. } else if (microdvd->frame_rate.num) {
  139. /* fallback on user specified frame rate */
  140. pts_info = microdvd->frame_rate;
  141. }
  142. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  143. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  144. st->codecpar->codec_id = AV_CODEC_ID_MICRODVD;
  145. return 0;
  146. fail:
  147. ff_subtitles_queue_clean(&microdvd->q);
  148. return ret;
  149. }
  150. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  151. {
  152. MicroDVDContext *microdvd = s->priv_data;
  153. return ff_subtitles_queue_read_packet(&microdvd->q, pkt);
  154. }
  155. static int microdvd_read_seek(AVFormatContext *s, int stream_index,
  156. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  157. {
  158. MicroDVDContext *microdvd = s->priv_data;
  159. return ff_subtitles_queue_seek(&microdvd->q, s, stream_index,
  160. min_ts, ts, max_ts, flags);
  161. }
  162. static int microdvd_read_close(AVFormatContext *s)
  163. {
  164. MicroDVDContext *microdvd = s->priv_data;
  165. ff_subtitles_queue_clean(&microdvd->q);
  166. return 0;
  167. }
  168. #define OFFSET(x) offsetof(MicroDVDContext, x)
  169. #define SD AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_DECODING_PARAM
  170. static const AVOption microdvd_options[] = {
  171. { "subfps", "set the movie frame rate fallback", OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, SD },
  172. { NULL }
  173. };
  174. static const AVClass microdvd_class = {
  175. .class_name = "microdvddec",
  176. .item_name = av_default_item_name,
  177. .option = microdvd_options,
  178. .version = LIBAVUTIL_VERSION_INT,
  179. };
  180. AVInputFormat ff_microdvd_demuxer = {
  181. .name = "microdvd",
  182. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  183. .priv_data_size = sizeof(MicroDVDContext),
  184. .read_probe = microdvd_probe,
  185. .read_header = microdvd_read_header,
  186. .read_packet = microdvd_read_packet,
  187. .read_seek2 = microdvd_read_seek,
  188. .read_close = microdvd_read_close,
  189. .priv_class = &microdvd_class,
  190. };