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

2024-03-02 Thread Marth64
Bump on this one. Would be really nice to have since dvd demuxer is merged,
and colors are part of the presentation. It did have 2 reviews. Thank you.


On Sun, Feb 18, 2024 at 10:02 PM Marth64  wrote:

> Signed-off-by: Marth64 
> ---
>  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.
>
> +@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 00..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 00..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 t

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

2024-02-18 Thread Marth64
Signed-off-by: Marth64 
---
 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.
 
+@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 00..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 00..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