Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Marth64 <marth64@proxyid.net>
To: ffmpeg-devel@ffmpeg.org
Cc: Marth64 <marth64@proxyid.net>
Subject: [FFmpeg-devel] [PATCH v2 2/3] avformat/dvdvideodec: add CLUT utilities and subtitle color support
Date: Sat,  9 Mar 2024 12:27:51 -0600
Message-ID: <20240309182752.3204466-3-marth64@proxyid.net> (raw)
In-Reply-To: <20240309182752.3204466-1-marth64@proxyid.net>

Signed-off-by: Marth64 <marth64@proxyid.net>
---
 libavformat/Makefile      |  2 +-
 libavformat/dvdclut.c     | 75 +++++++++++++++++++++++++++++++++++++++
 libavformat/dvdclut.h     | 37 +++++++++++++++++++
 libavformat/dvdvideodec.c | 14 ++++++++
 4 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100644 libavformat/dvdclut.c
 create mode 100644 libavformat/dvdclut.h

diff --git a/libavformat/Makefile b/libavformat/Makefile
index 8811a0ffc9..a3bfc209c3 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -194,7 +194,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..cd4b103e4b
--- /dev/null
+++ b/libavformat/dvdclut.c
@@ -0,0 +1,75 @@
+/*
+ * 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)
+{
+    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 ca85aa8d3d..6f626ce9a0 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;
 
@@ -1040,6 +1042,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);
@@ -1047,6 +1051,12 @@ 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);
+
+    /* dvdsub palettes currently have no colorspace tagging and all muxers only support RGB */
+    /* this is not a lossless conversion, but no use cases are supported for the original YUV */
+    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);
 
@@ -1058,6 +1068,7 @@ static int dvdvideo_subp_stream_add(AVFormatContext *s, DVDVideoPGCSubtitleStrea
 {
     AVStream *st;
     FFStream *sti;
+    int ret;
 
     st = avformat_new_stream(s, NULL);
     if (!st)
@@ -1067,6 +1078,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);
 
-- 
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".

  parent reply	other threads:[~2024-03-09 18:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-09 18:27 [FFmpeg-devel] [PATCH v2 0/3] avformat/dvdvideodec: various improvements Marth64
2024-03-09 18:27 ` [FFmpeg-devel] [PATCH v2 1/3] avformat/dvdvideodec: assign mono channel layout explicitly Marth64
2024-03-10 14:01   ` Stefano Sabatini
2024-03-09 18:27 ` Marth64 [this message]
2024-03-10 14:10   ` [FFmpeg-devel] [PATCH v2 2/3] avformat/dvdvideodec: add CLUT utilities and subtitle color support Stefano Sabatini
2024-03-09 18:27 ` [FFmpeg-devel] [PATCH v2 3/3] avformat/dvdvideodec: add menu demuxing support Marth64
2024-03-10 19:53   ` Leo Izen
2024-03-10 20:03     ` Marth64
2024-03-12 10:22   ` Stefano Sabatini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240309182752.3204466-3-marth64@proxyid.net \
    --to=marth64@proxyid.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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