/libavcodec/assenc.c

http://github.com/FFmpeg/FFmpeg · C · 127 lines · 87 code · 17 blank · 23 comment · 9 complexity · 8f1378299042dbd8c10c5bef90c23608 MD5 · raw file

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