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.

232 lines
7.2KB

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