-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.h
More file actions
88 lines (72 loc) · 2.2 KB
/
decode.h
File metadata and controls
88 lines (72 loc) · 2.2 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
80
81
82
83
84
85
86
87
88
#include <avif/avif.h>
#include <stdio.h>
#include <string.h>
int decode(
uint8_t *avif_in,
int avif_in_size,
int config_only,
int decode_all,
uint32_t *width, uint32_t *height,
uint32_t *depth,
int maxThreads,
uint32_t *count,
uint8_t *delay,
uint8_t *out
) {
avifDecoder *decoder = avifDecoderCreate();
decoder->ignoreExif = 1;
decoder->ignoreXMP = 1;
decoder->maxThreads = maxThreads;
decoder->strictFlags = 0;
avifResult result = avifDecoderSetIOMemory(decoder, avif_in, avif_in_size);
if(result != AVIF_RESULT_OK) {
avifDecoderDestroy(decoder);
return 0;
}
result = avifDecoderParse(decoder);
if(result != AVIF_RESULT_OK) {
avifDecoderDestroy(decoder);
return 0;
}
*width = (uint32_t)decoder->image->width;
*height = (uint32_t)decoder->image->height;
*depth = (uint32_t)decoder->image->depth;
*count = (uint32_t)decoder->imageCount;
if(config_only) {
avifDecoderDestroy(decoder);
return 1;
}
avifRGBImage rgb;
avifRGBImageSetDefaults(&rgb, decoder->image);
rgb.maxThreads = maxThreads;
rgb.alphaPremultiplied = 1;
if(decoder->image->depth > 8) {
rgb.depth = 16;
}
if(decoder->imageCount > 1 && decode_all) {
rgb.chromaUpsampling = AVIF_CHROMA_UPSAMPLING_BEST_QUALITY;
}
while(avifDecoderNextImage(decoder) == AVIF_RESULT_OK) {
result = avifRGBImageAllocatePixels(&rgb);
if(result != AVIF_RESULT_OK) {
avifDecoderDestroy(decoder);
return 0;
}
result = avifImageYUVToRGB(decoder->image, &rgb);
if(result != AVIF_RESULT_OK) {
avifRGBImageFreePixels(&rgb);
avifDecoderDestroy(decoder);
return 0;
}
int buf_size = rgb.rowBytes * rgb.height;
memcpy(out + buf_size*decoder->imageIndex, rgb.pixels, buf_size);
memcpy(delay + sizeof(double)*decoder->imageIndex, &decoder->imageTiming.duration, sizeof(double));
avifRGBImageFreePixels(&rgb);
if(!decode_all) {
avifDecoderDestroy(decoder);
return 1;
}
}
avifDecoderDestroy(decoder);
return 1;
}