Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH v6 0/4] RCWT Closed Captions demuxer (meta)
@ 2024-03-19 22:44 Marth64
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer Marth64
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Marth64 @ 2024-03-19 22:44 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marth64

Since v5:
* probe check is more strict, passes probetest fuzz
* documentation typos

Signed-off-by: Marth64 <marth64@proxyid.net>
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer
  2024-03-19 22:44 [FFmpeg-devel] [PATCH v6 0/4] RCWT Closed Captions demuxer (meta) Marth64
@ 2024-03-19 22:44 ` Marth64
  2024-03-20 14:14   ` Andreas Rheinhardt
  2024-03-20 14:37   ` Stefano Sabatini
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 2/4] avformat/rcwtenc: remove repeated documentation Marth64
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 9+ messages in thread
From: Marth64 @ 2024-03-19 22:44 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marth64

Raw Captions With Time (RCWT) is a format native to ccextractor, a commonly
used open source tool for processing 608/708 Closed Captions (CC) sources.
RCWT can be used to archive the original CC bitstream. The muxer was added
in January 2024. In this commit, add the demuxer.

One can now demux RCWT files for rendering in ccaption_dec or interoperate
with ccextractor (which produces RCWT). Using the muxer/demuxer combination,
the CC bits can be kept for further processing or rendering with either tool.
This can be an effective approach to backup original CC presentations.

Prior to this, the next best solution was FFmpeg's SCC muxer, but SCC itself
is not compatible with ccextractor (which is a de facto OSS CC processing tool)
and it is a proprietary format.

Tests will follow.

Signed-off-by: Marth64 <marth64@proxyid.net>
---
 Changelog                |   2 +-
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/rcwtdec.c    | 150 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/rcwtdec.c

diff --git a/Changelog b/Changelog
index e3ca52430c..abe5f79b85 100644
--- a/Changelog
+++ b/Changelog
@@ -19,7 +19,7 @@ version <next>:
 - lavu/eval: introduce randomi() function in expressions
 - VVC decoder
 - fsync filter
-- Raw Captions with Time (RCWT) closed caption muxer
+- RCWT (Raw Captions with Time) Closed Captions muxer and demuxer
 - ffmpeg CLI -bsf option may now be used for input as well as output
 - ffmpeg CLI options may now be used as -/opt <path>, which is equivalent
   to -opt <contents of file <path>>
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 94a949f555..a6de720d8c 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -493,6 +493,7 @@ OBJS-$(CONFIG_QOA_DEMUXER)               += qoadec.o
 OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
 OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += rawvideodec.o
 OBJS-$(CONFIG_RAWVIDEO_MUXER)            += rawenc.o
+OBJS-$(CONFIG_RCWT_DEMUXER)              += rcwtdec.o subtitles.o
 OBJS-$(CONFIG_RCWT_MUXER)                += rcwtenc.o subtitles.o
 OBJS-$(CONFIG_REALTEXT_DEMUXER)          += realtextdec.o subtitles.o
 OBJS-$(CONFIG_REDSPARK_DEMUXER)          += redspark.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index e15d0fa6d7..3140018f8d 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -391,6 +391,7 @@ extern const FFInputFormat  ff_qoa_demuxer;
 extern const FFInputFormat  ff_r3d_demuxer;
 extern const FFInputFormat  ff_rawvideo_demuxer;
 extern const FFOutputFormat ff_rawvideo_muxer;
+extern const FFInputFormat  ff_rcwt_demuxer;
 extern const FFOutputFormat ff_rcwt_muxer;
 extern const FFInputFormat  ff_realtext_demuxer;
 extern const FFInputFormat  ff_redspark_demuxer;
diff --git a/libavformat/rcwtdec.c b/libavformat/rcwtdec.c
new file mode 100644
index 0000000000..0d9eba025a
--- /dev/null
+++ b/libavformat/rcwtdec.c
@@ -0,0 +1,150 @@
+/*
+ * RCWT (Raw Captions With Time) demuxer
+ *
+ * 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
+ */
+
+/*
+ * RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
+ * used open source tool for processing 608/708 Closed Captions (CC) sources.
+ *
+ * This demuxer implements the specification as of March 2024, which has
+ * been stable and unchanged since April 2014.
+ *
+ * A free specification of RCWT can be found here:
+ * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
+ */
+
+#include "avformat.h"
+#include "demux.h"
+#include "internal.h"
+#include "subtitles.h"
+#include "libavutil/intreadwrite.h"
+
+#define RCWT_CLUSTER_MAX_BLOCKS             65535
+#define RCWT_BLOCK_SIZE                     3
+#define RCWT_HEADER_SIZE                    11
+
+typedef struct RCWTContext {
+    FFDemuxSubtitlesQueue q;
+} RCWTContext;
+
+static int rcwt_read_header(AVFormatContext *avf)
+{
+    RCWTContext *rcwt = avf->priv_data;
+
+    AVPacket      *sub = NULL;
+    AVStream      *st;
+    uint8_t       header[RCWT_HEADER_SIZE] = {0};
+    int           nb_bytes = 0;
+
+    /* validate the header */
+    nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE);
+    if (nb_bytes != RCWT_HEADER_SIZE) {
+        av_log(avf, AV_LOG_ERROR, "Header does not have the expected size "
+                                  "(expected=%d actual=%d)\n",
+                                  RCWT_HEADER_SIZE, nb_bytes);
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (AV_RB16(header + 6) != 0x0001) {
+        av_log(avf, AV_LOG_ERROR, "RCWT format version is not compatible "
+                                  "(only version 0.001 is known)\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    av_log(avf, AV_LOG_DEBUG, "RCWT writer application: %02X version: %02x\n",
+                              header[3], header[5]);
+
+    /* setup AVStream */
+    st = avformat_new_stream(avf, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+
+    st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
+    st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
+
+    avpriv_set_pts_info(st, 64, 1, 1000);
+
+    /* demux */
+    while (!avio_feof(avf->pb)) {
+        int64_t       cluster_pts       = AV_NOPTS_VALUE;
+        int           cluster_nb_blocks = 0;
+        int           cluster_size      = 0;
+        uint8_t       *cluster_buf;
+
+        cluster_pts       = avio_rl64(avf->pb);
+        cluster_nb_blocks = avio_rl16(avf->pb);
+        if (cluster_nb_blocks == 0)
+            continue;
+
+        cluster_size      = cluster_nb_blocks * RCWT_BLOCK_SIZE;
+        cluster_buf       = av_malloc(cluster_size);
+        if (!cluster_buf)
+            return AVERROR(ENOMEM);
+
+        nb_bytes          = avio_read(avf->pb, cluster_buf, cluster_size);
+        if (nb_bytes < 0)
+            return nb_bytes;
+
+        if (nb_bytes != cluster_size) {
+            av_freep(&cluster_buf);
+            av_log(avf, AV_LOG_ERROR, "Cluster does not have the expected size "
+                                      "(expected=%d actual=%d pos=%ld)\n",
+                                      cluster_size, nb_bytes, avio_tell(avf->pb));
+            return AVERROR_INVALIDDATA;
+        }
+
+        sub = ff_subtitles_queue_insert(&rcwt->q, cluster_buf, cluster_size, 0);
+        if (!sub) {
+            av_freep(&cluster_buf);
+            return AVERROR(ENOMEM);
+        }
+
+        sub->pos = avio_tell(avf->pb);
+        sub->pts = cluster_pts;
+
+        av_freep(&cluster_buf);
+        cluster_buf = NULL;
+    }
+
+    ff_subtitles_queue_finalize(avf, &rcwt->q);
+
+    return 0;
+}
+
+static int rcwt_probe(const AVProbeData *p)
+{
+    return p->buf_size > RCWT_HEADER_SIZE   &&
+           AV_RB16(p->buf) == 0xCCCC        &&
+           AV_RB8(p->buf + 2) == 0xED       &&
+           AV_RB16(p->buf + 6) == 0x0001    ? 50 : 0;
+}
+
+const FFInputFormat ff_rcwt_demuxer = {
+    .p.name         = "rcwt",
+    .p.long_name    = NULL_IF_CONFIG_SMALL("RCWT (Raw Captions With Time)"),
+    .p.extensions   = "bin",
+    .p.flags        = AVFMT_TS_DISCONT,
+    .priv_data_size = sizeof(RCWTContext),
+    .flags_internal = FF_FMT_INIT_CLEANUP,
+    .read_probe     = rcwt_probe,
+    .read_header    = rcwt_read_header,
+    .read_packet    = ff_subtitles_read_packet,
+    .read_seek2     = ff_subtitles_read_seek,
+    .read_close     = ff_subtitles_read_close
+};
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [FFmpeg-devel] [PATCH v6 2/4] avformat/rcwtenc: remove repeated documentation
  2024-03-19 22:44 [FFmpeg-devel] [PATCH v6 0/4] RCWT Closed Captions demuxer (meta) Marth64
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer Marth64
@ 2024-03-19 22:44 ` Marth64
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent Marth64
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 4/4] doc/indevs: update CC extraction example to use RCWT muxer Marth64
  3 siblings, 0 replies; 9+ messages in thread
From: Marth64 @ 2024-03-19 22:44 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marth64

The high level summary of RCWT can be delegated doc/muxers, which
makes it easier to maintain and more consistent with the documentation
of the demuxer.

Signed-off-by: Marth64 <marth64@proxyid.net>
---
 libavformat/rcwtenc.c | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/libavformat/rcwtenc.c b/libavformat/rcwtenc.c
index ed72de81ef..a1a8c06533 100644
--- a/libavformat/rcwtenc.c
+++ b/libavformat/rcwtenc.c
@@ -21,11 +21,6 @@
 /*
  * RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
  * used open source tool for processing 608/708 Closed Captions (CC) sources.
- * It can be used to archive the original, raw CC bitstream and to produce
- * a source file for later CC processing or conversion. As a result,
- * it also allows for interopability with ccextractor for processing CC data
- * extracted via ffmpeg. The format is simple to parse and can be used
- * to retain all lines and variants of CC.
  *
  * This muxer implements the specification as of March 2024, which has
  * been stable and unchanged since April 2014.
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent
  2024-03-19 22:44 [FFmpeg-devel] [PATCH v6 0/4] RCWT Closed Captions demuxer (meta) Marth64
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer Marth64
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 2/4] avformat/rcwtenc: remove repeated documentation Marth64
@ 2024-03-19 22:44 ` Marth64
  2024-03-20 14:39   ` Stefano Sabatini
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 4/4] doc/indevs: update CC extraction example to use RCWT muxer Marth64
  3 siblings, 1 reply; 9+ messages in thread
From: Marth64 @ 2024-03-19 22:44 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marth64

Signed-off-by: Marth64 <marth64@proxyid.net>
---
 doc/demuxers.texi | 26 ++++++++++++++++++++++++++
 doc/muxers.texi   | 29 +++++++++++++++++++----------
 2 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index b70f3a38d7..47e82eda55 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -1038,6 +1038,32 @@ the command:
 ffplay -f rawvideo -pixel_format rgb24 -video_size 320x240 -framerate 10 input.raw
 @end example
 
+@anchor{rcwtdec}
+@section rcwt
+
+RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
+used open source tool for processing 608/708 Closed Captions (CC) sources.
+For more information on the format, see @ref{rcwtenc,,,ffmpeg-formats}.
+
+This demuxer implements the specification as of March 2024, which has
+been stable and unchanged since April 2014.
+
+@subsection Examples
+
+@itemize
+@item
+Render Closed Captions to ASSA (using FFmpeg's CC decoder):
+@example
+ffmpeg -i CC.rcwt.bin CC.ass
+@end example
+
+@item
+Convert an RCWT backup to SCC:
+@example
+ffmpeg -i CC.rcwt.bin -c:s copy CC.scc
+@end example
+@end itemize
+
 @section sbg
 
 SBaGen script demuxer.
diff --git a/doc/muxers.texi b/doc/muxers.texi
index d9dd72e613..0695a317d4 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -2987,19 +2987,18 @@ ogg files can be safely chained.
 
 @end table
 
-@anchor{rcwt}
+@anchor{rcwtenc}
 @section rcwt
 
-Raw Captions With Time (RCWT) is a format native to ccextractor, a commonly
-used open source tool for processing 608/708 closed caption (CC) sources.
-It can be used to archive the original, raw CC bitstream and to produce
-a source file for later CC processing or conversion. As a result,
-it also allows for interopability with ccextractor for processing CC data
-extracted via ffmpeg. The format is simple to parse and can be used
-to retain all lines and variants of CC.
+RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
+used open source tool for processing 608/708 Closed Captions (CC) sources.
+It can be used to archive the original extracted CC bitstream and to produce
+a source file for later processing or conversion. The format allows
+for interoperability between ccextractor and FFmpeg, is simple to parse,
+and can be used to create a backup of the CC presentation.
 
-This muxer implements the specification as of 2024-01-05, which has
-been stable and unchanged for 10 years as of this writing.
+This muxer implements the specification as of March 2024, which has
+been stable and unchanged since April 2014.
 
 This muxer will have some nuances from the way that ccextractor muxes RCWT.
 No compatibility issues when processing the output with ccextractor
@@ -3009,6 +3008,16 @@ and outputs will not be a bit-exact match.
 A free specification of RCWT can be found here:
 @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
 
+@subsection Examples
+
+@itemize
+@item
+Extract Closed Captions to RCWT using lavfi:
+@example
+ffmpeg -f lavfi -i "movie=INPUT.mkv[out+subcc]" -map 0:s:0 -c:s copy CC.rcwt.bin
+@end example
+@end itemize
+
 @anchor{segment}
 @section segment, stream_segment, ssegment
 
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [FFmpeg-devel] [PATCH v6 4/4] doc/indevs: update CC extraction example to use RCWT muxer
  2024-03-19 22:44 [FFmpeg-devel] [PATCH v6 0/4] RCWT Closed Captions demuxer (meta) Marth64
                   ` (2 preceding siblings ...)
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent Marth64
@ 2024-03-19 22:44 ` Marth64
  3 siblings, 0 replies; 9+ messages in thread
From: Marth64 @ 2024-03-19 22:44 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Marth64

Signed-off-by: Marth64 <marth64@proxyid.net>
---
 doc/indevs.texi | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/doc/indevs.texi b/doc/indevs.texi
index d1b2bacf8b..fc14737181 100644
--- a/doc/indevs.texi
+++ b/doc/indevs.texi
@@ -1069,9 +1069,9 @@ ffplay -f lavfi "movie=test.avi[out0];amovie=test.wav[out1]"
 @end example
 
 @item
-Dump decoded frames to images and closed captions to a file (experimental):
+Dump decoded frames to images and Closed Captions to an RCWT backup:
 @example
-ffmpeg -f lavfi -i "movie=test.ts[out0+subcc]" -map v frame%08d.png -map s -c copy -f rawvideo subcc.bin
+ffmpeg -f lavfi -i "movie=test.ts[out0+subcc]" -map v frame%08d.png -map s -c copy subcc.bin
 @end example
 
 @end itemize
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer Marth64
@ 2024-03-20 14:14   ` Andreas Rheinhardt
  2024-03-20 14:37   ` Stefano Sabatini
  1 sibling, 0 replies; 9+ messages in thread
From: Andreas Rheinhardt @ 2024-03-20 14:14 UTC (permalink / raw)
  To: ffmpeg-devel

Marth64:
> Raw Captions With Time (RCWT) is a format native to ccextractor, a commonly
> used open source tool for processing 608/708 Closed Captions (CC) sources.
> RCWT can be used to archive the original CC bitstream. The muxer was added
> in January 2024. In this commit, add the demuxer.
> 
> One can now demux RCWT files for rendering in ccaption_dec or interoperate
> with ccextractor (which produces RCWT). Using the muxer/demuxer combination,
> the CC bits can be kept for further processing or rendering with either tool.
> This can be an effective approach to backup original CC presentations.
> 
> Prior to this, the next best solution was FFmpeg's SCC muxer, but SCC itself
> is not compatible with ccextractor (which is a de facto OSS CC processing tool)
> and it is a proprietary format.
> 
> Tests will follow.
> 
> Signed-off-by: Marth64 <marth64@proxyid.net>
> ---
>  Changelog                |   2 +-
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/rcwtdec.c    | 150 +++++++++++++++++++++++++++++++++++++++
>  4 files changed, 153 insertions(+), 1 deletion(-)
>  create mode 100644 libavformat/rcwtdec.c
> 
> diff --git a/Changelog b/Changelog
> index e3ca52430c..abe5f79b85 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -19,7 +19,7 @@ version <next>:
>  - lavu/eval: introduce randomi() function in expressions
>  - VVC decoder
>  - fsync filter
> -- Raw Captions with Time (RCWT) closed caption muxer
> +- RCWT (Raw Captions with Time) Closed Captions muxer and demuxer
>  - ffmpeg CLI -bsf option may now be used for input as well as output
>  - ffmpeg CLI options may now be used as -/opt <path>, which is equivalent
>    to -opt <contents of file <path>>
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 94a949f555..a6de720d8c 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -493,6 +493,7 @@ OBJS-$(CONFIG_QOA_DEMUXER)               += qoadec.o
>  OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
>  OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += rawvideodec.o
>  OBJS-$(CONFIG_RAWVIDEO_MUXER)            += rawenc.o
> +OBJS-$(CONFIG_RCWT_DEMUXER)              += rcwtdec.o subtitles.o
>  OBJS-$(CONFIG_RCWT_MUXER)                += rcwtenc.o subtitles.o
>  OBJS-$(CONFIG_REALTEXT_DEMUXER)          += realtextdec.o subtitles.o
>  OBJS-$(CONFIG_REDSPARK_DEMUXER)          += redspark.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index e15d0fa6d7..3140018f8d 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -391,6 +391,7 @@ extern const FFInputFormat  ff_qoa_demuxer;
>  extern const FFInputFormat  ff_r3d_demuxer;
>  extern const FFInputFormat  ff_rawvideo_demuxer;
>  extern const FFOutputFormat ff_rawvideo_muxer;
> +extern const FFInputFormat  ff_rcwt_demuxer;
>  extern const FFOutputFormat ff_rcwt_muxer;
>  extern const FFInputFormat  ff_realtext_demuxer;
>  extern const FFInputFormat  ff_redspark_demuxer;
> diff --git a/libavformat/rcwtdec.c b/libavformat/rcwtdec.c
> new file mode 100644
> index 0000000000..0d9eba025a
> --- /dev/null
> +++ b/libavformat/rcwtdec.c
> @@ -0,0 +1,150 @@
> +/*
> + * RCWT (Raw Captions With Time) demuxer
> + *
> + * 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
> + */
> +
> +/*
> + * RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
> + * used open source tool for processing 608/708 Closed Captions (CC) sources.
> + *
> + * This demuxer implements the specification as of March 2024, which has
> + * been stable and unchanged since April 2014.
> + *
> + * A free specification of RCWT can be found here:
> + * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
> + */
> +
> +#include "avformat.h"
> +#include "demux.h"
> +#include "internal.h"
> +#include "subtitles.h"
> +#include "libavutil/intreadwrite.h"
> +
> +#define RCWT_CLUSTER_MAX_BLOCKS             65535
> +#define RCWT_BLOCK_SIZE                     3
> +#define RCWT_HEADER_SIZE                    11
> +
> +typedef struct RCWTContext {
> +    FFDemuxSubtitlesQueue q;
> +} RCWTContext;
> +
> +static int rcwt_read_header(AVFormatContext *avf)
> +{
> +    RCWTContext *rcwt = avf->priv_data;
> +
> +    AVPacket      *sub = NULL;
> +    AVStream      *st;
> +    uint8_t       header[RCWT_HEADER_SIZE] = {0};

Pointless zeroing.

> +    int           nb_bytes = 0;
> +
> +    /* validate the header */
> +    nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE);

Better use ffio_read_size()

> +    if (nb_bytes != RCWT_HEADER_SIZE) {
> +        av_log(avf, AV_LOG_ERROR, "Header does not have the expected size "
> +                                  "(expected=%d actual=%d)\n",
> +                                  RCWT_HEADER_SIZE, nb_bytes);

Pointless logmessage

> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (AV_RB16(header + 6) != 0x0001) {
> +        av_log(avf, AV_LOG_ERROR, "RCWT format version is not compatible "
> +                                  "(only version 0.001 is known)\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    av_log(avf, AV_LOG_DEBUG, "RCWT writer application: %02X version: %02x\n",
> +                              header[3], header[5]);
> +
> +    /* setup AVStream */
> +    st = avformat_new_stream(avf, NULL);
> +    if (!st)
> +        return AVERROR(ENOMEM);
> +
> +    st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
> +    st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
> +
> +    avpriv_set_pts_info(st, 64, 1, 1000);
> +
> +    /* demux */
> +    while (!avio_feof(avf->pb)) {
> +        int64_t       cluster_pts       = AV_NOPTS_VALUE;
> +        int           cluster_nb_blocks = 0;
> +        int           cluster_size      = 0;
> +        uint8_t       *cluster_buf;
> +
> +        cluster_pts       = avio_rl64(avf->pb);
> +        cluster_nb_blocks = avio_rl16(avf->pb);

Why are you initializing these variables with values that are
immediately overwritten?

> +        if (cluster_nb_blocks == 0)
> +            continue;
> +
> +        cluster_size      = cluster_nb_blocks * RCWT_BLOCK_SIZE;
> +        cluster_buf       = av_malloc(cluster_size);
> +        if (!cluster_buf)
> +            return AVERROR(ENOMEM);
> +
> +        nb_bytes          = avio_read(avf->pb, cluster_buf, cluster_size);
> +        if (nb_bytes < 0)
> +            return nb_bytes;

Leak of cluster_buf

> +
> +        if (nb_bytes != cluster_size) {
> +            av_freep(&cluster_buf);
> +            av_log(avf, AV_LOG_ERROR, "Cluster does not have the expected size "
> +                                      "(expected=%d actual=%d pos=%ld)\n",

Wrong printf-specifier

> +                                      cluster_size, nb_bytes, avio_tell(avf->pb));

Also: Pointless logmessage

> +            return AVERROR_INVALIDDATA;
> +        }
> +
> +        sub = ff_subtitles_queue_insert(&rcwt->q, cluster_buf, cluster_size, 0);
> +        if (!sub) {
> +            av_freep(&cluster_buf);
> +            return AVERROR(ENOMEM);
> +        }
> +
> +        sub->pos = avio_tell(avf->pb);

This will use the position after data, not before the data.

> +        sub->pts = cluster_pts;
> +
> +        av_freep(&cluster_buf);
> +        cluster_buf = NULL;

This is already done by av_freep; also: your whole approach has an
unnecessary alloc+memcpy per packet. It would be better if you extended
ff_subtitles_queue_insert() to allow inserting NULL buffers with size
zero and then read the packet via av_get_packet(). This would also avoid
having this cluster_buf pointer which needs to be manually freed on
every error path.

> +    }
> +
> +    ff_subtitles_queue_finalize(avf, &rcwt->q);
> +
> +    return 0;
> +}
> +
> +static int rcwt_probe(const AVProbeData *p)
> +{
> +    return p->buf_size > RCWT_HEADER_SIZE   &&
> +           AV_RB16(p->buf) == 0xCCCC        &&
> +           AV_RB8(p->buf + 2) == 0xED       &&
> +           AV_RB16(p->buf + 6) == 0x0001    ? 50 : 0;
> +}
> +
> +const FFInputFormat ff_rcwt_demuxer = {
> +    .p.name         = "rcwt",
> +    .p.long_name    = NULL_IF_CONFIG_SMALL("RCWT (Raw Captions With Time)"),
> +    .p.extensions   = "bin",
> +    .p.flags        = AVFMT_TS_DISCONT,
> +    .priv_data_size = sizeof(RCWTContext),
> +    .flags_internal = FF_FMT_INIT_CLEANUP,
> +    .read_probe     = rcwt_probe,
> +    .read_header    = rcwt_read_header,
> +    .read_packet    = ff_subtitles_read_packet,
> +    .read_seek2     = ff_subtitles_read_seek,
> +    .read_close     = ff_subtitles_read_close
> +};

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer Marth64
  2024-03-20 14:14   ` Andreas Rheinhardt
@ 2024-03-20 14:37   ` Stefano Sabatini
  1 sibling, 0 replies; 9+ messages in thread
From: Stefano Sabatini @ 2024-03-20 14:37 UTC (permalink / raw)
  To: FFmpeg development discussions and patches; +Cc: Marth64

On date Tuesday 2024-03-19 17:44:26 -0500, Marth64 wrote:
> Raw Captions With Time (RCWT) is a format native to ccextractor, a commonly
> used open source tool for processing 608/708 Closed Captions (CC) sources.
> RCWT can be used to archive the original CC bitstream. The muxer was added
> in January 2024. In this commit, add the demuxer.
> 
> One can now demux RCWT files for rendering in ccaption_dec or interoperate
> with ccextractor (which produces RCWT). Using the muxer/demuxer combination,
> the CC bits can be kept for further processing or rendering with either tool.
> This can be an effective approach to backup original CC presentations.
> 
> Prior to this, the next best solution was FFmpeg's SCC muxer, but SCC itself
> is not compatible with ccextractor (which is a de facto OSS CC processing tool)
> and it is a proprietary format.
> 
> Tests will follow.
> 
> Signed-off-by: Marth64 <marth64@proxyid.net>
> ---
>  Changelog                |   2 +-
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/rcwtdec.c    | 150 +++++++++++++++++++++++++++++++++++++++

missing docs in the update?

>  4 files changed, 153 insertions(+), 1 deletion(-)
>  create mode 100644 libavformat/rcwtdec.c
> 
> diff --git a/Changelog b/Changelog
> index e3ca52430c..abe5f79b85 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -19,7 +19,7 @@ version <next>:
>  - lavu/eval: introduce randomi() function in expressions
>  - VVC decoder
>  - fsync filter
> -- Raw Captions with Time (RCWT) closed caption muxer
> +- RCWT (Raw Captions with Time) Closed Captions muxer and demuxer
>  - ffmpeg CLI -bsf option may now be used for input as well as output
>  - ffmpeg CLI options may now be used as -/opt <path>, which is equivalent
>    to -opt <contents of file <path>>
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 94a949f555..a6de720d8c 100644
> --- a/libavformat/Makefile
> +++ b/libavformat/Makefile
> @@ -493,6 +493,7 @@ OBJS-$(CONFIG_QOA_DEMUXER)               += qoadec.o
>  OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
>  OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += rawvideodec.o
>  OBJS-$(CONFIG_RAWVIDEO_MUXER)            += rawenc.o
> +OBJS-$(CONFIG_RCWT_DEMUXER)              += rcwtdec.o subtitles.o
>  OBJS-$(CONFIG_RCWT_MUXER)                += rcwtenc.o subtitles.o
>  OBJS-$(CONFIG_REALTEXT_DEMUXER)          += realtextdec.o subtitles.o
>  OBJS-$(CONFIG_REDSPARK_DEMUXER)          += redspark.o
> diff --git a/libavformat/allformats.c b/libavformat/allformats.c
> index e15d0fa6d7..3140018f8d 100644
> --- a/libavformat/allformats.c
> +++ b/libavformat/allformats.c
> @@ -391,6 +391,7 @@ extern const FFInputFormat  ff_qoa_demuxer;
>  extern const FFInputFormat  ff_r3d_demuxer;
>  extern const FFInputFormat  ff_rawvideo_demuxer;
>  extern const FFOutputFormat ff_rawvideo_muxer;
> +extern const FFInputFormat  ff_rcwt_demuxer;
>  extern const FFOutputFormat ff_rcwt_muxer;
>  extern const FFInputFormat  ff_realtext_demuxer;
>  extern const FFInputFormat  ff_redspark_demuxer;
> diff --git a/libavformat/rcwtdec.c b/libavformat/rcwtdec.c
> new file mode 100644
> index 0000000000..0d9eba025a
> --- /dev/null
> +++ b/libavformat/rcwtdec.c
> @@ -0,0 +1,150 @@
> +/*
> + * RCWT (Raw Captions With Time) demuxer
> + *
> + * 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
> + */
> +
> +/*
> + * RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
> + * used open source tool for processing 608/708 Closed Captions (CC) sources.
> + *
> + * This demuxer implements the specification as of March 2024, which has
> + * been stable and unchanged since April 2014.
> + *
> + * A free specification of RCWT can be found here:
> + * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
> + */
> +
> +#include "avformat.h"
> +#include "demux.h"
> +#include "internal.h"
> +#include "subtitles.h"
> +#include "libavutil/intreadwrite.h"
> +
> +#define RCWT_CLUSTER_MAX_BLOCKS             65535
> +#define RCWT_BLOCK_SIZE                     3
> +#define RCWT_HEADER_SIZE                    11
> +
> +typedef struct RCWTContext {
> +    FFDemuxSubtitlesQueue q;
> +} RCWTContext;
> +
> +static int rcwt_read_header(AVFormatContext *avf)
> +{
> +    RCWTContext *rcwt = avf->priv_data;
> +
> +    AVPacket      *sub = NULL;
> +    AVStream      *st;
> +    uint8_t       header[RCWT_HEADER_SIZE] = {0};
> +    int           nb_bytes = 0;
> +
> +    /* validate the header */
> +    nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE);
> +    if (nb_bytes != RCWT_HEADER_SIZE) {
> +        av_log(avf, AV_LOG_ERROR, "Header does not have the expected size "
> +                                  "(expected=%d actual=%d)\n",
> +                                  RCWT_HEADER_SIZE, nb_bytes);
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (AV_RB16(header + 6) != 0x0001) {
> +        av_log(avf, AV_LOG_ERROR, "RCWT format version is not compatible "
> +                                  "(only version 0.001 is known)\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    av_log(avf, AV_LOG_DEBUG, "RCWT writer application: %02X version: %02x\n",
> +                              header[3], header[5]);
> +
> +    /* setup AVStream */
> +    st = avformat_new_stream(avf, NULL);
> +    if (!st)
> +        return AVERROR(ENOMEM);
> +
> +    st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
> +    st->codecpar->codec_id   = AV_CODEC_ID_EIA_608;
> +
> +    avpriv_set_pts_info(st, 64, 1, 1000);
> +
> +    /* demux */
> +    while (!avio_feof(avf->pb)) {

> +        int64_t       cluster_pts       = AV_NOPTS_VALUE;
> +        int           cluster_nb_blocks = 0;
> +        int           cluster_size      = 0;

do you need to init these ones?

[...]

Looks good to me otherwise.
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent
  2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent Marth64
@ 2024-03-20 14:39   ` Stefano Sabatini
  2024-03-20 18:06     ` Marth64
  0 siblings, 1 reply; 9+ messages in thread
From: Stefano Sabatini @ 2024-03-20 14:39 UTC (permalink / raw)
  To: FFmpeg development discussions and patches; +Cc: Marth64

On date Tuesday 2024-03-19 17:44:28 -0500, Marth64 wrote:
> Signed-off-by: Marth64 <marth64@proxyid.net>
> ---
>  doc/demuxers.texi | 26 ++++++++++++++++++++++++++
>  doc/muxers.texi   | 29 +++++++++++++++++++----------
>  2 files changed, 45 insertions(+), 10 deletions(-)
> 
> diff --git a/doc/demuxers.texi b/doc/demuxers.texi
> index b70f3a38d7..47e82eda55 100644
> --- a/doc/demuxers.texi
> +++ b/doc/demuxers.texi
> @@ -1038,6 +1038,32 @@ the command:
>  ffplay -f rawvideo -pixel_format rgb24 -video_size 320x240 -framerate 10 input.raw
>  @end example
>  
> +@anchor{rcwtdec}
> +@section rcwt
> +
> +RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
> +used open source tool for processing 608/708 Closed Captions (CC) sources.
> +For more information on the format, see @ref{rcwtenc,,,ffmpeg-formats}.
> +
> +This demuxer implements the specification as of March 2024, which has
> +been stable and unchanged since April 2014.
> +
> +@subsection Examples
> +
> +@itemize
> +@item

> +Render Closed Captions to ASSA (using FFmpeg's CC decoder):

what is ASSA?

> +@example
> +ffmpeg -i CC.rcwt.bin CC.ass
> +@end example
> +
> +@item

> +Convert an RCWT backup to SCC:

let's expand SCC

> +@example
> +ffmpeg -i CC.rcwt.bin -c:s copy CC.scc
> +@end example
> +@end itemize
> +
LGTM otheriwse, thanks.
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent
  2024-03-20 14:39   ` Stefano Sabatini
@ 2024-03-20 18:06     ` Marth64
  0 siblings, 0 replies; 9+ messages in thread
From: Marth64 @ 2024-03-20 18:06 UTC (permalink / raw)
  To: FFmpeg development discussions and patches, Marth64

> what is ASSA?
This should just be ASS, will update

> let's expand SCC
> for the sake of exclicitness, I'd avoid more acronyms, I'd guess this
> is source Closed Caption?
Actually SCC is a format itself (Scenarist Closed Caption) of which FFmpeg
has a muxer and demuxer.
SCC is one of the standard proprietary formats to exchange Closed Captions.
I'll update the doc to highlight the fact better that this is a different
format.

Thank you for the reviews. Working on updates, will send a v7 when ready.
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-03-20 18:07 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-19 22:44 [FFmpeg-devel] [PATCH v6 0/4] RCWT Closed Captions demuxer (meta) Marth64
2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 1/4] avformat/rcwtdec: add RCWT Closed Captions demuxer Marth64
2024-03-20 14:14   ` Andreas Rheinhardt
2024-03-20 14:37   ` Stefano Sabatini
2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 2/4] avformat/rcwtenc: remove repeated documentation Marth64
2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 3/4] doc/{muxers, demuxers}: add RCWT demuxer doc and refresh the muxer's doc to be consistent Marth64
2024-03-20 14:39   ` Stefano Sabatini
2024-03-20 18:06     ` Marth64
2024-03-19 22:44 ` [FFmpeg-devel] [PATCH v6 4/4] doc/indevs: update CC extraction example to use RCWT muxer Marth64

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git