assdec.c 5.8 KB

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