From patchwork Sat Sep 18 03:53:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 30348 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp2792222iov; Fri, 17 Sep 2021 20:54:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzjSVpaByuGd0w0lskfQXFENrRwuGXcWsVytzQyKdgAyirkUxtcUOHtpYIOaPB0nnwbjf2F X-Received: by 2002:a17:906:e216:: with SMTP id gf22mr15817118ejb.357.1631937287019; Fri, 17 Sep 2021 20:54:47 -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 c13si8563744edv.152.2021.09.17.20.54.46; Fri, 17 Sep 2021 20:54:47 -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="aBLKh/rH"; 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 56CAD68B211; Sat, 18 Sep 2021 06:54:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11olkn2020.outbound.protection.outlook.com [40.92.20.20]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2A75B68988E for ; Sat, 18 Sep 2021 06:53:58 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=baN+Nxb9HO75j34SjiL/2QGQ4t1Ik0I7jPblNKTAr3cRqWWbMQ1IGUnDKPz8Od3lGCxXt1HphY41d61QXW9fZS074Nh38Yw2i4DNINfz/zIkjmI3FD9EoARdmDOyiFyy1tBkA3BA8LSLem8ZkdWZEBnptbdoUu6h9anJBw4xxAnwKMdyqMcpv5wzMSxUtt3en01RYzIwdVstz3m1eKMD1EBlqtT952POBN7pFkNiDV4OjzvsFd3o/9vCy3V8yPnQAn9sr3K9ibfCcM7IQGWbWzYc2qF88gPdTTCwevO0BNc4c/Mzehmihf9pFUKesPCCDG1AfHjEtGUHFkZegx1BRg== 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=Zoqe370gEZ+MUt/eU1UdaA25sNg93WUvDn4AEtuOdfY=; b=CeNueaKEB83YeZqYmv0D/G52LYL/xXxQXn7uB7l26JobyrZUd/wCwmyvkOPJL9ehkgxU1qJr440tlMuItTmYBhv6pbPZLCVIBWf+BaLE+rnCHgH7VGg7gT1HvmHOAlOhfs9rkcHlWqBAJmV87VZWL+ndyV5jj5+YXU2uWQLEvqGkDS3WFp10Pl2THBe7oA+5MEy7uKKT5XM0g6Px3ZgLSZWzMQy5RWudUorL63gDwP+ZrwQsXappCKlG0s6GmquIUJZlCRQK2mYbYoTuFzYQnNQJ6fV6HEVhQ3qIVNlDTKLFXDJjamL7874yFBrjOiSKpKceLR9dgCFA50jgOWYOZA== 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=Zoqe370gEZ+MUt/eU1UdaA25sNg93WUvDn4AEtuOdfY=; b=aBLKh/rHtK/upETT0ZgwLFGCSPkM30gP3soSxPpWNEEy9JoaztS6zko4+SVgDDe6cHO81A3ayyRlBNTy8otoh449MaWieCPuRzivSxGTBT7W7rkBogAR7qdu6NkUmTtWlwAb52DZUA54GiScn4S74b3yPwbs/nDxrm/Mn1++piiIcSLS0xIbdFC85vNdm20ta7evZZ0YH2svbBCeKkVTdZfnXWGta0X26WdKuwzpPeo1h62AEqvIdrFWz3WlkVZNje2pErc7LF5S2Hr9sglUIJ4OwfcKgO4yR8BNbVucqjA0scwRJMb1uht5fDX7gMia+6YpKowX0A27WsLysiPtDQ== Received: from BYAPR04MB5976.namprd04.prod.outlook.com (2603:10b6:a03:f2::32) by BYAPR04MB4744.namprd04.prod.outlook.com (2603:10b6:a03:16::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Sat, 18 Sep 2021 03:53:56 +0000 Received: from BYAPR04MB5976.namprd04.prod.outlook.com ([fe80::ecc6:937b:3740:1816]) by BYAPR04MB5976.namprd04.prod.outlook.com ([fe80::ecc6:937b:3740:1816%6]) with mapi id 15.20.4523.015; Sat, 18 Sep 2021 03:53:56 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v7 12/12] avfilter/split_cc: Add split_cc filter for closed caption handling Thread-Index: AQHXrEDOSprzysrUdEezDqhLd6tUkA== Date: Sat, 18 Sep 2021 03:53:56 +0000 Message-ID: References: <0929c32d8c695549bb9848e24113a07810118645.1631934103.git.softworkz@hotmail.com> <5a0310cb87ef181680b26efc53f0eea6b9e939fe.1631934104.git.softworkz@hotmail.com> <42f16c26e63b3ffec87acef3b2e95a4085b4b86b.1631934104.git.softworkz@hotmail.com> <5481c55bd4e5d17194e6b87b13760fb6c1186377.1631934104.git.softworkz@hotmail.com> <764bd747b932dd5bc54c2c8cbd9c8894def71c7f.1631934104.git.softworkz@hotmail.com> <9c92f98fcfa2683a7602ec85e17dc51c5a79c7b9.1631934104.git.softworkz@hotmail.com> <98d97c1ba7d0160e53327ca17ad30962e61d34b6.1631934104.git.softworkz@hotmail.com> <011ba90340ac9e1cf736ca584170f0ecfcc35c4b.1631934104.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: [MrfVDjEZiV5gMgQN7lRz5t2Wl5ADSB8o] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 348c68c8-4242-4981-43a5-08d97a57f0a1 x-ms-traffictypediagnostic: BYAPR04MB4744: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 6rfYVi4dnc5x3Zwu+YEGjdRV2Q8MHMCqdmG/2YH+O/5MzhHAn3MWamc8FN8YBu1ZTy9kAKEN4A4U/Q+MGLqXQggUJ+3pD+jzQK2cSyjJ8E8Mh8rVBPlmOqJpCTGAVxiDhdw1kik85oUBLnoySv7U2e7LhtrQ+FV2j938YoG6nzhimGgGFfZBz8AvdvZyyPL1/NQZkN8pS4s80hwA1/Hi48bI+XCBUBIajrgPSRy5YC3qwJBGjY8M5+BhYpdTZ4E2sU/PZHFvQ9eupAAJq99GFZ7L6LztO8RaXFIGgCj1qquW5jzhTHoMeiElA7hPOo8a4qhnFaGg7Vhbh4uEy6R3drwb/j9gL9uDmRfkOegNTxK3s6sIGjrUw6YubVSCc9nZj57IwzgGRmSx51vP0+RqnFYNNLHkS7k64gBkyBV8/l8p+EwBpVQNS8PXwtF7fLZ1ktnfIrXtSHVYug9+jnXRDvVNv5FIIqeub1LXRIEE/hc= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: LcAsLUGxVgVcvPgw+DrvyIQqi6Si1KCUkvW953nGdjGNi5lxXyA1qLYjMj+onK0rChvZQFBEyZFRIV546N6Axy3gpvM+ww7lgbRTChdDuiDSwC9Bl+7PfIxFZMgPYhjbsYQnDVH9xPVTvR+k/sEDCQ== 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: BYAPR04MB5976.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 348c68c8-4242-4981-43a5-08d97a57f0a1 X-MS-Exchange-CrossTenant-originalarrivaltime: 18 Sep 2021 03:53:56.6440 (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: BYAPR04MB4744 Subject: [FFmpeg-devel] [PATCH v7 12/12] avfilter/split_cc: Add split_cc filter for closed caption handling 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: ISFt9BESLO5Y - split_cc {V -> VS) Extract closed-caption (A53) data from video frames as subtitle Frames ffmpeg -y -loglevel verbose -i "https://streams.videolan.org/streams/ts/CC/NewsStream-608-ac3.ts" -filter_complex "[0:v]split_cc[vid1],textmod=mode=remove_chars:find='@',[vid1]overlay_textsubs" output.mkv Signed-off-by: softworkz --- doc/filters.texi | 24 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_split_cc.c | 271 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 297 insertions(+) create mode 100644 libavfilter/sf_split_cc.c diff --git a/doc/filters.texi b/doc/filters.texi index c8bf444d2c..958438a372 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -25300,6 +25300,30 @@ string containing ASS style format @code{KEY=VALUE} couples separated by ",". @end table +@section split_cc + +Split-out closed-caption/A53 subtitles from video frame side data. + +This filter provides an input and an output for video frames, which are just passed through without modification. +The second out provides subtitle frames which are extracted from video frame side data. + +Inputs: +- 0: Video + +Outputs: +- 0: Video (same as input) +- 1: Subtitles [text] + +It accepts the following parameters: + +@table @option + +@item use_cc_styles +Emit closed caption style header. This will make closed captions look like on normal TV devices. +(white font on black background rectangles) + +@end table + @section textsub2video Converts text subtitles to video frames. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 94ec11c4ce..7fcd5a592b 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -538,6 +538,7 @@ OBJS-$(CONFIG_NULLSINK_FILTER) += vsink_nullsink.o # subtitle filters OBJS-$(CONFIG_TEXTMOD_FILTER) += sf_textmod.o +OBJS-$(CONFIG_SPLIT_CC_FILTER) += sf_split_cc.o OBJS-$(CONFIG_STRIPSTYLES_FILTER) += sf_stripstyles.o # multimedia filters diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 88dd97adf1..168796c74e 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -526,6 +526,7 @@ extern const AVFilter ff_avf_showvolume; extern const AVFilter ff_avf_showwaves; extern const AVFilter ff_avf_showwavespic; extern const AVFilter ff_vaf_spectrumsynth; +extern const AVFilter ff_sf_split_cc; extern const AVFilter ff_sf_stripstyles; extern const AVFilter ff_sf_textmod; extern const AVFilter ff_svf_graphicsub2video; diff --git a/libavfilter/sf_split_cc.c b/libavfilter/sf_split_cc.c new file mode 100644 index 0000000000..26af09c670 --- /dev/null +++ b/libavfilter/sf_split_cc.c @@ -0,0 +1,271 @@ +/* + * 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 + * subtitle filter for splitting out closed-caption/A53 subtitles from video frame side data + */ + +#include + +#include "libavutil/avassert.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "internal.h" +#include "subtitles.h" +#include "libavcodec/avcodec.h" + +typedef struct SplitCaptionsContext { + const AVClass *class; + enum AVSubtitleType format; + AVCodecContext *cc_dec; + int eof; + AVFrame *next_sub_frame; + int new_frame; + char *subtile_header; + int use_cc_styles; +} SplitCaptionsContext; + +static int init(AVFilterContext *ctx) +{ + SplitCaptionsContext *s = ctx->priv; + + if (!s->cc_dec) { + AVDictionary *options = NULL; + int ret; + const AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_EIA_608); + if (!codec) { + av_log(ctx, AV_LOG_ERROR, "failed to find EIA-608/708 decoder\n"); + return AVERROR_DECODER_NOT_FOUND; + } + + if (!((s->cc_dec = avcodec_alloc_context3(codec)))) { + av_log(ctx, AV_LOG_ERROR, "failed to allocate EIA-608/708 decoder\n"); + return AVERROR(ENOMEM); + } + + av_dict_set(&options, "sub_text_format", "ass", 0); + av_dict_set(&options, "real_time", "0", 0); + + if ((ret = avcodec_open2(s->cc_dec, codec, &options)) < 0) { + av_log(ctx, AV_LOG_ERROR, "failed to open EIA-608/708 decoder: %i\n", ret); + return ret; + } + + if (s->cc_dec->subtitle_header && s->use_cc_styles) + s->subtile_header = av_strdup((char *)s->cc_dec->subtitle_header); + + av_dict_free(&options); + } + + return 0; +} + +static void uninit(AVFilterContext *ctx) +{ + SplitCaptionsContext *s = ctx->priv; + if (s->next_sub_frame) + av_frame_unref(s->next_sub_frame); +} + +static int config_input(AVFilterLink *link) +{ + const SplitCaptionsContext *context = link->dst->priv; + + if (context->cc_dec) + context->cc_dec->pkt_timebase = link->time_base; + + return 0; +} + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats; + AVFilterLink *inlink0 = ctx->inputs[0]; + AVFilterLink *outlink0 = ctx->outputs[0]; + AVFilterLink *outlink1 = ctx->outputs[1]; + static const enum AVSubtitleType subtitle_fmts[] = { AV_SUBTITLE_FMT_ASS, AV_SUBTITLE_FMT_NB }; + int ret; + + /* set input0 video formats */ + formats = ff_all_formats(AVMEDIA_TYPE_VIDEO); + if ((ret = ff_formats_ref(formats, &inlink0->outcfg.formats)) < 0) + return ret; + + /* set output0 video formats */ + if ((ret = ff_formats_ref(formats, &outlink0->incfg.formats)) < 0) + return ret; + + /* set output1 subtitle formats */ + formats = ff_make_format_list(subtitle_fmts); + if ((ret = ff_formats_ref(formats, &outlink1->incfg.formats)) < 0) + return ret; + + return 0; +} + +static int config_sub_output(AVFilterLink *outlink) +{ + const AVFilterLink *inlink = outlink->src->inputs[0]; + + outlink->frame_rate = inlink->frame_rate; + outlink->time_base = inlink->time_base; + outlink->format = AV_SUBTITLE_FMT_ASS; + + return 0; +} + +static int request_sub_frame(AVFilterLink *outlink) +{ + SplitCaptionsContext *s = outlink->src->priv; + + if (s->eof) + return AVERROR_EOF; + + if (s->next_sub_frame && s->new_frame) { + s->next_sub_frame->pts++; + + AVFrame *out = av_frame_clone(s->next_sub_frame); + if (!out) + return AVERROR(ENOMEM); + + out->subtitle_pts = av_rescale_q(s->next_sub_frame->pts, outlink->time_base, AV_TIME_BASE_Q); + s->new_frame = 0; + + return ff_filter_frame(outlink, out); + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFrameSideData *sd; + SplitCaptionsContext *s = inlink->dst->priv; + AVFilterLink *outlink0 = inlink->dst->outputs[0]; + AVFilterLink *outlink1 = inlink->dst->outputs[1]; + AVPacket pkt = {0}; + AVFrame *sub_out; + + int ret; + + outlink0->format = inlink->format; + + if (!frame) + return AVERROR(ENOMEM); + + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC); + + if (sd) { + int got_output = 0; + + av_new_packet(&pkt, (int)sd->size); + if (!((pkt.buf = av_buffer_ref(sd->buf)))) + return AVERROR(ENOMEM); + + pkt.data = sd->data; + pkt.size = (int)sd->size; + pkt.pts = frame->pts; + + sub_out = ff_get_subtitles_buffer(outlink1, AV_SUBTITLE_FMT_ASS); + if (!sub_out) + return AVERROR(ENOMEM); + + sub_out->subtitle_header = (char*)s->cc_dec->subtitle_header; + + ret = avcodec_decode_subtitle3(s->cc_dec, sub_out, &got_output, &pkt); + + if (ret < 0) + goto fail; + + if (got_output) { + sub_out->pts = frame->pts; + av_frame_unref(s->next_sub_frame); + s->next_sub_frame = sub_out; + s->new_frame = 1; + + ret = request_sub_frame(outlink1); + if (ret < 0) + return ret; + } + else + av_frame_free(&sub_out); + } + + if (!s->next_sub_frame) { + s->next_sub_frame = ff_get_subtitles_buffer(outlink1, outlink1->format); + s->next_sub_frame->subtitle_end_time = 1000; + s->next_sub_frame->pts = frame->pts; + s->new_frame = 1; + s->next_sub_frame->subtitle_header = (char*)s->cc_dec->subtitle_header; + } + else + s->next_sub_frame->pts = frame->pts; + + ret = ff_filter_frame(outlink0, frame); + +fail: + av_packet_unref(&pkt); + return ret; +} + +#define OFFSET(x) offsetof(SplitCaptionsContext, x) +#define FLAGS (AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_FILTERING_PARAM) + +static const AVOption split_cc_options[] = { + { "use_cc_styles", "Emit closed caption style header", OFFSET(use_cc_styles), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS, NULL }, + { NULL }, +}; + +AVFILTER_DEFINE_CLASS(split_cc); + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, +}; + +static const AVFilterPad outputs[] = { + { + .name = "video_passthrough", + .type = AVMEDIA_TYPE_VIDEO, + }, + { + .name = "subtitles", + .type = AVMEDIA_TYPE_SUBTITLE, + .request_frame = request_sub_frame, + .config_props = config_sub_output, + }, +}; + +const AVFilter ff_sf_split_cc = { + .name = "split_cc", + .description = NULL_IF_CONFIG_SMALL("Extract closed-caption (A53) data from video as subtitle stream."), + .init = init, + .uninit = uninit, + .query_formats = query_formats, + .priv_size = sizeof(SplitCaptionsContext), + .priv_class = &split_cc_class, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), +};