FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/jpegxl_anim_dec.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 81 99 81.8%
Functions: 4 4 100.0%
Branches: 37 62 59.7%

Line Branch Exec Source
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"
32 #include "libavcodec/jpegxl_parse.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/opt.h"
35
36 #include "avformat.h"
37 #include "demux.h"
38 #include "internal.h"
39
40 typedef struct JXLAnimDemuxContext {
41 AVBufferRef *initial;
42 } JXLAnimDemuxContext;
43
44 7128 static int jpegxl_anim_probe(const AVProbeData *p)
45 {
46 uint8_t buffer[4096 + AV_INPUT_BUFFER_PADDING_SIZE];
47 7128 int copied = 0, ret;
48 7128 FFJXLMetadata meta = { 0 };
49
50 /* this is a raw codestream */
51
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7125 times.
7128 if (AV_RL16(p->buf) == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
52 3 ret = ff_jpegxl_parse_codestream_header(p->buf, p->buf_size, &meta, 5);
53
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
3 if (ret >= 0 && meta.animation_offset > 0)
54 1 return AVPROBE_SCORE_MAX;
55
56 2 return 0;
57 }
58
59 /* not a JPEG XL file at all */
60
2/2
✓ Branch 0 taken 7122 times.
✓ Branch 1 taken 3 times.
7125 if (AV_RL64(p->buf) != FF_JPEGXL_CONTAINER_SIGNATURE_LE)
61 7122 return 0;
62
63
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 if (ff_jpegxl_collect_codestream_header(p->buf, p->buf_size, buffer,
64 sizeof(buffer) - AV_INPUT_BUFFER_PADDING_SIZE, &copied) <= 0
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 || copied <= 0)
66 return 0;
67
68 3 ret = ff_jpegxl_parse_codestream_header(buffer, copied, &meta, 10);
69
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
3 if (ret >= 0 && meta.animation_offset > 0)
70 1 return AVPROBE_SCORE_MAX;
71
72 2 return 0;
73 }
74
75 2 static int jpegxl_anim_read_header(AVFormatContext *s)
76 {
77 2 JXLAnimDemuxContext *ctx = s->priv_data;
78 2 AVIOContext *pb = s->pb;
79 AVStream *st;
80 uint8_t head[256 + AV_INPUT_BUFFER_PADDING_SIZE];
81 2 const int sizeofhead = sizeof(head) - AV_INPUT_BUFFER_PADDING_SIZE;
82 2 int headsize = 0, ret;
83 2 FFJXLMetadata meta = { 0 };
84
85 2 uint64_t sig16 = avio_rl16(pb);
86
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (sig16 == FF_JPEGXL_CODESTREAM_SIGNATURE_LE) {
87 1 AV_WL16(head, sig16);
88 1 headsize = avio_read(s->pb, head + 2, sizeofhead - 2);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (headsize < 0)
90 return headsize;
91 1 headsize += 2;
92 1 ctx->initial = av_buffer_alloc(headsize);
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ctx->initial)
94 return AVERROR(ENOMEM);
95 1 memcpy(ctx->initial->data, head, headsize);
96 } else {
97 1 uint64_t sig64 = avio_rl64(pb);
98 1 sig64 = (sig64 << 16) | sig16;
99
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (sig64 != FF_JPEGXL_CONTAINER_SIGNATURE_LE)
100 return AVERROR_INVALIDDATA;
101 1 avio_skip(pb, 2); // first box always 12 bytes
102 while (1) {
103 1 int copied = 0;
104 uint8_t buf[4096];
105 1 int read = avio_read(pb, buf, sizeof(buf));
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (read < 0)
107 return read;
108
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!ctx->initial) {
109 1 ctx->initial = av_buffer_alloc(read + 12);
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ctx->initial)
111 return AVERROR(ENOMEM);
112 1 AV_WL64(ctx->initial->data, FF_JPEGXL_CONTAINER_SIGNATURE_LE);
113 1 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 1 ff_jpegxl_collect_codestream_header(buf, read, head + headsize, sizeofhead - headsize, &copied);
120 1 memcpy(ctx->initial->data + (ctx->initial->size - read), buf, read);
121 1 headsize += copied;
122
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (headsize >= sizeofhead || read < sizeof(buf))
123 break;
124 }
125 }
126
127 /* offset in bits of the animation header */
128 2 ret = ff_jpegxl_parse_codestream_header(head, headsize, &meta, 0);
129
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (ret < 0 || meta.animation_offset <= 0)
130 return AVERROR_INVALIDDATA;
131
132 2 st = avformat_new_stream(s, NULL);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!st)
134 return AVERROR(ENOMEM);
135
136 2 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
137 2 st->codecpar->codec_id = AV_CODEC_ID_JPEGXL;
138 2 avpriv_set_pts_info(st, 1, meta.timebase.num, meta.timebase.den);
139 2 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
140
141 2 return 0;
142 }
143
144 /* the decoder requires the full input file as a single packet */
145 7 static int jpegxl_anim_read_packet(AVFormatContext *s, AVPacket *pkt)
146 {
147 7 JXLAnimDemuxContext *ctx = s->priv_data;
148 7 AVIOContext *pb = s->pb;
149 int ret;
150 int64_t size;
151 7 size_t offset = 0;
152
153 7 size = avio_size(pb);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (size < 0)
155 return size;
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (size > INT_MAX)
157 return AVERROR(EDOM);
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (size == 0)
159 size = 4096;
160
161
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
7 if (ctx->initial && size < ctx->initial->size)
162 size = ctx->initial->size;
163
164 7 ret = av_new_packet(pkt, size);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
166 return ret;
167
168
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (ctx->initial) {
169 2 offset = ctx->initial->size;
170 2 memcpy(pkt->data, ctx->initial->data, offset);
171 2 av_buffer_unref(&ctx->initial);
172 }
173
174 7 pkt->pos = avio_tell(pb) - offset;
175
176 7 ret = avio_read(pb, pkt->data + offset, size - offset);
177
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if (ret < 0)
178 5 return ret;
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < size - offset)
180 pkt->size = ret + offset;
181
182 2 return 0;
183 }
184
185 2 static int jpegxl_anim_close(AVFormatContext *s)
186 {
187 2 JXLAnimDemuxContext *ctx = s->priv_data;
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ctx->initial)
189 av_buffer_unref(&ctx->initial);
190
191 2 return 0;
192 }
193
194 const FFInputFormat ff_jpegxl_anim_demuxer = {
195 .p.name = "jpegxl_anim",
196 .p.long_name = NULL_IF_CONFIG_SMALL("Animated JPEG XL"),
197 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
198 .p.mime_type = "image/jxl",
199 .p.extensions = "jxl",
200 .priv_data_size = sizeof(JXLAnimDemuxContext),
201 .read_probe = jpegxl_anim_probe,
202 .read_header = jpegxl_anim_read_header,
203 .read_packet = jpegxl_anim_read_packet,
204 .read_close = jpegxl_anim_close,
205 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
206 };
207