vf_ass.c 7.6 KB

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