assdec.c 5.8 KB

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