FFmpeg
Loading...
Searching...
No Matches
apnggen.c
Go to the documentation of this file.
1/*
2 * Generate the sub-8-bit APNG streams used by the FATE tests.
3 *
4 * The frames are palette images. The first frame fills the canvas. Every
5 * later frame is a smaller rectangle whose x offset is odd, so it is not
6 * aligned to the byte that holds the packed samples. These files are built
7 * here, the same way audiogen builds its wave files, so the tests do not
8 * need media from the fate-suite.
9 *
10 * A file name containing "biganc" gets a large private ancillary chunk
11 * before the first IDAT, to check that probing still finds the animation
12 * (issue #26132). A file name containing "plain" omits acTL/fcTL and emits
13 * a single frame, giving a non-animated PNG control case.
14 *
15 * "bndhdr" inserts a caBX immediately after IHDR sized so the next chunk
16 * starts at offset 4096, where the probe buffer ends on a header boundary.
17 * "bndplte" inserts a 4000-byte caBX there, so a 4-bit PLTE crosses offset
18 * 4096. Both names require the depth marker as well.
19 *
20 * Copyright (c) 2026 Jun Zhao
21 *
22 * This file is part of FFmpeg.
23 *
24 * FFmpeg is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU Lesser General Public
26 * License as published by the Free Software Foundation; either
27 * version 2.1 of the License, or (at your option) any later version.
28 *
29 * FFmpeg is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 * Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public
35 * License along with FFmpeg; if not, write to the Free Software
36 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 */
38
39#include <errno.h>
40#include <stdint.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45/* Large enough to push the first IDAT past the 2048-byte probe buffer. */
46enum { CANVAS_W = 24, CANVAS_H = 20, FRAMES = 6, ANC_SIZE = 4096 };
47
48static const uint8_t ancillary[ANC_SIZE] = { 0 };
49
50static const uint8_t palette[16][3] = {
51 { 0, 0, 0 }, { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 },
52 { 255, 255, 0 }, { 255, 0, 255 }, { 0, 255, 255 }, { 255, 128, 0 },
53 { 128, 0, 255 }, { 128, 255, 0 }, { 0, 128, 255 }, { 255, 0, 128 },
54 { 64, 64, 64 }, { 192, 192, 192 }, { 128, 64, 0 }, { 255, 255, 255 },
55};
56
57typedef struct {
58 uint8_t data[ANC_SIZE + 2048];
59 int len;
60} Buf;
61
62static void die(const char *path)
63{
64 fprintf(stderr, "apnggen: %s: %s\n", path, strerror(errno));
65 exit(1);
66}
67
68static uint32_t crc32(const uint8_t *data, size_t size)
69{
70 static uint32_t table[256];
71 uint32_t crc = 0xFFFFFFFFu;
72 size_t i;
73
74 if (!table[1]) {
75 for (i = 0; i < 256; i++) {
76 uint32_t c = i;
77 int k;
78
79 for (k = 0; k < 8; k++)
80 c = (c & 1) ? 0xEDB88320u ^ (c >> 1) : c >> 1;
81 table[i] = c;
82 }
83 }
84 for (i = 0; i < size; i++)
85 crc = table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8);
86 return crc ^ 0xFFFFFFFFu;
87}
88
89static void put(Buf *b, const void *src, int n)
90{
91 if (n < 0 || b->len > (int)sizeof(b->data) - n) {
92 fprintf(stderr, "apnggen: output too large\n");
93 exit(1);
94 }
95 memcpy(b->data + b->len, src, n);
96 b->len += n;
97}
98
99static void put_be32(Buf *b, uint32_t v)
100{
101 uint8_t p[4] = { v >> 24, v >> 16, v >> 8, v };
102
103 put(b, p, 4);
104}
105
106static void chunk(Buf *b, const char type[4], const uint8_t *data, int n)
107{
108 int start = b->len + 4;
109
110 put_be32(b, n);
111 put(b, type, 4);
112 if (n)
113 put(b, data, n);
114 put_be32(b, crc32(b->data + start, 4 + n));
115}
116
117/* First frame covers the canvas. Later frames are 7x5 rectangles on odd x
118 * offsets, one pixel further right each frame. */
119static int sample(int depth, int frame, int x, int y)
120{
121 int max = (1 << depth) - 1;
122
123 if (!frame)
124 return (x / 4 + y / 5) & max;
125 if (depth == 1)
126 return (x + y + frame) & 1;
127 return 1 + (x + y + frame) % max;
128}
129
130static int pack_frame(uint8_t *dst, int depth, int frame, int w, int h)
131{
132 int row_bytes = (w * depth + 7) / 8;
133 int out = 0, y, x;
134
135 for (y = 0; y < h; y++) {
136 dst[out++] = 0;
137 memset(dst + out, 0, row_bytes);
138 for (x = 0; x < w; x++) {
139 int bit = x * depth;
140
141 dst[out + bit / 8] |= sample(depth, frame, x, y) << (8 - depth - bit % 8);
142 }
143 out += row_bytes;
144 }
145 return out;
146}
147
148/* zlib wrapper around one stored deflate block. No compressor needed. */
149static void put_stored(Buf *b, const uint8_t *raw, int n)
150{
151 unsigned neg = ~n & 0xFFFF;
152 uint8_t hdr[7] = { 0x78, 0x01, 0x01, n, n >> 8, neg, neg >> 8 };
153 uint32_t a = 1, s = 0;
154 int i;
155
156 put(b, hdr, 7);
157 put(b, raw, n);
158 for (i = 0; i < n; i++) {
159 a += raw[i];
160 if (a >= 65521)
161 a -= 65521;
162 s += a;
163 if (s >= 65521)
164 s -= 65521;
165 }
166 put_be32(b, (s << 16) | a);
167}
168
169static void write_apng(const char *path, int depth, int big_anc, int plain,
170 int anc_after_ihdr)
171{
172 static const uint8_t sig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
173 uint8_t ihdr[13] = { 0, 0, 0, CANVAS_W, 0, 0, 0, CANVAS_H, depth, 3 };
174 uint8_t actl[8] = { 0, 0, 0, FRAMES, 0, 0, 0, 1 };
175 uint8_t plte[16 * 3], raw[512], fctl[26];
176 Buf out = { 0 }, image = { 0 };
177 FILE *f;
178 int colors = 1 << depth, seq = 0, frame, frames = plain ? 1 : FRAMES, i;
179
180 if (anc_after_ihdr < 0 || anc_after_ihdr > ANC_SIZE) {
181 fprintf(stderr, "apnggen: ancillary payload does not fit\n");
182 exit(1);
183 }
184
185 for (i = 0; i < colors; i++)
186 memcpy(plte + 3 * i, palette[i], 3);
187
188 put(&out, sig, 8);
189 chunk(&out, "IHDR", ihdr, sizeof(ihdr));
190 if (anc_after_ihdr)
191 chunk(&out, "caBX", ancillary, anc_after_ihdr);
192 if (!plain)
193 chunk(&out, "acTL", actl, sizeof(actl));
194 chunk(&out, "PLTE", plte, colors * 3);
195 if (big_anc)
196 chunk(&out, "caBX", ancillary, sizeof(ancillary));
197
198 for (frame = 0; frame < frames; frame++) {
199 int sub = frame != 0;
200 int x = sub ? 1 + 2 * (frame - 1) : 0;
201 int y = sub ? 1 + (frame - 1) : 0;
202 int w = sub ? 7 : CANVAS_W;
203 int h = sub ? 5 : CANVAS_H;
204
205 if (!plain) {
206 memset(fctl, 0, sizeof(fctl));
207 fctl[3] = seq++;
208 fctl[7] = w;
209 fctl[11] = h;
210 fctl[15] = x;
211 fctl[19] = y;
212 fctl[21] = 1; /* delay numerator */
213 fctl[23] = 10; /* delay denominator: 1/10 s */
214 chunk(&out, "fcTL", fctl, sizeof(fctl));
215 }
216
217 image.len = 0;
218 if (sub)
219 put_be32(&image, seq++);
220 put_stored(&image, raw, pack_frame(raw, depth, frame, w, h));
221 chunk(&out, sub ? "fdAT" : "IDAT", image.data, image.len);
222 }
223 chunk(&out, "IEND", NULL, 0);
224
225 f = fopen(path, "wb");
226 if (!f || fwrite(out.data, 1, out.len, f) != (size_t)out.len || fclose(f))
227 die(path);
228}
229
230int main(int argc, char **argv)
231{
232 const char *path = argc == 2 ? argv[1] : NULL;
233 int depth = 0, anc_after_ihdr = 0;
234
235 if (!path) {
236 fprintf(stderr, "usage: apnggen OUTPUT.png\n");
237 return 1;
238 }
239 if (strstr(path, "1bit"))
240 depth = 1;
241 else if (strstr(path, "2bit"))
242 depth = 2;
243 else if (strstr(path, "4bit"))
244 depth = 4;
245 if (!depth) {
246 fprintf(stderr, "apnggen: unsupported file name %s\n", path);
247 return 1;
248 }
249 /* IHDR ends at offset 33. A 4051-byte payload plus its 12-byte wrapper
250 * ends at 4096, so the next header is absent from that probe buffer.
251 * A 4000-byte payload leaves a 4-bit PLTE crossing the same offset:
252 * the chunk ends at 4045, acTL at 4065, and PLTE at 4125. */
253 if (strstr(path, "bndhdr"))
254 anc_after_ihdr = 4051;
255 else if (strstr(path, "bndplte")) {
256 if (depth != 4) {
257 fprintf(stderr, "apnggen: bndplte requires a 4-bit file name\n");
258 return 1;
259 }
260 anc_after_ihdr = 4000;
261 }
262 write_apng(path, depth, strstr(path, "biganc") != NULL,
263 strstr(path, "plain") != NULL, anc_after_ihdr);
264 return 0;
265}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static FILE * out
static int frames
static int pack_frame(uint8_t *dst, int depth, int frame, int w, int h)
Definition apnggen.c:130
static void put(Buf *b, const void *src, int n)
Definition apnggen.c:89
static void put_stored(Buf *b, const uint8_t *raw, int n)
Definition apnggen.c:149
static void chunk(Buf *b, const char type[4], const uint8_t *data, int n)
Definition apnggen.c:106
static void die(const char *path)
Definition apnggen.c:62
static const uint8_t palette[16][3]
Definition apnggen.c:50
static void write_apng(const char *path, int depth, int big_anc, int plain, int anc_after_ihdr)
Definition apnggen.c:169
static void put_be32(Buf *b, uint32_t v)
Definition apnggen.c:99
static uint32_t crc32(const uint8_t *data, size_t size)
Definition apnggen.c:68
static const uint8_t ancillary[ANC_SIZE]
Definition apnggen.c:48
@ FRAMES
Definition apnggen.c:46
@ CANVAS_W
Definition apnggen.c:46
@ ANC_SIZE
Definition apnggen.c:46
@ CANVAS_H
Definition apnggen.c:46
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define bit(string, value)
Definition cbs_mpeg2.c:56
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
#define max(a, b)
static AVFrame * frame
int main
Definition dovi_rpuenc.c:38
char * path
Definition ffplay.c:385
#define sample
int a
#define b
Definition input.c:43
enum MPFType type
Definition jpegmpfenc.c:55
uint8_t w
Definition llvidencdsp.c:39
const char data[16]
Definition mxf.c:155
static const uint16_t table[]
Definition prosumer.c:203
static volatile sig_atomic_t sig
Definition signal.c:48
Definition apnggen.c:57
uint8_t data[ANC_SIZE+2048]
Definition apnggen.c:58
int len
Definition apnggen.c:59
static const uint8_t plain[]
Definition aes_ctr.c:27
#define src
Definition vp8dsp.c:248
int size
int len
static double c[64]