* [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support
@ 2023-03-03 20:31 Leo Izen
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support Leo Izen
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Leo Izen @ 2023-03-03 20:31 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Leo Izen
This patch adds support for animated JPEG XL files in both the libjxl
decoder wrapper and a separate demuxer to properly set the timebase for it.
Leo Izen (2):
avcodec/libjxldec: add animated decode support
avformat/jpegxl_anim_dec: add animated JPEG XL demuxer
MAINTAINERS | 1 +
libavcodec/libjxldec.c | 103 +++++++++----
libavcodec/version.h | 2 +-
libavformat/Makefile | 1 +
libavformat/allformats.c | 1 +
libavformat/img2dec.c | 2 +-
libavformat/jpegxl_anim_dec.c | 266 ++++++++++++++++++++++++++++++++++
libavformat/jpegxl_probe.c | 19 +--
libavformat/jpegxl_probe.h | 7 +-
libavformat/version.h | 2 +-
10 files changed, 365 insertions(+), 39 deletions(-)
create mode 100644 libavformat/jpegxl_anim_dec.c
--
2.39.2
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support
2023-03-03 20:31 [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
@ 2023-03-03 20:31 ` Leo Izen
2023-03-17 18:59 ` Anton Khirnov
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 2/2] avformat/jpegxl_anim_dec: add animated JPEG XL demuxer Leo Izen
2023-03-10 20:28 ` [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
2 siblings, 1 reply; 5+ messages in thread
From: Leo Izen @ 2023-03-03 20:31 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Leo Izen
Migrate the libjxl decoder wrapper from the decode_frame method to the
receive_frame method, which allows sending more than one frame from a
single packet. This allows the libjxl decoder to decode JPEG XL files
that are animated, and emit every frame of the animation. Now, clients
that feed the libjxl decoder with an animated JPEG XL file will be able
to receieve the full animation.
Signed-off-by: Leo Izen <leo.izen@gmail.com>
---
libavcodec/libjxldec.c | 103 ++++++++++++++++++++++++++++++-----------
libavcodec/version.h | 2 +-
2 files changed, 78 insertions(+), 27 deletions(-)
diff --git a/libavcodec/libjxldec.c b/libavcodec/libjxldec.c
index 045a1535f9..394fd8698a 100644
--- a/libavcodec/libjxldec.c
+++ b/libavcodec/libjxldec.c
@@ -52,13 +52,20 @@ typedef struct LibJxlDecodeContext {
#endif
JxlDecoderStatus events;
AVBufferRef *iccp;
+ AVPacket *avpkt;
+ size_t remaining;
+ int64_t pts;
+ int64_t frame_duration;
+ int prev_is_last;
+ AVRational timebase;
} LibJxlDecodeContext;
static int libjxl_init_jxl_decoder(AVCodecContext *avctx)
{
LibJxlDecodeContext *ctx = avctx->priv_data;
- ctx->events = JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE | JXL_DEC_COLOR_ENCODING;
+ ctx->events = JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE
+ | JXL_DEC_COLOR_ENCODING | JXL_DEC_FRAME;
if (JxlDecoderSubscribeEvents(ctx->decoder, ctx->events) != JXL_DEC_SUCCESS) {
av_log(avctx, AV_LOG_ERROR, "Error subscribing to JXL events\n");
return AVERROR_EXTERNAL;
@@ -71,6 +78,8 @@ static int libjxl_init_jxl_decoder(AVCodecContext *avctx)
memset(&ctx->basic_info, 0, sizeof(JxlBasicInfo));
memset(&ctx->jxl_pixfmt, 0, sizeof(JxlPixelFormat));
+ ctx->prev_is_last = 1;
+ ctx->frame_duration = 1;
return 0;
}
@@ -93,6 +102,11 @@ static av_cold int libjxl_decode_init(AVCodecContext *avctx)
return AVERROR_EXTERNAL;
}
+ ctx->avpkt = av_packet_alloc();
+ if (!ctx->avpkt)
+ return AVERROR(ENOMEM);
+ ctx->pts = 0;
+
return libjxl_init_jxl_decoder(avctx);
}
@@ -328,18 +342,33 @@ static int libjxl_color_encoding_event(AVCodecContext *avctx, AVFrame *frame)
return 0;
}
-static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
+static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame)
{
LibJxlDecodeContext *ctx = avctx->priv_data;
- const uint8_t *buf = avpkt->data;
- size_t remaining = avpkt->size;
JxlDecoderStatus jret;
int ret;
- *got_frame = 0;
+ AVPacket *pkt = ctx->avpkt;
+
+ if (!pkt->size) {
+ av_packet_unref(pkt);
+ ret = ff_decode_get_packet(avctx, pkt);
+ if (ret < 0 && ret != AVERROR_EOF)
+ return ret;
+ ctx->remaining = pkt->size;
+ if (!pkt->size) {
+ /* empty packet means eof */
+ if (ret >= 0) {
+ av_packet_unref(pkt);
+ return AVERROR(EAGAIN);
+ } else {
+ return AVERROR_EOF;
+ }
+ }
+ }
while (1) {
- jret = JxlDecoderSetInput(ctx->decoder, buf, remaining);
+ jret = JxlDecoderSetInput(ctx->decoder, pkt->data + (pkt->size - ctx->remaining), ctx->remaining);
if (jret == JXL_DEC_ERROR) {
/* this should never happen here unless there's a bug in libjxl */
@@ -353,19 +382,18 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
* of bytes remaining to be read, rather than
* the number of bytes that it did read
*/
- remaining = JxlDecoderReleaseInput(ctx->decoder);
- buf = avpkt->data + avpkt->size - remaining;
+ ctx->remaining = JxlDecoderReleaseInput(ctx->decoder);
switch(jret) {
case JXL_DEC_ERROR:
av_log(avctx, AV_LOG_ERROR, "Unknown libjxl decode error\n");
return AVERROR_INVALIDDATA;
case JXL_DEC_NEED_MORE_INPUT:
- if (remaining == 0) {
+ av_log(avctx, AV_LOG_DEBUG, "NEED_MORE_INPUT event emitted\n");
+ if (ctx->remaining == 0) {
av_log(avctx, AV_LOG_ERROR, "Unexpected end of JXL codestream\n");
return AVERROR_INVALIDDATA;
}
- av_log(avctx, AV_LOG_DEBUG, "NEED_MORE_INPUT event emitted\n");
continue;
case JXL_DEC_BASIC_INFO:
av_log(avctx, AV_LOG_DEBUG, "BASIC_INFO event emitted\n");
@@ -384,6 +412,13 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
}
if ((ret = ff_set_dimensions(avctx, ctx->basic_info.xsize, ctx->basic_info.ysize)) < 0)
return ret;
+ if (ctx->basic_info.have_animation) {
+ ctx->timebase = av_make_q(
+ ctx->basic_info.animation.tps_denominator,
+ ctx->basic_info.animation.tps_numerator);
+ } else {
+ ctx->timebase = avctx->pkt_timebase;
+ }
continue;
case JXL_DEC_COLOR_ENCODING:
av_log(avctx, AV_LOG_DEBUG, "COLOR_ENCODING event emitted\n");
@@ -407,11 +442,28 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
}
#endif
continue;
+ case JXL_DEC_FRAME:
+ av_log(avctx, AV_LOG_DEBUG, "FRAME event emitted\n");
+ if (!ctx->basic_info.have_animation || ctx->prev_is_last) {
+ frame->pict_type = AV_PICTURE_TYPE_I;
+ frame->key_frame = 1;
+ }
+ if (ctx->basic_info.have_animation) {
+ JxlFrameHeader header;
+ if (JxlDecoderGetFrameHeader(ctx->decoder, &header) != JXL_DEC_SUCCESS) {
+ av_log(avctx, AV_LOG_ERROR, "Bad libjxl dec frame event\n");
+ return AVERROR_EXTERNAL;
+ }
+ ctx->prev_is_last = header.is_last;
+ ctx->frame_duration = header.duration;
+ } else {
+ ctx->prev_is_last = 1;
+ ctx->frame_duration = 1;
+ }
+ continue;
case JXL_DEC_FULL_IMAGE:
/* full image is one frame, even if animated */
av_log(avctx, AV_LOG_DEBUG, "FULL_IMAGE event emitted\n");
- frame->pict_type = AV_PICTURE_TYPE_I;
- frame->key_frame = 1;
if (ctx->iccp) {
AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_ICC_PROFILE, ctx->iccp);
if (!sd)
@@ -419,25 +471,23 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
/* ownership is transfered, and it is not ref-ed */
ctx->iccp = NULL;
}
- *got_frame = 1;
- return avpkt->size - remaining;
+ frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase);
+ ctx->pts += ctx->frame_duration;
+ return 0;
case JXL_DEC_SUCCESS:
av_log(avctx, AV_LOG_DEBUG, "SUCCESS event emitted\n");
/*
- * The SUCCESS event isn't fired until after JXL_DEC_FULL_IMAGE. If this
- * stream only contains one JXL image then JXL_DEC_SUCCESS will never fire.
- * If the image2 sequence being decoded contains several JXL files, then
- * libjxl will fire this event after the next AVPacket has been passed,
- * which means the current packet is actually the next image in the sequence.
- * This is why we reset the decoder and populate the packet data now, since
- * this is the next packet and it has not been decoded yet. The decoder does
- * have to be reset to allow us to use it for the next image, or libjxl
- * will become very confused if the header information is not identical.
+ * this event will be fired when the zero-length EOF
+ * packet is sent to the decoder by the client,
+ * but it will also be fired when the next image of
+ * an image2pipe sequence is loaded up
*/
JxlDecoderReset(ctx->decoder);
libjxl_init_jxl_decoder(avctx);
- buf = avpkt->data;
- remaining = avpkt->size;
+ if (!ctx->remaining) {
+ av_packet_unref(pkt);
+ return AVERROR_EOF;
+ }
continue;
default:
av_log(avctx, AV_LOG_ERROR, "Bad libjxl event: %d\n", jret);
@@ -457,6 +507,7 @@ static av_cold int libjxl_decode_close(AVCodecContext *avctx)
JxlDecoderDestroy(ctx->decoder);
ctx->decoder = NULL;
av_buffer_unref(&ctx->iccp);
+ av_packet_free(&ctx->avpkt);
return 0;
}
@@ -468,7 +519,7 @@ const FFCodec ff_libjxl_decoder = {
.p.id = AV_CODEC_ID_JPEGXL,
.priv_data_size = sizeof(LibJxlDecodeContext),
.init = libjxl_decode_init,
- FF_CODEC_DECODE_CB(libjxl_decode_frame),
+ FF_CODEC_RECEIVE_FRAME_CB(libjxl_receive_frame),
.close = libjxl_decode_close,
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_OTHER_THREADS,
.caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
diff --git a/libavcodec/version.h b/libavcodec/version.h
index da54f87887..39dbec0208 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
#include "version_major.h"
#define LIBAVCODEC_VERSION_MINOR 6
-#define LIBAVCODEC_VERSION_MICRO 100
+#define LIBAVCODEC_VERSION_MICRO 101
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
--
2.39.2
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* [FFmpeg-devel] [PATCH 2/2] avformat/jpegxl_anim_dec: add animated JPEG XL demuxer
2023-03-03 20:31 [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support Leo Izen
@ 2023-03-03 20:31 ` Leo Izen
2023-03-10 20:28 ` [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
2 siblings, 0 replies; 5+ messages in thread
From: Leo Izen @ 2023-03-03 20:31 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: Leo Izen
Animated JPEG XL files requires a separate demuxer than image2, because
the timebase information is set by the demuxer. Should the timebase of
an animated JPEG XL file be incompatible with the timebase set by the
image2pipe demuxer (usually 1/25 unless set otherwise), rescaling will
fail. Adding a separate demuxer for animated JPEG XL files allows the
timebase to be set correctly.
Signed-off-by: Leo Izen <leo.izen@gmail.com>
---
MAINTAINERS | 1 +
libavformat/Makefile | 1 +
libavformat/allformats.c | 1 +
libavformat/img2dec.c | 2 +-
libavformat/jpegxl_anim_dec.c | 266 ++++++++++++++++++++++++++++++++++
libavformat/jpegxl_probe.c | 19 +--
libavformat/jpegxl_probe.h | 7 +-
libavformat/version.h | 2 +-
8 files changed, 287 insertions(+), 12 deletions(-)
create mode 100644 libavformat/jpegxl_anim_dec.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 854ccc3fa4..d57e4a8ed3 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -432,6 +432,7 @@ Muxers/Demuxers:
ipmovie.c Mike Melanson
ircam* Paul B Mahol
iss.c Stefan Gehrer
+ jpegxl_anim_dec.c Leo Izen
jpegxl_probe.* Leo Izen
jvdec.c Peter Ross
kvag.c Zane van Iperen
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 47bbbbfb2a..53fbeae5a0 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -316,6 +316,7 @@ OBJS-$(CONFIG_IVF_MUXER) += ivfenc.o
OBJS-$(CONFIG_IVR_DEMUXER) += rmdec.o rm.o rmsipr.o
OBJS-$(CONFIG_JACOSUB_DEMUXER) += jacosubdec.o subtitles.o
OBJS-$(CONFIG_JACOSUB_MUXER) += jacosubenc.o rawenc.o
+OBJS-$(CONFIG_JPEGXL_ANIM_DEMUXER) += jpegxl_anim_dec.o jpegxl_probe.o
OBJS-$(CONFIG_JV_DEMUXER) += jvdec.o
OBJS-$(CONFIG_KUX_DEMUXER) += flvdec.o
OBJS-$(CONFIG_KVAG_DEMUXER) += kvag.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index cb5b69e9cd..a48c4bab61 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -238,6 +238,7 @@ extern const AVInputFormat ff_ivr_demuxer;
extern const AVInputFormat ff_jacosub_demuxer;
extern const FFOutputFormat ff_jacosub_muxer;
extern const AVInputFormat ff_jv_demuxer;
+extern const AVInputFormat ff_jpegxl_anim_demuxer;
extern const AVInputFormat ff_kux_demuxer;
extern const AVInputFormat ff_kvag_demuxer;
extern const FFOutputFormat ff_kvag_muxer;
diff --git a/libavformat/img2dec.c b/libavformat/img2dec.c
index 5a63d7c81d..8d02f4e9ef 100644
--- a/libavformat/img2dec.c
+++ b/libavformat/img2dec.c
@@ -850,7 +850,7 @@ static int jpegxl_probe(const AVProbeData *p)
if (AV_RL16(b) != FF_JPEGXL_CODESTREAM_SIGNATURE_LE)
return 0;
#if CONFIG_IMAGE_JPEGXL_PIPE_DEMUXER
- if (ff_jpegxl_verify_codestream_header(p->buf, p->buf_size) >= 0)
+ if (ff_jpegxl_verify_codestream_header(p->buf, p->buf_size, 1) >= 0)
return AVPROBE_SCORE_MAX - 2;
#endif
return 0;
diff --git a/libavformat/jpegxl_anim_dec.c b/libavformat/jpegxl_anim_dec.c
new file mode 100644
index 0000000000..6ea6c46d8f
--- /dev/null
+++ b/libavformat/jpegxl_anim_dec.c
@@ -0,0 +1,266 @@
+/*
+ * Animated JPEG XL Demuxer
+ * Copyright (c) 2023 Leo Izen (thebombzen)
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * Animated JPEG XL Demuxer
+ * @see ISO/IEC 18181-1 and 18181-2
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#define BITSTREAM_READER_LE
+#include "libavcodec/get_bits.h"
+
+#include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+
+#include "avformat.h"
+#include "internal.h"
+#include "jpegxl_probe.h"
+
+typedef struct JXLAnimDemuxContext {
+ AVBufferRef *initial;
+} JXLAnimDemuxContext;
+
+/*
+ * 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
+ */
+static int jpegxl_collect_codestream_header(const uint8_t *input_buffer, int input_len, uint8_t *buffer, int buflen, int *copied) {
+ const uint8_t *b = input_buffer;
+ *copied = 0;
+
+ while (1) {
+ uint64_t size;
+ uint32_t tag;
+ int head_size = 8;
+
+ if (b - input_buffer >= input_len - 16)
+ break;
+
+ size = AV_RB32(b);
+ b += 4;
+ if (size == 1) {
+ size = AV_RB64(b);
+ b += 8;
+ head_size = 16;
+ }
+ /* invalid ISOBMFF size */
+ if (size > 0 && size <= head_size)
+ return AVERROR_INVALIDDATA;
+ if (size > 0)
+ size -= head_size;
+
+ tag = AV_RL32(b);
+ b += 4;
+ if (tag == MKTAG('j', 'x', 'l', 'p')) {
+ b += 4;
+ size -= 4;
+ }
+
+ if (tag == MKTAG('j', 'x', 'l', 'c') || tag == MKTAG('j', 'x', 'l', 'p')) {
+ /*
+ * size = 0 means "until EOF". this is legal but uncommon
+ * here we just set it to the remaining size of the probe buffer
+ * which at this point should always be nonnegative
+ */
+ if (size == 0 || size > input_len - (b - input_buffer))
+ size = input_len - (b - input_buffer);
+
+ if (size > buflen - *copied)
+ size = buflen - *copied;
+ /*
+ * arbitrary chunking of the payload makes this memcpy hard to avoid
+ * in practice this will only be performed one or two times at most
+ */
+ memcpy(buffer + *copied, b, size);
+ *copied += size;
+ }
+ b += size;
+ if (b >= input_buffer + input_len || *copied >= buflen)
+ break;
+ }
+
+ return b - input_buffer;
+}
+
+static int jpegxl_anim_probe(const AVProbeData *p)
+{
+ uint8_t buffer[4096];
+ int copied;
+
+ /* this is a raw codestream */
+ if (AV_RL16(p->buf) == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
+ if (ff_jpegxl_verify_codestream_header(p->buf, p->buf_size, 1) >= 1)
+ return AVPROBE_SCORE_MAX;
+
+ return 0;
+ }
+
+ /* not a JPEG XL file at all */
+ if (AV_RL64(p->buf) != FF_JPEGXL_CONTAINER_SIGNATURE_LE)
+ return 0;
+
+ if (jpegxl_collect_codestream_header(p->buf, p->buf_size, buffer, sizeof(buffer), &copied) <= 0 || copied <= 0)
+ return 0;
+
+ if (ff_jpegxl_verify_codestream_header(buffer, copied, 0) >= 1)
+ return AVPROBE_SCORE_MAX;
+
+ return 0;
+}
+
+static int jpegxl_anim_read_header(AVFormatContext *s)
+{
+ JXLAnimDemuxContext *ctx = s->priv_data;
+ AVIOContext *pb = s->pb;
+ AVStream *st;
+ int offset = 0;
+ uint8_t head[256];
+ int headsize = 0;
+ int ctrl;
+ AVRational tb;
+ GetBitContext gbi, *gb = &gbi;
+
+ uint64_t sig16 = avio_rl16(pb);
+ if (sig16 == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
+ AV_WL16(head, sig16);
+ headsize = avio_read(s->pb, head + 2, sizeof(head) - 2);
+ if (headsize < 0)
+ return headsize;
+ headsize += 2;
+ ctx->initial = av_buffer_alloc(headsize);
+ if (!ctx->initial)
+ return AVERROR(ENOMEM);
+ memcpy(ctx->initial->data, head, headsize);
+ } else {
+ uint64_t sig64 = avio_rl64(pb);
+ sig64 = (sig64 << 16) | sig16;
+ if (sig64 != FF_JPEGXL_CONTAINER_SIGNATURE_LE)
+ return AVERROR_INVALIDDATA;
+ avio_skip(pb, 2); // first box always 12 bytes
+ while (1) {
+ int copied;
+ uint8_t buf[4096];
+ int read = avio_read(pb, buf, sizeof(buf));
+ if (read < 0)
+ return read;
+ if (!ctx->initial) {
+ ctx->initial = av_buffer_alloc(read + 12);
+ if (!ctx->initial)
+ return AVERROR(ENOMEM);
+ AV_WL64(ctx->initial->data, FF_JPEGXL_CONTAINER_SIGNATURE_LE);
+ AV_WL32(ctx->initial->data + 8, 0x0a870a0d);
+ } else {
+ /* this only should be happening zero or one times in practice */
+ if (av_buffer_realloc(&ctx->initial, ctx->initial->size + read) < 0)
+ return AVERROR(ENOMEM);
+ }
+ jpegxl_collect_codestream_header(buf, read, head + headsize, sizeof(head) - headsize, &copied);
+ memcpy(ctx->initial->data + (ctx->initial->size - read), buf, read);
+ headsize += copied;
+ if (headsize >= sizeof(head) || read < sizeof(buf))
+ break;
+ }
+ }
+ /* offset in bits of the animation header */
+ offset = ff_jpegxl_verify_codestream_header(head, headsize, 0);
+ if (offset <= 0)
+ return AVERROR_INVALIDDATA;
+ if (init_get_bits8(gb, head, headsize) < 0)
+ return AVERROR_INVALIDDATA;
+ skip_bits_long(gb, offset);
+
+ st = avformat_new_stream(s, NULL);
+ if (!st)
+ return AVERROR(ENOMEM);
+
+ st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
+ st->codecpar->codec_id = AV_CODEC_ID_JPEGXL;
+ ctrl = get_bits(gb, 2);
+ tb.den = (const uint32_t[]){100, 1000, 1, 1}[ctrl] + get_bits_long(gb, (const uint32_t[]){0, 0, 10, 30}[ctrl]);
+ ctrl = get_bits(gb, 2);
+ tb.num = (const uint32_t[]){1, 1001, 1, 1}[ctrl] + get_bits_long(gb, (const uint32_t[]){0, 0, 8, 10}[ctrl]);
+ avpriv_set_pts_info(st, 1, tb.num, tb.den);
+
+ return 0;
+}
+
+/* the decoder requires the full input file as a single packet */
+static int jpegxl_anim_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ JXLAnimDemuxContext *ctx = s->priv_data;
+ AVIOContext *pb = s->pb;
+ int ret;
+ int64_t size;
+ size_t offset = 0;
+
+ if ((size = avio_size(pb)) < 0)
+ return size;
+
+ /* animated JXL this big should not exist */
+ if (size > INT_MAX)
+ return AVERROR_INVALIDDATA;
+
+ if (ctx->initial && size < ctx->initial->size)
+ size = ctx->initial->size;
+
+ if ((ret = av_new_packet(pkt, size) < 0))
+ return ret;
+
+ if (ctx->initial) {
+ offset = ctx->initial->size;
+ memcpy(pkt->data, ctx->initial->data, offset);
+ av_buffer_unref(&ctx->initial);
+ }
+
+ if ((ret = avio_read(pb, pkt->data + offset, size - offset)) < 0)
+ return ret;
+
+ return 0;
+}
+
+static int jpegxl_anim_close(AVFormatContext *s)
+{
+ JXLAnimDemuxContext *ctx = s->priv_data;
+ if (ctx->initial)
+ av_buffer_unref(&ctx->initial);
+
+ return 0;
+}
+
+const AVInputFormat ff_jpegxl_anim_demuxer = {
+ .name = "jpegxl_anim",
+ .long_name = NULL_IF_CONFIG_SMALL("Animated JPEG XL"),
+ .priv_data_size = sizeof(JXLAnimDemuxContext),
+ .read_probe = jpegxl_anim_probe,
+ .read_header = jpegxl_anim_read_header,
+ .read_packet = jpegxl_anim_read_packet,
+ .read_close = jpegxl_anim_close,
+ .flags_internal = FF_FMT_INIT_CLEANUP,
+ .flags = AVFMT_GENERIC_INDEX,
+ .mime_type = "image/jxl",
+ .extensions = "jxl",
+};
diff --git a/libavformat/jpegxl_probe.c b/libavformat/jpegxl_probe.c
index 3de002f004..a3845b037d 100644
--- a/libavformat/jpegxl_probe.c
+++ b/libavformat/jpegxl_probe.c
@@ -208,7 +208,7 @@ static void jpegxl_skip_bit_depth(GetBitContext *gb)
* validate a Jpeg XL Extra Channel Info bundle
* @return >= 0 upon valid, < 0 upon invalid
*/
-static int jpegxl_read_extra_channel_info(GetBitContext *gb)
+static int jpegxl_read_extra_channel_info(GetBitContext *gb, int validate_level)
{
int all_default = jxl_bits(1);
uint32_t type, name_len = 0;
@@ -217,7 +217,7 @@ static int jpegxl_read_extra_channel_info(GetBitContext *gb)
type = jxl_enum();
if (type > 63)
return -1; /* enum types cannot be 64+ */
- if (type == FF_JPEGXL_CT_BLACK)
+ if (type == FF_JPEGXL_CT_BLACK && validate_level)
return -1;
jpegxl_skip_bit_depth(gb);
jxl_u32(0, 3, 4, 1, 0, 0, 0, 3); /* dim-shift */
@@ -242,12 +242,12 @@ static int jpegxl_read_extra_channel_info(GetBitContext *gb)
return 0;
}
-/* verify that a codestream header is valid */
-int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen)
+int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen, int validate_level)
{
GetBitContext gbi, *gb = &gbi;
int all_default, extra_fields = 0;
int xyb_encoded = 1, have_icc_profile = 0;
+ int animation_offset = 0;
uint32_t num_extra_channels;
uint64_t extensions;
int ret;
@@ -259,7 +259,7 @@ int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen)
if (jxl_bits(16) != FF_JPEGXL_CODESTREAM_SIGNATURE_LE)
return -1;
- if (jpegxl_read_size_header(gb) < 0)
+ if (jpegxl_read_size_header(gb) < 0 && validate_level)
return -1;
all_default = jxl_bits(1);
@@ -285,6 +285,7 @@ int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen)
/* animation header */
if (jxl_bits(1)) {
+ animation_offset = get_bits_count(gb);
jxl_u32(100, 1000, 1, 1, 0, 0, 10, 30);
jxl_u32(1, 1001, 1, 1, 0, 0, 8, 10);
jxl_u32(0, 0, 0, 0, 0, 3, 16, 32);
@@ -296,14 +297,14 @@ int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen)
jpegxl_skip_bit_depth(gb);
/* modular_16bit_buffers must equal 1 */
- if (!jxl_bits(1))
+ if (!jxl_bits(1) && validate_level)
return -1;
num_extra_channels = jxl_u32(0, 1, 2, 1, 0, 0, 4, 12);
- if (num_extra_channels > 4)
+ if (num_extra_channels > 4 && validate_level)
return -1;
for (uint32_t i = 0; i < num_extra_channels; i++) {
- if (jpegxl_read_extra_channel_info(gb) < 0)
+ if (jpegxl_read_extra_channel_info(gb, validate_level) < 0)
return -1;
}
@@ -392,5 +393,5 @@ int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen)
if (get_bits_left(gb) < 0)
return -1;
- return 0;
+ return animation_offset;
}
diff --git a/libavformat/jpegxl_probe.h b/libavformat/jpegxl_probe.h
index 2960e81e11..496445fbce 100644
--- a/libavformat/jpegxl_probe.h
+++ b/libavformat/jpegxl_probe.h
@@ -27,6 +27,11 @@
#define FF_JPEGXL_CODESTREAM_SIGNATURE_LE 0x0aff
#define FF_JPEGXL_CONTAINER_SIGNATURE_LE 0x204c584a0c000000
-int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen);
+/**
+ * @brief verify that a codestream header is valid
+ * @return Negative upon error, 0 upon verifying that the codestream is not animated,
+ * and 1 upon verifying that it is animated
+ */
+int ff_jpegxl_verify_codestream_header(const uint8_t *buf, int buflen, int validate_level);
#endif /* AVFORMAT_JPEGXL_PROBE_H */
diff --git a/libavformat/version.h b/libavformat/version.h
index af7d0a1024..e2634b85ae 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,7 +31,7 @@
#include "version_major.h"
-#define LIBAVFORMAT_VERSION_MINOR 4
+#define LIBAVFORMAT_VERSION_MINOR 5
#define LIBAVFORMAT_VERSION_MICRO 100
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
--
2.39.2
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support
2023-03-03 20:31 [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support Leo Izen
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 2/2] avformat/jpegxl_anim_dec: add animated JPEG XL demuxer Leo Izen
@ 2023-03-10 20:28 ` Leo Izen
2 siblings, 0 replies; 5+ messages in thread
From: Leo Izen @ 2023-03-10 20:28 UTC (permalink / raw)
To: FFmpeg Development
On 3/3/23 15:31, Leo Izen wrote:
> This patch adds support for animated JPEG XL files in both the libjxl
> decoder wrapper and a separate demuxer to properly set the timebase for it.
>
> Leo Izen (2):
> avcodec/libjxldec: add animated decode support
> avformat/jpegxl_anim_dec: add animated JPEG XL demuxer
>
> MAINTAINERS | 1 +
> libavcodec/libjxldec.c | 103 +++++++++----
> libavcodec/version.h | 2 +-
> libavformat/Makefile | 1 +
> libavformat/allformats.c | 1 +
> libavformat/img2dec.c | 2 +-
> libavformat/jpegxl_anim_dec.c | 266 ++++++++++++++++++++++++++++++++++
> libavformat/jpegxl_probe.c | 19 +--
> libavformat/jpegxl_probe.h | 7 +-
> libavformat/version.h | 2 +-
> 10 files changed, 365 insertions(+), 39 deletions(-)
> create mode 100644 libavformat/jpegxl_anim_dec.c
>
Bumping for review, thanks.
- Leo Izen (thebombzen)
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support Leo Izen
@ 2023-03-17 18:59 ` Anton Khirnov
0 siblings, 0 replies; 5+ messages in thread
From: Anton Khirnov @ 2023-03-17 18:59 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Leo Izen
Quoting Leo Izen (2023-03-03 21:31:45)
> -static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
> +static int libjxl_receive_frame(AVCodecContext *avctx, AVFrame *frame)
> {
> LibJxlDecodeContext *ctx = avctx->priv_data;
> - const uint8_t *buf = avpkt->data;
> - size_t remaining = avpkt->size;
> JxlDecoderStatus jret;
> int ret;
> - *got_frame = 0;
> + AVPacket *pkt = ctx->avpkt;
> +
> + if (!pkt->size) {
Shouldn't this live inside the loop?
It won't happen currently, but in principle ff_decode_get_packet() could
return more than one packet.
> + av_packet_unref(pkt);
> + ret = ff_decode_get_packet(avctx, pkt);
> + if (ret < 0 && ret != AVERROR_EOF)
> + return ret;
> + ctx->remaining = pkt->size;
> + if (!pkt->size) {
> + /* empty packet means eof */
> + if (ret >= 0) {
> + av_packet_unref(pkt);
> + return AVERROR(EAGAIN);
pkt->size == 0 && ret >= 0
should not happen
> + } else {
> + return AVERROR_EOF;
> + }
> + }
> + }
>
> while (1) {
>
> - jret = JxlDecoderSetInput(ctx->decoder, buf, remaining);
> + jret = JxlDecoderSetInput(ctx->decoder, pkt->data + (pkt->size - ctx->remaining), ctx->remaining);
Wouldn't it be simpler to offset pkt->data and decremented pkt->size?
Then you shouldn't need ctx->remaining at all.
> @@ -419,25 +471,23 @@ static int libjxl_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_f
> /* ownership is transfered, and it is not ref-ed */
> ctx->iccp = NULL;
> }
> - *got_frame = 1;
> - return avpkt->size - remaining;
> + frame->pts = av_rescale_q(ctx->pts, ctx->timebase, avctx->pkt_timebase);
I think pkt_timebase does not have to be set.
Also, you should set frame->duration.
--
Anton Khirnov
_______________________________________________
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".
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-03-17 18:59 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-03 20:31 [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 1/2] avcodec/libjxldec: add animated decode support Leo Izen
2023-03-17 18:59 ` Anton Khirnov
2023-03-03 20:31 ` [FFmpeg-devel] [PATCH 2/2] avformat/jpegxl_anim_dec: add animated JPEG XL demuxer Leo Izen
2023-03-10 20:28 ` [FFmpeg-devel] [PATCH 0/2] JPEG XL Animation Support Leo Izen
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