FFmpeg
Loading...
Searching...
No Matches
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"
34#include "libavutil/opt.h"
35
36#include "avformat.h"
37#include "demux.h"
38#include "internal.h"
39
43
44static int jpegxl_anim_probe(const AVProbeData *p)
45{
46 uint8_t buffer[4096 + AV_INPUT_BUFFER_PADDING_SIZE] = {0};
47 int copied = 0, ret;
48 FFJXLMetadata meta = { 0 };
49
50 /* this is a raw codestream */
52 ret = ff_jpegxl_parse_codestream_header(p->buf, p->buf_size, &meta, 5);
53 if (ret >= 0 && meta.animation_offset > 0)
54 return AVPROBE_SCORE_MAX;
55
56 return 0;
57 }
58
59 /* not a JPEG XL file at all */
61 return 0;
62
63 if (ff_jpegxl_collect_codestream_header(p->buf, p->buf_size, buffer,
64 sizeof(buffer) - AV_INPUT_BUFFER_PADDING_SIZE, &copied) <= 0
65 || copied <= 0)
66 return 0;
67
68 ret = ff_jpegxl_parse_codestream_header(buffer, copied, &meta, 10);
69 if (ret >= 0 && meta.animation_offset > 0)
70 return AVPROBE_SCORE_MAX;
71
72 return 0;
73}
74
76{
77 JXLAnimDemuxContext *ctx = s->priv_data;
78 AVIOContext *pb = s->pb;
79 AVStream *st;
80 uint8_t head[256 + AV_INPUT_BUFFER_PADDING_SIZE];
81 const int sizeofhead = sizeof(head) - AV_INPUT_BUFFER_PADDING_SIZE;
82 int headsize = 0, ret;
83 FFJXLMetadata meta = { 0 };
84
85 uint64_t sig16 = avio_rl16(pb);
87 AV_WL16(head, sig16);
88 headsize = avio_read(s->pb, head + 2, sizeofhead - 2);
89 if (headsize < 0)
90 return headsize;
91 headsize += 2;
92 ctx->initial = av_buffer_alloc(headsize);
93 if (!ctx->initial)
94 return AVERROR(ENOMEM);
95 memcpy(ctx->initial->data, head, headsize);
96 } else {
97 uint64_t sig64 = avio_rl64(pb);
98 sig64 = (sig64 << 16) | sig16;
100 return AVERROR_INVALIDDATA;
101 avio_skip(pb, 2); // first box always 12 bytes
102 while (1) {
103 int copied = 0;
104 uint8_t buf[4096];
105 int read = avio_read(pb, buf, sizeof(buf));
106 if (read < 0)
107 return read;
108 if (!ctx->initial) {
109 ctx->initial = av_buffer_alloc(read + 12);
110 if (!ctx->initial)
111 return AVERROR(ENOMEM);
113 AV_WL32(ctx->initial->data + 8, 0x0a870a0d);
114 } else {
115 /* this only should be happening zero or one times in practice */
116 if (av_buffer_realloc(&ctx->initial, ctx->initial->size + read) < 0)
117 return AVERROR(ENOMEM);
118 }
119 ff_jpegxl_collect_codestream_header(buf, read, head + headsize, sizeofhead - headsize, &copied);
120 memcpy(ctx->initial->data + (ctx->initial->size - read), buf, read);
121 headsize += copied;
122 if (headsize >= sizeofhead || read < sizeof(buf))
123 break;
124 }
125 }
126
127 memset(head + headsize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
128
129 /* offset in bits of the animation header */
130 ret = ff_jpegxl_parse_codestream_header(head, headsize, &meta, 0);
131 if (ret < 0 || meta.animation_offset <= 0)
132 return AVERROR_INVALIDDATA;
133
135 if (!st)
136 return AVERROR(ENOMEM);
137
140 avpriv_set_pts_info(st, 1, meta.timebase.num, meta.timebase.den);
142
143 return 0;
144}
145
146/* the decoder requires the full input file as a single packet */
148{
149 JXLAnimDemuxContext *ctx = s->priv_data;
150 AVIOContext *pb = s->pb;
151 int ret;
153 size_t offset = 0;
154
155 size = avio_size(pb);
156 if (size < 0)
157 return size;
158 if (size > INT_MAX)
159 return AVERROR(EDOM);
160 if (size == 0)
161 size = 4096;
162
163 if (ctx->initial && size < ctx->initial->size)
164 size = ctx->initial->size;
165
166 ret = av_new_packet(pkt, size);
167 if (ret < 0)
168 return ret;
169
170 if (ctx->initial) {
171 offset = ctx->initial->size;
172 memcpy(pkt->data, ctx->initial->data, offset);
173 av_buffer_unref(&ctx->initial);
174 }
175
176 pkt->pos = avio_tell(pb) - offset;
177
178 ret = avio_read(pb, pkt->data + offset, size - offset);
179 if (ret < 0)
180 return ret;
181 if (ret < size - offset)
182 pkt->size = ret + offset;
183
184 return 0;
185}
186
188{
189 JXLAnimDemuxContext *ctx = s->priv_data;
190 if (ctx->initial)
191 av_buffer_unref(&ctx->initial);
192
193 return 0;
194}
195
197 .p.name = "jpegxl_anim",
198 .p.long_name = NULL_IF_CONFIG_SMALL("Animated JPEG XL"),
200 .p.mime_type = "image/jxl",
201 .p.extensions = "jxl",
202 .priv_data_size = sizeof(JXLAnimDemuxContext),
207 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
208};
const FFInputFormat ff_jpegxl_anim_demuxer
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:611
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
uint64_t avio_rl64(AVIOContext *s)
Definition aviobuf.c:741
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
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.
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_JPEGXL_ANIM
Definition codec_id.h:322
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
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
int av_buffer_realloc(AVBufferRef **pbuf, size_t size)
Reallocate a given buffer.
Definition buffer.c:183
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition buffer.c:77
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_WL64(p, v)
#define AV_WL32(p, v)
#define AV_RL64(p)
#define AV_RL16(p)
#define AV_WL16(p, v)
#define FF_JPEGXL_CONTAINER_SIGNATURE_LE
Definition jpegxl.h:26
#define FF_JPEGXL_CODESTREAM_SIGNATURE_LE
Definition jpegxl.h:25
static int jpegxl_anim_probe(const AVProbeData *p)
static int jpegxl_anim_read_header(AVFormatContext *s)
static int jpegxl_anim_read_packet(AVFormatContext *s, AVPacket *pkt)
static int jpegxl_anim_close(AVFormatContext *s)
unsigned offset
Definition libaomenc.c:763
int ff_jpegxl_collect_codestream_header(const uint8_t *input_buffer, int input_len, uint8_t *buffer, int buflen, int *copied)
int ff_jpegxl_parse_codestream_header(const uint8_t *buf, int buflen, FFJXLMetadata *meta, int validate)
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
AVOptions.
A reference to a data buffer.
Definition buffer.h:82
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational timebase
enum AVStreamParseType need_parsing
Definition internal.h:321
AVBufferRef * initial
static AVFormatContext * ctx
Definition movenc.c:49
static char buffer[20]
Definition seek.c:32
int size