diff mbox series

[FFmpeg-devel,v5,12/12] avfilter/stripstyles: Add stripstyles filter

Message ID MN2PR04MB598189CD27C2E35AB817FAD6BAD89@MN2PR04MB5981.namprd04.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel,v5,01/12] avutil/frame: Subtitle Filtering - Add AVMediaType property to AVFrame
Related show

Checks

Context Check Description
andriy/configurex86 warning Failed to apply patch

Commit Message

Soft Works Sept. 12, 2021, 3:22 a.m. UTC
Signed-off-by: softworkz <softworkz@hotmail.com>
---
 doc/filters.texi             |  24 ++++
 libavfilter/Makefile         |   1 +
 libavfilter/allfilters.c     |   1 +
 libavfilter/sf_stripstyles.c | 215 +++++++++++++++++++++++++++++++++++
 4 files changed, 241 insertions(+)
 create mode 100644 libavfilter/sf_stripstyles.c
diff mbox series

Patch

diff --git a/doc/filters.texi b/doc/filters.texi
index 9fd2876d63..6c74dacee3 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -25024,6 +25024,30 @@  existing filters using @code{--disable-filters}.
 
 Below is a description of the currently available subtitle filters.
 
+@section stripstyles
+
+Remove all inline styles from subtitle events.
+
+It accepts the following parameters:
+
+@table @option
+@item remove_animated
+Also remove text which is subject to animation (default: true)
+Usually, animated text elements are used used in addition to static subtitle lines for creating effects, so in most cases it is safe to remove the animation content.
+If subtitle text is missing, try setting this to false.
+
+@end table
+
+@subsection Examples
+
+@itemize
+@item
+Change all characters to upper case while keeping all styles and animations:
+@example
+ffmpeg -i "https://streams.videolan.org/samples/sub/SSA/subtitle_testing_complex.mkv" -filter_complex "[0:1]stripstyles" -map 0 output.mkv
+@end example
+@end itemize
+
 @section textmod
 
 Modify subtitle text in a number of ways.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 5a5a4be47e..626e1bed74 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -536,6 +536,7 @@  OBJS-$(CONFIG_NULLSINK_FILTER)               += vsink_nullsink.o
 
 # subtitle filters
 OBJS-$(CONFIG_TEXTMOD_FILTER)                += sf_textmod.o
+OBJS-$(CONFIG_STRIPSTYLES_FILTER)            += sf_stripstyles.o
 
 # multimedia filters
 OBJS-$(CONFIG_ABITSCOPE_FILTER)              += avf_abitscope.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 6d7a535ee8..8f7d697408 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -524,6 +524,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_stripstyles;
 extern const AVFilter ff_sf_textmod;
 extern const AVFilter ff_svf_graphicsub2video;
 extern const AVFilter ff_svf_textsub2video;
diff --git a/libavfilter/sf_stripstyles.c b/libavfilter/sf_stripstyles.c
new file mode 100644
index 0000000000..20e42b47e0
--- /dev/null
+++ b/libavfilter/sf_stripstyles.c
@@ -0,0 +1,215 @@ 
+/*
+ * 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 removes inline-styles from subtitles
+ */
+
+#include <libavcodec/ass.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "internal.h"
+#include "libavcodec/avcodec.h"
+#include "libavcodec/ass_split.h"
+
+typedef struct StripStylesContext {
+    const AVClass *class;
+    enum AVSubtitleType format;
+    int remove_animated;
+} StripStylesContext;
+
+typedef struct DialogContext {
+    const AVClass *class;
+    const StripStylesContext* ss_ctx;
+    AVBPrint buffer;
+    int drawing_scale;
+    int is_animated;
+} DialogContext;
+
+static void dialog_text_cb(void *priv, const char *text, int len)
+{
+    DialogContext *s = priv;
+    if (!s->drawing_scale && (!s->is_animated || !s->ss_ctx->remove_animated))
+        av_bprint_append_data(&s->buffer, text, len);
+}
+
+static void dialog_new_line_cb(void *priv, int forced)
+{
+    DialogContext *s = priv;
+    if (!s->drawing_scale && !s->is_animated)
+        av_bprint_append_data(&s->buffer, forced ? "\\N" : "\\n", 2);
+}
+
+static void dialog_drawing_mode_cb(void *priv, int scale)
+{
+    DialogContext *s = priv;
+    s->drawing_scale = scale;
+}
+
+static void dialog_animate_cb(void *priv, int t1, int t2, int accel, char *style)
+{
+    DialogContext *s = priv;
+    s->is_animated = 1;
+}
+
+static void dialog_move_cb(void *priv, int x1, int y1, int x2, int y2, int t1, int t2)
+{
+    DialogContext *s = priv;
+    if (t1 >= 0 || t2 >= 0)
+        s->is_animated = 1;
+}
+
+static const ASSCodesCallbacks dialog_callbacks = {
+    .text             = dialog_text_cb,
+    .new_line         = dialog_new_line_cb,
+    .drawing_mode     = dialog_drawing_mode_cb,
+    .animate          = dialog_animate_cb,
+    .move             = dialog_move_cb,
+};
+
+static int query_formats(AVFilterContext *ctx)
+{
+    AVFilterFormats *formats;
+    AVFilterLink *inlink = ctx->inputs[0];
+    AVFilterLink *outlink = ctx->outputs[0];
+    static const enum AVSubtitleType subtitle_fmts[] = { AV_SUBTITLE_FMT_ASS, AV_SUBTITLE_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 */
+    if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
+        return ret;
+
+    return 0;
+}
+
+static char *ass_get_line(int readorder, int layer, const char *style,
+                        const char *speaker, const char *effect, const char *text)
+{
+    return av_asprintf("%d,%d,%s,%s,0,0,0,%s,%s",
+                       readorder, layer, style ? style : "Default",
+                       speaker ? speaker : "", effect, text);
+}
+
+static char *process_dialog(StripStylesContext *s, const char *ass_line)
+{
+    ASSDialog *dialog = ff_ass_split_dialog(NULL, ass_line);
+    char *result = NULL, *text;
+    DialogContext dlg_ctx = { 0 };
+
+    if (!dialog)
+        return NULL;
+
+    dlg_ctx.ss_ctx = s;
+
+    av_bprint_init(&dlg_ctx.buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+    ff_ass_split_override_codes(&dialog_callbacks, &dlg_ctx, dialog->text);
+
+    av_bprint_finalize(&dlg_ctx.buffer, &text);
+
+    if (text && strlen(text) > 0)
+        result = ass_get_line(dialog->readorder, dialog->layer, dialog->style, dialog->name, dialog->effect, text);
+
+    av_free(text);
+    ff_ass_free_dialog(&dialog);
+
+    return result;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *src_frame)
+{
+    StripStylesContext *s = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFrame *out;
+
+    outlink->format = inlink->format;
+
+    out = av_frame_clone(src_frame);
+    if (!out)
+        return AVERROR(ENOMEM);
+
+    for (unsigned i = 0; i < out->num_subtitle_rects; i++) {
+
+        AVSubtitleRect *rect = out->subtitle_rects[i];
+
+        if (rect->ass) {
+            if (rect->ass) {
+                char *tmp = rect->ass;
+                rect->ass = process_dialog(s, rect->ass);
+
+                if (rect->ass) {
+                    av_log(inlink->dst, AV_LOG_DEBUG, "original: %s\n", tmp);
+                    av_log(inlink->dst, AV_LOG_DEBUG, "stripped: %s\n", rect->ass);
+                }
+                else
+                    rect->ass = av_strdup("");
+
+                av_free(tmp);
+            }
+        }
+    }
+
+    av_frame_free(&src_frame);
+    return ff_filter_frame(outlink, out);
+}
+
+#define OFFSET(x) offsetof(StripStylesContext, x)
+#define FLAGS (AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_FILTERING_PARAM)
+
+static const AVOption stripstyles_options[] = {
+    { "remove_animated", "remove animated text (default: yes)",   OFFSET(remove_animated),  AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1, FLAGS, 0 },
+    { NULL },
+};
+
+AVFILTER_DEFINE_CLASS(stripstyles);
+
+static const AVFilterPad inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_SUBTITLE,
+        .filter_frame = filter_frame,
+    },
+};
+
+static const AVFilterPad outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_SUBTITLE,
+    },
+};
+
+const AVFilter ff_sf_stripstyles = {
+    .name          = "stripstyles",
+    .description   = NULL_IF_CONFIG_SMALL("Strip subtitle inline styles"),
+    .query_formats = query_formats,
+    .priv_size     = sizeof(StripStylesContext),
+    .priv_class    = &stripstyles_class,
+    FILTER_INPUTS(inputs),
+    FILTER_OUTPUTS(outputs),
+};
+