From patchwork Mon Dec 13 23:40:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 32468 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp6141488iog; Mon, 13 Dec 2021 15:43:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJyUW9iXf1vL8XbgpoepAmVsyW8sk7eRaZwvd4nSN3Kik+8jqlrKhI0K6+YDGlY2/l+MV65y X-Received: by 2002:a50:bb2a:: with SMTP id y39mr2731029ede.348.1639439014043; Mon, 13 Dec 2021 15:43:34 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g9si16331511ejk.428.2021.12.13.15.43.33; Mon, 13 Dec 2021 15:43:34 -0800 (PST) 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=uCYGKMFo; 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 7660A68B0ED; Tue, 14 Dec 2021 01:40:59 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11olkn2058.outbound.protection.outlook.com [40.92.18.58]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8F3B368B0D8 for ; Tue, 14 Dec 2021 01:40:57 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EUdkaBJOdMZAawwRJAvzIVDfgVSQayz4HTPA43Dpmy+ooj80JUxH96dGxTOQXzxg7phK6blnv4WGbzidCLwCb7WQ/btHClrWrfnGh1yF0b7585HtuoHgavEU5j0xj4JUI6EtxngS9Zh18zbJZoo8DuCeV/1USC3SEI0SBb5e4a9OJQejFeRcoiify1Dl2bpYAcYAMR7BUvqkJH8Tt7Ho5jOZAKFjW2C1L5UolLuIQMxg3asYnMK9vrZBDoEMgvQiFz2qc2NtOVVzTOtuKt7642L4ntZR50SdXzKvRACDsvFkqMxtCxLqr1rtpR67tZhfZQY/i8m62wdY3Q2t+f0cRw== 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=8Ceo1xIugAWYE8IwIkGlYhuBlWyRCi9Hh3Dth0WQ7sM=; b=KV+Yjx7XYmFIS37rhh0XQ30Js1ue6qv2HHk2s52iMwi+bZkiDtPVPv+dwaEJarB8plrYH7zScg9bXGdU67CNorYM0agVs4hx5ShK31J0tWBDdbFzhCJM2RSihje4YOSxO5avEkHEq2LgUYby06iy9XWg1LDPCw0HM+v/7DWj1FO25Ly+Tz/2HWj5oHFMPArEWO3FHHoU3iG8sIzIE0tH+LcW9hdDu89qA4/1C8BRyppBUYLRul68+TopDls3K0xnZkXm6I1ZTGA89mibn5oomCly+ZNeOjxkTgELNTmhUSQkc+A0dhZowuyEq3ezoPpf4hUhbby3507OVm3OgWNP2g== 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=8Ceo1xIugAWYE8IwIkGlYhuBlWyRCi9Hh3Dth0WQ7sM=; b=uCYGKMFoiJUmHRSNdLHkjkwc2x8d3icktENY8xDycgn9Yjlu7x841F/uSz8L2WNcwD2bMyaAkoJtr2fAXYft10JygGRTtdwL2cuvInHt0GXN0wHPIGNMXmaUEIsU/oaPlbM7D188K90t/V0PORtHmUOodGkEVzeK1G2HbqvEhL83gkLu6pGjw3zOnlePOxra/NNx40Ifhz8v0ES9BVM7GKsHlTlQfbVMeXgivdtSky+LntehMVOxei1JkhpKv779gxpI/+HBjzxz9gSckR66hwTfY107lo0msAbPCy9RxT8oV6fEkcG75cVsNL4aYa++qrSHvWsdo7aL6M0ks2vgAA== Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:b::20) by DM8P223MB0397.NAMP223.PROD.OUTLOOK.COM (2603:10b6:8:b::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Mon, 13 Dec 2021 23:40:54 +0000 Received: from DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::9c8d:fc63:9488:9775]) by DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM ([fe80::9c8d:fc63:9488:9775%7]) with mapi id 15.20.4778.018; Mon, 13 Dec 2021 23:40:54 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v24 17/21] avfilter/stripstyles: Add stripstyles filter Thread-Index: AQHX8HreVY0gr8CwNkSsCoXbQeojNg== Date: Mon, 13 Dec 2021 23:40:54 +0000 Message-ID: References: <1ef42eb01b3627bbad539abd46206f320bb39697.1639438462.git.softworkz@hotmail.com> <4148d77a82d4b7e229fc81ce9bc1bc06904533f1.1639438462.git.softworkz@hotmail.com> <2ac798924ee984010d2e85f00bc992d9aa106171.1639438462.git.softworkz@hotmail.com> <300ea7188b331ca5e00dd7e04c03df8aba2a99c0.1639438462.git.softworkz@hotmail.com> <9474726b26f0d3e8f353d07caed2fbd33cdf473a.1639438462.git.softworkz@hotmail.com> <424522c96b5cdf6b03faa6bdceb1d25449463429.1639438462.git.softworkz@hotmail.com> <90caff245055005e6a4a890b3665da55aa853b31.1639438462.git.softworkz@hotmail.com> <0ebcf3ed2adb02d3ffb243a499b434c3bdd976a7.1639438462.git.softworkz@hotmail.com> <4b5c1c1185075fd6e5d8ef70e07fdce6f896d699.1639438462.git.softworkz@hotmail.com> In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [4JQ1hbcnzbPmg/cm3wG6aorSBPwx6HH8] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 86202cd9-f856-47c8-0c34-08d9be92016a x-ms-traffictypediagnostic: DM8P223MB0397:EE_ x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 1ScyDtPLbk+suXvYrRpBrkPAFyb+ZIKxkfkdDrSLCeZ/Plx2B26ATu+mL2u0JUEtjK8qxB8gLKDRcAg2DAzdsSS+tv5614h3kzVFEpc9qwDOZ04hgZn1jXuVwvmsjcVPrn1TBTXClOLvmxZyXIDLgXz2pBsGRGVrBWebPE+BipZCGSQsIrC+DfaA6TY21Bqiq5kvwdHHjT3/6E5hZzRifo0pEAlU+2RRuto5U8e7bRt8penFKAmCGu2AnpbBz3K1ksAJb5PUNCPGMXVImZeLmJotrXw2lhc8wFBiAy8znl5esn1ajl3Ms9agfyVFfprYYFzX97ceFSwHpPGjVDC43ZsZIXaHHom3X6WEOVNTbYFC/jyTfkwg80kXdyl1YO7gdtXRSjablsrMsElw4sMUIrrlR6dbUv9uJI1Nb0hQwrBlrIFpsNOso9WhFr1Ngt9X75MLECioHIuVdCeOWonuGwIY9eBZWH0Fpy9T9JoRdG77flftupwOcCNuL7iDgMSb0Nssss6vqDhcXZXJ/91JNbiIwYwgYt7qxl/UZYO1cI88n8JTgTo7uZwI/pGNg50GbIGIYiMh6WvEuoyc2LlLyVdh+9ZE7Z84lXbcR57R+OTPeLRmONbaGeGSNbbsiUQxLCw/0ZlJx6+6/QZ0pElZcCQPv2Daj1cgIEVxRHEFOY4= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?utf-8?q?yx1Q7A50AJnjK5KCGAa0f6p/j46v?= =?utf-8?q?+MhXe95zvTw7/YCW5jowZWjwI0R3Nf2KhUTQgLPz79iVnHHHAaWNw9cblYWNQnV2S?= =?utf-8?q?yp1bJy0fyJWQ0URR6f1BSG+ahERvGU8cz6zXY3ZqYks/czU3eW5RbaQDnQZAp5U6u?= =?utf-8?q?a0j6X6t8ObITxXaKiuL4aEWy9PoTWq49TTB7xxzJ1f/0dLOiabdWDnDQfRS/KFDk5?= =?utf-8?q?7aWUSAer/cfiZ4kW70ug75pyUWofM+k4/HTmir8+iCahjsVlfoP4KiJ6/dUtM3kW4?= =?utf-8?q?h3cfoZypfnfL8gh7nxG8UchOMy8yrK1f0zfQwCmHqJTkKUAYXojB5Baa5c0+4GOGH?= =?utf-8?q?s4qUDRZWdcDscY+rPFDJuAm+1TplpCURqVMBTQpshmviXhNssbKvzxFoyauWJX38z?= =?utf-8?q?Br5f+xhmfL0jDNrhuxloocnjaCa3kbfOr6IGonlPn/eevWq5DligwSluCque6qKul?= =?utf-8?q?/pjxS+gR7WW4Y2/rohgnWtIyzZ1UG1cRXXdeZN4QtLrl6Kd+WLRiV/j3lOJWPuxYc?= =?utf-8?q?F+ytRjFpoTVLO6zNPy8jwy08NV88W0QfmmIeFAwj1WUnRSuTt627SWQAAnLiVi+ab?= =?utf-8?q?xiVfQpq1s0Uh37ncsTV/iLeutezdZP6JNlEsOtLg/W8+u08rkmlWL7IUKYKZ6Kamz?= =?utf-8?q?dKDkbT4VqGBV/uiZdujZZQjv0jXoZB46lSoKfytYVVJJOs/yZrCyNZHOfcmefD2q7?= =?utf-8?q?u8Zz3J5CEgz25tkncFzAB1GztiU1FiT1xhGnOls1gyVa9ncAWPV3YgNvNCOIJYE4u?= =?utf-8?q?PiTrcJFK/PS93C8bX+5C/aFfFCk7NKjO+f4YglL0/Gx0u0qNPOT29Z09G5Q49V46J?= =?utf-8?q?ruy/ybftM669SH6XJnQxlJBUoW3eZW4GcKwpMOLBNV2UAXAio+ELVEb9yFBuYTMY0?= =?utf-8?q?4r30bmDa6sxoifNiKxSMID3MZv4mkNHK2DeBA5rPcS0Q=3D=3D?= MIME-Version: 1.0 X-OriginatorOrg: sct-15-20-4755-11-msonline-outlook-1ff67.templateTenant X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DM8P223MB0365.NAMP223.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 86202cd9-f856-47c8-0c34-08d9be92016a X-MS-Exchange-CrossTenant-originalarrivaltime: 13 Dec 2021 23:40:54.6899 (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: DM8P223MB0397 Subject: [FFmpeg-devel] [PATCH v24 17/21] 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: r7ZzqEhpQOrq - stripstyles {S -> S) Remove all inline styles from subtitle events Signed-off-by: softworkz --- doc/filters.texi | 37 +++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_stripstyles.c | 196 +++++++++++++++++++++++++++++++++++ 4 files changed, 235 insertions(+) create mode 100644 libavfilter/sf_stripstyles.c diff --git a/doc/filters.texi b/doc/filters.texi index 6ef95c4926..4806132cc1 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -25780,6 +25780,43 @@ ffmpeg -i "http://streams.videolan.org/samples/sub/SSA/subtitle_testing_complex. @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. + +@item select_layer +Process only ASS subtitle events from a specific layer. This allows to filter out certain effects where an ASS author duplicates the text onto multiple layers. + +@end table + +@subsection Examples + +@itemize +@item +Remove styles and animations from ASS subtitles and output events from ass layer 0 only. Then convert asn save as SRT stream: +@example +ffmpeg -i "https://streams.videolan.org/samples/sub/SSA/subtitle_testing_complex.mkv" -filter_complex "[0:1]stripstyles=select_layer=0" -map 0 -c:s srt output.mkv +@end example +@end itemize + @section textmod diff --git a/libavfilter/Makefile b/libavfilter/Makefile index d47bdfa8f6..d69fa61c91 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -559,6 +559,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 cc8f43c925..32a0ebe71b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -548,6 +548,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..82cb9c7647 --- /dev/null +++ b/libavfilter/sf_stripstyles.c @@ -0,0 +1,196 @@ +/* + * 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; + int select_layer; +} 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; + + if (s->select_layer >= 0 && dialog->layer != s->select_layer) + 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_INFO, "original: %d %s\n", i, tmp); + av_log(inlink->dst, AV_LOG_INFO, "stripped: %d %s\n", i, 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 }, + { "select_layer", "process a specific ass layer only", OFFSET(remove_animated), AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX, 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), +}; +