diff mbox series

[FFmpeg-devel,v3,5/6] avformat/rcwtdec: add RCWT Closed Captions demuxer

Message ID 20240312060005.2111135-6-marth64@proxyid.net
State New
Headers show
Series Closed Captions improvements (phase 1) | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Marth64 March 12, 2024, 6 a.m. UTC
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>
---
 libavformat/Makefile     |   1 +
 libavformat/allformats.c |   1 +
 libavformat/rcwtdec.c    | 158 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+)
 create mode 100644 libavformat/rcwtdec.c

Comments

Andreas Rheinhardt March 12, 2024, 11:44 a.m. UTC | #1
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>
> ---
>  libavformat/Makefile     |   1 +
>  libavformat/allformats.c |   1 +
>  libavformat/rcwtdec.c    | 158 +++++++++++++++++++++++++++++++++++++++
>  3 files changed, 160 insertions(+)
>  create mode 100644 libavformat/rcwtdec.c
> 
> diff --git a/libavformat/Makefile b/libavformat/Makefile
> index 8811a0ffc9..2092ca9f38 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 0a0e76138f..b89a49b6ec 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..f553f13366
> --- /dev/null
> +++ b/libavformat/rcwtdec.c
> @@ -0,0 +1,158 @@
> +/*
> + * 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.
> + * 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 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/avstring.h"
> +#include "libavutil/intreadwrite.h"

What are these two headers used for? (Didn't you add the same unused
headers to the muxer?)

> +
> +#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;
> +
> +    int64_t       cluster_pts = AV_NOPTS_VALUE;
> +    int           cluster_nb_blocks = 0;
> +    int           cluster_size = 0;
> +    uint8_t       *cluster_buf;

Use smaller scope for these.

> +
> +    /* validate the header */
> +    nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE);
> +    if (nb_bytes != RCWT_HEADER_SIZE || AV_RB16(header) != 0xCCCC || header[2] != 0xED) {
> +        av_log(avf, AV_LOG_ERROR, "Input is not an RCWT file\n");
> +        return AVERROR_INVALIDDATA;
> +    }

Such checks belong in a probe function (where it already is); the
demuxer is simply supposed to demux based upon the assumption that the
file is of the format.

> +
> +    if ((header[3] != 0xCC && header[3] != 0xFF) || header[4] != 0x00) {
> +        av_log(avf, AV_LOG_ERROR, "Input writing application is not supported, only "
> +                                  "0xCC00 (ccextractor) or 0xFF00 (FFmpeg) are compatible\n");
> +        return AVERROR_INVALIDDATA;

This will basically make it impossible to create a new application for
writing this format (or rather: it will force this new muxer to lie for
compatibility reasons).

> +    }
> +
> +    if (AV_RB16(header + 6) != 0x0001) {
> +        av_log(avf, AV_LOG_ERROR, "Input RCWT version is not compatible "
> +                                  "(only version 0.001 is known)\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    if (header[3] == 0xFF && header[5] != 0x60) {
> +        av_log(avf, AV_LOG_ERROR, "Input was written by a different version of FFmpeg "
> +                                  "and unsupported, consider upgrading\n");
> +        return AVERROR_INVALIDDATA;
> +    }
> +
> +    /* 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)) {
> +        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_calloc(cluster_nb_blocks, RCWT_BLOCK_SIZE);

Why are you zeroing when you are overwriting everything lateron anyway?

> +        if (!cluster_buf)
> +            return AVERROR(ENOMEM);
> +
> +        nb_bytes          = avio_read(avf->pb, cluster_buf, cluster_size);
> +        if (nb_bytes != cluster_size) {
> +            av_freep(&cluster_buf);
> +            av_log(avf, AV_LOG_ERROR, "Input cluster has invalid size "
> +                                      "(expected=%d actual=%d pos=%ld)\n",
> +                                      cluster_size, nb_bytes, avio_tell(avf->pb));

Not really useful message.

> +            return AVERROR_INVALIDDATA;

You should better use ffio_read() and return the error.

> +        }
> +
> +        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;

The muxer splits packets with >= 2^16 blocks. Should the demuxer
recombine such packets?

> +    }
> +
> +    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 ? 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
> +};
Marth64 March 12, 2024, 2:12 p.m. UTC | #2
Will address, thanks for the review.
diff mbox series

Patch

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 8811a0ffc9..2092ca9f38 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 0a0e76138f..b89a49b6ec 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..f553f13366
--- /dev/null
+++ b/libavformat/rcwtdec.c
@@ -0,0 +1,158 @@ 
+/*
+ * 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.
+ * 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 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/avstring.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;
+
+    int64_t       cluster_pts = AV_NOPTS_VALUE;
+    int           cluster_nb_blocks = 0;
+    int           cluster_size = 0;
+    uint8_t       *cluster_buf;
+
+    /* validate the header */
+    nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE);
+    if (nb_bytes != RCWT_HEADER_SIZE || AV_RB16(header) != 0xCCCC || header[2] != 0xED) {
+        av_log(avf, AV_LOG_ERROR, "Input is not an RCWT file\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if ((header[3] != 0xCC && header[3] != 0xFF) || header[4] != 0x00) {
+        av_log(avf, AV_LOG_ERROR, "Input writing application is not supported, only "
+                                  "0xCC00 (ccextractor) or 0xFF00 (FFmpeg) are compatible\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (AV_RB16(header + 6) != 0x0001) {
+        av_log(avf, AV_LOG_ERROR, "Input RCWT version is not compatible "
+                                  "(only version 0.001 is known)\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    if (header[3] == 0xFF && header[5] != 0x60) {
+        av_log(avf, AV_LOG_ERROR, "Input was written by a different version of FFmpeg "
+                                  "and unsupported, consider upgrading\n");
+        return AVERROR_INVALIDDATA;
+    }
+
+    /* 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)) {
+        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_calloc(cluster_nb_blocks, RCWT_BLOCK_SIZE);
+        if (!cluster_buf)
+            return AVERROR(ENOMEM);
+
+        nb_bytes          = avio_read(avf->pb, cluster_buf, cluster_size);
+        if (nb_bytes != cluster_size) {
+            av_freep(&cluster_buf);
+            av_log(avf, AV_LOG_ERROR, "Input cluster has invalid 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 ? 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
+};