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.

250 lines
8.0KB

  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. #include "libavutil/opt.h"
  25. typedef struct DialogueLine {
  26. int readorder;
  27. char *line;
  28. struct DialogueLine *prev, *next;
  29. } DialogueLine;
  30. typedef struct ASSContext {
  31. const AVClass *class;
  32. int write_ts; // 0: ssa (timing in payload), 1: ass (matroska like)
  33. int expected_readorder;
  34. DialogueLine *dialogue_cache;
  35. DialogueLine *last_added_dialogue;
  36. int cache_size;
  37. int ssa_mode;
  38. int ignore_readorder;
  39. uint8_t *trailer;
  40. size_t trailer_size;
  41. } ASSContext;
  42. static int write_header(AVFormatContext *s)
  43. {
  44. ASSContext *ass = s->priv_data;
  45. AVCodecContext *avctx = s->streams[0]->codec;
  46. if (s->nb_streams != 1 || (avctx->codec_id != AV_CODEC_ID_SSA &&
  47. avctx->codec_id != AV_CODEC_ID_ASS)) {
  48. av_log(s, AV_LOG_ERROR, "Exactly one ASS/SSA stream is needed.\n");
  49. return AVERROR(EINVAL);
  50. }
  51. ass->write_ts = avctx->codec_id == AV_CODEC_ID_ASS;
  52. avpriv_set_pts_info(s->streams[0], 64, 1, 100);
  53. if (avctx->extradata_size > 0) {
  54. size_t header_size = avctx->extradata_size;
  55. uint8_t *trailer = strstr(avctx->extradata, "\n[Events]");
  56. if (trailer)
  57. trailer = strstr(trailer, "Format:");
  58. if (trailer)
  59. trailer = strstr(trailer, "\n");
  60. if (trailer++) {
  61. header_size = (trailer - avctx->extradata);
  62. ass->trailer_size = avctx->extradata_size - header_size;
  63. if (ass->trailer_size)
  64. ass->trailer = trailer;
  65. }
  66. avio_write(s->pb, avctx->extradata, header_size);
  67. if (avctx->extradata[header_size - 1] != '\n')
  68. avio_write(s->pb, "\r\n", 2);
  69. ass->ssa_mode = !strstr(avctx->extradata, "\n[V4+ Styles]");
  70. if (!strstr(avctx->extradata, "\n[Events]"))
  71. avio_printf(s->pb, "[Events]\r\nFormat: %s, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
  72. ass->ssa_mode ? "Marked" : "Layer");
  73. }
  74. avio_flush(s->pb);
  75. return 0;
  76. }
  77. static void purge_dialogues(AVFormatContext *s, int force)
  78. {
  79. int n = 0;
  80. ASSContext *ass = s->priv_data;
  81. DialogueLine *dialogue = ass->dialogue_cache;
  82. while (dialogue && (dialogue->readorder == ass->expected_readorder || force)) {
  83. DialogueLine *next = dialogue->next;
  84. if (dialogue->readorder != ass->expected_readorder) {
  85. av_log(s, AV_LOG_WARNING, "ReadOrder gap found between %d and %d\n",
  86. ass->expected_readorder, dialogue->readorder);
  87. ass->expected_readorder = dialogue->readorder;
  88. }
  89. avio_printf(s->pb, "Dialogue: %s\r\n", dialogue->line);
  90. if (dialogue == ass->last_added_dialogue)
  91. ass->last_added_dialogue = next;
  92. av_freep(&dialogue->line);
  93. av_free(dialogue);
  94. if (next)
  95. next->prev = NULL;
  96. dialogue = ass->dialogue_cache = next;
  97. ass->expected_readorder++;
  98. n++;
  99. }
  100. ass->cache_size -= n;
  101. if (n > 1)
  102. av_log(s, AV_LOG_DEBUG, "wrote %d ASS lines, cached dialogues: %d, waiting for event id %d\n",
  103. n, ass->cache_size, ass->expected_readorder);
  104. }
  105. static void insert_dialogue(ASSContext *ass, DialogueLine *dialogue)
  106. {
  107. DialogueLine *cur, *next = NULL, *prev = NULL;
  108. /* from the last added to the end of the list */
  109. if (ass->last_added_dialogue) {
  110. for (cur = ass->last_added_dialogue; cur; cur = cur->next) {
  111. if (cur->readorder > dialogue->readorder)
  112. break;
  113. prev = cur;
  114. next = cur->next;
  115. }
  116. }
  117. /* from the beginning to the last one added */
  118. if (!prev) {
  119. next = ass->dialogue_cache;
  120. for (cur = next; cur != ass->last_added_dialogue; cur = cur->next) {
  121. if (cur->readorder > dialogue->readorder)
  122. break;
  123. prev = cur;
  124. next = cur->next;
  125. }
  126. }
  127. if (prev) {
  128. prev->next = dialogue;
  129. dialogue->prev = prev;
  130. } else {
  131. dialogue->prev = ass->dialogue_cache;
  132. ass->dialogue_cache = dialogue;
  133. }
  134. if (next) {
  135. next->prev = dialogue;
  136. dialogue->next = next;
  137. }
  138. ass->cache_size++;
  139. ass->last_added_dialogue = dialogue;
  140. }
  141. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  142. {
  143. ASSContext *ass = s->priv_data;
  144. if (ass->write_ts) {
  145. long int layer;
  146. char *p = pkt->data;
  147. int64_t start = pkt->pts;
  148. int64_t end = start + pkt->duration;
  149. int hh1, mm1, ss1, ms1;
  150. int hh2, mm2, ss2, ms2;
  151. DialogueLine *dialogue = av_mallocz(sizeof(*dialogue));
  152. if (!dialogue)
  153. return AVERROR(ENOMEM);
  154. dialogue->readorder = strtol(p, &p, 10);
  155. if (dialogue->readorder < ass->expected_readorder)
  156. av_log(s, AV_LOG_WARNING, "Unexpected ReadOrder %d\n",
  157. dialogue->readorder);
  158. if (*p == ',')
  159. p++;
  160. if (ass->ssa_mode && !strncmp(p, "Marked=", 7))
  161. p += 7;
  162. layer = strtol(p, &p, 10);
  163. if (*p == ',')
  164. p++;
  165. hh1 = (int)(start / 360000); mm1 = (int)(start / 6000) % 60;
  166. hh2 = (int)(end / 360000); mm2 = (int)(end / 6000) % 60;
  167. ss1 = (int)(start / 100) % 60; ms1 = (int)(start % 100);
  168. ss2 = (int)(end / 100) % 60; ms2 = (int)(end % 100);
  169. if (hh1 > 9) hh1 = 9, mm1 = 59, ss1 = 59, ms1 = 99;
  170. if (hh2 > 9) hh2 = 9, mm2 = 59, ss2 = 59, ms2 = 99;
  171. dialogue->line = av_asprintf("%s%ld,%d:%02d:%02d.%02d,%d:%02d:%02d.%02d,%s",
  172. ass->ssa_mode ? "Marked=" : "",
  173. layer, hh1, mm1, ss1, ms1, hh2, mm2, ss2, ms2, p);
  174. if (!dialogue->line) {
  175. av_free(dialogue);
  176. return AVERROR(ENOMEM);
  177. }
  178. insert_dialogue(ass, dialogue);
  179. purge_dialogues(s, ass->ignore_readorder);
  180. } else {
  181. avio_write(s->pb, pkt->data, pkt->size);
  182. }
  183. return 0;
  184. }
  185. static int write_trailer(AVFormatContext *s)
  186. {
  187. ASSContext *ass = s->priv_data;
  188. purge_dialogues(s, 1);
  189. if (ass->trailer) {
  190. avio_write(s->pb, ass->trailer, ass->trailer_size);
  191. }
  192. return 0;
  193. }
  194. #define OFFSET(x) offsetof(ASSContext, x)
  195. #define E AV_OPT_FLAG_ENCODING_PARAM
  196. static const AVOption options[] = {
  197. { "ignore_readorder", "write events immediately, even if they're out-of-order", OFFSET(ignore_readorder), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, E },
  198. { NULL },
  199. };
  200. static const AVClass ass_class = {
  201. .class_name = "ass muxer",
  202. .item_name = av_default_item_name,
  203. .option = options,
  204. .version = LIBAVUTIL_VERSION_INT,
  205. };
  206. AVOutputFormat ff_ass_muxer = {
  207. .name = "ass",
  208. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  209. .mime_type = "text/x-ssa",
  210. .extensions = "ass,ssa",
  211. .priv_data_size = sizeof(ASSContext),
  212. .subtitle_codec = AV_CODEC_ID_SSA,
  213. .write_header = write_header,
  214. .write_packet = write_packet,
  215. .write_trailer = write_trailer,
  216. .flags = AVFMT_GLOBALHEADER | AVFMT_NOTIMESTAMPS | AVFMT_TS_NONSTRICT,
  217. .priv_class = &ass_class,
  218. };