PR #26214 opened by Pu Cheng (GeekQAQ)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26214
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26214.patch

Erasing a screen (flush_decoder(), EDM, and ENM in real-time mode) only
clears row_used and leaves the characters, charsets, colors and fonts of
the rows in place. A later command that marks a row as used without
overwriting all of it (a roll-up carriage return, a mid-row code, text
shorter than the previous one) then brings back erased text: after
avcodec_flush_buffers() text of the previous stream appears in the
captions of the next one, and within one stream text erased by EDM
reappears.

Clear the whole screen instead.

Signed-off-by: Pu Cheng <[email protected]>



>From 0333081f76b300c59769d8e17d148dec1f323e33 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:08:21 +0800
Subject: [PATCH 1/3] avcodec/ccaption_dec: reset the buffer times on flush

flush_decoder() resets the screens, the mode, the cursor, the buffers
and last_real_time, but not buffer_time[], which holds the start and end
time of the buffered caption. After avcodec_flush_buffers() the first
caption of the next stream gets its start time (and so its pts and
duration) from the previous stream.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavcodec/ccaption_dec.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c
index 489625489d7..48de672067f 100644
--- a/libavcodec/ccaption_dec.c
+++ b/libavcodec/ccaption_dec.c
@@ -327,6 +327,7 @@ static av_cold void flush_decoder(AVCodecContext *avctx)
     ctx->cursor_charset = 0;
     ctx->active_screen = 0;
     ctx->last_real_time = 0;
+    ctx->buffer_time[0] = ctx->buffer_time[1] = 0;
     ctx->screen_touched = 0;
     ctx->buffer_changed = 0;
     if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
-- 
2.54.1


>From 2c53761040b25d7eff1718eb4db90d8266054a80 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:08:21 +0800
Subject: [PATCH 2/3] avcodec/ccaption_dec: restore the data_field option on
 flush

With data_field set to auto (-1, the default), the decoder stores the
field of the first cc_data triplet in the option itself and from then
on only decodes that field. flush_decoder() does not restore it, so
after avcodec_flush_buffers() a stream whose captions are on the other
field is skipped entirely, while a newly opened decoder picks the field
of the new stream.

Keep the option value separate and restore it on flush.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavcodec/ccaption_dec.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c
index 48de672067f..93288aa0936 100644
--- a/libavcodec/ccaption_dec.c
+++ b/libavcodec/ccaption_dec.c
@@ -258,6 +258,7 @@ typedef struct CCaptionSubContext {
     int real_time;
     int real_time_latency_msec;
     int data_field;
+    int data_field_opt;
     struct Screen screen[2];
     int active_screen;
     uint8_t cursor_row;
@@ -283,6 +284,7 @@ static av_cold int init_decoder(AVCodecContext *avctx)
     CCaptionSubContext *ctx = avctx->priv_data;
 
     ctx->logctx = avctx;
+    ctx->data_field_opt = ctx->data_field;
 
     av_bprint_init(&ctx->buffer[0], 0, AV_BPRINT_SIZE_UNLIMITED);
     av_bprint_init(&ctx->buffer[1], 0, AV_BPRINT_SIZE_UNLIMITED);
@@ -320,6 +322,7 @@ static av_cold void flush_decoder(AVCodecContext *avctx)
     ctx->mode = CCMODE_ROLLUP;
     ctx->rollup = 2;
     ctx->cursor_row = 10;
+    ctx->data_field = ctx->data_field_opt;
     ctx->cursor_column = 0;
     ctx->cursor_font = 0;
     ctx->cursor_color = 0;
-- 
2.54.1


>From dababcfbad9244277bb92c6ceb7378601982749d Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Fri, 20 Nov 2026 14:08:21 +0800
Subject: [PATCH 3/3] avcodec/ccaption_dec: erase the characters when erasing a
 screen

Erasing a screen (flush_decoder(), EDM, and ENM in real-time mode) only
clears row_used and leaves the characters, charsets, colors and fonts of
the rows in place. A later command that marks a row as used without
overwriting all of it (a roll-up carriage return, a mid-row code, text
shorter than the previous one) then brings back erased text: after
avcodec_flush_buffers() text of the previous stream appears in the
captions of the next one, and within one stream text erased by EDM
reappears.

Clear the whole screen instead.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavcodec/ccaption_dec.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c
index 93288aa0936..fb0917096f1 100644
--- a/libavcodec/ccaption_dec.c
+++ b/libavcodec/ccaption_dec.c
@@ -312,11 +312,17 @@ static av_cold int close_decoder(AVCodecContext *avctx)
     return 0;
 }
 
+/* Erase a screen: characters, attributes and the row flags, as in a new 
decoder. */
+static void erase_screen(struct Screen *screen)
+{
+    memset(screen, 0, sizeof(*screen));
+}
+
 static av_cold void flush_decoder(AVCodecContext *avctx)
 {
     CCaptionSubContext *ctx = avctx->priv_data;
-    ctx->screen[0].row_used = 0;
-    ctx->screen[1].row_used = 0;
+    erase_screen(&ctx->screen[0]);
+    erase_screen(&ctx->screen[1]);
     ctx->prev_cmd[0] = 0;
     ctx->prev_cmd[1] = 0;
     ctx->mode = CCMODE_ROLLUP;
@@ -684,7 +690,7 @@ static int handle_edm(CCaptionSubContext *ctx)
     if (!ctx->real_time)
         ret = capture_screen(ctx);
 
-    screen->row_used = 0;
+    erase_screen(screen);
     ctx->bg_color = CCCOL_BLACK;
 
     // In realtime mode, emit an empty caption so the last one doesn't
@@ -823,7 +829,7 @@ static int process_cc608(CCaptionSubContext *ctx, uint8_t 
hi, uint8_t lo)
             // for our own buffering.
             if (ctx->real_time) {
                 struct Screen *screen = ctx->screen + !ctx->active_screen;
-                screen->row_used = 0;
+                erase_screen(screen);
             }
             break;
         case 0x2f:
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to