From d14b534482fb7e2ece9db9f823106a38c51f2376 Mon Sep 17 00:00:00 2001
From: rogerdpack <rogerpack2005@gmail.com>
Date: Tue, 28 Apr 2020 05:19:40 +0000
Subject: [PATCH 2/3] closed caption decoder: add new parameter to allow output
 to avoid repeated lines

Signed-off-by: rogerdpack <rogerpack2005@gmail.com>
---
 doc/decoders.texi         | 62 +++++++++++++++++++++++++++++++++++++++
 libavcodec/ccaption_dec.c | 11 +++++--
 2 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/doc/decoders.texi b/doc/decoders.texi
index 0c5a39bc9c..b4601d7e3c 100644
--- a/doc/decoders.texi
+++ b/doc/decoders.texi
@@ -252,6 +252,68 @@ Enabled by default.
 
 @end table
 
+@section cc_dec_raw_608
+
+Closed Caption decoder.
+Decodes "raw 608 byte pairs" closed captions.
+
+@subsection Options
+
+@table @option
+@item real_time
+Specifies if the decoder should output "the current screen"
+after each new character appears (in general, with closed
+captions, one new character can appear per frame).
+Default is false, decoder outputs "the current screen"
+only when a line feed occurs.
+
+@item rollup_override_line_count
+Specifies the number of lines to output (if the closed
+caption is using rollup, which is common).
+Typically the stream itself designates how many lines of
+closed caption material to display on the screen.
+This is typically more than one and means some lines are
+repeated in the output, since output occurs with each carriage
+return.
+Default is "0" (starts in rollup mode with line count 2,
+then then changes to whatever the closed caption stream
+later specifies).
+If you want a line output (no duplication), set this value to "1".
+
+@end table
+
+@section dvbsub
+@section cc_dec
+
+Closed Caption decoder.
+
+Decodes "608 over 708" closed captions.
+
+@subsection Options
+
+@table @option
+@item real_time
+Specifies if the decoder should output "the current screen"
+after each new character appears (in general, with closed
+captions, one new character can appear per frame).
+Default is false, decoder outputs "the current screen"
+only when a line feed occurs.
+
+@item rollup_override_line_count
+Specifies the number of lines to output (if the closed
+caption is using rollup, which is common).
+Typically the stream itself designates how many lines of
+closed caption material to display on the screen.
+This is typically more than one and means some lines are
+repeated in the output, since output occurs with each carriage
+return.
+Default is "0" (starts in rollup mode with line count 2,
+then then changes to whatever the closed caption stream
+later specifies).
+If you want a line output (no duplication), set this value to "1".
+
+@end table
+
 @section dvbsub
 
 @subsection Options
diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c
index 07091f4572..8cd948d709 100644
--- a/libavcodec/ccaption_dec.c
+++ b/libavcodec/ccaption_dec.c
@@ -238,6 +238,7 @@ typedef struct CCaptionSubContext {
     AVBPrint buffer;
     int buffer_changed;
     int rollup;
+    int rollup_override_line_count;
     enum cc_mode mode;
     int64_t start_time;
     /* visible screen time */
@@ -261,7 +262,7 @@ static av_cold int init_decoder(AVCodecContext *avctx)
     av_bprint_init(&ctx->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
     /* taking by default roll up to 2 */
     ctx->mode = CCMODE_ROLLUP;
-    ctx->rollup = 2;
+    ctx->rollup = ctx->rollup_override_line_count || 2;
     ctx->cursor_row = 10;
     ret = ff_ass_subtitle_header(avctx, "Monospace",
                                  ASS_DEFAULT_FONT_SIZE,
@@ -697,7 +698,12 @@ static void process_cc608(CCaptionSubContext *ctx, int64_t pts, uint8_t hi, uint
         case 0x25:
         case 0x26:
         case 0x27:
-            ctx->rollup = lo - 0x23;
+            if (ctx->rollup_override_line_count) {
+                ctx->rollup = ctx->rollup_override_line_count;
+            } else {
+                ctx->rollup = (lo - 0x23);
+            }
+            av_log(ctx, AV_LOG_DEBUG, "setting rollup to %d, requested from stream was %d, override is %d\n", ctx->rollup, (lo - 0x23), ctx->rollup_override_line_count);
             ctx->mode = CCMODE_ROLLUP;
             break;
         case 0x29:
@@ -842,6 +848,7 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp
 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
 static const AVOption options[] = {
     { "real_time", "emit subtitle events as they are decoded for real-time display", OFFSET(real_time), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD },
+    { "rollup_override_line_count", "force number of rollup lines [overrides any number specified by the captions themselves]", OFFSET(rollup_override_line_count), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, SD },
     {NULL}
 };
 
-- 
2.17.1

