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.

245 lines
7.5KB

  1. /*
  2. * Copyright (c) 2011 Baptiste Coudurier
  3. * Copyright (c) 2011 Stefano Sabatini
  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. /**
  22. * @file
  23. * Libass subtitles burning filter.
  24. *
  25. * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
  26. */
  27. #include <ass/ass.h>
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/parseutils.h"
  32. #include "drawutils.h"
  33. #include "avfilter.h"
  34. typedef struct {
  35. const AVClass *class;
  36. ASS_Library *library;
  37. ASS_Renderer *renderer;
  38. ASS_Track *track;
  39. char *filename;
  40. uint8_t rgba_map[4];
  41. int pix_step[4]; ///< steps per pixel for each plane of the main output
  42. char *original_size_str;
  43. int original_w, original_h;
  44. FFDrawContext draw;
  45. } AssContext;
  46. #define OFFSET(x) offsetof(AssContext, x)
  47. static const AVOption ass_options[] = {
  48. {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_size_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX },
  49. {NULL},
  50. };
  51. static const char *ass_get_name(void *ctx)
  52. {
  53. return "ass";
  54. }
  55. static const AVClass ass_class = {
  56. "AssContext",
  57. ass_get_name,
  58. ass_options
  59. };
  60. /* libass supports a log level ranging from 0 to 7 */
  61. int ass_libav_log_level_map[] = {
  62. AV_LOG_QUIET, /* 0 */
  63. AV_LOG_PANIC, /* 1 */
  64. AV_LOG_FATAL, /* 2 */
  65. AV_LOG_ERROR, /* 3 */
  66. AV_LOG_WARNING, /* 4 */
  67. AV_LOG_INFO, /* 5 */
  68. AV_LOG_VERBOSE, /* 6 */
  69. AV_LOG_DEBUG, /* 7 */
  70. };
  71. static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
  72. {
  73. int level = ass_libav_log_level_map[ass_level];
  74. av_vlog(ctx, level, fmt, args);
  75. av_log(ctx, level, "\n");
  76. }
  77. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  78. {
  79. AssContext *ass = ctx->priv;
  80. int ret;
  81. ass->class = &ass_class;
  82. av_opt_set_defaults(ass);
  83. if (args)
  84. ass->filename = av_get_token(&args, ":");
  85. if (!ass->filename || !*ass->filename) {
  86. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  87. return AVERROR(EINVAL);
  88. }
  89. if (*args++ == ':' && (ret = av_set_options_string(ass, args, "=", ":")) < 0) {
  90. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  91. return ret;
  92. }
  93. if (ass->original_size_str &&
  94. av_parse_video_size(&ass->original_w, &ass->original_h,
  95. ass->original_size_str) < 0) {
  96. av_log(ctx, AV_LOG_ERROR,
  97. "Invalid original size '%s'.\n", ass->original_size_str);
  98. return AVERROR(EINVAL);
  99. }
  100. ass->library = ass_library_init();
  101. if (!ass->library) {
  102. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
  103. return AVERROR(EINVAL);
  104. }
  105. ass_set_message_cb(ass->library, ass_log, ctx);
  106. ass->renderer = ass_renderer_init(ass->library);
  107. if (!ass->renderer) {
  108. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
  109. return AVERROR(EINVAL);
  110. }
  111. ass->track = ass_read_file(ass->library, ass->filename, NULL);
  112. if (!ass->track) {
  113. av_log(ctx, AV_LOG_ERROR,
  114. "Could not create a libass track when reading file '%s'\n",
  115. ass->filename);
  116. return AVERROR(EINVAL);
  117. }
  118. ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
  119. return 0;
  120. }
  121. static av_cold void uninit(AVFilterContext *ctx)
  122. {
  123. AssContext *ass = ctx->priv;
  124. av_freep(&ass->filename);
  125. av_freep(&ass->original_size_str);
  126. if (ass->track)
  127. ass_free_track(ass->track);
  128. if (ass->renderer)
  129. ass_renderer_done(ass->renderer);
  130. if (ass->library)
  131. ass_library_done(ass->library);
  132. }
  133. static int query_formats(AVFilterContext *ctx)
  134. {
  135. avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
  136. return 0;
  137. }
  138. static int config_input(AVFilterLink *inlink)
  139. {
  140. AssContext *ass = inlink->dst->priv;
  141. ff_draw_init(&ass->draw, inlink->format, 0);
  142. ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
  143. if (ass->original_w && ass->original_h)
  144. ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
  145. (double)ass->original_w / ass->original_h);
  146. return 0;
  147. }
  148. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  149. /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
  150. #define AR(c) ( (c)>>24)
  151. #define AG(c) (((c)>>16)&0xFF)
  152. #define AB(c) (((c)>>8) &0xFF)
  153. #define AA(c) ((0xFF-c) &0xFF)
  154. static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
  155. const ASS_Image *image)
  156. {
  157. for (; image; image = image->next) {
  158. uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
  159. FFDrawColor color;
  160. ff_draw_color(&ass->draw, &color, rgba_color);
  161. ff_blend_mask(&ass->draw, &color,
  162. picref->data, picref->linesize,
  163. picref->video->w, picref->video->h,
  164. image->bitmap, image->stride, image->w, image->h,
  165. 3, 0, image->dst_x, image->dst_y);
  166. }
  167. }
  168. static void end_frame(AVFilterLink *inlink)
  169. {
  170. AVFilterContext *ctx = inlink->dst;
  171. AVFilterLink *outlink = ctx->outputs[0];
  172. AssContext *ass = ctx->priv;
  173. AVFilterBufferRef *picref = inlink->cur_buf;
  174. int detect_change = 0;
  175. double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
  176. ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
  177. time_ms, &detect_change);
  178. if (detect_change)
  179. av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
  180. overlay_ass_image(ass, picref, image);
  181. avfilter_draw_slice(outlink, 0, picref->video->h, 1);
  182. avfilter_end_frame(outlink);
  183. }
  184. AVFilter avfilter_vf_ass = {
  185. .name = "ass",
  186. .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
  187. .priv_size = sizeof(AssContext),
  188. .init = init,
  189. .uninit = uninit,
  190. .query_formats = query_formats,
  191. .inputs = (const AVFilterPad[]) {
  192. { .name = "default",
  193. .type = AVMEDIA_TYPE_VIDEO,
  194. .get_video_buffer = avfilter_null_get_video_buffer,
  195. .start_frame = avfilter_null_start_frame,
  196. .draw_slice = null_draw_slice,
  197. .end_frame = end_frame,
  198. .config_props = config_input,
  199. .min_perms = AV_PERM_WRITE | AV_PERM_READ,
  200. .rej_perms = AV_PERM_PRESERVE },
  201. { .name = NULL}
  202. },
  203. .outputs = (const AVFilterPad[]) {
  204. { .name = "default",
  205. .type = AVMEDIA_TYPE_VIDEO, },
  206. { .name = NULL}
  207. },
  208. };