Skip to content

Commit 1b47a9b

Browse files
committed
add benchmark scripts
1 parent 436d5a4 commit 1b47a9b

4 files changed

Lines changed: 434 additions & 0 deletions

File tree

bench/cgif_bench.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
6+
#include "cgif.h"
7+
8+
#define WIDTH 8192
9+
#define HEIGHT 8192
10+
11+
static uint64_t seed = 42;
12+
13+
__attribute__((no_sanitize("integer")))
14+
static int psdrand(void) {
15+
seed = 6364136223846793005ULL * seed + 1;
16+
return seed >> 33;
17+
}
18+
19+
static void encode(const char *path, uint8_t *palette, uint16_t numColors, uint8_t *pImageData) {
20+
CGIF_Config gConfig;
21+
CGIF_FrameConfig fConfig;
22+
23+
memset(&gConfig, 0, sizeof(gConfig));
24+
gConfig.width = WIDTH;
25+
gConfig.height = HEIGHT;
26+
gConfig.pGlobalPalette = palette;
27+
gConfig.numGlobalPaletteEntries = numColors;
28+
gConfig.path = path;
29+
30+
CGIF *pGIF = cgif_newgif(&gConfig);
31+
memset(&fConfig, 0, sizeof(fConfig));
32+
fConfig.pImageData = pImageData;
33+
cgif_addframe(pGIF, &fConfig);
34+
cgif_close(pGIF);
35+
}
36+
37+
int main(int argc, char **argv) {
38+
if (argc != 2) {
39+
fprintf(stderr, "usage: %s <stripe|gradient|noise|solid>\n", argv[0]);
40+
return 1;
41+
}
42+
43+
uint8_t palette256[256 * 3];
44+
for (int i = 0; i < 256; ++i) {
45+
palette256[i * 3] = i;
46+
palette256[i * 3 + 1] = i;
47+
palette256[i * 3 + 2] = i;
48+
}
49+
50+
uint8_t palette3[] = {0xFF,0x00,0x00, 0x00,0xFF,0x00, 0x00,0x00,0xFF};
51+
uint8_t palette4[] = {0xFF,0x00,0x00, 0x00,0xFF,0x00, 0x00,0x00,0xFF, 0xFF,0xFF,0x00};
52+
uint8_t *img = malloc(WIDTH * HEIGHT);
53+
54+
if (strcmp(argv[1], "stripe") == 0) {
55+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
56+
img[i] = (i % WIDTH) / 4 % 3;
57+
encode("/dev/null", palette3, 3, img);
58+
59+
} else if (strcmp(argv[1], "gradient") == 0) {
60+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
61+
img[i] = (i % WIDTH) * 256 / WIDTH;
62+
encode("/dev/null", palette256, 256, img);
63+
64+
} else if (strcmp(argv[1], "noise") == 0) {
65+
seed = 42;
66+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
67+
img[i] = psdrand() % 256;
68+
encode("/dev/null", palette256, 256, img);
69+
70+
} else if (strcmp(argv[1], "solid") == 0) {
71+
memset(img, 0, WIDTH * HEIGHT);
72+
encode("/dev/null", palette3, 3, img);
73+
74+
} else if (strcmp(argv[1], "checker") == 0) {
75+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
76+
img[i] = (((i % WIDTH) + (i / WIDTH)) & 1);
77+
encode("/dev/null", palette3, 3, img);
78+
79+
} else if (strcmp(argv[1], "dither") == 0) {
80+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
81+
img[i] = ((i % WIDTH) + (i / WIDTH) * 7) % 256;
82+
encode("/dev/null", palette256, 256, img);
83+
84+
} else if (strcmp(argv[1], "fewnoise") == 0) {
85+
seed = 42;
86+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
87+
img[i] = psdrand() % 4;
88+
encode("/dev/null", palette4, 4, img);
89+
90+
} else {
91+
fprintf(stderr, "unknown pattern: %s\n", argv[1]);
92+
free(img);
93+
return 1;
94+
}
95+
96+
free(img);
97+
return 0;
98+
}

bench/ffmpeg_bench.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <libavcodec/avcodec.h>
6+
#include <libavutil/frame.h>
7+
#include <libavutil/imgutils.h>
8+
9+
#define WIDTH 8192
10+
#define HEIGHT 8192
11+
12+
static uint64_t seed = 42;
13+
14+
__attribute__((no_sanitize("integer")))
15+
static int psdrand(void) {
16+
seed = 6364136223846793005ULL * seed + 1;
17+
return seed >> 33;
18+
}
19+
20+
static void encode(const char *path, uint32_t *palette, uint8_t *img) {
21+
const AVCodec *codec = avcodec_find_encoder_by_name("gif");
22+
AVCodecContext *ctx = avcodec_alloc_context3(codec);
23+
ctx->width = WIDTH;
24+
ctx->height = HEIGHT;
25+
ctx->pix_fmt = AV_PIX_FMT_PAL8;
26+
ctx->time_base = (AVRational){1, 25};
27+
avcodec_open2(ctx, codec, NULL);
28+
29+
AVFrame *frame = av_frame_alloc();
30+
frame->format = AV_PIX_FMT_PAL8;
31+
frame->width = WIDTH;
32+
frame->height = HEIGHT;
33+
frame->pts = 0;
34+
av_frame_get_buffer(frame, 0);
35+
36+
memcpy(frame->data[1], palette, 256 * 4);
37+
for (int y = 0; y < HEIGHT; ++y)
38+
memcpy(frame->data[0] + y * frame->linesize[0], img + y * WIDTH, WIDTH);
39+
40+
AVPacket *pkt = av_packet_alloc();
41+
avcodec_send_frame(ctx, frame);
42+
avcodec_send_frame(ctx, NULL);
43+
44+
FILE *f = fopen(path, "wb");
45+
while (avcodec_receive_packet(ctx, pkt) == 0) {
46+
fwrite(pkt->data, 1, pkt->size, f);
47+
av_packet_unref(pkt);
48+
}
49+
fclose(f);
50+
51+
av_packet_free(&pkt);
52+
av_frame_free(&frame);
53+
avcodec_free_context(&ctx);
54+
}
55+
56+
int main(int argc, char **argv) {
57+
if (argc != 2) {
58+
fprintf(stderr, "usage: %s <stripe|gradient|noise|solid>\n", argv[0]);
59+
return 1;
60+
}
61+
62+
uint32_t palette256[256];
63+
for (int i = 0; i < 256; ++i)
64+
palette256[i] = 0xFF000000 | (i << 16) | (i << 8) | i;
65+
66+
uint32_t palette3[256] = {0};
67+
palette3[0] = 0xFF0000FF;
68+
palette3[1] = 0xFF00FF00;
69+
palette3[2] = 0xFFFF0000;
70+
71+
uint32_t palette4[256] = {0};
72+
palette4[0] = 0xFF0000FF;
73+
palette4[1] = 0xFF00FF00;
74+
palette4[2] = 0xFFFF0000;
75+
palette4[3] = 0xFF00FFFF;
76+
77+
uint8_t *img = malloc(WIDTH * HEIGHT);
78+
79+
if (strcmp(argv[1], "stripe") == 0) {
80+
for (int i = 0; i < WIDTH * HEIGHT; ++i) img[i] = (i % WIDTH) / 4 % 3;
81+
encode("/dev/null", palette3, img);
82+
} else if (strcmp(argv[1], "gradient") == 0) {
83+
for (int i = 0; i < WIDTH * HEIGHT; ++i) img[i] = (i % WIDTH) * 256 / WIDTH;
84+
encode("/dev/null", palette256, img);
85+
} else if (strcmp(argv[1], "noise") == 0) {
86+
seed = 42;
87+
for (int i = 0; i < WIDTH * HEIGHT; ++i) img[i] = psdrand() % 256;
88+
encode("/dev/null", palette256, img);
89+
} else if (strcmp(argv[1], "solid") == 0) {
90+
memset(img, 0, WIDTH * HEIGHT);
91+
encode("/dev/null", palette3, img);
92+
} else if (strcmp(argv[1], "checker") == 0) {
93+
for (int i = 0; i < WIDTH * HEIGHT; ++i) img[i] = (((i % WIDTH) + (i / WIDTH)) & 1);
94+
encode("/dev/null", palette3, img);
95+
} else if (strcmp(argv[1], "dither") == 0) {
96+
for (int i = 0; i < WIDTH * HEIGHT; ++i) img[i] = ((i % WIDTH) + (i / WIDTH) * 7) % 256;
97+
encode("/dev/null", palette256, img);
98+
} else if (strcmp(argv[1], "fewnoise") == 0) {
99+
seed = 42;
100+
for (int i = 0; i < WIDTH * HEIGHT; ++i) img[i] = psdrand() % 4;
101+
encode("/dev/null", palette4, img);
102+
} else {
103+
fprintf(stderr, "unknown pattern: %s\n", argv[1]);
104+
free(img);
105+
return 1;
106+
}
107+
108+
free(img);
109+
return 0;
110+
}

bench/giflib_bench.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <stdlib.h>
2+
#include <stdint.h>
3+
#include <string.h>
4+
#include <stdio.h>
5+
#include <gif_lib.h>
6+
7+
#define WIDTH 8192
8+
#define HEIGHT 8192
9+
10+
static uint64_t seed = 42;
11+
12+
__attribute__((no_sanitize("integer")))
13+
static int psdrand(void) {
14+
seed = 6364136223846793005ULL * seed + 1;
15+
return seed >> 33;
16+
}
17+
18+
static void encode(const char *path, GifColorType *colors, int numColors, uint8_t *img) {
19+
int error;
20+
int mapSize = 1;
21+
while (mapSize < numColors) mapSize *= 2;
22+
23+
GifFileType *gif = EGifOpenFileName(path, false, &error);
24+
ColorMapObject *cmap = GifMakeMapObject(mapSize, NULL);
25+
memcpy(cmap->Colors, colors, numColors * sizeof(GifColorType));
26+
27+
EGifSetGifVersion(gif, true);
28+
EGifPutScreenDesc(gif, WIDTH, HEIGHT, 8, 0, cmap);
29+
EGifPutImageDesc(gif, 0, 0, WIDTH, HEIGHT, false, NULL);
30+
31+
for (int y = 0; y < HEIGHT; ++y)
32+
EGifPutLine(gif, img + y * WIDTH, WIDTH);
33+
34+
GifFreeMapObject(cmap);
35+
EGifCloseFile(gif, &error);
36+
}
37+
38+
int main(int argc, char **argv) {
39+
if (argc != 2) {
40+
fprintf(stderr, "usage: %s <stripe|gradient|noise|solid>\n", argv[0]);
41+
return 1;
42+
}
43+
44+
GifColorType palette256[256];
45+
for (int i = 0; i < 256; ++i) {
46+
palette256[i].Red = i; palette256[i].Green = i; palette256[i].Blue = i;
47+
}
48+
49+
GifColorType palette3[3] = {{0xFF,0,0},{0,0xFF,0},{0,0,0xFF}};
50+
GifColorType palette4[4] = {{0xFF,0,0},{0,0xFF,0},{0,0,0xFF},{0xFF,0xFF,0}};
51+
uint8_t *img = malloc(WIDTH * HEIGHT);
52+
53+
if (strcmp(argv[1], "stripe") == 0) {
54+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
55+
img[i] = (i % WIDTH) / 4 % 3;
56+
encode("/dev/null", palette3, 3, img);
57+
58+
} else if (strcmp(argv[1], "gradient") == 0) {
59+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
60+
img[i] = (i % WIDTH) * 256 / WIDTH;
61+
encode("/dev/null", palette256, 256, img);
62+
63+
} else if (strcmp(argv[1], "noise") == 0) {
64+
seed = 42;
65+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
66+
img[i] = psdrand() % 256;
67+
encode("/dev/null", palette256, 256, img);
68+
69+
} else if (strcmp(argv[1], "solid") == 0) {
70+
memset(img, 0, WIDTH * HEIGHT);
71+
encode("/dev/null", palette3, 3, img);
72+
73+
} else if (strcmp(argv[1], "checker") == 0) {
74+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
75+
img[i] = (((i % WIDTH) + (i / WIDTH)) & 1);
76+
encode("/dev/null", palette3, 3, img);
77+
78+
} else if (strcmp(argv[1], "dither") == 0) {
79+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
80+
img[i] = ((i % WIDTH) + (i / WIDTH) * 7) % 256;
81+
encode("/dev/null", palette256, 256, img);
82+
83+
} else if (strcmp(argv[1], "fewnoise") == 0) {
84+
seed = 42;
85+
for (int i = 0; i < WIDTH * HEIGHT; ++i)
86+
img[i] = psdrand() % 4;
87+
encode("/dev/null", palette4, 4, img);
88+
89+
} else {
90+
fprintf(stderr, "unknown pattern: %s\n", argv[1]);
91+
free(img);
92+
return 1;
93+
}
94+
95+
free(img);
96+
return 0;
97+
}

0 commit comments

Comments
 (0)