diff mbox series

[FFmpeg-devel,2/3] closed caption decoder: add new parameter to allow output to avoid repeated lines

Message ID CAL1QdWdiw-z+NreOoF2qBWLoRhcrVp2BEwDp3nVpqZbNWdbnkw@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel,1/3] closed caption decoder: accept and decode a new codec type of 'raw 608 byte pairs' | expand

Commit Message

Roger Pack April 28, 2020, 6:18 a.m. UTC
I didn't actually *need* this but thought the functionality was very
nice to have, so here's my contribution.

It allows for forcing "rollup" style closed captions to output one
line at a time (basically, when there is a carriage return, it outputs
a line), so there is never any duplication in the output, for instance
if it's converted to .srt or .vtt files.  Without this you have output
like this:


00:01.536 --> 00:03.605
AND AS A SECONDARY QUESTION, I

00:03.605 --> 00:05.707
AND AS A SECONDARY QUESTION, I
WOULD ASK YOU HOW OUR VETERANS

00:05.706 --> 00:09.343
WOULD ASK YOU HOW OUR VETERANS
AND ACTIVE DUTY MILITARY HAVE

00:09.344 --> 00:10.412
AND ACTIVE DUTY MILITARY HAVE
FARED UNDER THIS COMMANDER IN

With it set to "1" you get output like this:


00:02.014 --> 00:03.049
SOLDIERS HOME IN HOLYOKE,

00:03.048 --> 00:04.116
MASSACHUSETTS, HAVE LOST 73.

00:04.116 --> 00:04.850
ANOTHER FACILITY IN NEW ORLEANS,

00:04.850 --> 00:06.184
53.

00:06.184 --> 00:07.753
VETERANS ARE IN REAL PERIL

00:07.753 --> 00:14.193
BECAUSE OF A LACK OF PREPARATION

00:14.193 --> 00:15.727
AND THEN THE SAME DYSFUNCTION

...

Thanks.

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

Comments

Roger Pack April 30, 2020, 5:30 a.m. UTC | #1
On Tue, Apr 28, 2020 at 1:46 AM Hendrik Leppkes <h.leppkes@gmail.com> wrote:
>
> On Tue, Apr 28, 2020 at 8:19 AM Roger Pack <rogerdpack2@gmail.com> wrote:
> >
> > I didn't actually *need* this but thought the functionality was very
> > nice to have, so here's my contribution.
> >
> > It allows for forcing "rollup" style closed captions to output one
> > line at a time (basically, when there is a carriage return, it outputs
> > a line), so there is never any duplication in the output, for instance
> > if it's converted to .srt or .vtt files.  Without this you have output
> > like this:
> >
> >
> > 00:01.536 --> 00:03.605
> > AND AS A SECONDARY QUESTION, I
> >
> > 00:03.605 --> 00:05.707
> > AND AS A SECONDARY QUESTION, I
> > WOULD ASK YOU HOW OUR VETERANS
> >
> > 00:05.706 --> 00:09.343
> > WOULD ASK YOU HOW OUR VETERANS
> > AND ACTIVE DUTY MILITARY HAVE
> >
> > 00:09.344 --> 00:10.412
> > AND ACTIVE DUTY MILITARY HAVE
> > FARED UNDER THIS COMMANDER IN
> >
> > With it set to "1" you get output like this:
> >
> >
> > 00:02.014 --> 00:03.049
> > SOLDIERS HOME IN HOLYOKE,
> >
> > 00:03.048 --> 00:04.116
> > MASSACHUSETTS, HAVE LOST 73.
> >
> > 00:04.116 --> 00:04.850
> > ANOTHER FACILITY IN NEW ORLEANS,
> >
> > 00:04.850 --> 00:06.184
> > 53.
> >
> > 00:06.184 --> 00:07.753
> > VETERANS ARE IN REAL PERIL
> >
> > 00:07.753 --> 00:14.193
> > BECAUSE OF A LACK OF PREPARATION
> >
> > 00:14.193 --> 00:15.727
> > AND THEN THE SAME DYSFUNCTION
> >
>
> I would've figured that even when you split it into  individual lines,
> that each line would show for the same amount as it did before, ie.
> with overlap in the SRT. Otherwise, some lines like the "ANOTHER
> FACILITY IN NEW ORLEANS," one are shown for not even a second, which
> seems pretty bad.


The chief goal of the new setting is to make it easier for automated
processes to parse the output (realtime or otherwise).

Like "read from analog tuner and tail the .vtt file and see what the
subtitles are" without having to worry about duplication of lines.  So
it not displaying for long isn't a problem in this case.
This seems like a useful functionality for me.
The goal here is to just show the latest line in the .srt output, no
previous lines.  I think that's useful, though not for display
purposes, in which case they can just use the default anyway?

Thanks.
-roger-

>
> - Hendrik
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

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