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/pixdesc.h"
34 #include "drawutils.h"
39 ASS_Renderer *renderer;
44 int pix_step[4]; ///< steps per pixel for each plane of the main output
47 /* libass supports a log level ranging from 0 to 7 */
48 int ass_libav_log_level_map[] = {
53 AV_LOG_WARNING, /* 4 */
55 AV_LOG_VERBOSE, /* 6 */
59 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
61 int level = ass_libav_log_level_map[ass_level];
63 av_vlog(ctx, level, fmt, args);
64 av_log(ctx, level, "\n");
67 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
69 AssContext *ass = ctx->priv;
72 ass->filename = av_get_token(&args, ":");
73 if (!ass->filename || !*ass->filename) {
74 av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
75 return AVERROR(EINVAL);
78 ass->library = ass_library_init();
80 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
81 return AVERROR(EINVAL);
83 ass_set_message_cb(ass->library, ass_log, ctx);
85 ass->renderer = ass_renderer_init(ass->library);
87 av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
88 return AVERROR(EINVAL);
91 ass->track = ass_read_file(ass->library, ass->filename, NULL);
93 av_log(ctx, AV_LOG_ERROR,
94 "Could not create a libass track when reading file '%s'\n",
96 return AVERROR(EINVAL);
99 ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
104 static av_cold void uninit(AVFilterContext *ctx)
106 AssContext *ass = ctx->priv;
108 av_freep(&ass->filename);
110 ass_free_track(ass->track);
112 ass_renderer_done(ass->renderer);
114 ass_library_done(ass->library);
117 static int query_formats(AVFilterContext *ctx)
119 static const enum PixelFormat pix_fmts[] = {
120 PIX_FMT_ARGB, PIX_FMT_RGBA,
121 PIX_FMT_ABGR, PIX_FMT_BGRA,
122 PIX_FMT_RGB24, PIX_FMT_BGR24,
126 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
131 static int config_input(AVFilterLink *inlink)
133 AssContext *ass = inlink->dst->priv;
134 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
135 double sar = inlink->sample_aspect_ratio.num ?
136 av_q2d(inlink->sample_aspect_ratio) : 1;
137 double dar = inlink->w / inlink->h * sar;
139 av_image_fill_max_pixsteps(ass->pix_step, NULL, pix_desc);
140 ff_fill_rgba_map(ass->rgba_map, inlink->format);
142 ass->hsub = pix_desc->log2_chroma_w;
143 ass->vsub = pix_desc->log2_chroma_h;
145 ass_set_frame_size (ass->renderer, inlink->w, inlink->h);
146 ass_set_aspect_ratio(ass->renderer, dar, sar);
151 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
158 #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off) { \
159 p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
160 alpha = rgba_color[A] * (val) * 129; \
161 *(p+r_off) = (alpha * rgba_color[R] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
162 *(p+g_off) = (alpha * rgba_color[G] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
163 *(p+b_off) = (alpha * rgba_color[B] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
166 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
167 #define AR(c) ( (c)>>24)
168 #define AG(c) (((c)>>16)&0xFF)
169 #define AB(c) (((c)>>8) &0xFF)
170 #define AA(c) ((0xFF-c) &0xFF)
172 static void overlay_ass_image(AVFilterBufferRef *picref, const uint8_t *rgba_map, const int pix_step,
173 const ASS_Image *image)
178 const int ro = rgba_map[R];
179 const int go = rgba_map[G];
180 const int bo = rgba_map[B];
182 for (; image; image = image->next) {
183 uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
184 unsigned char *row = image->bitmap;
186 for (i = 0; i < image->h; i++) {
187 for (j = 0; j < image->w; j++) {
189 SET_PIXEL_RGB(picref, rgba_color, row[j], image->dst_x + j, image->dst_y + i, pix_step, ro, go, bo);
192 row += image->stride;
197 static void end_frame(AVFilterLink *inlink)
199 AVFilterContext *ctx = inlink->dst;
200 AVFilterLink *outlink = ctx->outputs[0];
201 AssContext *ass = ctx->priv;
202 AVFilterBufferRef *picref = inlink->cur_buf;
203 int detect_change = 0;
204 double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
205 ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
206 time_ms, &detect_change);
209 av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
211 overlay_ass_image(picref, ass->rgba_map, ass->pix_step[0], image);
213 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
214 avfilter_end_frame(outlink);
217 AVFilter avfilter_vf_ass = {
219 .description = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
220 .priv_size = sizeof(AssContext),
223 .query_formats = query_formats,
225 .inputs = (const AVFilterPad[]) {
227 .type = AVMEDIA_TYPE_VIDEO,
228 .get_video_buffer = avfilter_null_get_video_buffer,
229 .start_frame = avfilter_null_start_frame,
230 .draw_slice = null_draw_slice,
231 .end_frame = end_frame,
232 .config_props = config_input,
233 .min_perms = AV_PERM_WRITE | AV_PERM_READ,
234 .rej_perms = AV_PERM_PRESERVE },
237 .outputs = (const AVFilterPad[]) {
239 .type = AVMEDIA_TYPE_VIDEO, },