assdec.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #define MAX_LINESIZE 2000
  23. typedef struct ASSContext{
  24. uint8_t *event_buffer;
  25. uint8_t **event;
  26. unsigned int event_count;
  27. unsigned int event_index;
  28. }ASSContext;
  29. static void get_line(ByteIOContext *s, char *buf, int maxlen)
  30. {
  31. int i = 0;
  32. char c;
  33. do{
  34. c = get_byte(s);
  35. if (i < maxlen-1)
  36. buf[i++] = c;
  37. }while(c != '\n' && c);
  38. buf[i] = 0;
  39. }
  40. static int probe(AVProbeData *p)
  41. {
  42. const char *header= "[Script Info]";
  43. if( !memcmp(p->buf , header, strlen(header))
  44. || !memcmp(p->buf+3, header, strlen(header)))
  45. return AVPROBE_SCORE_MAX;
  46. return 0;
  47. }
  48. static int read_close(AVFormatContext *s)
  49. {
  50. ASSContext *ass = s->priv_data;
  51. av_freep(&ass->event_buffer);
  52. av_freep(&ass->event);
  53. return 0;
  54. }
  55. static int64_t get_pts(const uint8_t *p)
  56. {
  57. int hour, min, sec, hsec;
  58. if(sscanf(p, "%*[^,],%d:%d:%d%*c%d", &hour, &min, &sec, &hsec) != 4)
  59. return AV_NOPTS_VALUE;
  60. // av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d [%s]\n", i, hour, min, sec, hsec, p);
  61. min+= 60*hour;
  62. sec+= 60*min;
  63. return sec*100+hsec;
  64. }
  65. static int event_cmp(uint8_t **a, uint8_t **b)
  66. {
  67. return get_pts(*a) - get_pts(*b);
  68. }
  69. static int read_header(AVFormatContext *s, AVFormatParameters *ap)
  70. {
  71. int i, header_remaining;
  72. ASSContext *ass = s->priv_data;
  73. ByteIOContext *pb = s->pb;
  74. AVStream *st;
  75. int allocated[2]={0};
  76. uint8_t *p, **dst[2]={0};
  77. int pos[2]={0};
  78. st = av_new_stream(s, 0);
  79. if (!st)
  80. return -1;
  81. av_set_pts_info(st, 64, 1, 100);
  82. st->codec->codec_type = CODEC_TYPE_SUBTITLE;
  83. st->codec->codec_id= CODEC_ID_SSA;
  84. header_remaining= INT_MAX;
  85. dst[0] = &st->codec->extradata;
  86. dst[1] = &ass->event_buffer;
  87. while(!url_feof(pb)){
  88. uint8_t line[MAX_LINESIZE];
  89. get_line(pb, line, sizeof(line));
  90. if(!memcmp(line, "[Events]", 8))
  91. header_remaining= 2;
  92. else if(line[0]=='[')
  93. header_remaining= INT_MAX;
  94. i= header_remaining==0;
  95. if(i && get_pts(line) == AV_NOPTS_VALUE)
  96. continue;
  97. p = av_fast_realloc(*(dst[i]), &allocated[i], pos[i]+MAX_LINESIZE);
  98. if(!p)
  99. goto fail;
  100. *(dst[i])= p;
  101. memcpy(p + pos[i], line, strlen(line)+1);
  102. pos[i] += strlen(line);
  103. if(i) ass->event_count++;
  104. else header_remaining--;
  105. }
  106. st->codec->extradata_size= pos[0];
  107. if(ass->event_count >= UINT_MAX / sizeof(*ass->event))
  108. goto fail;
  109. ass->event= av_malloc(ass->event_count * sizeof(*ass->event));
  110. p= ass->event_buffer;
  111. for(i=0; i<ass->event_count; i++){
  112. ass->event[i]= p;
  113. while(*p && *p != '\n')
  114. p++;
  115. p++;
  116. }
  117. qsort(ass->event, ass->event_count, sizeof(*ass->event), (void*)event_cmp);
  118. return 0;
  119. fail:
  120. read_close(s);
  121. return -1;
  122. }
  123. static int read_packet(AVFormatContext *s, AVPacket *pkt)
  124. {
  125. ASSContext *ass = s->priv_data;
  126. uint8_t *p, *end;
  127. if(ass->event_index >= ass->event_count)
  128. return AVERROR(EIO);
  129. p= ass->event[ ass->event_index ];
  130. end= strchr(p, '\n');
  131. av_new_packet(pkt, end ? end-p+1 : strlen(p));
  132. pkt->flags |= PKT_FLAG_KEY;
  133. pkt->pos= p - ass->event_buffer + s->streams[0]->codec->extradata_size;
  134. pkt->pts= pkt->dts= get_pts(p);
  135. memcpy(pkt->data, p, pkt->size);
  136. ass->event_index++;
  137. return 0;
  138. }
  139. AVInputFormat ass_demuxer = {
  140. "ass",
  141. NULL_IF_CONFIG_SMALL("SSA/ASS format"),
  142. sizeof(ASSContext),
  143. probe,
  144. read_header,
  145. read_packet,
  146. read_close,
  147. // read_seek,
  148. };