From patchwork Mon Aug 30 08:17:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 29870 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp3738099iov; Mon, 30 Aug 2021 01:17:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw6AKkkuhrKTIUjgqLMBDhaSooPyOKDAR7LIZt6BRn3lg69e4JdYsdHBWhIHq8dl2dcKrIu X-Received: by 2002:a50:ff19:: with SMTP id a25mr21948832edu.311.1630311458856; Mon, 30 Aug 2021 01:17:38 -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 hv22si8573765ejc.188.2021.08.30.01.17.38; Mon, 30 Aug 2021 01:17: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=@hotmail.com header.s=selector1 header.b=ZR3fGpjP; 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 745EB68A2D5; Mon, 30 Aug 2021 11:17:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam08olkn2061.outbound.protection.outlook.com [40.92.46.61]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7933F68A10A for ; Mon, 30 Aug 2021 11:17:08 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WX2BeKnMrYfi4evm9CXSUjBSzS5Kf4+X0hD2LBY/jji6H3ADTCS/Q7Jlsv81Vuqu2CWqZ60y/8TeWic20BiFQvEoYhaxiZCXL3G+O1G2oTP6/Jm0RRpMcjsP/4HmRAZd3Tm2g9Wja/mtFTIlRb2QvM/c59cwu3Y1lN13LvvlT615Ng6REyaE89ASmvGxq7KN5dCP1/haiNciWVbrT7lwYD/gTZfue0SO4h9Ka3IYxY556Ikm3dK88Phjb/jRx7St41+wpgzGnHhUE7VZ7cwXl427ewvjNsj4aGEjFQn8b9TVzkDJbeDNSFHmK9wRNg1v/H/EtMAU8US/Zale6FDM6g== 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-SenderADCheck; bh=cliA7OcqsRBJmhkPDtin53W9YR2aJY6xcHqIcJpgkVU=; b=mDx8X4u3ITAHuoqUlolRBBnaKmE1cQVjfuuxuvcVuTCkb/ANN0vWHT6Q2ItPt0X998d0VT+zVxvLYp7yY7j5+TaMJ5ED9fbdn4/bvmx7+SntUhCFByiZ+KBRrNmBosbmQgCGUXSmgojFDmud4B5ze85aHpmH0zi8KNm8TeCU+Elyc3IsFiZnmRm1Hue18sY+1ioKFowYpajW4y070b1WicdTWa8krgr8wU7j3oVn15rOPRhC7PzU9qgPFQNHWCnMkjGtHF38hfUyzH4FD6M07R4PiHBJTsgktx7jaRCCk2Y2AORAgxeGqyOLIZQjMm86asflSAHLnwvECQp78Nhiww== 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=cliA7OcqsRBJmhkPDtin53W9YR2aJY6xcHqIcJpgkVU=; b=ZR3fGpjPdUE8F+gg72A3QfnmWwj8rviGfIGcOnglOMPoKMWTvYICVkTRaAsoqZ0fRWNhtaqklZjVqTOxAG+FwTbzW7AmcoJVbiN47bup2mRquHmotuwy8+fE/w25SGgWG1p+A4k5ZFtsBcO7eFaL2K5n6HUP+QeQDVqwOgBuVSviD8FbvbrofJdGQFj5aF2UpKc2FeMHiQCswkK2iQBlLgmL37Ciw+bUY1mJw6lHuPXDg3f/VOth///62bxr1fwJXvRtGA7jNF93JLPbfDGkB7POaHQze63MH/vCOxZ/Oh/2vPkLqcpt/QEnKhEFWDBC/k6qfNNyptQ/+tUCnlhaUQ== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5853.namprd04.prod.outlook.com (2603:10b6:208:a6::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4457.19; Mon, 30 Aug 2021 08:17:06 +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.4457.024; Mon, 30 Aug 2021 08:17:06 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v2 7/8] avfilter/sleet: Add sleet filter Thread-Index: AdeUeEEb3Th5Ze/STdKI6w1tUaGrBg== Date: Mon, 30 Aug 2021 08:17:06 +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: [bxOEGm50zY0X12N7KA3lTfmDgvD+x9B2] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 947d9c87-6892-40f6-d87e-08d96b8e8df7 x-ms-traffictypediagnostic: MN2PR04MB5853: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: keEg206+p7+zB1I/HlcpEgeUHk+WFuP5BOpd6Cw/ZbmU21hScWvxFjVYNLVRVZo1HS0vcYeF7cYtEAJ9nP0+P9DPiEw/WwmlEQyDU84sJno7L42pKXqh38P1FJCWw1WEQXsRY+XemOEaYva8iEIGpcLyZRRS1pK7CxZQxHRHsOGsCS4jr1iAf6tqe9psHMdJmVJWdYnboMoZ+7T1/xx1cNlZb+h+2+oFSYIFNcYfK6uuDPSAXpHxDhbqzDJvHDg4sfI3Jz5mcpHHoH/ejFym9NjvRckQ4AlIzd5q+EWiDR5/F5IgwaHH7k1bCXbuGohZQnH50bSpdBk9VhnmTyQpdHXbOl1fe5lzocqLtM1/s5W/sgeAIHVd8GvPSwSSzWzIfMBSN0e0rDQB6Rc/54Qe66gpoTuEypmeIkPby2WRFsb6Z3Fq9mB+OSQcpD2UXeeT x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: JXpCHef3g+Eb1gEKPFIUehLL07m+8RM3RTfO1OVvxXmhfECtK2eYPBQzKniuq9KCT1vAsVOordDo7SPs4c90yy4Hp7VQgxBOK5i2SAH3yxDBBsihdTXF/YWHnhNYGIbXPrJLgV80FzH32lGFW7RQpg== 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: 947d9c87-6892-40f6-d87e-08d96b8e8df7 X-MS-Exchange-CrossTenant-originalarrivaltime: 30 Aug 2021 08:17:06.0704 (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: MN2PR04MB5853 Subject: [FFmpeg-devel] [PATCH v2 7/8] avfilter/sleet: Add sleet 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: 4tms4CXTpQDz Signed-off-by: softworkz --- v2 Update: - Implemented Andreas' suggestions - overlay_subs filter: - removed duplicated code - implemented direct (no pre-conversion) blending of graphical subtitle rects - Supported input formats: - all packed RGB formats (with and without alpha) - yuv420p, yuv422p, yuv444p libavfilter/Makefile | 3 + libavfilter/allfilters.c | 1 + libavfilter/sf_sleet.c | 209 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 libavfilter/sf_sleet.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index e38c6b6f6d..25dd1276de 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -526,6 +526,9 @@ OBJS-$(CONFIG_YUVTESTSRC_FILTER) += vsrc_testsrc.o OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o +# subtitle filters +OBJS-$(CONFIG_SLEET_FILTER) += sf_sleet.o + # multimedia filters OBJS-$(CONFIG_ABITSCOPE_FILTER) += avf_abitscope.o OBJS-$(CONFIG_ADRAWGRAPH_FILTER) += f_drawgraph.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 5bd54db2c8..efe16b8e1b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -519,6 +519,7 @@ extern const AVFilter ff_avf_showwaves; extern const AVFilter ff_avf_showwavespic; extern const AVFilter ff_vaf_spectrumsynth; extern const AVFilter ff_svf_sub2video; +extern const AVFilter ff_sf_sleet; /* multimedia sources */ extern const AVFilter ff_avsrc_amovie; diff --git a/libavfilter/sf_sleet.c b/libavfilter/sf_sleet.c new file mode 100644 index 0000000000..cf7701c01f --- /dev/null +++ b/libavfilter/sf_sleet.c @@ -0,0 +1,209 @@ +/* + * 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 translates to 'leet speak' + */ + +#include "libavutil/avassert.h" +#include "libavutil/avstring.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "internal.h" +#include "libavcodec/avcodec.h" + +static const char* alphabet_src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; +static const char* alphabet_dst = "abcd3f6#1jklmn0pq257uvwxyzAB(D3F6#1JKLMN0PQ257UVWXYZ"; + + +typedef struct LeetContext { + const AVClass *class; + enum AVSubtitleType format; +} LeetContext; + +static const AVOption sleet_options[] = { + { NULL } +}; + +AVFILTER_DEFINE_CLASS(sleet); + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats = NULL; + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; + static const enum AVSubtitleType subtitle_fmts[] = { SUBTITLE_ASS, SUBTITLE_NONE }; + static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGB32, AV_PIX_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 */ + formats = ff_make_format_list(pix_fmts); + if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0) + return ret; + + return 0; +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + LeetContext *s = ctx->priv; + + s->format = inlink->format; + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + LeetContext *s = outlink->src->priv; + + outlink->format = s->format; + + return 0; +} + +static void avsubtitle_free_ref(void *opaque, uint8_t *data) +{ + avsubtitle_free((AVSubtitle *)data); +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *src_frame) +{ + LeetContext *s = inlink->dst->priv; + AVFilterLink *outlink = inlink->dst->outputs[0]; + AVSubtitle *sub; + int ret; + AVFrame *out; + unsigned int num_rects; + uint8_t *dst; + + 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->text) { + dst_rect->text = av_strdup(src_rect->text); + if (!dst_rect->text) + return AVERROR(ENOMEM); + + for (size_t n = 0; n < strlen(dst_rect->text); n++) { + for (size_t t = 0; t < FF_ARRAY_ELEMS(alphabet_src); t++) { + if (dst_rect->text[n] == alphabet_src[t]) { + dst_rect->text[n] = alphabet_dst[t]; + break; + } + } + } + } + + if (src_rect->ass) { + dst_rect->ass = av_strdup(src_rect->ass); + if (!dst_rect->ass) + return AVERROR(ENOMEM); + + for (size_t n = 0; n < strlen(dst_rect->ass); n++) { + for (size_t t = 0; t < FF_ARRAY_ELEMS(alphabet_src); t++) { + if (dst_rect->ass[n] == alphabet_src[t]) { + dst_rect->ass[n] = alphabet_dst[t]; + break; + } + } + } + } + } + } + + av_frame_free(&src_frame); + return ff_filter_frame(outlink, out); +} + +static const AVFilterPad sleet_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_SUBTITLE, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad sleet_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_SUBTITLE, + .config_props = config_output, + }, + { NULL } +}; + +const AVFilter ff_sf_sleet = { + .name = "sleet", + .description = NULL_IF_CONFIG_SMALL("Translate text subtitles to 'leet speak'"), + .query_formats = query_formats, + .priv_size = sizeof(LeetContext), + .priv_class = &sleet_class, + .inputs = sleet_inputs, + .outputs = sleet_outputs, +};