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.

229 lines
7.4KB

  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. #include "internal.h"
  35. #include "formats.h"
  36. #include "video.h"
  37. typedef struct {
  38. const AVClass *class;
  39. ASS_Library *library;
  40. ASS_Renderer *renderer;
  41. ASS_Track *track;
  42. char *filename;
  43. uint8_t rgba_map[4];
  44. int pix_step[4]; ///< steps per pixel for each plane of the main output
  45. int original_w, original_h;
  46. FFDrawContext draw;
  47. } AssContext;
  48. #define OFFSET(x) offsetof(AssContext, x)
  49. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  50. static const AVOption ass_options[] = {
  51. {"filename", "set the filename of the ASS file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  52. {"f", "set the filename of the ASS file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  53. {"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, FLAGS },
  54. {NULL},
  55. };
  56. AVFILTER_DEFINE_CLASS(ass);
  57. /* libass supports a log level ranging from 0 to 7 */
  58. static const int ass_libavfilter_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_libavfilter_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)
  75. {
  76. AssContext *ass = ctx->priv;
  77. static const char *shorthand[] = { "filename", NULL };
  78. int ret;
  79. ass->class = &ass_class;
  80. av_opt_set_defaults(ass);
  81. if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
  82. return ret;
  83. if (!ass->filename) {
  84. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  85. return AVERROR(EINVAL);
  86. }
  87. ass->library = ass_library_init();
  88. if (!ass->library) {
  89. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
  90. return AVERROR(EINVAL);
  91. }
  92. ass_set_message_cb(ass->library, ass_log, ctx);
  93. ass->renderer = ass_renderer_init(ass->library);
  94. if (!ass->renderer) {
  95. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
  96. return AVERROR(EINVAL);
  97. }
  98. ass->track = ass_read_file(ass->library, ass->filename, NULL);
  99. if (!ass->track) {
  100. av_log(ctx, AV_LOG_ERROR,
  101. "Could not create a libass track when reading file '%s'\n",
  102. ass->filename);
  103. return AVERROR(EINVAL);
  104. }
  105. ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
  106. return 0;
  107. }
  108. static av_cold void uninit(AVFilterContext *ctx)
  109. {
  110. AssContext *ass = ctx->priv;
  111. av_opt_free(ass);
  112. if (ass->track)
  113. ass_free_track(ass->track);
  114. if (ass->renderer)
  115. ass_renderer_done(ass->renderer);
  116. if (ass->library)
  117. ass_library_done(ass->library);
  118. }
  119. static int query_formats(AVFilterContext *ctx)
  120. {
  121. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  122. return 0;
  123. }
  124. static int config_input(AVFilterLink *inlink)
  125. {
  126. AssContext *ass = inlink->dst->priv;
  127. ff_draw_init(&ass->draw, inlink->format, 0);
  128. ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
  129. if (ass->original_w && ass->original_h)
  130. ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
  131. (double)ass->original_w / ass->original_h);
  132. return 0;
  133. }
  134. static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
  135. /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
  136. #define AR(c) ( (c)>>24)
  137. #define AG(c) (((c)>>16)&0xFF)
  138. #define AB(c) (((c)>>8) &0xFF)
  139. #define AA(c) ((0xFF-c) &0xFF)
  140. static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
  141. const ASS_Image *image)
  142. {
  143. for (; image; image = image->next) {
  144. uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
  145. FFDrawColor color;
  146. ff_draw_color(&ass->draw, &color, rgba_color);
  147. ff_blend_mask(&ass->draw, &color,
  148. picref->data, picref->linesize,
  149. picref->video->w, picref->video->h,
  150. image->bitmap, image->stride, image->w, image->h,
  151. 3, 0, image->dst_x, image->dst_y);
  152. }
  153. }
  154. static int end_frame(AVFilterLink *inlink)
  155. {
  156. AVFilterContext *ctx = inlink->dst;
  157. AVFilterLink *outlink = ctx->outputs[0];
  158. AssContext *ass = ctx->priv;
  159. AVFilterBufferRef *picref = inlink->cur_buf;
  160. int detect_change = 0;
  161. double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
  162. ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
  163. time_ms, &detect_change);
  164. if (detect_change)
  165. av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
  166. overlay_ass_image(ass, picref, image);
  167. ff_draw_slice(outlink, 0, picref->video->h, 1);
  168. return ff_end_frame(outlink);
  169. }
  170. AVFilter avfilter_vf_ass = {
  171. .name = "ass",
  172. .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
  173. .priv_size = sizeof(AssContext),
  174. .init = init,
  175. .uninit = uninit,
  176. .query_formats = query_formats,
  177. .inputs = (const AVFilterPad[]) {
  178. { .name = "default",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .get_video_buffer = ff_null_get_video_buffer,
  181. .start_frame = ff_null_start_frame,
  182. .draw_slice = null_draw_slice,
  183. .end_frame = end_frame,
  184. .config_props = config_input,
  185. .min_perms = AV_PERM_WRITE | AV_PERM_READ },
  186. { .name = NULL}
  187. },
  188. .outputs = (const AVFilterPad[]) {
  189. { .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO, },
  191. { .name = NULL}
  192. },
  193. .priv_class = &ass_class,
  194. };