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.

207 lines
6.8KB

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