assenc.c 7.7 KB

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