blob: ae1ecaf493f06833567d486171cfa583ed8d70ed [file] [log] [blame]
Joe Drago444f0512019-01-23 17:03:24 -08001// Copyright 2019 Joe Drago. All rights reserved.
2// SPDX-License-Identifier: BSD-2-Clause
3
4#ifndef AVIF_INTERNAL_H
5#define AVIF_INTERNAL_H
6
7#include "avif/avif.h"
8
Joe Dragoc0d3d662019-04-12 14:10:16 -07009#ifdef __cplusplus
10extern "C" {
11#endif
12
Joe Drago444f0512019-01-23 17:03:24 -080013// Yes, clamp macros are nasty. Do not use them.
Joe Drago7a9a6612019-07-17 11:21:24 -070014#define AVIF_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
Joe Drago444f0512019-01-23 17:03:24 -080015
16// Used by stream related things.
Joe Drago7a9a6612019-07-17 11:21:24 -070017#define CHECK(A) \
Joe Drago97b071c2019-07-17 14:24:56 -070018 if (!(A)) \
19 return AVIF_FALSE;
Joe Drago444f0512019-01-23 17:03:24 -080020
21// ---------------------------------------------------------------------------
Joe Dragocd1e4c32019-02-08 11:26:31 -080022// URNs
23
24#define URN_ALPHA0 "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"
25#define URN_ALPHA1 "urn:mpeg:hevc:2015:auxid:1"
26
27// ---------------------------------------------------------------------------
Joe Drago444f0512019-01-23 17:03:24 -080028// Utils
29
30float avifRoundf(float v);
31
32uint16_t avifHTONS(uint16_t s);
33uint16_t avifNTOHS(uint16_t s);
34uint32_t avifHTONL(uint32_t l);
35uint32_t avifNTOHL(uint32_t l);
36uint64_t avifHTON64(uint64_t l);
37uint64_t avifNTOH64(uint64_t l);
38
Joe Dragocd5b93b2019-04-18 11:19:54 -070039int avifFullToLimitedY(int depth, int v);
40int avifFullToLimitedUV(int depth, int v);
41int avifLimitedToFullY(int depth, int v);
42int avifLimitedToFullUV(int depth, int v);
Joe Drago7efd8032019-02-08 14:49:15 -080043
Joe Drago678b9382019-02-09 03:17:47 -080044void avifCalcYUVCoefficients(avifImage * image, float * outR, float * outG, float * outB);
45
Joe Drago05559c92019-07-17 16:33:38 -070046#define AVIF_ARRAY_DECLARE(TYPENAME, ITEMSTYPE, ITEMSNAME) \
47 typedef struct TYPENAME \
48 { \
49 ITEMSTYPE * ITEMSNAME; \
50 uint32_t elementSize; \
51 uint32_t count; \
52 uint32_t capacity; \
Joe Drago399df4f2019-07-23 16:45:14 -070053 } TYPENAME
Joe Drago05559c92019-07-17 16:33:38 -070054void avifArrayCreate(void * arrayStruct, uint32_t elementSize, uint32_t initialCapacity);
55uint32_t avifArrayPushIndex(void * arrayStruct);
56void * avifArrayPushPtr(void * arrayStruct);
57void avifArrayPush(void * arrayStruct, void * element);
58void avifArrayDestroy(void * arrayStruct);
59
Joe Drago46ea0582019-07-22 15:55:47 -070060AVIF_ARRAY_DECLARE(avifRawDataArray, avifRawData, raw);
61
62// Used internally by avifDecoderNextImage() when there is limited range alpha
63void avifImageCopyDecoderAlpha(avifImage * image);
64
Joe Drago444f0512019-01-23 17:03:24 -080065// ---------------------------------------------------------------------------
66// Memory management
67
68void * avifAlloc(size_t size);
69void avifFree(void * p);
70
71// ---------------------------------------------------------------------------
Joe Drago46ea0582019-07-22 15:55:47 -070072// avifCodecDecodeInput
73
74typedef struct avifCodecDecodeInput
75{
76 avifRawDataArray samples;
77 avifBool alpha; // if true, this is decoding an alpha plane
78} avifCodecDecodeInput;
79
Joe Drago399df4f2019-07-23 16:45:14 -070080avifCodecDecodeInput * avifCodecDecodeInputCreate(void);
Joe Drago46ea0582019-07-22 15:55:47 -070081void avifCodecDecodeInputDestroy(avifCodecDecodeInput * decodeInput);
82
83// ---------------------------------------------------------------------------
Joe Drago33f1d362019-02-13 16:46:22 -080084// avifCodec (abstraction layer to use different AV1 implementations)
85
Joe Drago15857972019-02-13 17:48:28 -080086typedef struct avifCodecConfigurationBox
87{
88 // [skipped; is constant] unsigned int (1)marker = 1;
89 // [skipped; is constant] unsigned int (7)version = 1;
90
91 uint8_t seqProfile; // unsigned int (3) seq_profile;
92 uint8_t seqLevelIdx0; // unsigned int (5) seq_level_idx_0;
93 uint8_t seqTier0; // unsigned int (1) seq_tier_0;
94 uint8_t highBitdepth; // unsigned int (1) high_bitdepth;
95 uint8_t twelveBit; // unsigned int (1) twelve_bit;
96 uint8_t monochrome; // unsigned int (1) monochrome;
97 uint8_t chromaSubsamplingX; // unsigned int (1) chroma_subsampling_x;
98 uint8_t chromaSubsamplingY; // unsigned int (1) chroma_subsampling_y;
99 uint8_t chromaSamplePosition; // unsigned int (2) chroma_sample_position;
100
101 // unsigned int (3)reserved = 0;
102 // unsigned int (1)initial_presentation_delay_present;
103 // if (initial_presentation_delay_present) {
104 // unsigned int (4)initial_presentation_delay_minus_one;
105 // } else {
106 // unsigned int (4)reserved = 0;
107 // }
108} avifCodecConfigurationBox;
109
Joe Drago33f1d362019-02-13 16:46:22 -0800110typedef enum avifCodecPlanes
111{
112 AVIF_CODEC_PLANES_COLOR = 0, // YUV
113 AVIF_CODEC_PLANES_ALPHA,
114
115 AVIF_CODEC_PLANES_COUNT
116} avifCodecPlanes;
117
Joe Drago3533b972019-07-12 14:32:20 -0700118struct avifCodec;
Joe Drago33f1d362019-02-13 16:46:22 -0800119struct avifCodecInternal;
120
Joe Drago46ea0582019-07-22 15:55:47 -0700121typedef avifBool (*avifCodecDecodeFunc)(struct avifCodec * codec);
Joe Drago97b071c2019-07-17 14:24:56 -0700122// avifCodecAlphaLimitedRangeFunc: returns AVIF_TRUE if an alpha plane exists and was encoded with limited range
123typedef avifBool (*avifCodecAlphaLimitedRangeFunc)(struct avifCodec * codec);
Joe Drago46ea0582019-07-22 15:55:47 -0700124typedef avifBool (*avifCodecGetNextImageFunc)(struct avifCodec * codec, avifImage * image);
Joe Drago97b071c2019-07-17 14:24:56 -0700125// avifCodecEncodeImageFunc: if either OBU* is null, skip its encode. alpha should always be lossless
Joe Drago46ea0582019-07-22 15:55:47 -0700126typedef avifBool (*avifCodecEncodeImageFunc)(struct avifCodec * codec, avifImage * image, avifEncoder * encoder, avifRawData * obu, avifBool alpha);
127typedef void (*avifCodecGetConfigurationBoxFunc)(struct avifCodec * codec, avifCodecConfigurationBox * outConfig);
Joe Drago7a9a6612019-07-17 11:21:24 -0700128typedef void (*avifCodecDestroyInternalFunc)(struct avifCodec * codec);
Joe Drago3533b972019-07-12 14:32:20 -0700129
Joe Drago33f1d362019-02-13 16:46:22 -0800130typedef struct avifCodec
131{
Joe Drago46ea0582019-07-22 15:55:47 -0700132 avifCodecDecodeInput * decodeInput;
Joe Drago33f1d362019-02-13 16:46:22 -0800133 struct avifCodecInternal * internal; // up to each codec to use how it wants
Joe Drago3533b972019-07-12 14:32:20 -0700134
135 avifCodecDecodeFunc decode;
Joe Drago3533b972019-07-12 14:32:20 -0700136 avifCodecAlphaLimitedRangeFunc alphaLimitedRange;
Joe Drago46ea0582019-07-22 15:55:47 -0700137 avifCodecGetNextImageFunc getNextImage;
Joe Drago3533b972019-07-12 14:32:20 -0700138 avifCodecEncodeImageFunc encodeImage;
139 avifCodecGetConfigurationBoxFunc getConfigurationBox;
140 avifCodecDestroyInternalFunc destroyInternal;
Joe Drago33f1d362019-02-13 16:46:22 -0800141} avifCodec;
142
Joe Drago399df4f2019-07-23 16:45:14 -0700143avifCodec * avifCodecCreateAOM(void); // requires AVIF_CODEC_AOM
144avifCodec * avifCodecCreateDav1d(void); // requires AVIF_CODEC_DAV1D
Joe Drago33f1d362019-02-13 16:46:22 -0800145void avifCodecDestroy(avifCodec * codec);
146
Joe Drago33f1d362019-02-13 16:46:22 -0800147// ---------------------------------------------------------------------------
Joe Drago444f0512019-01-23 17:03:24 -0800148// avifStream
149
150typedef size_t avifBoxMarker;
151
152typedef struct avifStream
153{
154 avifRawData * raw;
155 size_t offset;
156} avifStream;
157
Joe Drago8f7a3002019-02-07 19:35:37 -0800158typedef struct avifBoxHeader
159{
160 size_t size;
161 uint8_t type[4];
162} avifBoxHeader;
163
164uint8_t * avifStreamCurrent(avifStream * stream);
165
Joe Drago444f0512019-01-23 17:03:24 -0800166void avifStreamStart(avifStream * stream, avifRawData * raw);
167
168// Read
169avifBool avifStreamHasBytesLeft(avifStream * stream, size_t byteCount);
Joe Drago8f7a3002019-02-07 19:35:37 -0800170size_t avifStreamRemainingBytes(avifStream * stream);
Joe Dragoeb652d82019-04-23 16:29:07 -0700171size_t avifStreamOffset(avifStream * stream);
172void avifStreamSetOffset(avifStream * stream, size_t offset);
Joe Drago444f0512019-01-23 17:03:24 -0800173avifBool avifStreamSkip(avifStream * stream, size_t byteCount);
174avifBool avifStreamRead(avifStream * stream, uint8_t * data, size_t size);
175avifBool avifStreamReadU16(avifStream * stream, uint16_t * v);
176avifBool avifStreamReadU32(avifStream * stream, uint32_t * v);
177avifBool avifStreamReadUX8(avifStream * stream, uint64_t * v, uint64_t factor); // Reads a factor*8 sized uint, saves in v
178avifBool avifStreamReadU64(avifStream * stream, uint64_t * v);
Joe Dragocd1e4c32019-02-08 11:26:31 -0800179avifBool avifStreamReadString(avifStream * stream, char * output, size_t outputSize);
Joe Drago8f7a3002019-02-07 19:35:37 -0800180avifBool avifStreamReadBoxHeader(avifStream * stream, avifBoxHeader * header);
181avifBool avifStreamReadVersionAndFlags(avifStream * stream, uint8_t * version, uint8_t * flags); // flags is an optional uint8_t[3]
182avifBool avifStreamReadAndEnforceVersion(avifStream * stream, uint8_t enforcedVersion); // currently discards flags
Joe Drago444f0512019-01-23 17:03:24 -0800183
184// Write
185void avifStreamFinishWrite(avifStream * stream);
186void avifStreamWrite(avifStream * stream, const uint8_t * data, size_t size);
Joe Dragoea252af2019-02-12 12:08:38 -0800187void avifStreamWriteChars(avifStream * stream, const char * chars, size_t size);
Joe Drago444f0512019-01-23 17:03:24 -0800188avifBoxMarker avifStreamWriteBox(avifStream * stream, const char * type, int version /* -1 for "not a FullBox" */, size_t contentSize);
189void avifStreamFinishBox(avifStream * stream, avifBoxMarker marker);
Joe Dragoc36192a2019-01-23 18:50:25 -0800190void avifStreamWriteU8(avifStream * stream, uint8_t v);
Joe Drago444f0512019-01-23 17:03:24 -0800191void avifStreamWriteU16(avifStream * stream, uint16_t v);
192void avifStreamWriteU32(avifStream * stream, uint32_t v);
193void avifStreamWriteZeros(avifStream * stream, size_t byteCount);
194
Joe Dragoc0d3d662019-04-12 14:10:16 -0700195#ifdef __cplusplus
196} // extern "C"
197#endif
198
Joe Drago444f0512019-01-23 17:03:24 -0800199#endif // ifndef AVIF_INTERNAL_H