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.

198 lines
6.3KB

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