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;
55 int pix_step[4]; ///< steps per pixel for each plane of the main output
56 int original_w, original_h;
60 #define OFFSET(x) offsetof(AssContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
63 #define COMMON_OPTIONS \
64 {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
65 {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS }, \
66 {"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 }, \
68 /* libass supports a log level ranging from 0 to 7 */
69 static const int ass_libavfilter_log_level_map[] = {
74 AV_LOG_WARNING, /* 4 */
76 AV_LOG_VERBOSE, /* 6 */
80 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
82 int level = ass_libavfilter_log_level_map[ass_level];
84 av_vlog(ctx, level, fmt, args);
85 av_log(ctx, level, "\n");
88 static av_cold int init(AVFilterContext *ctx)
90 AssContext *ass = ctx->priv;
93 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
94 return AVERROR(EINVAL);
97 ass->library = ass_library_init();
99 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
100 return AVERROR(EINVAL);
102 ass_set_message_cb(ass->library, ass_log, ctx);
104 ass->renderer = ass_renderer_init(ass->library);
105 if (!ass->renderer) {
106 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
107 return AVERROR(EINVAL);
110 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
114 static av_cold void uninit(AVFilterContext *ctx)
116 AssContext *ass = ctx->priv;
119 ass_free_track(ass->track);
121 ass_renderer_done(ass->renderer);
123 ass_library_done(ass->library);
126 static int query_formats(AVFilterContext *ctx)
128 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
132 static int config_input(AVFilterLink *inlink)
134 AssContext *ass = inlink->dst->priv;
136 ff_draw_init(&ass->draw, inlink->format, 0);
138 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
139 if (ass->original_w && ass->original_h)
140 ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
141 (double)ass->original_w / ass->original_h);
146 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
147 #define AR(c) ( (c)>>24)
148 #define AG(c) (((c)>>16)&0xFF)
149 #define AB(c) (((c)>>8) &0xFF)
150 #define AA(c) ((0xFF-c) &0xFF)
152 static void overlay_ass_image(AssContext *ass, AVFrame *picref,
153 const ASS_Image *image)
155 for (; image; image = image->next) {
156 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
158 ff_draw_color(&ass->draw, &color, rgba_color);
159 ff_blend_mask(&ass->draw, &color,
160 picref->data, picref->linesize,
161 picref->width, picref->height,
162 image->bitmap, image->stride, image->w, image->h,
163 3, 0, image->dst_x, image->dst_y);
167 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
169 AVFilterContext *ctx = inlink->dst;
170 AVFilterLink *outlink = ctx->outputs[0];
171 AssContext *ass = ctx->priv;
172 int detect_change = 0;
173 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
174 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
175 time_ms, &detect_change);
178 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
180 overlay_ass_image(ass, picref, image);
182 return ff_filter_frame(outlink, picref);
185 static const AVFilterPad ass_inputs[] = {
188 .type = AVMEDIA_TYPE_VIDEO,
189 .filter_frame = filter_frame,
190 .config_props = config_input,
196 static const AVFilterPad ass_outputs[] = {
199 .type = AVMEDIA_TYPE_VIDEO,
204 #if CONFIG_ASS_FILTER
206 static const AVOption ass_options[] = {
211 AVFILTER_DEFINE_CLASS(ass);
213 static av_cold int init_ass(AVFilterContext *ctx)
215 AssContext *ass = ctx->priv;
221 ass->track = ass_read_file(ass->library, ass->filename, NULL);
223 av_log(ctx, AV_LOG_ERROR,
224 "Could not create a libass track when reading file '%s'\n",
226 return AVERROR(EINVAL);
231 AVFilter avfilter_vf_ass = {
233 .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
234 .priv_size = sizeof(AssContext),
237 .query_formats = query_formats,
238 .inputs = ass_inputs,
239 .outputs = ass_outputs,
240 .priv_class = &ass_class,
244 #if CONFIG_SUBTITLES_FILTER
246 static const AVOption subtitles_options[] = {
248 {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
252 AVFILTER_DEFINE_CLASS(subtitles);
254 static av_cold int init_subtitles(AVFilterContext *ctx)
257 AVDictionary *codec_opts = NULL;
258 AVFormatContext *fmt = NULL;
259 AVCodecContext *dec_ctx = NULL;
261 const AVCodecDescriptor *dec_desc;
264 AssContext *ass = ctx->priv;
270 ass->track = ass_new_track(ass->library);
272 av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
273 return AVERROR(EINVAL);
276 /* Open subtitles file */
277 ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
279 av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
282 ret = avformat_find_stream_info(fmt, NULL);
286 /* Locate subtitles stream */
287 ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
289 av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
294 st = fmt->streams[sid];
298 dec = avcodec_find_decoder(dec_ctx->codec_id);
300 av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
301 avcodec_get_name(dec_ctx->codec_id));
302 return AVERROR(EINVAL);
304 dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
305 if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) {
306 av_log(ctx, AV_LOG_ERROR,
307 "Only text based subtitles are currently supported\n");
308 return AVERROR_PATCHWELCOME;
311 av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
312 ret = avcodec_open2(dec_ctx, dec, &codec_opts);
316 /* Decode subtitles and push them into the renderer (libass) */
317 if (dec_ctx->subtitle_header)
318 ass_process_codec_private(ass->track,
319 dec_ctx->subtitle_header,
320 dec_ctx->subtitle_header_size);
321 av_init_packet(&pkt);
324 while (av_read_frame(fmt, &pkt) >= 0) {
326 AVSubtitle sub = {0};
328 if (pkt.stream_index == sid) {
329 ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
331 av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
333 } else if (got_subtitle) {
334 for (i = 0; i < sub.num_rects; i++) {
335 char *ass_line = sub.rects[i]->ass;
338 ass_process_data(ass->track, ass_line, strlen(ass_line));
342 av_free_packet(&pkt);
343 avsubtitle_free(&sub);
347 av_dict_free(&codec_opts);
349 avcodec_close(dec_ctx);
351 avformat_close_input(&fmt);
355 AVFilter avfilter_vf_subtitles = {
357 .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
358 .priv_size = sizeof(AssContext),
359 .init = init_subtitles,
361 .query_formats = query_formats,
362 .inputs = ass_inputs,
363 .outputs = ass_outputs,
364 .priv_class = &subtitles_class,