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