|
| 1 | +/* IIP AVIF Compressor Class: |
| 2 | + Handles alpha channels, ICC profiles and XMP metadata |
| 3 | +
|
| 4 | + Copyright (C) 2024 Ruven Pillay |
| 5 | +
|
| 6 | + This program is free software; you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation; either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program; if not, write to the Free Software Foundation, |
| 18 | + Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +
|
| 20 | +*/ |
| 21 | + |
| 22 | + |
| 23 | +#include "AVIFCompressor.h" |
| 24 | + |
| 25 | +#if HAVE_STL_THREAD |
| 26 | +#include <thread> |
| 27 | +#endif |
| 28 | + |
| 29 | +using namespace std; |
| 30 | + |
| 31 | + |
| 32 | +/// Initialize chunk-based encoding for the CVT handler |
| 33 | +void AVIFCompressor::InitCompression( const RawTile& rawtile, unsigned int strip_height ){ |
| 34 | + |
| 35 | + // Manually set up the correct width and height for this particular tile and point to the existing data buffer |
| 36 | + tile.width = rawtile.width; |
| 37 | + tile.height = rawtile.height; |
| 38 | + tile.channels = rawtile.channels; |
| 39 | + tile.bpc = rawtile.bpc; |
| 40 | + tile.data = rawtile.data; |
| 41 | + tile.dataLength = rawtile.dataLength; |
| 42 | + tile.capacity = rawtile.capacity; |
| 43 | + tile.memoryManaged = 0; // We don't want the RawTile destructor to free this memory |
| 44 | + |
| 45 | + // libavif cannot handle strip or region-based encoding, so compress the entire image in one go |
| 46 | + this->Compress( tile ); |
| 47 | + |
| 48 | + current_chunk = 0; |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +/// libwebp cannot handle line or region-based encoding, so simulate strip-based output using byte chunks |
| 54 | +unsigned int AVIFCompressor::CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height ){ |
| 55 | + |
| 56 | + // Initialize our chunk size only once at the start of the sequence |
| 57 | + if( current_chunk == 0 ) chunk_size = (unsigned int)( ( (tile.dataLength*tile_height) + (tile_height/2) ) / tile.height ); |
| 58 | + |
| 59 | + // Make sure we don't over-run our allocated memory |
| 60 | + if( (current_chunk + chunk_size) > (tile.dataLength - 1) ) chunk_size = tile.dataLength - current_chunk; |
| 61 | + |
| 62 | + // Copy our chunk of data to the given output buffer |
| 63 | + if( chunk_size > 0 ){ |
| 64 | + unsigned char* data = (unsigned char*) tile.data; |
| 65 | + memcpy( output, &data[current_chunk], chunk_size ); |
| 66 | + current_chunk += chunk_size; |
| 67 | + } |
| 68 | + |
| 69 | + return chunk_size; |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +unsigned int AVIFCompressor::Finish( unsigned char* output ){ |
| 75 | + |
| 76 | + // Output any remaining bytes |
| 77 | + if( current_chunk < tile.dataLength-1 ){ |
| 78 | + unsigned char* data = (unsigned char*) tile.data; |
| 79 | + chunk_size = tile.dataLength - current_chunk - 1; |
| 80 | + memcpy( output, &data[current_chunk], chunk_size ); |
| 81 | + return chunk_size; |
| 82 | + } |
| 83 | + |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +/// Compress a single tile of data |
| 90 | +unsigned int AVIFCompressor::Compress( RawTile& rawtile ){ |
| 91 | + |
| 92 | + avifResult OK; |
| 93 | + avifRWData output = AVIF_DATA_EMPTY; |
| 94 | + |
| 95 | + // Initialize image structure |
| 96 | + avifPixelFormat format = AVIF_PIXEL_FORMAT_YUV420; |
| 97 | + |
| 98 | +#if AVIF_VERSION_MAJOR >= 1 |
| 99 | + // Use full 4:4:4 sampling for lossless |
| 100 | + if( this->Q == -1 ) format = AVIF_PIXEL_FORMAT_YUV444; |
| 101 | +#endif |
| 102 | + |
| 103 | + if( rawtile.channels == 1 ) format = AVIF_PIXEL_FORMAT_YUV400; |
| 104 | + |
| 105 | + // Create our image structure |
| 106 | + avif = avifImageCreate( (uint32_t) rawtile.width, (uint32_t) rawtile.height, (uint32_t) rawtile.bpc, format ); |
| 107 | + if( !avif ){ |
| 108 | + throw string( "AVIFCompressor :: avifImageCreate() error" ); |
| 109 | + } |
| 110 | + |
| 111 | + avifRGBImage rgb; |
| 112 | + avifRGBImageSetDefaults( &rgb, avif ); |
| 113 | + |
| 114 | + |
| 115 | + // Set channel layout |
| 116 | + rgb.format = (rawtile.channels==4) ? AVIF_RGB_FORMAT_RGBA : AVIF_RGB_FORMAT_RGB; |
| 117 | + |
| 118 | + |
| 119 | + // Monochrome single band input not directly supported - duplicate to 3 identical bands |
| 120 | + if( rawtile.channels == 1 ) rawtile.triplicate(); |
| 121 | + |
| 122 | +#if AVIF_VERSION_MAJOR >= 1 |
| 123 | + rgb.chromaDownsampling = AVIF_CHROMA_DOWNSAMPLING_FASTEST; |
| 124 | +#endif |
| 125 | + rgb.rowBytes = rawtile.width * rawtile.channels * (rawtile.bpc/8); |
| 126 | + rgb.pixels = (uint8_t*) rawtile.data; // rgb.pixels type is uint8_t even for 10 bit AVIF |
| 127 | + |
| 128 | + |
| 129 | + // Initialize encoder |
| 130 | + encoder = avifEncoderCreate(); |
| 131 | + if( !encoder ){ |
| 132 | + throw string( "AVIFCompressor :: avifEncoderCreate() error" ); |
| 133 | + } |
| 134 | + |
| 135 | + // Set our encoder options |
| 136 | + encoder->codecChoice = this->codec; |
| 137 | + encoder->speed = AVIF_SPEED_FASTEST; |
| 138 | + |
| 139 | + |
| 140 | + // Auto-tiling and Quality parameter only exists in version 1 onwards |
| 141 | +#if AVIF_VERSION_MAJOR >= 1 |
| 142 | + encoder->autoTiling = true; |
| 143 | + if( this->Q == -1 ){ |
| 144 | + encoder->quality = AVIF_QUALITY_LOSSLESS; |
| 145 | + encoder->maxQuantizer = AVIF_QUANTIZER_LOSSLESS; |
| 146 | + } |
| 147 | + else encoder->quality = this->Q; |
| 148 | +#else |
| 149 | + encoder->maxQuantizer = AVIF_QUANTIZER_WORST_QUALITY; |
| 150 | +#endif |
| 151 | + |
| 152 | + // Set threading concurrency |
| 153 | +#if AVIF_VERSION_MAJOR >= 1 && HAVE_STL_THREAD |
| 154 | + rgb.maxThreads = std::thread::hardware_concurrency(); |
| 155 | + encoder->maxThreads = rgb.maxThreads; |
| 156 | +#endif |
| 157 | + |
| 158 | + |
| 159 | + if( (OK=avifImageRGBToYUV( avif, &rgb )) != AVIF_RESULT_OK ){ |
| 160 | + throw string( "Failed to convert to YUV(A) " + string(avifResultToString(OK)) ); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + // Add ICC profile and XMP metadata to our image |
| 165 | + writeICCProfile(); |
| 166 | + writeXMPMetadata(); |
| 167 | + |
| 168 | + |
| 169 | + if( (OK=avifEncoderAddImage( encoder, avif, 1, AVIF_ADD_IMAGE_FLAG_SINGLE )) != AVIF_RESULT_OK ){ |
| 170 | + throw string( "AVIFCompressor :: Failed to add image to encoder: " + string(avifResultToString(OK)) ); |
| 171 | + } |
| 172 | + |
| 173 | + if( (OK=avifEncoderFinish( encoder, &output )) != AVIF_RESULT_OK ){ |
| 174 | + throw string( "AVIFCompressor :: Failed to finish encode: " + string(avifResultToString(OK)) ); |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + // Allocate the appropriate amount of memory if the encoded AVIF is larger than the raw image buffer |
| 179 | + if( output.size > rawtile.capacity ){ |
| 180 | + if( rawtile.memoryManaged ) delete[] (unsigned char*) rawtile.data; |
| 181 | + rawtile.data = new unsigned char[output.size]; |
| 182 | + rawtile.capacity = output.size; |
| 183 | + } |
| 184 | + |
| 185 | + // Copy the encoded data back into our rawtile buffer |
| 186 | + memcpy( rawtile.data, output.data, output.size ); |
| 187 | + |
| 188 | + if( avif ){ |
| 189 | + avifImageDestroy( avif ); |
| 190 | + } |
| 191 | + if( encoder ){ |
| 192 | + avifEncoderDestroy( encoder ); |
| 193 | + } |
| 194 | + |
| 195 | + // Return our compressed tile |
| 196 | + rawtile.dataLength = output.size; |
| 197 | + |
| 198 | + // Free our output structure |
| 199 | + avifRWDataFree( &output ); |
| 200 | + |
| 201 | + rawtile.quality = this->Q; |
| 202 | + rawtile.compressionType = ImageEncoding::AVIF; |
| 203 | + return rawtile.dataLength; |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +/// Write ICC profile |
| 209 | +void AVIFCompressor::writeICCProfile() |
| 210 | +{ |
| 211 | + size_t len = icc.size(); |
| 212 | + if( len == 0 ) return; |
| 213 | + |
| 214 | +#if AVIF_VERSION_MAJOR < 1 |
| 215 | + // No return from version < 1 |
| 216 | + avifImageSetProfileICC( avif, (const uint8_t*) icc.c_str(), len ); |
| 217 | +#else |
| 218 | + if( avifImageSetProfileICC( avif, (const uint8_t*) icc.c_str(), len ) != AVIF_RESULT_OK ){ |
| 219 | + throw string( "AVIFCompressor :: Error adding ICC profile" ); |
| 220 | + } |
| 221 | +#endif |
| 222 | +} |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | +/// Write XMP metadata |
| 227 | +void AVIFCompressor::writeXMPMetadata() |
| 228 | +{ |
| 229 | + size_t len = xmp.size(); |
| 230 | + if( len == 0 ) return; |
| 231 | + |
| 232 | +#if AVIF_VERSION_MAJOR < 1 |
| 233 | + // No return from version < 1 |
| 234 | + avifImageSetMetadataXMP( avif, (const uint8_t*) xmp.c_str(), len ); |
| 235 | +#else |
| 236 | + if( avifImageSetMetadataXMP( avif, (const uint8_t*) xmp.c_str(), len ) != AVIF_RESULT_OK ){ |
| 237 | + throw string( "AVIFCompressor :: Error adding XMP metadata" ); |
| 238 | + } |
| 239 | +#endif |
| 240 | +} |
0 commit comments