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.

200 lines
6.5KB

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