diff mbox series

[FFmpeg-devel,v9,12/13] avfilter/split_cc: Add split_cc filter for closed caption handling

Message ID MN2PR04MB5981FC240CB5D4B1BA8A3AFEBAA49@MN2PR04MB5981.namprd04.prod.outlook.com
State Superseded, archived
Headers show
Series [FFmpeg-devel,v9,01/13] global: Prepare AVFrame for subtitle handling
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Soft Works Sept. 24, 2021, 12:31 a.m. UTC
- 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 <softworkz@hotmail.com>
---
 configure                 |   1 +
 doc/filters.texi          |  45 ++++++
 libavfilter/Makefile      |   1 +
 libavfilter/allfilters.c  |   1 +
 libavfilter/sf_split_cc.c | 298 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 346 insertions(+)
 create mode 100644 libavfilter/sf_split_cc.c
diff mbox series

Patch

diff --git a/configure b/configure
index 37fc4c20e7..6da059e6ee 100755
--- a/configure
+++ b/configure
@@ -3663,6 +3663,7 @@  spp_filter_select="fft idctdsp fdctdsp me_cmp pixblockdsp"
 sr_filter_deps="avformat swscale"
 sr_filter_select="dnn"
 stereo3d_filter_deps="gpl"
+split_cc_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 2d3dcdd7e6..c48e1fcf11 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -25378,6 +25378,16 @@  string containing ASS style format @code{KEY=VALUE} couples separated by ",".
 
 @end table
 
+@subsection Examples
+
+@itemize
+@item
+Overlay ASS subtitles with animations:
+@example
+ffmpeg -i "http://streams.videolan.org/samples/sub/SSA/subtitle_testing_complex.mkv" -filter_complex "[0:v]overlay_textsubs" -map 0 -y out.mkv
+@end example
+@end itemize
+
 @section show_speaker
 
 Prepend speaker names to subtitle lines (when available).
@@ -25441,6 +25451,41 @@  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
+
+@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]split_cc[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 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 b482f68286..802465fce2 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..d96912a90b
--- /dev/null
+++ b/libavfilter/sf_split_cc.c
@@ -0,0 +1,298 @@ 
+/*
+ * 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;
+} SplitCaptionsContext;
+
+static int init(AVFilterContext *ctx)
+{
+    SplitCaptionsContext *s = ctx->priv;
+
+    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);
+    }
+
+    if ((ret = avcodec_open2(s->cc_dec, codec, NULL)) < 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;
+
+            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 },
+    { 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),
+};