-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.h
More file actions
79 lines (63 loc) · 1.91 KB
/
encode.h
File metadata and controls
79 lines (63 loc) · 1.91 KB
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
#include <avif/avif.h>
#include <stdio.h>
#include <string.h>
uint8_t* encode(
uint8_t *rgb_in,
int width, int height,
int maxThreads,
int alphaPremultiplied, int depth, avifPixelFormat chroma,
int quality, int speed,
size_t *size
) {
avifResult result;
avifImage *image = avifImageCreate(width, height, depth, chroma);
if (!image) {
fprintf(stderr, "[avifImageCreate] Out of memory\n");
goto cleanup;
}
avifRGBImage rgb;
avifRGBImageSetDefaults(&rgb, image);
rgb.maxThreads = maxThreads;
rgb.alphaPremultiplied = alphaPremultiplied;
result = avifRGBImageAllocatePixels(&rgb);
if (result != AVIF_RESULT_OK) {
fprintf(stderr, "[avifRGBImageAllocatePixels] Out of memory\n");
goto cleanup;
}
rgb.pixels = rgb_in;
result = avifImageRGBToYUV(image, &rgb);
if (result != AVIF_RESULT_OK) {
fprintf(stderr, "[avifImageRGBToYUV] Out of memory\n");
goto cleanup;
}
avifRWData output = AVIF_DATA_EMPTY;
avifEncoder *encoder = avifEncoderCreate();
if (!encoder) {
fprintf(stderr, "[avifEncoderCreate] Out of memory\n");
goto cleanup;
}
encoder->maxThreads = maxThreads;
encoder->quality = quality;
encoder->speed = speed;
result = avifEncoderAddImage(encoder, image, 1, AVIF_ADD_IMAGE_FLAG_SINGLE);
if (result != AVIF_RESULT_OK) {
fprintf(stderr, "Failed to add image to encoder: %s\n", avifResultToString(result));
goto cleanup;
}
result = avifEncoderFinish(encoder, &output);
if (result != AVIF_RESULT_OK) {
fprintf(stderr, "Failed to finish encode: %s\n", avifResultToString(result));
goto cleanup;
}
*size = output.size;
return output.data;
cleanup:
if (image) {
avifImageDestroy(image);
}
if (encoder) {
avifEncoderDestroy(encoder);
}
avifRWDataFree(&output);
avifRGBImageFreePixels(&rgb);
}