]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_subtitles.c
Merge commit '1c77ead1fb14d64e6798fc5e32af04abcb3cebb4'
[ffmpeg] / libavfilter / vf_subtitles.c
1 /*
2  * Copyright (c) 2011 Baptiste Coudurier
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2012 Clément Bœsch
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * Libass subtitles burning filter.
26  *
27  * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html}
28  */
29
30 #include <ass/ass.h>
31
32 #include "config.h"
33 #if CONFIG_SUBTITLES_FILTER
34 # include "libavcodec/avcodec.h"
35 # include "libavformat/avformat.h"
36 #endif
37 #include "libavutil/avstring.h"
38 #include "libavutil/imgutils.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/parseutils.h"
41 #include "drawutils.h"
42 #include "avfilter.h"
43 #include "internal.h"
44 #include "formats.h"
45 #include "video.h"
46
47 typedef struct {
48     const AVClass *class;
49     ASS_Library  *library;
50     ASS_Renderer *renderer;
51     ASS_Track    *track;
52     char *filename;
53     char *charenc;
54     int stream_index;
55     uint8_t rgba_map[4];
56     int     pix_step[4];       ///< steps per pixel for each plane of the main output
57     int original_w, original_h;
58     int shaping;
59     FFDrawContext draw;
60 } AssContext;
61
62 #define OFFSET(x) offsetof(AssContext, x)
63 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
64
65 #define COMMON_OPTIONS \
66     {"filename",       "set the filename of file to read",                         OFFSET(filename),   AV_OPT_TYPE_STRING,     {.str = NULL},  CHAR_MIN, CHAR_MAX, FLAGS }, \
67     {"f",              "set the filename of file to read",                         OFFSET(filename),   AV_OPT_TYPE_STRING,     {.str = NULL},  CHAR_MIN, CHAR_MAX, FLAGS }, \
68     {"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 }, \
69
70 /* libass supports a log level ranging from 0 to 7 */
71 static const int ass_libavfilter_log_level_map[] = {
72     [0] = AV_LOG_FATAL,     /* MSGL_FATAL */
73     [1] = AV_LOG_ERROR,     /* MSGL_ERR */
74     [2] = AV_LOG_WARNING,   /* MSGL_WARN */
75     [3] = AV_LOG_WARNING,   /* <undefined> */
76     [4] = AV_LOG_INFO,      /* MSGL_INFO */
77     [5] = AV_LOG_INFO,      /* <undefined> */
78     [6] = AV_LOG_VERBOSE,   /* MSGL_V */
79     [7] = AV_LOG_DEBUG,     /* MSGL_DBG2 */
80 };
81
82 static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
83 {
84     const int ass_level_clip = av_clip(ass_level, 0,
85         FF_ARRAY_ELEMS(ass_libavfilter_log_level_map) - 1);
86     const int level = ass_libavfilter_log_level_map[ass_level_clip];
87
88     av_vlog(ctx, level, fmt, args);
89     av_log(ctx, level, "\n");
90 }
91
92 static av_cold int init(AVFilterContext *ctx)
93 {
94     AssContext *ass = ctx->priv;
95
96     if (!ass->filename) {
97         av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
98         return AVERROR(EINVAL);
99     }
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     return 0;
115 }
116
117 static av_cold void uninit(AVFilterContext *ctx)
118 {
119     AssContext *ass = ctx->priv;
120
121     if (ass->track)
122         ass_free_track(ass->track);
123     if (ass->renderer)
124         ass_renderer_done(ass->renderer);
125     if (ass->library)
126         ass_library_done(ass->library);
127 }
128
129 static int query_formats(AVFilterContext *ctx)
130 {
131     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
132     return 0;
133 }
134
135 static int config_input(AVFilterLink *inlink)
136 {
137     AssContext *ass = inlink->dst->priv;
138
139     ff_draw_init(&ass->draw, inlink->format, 0);
140
141     ass_set_frame_size  (ass->renderer, inlink->w, inlink->h);
142     if (ass->original_w && ass->original_h)
143         ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
144                              (double)ass->original_w / ass->original_h);
145     if (ass->shaping != -1)
146         ass_set_shaper(ass->renderer, ass->shaping);
147
148     return 0;
149 }
150
151 /* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
152 #define AR(c)  ( (c)>>24)
153 #define AG(c)  (((c)>>16)&0xFF)
154 #define AB(c)  (((c)>>8) &0xFF)
155 #define AA(c)  ((0xFF-c) &0xFF)
156
157 static void overlay_ass_image(AssContext *ass, AVFrame *picref,
158                               const ASS_Image *image)
159 {
160     for (; image; image = image->next) {
161         uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
162         FFDrawColor color;
163         ff_draw_color(&ass->draw, &color, rgba_color);
164         ff_blend_mask(&ass->draw, &color,
165                       picref->data, picref->linesize,
166                       picref->width, picref->height,
167                       image->bitmap, image->stride, image->w, image->h,
168                       3, 0, image->dst_x, image->dst_y);
169     }
170 }
171
172 static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
173 {
174     AVFilterContext *ctx = inlink->dst;
175     AVFilterLink *outlink = ctx->outputs[0];
176     AssContext *ass = ctx->priv;
177     int detect_change = 0;
178     double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
179     ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
180                                         time_ms, &detect_change);
181
182     if (detect_change)
183         av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
184
185     overlay_ass_image(ass, picref, image);
186
187     return ff_filter_frame(outlink, picref);
188 }
189
190 static const AVFilterPad ass_inputs[] = {
191     {
192         .name             = "default",
193         .type             = AVMEDIA_TYPE_VIDEO,
194         .filter_frame     = filter_frame,
195         .config_props     = config_input,
196         .needs_writable   = 1,
197     },
198     { NULL }
199 };
200
201 static const AVFilterPad ass_outputs[] = {
202     {
203         .name = "default",
204         .type = AVMEDIA_TYPE_VIDEO,
205     },
206     { NULL }
207 };
208
209 #if CONFIG_ASS_FILTER
210
211 static const AVOption ass_options[] = {
212     COMMON_OPTIONS
213     {"shaping", "set shaping engine", OFFSET(shaping), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "shaping_mode"},
214         {"auto", NULL,                 0, AV_OPT_TYPE_CONST, {.i64 = -1},                  INT_MIN, INT_MAX, FLAGS, "shaping_mode"},
215         {"simple",  "simple shaping",  0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_SIMPLE},  INT_MIN, INT_MAX, FLAGS, "shaping_mode"},
216         {"complex", "complex shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_COMPLEX}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"},
217     {NULL},
218 };
219
220 AVFILTER_DEFINE_CLASS(ass);
221
222 static av_cold int init_ass(AVFilterContext *ctx)
223 {
224     AssContext *ass = ctx->priv;
225     int ret = init(ctx);
226
227     if (ret < 0)
228         return ret;
229
230     /* Initialize fonts */
231     ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
232
233     ass->track = ass_read_file(ass->library, ass->filename, NULL);
234     if (!ass->track) {
235         av_log(ctx, AV_LOG_ERROR,
236                "Could not create a libass track when reading file '%s'\n",
237                ass->filename);
238         return AVERROR(EINVAL);
239     }
240     return 0;
241 }
242
243 AVFilter ff_vf_ass = {
244     .name          = "ass",
245     .description   = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
246     .priv_size     = sizeof(AssContext),
247     .init          = init_ass,
248     .uninit        = uninit,
249     .query_formats = query_formats,
250     .inputs        = ass_inputs,
251     .outputs       = ass_outputs,
252     .priv_class    = &ass_class,
253 };
254 #endif
255
256 #if CONFIG_SUBTITLES_FILTER
257
258 static const AVOption subtitles_options[] = {
259     COMMON_OPTIONS
260     {"charenc",      "set input character encoding", OFFSET(charenc),      AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
261     {"stream_index", "set stream index",             OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1,       INT_MAX,  FLAGS},
262     {"si",           "set stream index",             OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1,       INT_MAX,  FLAGS},
263     {NULL},
264 };
265
266 static const char * const font_mimetypes[] = {
267     "application/x-truetype-font",
268     "application/vnd.ms-opentype",
269     "application/x-font-ttf",
270     NULL
271 };
272
273 static int attachment_is_font(AVStream * st)
274 {
275     const AVDictionaryEntry *tag = NULL;
276     int n;
277
278     tag = av_dict_get(st->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE);
279
280     if (tag) {
281         for (n = 0; font_mimetypes[n]; n++) {
282             if (av_strcasecmp(font_mimetypes[n], tag->value) == 0)
283                 return 1;
284         }
285     }
286     return 0;
287 }
288
289 AVFILTER_DEFINE_CLASS(subtitles);
290
291 static av_cold int init_subtitles(AVFilterContext *ctx)
292 {
293     int j, ret, sid;
294     int k = 0;
295     AVDictionary *codec_opts = NULL;
296     AVFormatContext *fmt = NULL;
297     AVCodecContext *dec_ctx = NULL;
298     AVCodec *dec = NULL;
299     const AVCodecDescriptor *dec_desc;
300     AVStream *st;
301     AVPacket pkt;
302     AssContext *ass = ctx->priv;
303
304     /* Init libass */
305     ret = init(ctx);
306     if (ret < 0)
307         return ret;
308     ass->track = ass_new_track(ass->library);
309     if (!ass->track) {
310         av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
311         return AVERROR(EINVAL);
312     }
313
314     /* Open subtitles file */
315     ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
316     if (ret < 0) {
317         av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
318         goto end;
319     }
320     ret = avformat_find_stream_info(fmt, NULL);
321     if (ret < 0)
322         goto end;
323
324     /* Locate subtitles stream */
325     if (ass->stream_index < 0)
326         ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
327     else {
328         ret = -1;
329         if (ass->stream_index < fmt->nb_streams) {
330             for (j = 0; j < fmt->nb_streams; j++) {
331                 if (fmt->streams[j]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
332                     if (ass->stream_index == k) {
333                         ret = j;
334                         break;
335                     }
336                     k++;
337                 }
338             }
339         }
340     }
341
342     if (ret < 0) {
343         av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
344                ass->filename);
345         goto end;
346     }
347     sid = ret;
348     st = fmt->streams[sid];
349
350     /* Load attached fonts */
351     for (j = 0; j < fmt->nb_streams; j++) {
352         AVStream *st = fmt->streams[j];
353         if (st->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT &&
354             attachment_is_font(st)) {
355             const AVDictionaryEntry *tag = NULL;
356             tag = av_dict_get(st->metadata, "filename", NULL,
357                               AV_DICT_MATCH_CASE);
358
359             if (tag) {
360                 av_log(ctx, AV_LOG_DEBUG, "Loading attached font: %s\n",
361                        tag->value);
362                 ass_add_font(ass->library, tag->value,
363                              st->codec->extradata,
364                              st->codec->extradata_size);
365             } else {
366                 av_log(ctx, AV_LOG_WARNING,
367                        "Font attachment has no filename, ignored.\n");
368             }
369         }
370     }
371
372     /* Initialize fonts */
373     ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
374
375     /* Open decoder */
376     dec_ctx = st->codec;
377     dec = avcodec_find_decoder(dec_ctx->codec_id);
378     if (!dec) {
379         av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
380                avcodec_get_name(dec_ctx->codec_id));
381         return AVERROR(EINVAL);
382     }
383     dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
384     if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) {
385         av_log(ctx, AV_LOG_ERROR,
386                "Only text based subtitles are currently supported\n");
387         return AVERROR_PATCHWELCOME;
388     }
389     if (ass->charenc)
390         av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
391     ret = avcodec_open2(dec_ctx, dec, &codec_opts);
392     if (ret < 0)
393         goto end;
394
395     /* Decode subtitles and push them into the renderer (libass) */
396     if (dec_ctx->subtitle_header)
397         ass_process_codec_private(ass->track,
398                                   dec_ctx->subtitle_header,
399                                   dec_ctx->subtitle_header_size);
400     av_init_packet(&pkt);
401     pkt.data = NULL;
402     pkt.size = 0;
403     while (av_read_frame(fmt, &pkt) >= 0) {
404         int i, got_subtitle;
405         AVSubtitle sub = {0};
406
407         if (pkt.stream_index == sid) {
408             ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
409             if (ret < 0) {
410                 av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
411                        av_err2str(ret));
412             } else if (got_subtitle) {
413                 for (i = 0; i < sub.num_rects; i++) {
414                     char *ass_line = sub.rects[i]->ass;
415                     if (!ass_line)
416                         break;
417                     ass_process_data(ass->track, ass_line, strlen(ass_line));
418                 }
419             }
420         }
421         av_free_packet(&pkt);
422         avsubtitle_free(&sub);
423     }
424
425 end:
426     av_dict_free(&codec_opts);
427     if (dec_ctx)
428         avcodec_close(dec_ctx);
429     if (fmt)
430         avformat_close_input(&fmt);
431     return ret;
432 }
433
434 AVFilter ff_vf_subtitles = {
435     .name          = "subtitles",
436     .description   = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
437     .priv_size     = sizeof(AssContext),
438     .init          = init_subtitles,
439     .uninit        = uninit,
440     .query_formats = query_formats,
441     .inputs        = ass_inputs,
442     .outputs       = ass_outputs,
443     .priv_class    = &subtitles_class,
444 };
445 #endif