From patchwork Sat Sep 11 08:40:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 30146 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp2248662iov; Sat, 11 Sep 2021 01:40:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjP1AV6JjXkhXPnyX1cAPM26gmrPsD7o8+AtzEboXZnUqxnZJP58Y4vK+6BmmeQvEXyEwE X-Received: by 2002:a17:906:9989:: with SMTP id af9mr1908583ejc.480.1631349655534; Sat, 11 Sep 2021 01:40:55 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id dp15si1333424ejc.221.2021.09.11.01.40.55; Sat, 11 Sep 2021 01:40:55 -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=@hotmail.com header.s=selector1 header.b=U9PDvfTc; arc=fail (body hash mismatch); 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=NONE dis=NONE) header.from=hotmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 01CA868A799; Sat, 11 Sep 2021 11:40:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam08olkn2046.outbound.protection.outlook.com [40.92.45.46]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 092CF68A826 for ; Sat, 11 Sep 2021 11:40:46 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ahB+3l7SyvcuNHoLKABqRBNyQtb7J9a/gfv16qaMO8geFgcmwx5LaApfXADdaXwuAqxNe5aGMVjpUZwtXjiBJaD3d+vyLCBWbTBkIECbgEDnnnei96uE8FE4IjImzwx1ynny0pqcxB9iTn7IXudR6OFggd7iNng8K5Cjv50VNfEmbtyTrNP1en7nfZFlfnI1/ftz5gjbx/j76s9jqw6a3y/3IryuHD6r6azgDwMDtoaXlZSOvcrBYYd5OH+/RhhAJ75sqRsCD1lIOv2q26ah1ATetdokZUFI5GBjxSacbc/OUG5xcLO8uJQuErepRpE2MJSPoPOvJbw3Q0DXvT36+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=pz1HmFG1xQWXtCcif8QPO46yWQABGbr8LP9ZKDKyDUw=; b=iynuBo8f70WhBvxuGE7Kj1yhG+ckLqcn36XLMf5+1UR2rBv906+k8ahDOr5wPNzcc3WcSem235Hcsok1jNRbOfptbkwCNwDu5wu1ctWgf+uzdKEfdGM5uKoaTVIldl1vTBpivRYbaMquS1yMiHesLgytsYbfwRDa4z8DoNNk2YEMheOkBTAu3AuUN68wpRGzJbV2aLsX6oT90Ol9+AEiKTzEV5YGrslk28xRJo3HCmI6qExhYoYBl/aGy0PPu4BJ7EhtvBGLofK5WT58npk6P883sIqHfNbrJP3Qva52T1ROAGf3hBajSLpK57MMwOMHaCjWgwDZbBBMhd2DNsPP6g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pz1HmFG1xQWXtCcif8QPO46yWQABGbr8LP9ZKDKyDUw=; b=U9PDvfTcJgnF1cnPBB/DdBBfxC9wSnaat+0AXfLA8BOYaBZ0O5bB9A0SzwhWUKThbCC+VqLVyAXLXLNnGcxKk2m0SZVGyVUgD99+/KbEyd/C8AFZx3M2kfUok7jdQsNfiEVenSeaw3Y+2IBygqSMrTjL0MJ97kF6li43oOIOSSnVB4fuLAz/o/VjB3GxXJ5L/p3iyLfP4nt4lV8mCoEVcjhJHEvzqwj9V+WaBDYclGSNpkETNypkz9+BndsWssqSouo6Od1WxlgHn1UPmnuCNsukmVNRGK00OGkHp6CYF6T6CU84jd4iJlEDpNjVsz+vpxWXzPH+NVOe9Vyg0MGc2Q== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5727.namprd04.prod.outlook.com (2603:10b6:208:a5::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4500.14; Sat, 11 Sep 2021 08:40:44 +0000 Received: from MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::ecfe:2528:2012:22cb]) by MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::ecfe:2528:2012:22cb%5]) with mapi id 15.20.4500.018; Sat, 11 Sep 2021 08:40:43 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v4 16/18] avfilter/stripstyles: Add stripstyles filter Thread-Index: AdemwaK+97Jc4GN3Q06bI8Ck9TbtCQ== Date: Sat, 11 Sep 2021 08:40:43 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [nwxZa0HzWsk19y/1iPIXtkYV1DRYYkWv+xHFPMGGU3Q=] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 60854430-68af-49f4-098a-08d974ffd808 x-ms-traffictypediagnostic: MN2PR04MB5727: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: AUQUpK79ueR077/Hs0gSo/bAEa+eWg+QR93kGeNRkSUeEo4Tv2jk/B1Yfb2W1UOCoBig4oysVIDh7MDQlUFAPhwwvt2qsJ6rfRCjFAMr+Ib0PYITaupKaPZ+r1md3QSxVV3a5FS3Hk1j2SNe42dFQICQrnMEbkgqWcQewU8GKVGWDpIz1FdqD4+ri4h+00wpftf9BAapjK8gHmG6iBtKR4nYvoQY3r4A6KbddCQkMYDCEp55WV8vOmTiAb5EWt4ZJer9fPJZ81BnQTbd18Hjx01q/vb/yuRYvxVXuBVy5E8onu2oeLKk4ivuQVrMfaLBB3B7WpGswO3EJOvo1JUzfBtzQ6HhU0aT6+H4ljj+4rQxLdoYNY4Dz9c3r4jgfjwWpxtWNEjVFAb/PF2xC7N1TLdFvEFu2GQvM04h82fKkWyKuM7nLh6m2RoKPhkqDVj2 x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: idN54yEK32qFWCp1oqVeSJ3IK6mSyLEBkd9HiCLKTwfVqCVcpPclVEx/lE0ng3EBGOGFtK/9rj4tepLNiwvYPw2/4sUayG8rsFh2D//nonN0FsOClRMwdIUpOAyiFqKpfpaF09klbX8Z6zO7DdpDfA== x-ms-exchange-transport-forked: True MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-3174-20-msonline-outlook-529c7.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MN2PR04MB5981.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 60854430-68af-49f4-098a-08d974ffd808 X-MS-Exchange-CrossTenant-originalarrivaltime: 11 Sep 2021 08:40:43.8334 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5727 Subject: [FFmpeg-devel] [PATCH v4 16/18] avfilter/stripstyles: Add stripstyles filter 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: mP888AZX59z2 Signed-off-by: softworkz --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_stripstyles.c | 246 +++++++++++++++++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 libavfilter/sf_stripstyles.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 5a5a4be47e..626e1bed74 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -536,6 +536,7 @@ OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o # subtitle filters OBJS-$(CONFIG_TEXTMOD_FILTER) += sf_textmod.o +OBJS-$(CONFIG_STRIPSTYLES_FILTER) += sf_stripstyles.o # multimedia filters OBJS-$(CONFIG_ABITSCOPE_FILTER) += avf_abitscope.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 6d7a535ee8..8f7d697408 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -524,6 +524,7 @@ extern const AVFilter ff_avf_showvolume; extern const AVFilter ff_avf_showwaves; extern const AVFilter ff_avf_showwavespic; extern const AVFilter ff_vaf_spectrumsynth; +extern const AVFilter ff_sf_stripstyles; extern const AVFilter ff_sf_textmod; extern const AVFilter ff_svf_graphicsub2video; extern const AVFilter ff_svf_textsub2video; diff --git a/libavfilter/sf_stripstyles.c b/libavfilter/sf_stripstyles.c new file mode 100644 index 0000000000..de2be0b1a2 --- /dev/null +++ b/libavfilter/sf_stripstyles.c @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2021 softworkz + * + * 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 + * text subtitle filter which removes inline-styles from subtitles + */ + +#include + +#include "libavutil/avassert.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "internal.h" +#include "libavcodec/avcodec.h" +#include "libavcodec/ass_split.h" + +typedef struct StripStylesContext { + const AVClass *class; + enum AVSubtitleType format; + int remove_animated; +} StripStylesContext; + +typedef struct DialogContext { + const AVClass *class; + const StripStylesContext* ss_ctx; + AVBPrint buffer; + int drawing_scale; + int is_animated; +} DialogContext; + +static void dialog_text_cb(void *priv, const char *text, int len) +{ + DialogContext *s = priv; + if (!s->drawing_scale && (!s->is_animated || !s->ss_ctx->remove_animated)) + av_bprint_append_data(&s->buffer, text, len); +} + +static void dialog_new_line_cb(void *priv, int forced) +{ + DialogContext *s = priv; + if (!s->drawing_scale && !s->is_animated) + av_bprint_append_data(&s->buffer, forced ? "\\N" : "\\n", 2); +} + +static void dialog_drawing_mode_cb(void *priv, int scale) +{ + DialogContext *s = priv; + s->drawing_scale = scale; +} + +static void dialog_animate_cb(void *priv, int t1, int t2, int accel, char *style) +{ + DialogContext *s = priv; + s->is_animated = 1; +} + +static void dialog_move_cb(void *priv, int x1, int y1, int x2, int y2, int t1, int t2) +{ + DialogContext *s = priv; + if (t1 >= 0 || t2 >= 0) + s->is_animated = 1; +} + +static const ASSCodesCallbacks dialog_callbacks = { + .text = dialog_text_cb, + .new_line = dialog_new_line_cb, + .drawing_mode = dialog_drawing_mode_cb, + .animate = dialog_animate_cb, + .move = dialog_move_cb, +}; + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats; + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + static const enum AVSubtitleType subtitle_fmts[] = { AV_SUBTITLE_FMT_ASS, AV_SUBTITLE_FMT_NONE }; + int ret; + + /* set input subtitle format */ + formats = ff_make_format_list(subtitle_fmts); + if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0) + return ret; + + /* set output video format */ + if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0) + return ret; + + return 0; +} + +static char *ass_get_line(int readorder, int layer, const char *style, + const char *speaker, const char *effect, const char *text) +{ + return av_asprintf("%d,%d,%s,%s,0,0,0,%s,%s", + readorder, layer, style ? style : "Default", + speaker ? speaker : "", effect, text); +} + +static char *process_dialog(StripStylesContext *s, const char *ass_line) +{ + ASSDialog *dialog = ff_ass_split_dialog(NULL, ass_line); + char *result = NULL, *text; + DialogContext dlg_ctx = { 0 }; + + if (!dialog) + return NULL; + + dlg_ctx.ss_ctx = s; + + av_bprint_init(&dlg_ctx.buffer, 0, AV_BPRINT_SIZE_UNLIMITED); + + ff_ass_split_override_codes(&dialog_callbacks, &dlg_ctx, dialog->text); + + av_bprint_finalize(&dlg_ctx.buffer, &text); + + if (text && strlen(text) > 0) + result = ass_get_line(dialog->readorder, dialog->layer, dialog->style, dialog->name, dialog->effect, text); + + av_free(text); + ff_ass_free_dialog(&dialog); + + return result; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *src_frame) +{ + StripStylesContext *s = inlink->dst->priv; + AVFilterLink *outlink = inlink->dst->outputs[0]; + AVSubtitle *sub; + int ret; + AVFrame *out; + + outlink->format = inlink->format; + + out = av_frame_alloc(); + if (!out) { + av_frame_free(&src_frame); + return AVERROR(ENOMEM); + } + + out->format = outlink->format; + + if ((ret = av_frame_get_buffer2(out, AVMEDIA_TYPE_SUBTITLE, 0)) < 0) + return ret; + + out->pts = src_frame->pts; + out->repeat_pict = src_frame->repeat_pict; + out->pkt_dts = src_frame->pkt_dts; + out->pkt_pos = src_frame->pkt_pos; + out->pkt_size = src_frame->pkt_size; + out->pkt_duration = src_frame->pkt_duration; + out->reordered_opaque = src_frame->reordered_opaque; + out->best_effort_timestamp = src_frame->best_effort_timestamp; + out->flags = src_frame->flags; + + sub = (AVSubtitle *)src_frame->data[0]; + + if (sub) { + AVSubtitle *out_sub = av_memdup(sub, sizeof(*out_sub)); + if (!out_sub) + return AVERROR(ENOMEM); + + out->buf[0] = av_buffer_create((uint8_t*)out_sub, sizeof(*out_sub), avsubtitle_free_ref, NULL, AV_BUFFER_FLAG_READONLY); + out->data[0] = (uint8_t*)out_sub; + + if (sub->num_rects) { + out_sub->rects = av_malloc_array(sub->num_rects, sizeof(AVSubtitleRect *)); + } + + for (unsigned i = 0; i < sub->num_rects; i++) { + + AVSubtitleRect *src_rect = sub->rects[i]; + AVSubtitleRect *dst_rect = av_memdup(src_rect, sizeof(*dst_rect)); + out_sub->rects[i] = dst_rect; + + if (src_rect->ass) { + dst_rect->ass = process_dialog(s, src_rect->ass); + + if (dst_rect->ass) { + av_log(inlink->dst, AV_LOG_DEBUG, "original: %s\n", src_rect->ass); + av_log(inlink->dst, AV_LOG_DEBUG, "stripped: %s\n", dst_rect->ass); + } + else + dst_rect->ass = av_strdup(""); + } + } + } + + av_frame_free(&src_frame); + return ff_filter_frame(outlink, out); +} + +#define OFFSET(x) offsetof(StripStylesContext, x) +#define FLAGS (AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_FILTERING_PARAM) + +static const AVOption stripstyles_options[] = { + { "remove_animated", "remove animated text (default: yes)", OFFSET(remove_animated), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS, 0 }, + { NULL }, +}; + +AVFILTER_DEFINE_CLASS(stripstyles); + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_SUBTITLE, + .filter_frame = filter_frame, + }, +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_SUBTITLE, + }, +}; + +const AVFilter ff_sf_stripstyles = { + .name = "stripstyles", + .description = NULL_IF_CONFIG_SMALL("Strip subtitle inline styles"), + .query_formats = query_formats, + .priv_size = sizeof(StripStylesContext), + .priv_class = &stripstyles_class, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), +}; +