]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_ass.c
Merge remote-tracking branch 'qatar/master'
[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
38 typedef struct {
39     const AVClass *class;
40     ASS_Library  *library;
41     ASS_Renderer *renderer;
42     ASS_Track    *track;
43     char *filename;
44     uint8_t rgba_map[4];
45     int     pix_step[4];       ///< steps per pixel for each plane of the main output
46     char *original_size_str;
47     int original_w, original_h;
48     FFDrawContext draw;
49 } AssContext;
50
51 #define OFFSET(x) offsetof(AssContext, x)
52
53 static const AVOption ass_options[] = {
54     {"original_size",  "set the size of the original video (used to scale fonts)", OFFSET(original_size_str), AV_OPT_TYPE_STRING, {.str = NULL},  CHAR_MIN, CHAR_MAX },
55     {NULL},
56 };
57
58 static const char *ass_get_name(void *ctx)
59 {
60     return "ass";
61 }
62
63 static const AVClass ass_class = {
64     "AssContext",
65     ass_get_name,
66     ass_options
67 };
68
69 /* libass supports a log level ranging from 0 to 7 */
70 int ass_libav_log_level_map[] = {
71     AV_LOG_QUIET,               /* 0 */
72     AV_LOG_PANIC,               /* 1 */
73     AV_LOG_FATAL,               /* 2 */
74     AV_LOG_ERROR,               /* 3 */
75     AV_LOG_WARNING,             /* 4 */
76     AV_LOG_INFO,                /* 5 */
77     AV_LOG_VERBOSE,             /* 6 */
78     AV_LOG_DEBUG,               /* 7 */
79 };
80
81 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
82 {
83     int level = ass_libav_log_level_map[ass_level];
84
85     av_vlog(ctx, level, fmt, args);
86     av_log(ctx, level, "\n");
87 }
88
89 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
90 {
91     AssContext *ass = ctx->priv;
92     int ret;
93
94     ass->class = &ass_class;
95     av_opt_set_defaults(ass);
96
97     if (args)
98         ass->filename = av_get_token(&args, ":");
99     if (!ass->filename || !*ass->filename) {
100         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
101         return AVERROR(EINVAL);
102     }
103
104     if (*args++ == ':' && (ret = av_set_options_string(ass, args, "=", ":")) < 0) {
105         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
106         return ret;
107     }
108
109     if (ass->original_size_str &&
110         av_parse_video_size(&ass->original_w, &ass->original_h,
111                             ass->original_size_str) < 0) {
112         av_log(ctx, AV_LOG_ERROR,
113                "Invalid original size '%s'.\n", ass->original_size_str);
114         return AVERROR(EINVAL);
115     }
116
117     ass->library = ass_library_init();
118     if (!ass->library) {
119         av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
120         return AVERROR(EINVAL);
121     }
122     ass_set_message_cb(ass->library, ass_log, ctx);
123
124     ass->renderer = ass_renderer_init(ass->library);
125     if (!ass->renderer) {
126         av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
127         return AVERROR(EINVAL);
128     }
129
130     ass->track = ass_read_file(ass->library, ass->filename, NULL);
131     if (!ass->track) {
132         av_log(ctx, AV_LOG_ERROR,
133                "Could not create a libass track when reading file '%s'\n",
134                ass->filename);
135         return AVERROR(EINVAL);
136     }
137
138     ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
139     return 0;
140 }
141
142 static av_cold void uninit(AVFilterContext *ctx)
143 {
144     AssContext *ass = ctx->priv;
145
146     av_freep(&ass->filename);
147     av_freep(&ass->original_size_str);
148     if (ass->track)
149         ass_free_track(ass->track);
150     if (ass->renderer)
151         ass_renderer_done(ass->renderer);
152     if (ass->library)
153         ass_library_done(ass->library);
154 }
155
156 static int query_formats(AVFilterContext *ctx)
157 {
158     avfilter_set_common_pixel_formats(ctx, ff_draw_supported_pixel_formats(0));
159     return 0;
160 }
161
162 static int config_input(AVFilterLink *inlink)
163 {
164     AssContext *ass = inlink->dst->priv;
165
166     ff_draw_init(&ass->draw, inlink->format, 0);
167
168     ass_set_frame_size  (ass->renderer, inlink->w, inlink->h);
169     if (ass->original_w && ass->original_h)
170         ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
171                              (double)ass->original_w / ass->original_h);
172
173     return 0;
174 }
175
176 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
177
178 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
179 #define AR(c)  ( (c)>>24)
180 #define AG(c)  (((c)>>16)&0xFF)
181 #define AB(c)  (((c)>>8) &0xFF)
182 #define AA(c)  ((0xFF-c) &0xFF)
183
184 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
185                               const ASS_Image *image)
186 {
187     for (; image; image = image->next) {
188         uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
189         FFDrawColor color;
190         ff_draw_color(&ass->draw, &color, rgba_color);
191         ff_blend_mask(&ass->draw, &color,
192                       picref->data, picref->linesize,
193                       picref->video->w, picref->video->h,
194                       image->bitmap, image->stride, image->w, image->h,
195                       3, 0, image->dst_x, image->dst_y);
196     }
197 }
198
199 static void end_frame(AVFilterLink *inlink)
200 {
201     AVFilterContext *ctx = inlink->dst;
202     AVFilterLink *outlink = ctx->outputs[0];
203     AssContext *ass = ctx->priv;
204     AVFilterBufferRef *picref = inlink->cur_buf;
205     int detect_change = 0;
206     double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
207     ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
208                                         time_ms, &detect_change);
209
210     if (detect_change)
211         av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
212
213     overlay_ass_image(ass, picref, image);
214
215     avfilter_draw_slice(outlink, 0, picref->video->h, 1);
216     avfilter_end_frame(outlink);
217 }
218
219 AVFilter avfilter_vf_ass = {
220     .name          = "ass",
221     .description   = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
222     .priv_size     = sizeof(AssContext),
223     .init          = init,
224     .uninit        = uninit,
225     .query_formats = query_formats,
226
227     .inputs = (const AVFilterPad[]) {
228         { .name             = "default",
229           .type             = AVMEDIA_TYPE_VIDEO,
230           .get_video_buffer = avfilter_null_get_video_buffer,
231           .start_frame      = avfilter_null_start_frame,
232           .draw_slice       = null_draw_slice,
233           .end_frame        = end_frame,
234           .config_props     = config_input,
235           .min_perms        = AV_PERM_WRITE | AV_PERM_READ,
236           .rej_perms        = AV_PERM_PRESERVE },
237         { .name = NULL}
238     },
239     .outputs = (const AVFilterPad[]) {
240         { .name             = "default",
241           .type             = AVMEDIA_TYPE_VIDEO, },
242         { .name = NULL}
243     },
244 };