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.

190 lines
6.3KB

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