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 {"filename", "set the filename of the ASS file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
58 {"f", "set the filename of the ASS file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
59 {"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 },
63 AVFILTER_DEFINE_CLASS(ass);
65 /* libass supports a log level ranging from 0 to 7 */
66 static const int ass_libavfilter_log_level_map[] = {
71 AV_LOG_WARNING, /* 4 */
73 AV_LOG_VERBOSE, /* 6 */
77 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
79 int level = ass_libavfilter_log_level_map[ass_level];
81 av_vlog(ctx, level, fmt, args);
82 av_log(ctx, level, "\n");
85 static av_cold int init(AVFilterContext *ctx, const char *args)
87 AssContext *ass = ctx->priv;
88 static const char *shorthand[] = { "filename", NULL };
91 ass->class = &ass_class;
92 av_opt_set_defaults(ass);
94 if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
98 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
99 return AVERROR(EINVAL);
102 ass->library = ass_library_init();
104 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
105 return AVERROR(EINVAL);
107 ass_set_message_cb(ass->library, ass_log, ctx);
109 ass->renderer = ass_renderer_init(ass->library);
110 if (!ass->renderer) {
111 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
112 return AVERROR(EINVAL);
115 ass->track = ass_read_file(ass->library, ass->filename, NULL);
117 av_log(ctx, AV_LOG_ERROR,
118 "Could not create a libass track when reading file '%s'\n",
120 return AVERROR(EINVAL);
123 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
127 static av_cold void uninit(AVFilterContext *ctx)
129 AssContext *ass = ctx->priv;
133 ass_free_track(ass->track);
135 ass_renderer_done(ass->renderer);
137 ass_library_done(ass->library);
140 static int query_formats(AVFilterContext *ctx)
142 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
146 static int config_input(AVFilterLink *inlink)
148 AssContext *ass = inlink->dst->priv;
150 ff_draw_init(&ass->draw, inlink->format, 0);
152 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
153 if (ass->original_w && ass->original_h)
154 ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
155 (double)ass->original_w / ass->original_h);
160 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
161 #define AR(c) ( (c)>>24)
162 #define AG(c) (((c)>>16)&0xFF)
163 #define AB(c) (((c)>>8) &0xFF)
164 #define AA(c) ((0xFF-c) &0xFF)
166 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
167 const ASS_Image *image)
169 for (; image; image = image->next) {
170 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
172 ff_draw_color(&ass->draw, &color, rgba_color);
173 ff_blend_mask(&ass->draw, &color,
174 picref->data, picref->linesize,
175 picref->video->w, picref->video->h,
176 image->bitmap, image->stride, image->w, image->h,
177 3, 0, image->dst_x, image->dst_y);
181 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
183 AVFilterContext *ctx = inlink->dst;
184 AVFilterLink *outlink = ctx->outputs[0];
185 AssContext *ass = ctx->priv;
186 int detect_change = 0;
187 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
188 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
189 time_ms, &detect_change);
192 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
194 overlay_ass_image(ass, picref, image);
196 return ff_filter_frame(outlink, picref);
199 static const AVFilterPad ass_inputs[] = {
202 .type = AVMEDIA_TYPE_VIDEO,
203 .filter_frame = filter_frame,
204 .config_props = config_input,
205 .min_perms = AV_PERM_READ | AV_PERM_WRITE,
210 static const AVFilterPad ass_outputs[] = {
213 .type = AVMEDIA_TYPE_VIDEO,
218 AVFilter avfilter_vf_ass = {
220 .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
221 .priv_size = sizeof(AssContext),
224 .query_formats = query_formats,
225 .inputs = ass_inputs,
226 .outputs = ass_outputs,
227 .priv_class = &ass_class,