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.

224 lines
7.6KB

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