]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_ass.c
Port MPlayer fixes for coverity issues in libmpcodecs.
[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 static int null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { return 0; }
161
162 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
163 #define AR(c)  ( (c)>>24)
164 #define AG(c)  (((c)>>16)&0xFF)
165 #define AB(c)  (((c)>>8) &0xFF)
166 #define AA(c)  ((0xFF-c) &0xFF)
167
168 static void overlay_ass_image(AssContext *ass, AVFilterBufferRef *picref,
169                               const ASS_Image *image)
170 {
171     for (; image; image = image->next) {
172         uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
173         FFDrawColor color;
174         ff_draw_color(&ass->draw, &color, rgba_color);
175         ff_blend_mask(&ass->draw, &color,
176                       picref->data, picref->linesize,
177                       picref->video->w, picref->video->h,
178                       image->bitmap, image->stride, image->w, image->h,
179                       3, 0, image->dst_x, image->dst_y);
180     }
181 }
182
183 static int end_frame(AVFilterLink *inlink)
184 {
185     AVFilterContext *ctx = inlink->dst;
186     AVFilterLink *outlink = ctx->outputs[0];
187     AssContext *ass = ctx->priv;
188     AVFilterBufferRef *picref = inlink->cur_buf;
189     int detect_change = 0;
190     double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
191     ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
192                                         time_ms, &detect_change);
193
194     if (detect_change)
195         av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
196
197     overlay_ass_image(ass, picref, image);
198
199     ff_draw_slice(outlink, 0, picref->video->h, 1);
200     return ff_end_frame(outlink);
201 }
202
203 AVFilter avfilter_vf_ass = {
204     .name          = "ass",
205     .description   = NULL_IF_CONFIG_SMALL("Render subtitles onto input video using the libass library."),
206     .priv_size     = sizeof(AssContext),
207     .init          = init,
208     .uninit        = uninit,
209     .query_formats = query_formats,
210
211     .inputs = (const AVFilterPad[]) {
212         { .name             = "default",
213           .type             = AVMEDIA_TYPE_VIDEO,
214           .get_video_buffer = ff_null_get_video_buffer,
215           .start_frame      = ff_null_start_frame,
216           .draw_slice       = null_draw_slice,
217           .end_frame        = end_frame,
218           .config_props     = config_input,
219           .min_perms        = AV_PERM_WRITE | AV_PERM_READ },
220         { .name = NULL}
221     },
222     .outputs = (const AVFilterPad[]) {
223         { .name             = "default",
224           .type             = AVMEDIA_TYPE_VIDEO, },
225         { .name = NULL}
226     },
227     .priv_class = &ass_class,
228 };