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.

216 lines
6.7KB

  1. /*
  2. * SSA/ASS muxer
  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/avstring.h"
  22. #include "avformat.h"
  23. #include "internal.h"
  24. typedef struct DialogueLine {
  25. int readorder;
  26. char *line;
  27. struct DialogueLine *prev, *next;
  28. } DialogueLine;
  29. typedef struct ASSContext{
  30. unsigned int extra_index;
  31. int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like)
  32. int expected_readorder;
  33. DialogueLine *dialogue_cache;
  34. DialogueLine *last_added_dialogue;
  35. int cache_size;
  36. }ASSContext;
  37. static int write_header(AVFormatContext *s)
  38. {
  39. ASSContext *ass = s->priv_data;
  40. AVCodecContext *avctx= s->streams[0]->codec;
  41. uint8_t *last= NULL;
  42. if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
  43. avctx->codec_id != AV_CODEC_ID_ASS)) {
  44. av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
  45. return -1;
  46. }
  47. ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
  48. avpriv_set_pts_info(s->streams[0], 64, 1, 100);
  49. while(ass->extra_index < avctx->extradata_size){
  50. uint8_t *p = avctx->extradata + ass->extra_index;
  51. uint8_t *end= strchr(p, '\n');
  52. if(!end) end= avctx->extradata + avctx->extradata_size;
  53. else end++;
  54. avio_write(s->pb, p, end-p);
  55. ass->extra_index += end-p;
  56. if(last && !memcmp(last, "[Events]", 8))
  57. break;
  58. last=p;
  59. }
  60. avio_flush(s->pb);
  61. return 0;
  62. }
  63. static void purge_dialogues(AVFormatContext *s, int force)
  64. {
  65. int n = 0;
  66. ASSContext *ass = s->priv_data;
  67. DialogueLine *dialogue = ass->dialogue_cache;
  68. while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
  69. DialogueLine *next = dialogue->next;
  70. if (dialogue->readorder != ass->expected_readorder) {
  71. av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
  72. ass->expected_readorder, dialogue->readorder);
  73. ass->expected_readorder = dialogue->readorder;
  74. }
  75. avio_printf(s->pb, "Dialogue: %s\r\n", dialogue->line);
  76. if (dialogue == ass->last_added_dialogue)
  77. ass->last_added_dialogue = next;
  78. av_free(dialogue->line);
  79. av_free(dialogue);
  80. if (next)
  81. next->prev = NULL;
  82. dialogue = ass->dialogue_cache = next;
  83. ass->expected_readorder++;
  84. n++;
  85. }
  86. ass->cache_size -= n;
  87. if (n > 1)
  88. av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
  89. n, ass->cache_size, ass->expected_readorder);
  90. }
  91. static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
  92. {
  93. DialogueLine *cur, *next = NULL, *prev = NULL;
  94. /* from the last added to the end of the list */
  95. if (ass->last_added_dialogue) {
  96. for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
  97. if (cur->readorder > dialogue->readorder)
  98. break;
  99. prev = cur;
  100. next = cur->next;
  101. }
  102. }
  103. /* from the beginning to the last one added */
  104. if (!prev) {
  105. next = ass->dialogue_cache;
  106. for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
  107. if (cur->readorder > dialogue->readorder)
  108. break;
  109. prev = cur;
  110. next = cur->next;
  111. }
  112. }
  113. if (prev) {
  114. prev->next = dialogue;
  115. dialogue->prev = prev;
  116. } else {
  117. dialogue->prev = ass->dialogue_cache;
  118. ass->dialogue_cache = dialogue;
  119. }
  120. if (next) {
  121. next->prev = dialogue;
  122. dialogue->next = next;
  123. }
  124. ass->cache_size++;
  125. ass->last_added_dialogue = dialogue;
  126. }
  127. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  128. {
  129. ASSContext *ass = s->priv_data;
  130. if (ass->write_ts) {
  131. long int layer;
  132. char *p = pkt->data;
  133. int64_t start = pkt->pts;
  134. int64_t end = start + pkt->duration;
  135. int hh1, mm1, ss1, ms1;
  136. int hh2, mm2, ss2, ms2;
  137. DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
  138. if (!dialogue)
  139. return AVERROR(ENOMEM);
  140. dialogue->readorder = strtol(p, &p, 10);
  141. if (dialogue->readorder < ass->expected_readorder)
  142. av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
  143. dialogue->readorder);
  144. if (*p == ',')
  145. p++;
  146. layer = strtol(p, &p, 10);
  147. if (*p == ',')
  148. p++;
  149. hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
  150. hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
  151. ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
  152. ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
  153. if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
  154. if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
  155. dialogue->line = av_asprintf("%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
  156. layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
  157. if (!dialogue->line) {
  158. av_free(dialogue);
  159. return AVERROR(ENOMEM);
  160. }
  161. insert_dialogue(ass, dialogue);
  162. purge_dialogues(s, 0);
  163. } else {
  164. avio_write(s->pb, pkt->data, pkt->size);
  165. }
  166. return 0;
  167. }
  168. static int write_trailer(AVFormatContext *s)
  169. {
  170. ASSContext *ass = s->priv_data;
  171. AVCodecContext *avctx= s->streams[0]->codec;
  172. purge_dialogues(s, 1);
  173. avio_write(s->pb, avctx->extradata + ass->extra_index,
  174. avctx->extradata_size - ass->extra_index);
  175. return 0;
  176. }
  177. AVOutputFormat ff_ass_muxer = {
  178. .name = "ass",
  179. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  180. .mime_type = "text/x-ssa",
  181. .extensions = "ass,ssa",
  182. .priv_data_size = sizeof(ASSContext),
  183. .subtitle_codec = AV_CODEC_ID_SSA,
  184. .write_header = write_header,
  185. .write_packet = write_packet,
  186. .write_trailer = write_trailer,
  187. .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT,
  188. };