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.

282 lines
8.8KB

  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 "libavutil/pixdesc.h"
  33. #include "drawutils.h"
  34. #include "avfilter.h"
  35. typedef struct {
  36. const AVClass *class;
  37. ASS_Library *library;
  38. ASS_Renderer *renderer;
  39. ASS_Track *track;
  40. int hsub, vsub;
  41. char *filename;
  42. uint8_t rgba_map[4];
  43. int pix_step[4]; ///< steps per pixel for each plane of the main output
  44. char *original_size_str;
  45. int original_w, original_h;
  46. } AssContext;
  47. #define OFFSET(x) offsetof(AssContext, x)
  48. static const AVOption ass_options[] = {
  49. {"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 },
  50. {NULL},
  51. };
  52. static const char *ass_get_name(void *ctx)
  53. {
  54. return "ass";
  55. }
  56. static const AVClass ass_class = {
  57. "AssContext",
  58. ass_get_name,
  59. ass_options
  60. };
  61. /* libass supports a log level ranging from 0 to 7 */
  62. int ass_libav_log_level_map[] = {
  63. AV_LOG_QUIET, /* 0 */
  64. AV_LOG_PANIC, /* 1 */
  65. AV_LOG_FATAL, /* 2 */
  66. AV_LOG_ERROR, /* 3 */
  67. AV_LOG_WARNING, /* 4 */
  68. AV_LOG_INFO, /* 5 */
  69. AV_LOG_VERBOSE, /* 6 */
  70. AV_LOG_DEBUG, /* 7 */
  71. };
  72. static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
  73. {
  74. int level = ass_libav_log_level_map[ass_level];
  75. av_vlog(ctx, level, fmt, args);
  76. av_log(ctx, level, "\n");
  77. }
  78. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  79. {
  80. AssContext *ass = ctx->priv;
  81. int ret;
  82. ass->class = &ass_class;
  83. av_opt_set_defaults(ass);
  84. if (args)
  85. ass->filename = av_get_token(&args, ":");
  86. if (!ass->filename || !*ass->filename) {
  87. av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
  88. return AVERROR(EINVAL);
  89. }
  90. if (*args++ == ':' && (ret = av_set_options_string(ass, args, "=", ":")) < 0) {
  91. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  92. return ret;
  93. }
  94. if (ass->original_size_str &&
  95. av_parse_video_size(&ass->original_w, &ass->original_h,
  96. ass->original_size_str) < 0) {
  97. av_log(ctx, AV_LOG_ERROR,
  98. "Invalid original size '%s'.\n", ass->original_size_str);
  99. return AVERROR(EINVAL);
  100. }
  101. ass->library = ass_library_init();
  102. if (!ass->library) {
  103. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
  104. return AVERROR(EINVAL);
  105. }
  106. ass_set_message_cb(ass->library, ass_log, ctx);
  107. ass->renderer = ass_renderer_init(ass->library);
  108. if (!ass->renderer) {
  109. av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
  110. return AVERROR(EINVAL);
  111. }
  112. ass->track = ass_read_file(ass->library, ass->filename, NULL);
  113. if (!ass->track) {
  114. av_log(ctx, AV_LOG_ERROR,
  115. "Could not create a libass track when reading file '%s'\n",
  116. ass->filename);
  117. return AVERROR(EINVAL);
  118. }
  119. ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
  120. return 0;
  121. }
  122. static av_cold void uninit(AVFilterContext *ctx)
  123. {
  124. AssContext *ass = ctx->priv;
  125. av_freep(&ass->filename);
  126. av_freep(&ass->original_size_str);
  127. if (ass->track)
  128. ass_free_track(ass->track);
  129. if (ass->renderer)
  130. ass_renderer_done(ass->renderer);
  131. if (ass->library)
  132. ass_library_done(ass->library);
  133. }
  134. static int query_formats(AVFilterContext *ctx)
  135. {
  136. static const enum PixelFormat pix_fmts[] = {
  137. PIX_FMT_ARGB, PIX_FMT_RGBA,
  138. PIX_FMT_ABGR, PIX_FMT_BGRA,
  139. PIX_FMT_RGB24, PIX_FMT_BGR24,
  140. PIX_FMT_NONE
  141. };
  142. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  143. return 0;
  144. }
  145. static int config_input(AVFilterLink *inlink)
  146. {
  147. AssContext *ass = inlink->dst->priv;
  148. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
  149. av_image_fill_max_pixsteps(ass->pix_step, NULL, pix_desc);
  150. ff_fill_rgba_map(ass->rgba_map, inlink->format);
  151. ass->hsub = pix_desc->log2_chroma_w;
  152. ass->vsub = pix_desc->log2_chroma_h;
  153. ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
  154. if (ass->original_w && ass->original_h)
  155. ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
  156. (double)ass->original_w / ass->original_h);
  157. return 0;
  158. }
  159. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  160. #define R 0
  161. #define G 1
  162. #define B 2
  163. #define A 3
  164. #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off) { \
  165. p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
  166. alpha = rgba_color[A] * (val) * 129; \
  167. *(p+r_off) = (alpha * rgba_color[R] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
  168. *(p+g_off) = (alpha * rgba_color[G] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
  169. *(p+b_off) = (alpha * rgba_color[B] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
  170. }
  171. /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
  172. #define AR(c) ( (c)>>24)
  173. #define AG(c) (((c)>>16)&0xFF)
  174. #define AB(c) (((c)>>8) &0xFF)
  175. #define AA(c) ((0xFF-c) &0xFF)
  176. static void overlay_ass_image(AVFilterBufferRef *picref, const uint8_t *rgba_map, const int pix_step,
  177. const ASS_Image *image)
  178. {
  179. int i, j;
  180. int alpha;
  181. uint8_t *p;
  182. const int ro = rgba_map[R];
  183. const int go = rgba_map[G];
  184. const int bo = rgba_map[B];
  185. for (; image; image = image->next) {
  186. uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
  187. unsigned char *row = image->bitmap;
  188. for (i = 0; i < image->h; i++) {
  189. for (j = 0; j < image->w; j++) {
  190. if (row[j]) {
  191. SET_PIXEL_RGB(picref, rgba_color, row[j], image->dst_x + j, image->dst_y + i, pix_step, ro, go, bo);
  192. }
  193. }
  194. row += image->stride;
  195. }
  196. }
  197. }
  198. static void end_frame(AVFilterLink *inlink)
  199. {
  200. AVFilterContext *ctx = inlink->dst;
  201. AVFilterLink *outlink = ctx->outputs[0];
  202. AssContext *ass = ctx->priv;
  203. AVFilterBufferRef *picref = inlink->cur_buf;
  204. int detect_change = 0;
  205. double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
  206. ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
  207. time_ms, &detect_change);
  208. if (detect_change)
  209. av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
  210. overlay_ass_image(picref, ass->rgba_map, ass->pix_step[0], image);
  211. avfilter_draw_slice(outlink, 0, picref->video->h, 1);
  212. avfilter_end_frame(outlink);
  213. }
  214. AVFilter avfilter_vf_ass = {
  215. .name = "ass",
  216. .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
  217. .priv_size = sizeof(AssContext),
  218. .init = init,
  219. .uninit = uninit,
  220. .query_formats = query_formats,
  221. .inputs = (const AVFilterPad[]) {
  222. { .name = "default",
  223. .type = AVMEDIA_TYPE_VIDEO,
  224. .get_video_buffer = avfilter_null_get_video_buffer,
  225. .start_frame = avfilter_null_start_frame,
  226. .draw_slice = null_draw_slice,
  227. .end_frame = end_frame,
  228. .config_props = config_input,
  229. .min_perms = AV_PERM_WRITE | AV_PERM_READ,
  230. .rej_perms = AV_PERM_PRESERVE },
  231. { .name = NULL}
  232. },
  233. .outputs = (const AVFilterPad[]) {
  234. { .name = "default",
  235. .type = AVMEDIA_TYPE_VIDEO, },
  236. { .name = NULL}
  237. },
  238. };