From patchwork Sun Oct 3 13:38:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: SomeRGuy X-Patchwork-Id: 30891 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp3434453iob; Sun, 3 Oct 2021 06:38:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzEGl3J3uzvO9b0FW8kIp8tiISYD5xCRCH2BKdT41dw0COmR2nJfTrU8nZvyqTJ7OoV7y5 X-Received: by 2002:a17:906:2405:: with SMTP id z5mr10532700eja.170.1633268318587; Sun, 03 Oct 2021 06:38:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633268318; cv=none; d=google.com; s=arc-20160816; b=Mwg4yoTS9BUa0wdqxmC3UQVE53S5Z1OYDOT/2VS5QctQtWDzJ5WqdFI+BMhdD4mC/G j2YzaFJxpti4Sn43HfKLRSbK4bwM7UOD2EVtHPgIqEM43YbYeCWAEam+8Hr6wdGI1p5m QTmXJvtOv5gog4vXQ89KzlmyjaV6h6gUv34fKOt1hIyR0uINCExy/Panadfxt0AHx4B6 fv6BRfNd3Ubuq9vdY80rttYAYizw8fjzJUr4/YncCxSD5Qs/ez0ZXhCLdK25XY7WB+aR kd7tawFNWWtC8MGVfOkfDghn6evvZrwd9gfS5yD7U6fGknzCTYn3Czo9q5Si/dwRSn5Q 2wGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=RnV9356KcV8xV3qk5B5aRzuqXPkgkxGZme1/+fALfzI=; b=cNCL92oSvmCKUoFNSnrCMTxxhhph05bw5HfyRGWK1t6mAbD9+1fVvifR/yqh9IepMa cpLGoTX/lC00KNzfFJC4r5G6TyPy4EivdSP6cO2nooKmftfnc68lnJehaaimzZXtrOrz fe40vOt0qr37H3BgRgNWEr8TvvlM1Iq4kjfR4GLMw6ZkAyBgT1ZChOop8VcQTFf86DQu zhL/OuAZKLNmwe8gXutAmPZLZRFrOxZ5M4FdcDS3ws6zGMOsAkL7pwC/Jpy2G7OvW7fN 1J1yT+LMVf8/fX/ZwuIMXeqYkpjbo4AP5ecRQf67rWattBcldKgWMDEG3DavIWNwFxMO Cqfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=G4EnXCYv; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x3si1231240edr.563.2021.10.03.06.38.38; Sun, 03 Oct 2021 06:38:38 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=G4EnXCYv; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A3F7268A830; Sun, 3 Oct 2021 16:38:35 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1325E68A5F0 for ; Sun, 3 Oct 2021 16:38:29 +0300 (EEST) Received: by mail-wm1-f66.google.com with SMTP id s198-20020a1ca9cf000000b0030d6986ea9fso1212452wme.1 for ; Sun, 03 Oct 2021 06:38:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zQuSBo+mVkX8oPqtOsQntmyuDByz8LNfnhsXSQANmDk=; b=G4EnXCYvV2bdTQb6hZ336qVXCFTiMUwEOnze2ANwILxxVYE6Eqd8ItOa7OYuTBBMWO +8bz0EcwQ+y0TbufcX25mi+HjiOcb02i+VgLwvXcUiE1xACRLmmgXmcGjrNweoWl3sMO OYthQk1K1zPDqplLI1hCe9wHg7NkSR8cMgU2a3eCcOnWPUH94HSx7BoY+GSySO/gtmOy 1IJjDCPgirMCt7fV4MQUsX+wzuUorCEUkvr9jVGnHYrYYZPA1Okb8Q1pv5jD6wHgToAf RZ2jkI0k0ry09Azri4kti38bK8HKnBQuE8hChFEXfyS0XgQFD1WffOUGjUJVoV0YnLd/ zbfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zQuSBo+mVkX8oPqtOsQntmyuDByz8LNfnhsXSQANmDk=; b=jw+wOZejXUP6LRHcCwo0a1sm2QYG96Z2xoDs1+Q8oOioak/LWDEzStkysP2wKPWAA0 2Ig6moJ9ztRkkJjEZEBCjYWqkciwa4GOuRXTCYUZENPxlGndnxjV5y3KPhBrd4Hlq1Mh 5FWoZ6mTd4OzrQdFrlDeDud9vieguId9lMlOYWETwzxKEewBUerb8D1F162qMUNUkxUk 9ZcWmfFPGYRYldpWDEreKwc/PbxbcRL/rF166nDHV3xrUn+YqVThQrSMAZGDWy8LzhVE dZknEAGxSmAm6fXt+tqtnwy0RJoBjeGBp/yQ6tT7KrBvsCLH6vNWBjzFJa1xKJZ0pMuW gFKw== X-Gm-Message-State: AOAM5306RAXQf7D/Lz7328RxEVcqqeQOnj5k3QHTZhEz0aWFRBXlV+rC jJVT5PxM3AUKwqf1R32k0k3YMdl5WZX1Dw== X-Received: by 2002:a1c:7d0c:: with SMTP id y12mr13775174wmc.6.1633268307871; Sun, 03 Oct 2021 06:38:27 -0700 (PDT) Received: from localhost.localdomain ([84.17.42.35]) by smtp.gmail.com with ESMTPSA id z79sm11957999wmc.17.2021.10.03.06.38.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Oct 2021 06:38:27 -0700 (PDT) From: SomeRGuy To: ffmpeg-devel@ffmpeg.org Date: Sun, 3 Oct 2021 16:38:08 +0300 Message-Id: <20211003133808.1560-1-wwakabot@gmail.com> X-Mailer: git-send-email 2.30.2.windows.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] Since mkvmerge 19 has fonts types from RFC 8081 and mkvmerge 58 on windows use new file/magic library X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: SomeRGuy Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: QmXxN4CE1Vyw Signed-off-by: SomeRGuy --- libavfilter/vf_subtitles.c | 991 +++++++++++++++++++------------------ 1 file changed, 499 insertions(+), 492 deletions(-) diff --git a/libavfilter/vf_subtitles.c b/libavfilter/vf_subtitles.c index f08a7fba9d..511722199c 100644 --- a/libavfilter/vf_subtitles.c +++ b/libavfilter/vf_subtitles.c @@ -1,492 +1,499 @@ -/* - * Copyright (c) 2011 Baptiste Coudurier - * Copyright (c) 2011 Stefano Sabatini - * Copyright (c) 2012 Clément Bœsch - * - * This file is part of FFmpeg. - * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -/** - * @file - * Libass subtitles burning filter. - * - * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html} - */ - -#include - -#include "config.h" -#if CONFIG_SUBTITLES_FILTER -# include "libavcodec/avcodec.h" -# include "libavformat/avformat.h" -#endif -#include "libavutil/avstring.h" -#include "libavutil/imgutils.h" -#include "libavutil/opt.h" -#include "libavutil/parseutils.h" -#include "drawutils.h" -#include "avfilter.h" -#include "internal.h" -#include "formats.h" -#include "video.h" - -typedef struct AssContext { - const AVClass *class; - ASS_Library *library; - ASS_Renderer *renderer; - ASS_Track *track; - char *filename; - char *fontsdir; - char *charenc; - char *force_style; - int stream_index; - int alpha; - uint8_t rgba_map[4]; - int pix_step[4]; ///< steps per pixel for each plane of the main output - int original_w, original_h; - int shaping; - FFDrawContext draw; -} AssContext; - -#define OFFSET(x) offsetof(AssContext, x) -#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM - -#define COMMON_OPTIONS \ - {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \ - {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ - {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \ - -/* libass supports a log level ranging from 0 to 7 */ -static const int ass_libavfilter_log_level_map[] = { - [0] = AV_LOG_FATAL, /* MSGL_FATAL */ - [1] = AV_LOG_ERROR, /* MSGL_ERR */ - [2] = AV_LOG_WARNING, /* MSGL_WARN */ - [3] = AV_LOG_WARNING, /* */ - [4] = AV_LOG_INFO, /* MSGL_INFO */ - [5] = AV_LOG_INFO, /* */ - [6] = AV_LOG_VERBOSE, /* MSGL_V */ - [7] = AV_LOG_DEBUG, /* MSGL_DBG2 */ -}; - -static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx) -{ - const int ass_level_clip = av_clip(ass_level, 0, - FF_ARRAY_ELEMS(ass_libavfilter_log_level_map) - 1); - const int level = ass_libavfilter_log_level_map[ass_level_clip]; - - av_vlog(ctx, level, fmt, args); - av_log(ctx, level, "\n"); -} - -static av_cold int init(AVFilterContext *ctx) -{ - AssContext *ass = ctx->priv; - - if (!ass->filename) { - av_log(ctx, AV_LOG_ERROR, "No filename provided!\n"); - return AVERROR(EINVAL); - } - - ass->library = ass_library_init(); - if (!ass->library) { - av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n"); - return AVERROR(EINVAL); - } - ass_set_message_cb(ass->library, ass_log, ctx); - - ass_set_fonts_dir(ass->library, ass->fontsdir); - ass_set_extract_fonts(ass->library, 1); - - ass->renderer = ass_renderer_init(ass->library); - if (!ass->renderer) { - av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n"); - return AVERROR(EINVAL); - } - - return 0; -} - -static av_cold void uninit(AVFilterContext *ctx) -{ - AssContext *ass = ctx->priv; - - if (ass->track) - ass_free_track(ass->track); - if (ass->renderer) - ass_renderer_done(ass->renderer); - if (ass->library) - ass_library_done(ass->library); -} - -static int query_formats(AVFilterContext *ctx) -{ - return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0)); -} - -static int config_input(AVFilterLink *inlink) -{ - AssContext *ass = inlink->dst->priv; - - ff_draw_init(&ass->draw, inlink->format, ass->alpha ? FF_DRAW_PROCESS_ALPHA : 0); - - ass_set_frame_size (ass->renderer, inlink->w, inlink->h); - if (ass->original_w && ass->original_h) - ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h, - (double)ass->original_w / ass->original_h); - if (ass->shaping != -1) - ass_set_shaper(ass->renderer, ass->shaping); - - return 0; -} - -/* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */ -#define AR(c) ( (c)>>24) -#define AG(c) (((c)>>16)&0xFF) -#define AB(c) (((c)>>8) &0xFF) -#define AA(c) ((0xFF-(c)) &0xFF) - -static void overlay_ass_image(AssContext *ass, AVFrame *picref, - const ASS_Image *image) -{ - for (; image; image = image->next) { - uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)}; - FFDrawColor color; - ff_draw_color(&ass->draw, &color, rgba_color); - ff_blend_mask(&ass->draw, &color, - picref->data, picref->linesize, - picref->width, picref->height, - image->bitmap, image->stride, image->w, image->h, - 3, 0, image->dst_x, image->dst_y); - } -} - -static int filter_frame(AVFilterLink *inlink, AVFrame *picref) -{ - AVFilterContext *ctx = inlink->dst; - AVFilterLink *outlink = ctx->outputs[0]; - AssContext *ass = ctx->priv; - int detect_change = 0; - double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000; - ASS_Image *image = ass_render_frame(ass->renderer, ass->track, - time_ms, &detect_change); - - if (detect_change) - av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms); - - overlay_ass_image(ass, picref, image); - - return ff_filter_frame(outlink, picref); -} - -static const AVFilterPad ass_inputs[] = { - { - .name = "default", - .type = AVMEDIA_TYPE_VIDEO, - .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, - .filter_frame = filter_frame, - .config_props = config_input, - }, -}; - -static const AVFilterPad ass_outputs[] = { - { - .name = "default", - .type = AVMEDIA_TYPE_VIDEO, - }, -}; - -#if CONFIG_ASS_FILTER - -static const AVOption ass_options[] = { - COMMON_OPTIONS - {"shaping", "set shaping engine", OFFSET(shaping), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "shaping_mode"}, - {"auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"}, - {"simple", "simple shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_SIMPLE}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"}, - {"complex", "complex shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_COMPLEX}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"}, - {NULL}, -}; - -AVFILTER_DEFINE_CLASS(ass); - -static av_cold int init_ass(AVFilterContext *ctx) -{ - AssContext *ass = ctx->priv; - int ret = init(ctx); - - if (ret < 0) - return ret; - - /* Initialize fonts */ - ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1); - - ass->track = ass_read_file(ass->library, ass->filename, NULL); - if (!ass->track) { - av_log(ctx, AV_LOG_ERROR, - "Could not create a libass track when reading file '%s'\n", - ass->filename); - return AVERROR(EINVAL); - } - return 0; -} - -const AVFilter ff_vf_ass = { - .name = "ass", - .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."), - .priv_size = sizeof(AssContext), - .init = init_ass, - .uninit = uninit, - .query_formats = query_formats, - FILTER_INPUTS(ass_inputs), - FILTER_OUTPUTS(ass_outputs), - .priv_class = &ass_class, -}; -#endif - -#if CONFIG_SUBTITLES_FILTER - -static const AVOption subtitles_options[] = { - COMMON_OPTIONS - {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS}, - {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS}, - {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS}, - {"force_style", "force subtitle style", OFFSET(force_style), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS}, - {NULL}, -}; - -static const char * const font_mimetypes[] = { - "application/x-truetype-font", - "application/vnd.ms-opentype", - "application/x-font-ttf", - NULL -}; - -static int attachment_is_font(AVStream * st) -{ - const AVDictionaryEntry *tag = NULL; - int n; - - tag = av_dict_get(st->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE); - - if (tag) { - for (n = 0; font_mimetypes[n]; n++) { - if (av_strcasecmp(font_mimetypes[n], tag->value) == 0) - return 1; - } - } - return 0; -} - -AVFILTER_DEFINE_CLASS(subtitles); - -static av_cold int init_subtitles(AVFilterContext *ctx) -{ - int j, ret, sid; - int k = 0; - AVDictionary *codec_opts = NULL; - AVFormatContext *fmt = NULL; - AVCodecContext *dec_ctx = NULL; - const AVCodec *dec; - const AVCodecDescriptor *dec_desc; - AVStream *st; - AVPacket pkt; - AssContext *ass = ctx->priv; - - /* Init libass */ - ret = init(ctx); - if (ret < 0) - return ret; - ass->track = ass_new_track(ass->library); - if (!ass->track) { - av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n"); - return AVERROR(EINVAL); - } - - /* Open subtitles file */ - ret = avformat_open_input(&fmt, ass->filename, NULL, NULL); - if (ret < 0) { - av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename); - goto end; - } - ret = avformat_find_stream_info(fmt, NULL); - if (ret < 0) - goto end; - - /* Locate subtitles stream */ - if (ass->stream_index < 0) - ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0); - else { - ret = -1; - if (ass->stream_index < fmt->nb_streams) { - for (j = 0; j < fmt->nb_streams; j++) { - if (fmt->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { - if (ass->stream_index == k) { - ret = j; - break; - } - k++; - } - } - } - } - - if (ret < 0) { - av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n", - ass->filename); - goto end; - } - sid = ret; - st = fmt->streams[sid]; - - /* Load attached fonts */ - for (j = 0; j < fmt->nb_streams; j++) { - AVStream *st = fmt->streams[j]; - if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT && - attachment_is_font(st)) { - const AVDictionaryEntry *tag = NULL; - tag = av_dict_get(st->metadata, "filename", NULL, - AV_DICT_MATCH_CASE); - - if (tag) { - av_log(ctx, AV_LOG_DEBUG, "Loading attached font: %s\n", - tag->value); - ass_add_font(ass->library, tag->value, - st->codecpar->extradata, - st->codecpar->extradata_size); - } else { - av_log(ctx, AV_LOG_WARNING, - "Font attachment has no filename, ignored.\n"); - } - } - } - - /* Initialize fonts */ - ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1); - - /* Open decoder */ - dec = avcodec_find_decoder(st->codecpar->codec_id); - if (!dec) { - av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n", - avcodec_get_name(st->codecpar->codec_id)); - ret = AVERROR_DECODER_NOT_FOUND; - goto end; - } - dec_desc = avcodec_descriptor_get(st->codecpar->codec_id); - if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) { - av_log(ctx, AV_LOG_ERROR, - "Only text based subtitles are currently supported\n"); - ret = AVERROR_PATCHWELCOME; - goto end; - } - if (ass->charenc) - av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0); - - dec_ctx = avcodec_alloc_context3(dec); - if (!dec_ctx) { - ret = AVERROR(ENOMEM); - goto end; - } - - ret = avcodec_parameters_to_context(dec_ctx, st->codecpar); - if (ret < 0) - goto end; - - /* - * This is required by the decoding process in order to rescale the - * timestamps: in the current API the decoded subtitles have their pts - * expressed in AV_TIME_BASE, and thus the lavc internals need to know the - * stream time base in order to achieve the rescaling. - * - * That API is old and needs to be reworked to match behaviour with A/V. - */ - dec_ctx->pkt_timebase = st->time_base; - - ret = avcodec_open2(dec_ctx, NULL, &codec_opts); - if (ret < 0) - goto end; - - if (ass->force_style) { - char **list = NULL; - char *temp = NULL; - char *ptr = av_strtok(ass->force_style, ",", &temp); - int i = 0; - while (ptr) { - av_dynarray_add(&list, &i, ptr); - if (!list) { - ret = AVERROR(ENOMEM); - goto end; - } - ptr = av_strtok(NULL, ",", &temp); - } - av_dynarray_add(&list, &i, NULL); - if (!list) { - ret = AVERROR(ENOMEM); - goto end; - } - ass_set_style_overrides(ass->library, list); - av_free(list); - } - /* Decode subtitles and push them into the renderer (libass) */ - if (dec_ctx->subtitle_header) - ass_process_codec_private(ass->track, - dec_ctx->subtitle_header, - dec_ctx->subtitle_header_size); - while (av_read_frame(fmt, &pkt) >= 0) { - int i, got_subtitle; - AVSubtitle sub = {0}; - - if (pkt.stream_index == sid) { - ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt); - if (ret < 0) { - av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n", - av_err2str(ret)); - } else if (got_subtitle) { - const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)); - const int64_t duration = sub.end_display_time; - for (i = 0; i < sub.num_rects; i++) { - char *ass_line = sub.rects[i]->ass; - if (!ass_line) - break; - ass_process_chunk(ass->track, ass_line, strlen(ass_line), - start_time, duration); - } - } - } - av_packet_unref(&pkt); - avsubtitle_free(&sub); - } - -end: - av_dict_free(&codec_opts); - avcodec_free_context(&dec_ctx); - avformat_close_input(&fmt); - return ret; -} - -const AVFilter ff_vf_subtitles = { - .name = "subtitles", - .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."), - .priv_size = sizeof(AssContext), - .init = init_subtitles, - .uninit = uninit, - .query_formats = query_formats, - FILTER_INPUTS(ass_inputs), - FILTER_OUTPUTS(ass_outputs), - .priv_class = &subtitles_class, -}; -#endif +/* + * Copyright (c) 2011 Baptiste Coudurier + * Copyright (c) 2011 Stefano Sabatini + * Copyright (c) 2012 Clément Bœsch + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * Libass subtitles burning filter. + * + * @see{http://www.matroska.org/technical/specs/subtitles/ssa.html} + */ + +#include + +#include "config.h" +#if CONFIG_SUBTITLES_FILTER +# include "libavcodec/avcodec.h" +# include "libavformat/avformat.h" +#endif +#include "libavutil/avstring.h" +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/parseutils.h" +#include "drawutils.h" +#include "avfilter.h" +#include "internal.h" +#include "formats.h" +#include "video.h" + +typedef struct AssContext { + const AVClass *class; + ASS_Library *library; + ASS_Renderer *renderer; + ASS_Track *track; + char *filename; + char *fontsdir; + char *charenc; + char *force_style; + int stream_index; + int alpha; + uint8_t rgba_map[4]; + int pix_step[4]; ///< steps per pixel for each plane of the main output + int original_w, original_h; + int shaping; + FFDrawContext draw; +} AssContext; + +#define OFFSET(x) offsetof(AssContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +#define COMMON_OPTIONS \ + {"filename", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"f", "set the filename of file to read", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"original_size", "set the size of the original video (used to scale fonts)", OFFSET(original_w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, \ + {"fontsdir", "set the directory containing the fonts to read", OFFSET(fontsdir), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, \ + {"alpha", "enable processing of alpha channel", OFFSET(alpha), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, FLAGS }, \ + +/* libass supports a log level ranging from 0 to 7 */ +static const int ass_libavfilter_log_level_map[] = { + [0] = AV_LOG_FATAL, /* MSGL_FATAL */ + [1] = AV_LOG_ERROR, /* MSGL_ERR */ + [2] = AV_LOG_WARNING, /* MSGL_WARN */ + [3] = AV_LOG_WARNING, /* */ + [4] = AV_LOG_INFO, /* MSGL_INFO */ + [5] = AV_LOG_INFO, /* */ + [6] = AV_LOG_VERBOSE, /* MSGL_V */ + [7] = AV_LOG_DEBUG, /* MSGL_DBG2 */ +}; + +static void ass_log(int ass_level, const char *fmt, va_list args, void *ctx) +{ + const int ass_level_clip = av_clip(ass_level, 0, + FF_ARRAY_ELEMS(ass_libavfilter_log_level_map) - 1); + const int level = ass_libavfilter_log_level_map[ass_level_clip]; + + av_vlog(ctx, level, fmt, args); + av_log(ctx, level, "\n"); +} + +static av_cold int init(AVFilterContext *ctx) +{ + AssContext *ass = ctx->priv; + + if (!ass->filename) { + av_log(ctx, AV_LOG_ERROR, "No filename provided!\n"); + return AVERROR(EINVAL); + } + + ass->library = ass_library_init(); + if (!ass->library) { + av_log(ctx, AV_LOG_ERROR, "Could not initialize libass.\n"); + return AVERROR(EINVAL); + } + ass_set_message_cb(ass->library, ass_log, ctx); + + ass_set_fonts_dir(ass->library, ass->fontsdir); + ass_set_extract_fonts(ass->library, 1); + + ass->renderer = ass_renderer_init(ass->library); + if (!ass->renderer) { + av_log(ctx, AV_LOG_ERROR, "Could not initialize libass renderer.\n"); + return AVERROR(EINVAL); + } + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + AssContext *ass = ctx->priv; + + if (ass->track) + ass_free_track(ass->track); + if (ass->renderer) + ass_renderer_done(ass->renderer); + if (ass->library) + ass_library_done(ass->library); +} + +static int query_formats(AVFilterContext *ctx) +{ + return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0)); +} + +static int config_input(AVFilterLink *inlink) +{ + AssContext *ass = inlink->dst->priv; + + ff_draw_init(&ass->draw, inlink->format, ass->alpha ? FF_DRAW_PROCESS_ALPHA : 0); + + ass_set_frame_size (ass->renderer, inlink->w, inlink->h); + if (ass->original_w && ass->original_h) + ass_set_aspect_ratio(ass->renderer, (double)inlink->w / inlink->h, + (double)ass->original_w / ass->original_h); + if (ass->shaping != -1) + ass_set_shaper(ass->renderer, ass->shaping); + + return 0; +} + +/* libass stores an RGBA color in the format RRGGBBTT, where TT is the transparency level */ +#define AR(c) ( (c)>>24) +#define AG(c) (((c)>>16)&0xFF) +#define AB(c) (((c)>>8) &0xFF) +#define AA(c) ((0xFF-(c)) &0xFF) + +static void overlay_ass_image(AssContext *ass, AVFrame *picref, + const ASS_Image *image) +{ + for (; image; image = image->next) { + uint8_t rgba_color[] = {AR(image->color), AG(image->color), AB(image->color), AA(image->color)}; + FFDrawColor color; + ff_draw_color(&ass->draw, &color, rgba_color); + ff_blend_mask(&ass->draw, &color, + picref->data, picref->linesize, + picref->width, picref->height, + image->bitmap, image->stride, image->w, image->h, + 3, 0, image->dst_x, image->dst_y); + } +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *picref) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + AssContext *ass = ctx->priv; + int detect_change = 0; + double time_ms = picref->pts * av_q2d(inlink->time_base) * 1000; + ASS_Image *image = ass_render_frame(ass->renderer, ass->track, + time_ms, &detect_change); + + if (detect_change) + av_log(ctx, AV_LOG_DEBUG, "Change happened at time ms:%f\n", time_ms); + + overlay_ass_image(ass, picref, image); + + return ff_filter_frame(outlink, picref); +} + +static const AVFilterPad ass_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, + .filter_frame = filter_frame, + .config_props = config_input, + }, +}; + +static const AVFilterPad ass_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, +}; + +#if CONFIG_ASS_FILTER + +static const AVOption ass_options[] = { + COMMON_OPTIONS + {"shaping", "set shaping engine", OFFSET(shaping), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "shaping_mode"}, + {"auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = -1}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"}, + {"simple", "simple shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_SIMPLE}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"}, + {"complex", "complex shaping", 0, AV_OPT_TYPE_CONST, {.i64 = ASS_SHAPING_COMPLEX}, INT_MIN, INT_MAX, FLAGS, "shaping_mode"}, + {NULL}, +}; + +AVFILTER_DEFINE_CLASS(ass); + +static av_cold int init_ass(AVFilterContext *ctx) +{ + AssContext *ass = ctx->priv; + int ret = init(ctx); + + if (ret < 0) + return ret; + + /* Initialize fonts */ + ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1); + + ass->track = ass_read_file(ass->library, ass->filename, NULL); + if (!ass->track) { + av_log(ctx, AV_LOG_ERROR, + "Could not create a libass track when reading file '%s'\n", + ass->filename); + return AVERROR(EINVAL); + } + return 0; +} + +const AVFilter ff_vf_ass = { + .name = "ass", + .description = NULL_IF_CONFIG_SMALL("Render ASS subtitles onto input video using the libass library."), + .priv_size = sizeof(AssContext), + .init = init_ass, + .uninit = uninit, + .query_formats = query_formats, + FILTER_INPUTS(ass_inputs), + FILTER_OUTPUTS(ass_outputs), + .priv_class = &ass_class, +}; +#endif + +#if CONFIG_SUBTITLES_FILTER + +static const AVOption subtitles_options[] = { + COMMON_OPTIONS + {"charenc", "set input character encoding", OFFSET(charenc), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS}, + {"stream_index", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS}, + {"si", "set stream index", OFFSET(stream_index), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS}, + {"force_style", "force subtitle style", OFFSET(force_style), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS}, + {NULL}, +}; + +static const char * const font_mimetypes[] = { + "font/ttf", + "font/otf", + "font/sfnt", + "font/woff", + "font/woff2", + "application/font-sfnt", + "application/font-woff", + "application/x-font-ttf", + "application/x-truetype-font", + "application/vnd.ms-opentype", + NULL +}; + +static int attachment_is_font(AVStream * st) +{ + const AVDictionaryEntry *tag = NULL; + int n; + + tag = av_dict_get(st->metadata, "mimetype", NULL, AV_DICT_MATCH_CASE); + + if (tag) { + for (n = 0; font_mimetypes[n]; n++) { + if (av_strcasecmp(font_mimetypes[n], tag->value) == 0) + return 1; + } + } + return 0; +} + +AVFILTER_DEFINE_CLASS(subtitles); + +static av_cold int init_subtitles(AVFilterContext *ctx) +{ + int j, ret, sid; + int k = 0; + AVDictionary *codec_opts = NULL; + AVFormatContext *fmt = NULL; + AVCodecContext *dec_ctx = NULL; + const AVCodec *dec; + const AVCodecDescriptor *dec_desc; + AVStream *st; + AVPacket pkt; + AssContext *ass = ctx->priv; + + /* Init libass */ + ret = init(ctx); + if (ret < 0) + return ret; + ass->track = ass_new_track(ass->library); + if (!ass->track) { + av_log(ctx, AV_LOG_ERROR, "Could not create a libass track\n"); + return AVERROR(EINVAL); + } + + /* Open subtitles file */ + ret = avformat_open_input(&fmt, ass->filename, NULL, NULL); + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, "Unable to open %s\n", ass->filename); + goto end; + } + ret = avformat_find_stream_info(fmt, NULL); + if (ret < 0) + goto end; + + /* Locate subtitles stream */ + if (ass->stream_index < 0) + ret = av_find_best_stream(fmt, AVMEDIA_TYPE_SUBTITLE, -1, -1, NULL, 0); + else { + ret = -1; + if (ass->stream_index < fmt->nb_streams) { + for (j = 0; j < fmt->nb_streams; j++) { + if (fmt->streams[j]->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE) { + if (ass->stream_index == k) { + ret = j; + break; + } + k++; + } + } + } + } + + if (ret < 0) { + av_log(ctx, AV_LOG_ERROR, "Unable to locate subtitle stream in %s\n", + ass->filename); + goto end; + } + sid = ret; + st = fmt->streams[sid]; + + /* Load attached fonts */ + for (j = 0; j < fmt->nb_streams; j++) { + AVStream *st = fmt->streams[j]; + if (st->codecpar->codec_type == AVMEDIA_TYPE_ATTACHMENT && + attachment_is_font(st)) { + const AVDictionaryEntry *tag = NULL; + tag = av_dict_get(st->metadata, "filename", NULL, + AV_DICT_MATCH_CASE); + + if (tag) { + av_log(ctx, AV_LOG_DEBUG, "Loading attached font: %s\n", + tag->value); + ass_add_font(ass->library, tag->value, + st->codecpar->extradata, + st->codecpar->extradata_size); + } else { + av_log(ctx, AV_LOG_WARNING, + "Font attachment has no filename, ignored.\n"); + } + } + } + + /* Initialize fonts */ + ass_set_fonts(ass->renderer, NULL, NULL, 1, NULL, 1); + + /* Open decoder */ + dec = avcodec_find_decoder(st->codecpar->codec_id); + if (!dec) { + av_log(ctx, AV_LOG_ERROR, "Failed to find subtitle codec %s\n", + avcodec_get_name(st->codecpar->codec_id)); + ret = AVERROR_DECODER_NOT_FOUND; + goto end; + } + dec_desc = avcodec_descriptor_get(st->codecpar->codec_id); + if (dec_desc && !(dec_desc->props & AV_CODEC_PROP_TEXT_SUB)) { + av_log(ctx, AV_LOG_ERROR, + "Only text based subtitles are currently supported\n"); + ret = AVERROR_PATCHWELCOME; + goto end; + } + if (ass->charenc) + av_dict_set(&codec_opts, "sub_charenc", ass->charenc, 0); + + dec_ctx = avcodec_alloc_context3(dec); + if (!dec_ctx) { + ret = AVERROR(ENOMEM); + goto end; + } + + ret = avcodec_parameters_to_context(dec_ctx, st->codecpar); + if (ret < 0) + goto end; + + /* + * This is required by the decoding process in order to rescale the + * timestamps: in the current API the decoded subtitles have their pts + * expressed in AV_TIME_BASE, and thus the lavc internals need to know the + * stream time base in order to achieve the rescaling. + * + * That API is old and needs to be reworked to match behaviour with A/V. + */ + dec_ctx->pkt_timebase = st->time_base; + + ret = avcodec_open2(dec_ctx, NULL, &codec_opts); + if (ret < 0) + goto end; + + if (ass->force_style) { + char **list = NULL; + char *temp = NULL; + char *ptr = av_strtok(ass->force_style, ",", &temp); + int i = 0; + while (ptr) { + av_dynarray_add(&list, &i, ptr); + if (!list) { + ret = AVERROR(ENOMEM); + goto end; + } + ptr = av_strtok(NULL, ",", &temp); + } + av_dynarray_add(&list, &i, NULL); + if (!list) { + ret = AVERROR(ENOMEM); + goto end; + } + ass_set_style_overrides(ass->library, list); + av_free(list); + } + /* Decode subtitles and push them into the renderer (libass) */ + if (dec_ctx->subtitle_header) + ass_process_codec_private(ass->track, + dec_ctx->subtitle_header, + dec_ctx->subtitle_header_size); + while (av_read_frame(fmt, &pkt) >= 0) { + int i, got_subtitle; + AVSubtitle sub = {0}; + + if (pkt.stream_index == sid) { + ret = avcodec_decode_subtitle2(dec_ctx, &sub, &got_subtitle, &pkt); + if (ret < 0) { + av_log(ctx, AV_LOG_WARNING, "Error decoding: %s (ignored)\n", + av_err2str(ret)); + } else if (got_subtitle) { + const int64_t start_time = av_rescale_q(sub.pts, AV_TIME_BASE_Q, av_make_q(1, 1000)); + const int64_t duration = sub.end_display_time; + for (i = 0; i < sub.num_rects; i++) { + char *ass_line = sub.rects[i]->ass; + if (!ass_line) + break; + ass_process_chunk(ass->track, ass_line, strlen(ass_line), + start_time, duration); + } + } + } + av_packet_unref(&pkt); + avsubtitle_free(&sub); + } + +end: + av_dict_free(&codec_opts); + avcodec_free_context(&dec_ctx); + avformat_close_input(&fmt); + return ret; +} + +const AVFilter ff_vf_subtitles = { + .name = "subtitles", + .description = NULL_IF_CONFIG_SMALL("Render text subtitles onto input video using the libass library."), + .priv_size = sizeof(AssContext), + .init = init_subtitles, + .uninit = uninit, + .query_formats = query_formats, + FILTER_INPUTS(ass_inputs), + FILTER_OUTPUTS(ass_outputs), + .priv_class = &subtitles_class, +}; +#endif