From patchwork Tue Sep 21 23:54:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 30444 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6506:0:0:0:0:0 with SMTP id z6csp2919931iob; Tue, 21 Sep 2021 16:56:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJybT4b/4/vxeOLNelWvDIeo6npRvnVvUC8DQXBpn1GKVxZFTS7W8HUI/VF/hL46RV39CIAk X-Received: by 2002:a17:907:1b06:: with SMTP id mp6mr36619290ejc.188.1632268613297; Tue, 21 Sep 2021 16:56:53 -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 2si471353ejh.749.2021.09.21.16.56.52; Tue, 21 Sep 2021 16:56:53 -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=VCATirZF; 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 549CE68B1AB; Wed, 22 Sep 2021 02:55:07 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12olkn2028.outbound.protection.outlook.com [40.92.23.28]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A2E4D68B0DF for ; Wed, 22 Sep 2021 02:55:00 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=S2J3D4J7RtGNlfeipesF4jVQvsjbKPLa8SrsvtGuLJDZC9XeghO+kB0Jx5nhC8nY91C/txwxp4slMzibcj4/OZpPF9rwRG2u29nGPjmQKhw7+WoqZjDgYwTqQjL2sCCe249xeDzbfRBlP05rLK+Qa+SfyHy3VcnQ+m7rbco7WcYOZxA/aQf1KI4phof8joFLTshQGti+SXKcaXqVNd5Na585SKyVX2/3fnoHOzDY0e5Dpg0XcBfOdu/5NHTmQ87Nus/6QA9QCW3j17K3LleTeiSxcRqHnB5poiqJbIIQazdK4aXeJ7btCJcXnsex5151sFqEYWRqi7IdUfPwbo4row== 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=NxwhzkTCSU6/bCxJGODIRn07LtrDXLLLeqGSAl6m3kk=; b=jXmRxts4Un0S9ELuk4zEs87HBc6+SrrWRHSzSiygRb+3jS59gLYY2pDtCRmH7a6v2/f+Wt3/YV7YRlmeUk/indbn1CtUdJcfKa8qamr4b0Lh4S6BlQ1CwsrLW+Pv1iSfrF2nJlYnaASXCyRNrSM1zlwKWBPyoaOl3q9S74KVatOSTUua5jNzvgKrmxv+M9tYt4Cm0xbaJbGHnprAgfMPd65lrU+Nlw0dVLCxWVc7iLHEU62v2CGPReC5iiZ7Or0B8P2kRY8VIXF4Hjn5OApEE38SuZtAQbvNmAJ47NeZQhMFniq/rFSErv/Wz8nXUGtU9kP2B8xLDFmppmxCv8KV2w== 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=NxwhzkTCSU6/bCxJGODIRn07LtrDXLLLeqGSAl6m3kk=; b=VCATirZFZFdP5m0VnxYJGFEPqAZNEOh6PupAHoE2mVBchJKoKTTaw09i5VcYXl9xx81bINDundcX/vfoWlcVivTwsCvQoReGzGV0JN4u/zgFU2vwSzasLpa3h3i3tSGa90I1UEIEwrLv3fNrGfAh9TZb3ZdXIagF57Uw/1tfWCSTwiaR8QPQtfIRbTprx2EC9znuXXUDUCBEytZuixItVnIhSoc6UkXHWppTweAzX389w5HL5e8iztXcZB84K8vxRHSJVAhO9VNgLwHo0Y0EmEufGWnz1HHy4NLHH0vqzN+qeZjJOFlhN3UNByPb4d7CneffOYhgtRqnnmzjZmWy0Q== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5520.namprd04.prod.outlook.com (2603:10b6:208:d7::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Tue, 21 Sep 2021 23:54:58 +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.4523.019; Tue, 21 Sep 2021 23:54:58 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v8 12/13] avfilter/split_cc: Add split_cc filter for closed caption handling Thread-Index: AQHXr0QVEv7lu8QfpkazoPkaJyioCw== Date: Tue, 21 Sep 2021 23:54:58 +0000 Message-ID: References: <2f4d7190c46bde35cfef16e74900bd11a42856a8.1632266268.git.softworkz@hotmail.com> <4855e43e9cec16b6fa4db8991ddba687b5b90517.1632266268.git.softworkz@hotmail.com> <9b2d5f77fd58279aa3bcfe53e9a211f7d088213a.1632266268.git.softworkz@hotmail.com> <0b57654b386ea5a47dff24924c5ba2a8a3ae3f12.1632266268.git.softworkz@hotmail.com> <49722fb7bf5aac7e56a2967d335a9550ba029c89.1632266268.git.softworkz@hotmail.com> In-Reply-To: <49722fb7bf5aac7e56a2967d335a9550ba029c89.1632266268.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: [I74rOhxrPzT+6ocG55A+cVxSV6hrPzxo] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 697f3ef7-560d-4f09-e9e8-08d97d5b37cc x-ms-traffictypediagnostic: MN2PR04MB5520: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: ANqd8+LJyHFOAX3gwsYJswziyzbno2PTR4Gn6rOi4iGIp4xTOXSWDp4RgKTfwyrjXOYNSbfgQKCWZVisQSCZEsm9apaJ+bZpJInt5hdtbAIF4iD4QSD7nfczrgfsoqOEHFVLobq3BK3a3LYoqjoj4H8BtPpMtkUINUYM/p6fONHe+qngxZbiWBzCA7IV+kJfhcSZVjf2B3EEZ0vh3E6K2+3KQkTUxpW48J8/c48P2IbUcctQsZho6PciNd9BVEEFGp/X0EPe8FbPEGud8YIZFc8IOTtkJ+JSRMMc1c0dLALIZhkmfxM2BgwRKk0IOoHJRqYmwAmClRcnBxSzESpl/AmZGBRsV48CN/+bVAMpUIsIdmi94xZpY1FvaUFbEJJAqyV3coxTEdPJvXQJENClCg0W5BcyoGx1QfHdDnDWkibISKj5smQAeR0JGI5Qgun0IXx/i5ozd9CvxYyjuwj/dRZmPBL5/9UKb7/NtVZT+6Q= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: 4xqKqlURmTxdnmaDSyyDQLZA55dDDG7l0ygTe/j8R63TuyYDGGlAdgbJAr089jB7Rxj0z87t8kN3fZwMzRQ/iNf9nY64q6g23Xv//gRBXknt0n36dlMi66X33tzHHXgyjbajIoxfJVy10qXb+3zjzQ== 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: 697f3ef7-560d-4f09-e9e8-08d97d5b37cc X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Sep 2021 23:54:58.0308 (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: MN2PR04MB5520 Subject: [FFmpeg-devel] [PATCH v8 12/13] 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: vx4dtTCb+Uq6 - 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 | 25 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_split_cc.c | 272 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 299 insertions(+) create mode 100644 libavfilter/sf_split_cc.c diff --git a/doc/filters.texi b/doc/filters.texi index 2d3dcdd7e6..da463e2cc1 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -25441,6 +25441,31 @@ ffmpeg -i INPUT -filter_complex "show_speaker=format=colon:style='@{\\c&HDD0000& @end example @end itemize + +@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 309c404bf7..39abf6d2a6 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_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 e2e4deea22..77c6379302 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_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..e0ba5d4bcd --- /dev/null +++ b/libavfilter/sf_split_cc.c @@ -0,0 +1,272 @@ +/* + * 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) { + AVFrame *out; + s->next_sub_frame->pts++; + + 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), +};