]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_ass.c
lavfi/blackdetect: switch to new ff_filter_frame() API
[ffmpeg] / libavfilter / vf_ass.c
1 /*
2  * Copyright (c) 2011 Baptiste Coudurier
3  * Copyright (c) 2011 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * Libass subtitles burning filter.
25  *
26  * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
27  */
28
29 #include <ass/ass.h>
30
31 #include "libavutil/avstring.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/parseutils.h"
35 #include "drawutils.h"
36 #include "avfilter.h"
37 #include "internal.h"
38 #include "formats.h"
39 #include "video.h"
40
41 typedef struct {
42     const AVClass *class;
43     ASS_Library  *library;
44     ASS_Renderer *renderer;
45     ASS_Track    *track;
46     char *filename;
47     uint8_t rgba_map[4];
48     int     pix_step[4];       ///< steps per pixel for each plane of the main output
49     int original_w, original_h;
50     FFDrawContext draw;
51 } AssContext;
52
53 #define OFFSET(x) offsetof(AssContext, x)
54 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
55
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 },
60     {NULL},
61 };
62
63 AVFILTER_DEFINE_CLASS(ass);
64
65 /* libass supports a log level ranging from 0 to 7 */
66 static const int ass_libavfilter_log_level_map[] = {
67     AV_LOG_QUIET,               /* 0 */
68     AV_LOG_PANIC,               /* 1 */
69     AV_LOG_FATAL,               /* 2 */
70     AV_LOG_ERROR,               /* 3 */
71     AV_LOG_WARNING,             /* 4 */
72     AV_LOG_INFO,                /* 5 */
73     AV_LOG_VERBOSE,             /* 6 */
74     AV_LOG_DEBUG,               /* 7 */
75 };
76
77 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
78 {
79     int level = ass_libavfilter_log_level_map[ass_level];
80
81     av_vlog(ctx, level, fmt, args);
82     av_log(ctx, level, "\n");
83 }
84
85 static av_cold int init(AVFilterContext *ctx, const char *args)
86 {
87     AssContext *ass = ctx->priv;
88     static const char *shorthand[] = { "filename", NULL };
89     int ret;
90
91     ass->class = &ass_class;
92     av_opt_set_defaults(ass);
93
94     if ((ret = av_opt_set_from_string(ass, args, shorthand, "=", ":")) < 0)
95         return ret;
96
97     if (!ass->filename) {
98         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
99         return AVERROR(EINVAL);
100     }
101
102     ass->library = ass_library_init();
103     if (!ass->library) {
104         av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
105         return AVERROR(EINVAL);
106     }
107     ass_set_message_cb(ass->library, ass_log, ctx);
108
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);
113     }
114
115     ass->track = ass_read_file(ass->library, ass->filename, NULL);
116     if (!ass->track) {
117         av_log(ctx, AV_LOG_ERROR,
118                "Could not create a libass track when reading file '%s'\n",
119                ass->filename);
120         return AVERROR(EINVAL);
121     }
122
123     ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
124     return 0;
125 }
126
127 static av_cold void uninit(AVFilterContext *ctx)
128 {
129     AssContext *ass = ctx->priv;
130
131     av_opt_free(ass);
132     if (ass->track)
133         ass_free_track(ass->track);
134     if (ass->renderer)
135         ass_renderer_done(ass->renderer);
136     if (ass->library)
137         ass_library_done(ass->library);
138 }
139
140 static int query_formats(AVFilterContext *ctx)
141 {
142     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
143     return 0;
144 }
145
146 static int config_input(AVFilterLink *inlink)
147 {
148     AssContext *ass = inlink->dst->priv;
149
150     ff_draw_init(&ass->draw, inlink->format, 0);
151
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);
156
157     return 0;
158 }
159
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)
165
166 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
167                               const ASS_Image *image)
168 {
169     for (; image; image = image->next) {
170         uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
171         FFDrawColor 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);
178     }
179 }
180
181 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
182 {
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);
190
191     if (detect_change)
192         av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
193
194     overlay_ass_image(ass, picref, image);
195
196     return ff_filter_frame(outlink, picref);
197 }
198
199 static const AVFilterPad ass_inputs[] = {
200     {
201         .name             = "default",
202         .type             = AVMEDIA_TYPE_VIDEO,
203         .filter_frame     = filter_frame,
204         .config_props     = config_input,
205         .min_perms        = AV_PERM_READ | AV_PERM_WRITE,
206     },
207     { NULL }
208 };
209
210 static const AVFilterPad ass_outputs[] = {
211     {
212         .name = "default",
213         .type = AVMEDIA_TYPE_VIDEO,
214     },
215     { NULL }
216 };
217
218 AVFilter avfilter_vf_ass = {
219     .name          = "ass",
220     .description   = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
221     .priv_size     = sizeof(AssContext),
222     .init          = init,
223     .uninit        = uninit,
224     .query_formats = query_formats,
225     .inputs        = ass_inputs,
226     .outputs       = ass_outputs,
227     .priv_class    = &ass_class,
228 };