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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#[cfg(feature = "codec-rav1e")]
pub use rav1e::capi::*;

pub const AVIF_PLANE_COUNT_YUV: usize = 3;

#[allow(non_camel_case_types)]
pub type avifBool = libc::c_int;

#[allow(non_camel_case_types)]
pub type __enum = libc::c_int;

#[allow(non_camel_case_types)]
pub type avifProfileFormat = __enum;

pub const AVIF_PROFILE_FORMAT_NONE: avifProfileFormat = 0;
pub const AVIF_PROFILE_FORMAT_ICC: avifProfileFormat = 1;
pub const AVIF_PROFILE_FORMAT_NCLX: avifProfileFormat = 2;

#[allow(non_camel_case_types)]
pub type avifPlanesFlags = __enum;
pub const AVIF_PLANES_YUV: avifPlanesFlags = 1;
pub const AVIF_PLANES_A: avifPlanesFlags = 1 << 1;
pub const AVIF_PLANES_ALL: avifPlanesFlags = 0xff;

#[allow(non_camel_case_types)]
pub type avifDecoderSource = __enum;
/// If a moov box is present in the .avif(s), use the tracks in it, otherwise decode the primary item.
pub const AVIF_DECODER_SOURCE_AUTO: avifDecoderSource = 0;

/// Use the primary item and the aux (alpha) item in the avif(s).
/// This is where single-image avifs store their image.
pub const AVIF_DECODER_SOURCE_PRIMARY_ITEM: avifDecoderSource = 1;

/// Use the chunks inside primary/aux tracks in the moov block.
/// This is where avifs image sequences store their images.
pub const AVIF_DECODER_SOURCE_TRACKS: avifDecoderSource = 2;

// Decode the thumbnail item. Currently unimplemented.
// pub const AVIF_DECODER_SOURCE_THUMBNAIL_ITEM

#[allow(non_camel_case_types)]
pub type avifRange = __enum;

pub const AVIF_RANGE_LIMITED: avifRange = 0;
pub const AVIF_RANGE_FULL: avifRange = 1;

#[allow(non_camel_case_types)]
pub type avifPixelFormat = __enum;

pub const AVIF_PIXEL_FORMAT_NONE: avifPixelFormat = 0;
pub const AVIF_PIXEL_FORMAT_YUV444: avifPixelFormat = 1;
pub const AVIF_PIXEL_FORMAT_YUV422: avifPixelFormat = 2;
pub const AVIF_PIXEL_FORMAT_YUV420: avifPixelFormat = 3;
pub const AVIF_PIXEL_FORMAT_YV12: avifPixelFormat = 4;

#[allow(non_camel_case_types)]
pub type avifRGBFormat = __enum;

pub const AVIF_RGB_FORMAT_RGB: avifRGBFormat = 0;
pub const AVIF_RGB_FORMAT_RGBA: avifRGBFormat = 1;
pub const AVIF_RGB_FORMAT_ARGB: avifRGBFormat = 2;
pub const AVIF_RGB_FORMAT_BGR: avifRGBFormat = 3;
pub const AVIF_RGB_FORMAT_BGRA: avifRGBFormat = 4;
pub const AVIF_RGB_FORMAT_ABGR: avifRGBFormat = 5;

#[allow(non_camel_case_types)]
pub type avifCodecChoice = __enum;
pub const AVIF_CODEC_CHOICE_AUTO: avifCodecChoice = 0;
pub const AVIF_CODEC_CHOICE_AOM: avifCodecChoice = 1;
pub const AVIF_CODEC_CHOICE_DAV1D: avifCodecChoice = 2;
pub const AVIF_CODEC_CHOICE_RAV1E: avifCodecChoice = 4;

#[repr(C)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub struct avifIOStats {
    colorOBUSize: libc::size_t,
    alphaOBUSize: libc::size_t,
}

#[repr(C)]
#[allow(non_snake_case)]
pub struct avifNclxColorProfile {
    pub colourPrimaries: u16,
    pub transferCharacteristics: u16,
    pub matrixCoefficients: u16,
    pub fullRangeFlag: u8,
}

pub const AVIF_QUANTIZER_LOSSLESS: libc::c_int = 0;
pub const AVIF_QUANTIZER_BEST_QUALITY: libc::c_int = 0;
pub const AVIF_QUANTIZER_WORST_QUALITY: libc::c_int = 63;

pub const AVIF_SPEED_DEFAULT: libc::c_int = -1;
pub const AVIF_SPEED_SLOWEST: libc::c_int = 0;
pub const AVIF_SPEED_FASTEST: libc::c_int = 10;

#[repr(C)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]
pub struct avifImage {
    pub width: u32,
    pub height: u32,
    /// all planes must share this depth; if depth>8, all planes are uint16_t internally
    pub depth: u32,

    pub yuvFormat: avifPixelFormat,
    pub yuvRange: avifRange,
    pub yuvPlanes: [*mut u8; AVIF_PLANE_COUNT_YUV],
    pub yuvRowBytes: [u32; AVIF_PLANE_COUNT_YUV],
    pub decoderOwnsYUVPlanes: avifBool,

    pub alphaRange: avifRange,
    pub alphaPlane: *mut u8,
    pub alphaRowBytes: u32,
    pub decoderOwnsAlphaPlane: avifBool,

    pub profileFormat: avifProfileFormat,
    pub icc: avifRWData,
    pub nclx: avifNclxColorProfile,

    // Metadata - set with avifImageSetMetadata*() before write, check .size>0 for existence after read
    pub exif: avifRWData,
    pub xmp: avifRWData,
}

#[repr(C)]
#[allow(non_camel_case_types)]
#[allow(non_snake_case)]

pub struct avifRGBImage {
    pub width: u32,
    pub height: u32,
    pub depth: u32,
    pub format: avifRGBFormat,

    pub pixels: *mut u8,
    pub rowBytes: u32,
}

impl Default for avifRGBImage {
    fn default() -> Self {
        unsafe { std::mem::zeroed() }
    }
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[allow(non_snake_case)]
pub struct avifEncoderData {
    _private: [u8; 0],
}

#[allow(non_camel_case_types)]
#[repr(C)]
pub struct avifDecoder {
    _private: [u8; 0],
}

#[allow(non_camel_case_types)]
#[repr(C)]
#[allow(non_snake_case)]
pub struct avifEncoder {
    pub codecChoice: avifCodecChoice,

    /// multithreading is disabled if <2)
    pub maxThreads: libc::c_int,
    /// quality
    pub minQuantizer: libc::c_int,
    /// quality
    pub maxQuantizer: libc::c_int,
    // quality
    pub minQuantizerAlpha: libc::c_int,
    // quality
    pub maxQuantizerAlpha: libc::c_int,

    /// range 0-6. Turn off tiling with 0
    pub tileRowsLog2: libc::c_int,
    /// range 0-6. Turn off tiling with 0
    pub tileColsLog2: libc::c_int,

    /// 0-10: 10 should produce a better quality image
    pub speed: libc::c_int,

    /// stats from the most recent write
    pub ioStats: avifIOStats,

    // Internals used by the encoder
    pub data: *mut avifEncoderData,
}

#[allow(non_camel_case_types)]
pub type avifResult = __enum;

pub const AVIF_RESULT_OK: avifResult = 0;
pub const AVIF_RESULT_UNKNOWN_ERROR: avifResult = 1;
pub const AVIF_RESULT_INVALID_FTYP: avifResult = 2;
pub const AVIF_RESULT_NO_CONTENT: avifResult = 3;
pub const AVIF_RESULT_NO_YUV_FORMAT_SELECTED: avifResult = 4;
pub const AVIF_RESULT_REFORMAT_FAILED: avifResult = 5;
pub const AVIF_RESULT_UNSUPPORTED_DEPTH: avifResult = 6;
pub const AVIF_RESULT_ENCODE_COLOR_FAILED: avifResult = 7;
pub const AVIF_RESULT_ENCODE_ALPHA_FAILED: avifResult = 8;
pub const AVIF_RESULT_BMFF_PARSE_FAILED: avifResult = 9;
pub const AVIF_RESULT_NO_AV1_ITEMS_FOUND: avifResult = 10;
pub const AVIF_RESULT_DECODE_COLOR_FAILED: avifResult = 11;
pub const AVIF_RESULT_DECODE_ALPHA_FAILED: avifResult = 12;
pub const AVIF_RESULT_COLOR_ALPHA_SIZE_MISMATCH: avifResult = 13;
pub const AVIF_RESULT_ISPE_SIZE_MISMATCH: avifResult = 14;
pub const AVIF_RESULT_NO_CODEC_AVAILABLE: avifResult = 15;
pub const AVIF_RESULT_NO_IMAGES_REMAINING: avifResult = 16;
pub const AVIF_RESULT_INVALID_EXIF_PAYLOAD: avifResult = 17;

#[repr(C)]
#[allow(non_camel_case_types)]
pub struct avifROData {
    pub data: *const u8,
    pub size: libc::size_t,
}
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct avifRWData {
    pub data: *mut u8,
    pub size: libc::size_t,
}

impl Default for avifRWData {
    fn default() -> Self {
        Self {
            data: std::ptr::null_mut(),
            size: 0,
        }
    }
}

#[link(name = "avif", kind = "static")]
extern "C" {
    pub fn avifImageCreateEmpty() -> *mut avifImage;
    pub fn avifImageCreate(
        width: libc::c_int,
        height: libc::c_int,
        depth: libc::c_int,
        yuvFormat: avifPixelFormat,
    ) -> *mut avifImage;
    pub fn avifImageDestroy(image: *mut avifImage) -> avifResult;

    pub fn avifEncoderCreate() -> *mut avifEncoder;
    pub fn avifEncoderWrite(
        encoder: *mut avifEncoder,
        image: *mut avifImage,
        output: *mut avifRWData,
    ) -> avifResult;
    pub fn avifEncoderDestroy(encoder: *mut avifEncoder);

    pub fn avifDecoderCreate() -> *mut avifDecoder;
    pub fn avifDecoderDestroy(decoder: *mut avifDecoder) -> avifResult;
    pub fn avifDecoderRead(
        decoder: *mut avifDecoder,
        image: *mut avifImage,
        data: *mut avifROData,
    ) -> avifResult;
    pub fn avifDecoderSetSource(decoder: *mut avifDecoder, source: avifDecoderSource)
        -> avifResult;
    pub fn avifDecoderParse(decoder: *mut avifDecoder, input: *mut avifROData) -> avifResult;
    pub fn avifDecoderNextImage(decoder: *mut avifDecoder) -> avifResult;
    pub fn avifDecoderNthImage(decoder: *mut avifDecoder, frameIndex: u32) -> avifResult;
    pub fn avifDecoderReset(decoder: *mut avifDecoder) -> avifResult;

    pub fn avifRWDataFree(raw: *mut avifRWData);

    pub fn avifRGBImageSetDefaults(rgb: *mut avifRGBImage, image: *mut avifImage);
    pub fn avifRGBImageAllocatePixels(rgb: *mut avifRGBImage);
    pub fn avifRGBImageFreePixels(rgb: *mut avifRGBImage);

    pub fn avifImageYUVToRGB(image: *mut avifImage, rgb: *mut avifRGBImage) -> avifResult;
    pub fn avifImageRGBToYUV(image: *mut avifImage, rgb: *mut avifRGBImage) -> avifResult;

    pub fn avifImageAllocatePlanes(image: *mut avifImage, planes: u32); // Ignores any pre-existing planes
    pub fn avifImageFreePlanes(image: *mut avifImage, planes: u32); // Ignores any pre-existing planes

    pub fn avifImageSetProfileNCLX(image: *mut avifImage, nclx: *mut avifNclxColorProfile);

    pub fn avifVersion() -> *const libc::c_char;
    pub fn avifCodecVersions(outBuffer: *mut libc::c_char);

    /// Returns AVIF_TRUE if input begins with a valid FileTypeBox (ftyp) that supports
    /// either the brand 'avif' or 'avis' (or both), without performing any allocations.
    pub fn avifPeekCompatibleFileType(input: *const avifROData) -> avifBool;
}