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
47typedef 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    FFDrawContext draw;
59} AssContext;
60
61#define OFFSET(x) offsetof(AssContext, x)
62#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
63
64#define COMMON_OPTIONS \
65    {"filename",       "set the filename of file to read",                         OFFSET(filename),   AV_OPT_TYPE_STRING,     {.str = NULL},  CHAR_MIN, CHAR_MAX, FLAGS }, \
66    {"f",              "set the filename of file to read",                         OFFSET(filename),   AV_OPT_TYPE_STRING,     {.str = NULL},  CHAR_MIN, CHAR_MAX, FLAGS }, \
67    {"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 }, \
68
69/* libass supports a log level ranging from 0 to 7 */
70static const int ass_libavfilter_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
81static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx)
82{
83    int level = ass_libavfilter_log_level_map[ass_level];
84
85    av_vlog(ctx, level, fmt, args);
86    av_log(ctx, level, "\n");
87}
88
89static av_cold int init(AVFilterContext *ctx)
90{
91    AssContext *ass = ctx->priv;
92
93    if (!ass->filename) {
94        av_log(ctx, AV_LOG_ERROR, "No filename provided!\n");
95        return AVERROR(EINVAL);
96    }
97
98    ass->library = ass_library_init();
99    if (!ass->library) {
100        av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n");
101        return AVERROR(EINVAL);
102    }
103    ass_set_message_cb(ass->library, ass_log, ctx);
104
105    ass->renderer = ass_renderer_init(ass->library);
106    if (!ass->renderer) {
107        av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n");
108        return AVERROR(EINVAL);
109    }
110
111    return 0;
112}
113
114static av_cold void uninit(AVFilterContext *ctx)
115{
116    AssContext *ass = ctx->priv;
117
118    if (ass->track)
119        ass_free_track(ass->track);
120    if (ass->renderer)
121        ass_renderer_done(ass->renderer);
122    if (ass->library)
123        ass_library_done(ass->library);
124}
125
126static int query_formats(AVFilterContext *ctx)
127{
128    ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
129    return 0;
130}
131
132static int config_input(AVFilterLink *inlink)
133{
134    AssContext *ass = inlink->dst->priv;
135
136    ff_draw_init(&ass->draw, inlink->format, 0);
137
138    ass_set_frame_size  (ass->renderer, inlink->w, inlink->h);
139    if (ass->original_w && ass->original_h)
140        ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h,
141                             (double)ass->original_w / ass->original_h);
142
143    return 0;
144}
145
146/* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */
147#define AR(c)  ( (c)>>24)
148#define AG(c)  (((c)>>16)&0xFF)
149#define AB(c)  (((c)>>8) &0xFF)
150#define AA(c)  ((0xFF-c) &0xFF)
151
152static void overlay_ass_image(AssContext *ass, AVFrame *picref,
153                              const ASS_Image *image)
154{
155    for (; image; image = image->next) {
156        uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)};
157        FFDrawColor color;
158        ff_draw_color(&ass->draw, &color, rgba_color);
159        ff_blend_mask(&ass->draw, &color,
160                      picref->data, picref->linesize,
161                      picref->width, picref->height,
162                      image->bitmap, image->stride, image->w, image->h,
163                      3, 0, image->dst_x, image->dst_y);
164    }
165}
166
167static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
168{
169    AVFilterContext *ctx = inlink->dst;
170    AVFilterLink *outlink = ctx->outputs[0];
171    AssContext *ass = ctx->priv;
172    int detect_change = 0;
173    double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000;
174    ASS_Image *image = ass_render_frame(ass->renderer, ass->track,
175                                        time_ms, &detect_change);
176
177    if (detect_change)
178        av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms);
179
180    overlay_ass_image(ass, picref, image);
181
182    return ff_filter_frame(outlink, picref);
183}
184
185static const AVFilterPad ass_inputs[] = {
186    {
187        .name             = "default",
188        .type             = AVMEDIA_TYPE_VIDEO,
189        .filter_frame     = filter_frame,
190        .config_props     = config_input,
191        .needs_writable   = 1,
192    },
193    { NULL }
194};
195
196static const AVFilterPad ass_outputs[] = {
197    {
198        .name = "default",
199        .type = AVMEDIA_TYPE_VIDEO,
200    },
201    { NULL }
202};
203
204#if CONFIG_ASS_FILTER
205
206static const AVOption ass_options[] = {
207    COMMON_OPTIONS
208    {NULL},
209};
210
211AVFILTER_DEFINE_CLASS(ass);
212
213static av_cold int init_ass(AVFilterContext *ctx)
214{
215    AssContext *ass = ctx->priv;
216    int ret = init(ctx);
217
218    if (ret < 0)
219        return ret;
220
221    /* Initialize fonts */
222    ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
223
224    ass->track = ass_read_file(ass->library, ass->filename, NULL);
225    if (!ass->track) {
226        av_log(ctx, AV_LOG_ERROR,
227               "Could not create a libass track when reading file '%s'\n",
228               ass->filename);
229        return AVERROR(EINVAL);
230    }
231    return 0;
232}
233
234AVFilter ff_vf_ass = {
235    .name          = "ass",
236    .description   = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."),
237    .priv_size     = sizeof(AssContext),
238    .init          = init_ass,
239    .uninit        = uninit,
240    .query_formats = query_formats,
241    .inputs        = ass_inputs,
242    .outputs       = ass_outputs,
243    .priv_class    = &ass_class,
244};
245#endif
246
247#if CONFIG_SUBTITLES_FILTER
248
249static const AVOption subtitles_options[] = {
250    COMMON_OPTIONS
251    {"charenc",      "set input character encoding", OFFSET(charenc),      AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS},
252    {"stream_index", "set stream index",             OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1,       INT_MAX,  FLAGS},
253    {"si",           "set stream index",             OFFSET(stream_index), AV_OPT_TYPE_INT,    { .i64 = -1 }, -1,       INT_MAX,  FLAGS},
254    {NULL},
255};
256
257static const char *font_mimetypes[] = {
258    "application/x-truetype-font",
259    "application/vnd.ms-opentype",
260    "application/x-font-ttf",
261    NULL
262};
263
264static int attachment_is_font(AVStream * st)
265{
266    const AVDictionaryEntry *tag = NULL;
267    int n;
268
269    tag = av_dict_get(st->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE);
270
271    if (tag) {
272        for (n = 0; font_mimetypes[n]; n++) {
273            if (av_strcasecmp(font_mimetypes[n], tag->value) == 0)
274                return 1;
275        }
276    }
277    return 0;
278}
279
280AVFILTER_DEFINE_CLASS(subtitles);
281
282static av_cold int init_subtitles(AVFilterContext *ctx)
283{
284    int j, ret, sid;
285    int k = 0;
286    AVDictionary *codec_opts = NULL;
287    AVFormatContext *fmt = NULL;
288    AVCodecContext *dec_ctx = NULL;
289    AVCodec *dec = NULL;
290    const AVCodecDescriptor *dec_desc;
291    AVStream *st;
292    AVPacket pkt;
293    AssContext *ass = ctx->priv;
294
295    /* Init libass */
296    ret = init(ctx);
297    if (ret < 0)
298        return ret;
299    ass->track = ass_new_track(ass->library);
300    if (!ass->track) {
301        av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n");
302        return AVERROR(EINVAL);
303    }
304
305    /* Open subtitles file */
306    ret = avformat_open_input(&fmt, ass->filename, NULL, NULL);
307    if (ret < 0) {
308        av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename);
309        goto end;
310    }
311    ret = avformat_find_stream_info(fmt, NULL);
312    if (ret < 0)
313        goto end;
314
315    /* Locate subtitles stream */
316    if (ass->stream_index < 0)
317        ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0);
318    else {
319        ret = -1;
320        if (ass->stream_index < fmt->nb_streams) {
321            for (j = 0; j < fmt->nb_streams; j++) {
322                if (fmt->streams[j]->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
323                    if (ass->stream_index == k) {
324                        ret = j;
325                        break;
326                    }
327                    k++;
328                }
329            }
330        }
331    }
332
333    if (ret < 0) {
334        av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n",
335               ass->filename);
336        goto end;
337    }
338    sid = ret;
339    st = fmt->streams[sid];
340
341    /* Load attached fonts */
342    for (j = 0; j < fmt->nb_streams; j++) {
343        AVStream *st = fmt->streams[j];
344        if (st->codec->codec_type == AVMEDIA_TYPE_ATTACHMENT &&
345            attachment_is_font(st)) {
346            const AVDictionaryEntry *tag = NULL;
347            tag = av_dict_get(st->metadata, "filename", NULL,
348                              AV_DICT_MATCH_CASE);
349
350            if (tag) {
351                av_log(ctx, AV_LOG_DEBUG, "Loading attached font: %s\n",
352                       tag->value);
353                ass_add_font(ass->library, tag->value,
354                             st->codec->extradata,
355                             st->codec->extradata_size);
356            } else {
357                av_log(ctx, AV_LOG_WARNING,
358                       "Font attachment has no filename, ignored.\n");
359            }
360        }
361    }
362
363    /* Initialize fonts */
364    ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1);
365
366    /* Open decoder */
367    dec_ctx = st->codec;
368    dec = avcodec_find_decoder(dec_ctx->codec_id);
369    if (!dec) {
370        av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n",
371               avcodec_get_name(dec_ctx->codec_id));
372        return AVERROR(EINVAL);
373    }
374    dec_desc = avcodec_descriptor_get(dec_ctx->codec_id);
375    if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) {
376        av_log(ctx, AV_LOG_ERROR,
377               "Only text based subtitles are currently supported\n");
378        return AVERROR_PATCHWELCOME;
379    }
380    if (ass->charenc)
381        av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0);
382    ret = avcodec_open2(dec_ctx, dec, &codec_opts);
383    if (ret < 0)
384        goto end;
385
386    /* Decode subtitles and push them into the renderer (libass) */
387    if (dec_ctx->subtitle_header)
388        ass_process_codec_private(ass->track,
389                                  dec_ctx->subtitle_header,
390                                  dec_ctx->subtitle_header_size);
391    av_init_packet(&pkt);
392    pkt.data = NULL;
393    pkt.size = 0;
394    while (av_read_frame(fmt, &pkt) >= 0) {
395        int i, got_subtitle;
396        AVSubtitle sub = {0};
397
398        if (pkt.stream_index == sid) {
399            ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt);
400            if (ret < 0) {
401                av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n",
402                       av_err2str(ret));
403            } else if (got_subtitle) {
404                for (i = 0; i < sub.num_rects; i++) {
405                    char *ass_line = sub.rects[i]->ass;
406                    if (!ass_line)
407                        break;
408                    ass_process_data(ass->track, ass_line, strlen(ass_line));
409                }
410            }
411        }
412        av_free_packet(&pkt);
413        avsubtitle_free(&sub);
414    }
415
416end:
417    av_dict_free(&codec_opts);
418    if (dec_ctx)
419        avcodec_close(dec_ctx);
420    if (fmt)
421        avformat_close_input(&fmt);
422    return ret;
423}
424
425AVFilter ff_vf_subtitles = {
426    .name          = "subtitles",
427    .description   = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."),
428    .priv_size     = sizeof(AssContext),
429    .init          = init_subtitles,
430    .uninit        = uninit,
431    .query_formats = query_formats,
432    .inputs        = ass_inputs,
433    .outputs       = ass_outputs,
434    .priv_class    = &subtitles_class,
435};
436#endif
437