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.

219 lines
5.9KB

  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_dlog(NULL, "%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->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  76. st->codec->codec_id= AV_CODEC_ID_SSA;
  77. header_remaining= INT_MAX;
  78. dst[0] = &st->codec->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) ass->event_count++;
  97. else header_remaining--;
  98. }
  99. st->codec->extradata_size= pos[0];
  100. if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
  101. goto fail;
  102. ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
  103. p= ass->event_buffer;
  104. for(i=0; i<ass->event_count; i++){
  105. ass->event[i]= p;
  106. while(*p && *p != '\n')
  107. p++;
  108. p++;
  109. }
  110. qsort(ass->event, ass->event_count, sizeof(*ass->event), event_cmp);
  111. return 0;
  112. fail:
  113. read_close(s);
  114. return -1;
  115. }
  116. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  117. {
  118. ASSContext *ass = s->priv_data;
  119. uint8_t *p, *end;
  120. if(ass->event_index >= ass->event_count)
  121. return AVERROR(EIO);
  122. p= ass->event[ ass->event_index ];
  123. end= strchr(p, '\n');
  124. av_new_packet(pkt, end ? end-p+1 : strlen(p));
  125. pkt->flags |= AV_PKT_FLAG_KEY;
  126. pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
  127. pkt->pts= pkt->dts= get_pts(p);
  128. memcpy(pkt->data, p, pkt->size);
  129. ass->event_index++;
  130. return 0;
  131. }
  132. static int read_seek2(AVFormatContext *s, int stream_index,
  133. int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  134. {
  135. ASSContext *ass = s->priv_data;
  136. if (flags & AVSEEK_FLAG_BYTE) {
  137. return AVERROR(ENOSYS);
  138. } else if (flags & AVSEEK_FLAG_FRAME) {
  139. if (ts < 0 || ts >= ass->event_count)
  140. return AVERROR(ERANGE);
  141. ass->event_index = ts;
  142. } else {
  143. int i, idx = -1;
  144. int64_t min_ts_diff = INT64_MAX;
  145. if (stream_index == -1) {
  146. AVRational time_base = s->streams[0]->time_base;
  147. ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
  148. min_ts = av_rescale_rnd(min_ts, time_base.den,
  149. time_base.num * (int64_t)AV_TIME_BASE,
  150. AV_ROUND_UP);
  151. max_ts = av_rescale_rnd(max_ts, time_base.den,
  152. time_base.num * (int64_t)AV_TIME_BASE,
  153. AV_ROUND_DOWN);
  154. }
  155. /* TODO: ass->event[] is sorted by pts so we could do a binary search */
  156. for (i=0; i<ass->event_count; i++) {
  157. int64_t pts = get_pts(ass->event[i]);
  158. int64_t ts_diff = FFABS(pts - ts);
  159. if (pts >= min_ts && pts <= max_ts && ts_diff < min_ts_diff) {
  160. min_ts_diff = ts_diff;
  161. idx = i;
  162. }
  163. }
  164. if (idx < 0)
  165. return AVERROR(ERANGE);
  166. ass->event_index = idx;
  167. }
  168. return 0;
  169. }
  170. AVInputFormat ff_ass_demuxer = {
  171. .name = "ass",
  172. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  173. .priv_data_size = sizeof(ASSContext),
  174. .read_probe = probe,
  175. .read_header = read_header,
  176. .read_packet = read_packet,
  177. .read_close = read_close,
  178. .read_seek2 = read_seek2,
  179. };