MCPcopy Create free account
hub / github.com/AOMediaCodec/libavif / main

Function main

examples/avif_example_encode.c:10–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <string.h>
9
10int main(int argc, char * argv[])
11{
12 if (argc != 3) {
13 fprintf(stderr, "avif_example_encode [encodeYUVDirectly:0/1] [output.avif]\n");
14 return 1;
15 }
16 avifBool encodeYUVDirectly = AVIF_FALSE;
17 if (argv[1][0] == '1') {
18 encodeYUVDirectly = AVIF_TRUE;
19 }
20 const char * outputFilename = argv[2];
21
22 int returnCode = 1;
23 avifEncoder * encoder = NULL;
24 avifRWData avifOutput = AVIF_DATA_EMPTY;
25 avifRGBImage rgb;
26 memset(&rgb, 0, sizeof(rgb));
27
28 avifImage * image = avifImageCreate(128, 128, 8, AVIF_PIXEL_FORMAT_YUV444); // these values dictate what goes into the final AVIF
29 if (!image) {
30 fprintf(stderr, "Out of memory\n");
31 goto cleanup;
32 }
33 // Configure image here: (see avif/avif.h)
34 // * colorPrimaries
35 // * transferCharacteristics
36 // * matrixCoefficients
37 // * avifImageSetProfileICC()
38 // * avifImageSetMetadataExif()
39 // * avifImageSetMetadataXMP()
40 // * yuvRange
41 // * alphaPremultiplied
42 // * transforms (transformFlags, pasp, clap, irot, imir)
43
44 if (encodeYUVDirectly) {
45 // If you have YUV(A) data you want to encode, use this path
46 printf("Encoding raw YUVA data\n");
47
48 const avifResult allocateResult = avifImageAllocatePlanes(image, AVIF_PLANES_ALL);
49 if (allocateResult != AVIF_RESULT_OK) {
50 fprintf(stderr, "Failed to allocate the planes: %s\n", avifResultToString(allocateResult));
51 goto cleanup;
52 }
53
54 // Fill your YUV(A) data here
55 const uint32_t uvHeight = avifImagePlaneHeight(image, AVIF_CHAN_U);
56 memset(image->yuvPlanes[AVIF_CHAN_Y], 255, image->yuvRowBytes[AVIF_CHAN_Y] * image->height);
57 memset(image->yuvPlanes[AVIF_CHAN_U], 128, image->yuvRowBytes[AVIF_CHAN_U] * uvHeight);
58 memset(image->yuvPlanes[AVIF_CHAN_V], 128, image->yuvRowBytes[AVIF_CHAN_V] * uvHeight);
59 memset(image->alphaPlane, 255, image->alphaRowBytes * image->height);
60 } else {
61 // If you have RGB(A) data you want to encode, use this path
62 printf("Encoding from converted RGBA\n");
63
64 avifRGBImageSetDefaults(&rgb, image);
65 // Override RGB(A)->YUV(A) defaults here:
66 // depth, format, chromaDownsampling, avoidLibYUV, ignoreAlpha, alphaPremultiplied, etc.
67

Callers

nothing calls this directly

Calls 14

avifImageCreateFunction · 0.85
avifImageAllocatePlanesFunction · 0.85
avifResultToStringFunction · 0.85
avifImagePlaneHeightFunction · 0.85
avifRGBImageSetDefaultsFunction · 0.85
avifImageRGBToYUVFunction · 0.85
avifEncoderCreateFunction · 0.85
avifEncoderAddImageFunction · 0.85
avifEncoderFinishFunction · 0.85
avifImageDestroyFunction · 0.85
avifEncoderDestroyFunction · 0.85

Tested by

no test coverage detected