diff mbox series

[FFmpeg-devel,v2,25/26] avcodec/webvttenc: Don't encode drawing codes and empty lines

Message ID 7ab6dedf807b362ebe698c9f081a148851ea5b7e.1642646916.git.ffmpegagent@gmail.com
State New
Headers show
Series Subtitle Filtering 2022 | expand

Checks

Context Check Description
andriy/configurex86 warning Failed to apply patch
andriy/configure_aarch64_jetson warning Failed to apply patch
andriy/configureppc warning Failed to apply patch

Commit Message

Matt Oliver Jan. 20, 2022, 2:48 a.m. UTC
From: softworkz <softworkz@hotmail.com>

Signed-off-by: softworkz <softworkz@hotmail.com>
---
 libavcodec/webvttenc.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/webvttenc.c b/libavcodec/webvttenc.c
index 48945dcb8e..62c4aa7ffd 100644
--- a/libavcodec/webvttenc.c
+++ b/libavcodec/webvttenc.c
@@ -39,6 +39,8 @@  typedef struct {
     int count;
     char stack[WEBVTT_STACK_SIZE];
     int stack_ptr;
+    int has_text;
+    int drawing_scale;
 } WebVTTContext;
 
 #ifdef __GNUC__
@@ -115,17 +117,24 @@  static void webvtt_style_apply(WebVTTContext *s, const char *style)
 static void webvtt_text_cb(void *priv, const char *text, int len)
 {
     WebVTTContext *s = priv;
-    av_bprint_append_data(&s->buffer, text, len);
+    if (!s->drawing_scale) {
+        av_bprint_append_data(&s->buffer, text, len);
+        s->has_text = 1;
+    }
 }
 
 static void webvtt_new_line_cb(void *priv, int forced)
 {
-    webvtt_print(priv, "\n");
+    WebVTTContext *s = priv;
+    if (!s->drawing_scale)
+        webvtt_print(priv, "\n");
 }
 
 static void webvtt_hard_space_cb(void *priv)
 {
-    webvtt_print(priv, "&nbsp;");
+    WebVTTContext *s = priv;
+    if (!s->drawing_scale)
+        webvtt_print(priv, "&nbsp;");
 }
 
 static void webvtt_style_cb(void *priv, char style, int close)
@@ -149,6 +158,12 @@  static void webvtt_end_cb(void *priv)
     webvtt_stack_push_pop(priv, 0, 1);
 }
 
+static void dialog_drawing_mode_cb(void *priv, int scale)
+{
+    WebVTTContext *s = priv;
+    s->drawing_scale = scale;
+}
+
 static const ASSCodesCallbacks webvtt_callbacks = {
     .text             = webvtt_text_cb,
     .new_line         = webvtt_new_line_cb,
@@ -161,6 +176,7 @@  static const ASSCodesCallbacks webvtt_callbacks = {
     .cancel_overrides = webvtt_cancel_overrides_cb,
     .move             = NULL,
     .end              = webvtt_end_cb,
+    .drawing_mode     = dialog_drawing_mode_cb,
 };
 
 static void ensure_ass_context(WebVTTContext* s, const AVFrame* frame)
@@ -211,16 +227,23 @@  static int webvtt_encode_frame(AVCodecContext* avctx, AVPacket* avpkt,
         }
 
         if (ass) {
+            const unsigned saved_len = s->buffer.len;
 
-            if (i > 0)
+            if (i > 0 && s->buffer.len > 0)
                 webvtt_new_line_cb(s, 0);
 
+            s->drawing_scale = 0;
+            s->has_text = 0;
+
             dialog = avpriv_ass_split_dialog(s->ass_ctx, ass);
             if (!dialog)
                 return AVERROR(ENOMEM);
             webvtt_style_apply(s, dialog->style);
             avpriv_ass_split_override_codes(&webvtt_callbacks, s, dialog->text);
             avpriv_ass_free_dialog(&dialog);
+
+            if (!s->has_text)
+                s->buffer.len = saved_len;
         }
     }