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