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.

225 lines
6.1KB

  1. /*
  2. * SSA/ASS demuxer
  3. * Copyright (c) 2008 Michael Niedermayer
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <stdint.h>
  22. #include "libavutil/mathematics.h"
  23. #include "avformat.h"
  24. #include "internal.h"
  25. #define MAX_LINESIZE 2000
  26. typedef struct ASSContext {
  27. uint8_t *event_buffer;
  28. uint8_t **event;
  29. unsigned int event_count;
  30. unsigned int event_index;
  31. } ASSContext;
  32. static int probe(AVProbeData *p)
  33. {
  34. const char *header = "[Script Info]";
  35. if (!memcmp(p->buf, header, strlen(header)) ||
  36. !memcmp(p->buf + 3, header, strlen(header)))
  37. return AVPROBE_SCORE_MAX;
  38. return 0;
  39. }
  40. static int read_close(AVFormatContext *s)
  41. {
  42. ASSContext *ass = s->priv_data;
  43. av_freep(&ass->event_buffer);
  44. av_freep(&ass->event);
  45. return 0;
  46. }
  47. static int64_t get_pts(const uint8_t *p)
  48. {
  49. int hour, min, sec, hsec;
  50. if (sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
  51. return AV_NOPTS_VALUE;
  52. av_log(NULL, AV_LOG_TRACE, "%d %d %d %d [%s]\n", hour, min, sec, hsec, p);
  53. min += 60 * hour;
  54. sec += 60 * min;
  55. return sec * 100 + hsec;
  56. }
  57. static int event_cmp(const void *_a, const void *_b)
  58. {
  59. const uint8_t *const *a = _a, *const *b = _b;
  60. return get_pts(*a) - get_pts(*b);
  61. }
  62. static int read_header(AVFormatContext *s)
  63. {
  64. int i, len, header_remaining;
  65. ASSContext *ass = s->priv_data;
  66. AVIOContext *pb = s->pb;
  67. AVStream *st;
  68. int allocated[2] = { 0 };
  69. uint8_t *p, **dst[2] = { 0 };
  70. int pos[2] = { 0 };
  71. st = avformat_new_stream(s, NULL);
  72. if (!st)
  73. return -1;
  74. avpriv_set_pts_info(st, 64, 1, 100);
  75. st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
  76. st->codecpar->codec_id = AV_CODEC_ID_SSA;
  77. header_remaining = INT_MAX;
  78. dst[0] = &st->codecpar->extradata;
  79. dst[1] = &ass->event_buffer;
  80. while (!pb->eof_reached) {
  81. uint8_t line[MAX_LINESIZE];
  82. len = ff_get_line(pb, line, sizeof(line));
  83. if (!memcmp(line, "[Events]", 8))
  84. header_remaining = 2;
  85. else if (line[0] == '[')
  86. header_remaining = INT_MAX;
  87. i = header_remaining == 0;
  88. if (i && get_pts(line) == AV_NOPTS_VALUE)
  89. continue;
  90. p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i] + MAX_LINESIZE);
  91. if (!p)
  92. goto fail;
  93. *(dst[i]) = p;
  94. memcpy(p + pos[i], line, len + 1);
  95. pos[i] += len;
  96. if (i)
  97. ass->event_count++;
  98. else
  99. header_remaining--;
  100. }
  101. st->codecpar->extradata_size = pos[0];
  102. if (ass->event_count >= UINT_MAX / sizeof(*ass->event))
  103. goto fail;
  104. ass->event = av_malloc(ass->event_count * sizeof(*ass->event));
  105. p = ass->event_buffer;
  106. for (i = 0; i < ass->event_count; i++) {
  107. ass->event[i] = p;
  108. while (*p && *p != '\n')
  109. p++;
  110. p++;
  111. }
  112. qsort(ass->event, ass->event_count, sizeof(*ass->event), event_cmp);
  113. return 0;
  114. fail:
  115. read_close(s);
  116. return -1;
  117. }
  118. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  119. {
  120. ASSContext *ass = s->priv_data;
  121. uint8_t *p, *end;
  122. int ret;
  123. if (ass->event_index >= ass->event_count)
  124. return AVERROR(EIO);
  125. p = ass->event[ass->event_index];
  126. end = strchr(p, '\n');
  127. ret = av_new_packet(pkt, end ? end - p + 1 : strlen(p));
  128. if (ret < 0)
  129. return ret;
  130. pkt->flags |= AV_PKT_FLAG_KEY;
  131. pkt->pos = p - ass->event_buffer + s->streams[0]->codecpar->extradata_size;
  132. pkt->pts = pkt->dts = get_pts(p);
  133. memcpy(pkt->data, p, pkt->size);
  134. ass->event_index++;
  135. return 0;
  136. }
  137. static int read_seek2(AVFormatContext *s, int stream_index,
  138. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  139. {
  140. ASSContext *ass = s->priv_data;
  141. if (flags & AVSEEK_FLAG_BYTE) {
  142. return AVERROR(ENOSYS);
  143. } else if (flags & AVSEEK_FLAG_FRAME) {
  144. if (ts < 0 || ts >= ass->event_count)
  145. return AVERROR(ERANGE);
  146. ass->event_index = ts;
  147. } else {
  148. int i, idx = -1;
  149. int64_t min_ts_diff = INT64_MAX;
  150. if (stream_index == -1) {
  151. AVRational time_base = s->streams[0]->time_base;
  152. ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
  153. min_ts = av_rescale_rnd(min_ts, time_base.den,
  154. time_base.num * (int64_t) AV_TIME_BASE,
  155. AV_ROUND_UP);
  156. max_ts = av_rescale_rnd(max_ts, time_base.den,
  157. time_base.num * (int64_t) AV_TIME_BASE,
  158. AV_ROUND_DOWN);
  159. }
  160. /* TODO: ass->event[] is sorted by pts so we could do a binary search */
  161. for (i = 0; i < ass->event_count; i++) {
  162. int64_t pts = get_pts(ass->event[i]);
  163. int64_t ts_diff = FFABS(pts - ts);
  164. if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
  165. min_ts_diff = ts_diff;
  166. idx = i;
  167. }
  168. }
  169. if (idx < 0)
  170. return AVERROR(ERANGE);
  171. ass->event_index = idx;
  172. }
  173. return 0;
  174. }
  175. AVInputFormat ff_ass_demuxer = {
  176. .name = "ass",
  177. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  178. .priv_data_size = sizeof(ASSContext),
  179. .read_probe = probe,
  180. .read_header = read_header,
  181. .read_packet = read_packet,
  182. .read_close = read_close,
  183. .read_seek2 = read_seek2,
  184. };