[FFmpeg-devel] [PATCH v14 2/2] libavformat/dvdvideo: add CLUT utilities and enable subtitle palette support

Marth64 marth64 at proxyid.net
Mon Feb 19 06:01:45 EET 2024


Signed-off-by: Marth64 <marth64 at proxyid.net>
---
 doc/demuxers.texi         |  5 +++
 libavformat/Makefile      |  2 +-
 libavformat/dvdclut.c     | 76 +++++++++++++++++++++++++++++++++++++++
 libavformat/dvdclut.h     | 37 +++++++++++++++++++
 libavformat/dvdvideodec.c | 14 ++++++++
 5 files changed, 133 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/dvdclut.c
 create mode 100644 libavformat/dvdclut.h

diff --git a/doc/demuxers.texi b/doc/demuxers.texi
index 062ea2ea42..d3c7675631 100644
--- a/doc/demuxers.texi
+++ b/doc/demuxers.texi
@@ -391,6 +391,11 @@ often with junk data intended for controlling a real DVD player's
 buffering speed and with no other material data value.
 Default is 1, true.
 
+ at item clut_rgb @var{bool}
+Output subtitle palettes (CLUTs) as RGB, required for Matroska.
+Disable to output the palette in its original YUV colorspace.
+Default is 1, true.
+
 @end table
 
 @subsection Examples
diff --git a/libavformat/Makefile b/libavformat/Makefile
index eda1f6e177..501159d9ca 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -192,7 +192,7 @@ OBJS-$(CONFIG_DTS_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_DV_MUXER)                  += dvenc.o
 OBJS-$(CONFIG_DVBSUB_DEMUXER)            += dvbsub.o rawdec.o
 OBJS-$(CONFIG_DVBTXT_DEMUXER)            += dvbtxt.o rawdec.o
-OBJS-$(CONFIG_DVDVIDEO_DEMUXER)          += dvdvideodec.o
+OBJS-$(CONFIG_DVDVIDEO_DEMUXER)          += dvdvideodec.o dvdclut.o
 OBJS-$(CONFIG_DXA_DEMUXER)               += dxa.o
 OBJS-$(CONFIG_EA_CDATA_DEMUXER)          += eacdata.o
 OBJS-$(CONFIG_EA_DEMUXER)                += electronicarts.o
diff --git a/libavformat/dvdclut.c b/libavformat/dvdclut.c
new file mode 100644
index 0000000000..2125e91541
--- /dev/null
+++ b/libavformat/dvdclut.c
@@ -0,0 +1,76 @@
+/*
+ * DVD-Video subpicture CLUT (Color Lookup Table) utilities
+ *
+ * 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
+ */
+
+#include "libavutil/bprint.h"
+#include "libavutil/colorspace.h"
+#include "libavutil/common.h"
+
+#include "dvdclut.h"
+#include "internal.h"
+
+int ff_dvdclut_palette_extradata_cat(const uint32_t *clut,
+                                     const size_t clut_size,
+                                     AVCodecParameters *par)
+{
+    int ret = 0;
+    AVBPrint bp;
+
+    if (clut_size != FF_DVDCLUT_CLUT_SIZE)
+        return AVERROR(EINVAL);
+
+    av_bprint_init(&bp, 0, FF_DVDCLUT_EXTRADATA_SIZE);
+
+    av_bprintf(&bp, "palette: ");
+
+    for (int i = 0; i < FF_DVDCLUT_CLUT_LEN; i++)
+        av_bprintf(&bp, "%06"PRIx32"%s", clut[i],
+                   i != (FF_DVDCLUT_CLUT_LEN - 1) ? ", " : "");
+
+    av_bprintf(&bp, "\n");
+
+    return ff_bprint_to_codecpar_extradata(par, &bp);
+}
+
+int ff_dvdclut_yuv_to_rgb(uint32_t *clut, const size_t clut_size)
+{
+    int y, cb, cr;
+    uint8_t r, g, b;
+    int r_add, g_add, b_add;
+
+    if (clut_size != FF_DVDCLUT_CLUT_SIZE)
+        return AVERROR(EINVAL);
+
+    for (int i = 0; i < FF_DVDCLUT_CLUT_LEN; i++) {
+        y  = (clut[i] >> 16) & 0xFF;
+        cr = (clut[i] >> 8)  & 0xFF;
+        cb = clut[i]         & 0xFF;
+
+        YUV_TO_RGB1_CCIR(cb, cr);
+
+        y = (y - 16) * FIX(255.0 / 219.0);
+        r = av_clip_uint8((y + r_add - 1024) >> SCALEBITS);
+        g = av_clip_uint8((y + g_add - 1024) >> SCALEBITS);
+        b = av_clip_uint8((y + b_add - 1024) >> SCALEBITS);
+
+        clut[i] = (r << 16) | (g << 8) | b;
+    }
+
+    return 0;
+}
diff --git a/libavformat/dvdclut.h b/libavformat/dvdclut.h
new file mode 100644
index 0000000000..41cea7e2c9
--- /dev/null
+++ b/libavformat/dvdclut.h
@@ -0,0 +1,37 @@
+/*
+ * DVD-Video subpicture CLUT (Color Lookup Table) utilities
+ *
+ * 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
+ */
+
+#ifndef AVFORMAT_DVDCLUT_H
+#define AVFORMAT_DVDCLUT_H
+
+#include "libavcodec/codec_par.h"
+
+/* ("palette: ") + ("rrggbb, "*15) + ("rrggbb") + \n + \0 */
+#define FF_DVDCLUT_EXTRADATA_SIZE        (9 + (8 * 15) + 6 + 1 + 1)
+#define FF_DVDCLUT_CLUT_LEN              16
+#define FF_DVDCLUT_CLUT_SIZE             FF_DVDCLUT_CLUT_LEN * sizeof(uint32_t)
+
+int ff_dvdclut_palette_extradata_cat(const uint32_t *clut,
+                                     const size_t clut_size,
+                                     AVCodecParameters *par);
+
+int ff_dvdclut_yuv_to_rgb(uint32_t *clut, const size_t clut_size);
+
+#endif /* AVFORMAT_DVDCLUT_H */
diff --git a/libavformat/dvdvideodec.c b/libavformat/dvdvideodec.c
index 3355010356..0951189124 100644
--- a/libavformat/dvdvideodec.c
+++ b/libavformat/dvdvideodec.c
@@ -49,6 +49,7 @@
 #include "avio_internal.h"
 #include "avlanguage.h"
 #include "demux.h"
+#include "dvdclut.h"
 #include "internal.h"
 #include "url.h"
 
@@ -95,6 +96,7 @@ typedef struct DVDVideoPGCSubtitleStreamEntry {
     int                                 startcode;
     enum DVDVideoSubpictureViewport     viewport;
     int                                 disposition;
+    uint32_t                            clut[FF_DVDCLUT_CLUT_LEN];
     const char                          *lang_iso;
 } DVDVideoPGCSubtitleStreamEntry;
 
@@ -128,6 +130,7 @@ typedef struct DVDVideoDemuxContext {
     int                         opt_angle;          /* the user-provided angle number (1-indexed) */
     int                         opt_chapter_end;    /* the user-provided exit PTT (0 for last) */
     int                         opt_chapter_start;  /* the user-provided entry PTT (1-indexed) */
+    int                         opt_clut_rgb;       /* output subtitle palette (CLUT) as RGB */
     int                         opt_pg;             /* the user-provided PG number (1-indexed) */
     int                         opt_pgc;            /* the user-provided PGC number (1-indexed) */
     int                         opt_preindex;       /* pre-indexing mode (2-pass read) */
@@ -1038,6 +1041,8 @@ break_error:
 static int dvdvideo_subp_stream_analyze(AVFormatContext *s, uint32_t offset, subp_attr_t subp_attr,
                                         DVDVideoPGCSubtitleStreamEntry *entry)
 {
+    DVDVideoDemuxContext *c = s->priv_data;
+
     char lang_dvd[3] = {0};
 
     entry->startcode = 0x20 + (offset & 0x1F);
@@ -1045,6 +1050,11 @@ static int dvdvideo_subp_stream_analyze(AVFormatContext *s, uint32_t offset, sub
     if (subp_attr.lang_extension == 9)
         entry->disposition |= AV_DISPOSITION_FORCED;
 
+    memcpy(&entry->clut, c->play_state.pgc->palette, FF_DVDCLUT_CLUT_SIZE);
+
+    if (c->opt_clut_rgb)
+        ff_dvdclut_yuv_to_rgb(entry->clut, FF_DVDCLUT_CLUT_SIZE);
+
     AV_WB16(lang_dvd, subp_attr.lang_code);
     entry->lang_iso = ff_convert_lang_to(lang_dvd, AV_LANG_ISO639_2_BIBL);
 
@@ -1066,6 +1076,9 @@ static int dvdvideo_subp_stream_add(AVFormatContext *s, DVDVideoPGCSubtitleStrea
     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
     st->codecpar->codec_id = AV_CODEC_ID_DVD_SUBTITLE;
 
+    if ((ret = ff_dvdclut_palette_extradata_cat(entry->clut, FF_DVDCLUT_CLUT_SIZE, st->codecpar)) < 0)
+        return ret;
+
     if (entry->lang_iso)
         av_dict_set(&st->metadata, "language", entry->lang_iso, 0);
 
@@ -1382,6 +1395,7 @@ static const AVOption dvdvideo_options[] = {
     {"angle",           "playback angle number",                                    OFFSET(opt_angle),          AV_OPT_TYPE_INT,    { .i64=1 },     1,          9,         AV_OPT_FLAG_DECODING_PARAM },
     {"chapter_end",     "exit chapter (PTT) number (0=end)",                        OFFSET(opt_chapter_end),    AV_OPT_TYPE_INT,    { .i64=0 },     0,          99,        AV_OPT_FLAG_DECODING_PARAM },
     {"chapter_start",   "entry chapter (PTT) number",                               OFFSET(opt_chapter_start),  AV_OPT_TYPE_INT,    { .i64=1 },     1,          99,        AV_OPT_FLAG_DECODING_PARAM },
+    {"clut_rgb",        "output subtitle palette (CLUT) as RGB",                    OFFSET(opt_clut_rgb),       AV_OPT_TYPE_BOOL,   { .i64=1 },     0,          1,         AV_OPT_FLAG_DECODING_PARAM },
     {"pg",              "entry PG number (0=auto)",                                 OFFSET(opt_pg),             AV_OPT_TYPE_INT,    { .i64=0 },     0,          255,       AV_OPT_FLAG_DECODING_PARAM },
     {"pgc",             "entry PGC number (0=auto)",                                OFFSET(opt_pgc),            AV_OPT_TYPE_INT,    { .i64=0 },     0,          999,       AV_OPT_FLAG_DECODING_PARAM },
     {"preindex",        "enable for accurate chapter markers, slow (2-pass read)",  OFFSET(opt_preindex),       AV_OPT_TYPE_BOOL,   { .i64=0 },     0,          1,         AV_OPT_FLAG_DECODING_PARAM },
-- 
2.34.1



More information about the ffmpeg-devel mailing list