vf_ass.c 7.2 KB

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