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.2KB

  1. /*
  2. * MicroDVD subtitle demuxer
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "internal.h"
  23. #include "libavutil/intreadwrite.h"
  24. #define MAX_LINESIZE 2048
  25. typedef struct {
  26. uint8_t lines[3][MAX_LINESIZE];
  27. int64_t pos[3];
  28. } MicroDVDContext;
  29. static int microdvd_probe(AVProbeData *p)
  30. {
  31. unsigned char c, *ptr = p->buf;
  32. int i;
  33. if (AV_RB24(ptr) == 0xEFBBBF)
  34. ptr += 3; /* skip UTF-8 BOM */
  35. for (i=0; i<3; i++) {
  36. if (sscanf(ptr, "{%*d}{}%c", &c) != 1 &&
  37. sscanf(ptr, "{%*d}{%*d}%c", &c) != 1 &&
  38. sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
  39. return 0;
  40. ptr += strcspn(ptr, "\n") + 1;
  41. }
  42. return AVPROBE_SCORE_MAX;
  43. }
  44. static int microdvd_read_header(AVFormatContext *s)
  45. {
  46. AVRational pts_info = (AVRational){ 2997, 125 }; /* default: 23.976 fps */
  47. MicroDVDContext *microdvd = s->priv_data;
  48. AVStream *st = avformat_new_stream(s, NULL);
  49. int i, frame;
  50. double fps;
  51. char c;
  52. if (!st)
  53. return -1;
  54. for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
  55. microdvd->pos[i] = avio_tell(s->pb);
  56. ff_get_line(s->pb, microdvd->lines[i], sizeof(microdvd->lines[i]));
  57. if ((sscanf(microdvd->lines[i], "{%d}{}%6lf", &frame, &fps) == 2 ||
  58. sscanf(microdvd->lines[i], "{%d}{%*d}%6lf", &frame, &fps) == 2)
  59. && frame <= 1 && fps > 3 && fps < 100)
  60. pts_info = av_d2q(fps, 100000);
  61. if (sscanf(microdvd->lines[i], "{DEFAULT}{}%c", &c) == 1) {
  62. st->codec->extradata = av_strdup(microdvd->lines[i] + 11);
  63. st->codec->extradata_size = strlen(st->codec->extradata);
  64. i--;
  65. }
  66. }
  67. avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
  68. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  69. st->codec->codec_id = CODEC_ID_MICRODVD;
  70. return 0;
  71. }
  72. static int64_t get_pts(const char *buf)
  73. {
  74. int frame;
  75. char c;
  76. if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
  77. return frame;
  78. return AV_NOPTS_VALUE;
  79. }
  80. static int get_duration(const char *buf)
  81. {
  82. int frame_start, frame_end;
  83. if (sscanf(buf, "{%d}{%d}", &frame_start, &frame_end) == 2)
  84. return frame_end - frame_start;
  85. return 0;
  86. }
  87. static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
  88. {
  89. MicroDVDContext *microdvd = s->priv_data;
  90. char buffer[MAX_LINESIZE];
  91. int64_t pos = avio_tell(s->pb);
  92. int i, len = 0, res = AVERROR_EOF;
  93. for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
  94. if (microdvd->lines[i][0]) {
  95. strcpy(buffer, microdvd->lines[i]);
  96. pos = microdvd->pos[i];
  97. len = strlen(buffer);
  98. microdvd->lines[i][0] = 0;
  99. break;
  100. }
  101. }
  102. if (!len)
  103. len = ff_get_line(s->pb, buffer, sizeof(buffer));
  104. if (buffer[0] && !(res = av_new_packet(pkt, len))) {
  105. memcpy(pkt->data, buffer, len);
  106. pkt->flags |= AV_PKT_FLAG_KEY;
  107. pkt->pos = pos;
  108. pkt->pts = pkt->dts = get_pts(buffer);
  109. if (pkt->pts != AV_NOPTS_VALUE) // TODO: handle "{}" duration
  110. pkt->duration = get_duration(buffer);
  111. }
  112. return res;
  113. }
  114. AVInputFormat ff_microdvd_demuxer = {
  115. .name = "microdvd",
  116. .long_name = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
  117. .priv_data_size = sizeof(MicroDVDContext),
  118. .read_probe = microdvd_probe,
  119. .read_header = microdvd_read_header,
  120. .read_packet = microdvd_read_packet,
  121. .flags = AVFMT_GENERIC_INDEX,
  122. };