FFmpeg
jpegxl_anim_dec.c
Go to the documentation of this file.
1 /*
2  * Animated JPEG XL Demuxer
3  * Copyright (c) 2023 Leo Izen (thebombzen)
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Animated JPEG XL Demuxer
25  * @see ISO/IEC 18181-1 and 18181-2
26  */
27 
28 #include <stdint.h>
29 #include <string.h>
30 
31 #include "libavcodec/jpegxl.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/opt.h"
35 
36 #include "avformat.h"
37 #include "internal.h"
38 
39 typedef struct JXLAnimDemuxContext {
42 
43 static int jpegxl_anim_probe(const AVProbeData *p)
44 {
45  uint8_t buffer[4096 + AV_INPUT_BUFFER_PADDING_SIZE];
46  int copied = 0, ret;
47  FFJXLMetadata meta = { 0 };
48 
49  /* this is a raw codestream */
52  if (ret >= 0 && meta.animation_offset > 0)
53  return AVPROBE_SCORE_MAX;
54 
55  return 0;
56  }
57 
58  /* not a JPEG XL file at all */
60  return 0;
61 
63  sizeof(buffer) - AV_INPUT_BUFFER_PADDING_SIZE, &copied) <= 0
64  || copied <= 0)
65  return 0;
66 
67  ret = ff_jpegxl_parse_codestream_header(buffer, copied, &meta, 10);
68  if (ret >= 0 && meta.animation_offset > 0)
69  return AVPROBE_SCORE_MAX;
70 
71  return 0;
72 }
73 
75 {
76  JXLAnimDemuxContext *ctx = s->priv_data;
77  AVIOContext *pb = s->pb;
78  AVStream *st;
79  uint8_t head[256 + AV_INPUT_BUFFER_PADDING_SIZE];
80  const int sizeofhead = sizeof(head) - AV_INPUT_BUFFER_PADDING_SIZE;
81  int headsize = 0, ret;
82  FFJXLMetadata meta = { 0 };
83 
84  uint64_t sig16 = avio_rl16(pb);
85  if (sig16 == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
86  AV_WL16(head, sig16);
87  headsize = avio_read(s->pb, head + 2, sizeofhead - 2);
88  if (headsize < 0)
89  return headsize;
90  headsize += 2;
91  ctx->initial = av_buffer_alloc(headsize);
92  if (!ctx->initial)
93  return AVERROR(ENOMEM);
94  memcpy(ctx->initial->data, head, headsize);
95  } else {
96  uint64_t sig64 = avio_rl64(pb);
97  sig64 = (sig64 << 16) | sig16;
99  return AVERROR_INVALIDDATA;
100  avio_skip(pb, 2); // first box always 12 bytes
101  while (1) {
102  int copied = 0;
103  uint8_t buf[4096];
104  int read = avio_read(pb, buf, sizeof(buf));
105  if (read < 0)
106  return read;
107  if (!ctx->initial) {
108  ctx->initial = av_buffer_alloc(read + 12);
109  if (!ctx->initial)
110  return AVERROR(ENOMEM);
112  AV_WL32(ctx->initial->data + 8, 0x0a870a0d);
113  } else {
114  /* this only should be happening zero or one times in practice */
115  if (av_buffer_realloc(&ctx->initial, ctx->initial->size + read) < 0)
116  return AVERROR(ENOMEM);
117  }
118  ff_jpegxl_collect_codestream_header(buf, read, head + headsize, sizeofhead - headsize, &copied);
119  memcpy(ctx->initial->data + (ctx->initial->size - read), buf, read);
120  headsize += copied;
121  if (headsize >= sizeofhead || read < sizeof(buf))
122  break;
123  }
124  }
125 
126  /* offset in bits of the animation header */
127  ret = ff_jpegxl_parse_codestream_header(head, headsize, &meta, 0);
128  if (ret < 0 || meta.animation_offset <= 0)
129  return AVERROR_INVALIDDATA;
130 
131  st = avformat_new_stream(s, NULL);
132  if (!st)
133  return AVERROR(ENOMEM);
134 
137  avpriv_set_pts_info(st, 1, meta.timebase.num, meta.timebase.den);
139 
140  return 0;
141 }
142 
143 /* the decoder requires the full input file as a single packet */
145 {
146  JXLAnimDemuxContext *ctx = s->priv_data;
147  AVIOContext *pb = s->pb;
148  int ret;
149  int64_t size;
150  size_t offset = 0;
151 
152  size = avio_size(pb);
153  if (size < 0)
154  return size;
155  if (size > INT_MAX)
156  return AVERROR(EDOM);
157  if (size == 0)
158  size = 4096;
159 
160  if (ctx->initial && size < ctx->initial->size)
161  size = ctx->initial->size;
162 
164  if (ret < 0)
165  return ret;
166 
167  if (ctx->initial) {
168  offset = ctx->initial->size;
169  memcpy(pkt->data, ctx->initial->data, offset);
170  av_buffer_unref(&ctx->initial);
171  }
172 
173  ret = avio_read(pb, pkt->data + offset, size - offset);
174  if (ret < 0)
175  return ret;
176  if (ret < size - offset)
177  pkt->size = ret + offset;
178 
179  return 0;
180 }
181 
183 {
184  JXLAnimDemuxContext *ctx = s->priv_data;
185  if (ctx->initial)
186  av_buffer_unref(&ctx->initial);
187 
188  return 0;
189 }
190 
192  .name = "jpegxl_anim",
193  .long_name = NULL_IF_CONFIG_SMALL("Animated JPEG XL"),
194  .priv_data_size = sizeof(JXLAnimDemuxContext),
199  .flags_internal = FF_FMT_INIT_CLEANUP,
201  .mime_type = "image/jxl",
202  .extensions = "jxl",
203 };
JXLAnimDemuxContext
Definition: jpegxl_anim_dec.c:39
FF_FMT_INIT_CLEANUP
#define FF_FMT_INIT_CLEANUP
For an AVInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: internal.h:46
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:424
ff_jpegxl_parse_codestream_header
int ff_jpegxl_parse_codestream_header(const uint8_t *buf, int buflen, FFJXLMetadata *meta, int validate)
Definition: jpegxl_parse.c:255
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AV_RL64
uint64_t_TMPL AV_RL64
Definition: bytestream.h:91
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:480
AVPacket::data
uint8_t * data
Definition: packet.h:491
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:455
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:370
FFJXLMetadata
Definition: jpegxl_parse.h:31
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:464
FF_JPEGXL_CONTAINER_SIGNATURE_LE
#define FF_JPEGXL_CONTAINER_SIGNATURE_LE
Definition: jpegxl.h:26
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:761
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:420
jpegxl_anim_probe
static int jpegxl_anim_probe(const AVProbeData *p)
Definition: jpegxl_anim_dec.c:43
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:481
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:761
AVRational::num
int num
Numerator.
Definition: rational.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AVInputFormat
Definition: avformat.h:549
FFJXLMetadata::timebase
AVRational timebase
Definition: jpegxl_parse.h:43
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:554
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:454
jpegxl_anim_close
static int jpegxl_anim_close(AVFormatContext *s)
Definition: jpegxl_anim_dec.c:182
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
jpegxl.h
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:391
AVFormatContext
Format I/O context.
Definition: avformat.h:1115
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:864
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:452
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVPacket::size
int size
Definition: packet.h:492
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
size
int size
Definition: twinvq_data.h:10344
FFJXLMetadata::animation_offset
int animation_offset
Definition: jpegxl_parse.h:42
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:410
AV_WL64
#define AV_WL64(p, v)
Definition: intreadwrite.h:438
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
jpegxl_anim_read_header
static int jpegxl_anim_read_header(AVFormatContext *s)
Definition: jpegxl_anim_dec.c:74
FF_JPEGXL_CODESTREAM_SIGNATURE_LE
#define FF_JPEGXL_CODESTREAM_SIGNATURE_LE
Definition: jpegxl.h:25
AV_CODEC_ID_JPEGXL
@ AV_CODEC_ID_JPEGXL
Definition: codec_id.h:316
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:841
avformat.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ff_jpegxl_anim_demuxer
const AVInputFormat ff_jpegxl_anim_demuxer
Definition: jpegxl_anim_dec.c:191
JXLAnimDemuxContext::initial
AVBufferRef * initial
Definition: jpegxl_anim_dec.c:40
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVRational::den
int den
Denominator.
Definition: rational.h:60
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:659
jpegxl_anim_read_packet
static int jpegxl_anim_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: jpegxl_anim_dec.c:144
av_buffer_realloc
int av_buffer_realloc(AVBufferRef **pbuf, size_t size)
Reallocate a given buffer.
Definition: buffer.c:183
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:365
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:29
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:468
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:785
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:691
jpegxl_parse.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_jpegxl_collect_codestream_header
int ff_jpegxl_collect_codestream_header(const uint8_t *input_buffer, int input_len, uint8_t *buffer, int buflen, int *copied)
Definition: jpegxl_parse.c:449
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231