1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* 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>
#include "libavcodec/jpegxl.h"
#include "libavcodec/jpegxl_parse.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/opt.h"
#include "avformat.h"
#include "demux.h"
#include "internal.h"
typedef struct JXLAnimDemuxContext {
AVBufferRef *initial;
} JXLAnimDemuxContext;
static int jpegxl_anim_probe(const AVProbeData *p)
{
uint8_t buffer[4096 + AV_INPUT_BUFFER_PADDING_SIZE];
int copied = 0, ret;
FFJXLMetadata meta = { 0 };
/* this is a raw codestream */
if (AV_RL16(p->buf) == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
ret = ff_jpegxl_parse_codestream_header(p->buf, p->buf_size, &meta, 5);
if (ret >= 0 && meta.animation_offset > 0)
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 (ff_jpegxl_collect_codestream_header(p->buf, p->buf_size, buffer,
sizeof(buffer) - AV_INPUT_BUFFER_PADDING_SIZE, &copied) <= 0
|| copied <= 0)
return 0;
ret = ff_jpegxl_parse_codestream_header(buffer, copied, &meta, 10);
if (ret >= 0 && meta.animation_offset > 0)
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;
uint8_t head[256 + AV_INPUT_BUFFER_PADDING_SIZE];
const int sizeofhead = sizeof(head) - AV_INPUT_BUFFER_PADDING_SIZE;
int headsize = 0, ret;
FFJXLMetadata meta = { 0 };
uint64_t sig16 = avio_rl16(pb);
if (sig16 == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
AV_WL16(head, sig16);
headsize = avio_read(s->pb, head + 2, sizeofhead - 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 = 0;
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);
}
ff_jpegxl_collect_codestream_header(buf, read, head + headsize, sizeofhead - headsize, &copied);
memcpy(ctx->initial->data + (ctx->initial->size - read), buf, read);
headsize += copied;
if (headsize >= sizeofhead || read < sizeof(buf))
break;
}
}
/* offset in bits of the animation header */
ret = ff_jpegxl_parse_codestream_header(head, headsize, &meta, 0);
if (ret < 0 || meta.animation_offset <= 0)
return AVERROR_INVALIDDATA;
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;
avpriv_set_pts_info(st, 1, meta.timebase.num, meta.timebase.den);
ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
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;
size = avio_size(pb);
if (size < 0)
return size;
if (size > INT_MAX)
return AVERROR(EDOM);
if (size == 0)
size = 4096;
if (ctx->initial && size < ctx->initial->size)
size = ctx->initial->size;
ret = av_new_packet(pkt, size);
if (ret < 0)
return ret;
if (ctx->initial) {
offset = ctx->initial->size;
memcpy(pkt->data, ctx->initial->data, offset);
av_buffer_unref(&ctx->initial);
}
pkt->pos = avio_tell(pb) - offset;
ret = avio_read(pb, pkt->data + offset, size - offset);
if (ret < 0)
return ret;
if (ret < size - offset)
pkt->size = ret + offset;
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 FFInputFormat ff_jpegxl_anim_demuxer = {
.p.name = "jpegxl_anim",
.p.long_name = NULL_IF_CONFIG_SMALL("Animated JPEG XL"),
.p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
.p.mime_type = "image/jxl",
.p.extensions = "jxl",
.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,
};
|