Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 1 | // Copyright 2020 Joe Drago. All rights reserved. |
| 2 | // SPDX-License-Identifier: BSD-2-Clause |
| 3 | |
| 4 | #include "avif/avif.h" |
| 5 | |
| 6 | #include <inttypes.h> |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | int 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 |
Yannis Guyon | f4a9638 | 2023-02-14 09:41:14 +0100 | [diff] [blame] | 29 | if (!image) { |
| 30 | fprintf(stderr, "Out of memory\n"); |
| 31 | goto cleanup; |
| 32 | } |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 33 | // Configure image here: (see avif/avif.h) |
| 34 | // * colorPrimaries |
| 35 | // * transferCharacteristics |
| 36 | // * matrixCoefficients |
| 37 | // * avifImageSetProfileICC() |
Wan-Teh Chang | b3e0f31 | 2022-10-10 14:09:44 -0700 | [diff] [blame] | 38 | // * avifImageSetMetadataExif() |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 39 | // * avifImageSetMetadataXMP() |
| 40 | // * yuvRange |
Wan-Teh Chang | 53adb6a | 2021-02-20 16:58:43 -0800 | [diff] [blame] | 41 | // * alphaPremultiplied |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 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 | |
Yannis Guyon | 1ce971e | 2022-08-01 20:22:04 +0200 | [diff] [blame] | 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 | } |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 53 | |
| 54 | // Fill your YUV(A) data here |
| 55 | memset(image->yuvPlanes[AVIF_CHAN_Y], 255, image->yuvRowBytes[AVIF_CHAN_Y] * image->height); |
| 56 | memset(image->yuvPlanes[AVIF_CHAN_U], 128, image->yuvRowBytes[AVIF_CHAN_U] * image->height); |
| 57 | memset(image->yuvPlanes[AVIF_CHAN_V], 128, image->yuvRowBytes[AVIF_CHAN_V] * image->height); |
| 58 | memset(image->alphaPlane, 255, image->alphaRowBytes * image->height); |
| 59 | } else { |
| 60 | // If you have RGB(A) data you want to encode, use this path |
| 61 | printf("Encoding from converted RGBA\n"); |
| 62 | |
| 63 | avifRGBImageSetDefaults(&rgb, image); |
Yannis Guyon | b3d441a | 2022-09-18 12:35:03 +0200 | [diff] [blame] | 64 | // Override RGB(A)->YUV(A) defaults here: |
| 65 | // depth, format, chromaDownsampling, avoidLibYUV, ignoreAlpha, alphaPremultiplied, etc. |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 66 | |
| 67 | // Alternative: set rgb.pixels and rgb.rowBytes yourself, which should match your chosen rgb.format |
| 68 | // Be sure to use uint16_t* instead of uint8_t* for rgb.pixels/rgb.rowBytes if (rgb.depth > 8) |
Yannis Guyon | d48836b | 2023-04-01 16:09:20 +0200 | [diff] [blame] | 69 | avifResult allocationResult = avifRGBImageAllocatePixels(&rgb); |
| 70 | if (allocationResult != AVIF_RESULT_OK) { |
| 71 | fprintf(stderr, "Allocation of RGB samples failed: %s\n", avifResultToString(allocationResult)); |
| 72 | goto cleanup; |
| 73 | } |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 74 | |
| 75 | // Fill your RGB(A) data here |
| 76 | memset(rgb.pixels, 255, rgb.rowBytes * image->height); |
| 77 | |
Yannis Guyon | b3d441a | 2022-09-18 12:35:03 +0200 | [diff] [blame] | 78 | avifResult convertResult = avifImageRGBToYUV(image, &rgb); |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 79 | if (convertResult != AVIF_RESULT_OK) { |
| 80 | fprintf(stderr, "Failed to convert to YUV(A): %s\n", avifResultToString(convertResult)); |
| 81 | goto cleanup; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | encoder = avifEncoderCreate(); |
Yannis Guyon | f4a9638 | 2023-02-14 09:41:14 +0100 | [diff] [blame] | 86 | if (!encoder) { |
| 87 | fprintf(stderr, "Out of memory\n"); |
| 88 | goto cleanup; |
| 89 | } |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 90 | // Configure your encoder here (see avif/avif.h): |
| 91 | // * maxThreads |
Wan-Teh Chang | 570c42c | 2022-11-29 15:42:19 -0800 | [diff] [blame] | 92 | // * quality |
| 93 | // * qualityAlpha |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 94 | // * tileRowsLog2 |
| 95 | // * tileColsLog2 |
| 96 | // * speed |
| 97 | // * keyframeInterval |
| 98 | // * timescale |
Wan-Teh Chang | 570c42c | 2022-11-29 15:42:19 -0800 | [diff] [blame] | 99 | encoder->quality = 60; |
| 100 | encoder->qualityAlpha = AVIF_QUALITY_LOSSLESS; |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 101 | |
| 102 | // Call avifEncoderAddImage() for each image in your sequence |
| 103 | // Only set AVIF_ADD_IMAGE_FLAG_SINGLE if you're not encoding a sequence |
Joe Drago | 5559185 | 2020-12-11 14:29:19 -0800 | [diff] [blame] | 104 | // Use avifEncoderAddImageGrid() instead with an array of avifImage* to make a grid image |
Joe Drago | e59e0a8 | 2020-10-30 17:32:31 -0700 | [diff] [blame] | 105 | avifResult addImageResult = avifEncoderAddImage(encoder, image, 1, AVIF_ADD_IMAGE_FLAG_SINGLE); |
| 106 | if (addImageResult != AVIF_RESULT_OK) { |
| 107 | fprintf(stderr, "Failed to add image to encoder: %s\n", avifResultToString(addImageResult)); |
| 108 | goto cleanup; |
| 109 | } |
| 110 | |
| 111 | avifResult finishResult = avifEncoderFinish(encoder, &avifOutput); |
| 112 | if (finishResult != AVIF_RESULT_OK) { |
| 113 | fprintf(stderr, "Failed to finish encode: %s\n", avifResultToString(finishResult)); |
| 114 | goto cleanup; |
| 115 | } |
| 116 | |
| 117 | printf("Encode success: %zu total bytes\n", avifOutput.size); |
| 118 | |
| 119 | FILE * f = fopen(outputFilename, "wb"); |
| 120 | size_t bytesWritten = fwrite(avifOutput.data, 1, avifOutput.size, f); |
| 121 | fclose(f); |
| 122 | if (bytesWritten != avifOutput.size) { |
| 123 | fprintf(stderr, "Failed to write %zu bytes\n", avifOutput.size); |
| 124 | goto cleanup; |
| 125 | } |
| 126 | printf("Wrote: %s\n", outputFilename); |
| 127 | |
| 128 | returnCode = 0; |
| 129 | cleanup: |
| 130 | if (image) { |
| 131 | avifImageDestroy(image); |
| 132 | } |
| 133 | if (encoder) { |
| 134 | avifEncoderDestroy(encoder); |
| 135 | } |
| 136 | avifRWDataFree(&avifOutput); |
| 137 | avifRGBImageFreePixels(&rgb); // Only use in conjunction with avifRGBImageAllocatePixels() |
| 138 | return returnCode; |
| 139 | } |