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.

133 lines
4.2KB

  1. /*
  2. * SSA/ASS encoder
  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 <string.h>
  22. #include "avcodec.h"
  23. #include "ass_split.h"
  24. #include "ass.h"
  25. #include "libavutil/avstring.h"
  26. #include "libavutil/internal.h"
  27. #include "libavutil/mem.h"
  28. typedef struct {
  29. int id; ///< current event id, ReadOrder field
  30. } ASSEncodeContext;
  31. static av_cold int ass_encode_init(AVCodecContext *avctx)
  32. {
  33. avctx->extradata = av_malloc(avctx->subtitle_header_size + 1);
  34. if (!avctx->extradata)
  35. return AVERROR(ENOMEM);
  36. memcpy(avctx->extradata, avctx->subtitle_header, avctx->subtitle_header_size);
  37. avctx->extradata_size = avctx->subtitle_header_size;
  38. avctx->extradata[avctx->extradata_size] = 0;
  39. return 0;
  40. }
  41. static int ass_encode_frame(AVCodecContext *avctx,
  42. unsigned char *buf, int bufsize,
  43. const AVSubtitle *sub)
  44. {
  45. ASSEncodeContext *s = avctx->priv_data;
  46. int i, len, total_len = 0;
  47. for (i=0; i<sub->num_rects; i++) {
  48. char ass_line[2048];
  49. const char *ass = sub->rects[i]->ass;
  50. if (sub->rects[i]->type != SUBTITLE_ASS) {
  51. av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
  52. return -1;
  53. }
  54. if (strncmp(ass, "Dialogue: ", 10)) {
  55. av_log(avctx, AV_LOG_ERROR, "AVSubtitle rectangle ass \"%s\""
  56. " does not look like a SSA markup\n", ass);
  57. return AVERROR_INVALIDDATA;
  58. }
  59. if (avctx->codec->id == AV_CODEC_ID_ASS) {
  60. long int layer;
  61. char *p;
  62. if (i > 0) {
  63. av_log(avctx, AV_LOG_ERROR, "ASS encoder supports only one "
  64. "ASS rectangle field.\n");
  65. return AVERROR_INVALIDDATA;
  66. }
  67. ass += 10; // skip "Dialogue: "
  68. /* parse Layer field. If it's a Marked field, the content
  69. * will be "Marked=N" instead of the layer num, so we will
  70. * have layer=0, which is fine. */
  71. layer = strtol(ass, &p, 10);
  72. #define SKIP_ENTRY(ptr) do { \
  73. char *sep = strchr(ptr, ','); \
  74. if (sep) \
  75. ptr = sep + 1; \
  76. } while (0)
  77. SKIP_ENTRY(p); // skip layer or marked
  78. SKIP_ENTRY(p); // skip start timestamp
  79. SKIP_ENTRY(p); // skip end timestamp
  80. snprintf(ass_line, sizeof(ass_line), "%d,%ld,%s", ++s->id, layer, p);
  81. ass_line[strcspn(ass_line, "\r\n")] = 0;
  82. ass = ass_line;
  83. }
  84. len = av_strlcpy(buf+total_len, ass, bufsize-total_len);
  85. if (len > bufsize-total_len-1) {
  86. av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
  87. return -1;
  88. }
  89. total_len += len;
  90. }
  91. return total_len;
  92. }
  93. #if CONFIG_SSA_ENCODER
  94. AVCodec ff_ssa_encoder = {
  95. .name = "ssa",
  96. .long_name = NULL_IF_CONFIG_SMALL("SSA (SubStation Alpha) subtitle"),
  97. .type = AVMEDIA_TYPE_SUBTITLE,
  98. .id = AV_CODEC_ID_SSA,
  99. .init = ass_encode_init,
  100. .encode_sub = ass_encode_frame,
  101. .priv_data_size = sizeof(ASSEncodeContext),
  102. };
  103. #endif
  104. #if CONFIG_ASS_ENCODER
  105. AVCodec ff_ass_encoder = {
  106. .name = "ass",
  107. .long_name = NULL_IF_CONFIG_SMALL("ASS (Advanced SubStation Alpha) subtitle"),
  108. .type = AVMEDIA_TYPE_SUBTITLE,
  109. .id = AV_CODEC_ID_ASS,
  110. .init = ass_encode_init,
  111. .encode_sub = ass_encode_frame,
  112. .priv_data_size = sizeof(ASSEncodeContext),
  113. };
  114. #endif