From patchwork Fri Sep 24 00:31:12 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 30537 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp988283iob; Thu, 23 Sep 2021 17:32:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJws1OmX8Um7Jks0dA7uChAfYH1GDZzy9P4bPued6kp4hDEgsyMFv+sOCXqHYlq5LbiXzHhT X-Received: by 2002:a50:d98d:: with SMTP id w13mr2005181edj.51.1632443562221; Thu, 23 Sep 2021 17:32:42 -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 ne20si8311932ejc.429.2021.09.23.17.32.41; Thu, 23 Sep 2021 17:32:42 -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=RKleppN3; 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 D983768A5E6; Fri, 24 Sep 2021 03:31:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11olkn2053.outbound.protection.outlook.com [40.92.18.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5DD6768A5AC for ; Fri, 24 Sep 2021 03:31:16 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X2gjD9/OhvTVr2lvyrTuS/TvtXQnczfTzTkIH1u4aniEK74fCRS/eZZ4qNM7WNU5KeU9X3hqqbKmPH5P79czZLVodr0AgsZHGT5tcQtsePiOkM/JXsKFLCfG+obf7X66K4nWaCWcDGShcCU+G0OeCkyyb/4om+DfEnaaODTb7CXskow4TJg33R/qS4o5sp2lVGeVg5jn0SxOXDBldrEYyDXYmpm/OAIF9HvMvqAv5rlU4rYSUZmfNa1v4Z1ohsuWYr39vi38gTI1sd5qQWGfD/kXH0LL7kfv06VspdS48xO0zKj8JG2rsh0feNfwJUJI2swOf0cNw0pYyqmtH4eBhw== 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=oXeb1y+10ySK1Fnz4zmLTuN2N8wCyxZDJsKYi2hnDIg=; b=dLnxcWdG2ryhhG9kzSa20X7051jwRuoXaMjEI3uQxTCpsFNi7HATwkyr6AxNOS0c2cjuys/vLF3sC5YCIEn+/RxMlSCK9OOcLBFPaAZ1T4koR9PB6uiTqkqJ7VxLydVUvnwgFM8V18qN9/We7QsvzL/HgaRyo/OV10Krdyd3ouZGVQb3JvI4aAc54xaJ2gZUZgfukIPwEB3fKaSHq16LoX8JDRkh9KHaC7vV91aUp0XZod1vxzxKEY4HYUC/k0rk5gJQXX776azfHY4MmSzpnH69eDggR951iNS8Hs0FissuNDo+iq2ix/3yuGt7qgzeea0Bb7eurqCzwdijbC/uJw== 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=oXeb1y+10ySK1Fnz4zmLTuN2N8wCyxZDJsKYi2hnDIg=; b=RKleppN3ZsY0P2jt27zXBz8jiExCBDFyFK3W/wGI+u+LqRu55Owi3XCXa2/aSGC13W159svBQgI+qwFbfoJH7Vc1lDPQsXDtXVymkQmxxxfIdpDYS5RzHjzxPpHmh348LQWnDzHa8wT8d3mzYpcwSysBjhMuplrdKw7N1q8PG+UHMOAUc00X742RtGQuXw6n5SRiQPfqJdqZhycA00erpb7G20OEflQADEFGut1i7LxatirUX3mPO2zRT8Ff//BxEsEeZszlTeBepCK5ojgKN+jXhCRTk+komEYMjguwlMp/qcBY2sG3RxFKRg12StQG8KZZPKncTrkcdMkFDwdmwg== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5743.namprd04.prod.outlook.com (2603:10b6:208:38::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.14; Fri, 24 Sep 2021 00:31:12 +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.4544.015; Fri, 24 Sep 2021 00:31:12 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v9 11/13] avfilter/stripstyles: Add stripstyles filter Thread-Index: AQHXsNt6c/DQYVYf+0qOf/VJSzgLkA== Date: Fri, 24 Sep 2021 00:31:12 +0000 Message-ID: References: <5bc443d3efba607cef8020fdacce3fbec59514b7.1632443311.git.softworkz@hotmail.com> <74b7003baacf9847f879d785c002e4a78293f57c.1632443311.git.softworkz@hotmail.com> <2b7a471b8c1a6fe4ce2c4133710261ecafbbf13d.1632443311.git.softworkz@hotmail.com> <64913621bc7223946c513126a1abe0ea2001d6f5.1632443311.git.softworkz@hotmail.com> In-Reply-To: <64913621bc7223946c513126a1abe0ea2001d6f5.1632443311.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: [9kblTJcf+dbD6Ve6JfKFDvMGTVY4ISOO] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 3a8e4ef2-26b0-4726-921f-08d97ef29cd6 x-ms-traffictypediagnostic: MN2PR04MB5743: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: oGNlrSsv497GogQie8h6c2HxOJqKsE1ubx6PLpiOgm45bs6C6q9jOSIzNyZot9u6UkU6D/zHD2n5iL8eYWYRYGLzYV6eqHMVfOwoW46BQCzWOd0otX5eDH80Xn+D0V6y8lu3PRx9MQUKWwK+qyzrabXQEHvJl00+kpLsYtXME3it2al3rMwRGYQcoybm/elxeqnL/CQkS1z9ofmKGqD4Ao5jHbdusJnfCFu0K68Hq01nJmMzuRzZTKL69Rv64i4/2CzAMF3rp4bmpeTKrPOSeYMrTricSviHmwP55/q4OQh9yYvu5raM7QitbbuMD0JQSlMWX4ywPHgupqpAtfuLi5wJgKgnNTA1wR3ajjjsPBoT46aJ1kUIR5mQT3lFLOqKiEQpI4WjKng8sDXII9C2UQeDZn5WlwADODIbgI4W9aseGB2v5YM5gx+QNUaMbomC x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: UYozpS9f78jDfLquzuxAe+AVPo6gg/W/5c2O4WZSBL9uUR0Vfv6bavQT0zLnDMGR29xr0N8pRVn4f2xosFJ0DtjzQq0WCO22cLh6ZXzb+6bktEU+ii54WPMq3m3rBcRNpwEvlgy3iY2kUxwomoNyGw== 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: 3a8e4ef2-26b0-4726-921f-08d97ef29cd6 X-MS-Exchange-CrossTenant-originalarrivaltime: 24 Sep 2021 00:31:12.6959 (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: MN2PR04MB5743 Subject: [FFmpeg-devel] [PATCH v9 11/13] 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: 7zofYlie8JrK - stripstyles {S -> S) Remove all inline styles from subtitle events Signed-off-by: softworkz --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_stripstyles.c | 211 +++++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 libavfilter/sf_stripstyles.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index e6fef97c08..309c404bf7 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -540,6 +540,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 8929080b46..b482f68286 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -528,6 +528,7 @@ extern const AVFilter ff_avf_showwavespic; extern const AVFilter ff_vaf_spectrumsynth; extern const AVFilter ff_sf_censor; extern const AVFilter ff_sf_show_speaker; +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..255fa7bf70 --- /dev/null +++ b/libavfilter/sf_stripstyles.c @@ -0,0 +1,211 @@ +/* + * 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.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 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) +{ + DialogContext dlg_ctx = { .ss_ctx = s }; + ASSDialog *dialog = ff_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); + + ff_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); + ff_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"), + .query_formats = query_formats, + .priv_size = sizeof(StripStylesContext), + .priv_class = &stripstyles_class, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), +}; +