From patchwork Thu Oct 21 03:19:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Soft Works X-Patchwork-Id: 31193 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2084:0:0:0:0 with SMTP id a4csp128251ioa; Wed, 20 Oct 2021 20:21:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJytyqGw7zUmzSNMvkf6WYsdOL0OEvfBI8ZnJdRv1QDkaJzm9BpNyue1sjZYMlAtfWiQ6iL5 X-Received: by 2002:a17:906:789:: with SMTP id l9mr4001531ejc.61.1634786509266; Wed, 20 Oct 2021 20:21:49 -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 kq16si5296633ejb.521.2021.10.20.20.21.48; Wed, 20 Oct 2021 20:21:49 -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=EM5u6Ba2; 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 F40E968A956; Thu, 21 Oct 2021 06:19:59 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10olkn2026.outbound.protection.outlook.com [40.92.42.26]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D2A2F68A12D for ; Thu, 21 Oct 2021 06:19:57 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mUZeDQmIl83zidi34sKE9lqBjWgdHvLyp/jUvAkM847PSSUgPvf2A8D9fTSX/y8PsQoU2da+aGyVskBqOgORim0OPkhN5B1H5sVFASCt/Y3cl1nsIx+ESLhOxdcGfMYRLLpsvAbdsuqCuK2YhgIcFShdkcAPhQ1dRRrjL4JGhWNR7VWt3EBqRpUqpC60y0ZLL0z6Mpp+/FWl8AQ4JQ5CXM8xkh5BpKnXZWF6116h09DQBKGKrjsif49vhsIW8Z1MfxSPQiPIUBrfCVn5Xz7O+VFcCQpRqVIVeGRVmHMd/WjwiWlXT2/YkBU1azGyeu8dYJkYEMYrQYPJgG4gfaWmEg== 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=Xnlei/LuyFMjwuPottG1nJ/9zeEBc0yXSyqO9KuHVOM=; b=g3rW8B3teZOSTp5oeoez2KhfQRC/1XJcUJrAXYVlINISaYxFMnMJhNRrmXP2xhTs0z85B+j9oYZbWT6ImoDQsgdWnPNgjTHWR6JcnU26r4JR8jC20foP8bkxElh4j8S7mjlM/r1BdX4p/SqQK+HFp0pfJa13FwwJi6beR5dMCfocYAC0wIgCF32b5kMf/f/lV8IWYP4kQ+rtdV0VZpEC2PSktkql4KzHfydNbtmForQBLogO28g2HGu4EiNr/z2XdlHD0ty2iBOuY7qwJrlDb32/R20H95W30i7If/mUJvcRuwBWdyR9QGaiNeNDUv3ypniixKtqZxeskuUUW7twUA== 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=Xnlei/LuyFMjwuPottG1nJ/9zeEBc0yXSyqO9KuHVOM=; b=EM5u6Ba2BTJ25Ob7/t0NPn+bo8ZkhonfEqsKsyZ01j5y9DIDI+fLnIizso2xfU1cOexTEnw/+53VAjIo1roaExeLp+gtg1nLMfAN7OITeMcDG7Ca3mmiuV5ZCd3M/bPFeZ/jW0lwiBJNfd0t0BoLyG7D5KnMy9AUGHQNSDu35uau6OSYKqgx2ldJNgrdGJ82L1PTg84Pw3pTckky44OdEAG2HJcCMPOJutSd+B3yRzA31FrISk3Xs9uezYkMgZYXKmnlNtLHfi/Vre1iEGaDkcX1gR1bGAiGm1QUpqW3A1Pbm5HtpzIhw/UGGH6tBIv0zUlQuowjlNng6YDzob9nSg== Received: from MN2PR04MB5981.namprd04.prod.outlook.com (2603:10b6:208:da::10) by MN2PR04MB5984.namprd04.prod.outlook.com (2603:10b6:208:d9::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Thu, 21 Oct 2021 03:19:53 +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.4608.018; Thu, 21 Oct 2021 03:19:53 +0000 From: Soft Works To: "ffmpeg-devel@ffmpeg.org" Thread-Topic: [PATCH v14 12/14] avfilter/splitcc: Add splitcc filter for closed caption handling Thread-Index: AQHXxiqD9/nuExSxk0mSiO9vA+arvQ== Date: Thu, 21 Oct 2021 03:19:52 +0000 Message-ID: References: <4f225627a420d32b8766306e418c45bc8af41c47.1634785894.git.softworkz@hotmail.com> <4d6c9a34829586c3ad247e2423faa0d763fa01c4.1634785894.git.softworkz@hotmail.com> <541cf61d503ee53b51ebccada8f9cb087ae3c549.1634785895.git.softworkz@hotmail.com> <55d8df19375a269c01802b3ab9b0e4c30d9654eb.1634785895.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: [szrOSVrFj+cYDn/WyuLcE5fnomhFVPTI] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 3be6d0ae-bd77-46a5-c60a-08d99441a626 x-ms-traffictypediagnostic: MN2PR04MB5984: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: mtxov5k1ts+yz7MzpRX+o8kTuBHAlECBJg5A+8mRZwqG20rKjw+hNHyLJwZxPW1daT7fiREYqwRv/So6rMFqsJVkoGyWoXAw+ktFQ4OArvsVVFcg4IXSjIOH7xgzgsqpDWNm6TiVzZfTLZjgdq3nU6Q3vrfaemVljgckQfiHjKCeLR3Fp5+a1cjGmdCC2Puyt98loYD/5AdfCgUyvsFw2bmY7Es6ogt+NLTY3/iCvBrHJLJ3vpihnABmNul/FkS9zxKRF591h+tlX7xXOKz8+/dSgBFSnKcx3wKbz8nSe+C737fYzIhRk7rg+PXQFQeEVhodouqwug2wN6/0MpotS+0gY1ZVp8zMsWNziC5l1wkUr3p4UEMEyKYpmvk/9GxTpRm7leKdz6Kpgi2LrYCs8Ph85svf7feUkt0RR3jiE9EoWz/3i+pYSBuBOIRH1XrGlqUwitmhfNKHL4xCgLcrewGTqnbd39z9CE/xqshH2bStaxAwMdc4ekz8PWqycKKAZ+MI1KYNdfCc5gIzGKtbwO7cwxJqSlH5iIxeqSOMbFRR2rRjSLnYO88LQSmrBN7uJ4NitqNZYkoUdLquYAVWTQ== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: CzaAIdlUHhlVwrGd0hrMtwcrakNC8JuUZmCujyomgVR8fdc4y++HMXiTCUewfP60EqquSOD08YyAoE7Yz2VHM4yWYaY+rs4nxw1R/mqjlbmskSx0MIKalALQ7MsiBdDjRgvOthwqK5PnZmQx76hXwoIcx9CY9aYSbtnf2ZIvouxSQ73Ty5YhMZ82mdFnzpcEg3bRIJe2fvkyDE3KavirAu39iQUN5rJPu1anRHpxZevDIgQtvLnmOC46ZXS6s5o8/SxvKKRJnkKcOP26xKa6EzHhbVJc1wfTJbuA7VK9eD3sYxuzuQ21qGpkpBvmx+hmKHHaeeh2VU/PHBktSKbKipVAntMTSwftKULHmotzlzaBBWvfviAgmH0keTxhiQQ+RcuEu0fBrkYPNaMEtD4tSfV67fkLbp9VdC1AYaMSGOrlmYQvM5laswaRcP7SFzRRs2vEyg0DHqjO7oqCkbdgmoNHXXqaept5EXW1/24G97mGH45d1l0lwbUKgUz3rv2KSEYxLJhgl9RvFfkAq8MUclI5FKvpxDfDUxfI1FB2lMUKyKHxmaWg2biw+G43opD3Y4ZaeNQgleZ8/FRN9DW9BUOOCRgen7bPmxRTeiuSOBAjz+1aXsGY1DNMgLRHLlt708otaI91Kr2tMCnSQ1UzFT4xM/N8eqnGrCOJwZMno+PY6k4RGFWm3X1FDpJlg/b8Vim09o5ourdUa7UMhznK6Q== 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: 3be6d0ae-bd77-46a5-c60a-08d99441a626 X-MS-Exchange-CrossTenant-originalarrivaltime: 21 Oct 2021 03:19:52.9840 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-userprincipalname: softworkz@hotmail.com X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5984 Subject: [FFmpeg-devel] [PATCH v14 12/14] avfilter/splitcc: Add splitcc 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: 1o0sj96ec1UT - splitcc {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]splitcc[vid1],textmod=mode=remove_chars:find='@',[vid1]overlay_textsubs" output.mkv Signed-off-by: softworkz --- configure | 1 + doc/filters.texi | 57 +++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/sf_splitcc.c | 315 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 375 insertions(+) create mode 100644 libavfilter/sf_splitcc.c diff --git a/configure b/configure index d32452934b..31092ab38b 100755 --- a/configure +++ b/configure @@ -3667,6 +3667,7 @@ spp_filter_select="fft idctdsp fdctdsp me_cmp pixblockdsp" sr_filter_deps="avformat swscale" sr_filter_select="dnn" stereo3d_filter_deps="gpl" +splitcc_filter_deps="cc_dec_decoder" subtitles_filter_deps="avformat avcodec libass" super2xsai_filter_deps="gpl" pixfmts_super2xsai_test_deps="super2xsai_filter" diff --git a/doc/filters.texi b/doc/filters.texi index c8ea318520..7f7ccc3f2a 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -25736,6 +25736,63 @@ ffmpeg -i INPUT -filter_complex "showspeaker=format=colon:style='@{\\c&HDD0000&\ @end example @end itemize + +@section splitcc + +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: +@itemize +@item 0: Video [ALL] +@end itemize + +Outputs: +@itemize +@item 0: Video (same as input) +@item 1: Subtitles [TEXT] +@end itemize + +It accepts the following parameters: + +@table @option + +@item use_cc_styles +Emit closed caption style header. +This will make closed captions appear in white font with a black rectangle background. + +@item real_time +Emit subtitle events as they are decoded for real-time display. + +@item real_time_latency_msec +Minimum elapsed time between emitting real-time subtitle events. +Only applies to real_time mode. + +@item data_field +Select data field. Possible values: + +@table @samp +@item auto +Pick first one that appears. +@item first +@item second +@end table + +@end table + +@subsection Examples + +@itemize +@item +Extract closed captions as text subtitle stream and overlay it onto the video: +@example +ffmpeg -i "https://streams.videolan.org/streams/ts/CC/NewsStream-608-ac3.ts" -filter_complex "[0:v:0]splitcc[vid1][sub1];[vid1][sub1]overlay_textsubs" output.mkv +@end example +@end itemize + + @section textsub2video Converts text subtitles to video frames. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 41e71e2db2..302bdaf416 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -548,6 +548,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_SPLITCC_FILTER) += sf_splitcc.o OBJS-$(CONFIG_STRIPSTYLES_FILTER) += sf_stripstyles.o # multimedia filters diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 49b10b882d..f0eccbacd4 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -536,6 +536,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_splitcc; extern const AVFilter ff_sf_stripstyles; extern const AVFilter ff_sf_textmod; extern const AVFilter ff_svf_graphicsub2video; diff --git a/libavfilter/sf_splitcc.c b/libavfilter/sf_splitcc.c new file mode 100644 index 0000000000..b48dfd5fa6 --- /dev/null +++ b/libavfilter/sf_splitcc.c @@ -0,0 +1,315 @@ +/* + * 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 "libavutil/opt.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; + AVBufferRef *subtitle_header; + int use_cc_styles; + int real_time; + int real_time_latency_msec; + int data_field; +} SplitCaptionsContext; + +static int init(AVFilterContext *ctx) +{ + SplitCaptionsContext *s = ctx->priv; + 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_int(&options, "real_time", s->real_time, 0); + av_dict_set_int(&options, "real_time_latency_msec", s->real_time_latency_msec, 0); + av_dict_set_int(&options, "data_field", s->data_field, 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->use_cc_styles && s->cc_dec->subtitle_header && s->cc_dec->subtitle_header[0] != 0) { + char* subtitle_header = av_strdup((char *)s->cc_dec->subtitle_header); + if (!subtitle_header) + return AVERROR(ENOMEM); + s->subtitle_header = av_buffer_create((uint8_t *)subtitle_header, strlen(subtitle_header) + 1, NULL, NULL, 0); + if (!s->subtitle_header) { + av_free(subtitle_header); + return AVERROR(ENOMEM); + } + } + + return 0; +} + +static void uninit(AVFilterContext *ctx) +{ + SplitCaptionsContext *s = ctx->priv; + av_frame_free(&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->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 decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt) +{ + int ret; + + *got_frame = 0; + + if (pkt) { + ret = avcodec_send_packet(avctx, pkt); + // In particular, we don't expect AVERROR(EAGAIN), because we read all + // decoded frames with avcodec_receive_frame() until done. + if (ret < 0 && ret != AVERROR_EOF) + return ret; + } + + ret = avcodec_receive_frame(avctx, frame); + if (ret < 0 && ret != AVERROR(EAGAIN)) + return ret; + if (ret >= 0) + *got_frame = 1; + + 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; + AVFrame *sub_out; + + int ret; + + outlink0->format = inlink->format; + + sd = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC); + + if (sd) { + int got_output = 0; + + pkt = av_packet_alloc(); + pkt->buf = av_buffer_ref(sd->buf); + if (!pkt->buf) { + ret = AVERROR(ENOMEM); + goto fail; + } + + 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) { + ret = AVERROR(ENOMEM); + goto fail; + } + + if ((ret = av_buffer_replace(&sub_out->subtitle_header, s->subtitle_header)) < 0) + goto fail; + + ret = decode(s->cc_dec, sub_out, &got_output, pkt); + + if (ret < 0) + goto fail; + + if (got_output) { + sub_out->pts = frame->pts; + av_frame_free(&s->next_sub_frame); + s->next_sub_frame = sub_out; + sub_out = NULL; + s->new_frame = 1; + + if ((ret = av_buffer_replace(&s->next_sub_frame->subtitle_header, s->subtitle_header)) < 0) + goto fail; + + ret = request_sub_frame(outlink1); + if (ret < 0) + goto fail; + } + } + + if (!s->next_sub_frame) { + s->next_sub_frame = ff_get_subtitles_buffer(outlink1, outlink1->format); + if (!s->next_sub_frame) { + ret = AVERROR(ENOMEM); + goto fail; + } + + s->next_sub_frame->subtitle_end_time = 100; + s->next_sub_frame->pts = frame->pts; + s->new_frame = 1; + + if ((ret = av_buffer_replace(&s->next_sub_frame->subtitle_header, s->subtitle_header)) < 0) + goto fail; + } + else + s->next_sub_frame->pts = frame->pts; + + ret = ff_filter_frame(outlink0, frame); + +fail: + av_packet_free(&pkt); + av_frame_free(&sub_out); + 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 }, + { "real_time", "emit subtitle events as they are decoded for real-time display", OFFSET(real_time), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS }, + { "real_time_latency_msec", "minimum elapsed time between emitting real-time subtitle events", OFFSET(real_time_latency_msec), AV_OPT_TYPE_INT, { .i64 = 200 }, 0, 500, FLAGS }, + { "data_field", "select data field", OFFSET(data_field), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, FLAGS, "data_field" }, + { "auto", "pick first one that appears", 0, AV_OPT_TYPE_CONST, { .i64 =-1 }, 0, 0, FLAGS, "data_field" }, + { "first", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "data_field" }, + { "second", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "data_field" }, + { 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_splitcc = { + .name = "splitcc", + .description = NULL_IF_CONFIG_SMALL("Extract closed-caption (A53) data from video as subtitle stream."), + .init = init, + .uninit = uninit, + .priv_size = sizeof(SplitCaptionsContext), + .priv_class = &split_cc_class, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), + FILTER_QUERY_FUNC(query_formats), +};