From patchwork Thu Oct 21 03:19:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 31185 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2084:0:0:0:0 with SMTP id a4csp128148ioa; Wed, 20 Oct 2021 20:21:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHSHu8ChjoOyqD86rdKFiO9JhpHNpZ1KbGdYHiBiL2gi4+/cq98sJc3Oi3qNwCjMhxyDMv X-Received: by 2002:a17:906:5343:: with SMTP id j3mr4022624ejo.538.1634786496957; Wed, 20 Oct 2021 20:21:36 -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 sc11si7616714ejc.129.2021.10.20.20.21.36; Wed, 20 Oct 2021 20:21:36 -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=Jcroo8DG; 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 C5C8A68A12D; Thu, 21 Oct 2021 06:19:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10olkn2026.outbound.protection.outlook.com [40.92.42.26]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 24D5368A12D for ; Thu, 21 Oct 2021 06:19:52 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=b7dWWb2w0Oi4t4ga7w0nApkMMxHX/+V5zQo32utv4/VR5fiw5dnavL1pMvaR4wVldJvkhnlj2OJJ+63HdcPv42KDYuwhZHtwdM8icdgEd6MDFrcCwr7SEahkMA00IdF4Wti3Cpi2F0Xjjzv5VtXkxbXhA7RZkQr8uhsrpcT1rMcRUUn3G7Df+dXRzYoSrKm8CPFxER7A6a702hNtaFCdoZYa8ochE2gnK0V71dAIvvHp7p/dMZtI/knWmc5jvkMj9/QNLMyEsaQ6uAPsoKxRo4VMQgk5CwgTe2NpODYuWbN/kdj3QhrR1gMRXFkA/9qES2qJwOL4AVSsHCFvcWmqtA== 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:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4ia5LCjHZcpm/XAtAZvUeQE6QgUgPlxdw+c7YASiYG0=; b=MKA/pwmU4K0TjogHjZ3tO9F/8gV1guV0+9kmgJOysGPHBzcsfM3wb2XtBzgYwYnb7yxbNFWtEUGIgR9JlpG1B3NIjQnkZOADfrzuuVhI35o/iKyc9T3GqAweiyFy42vI9muYCLwHx4Wgviaf/HNyFNK4q0jjU9mVnn7w+3eqaz+sPapg/s/XI++/cGhsKzRbNj8+ppwreA3uZuv8uvU7Rv8urBDMd/UibalCPyPG+IPLfq9qIGk/rJqQJwc1cIlxvgt9nXc/qpUpkh8sFE7Pijx3xeTRbZMVpxMqTFcUl/O7/x/5x9muP8Vo4yNM4Y9qOV0ZrmAPHl/vLbFbsbXbSA== 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=4ia5LCjHZcpm/XAtAZvUeQE6QgUgPlxdw+c7YASiYG0=; b=Jcroo8DGbBdXmpq6v/MStS9hTHw5MBzfw+W/U1hn3a7o+BWVZJV3KvX81CtdNFF6wC0pS7nUYF7T2362vUO17bzAAlDATAYKnXhJ4mq4yATi2jDOg46/INpgHtJRzeQeJ0rZX45B2pcr5AQSD6sYShgSGo8JFvKd1phYjwx7oKUKbRljCUFqrjLiF9m2RkyNTnoCWUZl4tKxbuGTWnzBWKGkO4UiZnoEq5LbydQakZwQl6wbOYSV3sRNYvNODmgg9k6ajNzYa5qOvTcIkuALw9YeIHuNHJPnusDoAe4Vmg0aC/9cvBUBBqTw1Ri+ICGKtS+QwV7dxuVooZ4wx+3WYw== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5984.namprd04.prod.outlook.com (2603:10b6:208:d9::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Thu, 21 Oct 2021 03:19:49 +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.4608.018; Thu, 21 Oct 2021 03:19:48 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v14 11/14] avfilter/stripstyles: Add stripstyles filter Thread-Index: AQHXxiqBnPI/KYAPI0OgZhcN0GLmFA== Date: Thu, 21 Oct 2021 03:19:48 +0000 Message-ID: References: <4f225627a420d32b8766306e418c45bc8af41c47.1634785894.git.softworkz@hotmail.com> <4d6c9a34829586c3ad247e2423faa0d763fa01c4.1634785894.git.softworkz@hotmail.com> <541cf61d503ee53b51ebccada8f9cb087ae3c549.1634785895.git.softworkz@hotmail.com> <55d8df19375a269c01802b3ab9b0e4c30d9654eb.1634785895.git.softworkz@hotmail.com> In-Reply-To: <55d8df19375a269c01802b3ab9b0e4c30d9654eb.1634785895.git.softworkz@hotmail.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [0XVxwLKXkXoRuvk2TyV3EI10WjZlAZTi] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: f2e21417-659c-4498-0fd8-08d99441a3b5 x-ms-traffictypediagnostic: MN2PR04MB5984: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: shJAh8iTuiMF72FGOMV3aypVPRk3VrFqoRbAnz+00GPZlCxtWRi8u4eFBbjRP63nGW9Y05vxu6lAVHMHM1OjrM8ti3FaBRawIbNHin5STftdBgIHwszXu/8DfqwdaqPiz2dkk0v7+Y+xF0pQpkqm8HMxsXMCrAjLy1TUnQI7e7Vi3TKD/SGxGMUTV1lmBVPi2hO7BhOduFp4FGhv0X79fDXKDzAoNwQcfhLXGNDx9Muk23AxLbYrx4xMMovohUaDnxa9J9hwA3jkL9GGTAgrB6+/F0P3wdbYTM9UpXGshvX+VmM3jEU5QeHGAMb9cfM5z3yo9d8dKY+NaZsHC+9TwYLaouDFw0lHCt3rcBQcEZ6EH7TrR51IGXEjrY5f5V8dXVdfJGlsbo3D9ksUjSjD+QTUrZ2j6O8tl0qJHBiBNjyipDWpeYBPyvqq1hoag3VS2PQFYNU5NnWuZgrQyJXqc3HwzUsgEG9pD7G+lrfixVhoNdfyxC5XhQ/fb86kcL1SdUi/ULiIZwSGt96BLmvjjSnqai+OGizYsgBkulz89OQASIg3pT4kl5s0bpE6Q5PN8Ewo/wF1F2Glajubi/mU8g== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: KFu+3fa3e6JS+EZB09j5NXuT20Wk3uTJGk4PqxMVGJb0AF/DLmcGL1zGWrrUQBfvDXOKvGnhdYYUk51ihphhALE6RG01ZLA2aSS+gH3d082osso3PYQrGElyRkRPktAgVJ0+h477bQvKoEzljCRErh6YpIbFQdrHUWEYhpmSkdAslavDK5ClgerS53IsttxcLvqRwcUJV1SLDNbXDbF2JIc1qPi4WyLnpUHIe1STejmXtCClS1F7i6hJWYnjYlPuVMtBOHEPOqpV9X9n4TCUTQW4ONNPfyF8BV+vcLQGBP0HZI2djOi1FAZetfR37ZPamZT5BmOuDxPp6bMAS3h6Tv5sYinXNsOmw6u5+QA+RGD8rpNT+ynniERTZLzBbxBEheEZ293gzIQ0z3Cm8n0jfTm77e8apIimYyyZhOcuWNZk9hAj1VjM5WfAl0CCXkWvAfWYEc1m68bouP5rKH41Ahe5V5QOA52L4LUcQq+IFquap/0qlxhCqeCdimBce95ozmDA/WzPgaLzLi8OZ+CLMI438R99UiX5Yo8mf/GmKn//dcuxImXBZghcvFZcywjrqneGEEh6vU2txCbgnmnO7xdVals13t55qKy9TCcET0Pg/pnAVE5NxxnGOlh+EVsA8t9mCqh9gjOg0NJVVGsmEdXqtEpYBFj2VOipxn6UP3QwEAkO8oLShiiIpieOnQL4Lk9xqtrRdBYcZCMR/Y9XYA== 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: f2e21417-659c-4498-0fd8-08d99441a3b5 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Oct 2021 03:19:48.8853 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-userprincipalname: softworkz@hotmail.com X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5984 Subject: [FFmpeg-devel] [PATCH v14 11/14] 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: O/f0AQZYXdYX - stripstyles {S -> S) Remove all inline styles from subtitle events Signed-off-by: softworkz --- doc/filters.texi | 34 +++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_stripstyles.c | 191 +++++++++++++++++++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 libavfilter/sf_stripstyles.c diff --git a/doc/filters.texi b/doc/filters.texi index 6e144acfcd..c8ea318520 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -25389,6 +25389,40 @@ ffmpeg -i "https://streams.videolan.org/ffmpeg/mkv_subtitles.mkv" -filter_comple @end example @end itemize +@section stripstyles + +Remove all inline styles from subtitle events. + +Inputs: +@itemize +@item 0: Subtitles[TEXT] +@end itemize + +Outputs: +@itemize +@item 0: Subtitles[TEXT] +@end itemize + +It accepts the following parameters: + +@table @option +@item remove_animated +Also remove text which is subject to animation (default: true) +Usually, animated text elements are used used in addition to static subtitle lines for creating effects, so in most cases it is safe to remove the animation content. +If subtitle text is missing, try setting this to false. + +@end table + +@subsection Examples + +@itemize +@item +Remove all styles and animations from subtitles: +@example +ffmpeg -i "https://streams.videolan.org/samples/sub/SSA/subtitle_testing_complex.mkv" -filter_complex "[0:1]stripstyles" -map 0 output.mkv +@end example +@end itemize + @section textmod diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 45f4c67d2c..41e71e2db2 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -548,6 +548,7 @@ OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o OBJS-$(CONFIG_CENSOR_FILTER) += sf_textmod.o OBJS-$(CONFIG_SHOW_SPEAKER_FILTER) += sf_textmod.o 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 108ce550cf..49b10b882d 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -536,6 +536,7 @@ extern const AVFilter ff_avf_showwavespic; extern const AVFilter ff_vaf_spectrumsynth; extern const AVFilter ff_sf_censor; extern const AVFilter ff_sf_showspeaker; +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..ed35c2afcc --- /dev/null +++ b/libavfilter/sf_stripstyles.c @@ -0,0 +1,191 @@ +/* + * 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 "libavutil/opt.h" +#include "internal.h" +#include "libavutil/ass_split_internal.h" +#include "libavutil/bprint.h" + +typedef struct StripStylesContext { + const AVClass *class; + enum AVSubtitleType format; + int remove_animated; +} StripStylesContext; + +typedef struct DialogContext { + 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; + + av_log(s->ss_ctx, AV_LOG_DEBUG, "dialog_text_cb: %s\n", text); + + 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 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) +{ + DialogContext dlg_ctx = { .ss_ctx = s }; + ASSDialog *dialog = avpriv_ass_split_dialog(NULL, ass_line); + char *result = NULL; + + if (!dialog) + return NULL; + + dlg_ctx.ss_ctx = s; + + av_bprint_init(&dlg_ctx.buffer, 0, AV_BPRINT_SIZE_UNLIMITED); + + avpriv_ass_split_override_codes(&dialog_callbacks, &dlg_ctx, dialog->text); + + if (av_bprint_is_complete(&dlg_ctx.buffer) + && dlg_ctx.buffer.len > 0) + result = ass_get_line(dialog->readorder, dialog->layer, dialog->style, dialog->name, dialog->effect, dlg_ctx.buffer.str); + + av_bprint_finalize(&dlg_ctx.buffer, NULL); + avpriv_ass_free_dialog(&dialog); + + return result; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + StripStylesContext *s = inlink->dst->priv; + AVFilterLink *outlink = inlink->dst->outputs[0]; + int ret; + + outlink->format = inlink->format; + + ret = av_frame_make_writable(frame); + if (ret <0 ) { + av_frame_free(&frame); + return AVERROR(ENOMEM); + } + + for (unsigned i = 0; i < frame->num_subtitle_areas; i++) { + + AVSubtitleArea *area = frame->subtitle_areas[i]; + + if (area->ass) { + char *tmp = area->ass; + area->ass = process_dialog(s, area->ass); + + if (area->ass) { + av_log(inlink->dst, AV_LOG_DEBUG, "original: %s\n", tmp); + av_log(inlink->dst, AV_LOG_DEBUG, "stripped: %s\n", area->ass); + } + else + area->ass = NULL; + + av_free(tmp); + } + } + + return ff_filter_frame(outlink, frame); +} + +#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"), + .priv_size = sizeof(StripStylesContext), + .priv_class = &stripstyles_class, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), + FILTER_SINGLE_SUBFMT(AV_SUBTITLE_FMT_ASS), +}; +