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.

106 lines
4.0KB

  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/avstring.h"
  24. #include "libavutil/common.h"
  25. int ff_ass_subtitle_header(AVCodecContext *avctx,
  26. const char *font, int font_size,
  27. int color, int back_color,
  28. int bold, int italic, int underline,
  29. int alignment)
  30. {
  31. avctx->subtitle_header = av_asprintf(
  32. "[Script Info]\r\n"
  33. "ScriptType: v4.00+\r\n"
  34. "\r\n"
  35. "[V4+ Styles]\r\n"
  36. "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\r\n"
  37. "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"
  38. "\r\n"
  39. "[Events]\r\n"
  40. "Format: Layer, Start, End, Style, Text\r\n",
  41. font, font_size, color, color, back_color, back_color,
  42. -bold, -italic, -underline, alignment);
  43. if (!avctx->subtitle_header)
  44. return AVERROR(ENOMEM);
  45. avctx->subtitle_header_size = strlen(avctx->subtitle_header);
  46. return 0;
  47. }
  48. int ff_ass_subtitle_header_default(AVCodecContext *avctx)
  49. {
  50. return ff_ass_subtitle_header(avctx, ASS_DEFAULT_FONT,
  51. ASS_DEFAULT_FONT_SIZE,
  52. ASS_DEFAULT_COLOR,
  53. ASS_DEFAULT_BACK_COLOR,
  54. ASS_DEFAULT_BOLD,
  55. ASS_DEFAULT_ITALIC,
  56. ASS_DEFAULT_UNDERLINE,
  57. ASS_DEFAULT_ALIGNMENT);
  58. }
  59. static int ts_to_string(char *str, int strlen, int ts)
  60. {
  61. int h, m, s;
  62. h = ts/360000; ts -= 360000*h;
  63. m = ts/ 6000; ts -= 6000*m;
  64. s = ts/ 100; ts -= 100*s;
  65. return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
  66. }
  67. int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
  68. int ts_start, int duration, int raw)
  69. {
  70. int len = 0, dlen;
  71. char s_start[16], s_end[16], header[48] = {0};
  72. AVSubtitleRect **rects;
  73. if (!raw) {
  74. ts_to_string(s_start, sizeof(s_start), ts_start);
  75. if (duration == -1)
  76. snprintf(s_end, sizeof(s_end), "9:59:59.99");
  77. else
  78. ts_to_string(s_end, sizeof(s_end), ts_start + duration);
  79. len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,Default,",
  80. s_start, s_end);
  81. }
  82. dlen = strcspn(dialog, "\n");
  83. dlen += dialog[dlen] == '\n';
  84. rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
  85. if (!rects)
  86. return AVERROR(ENOMEM);
  87. sub->rects = rects;
  88. sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
  89. rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
  90. rects[sub->num_rects]->type = SUBTITLE_ASS;
  91. rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
  92. strcpy (rects[sub->num_rects]->ass , header);
  93. av_strlcpy(rects[sub->num_rects]->ass + len, dialog, dlen + 1);
  94. sub->num_rects++;
  95. return dlen;
  96. }