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.

168 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 border_style, 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. "%d,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, border_style, 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_BORDERSTYLE,
  83. ASS_DEFAULT_ALIGNMENT);
  84. }
  85. char *ff_ass_get_dialog(int readorder, int layer, const char *style,
  86. const char *speaker, const char *text)
  87. {
  88. return av_asprintf("%d,%d,%s,%s,0,0,0,,%s",
  89. readorder, layer, style ? style : "Default",
  90. speaker ? speaker : "", text);
  91. }
  92. int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
  93. int readorder, int layer, const char *style,
  94. const char *speaker)
  95. {
  96. char *ass_str;
  97. AVSubtitleRect **rects;
  98. rects = av_realloc_array(sub->rects, sub->num_rects+1, sizeof(*sub->rects));
  99. if (!rects)
  100. return AVERROR(ENOMEM);
  101. sub->rects = rects;
  102. rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
  103. if (!rects[sub->num_rects])
  104. return AVERROR(ENOMEM);
  105. rects[sub->num_rects]->type = SUBTITLE_ASS;
  106. ass_str = ff_ass_get_dialog(readorder, layer, style, speaker, dialog);
  107. if (!ass_str)
  108. return AVERROR(ENOMEM);
  109. rects[sub->num_rects]->ass = ass_str;
  110. sub->num_rects++;
  111. return 0;
  112. }
  113. void ff_ass_decoder_flush(AVCodecContext *avctx)
  114. {
  115. FFASSDecoderContext *s = avctx->priv_data;
  116. if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
  117. s->readorder = 0;
  118. }
  119. void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size,
  120. const char *linebreaks, int keep_ass_markup)
  121. {
  122. const char *p_end = p + size;
  123. for (; p < p_end && *p; p++) {
  124. /* forced custom line breaks, not accounted as "normal" EOL */
  125. if (linebreaks && strchr(linebreaks, *p)) {
  126. av_bprintf(buf, "\\N");
  127. /* standard ASS escaping so random characters don't get mis-interpreted
  128. * as ASS */
  129. } else if (!keep_ass_markup && strchr("{}\\", *p)) {
  130. av_bprintf(buf, "\\%c", *p);
  131. /* some packets might end abruptly (no \0 at the end, like for example
  132. * in some cases of demuxing from a classic video container), some
  133. * might be terminated with \n or \r\n which we have to remove (for
  134. * consistency with those who haven't), and we also have to deal with
  135. * evil cases such as \r at the end of the buffer (and no \0 terminated
  136. * character) */
  137. } else if (p[0] == '\n') {
  138. /* some stuff left so we can insert a line break */
  139. if (p < p_end - 1)
  140. av_bprintf(buf, "\\N");
  141. } else if (p[0] == '\r' && p < p_end - 1 && p[1] == '\n') {
  142. /* \r followed by a \n, we can skip it. We don't insert the \N yet
  143. * because we don't know if it is followed by more text */
  144. continue;
  145. /* finally, a sane character */
  146. } else {
  147. av_bprint_chars(buf, *p, 1);
  148. }
  149. }
  150. }