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.

221 lines
7.4KB

  1. /*
  2. * SSA/ASS common functions
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.org>
  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 "avcodec.h"
  22. #include "ass.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/bprint.h"
  26. #include "libavutil/common.h"
  27. int ff_ass_subtitle_header(AVCodecContext *avctx,
  28. const char *font, int font_size,
  29. int color, int back_color,
  30. int bold, int italic, int underline,
  31. int alignment)
  32. {
  33. avctx->subtitle_header = av_asprintf(
  34. "[Script Info]\r\n"
  35. "ScriptType: v4.00+\r\n"
  36. "PlayResX: 384\r\n"
  37. "PlayResY: 288\r\n"
  38. "\r\n"
  39. "[V4+ Styles]\r\n"
  40. /* ASSv4 header */
  41. "Format: Name, "
  42. "Fontname, Fontsize, "
  43. "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
  44. "Bold, Italic, Underline, StrikeOut, "
  45. "ScaleX, ScaleY, "
  46. "Spacing, Angle, "
  47. "BorderStyle, Outline, Shadow, "
  48. "Alignment, MarginL, MarginR, MarginV, "
  49. "Encoding\r\n"
  50. "Style: "
  51. "Default," /* Name */
  52. "%s,%d," /* Font{name,size} */
  53. "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
  54. "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */
  55. "100,100," /* Scale{X,Y} */
  56. "0,0," /* Spacing, Angle */
  57. "1,1,0," /* BorderStyle, Outline, Shadow */
  58. "%d,10,10,10," /* Alignment, Margin[LRV] */
  59. "0\r\n" /* Encoding */
  60. "\r\n"
  61. "[Events]\r\n"
  62. "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
  63. font, font_size, color, color, back_color, back_color,
  64. -bold, -italic, -underline, alignment);
  65. if (!avctx->subtitle_header)
  66. return AVERROR(ENOMEM);
  67. avctx->subtitle_header_size = strlen(avctx->subtitle_header);
  68. return 0;
  69. }
  70. int ff_ass_subtitle_header_default(AVCodecContext *avctx)
  71. {
  72. return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
  73. ASS_DEFAULT_FONT_SIZE,
  74. ASS_DEFAULT_COLOR,
  75. ASS_DEFAULT_BACK_COLOR,
  76. ASS_DEFAULT_BOLD,
  77. ASS_DEFAULT_ITALIC,
  78. ASS_DEFAULT_UNDERLINE,
  79. ASS_DEFAULT_ALIGNMENT);
  80. }
  81. static void insert_ts(AVBPrint *buf, int ts)
  82. {
  83. if (ts == -1) {
  84. av_bprintf(buf, "9:59:59.99,");
  85. } else {
  86. int h, m, s;
  87. h = ts/360000; ts -= 360000*h;
  88. m = ts/ 6000; ts -= 6000*m;
  89. s = ts/ 100; ts -= 100*s;
  90. av_bprintf(buf, "%d:%02d:%02d.%02d,", h, m, s, ts);
  91. }
  92. }
  93. int ff_ass_bprint_dialog(AVBPrint *buf, const char *dialog,
  94. int ts_start, int duration, int raw)
  95. {
  96. int dlen;
  97. if (!raw || raw == 2) {
  98. long int layer = 0;
  99. if (raw == 2) {
  100. /* skip ReadOrder */
  101. dialog = strchr(dialog, ',');
  102. if (!dialog)
  103. return AVERROR_INVALIDDATA;
  104. dialog++;
  105. /* extract Layer or Marked */
  106. layer = strtol(dialog, (char**)&dialog, 10);
  107. if (*dialog != ',')
  108. return AVERROR_INVALIDDATA;
  109. dialog++;
  110. }
  111. av_bprintf(buf, "Dialogue: %ld,", layer);
  112. insert_ts(buf, ts_start);
  113. insert_ts(buf, duration == -1 ? -1 : ts_start + duration);
  114. if (raw != 2)
  115. av_bprintf(buf, "Default,,0,0,0,,");
  116. }
  117. dlen = strcspn(dialog, "\n");
  118. dlen += dialog[dlen] == '\n';
  119. av_bprintf(buf, "%.*s", dlen, dialog);
  120. if (raw == 2)
  121. av_bprintf(buf, "\r\n");
  122. return dlen;
  123. }
  124. int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
  125. int ts_start, int duration, int raw)
  126. {
  127. AVBPrint buf;
  128. int ret, dlen;
  129. AVSubtitleRect **rects;
  130. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  131. if ((ret = ff_ass_bprint_dialog(&buf, dialog, ts_start, duration, raw)) < 0)
  132. goto err;
  133. dlen = ret;
  134. if (!av_bprint_is_complete(&buf))
  135. goto errnomem;
  136. rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
  137. if (!rects)
  138. goto errnomem;
  139. sub->rects = rects;
  140. sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
  141. rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
  142. rects[sub->num_rects]->type = SUBTITLE_ASS;
  143. ret = av_bprint_finalize(&buf, &rects[sub->num_rects]->ass);
  144. if (ret < 0)
  145. goto err;
  146. sub->num_rects++;
  147. return dlen;
  148. errnomem:
  149. ret = AVERROR(ENOMEM);
  150. err:
  151. av_bprint_finalize(&buf, NULL);
  152. return ret;
  153. }
  154. int ff_ass_add_rect_bprint(AVSubtitle *sub, const AVBPrint *buf,
  155. int ts_start, int duration, int raw)
  156. {
  157. if (!av_bprint_is_complete(buf))
  158. return AVERROR(ENOMEM);
  159. return ff_ass_add_rect(sub, buf->str, ts_start, duration, raw);
  160. }
  161. void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
  162. const char *linebreaks, int keep_ass_markup)
  163. {
  164. const char *p_end = p + size;
  165. for (; p < p_end && *p; p++) {
  166. /* forced custom line breaks, not accounted as "normal" EOL */
  167. if (linebreaks && strchr(linebreaks, *p)) {
  168. av_bprintf(buf, "\\N");
  169. /* standard ASS escaping so random characters don't get mis-interpreted
  170. * as ASS */
  171. } else if (!keep_ass_markup && strchr("{}\\", *p)) {
  172. av_bprintf(buf, "\\%c", *p);
  173. /* some packets might end abruptly (no \0 at the end, like for example
  174. * in some cases of demuxing from a classic video container), some
  175. * might be terminated with \n or \r\n which we have to remove (for
  176. * consistency with those who haven't), and we also have to deal with
  177. * evil cases such as \r at the end of the buffer (and no \0 terminated
  178. * character) */
  179. } else if (p[0] == '\n') {
  180. /* some stuff left so we can insert a line break */
  181. if (p < p_end - 1)
  182. av_bprintf(buf, "\\N");
  183. } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
  184. /* \r followed by a \n, we can skip it. We don't insert the \N yet
  185. * because we don't know if it is followed by more text */
  186. continue;
  187. /* finally, a sane character */
  188. } else {
  189. av_bprint_chars(buf, *p, 1);
  190. }
  191. }
  192. av_bprintf(buf, "\r\n");
  193. }