2 * Copyright (c) 2011 Baptiste Coudurier
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of FFmpeg.
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.
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.
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
24 * Libass subtitles burning filter.
26 * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
31 #include "libavutil/avstring.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/parseutils.h"
35 #include "drawutils.h"
44 ASS_Renderer *renderer;
48 int pix_step[4]; ///< steps per pixel for each plane of the main output
49 int original_w, original_h;
53 #define OFFSET(x) offsetof(AssContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
56 static const AVOption ass_options[] = {
57 {"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 },
61 AVFILTER_DEFINE_CLASS(ass);
63 /* libass supports a log level ranging from 0 to 7 */
64 static const int ass_libavfilter_log_level_map[] = {
69 AV_LOG_WARNING, /* 4 */
71 AV_LOG_VERBOSE, /* 6 */
75 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
77 int level = ass_libavfilter_log_level_map[ass_level];
79 av_vlog(ctx, level, fmt, args);
80 av_log(ctx, level, "\n");
83 static av_cold int init(AVFilterContext *ctx, const char *args)
85 AssContext *ass = ctx->priv;
88 ass->class = &ass_class;
89 av_opt_set_defaults(ass);
92 ass->filename = av_get_token(&args, ":");
93 if (!ass->filename || !*ass->filename) {
94 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
95 return AVERROR(EINVAL);
98 if (*args++ == ':' && (ret = av_set_options_string(ass, args, "=", ":")) < 0)
101 ass->library = ass_library_init();
103 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
104 return AVERROR(EINVAL);
106 ass_set_message_cb(ass->library, ass_log, ctx);
108 ass->renderer = ass_renderer_init(ass->library);
109 if (!ass->renderer) {
110 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
111 return AVERROR(EINVAL);
114 ass->track = ass_read_file(ass->library, ass->filename, NULL);
116 av_log(ctx, AV_LOG_ERROR,
117 "Could not create a libass track when reading file '%s'\n",
119 return AVERROR(EINVAL);
122 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
126 static av_cold void uninit(AVFilterContext *ctx)
128 AssContext *ass = ctx->priv;
130 av_freep(&ass->filename);
132 ass_free_track(ass->track);
134 ass_renderer_done(ass->renderer);
136 ass_library_done(ass->library);
139 static int query_formats(AVFilterContext *ctx)
141 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
145 static int config_input(AVFilterLink *inlink)
147 AssContext *ass = inlink->dst->priv;
149 ff_draw_init(&ass->draw, inlink->format, 0);
151 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
152 if (ass->original_w && ass->original_h)
153 ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
154 (double)ass->original_w / ass->original_h);
159 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
161 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
162 #define AR(c) ( (c)>>24)
163 #define AG(c) (((c)>>16)&0xFF)
164 #define AB(c) (((c)>>8) &0xFF)
165 #define AA(c) ((0xFF-c) &0xFF)
167 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
168 const ASS_Image *image)
170 for (; image; image = image->next) {
171 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
173 ff_draw_color(&ass->draw, &color, rgba_color);
174 ff_blend_mask(&ass->draw, &color,
175 picref->data, picref->linesize,
176 picref->video->w, picref->video->h,
177 image->bitmap, image->stride, image->w, image->h,
178 3, 0, image->dst_x, image->dst_y);
182 static int end_frame(AVFilterLink *inlink)
184 AVFilterContext *ctx = inlink->dst;
185 AVFilterLink *outlink = ctx->outputs[0];
186 AssContext *ass = ctx->priv;
187 AVFilterBufferRef *picref = inlink->cur_buf;
188 int detect_change = 0;
189 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
190 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
191 time_ms, &detect_change);
194 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
196 overlay_ass_image(ass, picref, image);
198 ff_draw_slice(outlink, 0, picref->video->h, 1);
199 return ff_end_frame(outlink);
202 AVFilter avfilter_vf_ass = {
204 .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
205 .priv_size = sizeof(AssContext),
208 .query_formats = query_formats,
210 .inputs = (const AVFilterPad[]) {
212 .type = AVMEDIA_TYPE_VIDEO,
213 .get_video_buffer = ff_null_get_video_buffer,
214 .start_frame = ff_null_start_frame,
215 .draw_slice = null_draw_slice,
216 .end_frame = end_frame,
217 .config_props = config_input,
218 .min_perms = AV_PERM_WRITE | AV_PERM_READ },
221 .outputs = (const AVFilterPad[]) {
223 .type = AVMEDIA_TYPE_VIDEO, },
226 .priv_class = &ass_class,