Re: [FFmpeg-devel] [PATCH v4 2/4] avcodec/jpegxl_parser: add JPEG XL parser

2023-06-27 Thread James Almer

On 6/27/2023 8:18 PM, Leo Izen wrote:

On 6/27/23 18:58, James Almer wrote:

On 6/26/2023 12:49 PM, Leo Izen wrote:

+/*
+ * copies as much of the codestream into the buffer as possible
+ * pass a shorter buflen to request less
+ * returns the number of bytes consumed from input, may be greater 
than input_len

+ * if the input doesn't end on an ISOBMFF-box boundary
+ */
+int ff_jpegxl_collect_codestream_header(const uint8_t *input_buffer, 
int input_len,
+    uint8_t *buffer, int buflen, 
int *copied)

+{
+    GetByteContext gb;
+    int pos = 0;
+    bytestream2_init(, input_buffer, input_len);
+
+    while (1) {
+    uint64_t size;
+    uint32_t tag;
+    int head_size = 8;
+
+    if (bytestream2_get_bytes_left() < 16)
+    break;
+
+    size = bytestream2_get_be32();
+    if (size == 1) {
+    size = bytestream2_get_be64();
+    head_size = 16;
+    }
+    /* invalid ISOBMFF size */
+    if (size && size <= head_size)
+    return AVERROR_INVALIDDATA;


ISOBMFF? Are you propagating container bytes as if they were part of 
the bitstream from image2? Why is it not being handled by the mov/mp4 
demuxer instead?


It's not actually ISOBMFF, it just uses the same style of box layout. 
This is also how j2k works.


Ok.






+    if (ctx->meta.csp == JPEGXL_CS_GRAY) {
+    if (ctx->meta.bit_depth <= 8)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_YA8 : AV_PIX_FMT_GRAY8;

+    else if (ctx->meta.bit_depth <= 16)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_YA16 : AV_PIX_FMT_GRAY16;

+    else
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_NONE : AV_PIX_FMT_GRAYF32;

+    } else {
+    if (ctx->meta.bit_depth <= 8)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;

+    else if (ctx->meta.bit_depth <= 16)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBA64 : AV_PIX_FMT_RGB48;

+    else
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBAF32 : AV_PIX_FMT_RGBF32;


Again, don't fill avctx->pix_fmt, fill s->format.
___


If I don't fill this, the CLI will not show the pixel format without the 
external decoder library enabled. Should I consider this an ffmpeg.c bug?


That's expected, and it's not a bug. A given decoder can and many times 
disagrees with the parser. In an scenario without a decoder, remuxing 
still works as pix_fmt is not a container level parameter.

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v4 2/4] avcodec/jpegxl_parser: add JPEG XL parser

2023-06-27 Thread Leo Izen

On 6/27/23 18:58, James Almer wrote:

On 6/26/2023 12:49 PM, Leo Izen wrote:

+/*
+ * copies as much of the codestream into the buffer as possible
+ * pass a shorter buflen to request less
+ * returns the number of bytes consumed from input, may be greater 
than input_len

+ * if the input doesn't end on an ISOBMFF-box boundary
+ */
+int ff_jpegxl_collect_codestream_header(const uint8_t *input_buffer, 
int input_len,
+    uint8_t *buffer, int buflen, 
int *copied)

+{
+    GetByteContext gb;
+    int pos = 0;
+    bytestream2_init(, input_buffer, input_len);
+
+    while (1) {
+    uint64_t size;
+    uint32_t tag;
+    int head_size = 8;
+
+    if (bytestream2_get_bytes_left() < 16)
+    break;
+
+    size = bytestream2_get_be32();
+    if (size == 1) {
+    size = bytestream2_get_be64();
+    head_size = 16;
+    }
+    /* invalid ISOBMFF size */
+    if (size && size <= head_size)
+    return AVERROR_INVALIDDATA;


ISOBMFF? Are you propagating container bytes as if they were part of the 
bitstream from image2? Why is it not being handled by the mov/mp4 
demuxer instead?


It's not actually ISOBMFF, it just uses the same style of box layout. 
This is also how j2k works.





+    if (ctx->meta.csp == JPEGXL_CS_GRAY) {
+    if (ctx->meta.bit_depth <= 8)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_YA8 : AV_PIX_FMT_GRAY8;

+    else if (ctx->meta.bit_depth <= 16)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_YA16 : AV_PIX_FMT_GRAY16;

+    else
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_NONE : AV_PIX_FMT_GRAYF32;

+    } else {
+    if (ctx->meta.bit_depth <= 8)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;

+    else if (ctx->meta.bit_depth <= 16)
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBA64 : AV_PIX_FMT_RGB48;

+    else
+    avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBAF32 : AV_PIX_FMT_RGBF32;


Again, don't fill avctx->pix_fmt, fill s->format.
___


If I don't fill this, the CLI will not show the pixel format without the 
external decoder library enabled. Should I consider this an ffmpeg.c bug?


- Leo Izen

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v4 2/4] avcodec/jpegxl_parser: add JPEG XL parser

2023-06-27 Thread James Almer

On 6/26/2023 12:49 PM, Leo Izen wrote:

+/*
+ * copies as much of the codestream into the buffer as possible
+ * pass a shorter buflen to request less
+ * returns the number of bytes consumed from input, may be greater than 
input_len
+ * if the input doesn't end on an ISOBMFF-box boundary
+ */
+int ff_jpegxl_collect_codestream_header(const uint8_t *input_buffer, int 
input_len,
+uint8_t *buffer, int buflen, int 
*copied)
+{
+GetByteContext gb;
+int pos = 0;
+bytestream2_init(, input_buffer, input_len);
+
+while (1) {
+uint64_t size;
+uint32_t tag;
+int head_size = 8;
+
+if (bytestream2_get_bytes_left() < 16)
+break;
+
+size = bytestream2_get_be32();
+if (size == 1) {
+size = bytestream2_get_be64();
+head_size = 16;
+}
+/* invalid ISOBMFF size */
+if (size && size <= head_size)
+return AVERROR_INVALIDDATA;


ISOBMFF? Are you propagating container bytes as if they were part of the 
bitstream from image2? Why is it not being handled by the mov/mp4 
demuxer instead?



+if (ctx->meta.csp == JPEGXL_CS_GRAY) {
+if (ctx->meta.bit_depth <= 8)
+avctx->pix_fmt = s->format = ctx->meta.have_alpha ? AV_PIX_FMT_YA8 
: AV_PIX_FMT_GRAY8;
+else if (ctx->meta.bit_depth <= 16)
+avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_YA16 : AV_PIX_FMT_GRAY16;
+else
+avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_NONE : AV_PIX_FMT_GRAYF32;
+} else {
+if (ctx->meta.bit_depth <= 8)
+avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBA : AV_PIX_FMT_RGB24;
+else if (ctx->meta.bit_depth <= 16)
+avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBA64 : AV_PIX_FMT_RGB48;
+else
+avctx->pix_fmt = s->format = ctx->meta.have_alpha ? 
AV_PIX_FMT_RGBAF32 : AV_PIX_FMT_RGBF32;


Again, don't fill avctx->pix_fmt, fill s->format.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v4 2/4] avcodec/jpegxl_parser: add JPEG XL parser

2023-06-26 Thread Leo Izen
Add a parser to libavcodec for AV_CODEC_ID_JPEGXL. It doesn't find the
end of the stream in order to packetize the codec, but it does look at
the headers to set preliminary information like dimensions and pixel
format.

Note that much of this code is duplicated from avformat/jpegxl_probe.c,
but that code will be removed and call this instead in the next commit.

Signed-off-by: 
---
 libavcodec/Makefile|   3 +
 libavcodec/jpegxl.h|  72 ++
 libavcodec/jpegxl_parse.c  | 495 +
 libavcodec/jpegxl_parse.h  |  63 +
 libavcodec/jpegxl_parser.c | 182 ++
 libavcodec/parsers.c   |   1 +
 libavcodec/version.h   |   2 +-
 7 files changed, 817 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/jpegxl.h
 create mode 100644 libavcodec/jpegxl_parse.c
 create mode 100644 libavcodec/jpegxl_parse.h
 create mode 100644 libavcodec/jpegxl_parser.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 0e4d27f37b..1a2e68e3e9 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1055,6 +1055,8 @@ STLIBOBJS-$(CONFIG_AVFORMAT)   += to_upper4.o
 STLIBOBJS-$(CONFIG_ISO_MEDIA)  += mpegaudiotabs.o
 STLIBOBJS-$(CONFIG_FLV_MUXER)  += mpeg4audio_sample_rates.o
 STLIBOBJS-$(CONFIG_HLS_DEMUXER)+= ac3_channel_layout_tab.o
+STLIBOBJS-$(CONFIG_IMAGE_JPEGXL_PIPE_DEMUXER) += jpegxl_parse.o
+STLIBOBJS-$(CONFIG_JPEGXL_ANIM_DEMUXER)   += jpegxl_parse.o
 STLIBOBJS-$(CONFIG_MATROSKA_DEMUXER)   += mpeg4audio_sample_rates.o
 STLIBOBJS-$(CONFIG_MOV_DEMUXER)+= ac3_channel_layout_tab.o
 STLIBOBJS-$(CONFIG_MXF_MUXER)  += golomb.o
@@ -1184,6 +1186,7 @@ OBJS-$(CONFIG_HEVC_PARSER) += hevc_parser.o 
hevc_data.o
 OBJS-$(CONFIG_HDR_PARSER)  += hdr_parser.o
 OBJS-$(CONFIG_IPU_PARSER)  += ipu_parser.o
 OBJS-$(CONFIG_JPEG2000_PARSER) += jpeg2000_parser.o
+OBJS-$(CONFIG_JPEGXL_PARSER)   += jpegxl_parser.o jpegxl_parse.o
 OBJS-$(CONFIG_MISC4_PARSER)+= misc4_parser.o
 OBJS-$(CONFIG_MJPEG_PARSER)+= mjpeg_parser.o
 OBJS-$(CONFIG_MLP_PARSER)  += mlp_parse.o mlp_parser.o mlp.o
diff --git a/libavcodec/jpegxl.h b/libavcodec/jpegxl.h
new file mode 100644
index 00..d6faf1a8bd
--- /dev/null
+++ b/libavcodec/jpegxl.h
@@ -0,0 +1,72 @@
+/*
+ * JPEG XL Common Header Definitions
+ * Copyright (c) 2023 Leo Izen 
+ *
+ * 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 AVCODEC_JPEGXL_H
+#define AVCODEC_JPEGXL_H
+
+#define FF_JPEGXL_CODESTREAM_SIGNATURE_LE 0x0aff
+#define FF_JPEGXL_CONTAINER_SIGNATURE_LE 0x204c584a0c00
+
+typedef enum FFJXLExtraChannelType {
+JPEGXL_CT_ALPHA = 0,
+JPEGXL_CT_DEPTH,
+JPEGXL_CT_SPOT_COLOR,
+JPEGXL_CT_SELECTION_MASK,
+JPEGXL_CT_BLACK,
+JPEGXL_CT_CFA,
+JPEGXL_CT_THERMAL,
+JPEGXL_CT_NON_OPTIONAL = 15,
+JPEGXL_CT_OPTIONAL
+} FFJXLExtraChannelType;
+
+typedef enum FFJXLColorSpace {
+JPEGXL_CS_RGB = 0,
+JPEGXL_CS_GRAY,
+JPEGXL_CS_XYB,
+JPEGXL_CS_UNKNOWN
+} FFJXLColorSpace;
+
+typedef enum FFJXLWhitePoint {
+JPEGXL_WP_D65 = 1,
+JPEGXL_WP_CUSTOM,
+JPEGXL_WP_E = 10,
+JPEGXL_WP_DCI = 11
+} FFJXLWhitePoint;
+
+typedef enum FFJXLPrimaries {
+JPEGXL_PR_SRGB = 1,
+JPEGXL_PR_CUSTOM,
+JPEGXL_PR_2100 = 9,
+JPEGXL_PR_P3 = 11,
+} FFJXLPrimaries;
+
+typedef enum FFJXLTransferCharacteristic {
+JPEGXL_TR_BT709 = 1,
+JPEGXL_TR_UNKNOWN,
+JPEGXL_TR_LINEAR = 8,
+JPEGXL_TR_SRGB = 13,
+JPEGXL_TR_PQ = 16,
+JPEGXL_TR_DCI,
+JPEGXL_TR_HLG,
+JPEGXL_TR_GAMMA = 1 << 24,
+} FFJXLTransferCharacteristic;
+
+#endif /* AVCODEC_JPEGXL_H */
diff --git a/libavcodec/jpegxl_parse.c b/libavcodec/jpegxl_parse.c
new file mode 100644
index 00..023e6adf07
--- /dev/null
+++ b/libavcodec/jpegxl_parse.c
@@ -0,0 +1,495 @@
+/*
+ * JPEG XL Header Parser
+ * Copyright (c) 2023 Leo Izen 
+ *
+ * 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