2 * Copyright (c) 2011 Baptiste Coudurier
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2012 Clément Bœsch
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * Libass subtitles burning filter.
27 * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
33 #if CONFIG_SUBTITLES_FILTER
34 # include "libavcodec/avcodec.h"
35 # include "libavformat/avformat.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/parseutils.h"
41 #include "drawutils.h"
50 ASS_Renderer *renderer;
54 int pix_step[4]; ///< steps per pixel for each plane of the main output
55 int original_w, original_h;
59 #define OFFSET(x) offsetof(AssContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62 static const AVOption options[] = {
63 {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
64 {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
65 {"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 },
69 /* libass supports a log level ranging from 0 to 7 */
70 static const int ass_libavfilter_log_level_map[] = {
75 AV_LOG_WARNING, /* 4 */
77 AV_LOG_VERBOSE, /* 6 */
81 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
83 int level = ass_libavfilter_log_level_map[ass_level];
85 av_vlog(ctx, level, fmt, args);
86 av_log(ctx, level, "\n");
89 static av_cold int init(AVFilterContext *ctx, const char *args, const AVClass *class)
91 AssContext *ass = ctx->priv;
92 static const char *shorthand[] = { "filename", NULL };
96 av_opt_set_defaults(ass);
98 if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
101 if (!ass->filename) {
102 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
103 return AVERROR(EINVAL);
106 ass->library = ass_library_init();
108 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
109 return AVERROR(EINVAL);
111 ass_set_message_cb(ass->library, ass_log, ctx);
113 ass->renderer = ass_renderer_init(ass->library);
114 if (!ass->renderer) {
115 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
116 return AVERROR(EINVAL);
119 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
123 static av_cold void uninit(AVFilterContext *ctx)
125 AssContext *ass = ctx->priv;
129 ass_free_track(ass->track);
131 ass_renderer_done(ass->renderer);
133 ass_library_done(ass->library);
136 static int query_formats(AVFilterContext *ctx)
138 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
142 static int config_input(AVFilterLink *inlink)
144 AssContext *ass = inlink->dst->priv;
146 ff_draw_init(&ass->draw, inlink->format, 0);
148 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
149 if (ass->original_w && ass->original_h)
150 ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
151 (double)ass->original_w / ass->original_h);
156 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
157 #define AR(c) ( (c)>>24)
158 #define AG(c) (((c)>>16)&0xFF)
159 #define AB(c) (((c)>>8) &0xFF)
160 #define AA(c) ((0xFF-c) &0xFF)
162 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
163 const ASS_Image *image)
165 for (; image; image = image->next) {
166 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
168 ff_draw_color(&ass->draw, &color, rgba_color);
169 ff_blend_mask(&ass->draw, &color,
170 picref->data, picref->linesize,
171 picref->video->w, picref->video->h,
172 image->bitmap, image->stride, image->w, image->h,
173 3, 0, image->dst_x, image->dst_y);
177 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
179 AVFilterContext *ctx = inlink->dst;
180 AVFilterLink *outlink = ctx->outputs[0];
181 AssContext *ass = ctx->priv;
182 int detect_change = 0;
183 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
184 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
185 time_ms, &detect_change);
188 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
190 overlay_ass_image(ass, picref, image);
192 return ff_filter_frame(outlink, picref);
195 static const AVFilterPad ass_inputs[] = {
198 .type = AVMEDIA_TYPE_VIDEO,
199 .filter_frame = filter_frame,
200 .config_props = config_input,
201 .min_perms = AV_PERM_READ | AV_PERM_WRITE,
206 static const AVFilterPad ass_outputs[] = {
209 .type = AVMEDIA_TYPE_VIDEO,
214 #if CONFIG_ASS_FILTER
216 #define ass_options options
217 AVFILTER_DEFINE_CLASS(ass);
219 static av_cold int init_ass(AVFilterContext *ctx, const char *args)
221 AssContext *ass = ctx->priv;
222 int ret = init(ctx, args, &ass_class);
227 ass->track = ass_read_file(ass->library, ass->filename, NULL);
229 av_log(ctx, AV_LOG_ERROR,
230 "Could not create a libass track when reading file '%s'\n",
232 return AVERROR(EINVAL);
237 AVFilter avfilter_vf_ass = {
239 .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
240 .priv_size = sizeof(AssContext),
243 .query_formats = query_formats,
244 .inputs = ass_inputs,
245 .outputs = ass_outputs,
246 .priv_class = &ass_class,
250 #if CONFIG_SUBTITLES_FILTER
252 #define subtitles_options options
253 AVFILTER_DEFINE_CLASS(subtitles);
255 static av_cold int init_subtitles(AVFilterContext *ctx, const char *args)
258 AVFormatContext *fmt = NULL;
259 AVCodecContext *dec_ctx = NULL;
263 AssContext *ass = ctx->priv;
266 ret = init(ctx, args, &subtitles_class);
269 ass->track = ass_new_track(ass->library);
271 av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
272 return AVERROR(EINVAL);
275 /* Open subtitles file */
276 ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
278 av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
281 ret = avformat_find_stream_info(fmt, NULL);
285 /* Locate subtitles stream */
286 ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
288 av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
293 st = fmt->streams[sid];
297 dec = avcodec_find_decoder(dec_ctx->codec_id);
299 av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
300 avcodec_get_name(dec_ctx->codec_id));
301 return AVERROR(EINVAL);
303 ret = avcodec_open2(dec_ctx, dec, NULL);
307 /* Decode subtitles and push them into the renderer (libass) */
308 if (dec_ctx->subtitle_header)
309 ass_process_codec_private(ass->track,
310 dec_ctx->subtitle_header,
311 dec_ctx->subtitle_header_size);
312 av_init_packet(&pkt);
315 while (av_read_frame(fmt, &pkt) >= 0) {
319 if (pkt.stream_index == sid) {
320 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
321 if (ret < 0 || !got_subtitle)
323 for (i = 0; i < sub.num_rects; i++) {
324 char *ass_line = sub.rects[i]->ass;
327 ass_process_data(ass->track, ass_line, strlen(ass_line));
330 av_free_packet(&pkt);
331 avsubtitle_free(&sub);
336 avcodec_close(dec_ctx);
338 avformat_close_input(&fmt);
342 AVFilter avfilter_vf_subtitles = {
344 .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
345 .priv_size = sizeof(AssContext),
346 .init = init_subtitles,
348 .query_formats = query_formats,
349 .inputs = ass_inputs,
350 .outputs = ass_outputs,
351 .priv_class = &subtitles_class,