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