From patchwork Thu Aug 19 07:43:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 29610 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp64971iov; Thu, 19 Aug 2021 00:44:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqFK7EM3Pbg1taAbpTDtpuWs8YClh2NE6Jf3ksvga6C7exKAJJqrJd5vdi0AFgF4VIo1Vw X-Received: by 2002:a17:907:7291:: with SMTP id dt17mr14646689ejc.162.1629359073211; Thu, 19 Aug 2021 00:44:33 -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 p26si2767053edu.78.2021.08.19.00.44.32; Thu, 19 Aug 2021 00:44:33 -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=J8X002up; 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 1C36068A1EF; Thu, 19 Aug 2021 10:43:38 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11olkn2099.outbound.protection.outlook.com [40.92.19.99]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F282468A2B9 for ; Thu, 19 Aug 2021 10:43:32 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JX1sosEjg8679eztM97otoNSPM0Si0K3rMtcudh7w8vBrhcegIdWYyAyiQc3ROaxALl4F5wnNs1sa/f/znE2cp9CHIy393BGr1X1zFbKgm6ebW2RMK6AxaraMFoYkN/5RhH//qOyrIjNQH9TsxF2Do/r/KC+jyZjR/h1h5X82SgGqhYMO9jCLe5+s0RKKWsT9DukZTYKj8e7n+gTZcDDo3DeQpxIWKJDHliE6doD2kINr6/2wJNxdpQe+5xuzok7ocxPdM9JJZ76ybMP+HjoJ0W4XmmBw/P9gLn4C905uh+Ar0FbIDVpXW0NaqcwKHAbKJ0oTzOcugGiHESqpntXtQ== 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=m9cFQF3DyshM2NlNiXkO9uLCnE9BIHlC36nWiD4RcG8=; b=H13UalhukSoX5M3t7EZ4uhqGokwe/i8FfbQ/lJ8qw1UlOQxfDNsFPU4HLpuEE1ddaewhkhBOWWOegcpqYuymsUiFXJ+cBku2rYUcYU8m15Cf8ApxDnLbFFS7zfZ2LcOovhClOxfEehHMTLL5X2wxC91KA+N7tVHkpc3hzREnVf1XUj53ridk4XmKGZ7d4TeDnyM+npZ+/rz3l/+DST8SNw69vwuAIpgKQ3N/f6LSJEfGbwk6K3uAbS9IW5bGY7S+V7ZXoZKlXAyu+jtg0QXYpzI0pq5svBeUd2KTfWDZCeQyhtQZIfjI2sgwewjUO+pv4hAAIRzmbzbiW9kB8KDcjA== 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=m9cFQF3DyshM2NlNiXkO9uLCnE9BIHlC36nWiD4RcG8=; b=J8X002up8vkuEWM3ccd6wW6EanJ8UIqfP7XbSm2glurpjLiXrkjPs0N6G77Lzv8pbWr2ljSiuWyRD29dVYbRUqcoaFrAm6j02aTqVE3mVTw2nVhkdOtSiiCTX3WlWmxmOBLYDU2H1OsHCmDpc5jZscr58e8+IO6fbtMJZZfw7QvEBswBj+t9mw/tXNKak1eCRAljpwxwAyBUmA9OidqRDlVZkARJMNTP+bfH3r5FwzNTSP0/q4SIAWthmoK7IZxzAqYoQOGAO5VGtGmoc9DLv6VYLWSTsfyNl5p0KgU0s8r4VpZjowPk0ytspefoiH5aYWL7uHkrfcMI5SBMyZjLHA== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB6255.namprd04.prod.outlook.com (2603:10b6:208:d5::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.22; Thu, 19 Aug 2021 07:43:31 +0000 Received: from MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::5d83:1c26:c2b1:3a30]) by MN2PR04MB5981.namprd04.prod.outlook.com ([fe80::5d83:1c26:c2b1:3a30%6]) with mapi id 15.20.4415.024; Thu, 19 Aug 2021 07:43:31 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH 7/9] avfilter/sleet: Add sleet filter Thread-Index: AdeUeEEb3DbCQDzPTau6u4BdhirUZA== Date: Thu, 19 Aug 2021 07:43:31 +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: [24CuWBNJcLgi1wd6ZO77MP//ZmPzkNr+] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: cd237a83-0cf4-4ee6-d63d-08d962e50a9c x-ms-traffictypediagnostic: MN2PR04MB6255: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: gjaZT4st+EEgTn9KfgpHUA7MqZtZRM/LGfU4VGTv6z2nqO6RTSHnlr6YFmFJiC2YDgV8mZpbX3aurGkBxe+YKaUWowBAk4sDsc2TXX6itkEsp40XPkviXZn84mwL9fgOJUuQom3KjpTA/gzC4elb08mRZrBs4PaPByaCFCsyd4Wquto6PUagxDQTtwqPPOryqcCBsIhK/Q0Aqz/zkb0jln17G/Zw7O/kzehHdhrYf9G8ptDkmBPjOwhrycDlhinhq2LMZQ7f9+NdP3ytHXc9reJViZhLUPZF635qAlEW6qM5DFq4Opq9cr0Ly29lGSGZXOeavlcjkyN7p+mdba4QVkvg8qZwJZ9iRYwXWn8ocjDN7gpfimo3SxJFATBiw/DLdqznWJuqPXT7sroAW34AuoCf9PZds99c8KRK0q9uO1d09ybIdHOAxwa32GhOrPJY x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: LoJvnMXPFRubt8U92TJGSj7cRcjPPuKcKdKelsOzz99sAStTYXTZVYko6meGLtTC5HkNmYp6ZsVKCsmQSez3m7Kcz3t1M+gFr/FtekHini76F9Z9g1AojN7YLpjCNmMlFsGZxNTSJEwECowDUIK2qQ== 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: cd237a83-0cf4-4ee6-d63d-08d962e50a9c X-MS-Exchange-CrossTenant-originalarrivaltime: 19 Aug 2021 07:43:31.3499 (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: MN2PR04MB6255 Subject: [FFmpeg-devel] [PATCH 7/9] 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: c5au9MeEiMER Signed-off-by: softworkz --- 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 5f63ec0123..2a321a7f50 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, +};