| 1 |
/* |
| 2 |
libheif example application "heif". |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2017 Dirk Farin <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <cassert> |
| 28 |
#include <cerrno> |
| 29 |
#include <cstring> |
| 30 |
#include <getopt.h> |
| 31 |
|
| 32 |
#include <fstream> |
| 33 |
#include <iostream> |
| 34 |
#include <iomanip> |
| 35 |
#include <memory> |
| 36 |
#include <algorithm> |
| 37 |
#include <vector> |
| 38 |
#include <string> |
| 39 |
#include <sstream> |
| 40 |
#include <filesystem> |
| 41 |
#include <regex> |
| 42 |
#include <optional> |
| 43 |
|
| 44 |
#include <libheif/heif.h> |
| 45 |
#include <libheif/heif_properties.h> |
| 46 |
#include "libheif/heif_items.h" |
| 47 |
|
| 48 |
#include "heifio/decoder_jpeg.h" |
| 49 |
#include "heifio/decoder_png.h" |
| 50 |
#include "heifio/decoder_tiff.h" |
| 51 |
#include "heifio/decoder_y4m.h" |
| 52 |
|
| 53 |
#include "benchmark.h" |
| 54 |
#include "common.h" |
| 55 |
#include "SAI_datafile.h" |
| 56 |
#include "libheif/api_structs.h" |
| 57 |
#include "libheif/heif_experimental.h" |
| 58 |
#include "libheif/heif_sequences.h" |
| 59 |
#include "libheif/heif_uncompressed.h" |
| 60 |
|
| 61 |
// --- command line parameters |
| 62 |
|
| 63 |
int master_alpha = 1; |
| 64 |
int thumb_alpha = 1; |
| 65 |
int list_encoders = 0; |
| 66 |
int two_colr_boxes = 0; |
| 67 |
int premultiplied_alpha = 0; |
| 68 |
int run_benchmark = 0; |
| 69 |
heif_metadata_compression metadata_compression_method = heif_metadata_compression_off; |
| 70 |
int tiled_input_x_y = 0; |
| 71 |
const char* encoderId = nullptr; |
| 72 |
std::string chroma_downsampling; |
| 73 |
int cut_tiles = 0; |
| 74 |
int tiled_image_width = 0; |
| 75 |
int tiled_image_height = 0; |
| 76 |
std::string tiling_method = "grid"; |
| 77 |
heif_unci_compression unci_compression = heif_unci_compression_brotli; |
| 78 |
int add_pyramid_group = 0; |
| 79 |
|
| 80 |
uint16_t nclx_colour_primaries = 1; |
| 81 |
uint16_t nclx_transfer_characteristic = 13; |
| 82 |
uint16_t nclx_matrix_coefficients = 6; |
| 83 |
int nclx_full_range = true; |
| 84 |
|
| 85 |
std::optional<heif_content_light_level> clli; |
| 86 |
struct pixel_aspect_ratio |
| 87 |
{ |
| 88 |
uint32_t h,v; |
| 89 |
}; |
| 90 |
std::optional<pixel_aspect_ratio> pasp; |
| 91 |
|
| 92 |
// default to 30 fps |
| 93 |
uint32_t sequence_timebase = 30; |
| 94 |
uint32_t sequence_durations = 1; |
| 95 |
uint32_t sequence_repetitions = 1; |
| 96 |
std::string vmt_metadata_file; |
| 97 |
bool binary_metadata_track = false; |
| 98 |
std::string metadata_track_uri = "vmt:metadata"; |
| 99 |
|
| 100 |
int quality = 50; |
| 101 |
bool lossless = false; |
| 102 |
std::string output_filename; |
| 103 |
int logging_level = 0; |
| 104 |
bool option_show_parameters = false; |
| 105 |
int thumbnail_bbox_size = 0; |
| 106 |
int output_bit_depth = 10; |
| 107 |
bool force_enc_av1f = false; |
| 108 |
bool force_enc_avc = false; |
| 109 |
bool force_enc_hevc = false; |
| 110 |
bool force_enc_vvc = false; |
| 111 |
bool force_enc_uncompressed = false; |
| 112 |
bool force_enc_jpeg = false; |
| 113 |
bool force_enc_jpeg2000 = false; |
| 114 |
bool force_enc_htj2k = false; |
| 115 |
bool use_tiling = false; |
| 116 |
bool encode_sequence = false; |
| 117 |
bool use_video_handler = false; |
| 118 |
std::string option_mime_item_type; |
| 119 |
std::string option_mime_item_file; |
| 120 |
std::string option_mime_item_name; |
| 121 |
|
| 122 |
enum heif_sequence_gop_structure sequence_gop_structure = heif_sequence_gop_structure_lowdelay; |
| 123 |
int sequence_keyframe_distance_min = 0; |
| 124 |
int sequence_keyframe_distance_max = 0; |
| 125 |
int sequence_max_frames = 0; // 0 -> no maximum |
| 126 |
std::string option_gimi_track_id; |
| 127 |
std::string option_sai_data_file; |
| 128 |
|
| 129 |
|
| 130 |
enum heif_output_nclx_color_profile_preset |
| 131 |
{ |
| 132 |
heif_output_nclx_color_profile_preset_custom, // Default. Use the values provided by the user. |
| 133 |
heif_output_nclx_color_profile_preset_automatic, // Choose the profile depending on the input. |
| 134 |
heif_output_nclx_color_profile_preset_compatible, // Choose a profile that is decoded correctly by most systems. |
| 135 |
heif_output_nclx_color_profile_preset_Rec_601, // SD / JPEG / MPEG |
| 136 |
heif_output_nclx_color_profile_preset_Rec_709, // HDTV / (sRGB) |
| 137 |
heif_output_nclx_color_profile_preset_Rec_2020 // UHDTV |
| 138 |
}; |
| 139 |
|
| 140 |
heif_output_nclx_color_profile_preset output_color_profile_preset = heif_output_nclx_color_profile_preset_custom; |
| 141 |
|
| 142 |
|
| 143 |
std::string property_pitm_description; |
| 144 |
|
| 145 |
// for benchmarking |
| 146 |
|
| 147 |
#if !defined(_MSC_VER) |
| 148 |
#define HAVE_GETTIMEOFDAY 1 // TODO: should be set by CMake |
| 149 |
#endif |
| 150 |
|
| 151 |
#if HAVE_GETTIMEOFDAY |
| 152 |
#include <sys/time.h> |
| 153 |
timeval time_encoding_start; |
| 154 |
timeval time_encoding_end; |
| 155 |
#endif |
| 156 |
|
| 157 |
const int OPTION_NCLX_MATRIX_COEFFICIENTS = 1000; |
| 158 |
const int OPTION_NCLX_COLOUR_PRIMARIES = 1001; |
| 159 |
const int OPTION_NCLX_TRANSFER_CHARACTERISTIC = 1002; |
| 160 |
const int OPTION_NCLX_FULL_RANGE_FLAG = 1003; |
| 161 |
const int OPTION_PLUGIN_DIRECTORY = 1004; |
| 162 |
const int OPTION_PITM_DESCRIPTION = 1005; |
| 163 |
const int OPTION_USE_JPEG_COMPRESSION = 1006; |
| 164 |
const int OPTION_USE_JPEG2000_COMPRESSION = 1007; |
| 165 |
const int OPTION_VERBOSE = 1008; |
| 166 |
const int OPTION_USE_HTJ2K_COMPRESSION = 1009; |
| 167 |
const int OPTION_USE_VVC_COMPRESSION = 1010; |
| 168 |
const int OPTION_TILED_IMAGE_WIDTH = 1011; |
| 169 |
const int OPTION_TILED_IMAGE_HEIGHT = 1012; |
| 170 |
const int OPTION_TILING_METHOD = 1013; |
| 171 |
const int OPTION_UNCI_COMPRESSION = 1014; |
| 172 |
const int OPTION_CUT_TILES = 1015; |
| 173 |
const int OPTION_SEQUENCES_TIMEBASE = 1016; |
| 174 |
const int OPTION_SEQUENCES_DURATIONS = 1017; |
| 175 |
const int OPTION_SEQUENCES_FPS = 1018; |
| 176 |
const int OPTION_VMT_METADATA_FILE = 1019; |
| 177 |
const int OPTION_SEQUENCES_REPETITIONS = 1020; |
| 178 |
const int OPTION_COLOR_PROFILE_PRESET = 1021; |
| 179 |
const int OPTION_SET_CLLI = 1022; |
| 180 |
const int OPTION_SET_PASP = 1023; |
| 181 |
const int OPTION_SEQUENCES_GOP_STRUCTURE = 1024; |
| 182 |
const int OPTION_SEQUENCES_MIN_KEYFRAME_DISTANCE = 1025; |
| 183 |
const int OPTION_SEQUENCES_MAX_KEYFRAME_DISTANCE = 1026; |
| 184 |
const int OPTION_SEQUENCES_MAX_FRAMES = 1027; |
| 185 |
const int OPTION_USE_AVC_COMPRESSION = 1028; |
| 186 |
const int OPTION_BINARY_METADATA_TRACK = 1029; |
| 187 |
const int OPTION_METADATA_TRACK_URI = 1030; |
| 188 |
const int OPTION_ADD_MIME_ITEM = 1031; |
| 189 |
const int OPTION_MIME_ITEM_FILE = 1032; |
| 190 |
const int OPTION_MIME_ITEM_NAME = 1033; |
| 191 |
const int OPTION_METADATA_COMPRESSION = 1034; |
| 192 |
const int OPTION_SEQUENCES_GIMI_TRACK_ID = 1035; |
| 193 |
const int OPTION_SEQUENCES_SAI_DATA_FILE = 1036; |
| 194 |
const int OPTION_USE_HEVC_COMPRESSION = 1037; |
| 195 |
|
| 196 |
static option long_options[] = { |
| 197 |
{(char* const) "help", no_argument, 0, 'h'}, |
| 198 |
{(char* const) "version", no_argument, 0, 'v'}, |
| 199 |
{(char* const) "quality", required_argument, 0, 'q'}, |
| 200 |
{(char* const) "output", required_argument, 0, 'o'}, |
| 201 |
{(char* const) "lossless", no_argument, 0, 'L'}, |
| 202 |
{(char* const) "thumb", required_argument, 0, 't'}, |
| 203 |
{(char* const) "verbose", no_argument, 0, OPTION_VERBOSE}, |
| 204 |
{(char* const) "params", no_argument, 0, 'P'}, |
| 205 |
{(char* const) "no-alpha", no_argument, &master_alpha, 0}, |
| 206 |
{(char* const) "no-thumb-alpha", no_argument, &thumb_alpha, 0}, |
| 207 |
{(char* const) "list-encoders", no_argument, &list_encoders, 1}, |
| 208 |
{(char* const) "encoder", required_argument, 0, 'e'}, |
| 209 |
{(char* const) "bit-depth", required_argument, 0, 'b'}, |
| 210 |
{(char* const) "even-size", no_argument, 0, 'E'}, |
| 211 |
{(char* const) "avif", no_argument, 0, 'A'}, |
| 212 |
{(char* const) "hevc", no_argument, 0, OPTION_USE_HEVC_COMPRESSION}, |
| 213 |
{(char* const) "vvc", no_argument, 0, OPTION_USE_VVC_COMPRESSION}, |
| 214 |
{(char* const) "avc", no_argument, 0, OPTION_USE_AVC_COMPRESSION}, |
| 215 |
{(char* const) "jpeg", no_argument, 0, OPTION_USE_JPEG_COMPRESSION}, |
| 216 |
{(char* const) "jpeg2000", no_argument, 0, OPTION_USE_JPEG2000_COMPRESSION}, |
| 217 |
{(char* const) "htj2k", no_argument, 0, OPTION_USE_HTJ2K_COMPRESSION}, |
| 218 |
#if WITH_UNCOMPRESSED_CODEC |
| 219 |
{(char* const) "uncompressed", no_argument, 0, 'U'}, |
| 220 |
{(char* const) "unci-compression-method", required_argument, nullptr, OPTION_UNCI_COMPRESSION}, |
| 221 |
#endif |
| 222 |
{(char* const) "color-profile", required_argument, 0, OPTION_COLOR_PROFILE_PRESET}, |
| 223 |
{(char* const) "matrix_coefficients", required_argument, 0, OPTION_NCLX_MATRIX_COEFFICIENTS}, |
| 224 |
{(char* const) "colour_primaries", required_argument, 0, OPTION_NCLX_COLOUR_PRIMARIES}, |
| 225 |
{(char* const) "transfer_characteristic", required_argument, 0, OPTION_NCLX_TRANSFER_CHARACTERISTIC}, |
| 226 |
{(char* const) "full_range_flag", required_argument, 0, OPTION_NCLX_FULL_RANGE_FLAG}, |
| 227 |
{(char* const) "enable-two-colr-boxes", no_argument, &two_colr_boxes, 1}, |
| 228 |
{(char* const) "clli", required_argument, 0, OPTION_SET_CLLI}, |
| 229 |
{(char* const) "pasp", required_argument, 0, OPTION_SET_PASP}, |
| 230 |
{(char* const) "premultiplied-alpha", no_argument, &premultiplied_alpha, 1}, |
| 231 |
{(char* const) "plugin-directory", required_argument, 0, OPTION_PLUGIN_DIRECTORY}, |
| 232 |
{(char* const) "benchmark", no_argument, &run_benchmark, 1}, |
| 233 |
{(char* const) "enable-metadata-compression", required_argument, 0, OPTION_METADATA_COMPRESSION}, |
| 234 |
{(char* const) "pitm-description", required_argument, 0, OPTION_PITM_DESCRIPTION}, |
| 235 |
{(char* const) "chroma-downsampling", required_argument, 0, 'C'}, |
| 236 |
{(char* const) "cut-tiles", required_argument, nullptr, OPTION_CUT_TILES}, |
| 237 |
{(char* const) "tiled-input", no_argument, 0, 'T'}, |
| 238 |
{(char* const) "tiled-image-width", required_argument, nullptr, OPTION_TILED_IMAGE_WIDTH}, |
| 239 |
{(char* const) "tiled-image-height", required_argument, nullptr, OPTION_TILED_IMAGE_HEIGHT}, |
| 240 |
{(char* const) "tiled-input-x-y", no_argument, &tiled_input_x_y, 1}, |
| 241 |
{(char* const) "tiling-method", required_argument, nullptr, OPTION_TILING_METHOD}, |
| 242 |
{(char* const) "add-pyramid-group", no_argument, &add_pyramid_group, 1}, |
| 243 |
{(char* const) "sequence", no_argument, 0, 'S'}, |
| 244 |
{(char* const) "video", no_argument, 0, 'V'}, |
| 245 |
{(char* const) "timebase", required_argument, nullptr, OPTION_SEQUENCES_TIMEBASE}, |
| 246 |
{(char* const) "duration", required_argument, nullptr, OPTION_SEQUENCES_DURATIONS}, |
| 247 |
{(char* const) "fps", required_argument, nullptr, OPTION_SEQUENCES_FPS}, |
| 248 |
{(char* const) "repetitions", required_argument, nullptr, OPTION_SEQUENCES_REPETITIONS}, |
| 249 |
{(char* const) "max-frames", required_argument, nullptr, OPTION_SEQUENCES_MAX_FRAMES}, |
| 250 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 251 |
{(char* const) "vmt-metadata", required_argument, nullptr, OPTION_VMT_METADATA_FILE}, |
| 252 |
{(char* const) "binary-metadata-track", no_argument, nullptr, OPTION_BINARY_METADATA_TRACK}, |
| 253 |
{(char* const) "metadata-track-uri", required_argument, nullptr, OPTION_METADATA_TRACK_URI}, |
| 254 |
{(char* const) "add-mime-item", required_argument, nullptr, OPTION_ADD_MIME_ITEM}, |
| 255 |
{(char* const) "mime-item-file", required_argument, nullptr, OPTION_MIME_ITEM_FILE}, |
| 256 |
{(char* const) "mime-item-name", required_argument, nullptr, OPTION_MIME_ITEM_NAME}, |
| 257 |
#endif |
| 258 |
{(char* const) "gop-structure", required_argument, nullptr, OPTION_SEQUENCES_GOP_STRUCTURE}, |
| 259 |
{(char* const) "min-keyframe-distance", required_argument, nullptr, OPTION_SEQUENCES_MIN_KEYFRAME_DISTANCE}, |
| 260 |
{(char* const) "max-keyframe-distance", required_argument, nullptr, OPTION_SEQUENCES_MAX_KEYFRAME_DISTANCE}, |
| 261 |
{(char* const) "set-gimi-track-id", required_argument, nullptr, OPTION_SEQUENCES_GIMI_TRACK_ID}, |
| 262 |
{(char* const) "sai-data-file", required_argument, nullptr, OPTION_SEQUENCES_SAI_DATA_FILE}, |
| 263 |
{0, 0, 0, 0} |
| 264 |
}; |
| 265 |
|
| 266 |
|
| 267 |
void show_help(const char* argv0) |
| 268 |
{ |
| 269 |
std::filesystem::path p(argv0); |
| 270 |
std::string filename = p.filename().string(); |
| 271 |
|
| 272 |
std::stringstream sstr; |
| 273 |
sstr << " " << filename << " libheif version: " << heif_get_version(); |
| 274 |
|
| 275 |
std::string title = sstr.str(); |
| 276 |
|
| 277 |
std::cerr << title << "\n" |
| 278 |
<< std::string(title.length() + 1, '-') << "\n" |
| 279 |
<< "Usage: " << filename << " [options] <input-image> ...\n" |
| 280 |
<< "\n" |
| 281 |
<< "When specifying multiple source images, they will all be saved into the same HEIF/AVIF file.\n" |
| 282 |
<< "\n" |
| 283 |
<< "Some encoders (x265, aom) let you pass-through any parameters by prefixing them with the encoder name.\n" |
| 284 |
<< "For example, you may pass any x265 parameter by prefixing it with 'x265:'. For example, to set\n" |
| 285 |
<< "the 'ctu' parameter, you will have to set 'x265:ctu' in libheif (e.g.: -p x265:ctu=64).\n" |
| 286 |
<< "Note that when using the prefix, libheif cannot tell you which parameters and values are supported.\n" |
| 287 |
<< "\n" |
| 288 |
<< "Options:\n" |
| 289 |
<< " -h, --help show help\n" |
| 290 |
<< " -v, --version show version\n" |
| 291 |
<< " -o, --output output filename (optional)\n" |
| 292 |
<< " -q, --quality set output quality (0-100) for lossy compression\n" |
| 293 |
<< " -L, --lossless generate lossless output (-q has no effect). Image will be encoded as RGB (matrix_coefficients=0).\n" |
| 294 |
<< " -t, --thumb # generate thumbnail with maximum size # (default: off)\n" |
| 295 |
<< " --no-alpha do not save alpha channel\n" |
| 296 |
<< " --no-thumb-alpha do not save alpha channel in thumbnail image\n" |
| 297 |
<< " --verbose enable logging output (more will increase logging level)\n" |
| 298 |
<< " -b, --bit-depth # number of bits to use from an 16-bit PNG input, valid range: 9-16 (default: 10 bit)\n" |
| 299 |
<< " --premultiplied-alpha input image has premultiplied alpha\n" |
| 300 |
#if WITH_HEADER_COMPRESSION |
| 301 |
<< " --enable-metadata-compression ALGO enable metadata item compression (experimental)\n" |
| 302 |
<< " Choose algorithm from {off"; // TODO: add 'auto', but it currently equals 'off' |
| 303 |
if (heif_metadata_compression_method_supported(heif_metadata_compression_deflate)) { |
| 304 |
std::cerr << ",deflate,zlib"; |
| 305 |
} |
| 306 |
if (heif_metadata_compression_method_supported(heif_metadata_compression_brotli)) { |
| 307 |
std::cerr << ",brotli"; |
| 308 |
} |
| 309 |
std::cerr << "}.\n" |
| 310 |
#endif |
| 311 |
<< " -C, --chroma-downsampling ALGO force chroma downsampling algorithm (nn = nearest-neighbor / average / sharp-yuv)\n" |
| 312 |
<< " (sharp-yuv makes edges look sharper when using YUV420 with bilinear chroma upsampling)\n" |
| 313 |
<< " --benchmark measure encoding time, PSNR, and output file size\n" |
| 314 |
<< " --pitm-description TEXT set user description for primary image (experimental)\n" |
| 315 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 316 |
<< " --add-mime-item TYPE add a mime item of the specified content type (experimental)\n" |
| 317 |
<< " --mime-item-file FILE use the specified FILE as the data to put into the mime item (experimental)\n" |
| 318 |
#endif |
| 319 |
<< "\n" |
| 320 |
<< "codecs:\n" |
| 321 |
<< " -A, --avif encode as AVIF (not needed if output filename with .avif suffix is provided)\n" |
| 322 |
<< " --hevc encode as HEVC (default)\n" |
| 323 |
<< " --vvc encode as VVC (experimental)\n" |
| 324 |
<< " --avc encode as AVC (experimental)\n" |
| 325 |
<< " --jpeg encode as JPEG\n" |
| 326 |
<< " --jpeg2000 encode as JPEG 2000 (experimental)\n" |
| 327 |
<< " --htj2k encode as High Throughput JPEG 2000 (experimental)\n" |
| 328 |
#if WITH_UNCOMPRESSED_CODEC |
| 329 |
<< " -U, --uncompressed encode as uncompressed image (according to ISO 23001-17) (EXPERIMENTAL)\n" |
| 330 |
<< " --unci-compression METHOD choose one of these methods: none, deflate, zlib, brotli.\n" |
| 331 |
#endif |
| 332 |
<< " --list-encoders list all available encoders for all compression formats\n" |
| 333 |
<< " -e, --encoder ID select encoder to use (the IDs can be listed with --list-encoders)\n" |
| 334 |
<< " --plugin-directory DIR load all codec plugins in the directory\n" |
| 335 |
<< " -P, --params show all encoder parameters and exit, input file not required or used.\n" |
| 336 |
<< " -p NAME=VALUE set encoder parameter\n" |
| 337 |
<< "\n" |
| 338 |
<< "color profile:\n" |
| 339 |
<< " --color-profile NAME use a color profile preset for the output. Valid values are:\n" |
| 340 |
<< " custom: (default) use the provided matrix_coefficients, colour_primaries, transfer_characteristic\n" |
| 341 |
<< " auto: automatically guess suitable values from the input image characteristics\n" |
| 342 |
<< " compatible: use a profile that is decoded correctly by most applications by avoiding incomplete implementations\n" |
| 343 |
<< " 601: use Rec.601 (SD), close to JPEG\n" |
| 344 |
<< " 709: use Rec.709 (HDTV), close to sRGB\n" |
| 345 |
<< " 2020: use Rec.2020 (UHDTV), transfer curve will be selected based on bits per pixel\n" |
| 346 |
<< " --matrix_coefficients nclx profile: color conversion matrix coefficients, default=6 (see h.273)\n" |
| 347 |
<< " --colour_primaries nclx profile: color primaries (see h.273)\n" |
| 348 |
<< " --transfer_characteristic nclx profile: transfer characteristics (see h.273)\n" |
| 349 |
<< " --full_range_flag nclx profile: full range flag, default: 1\n" |
| 350 |
<< " --enable-two-colr-boxes will write both an ICC and an nclx color profile if both are present\n" |
| 351 |
<< " --clli MaxCLL,MaxPALL add 'content light level information' property to all encoded images\n" |
| 352 |
<< " --pasp h,v set pixel aspect ratio property to all encoded images\n" |
| 353 |
<< "\n" |
| 354 |
<< "tiling:\n" |
| 355 |
<< " --cut-tiles # cuts the input image into square tiles of the given width\n" |
| 356 |
<< " -T, --tiled-input input is a set of tile images (only provide one filename with two tile position numbers).\n" |
| 357 |
<< " For example, 'tile-01-05.jpg' would be a valid input filename.\n" |
| 358 |
<< " You only have to provide the filename of one tile as input, heif-enc will scan the directory\n" |
| 359 |
<< " for the other tiles and determine the range of tiles automatically.\n" |
| 360 |
<< " --tiled-image-width # override image width of tiled image\n" |
| 361 |
<< " --tiled-image-height # override image height of tiled image\n" |
| 362 |
<< " --tiled-input-x-y usually, the first number in the input tile filename should be the y position.\n" |
| 363 |
<< " With this option, this can be swapped so that the first number is x, the second number y.\n" |
| 364 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES || WITH_UNCOMPRESSED_CODEC |
| 365 |
<< " --tiling-method METHOD choose one of these methods: grid" |
| 366 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 367 |
", tili (experimental)" |
| 368 |
#endif |
| 369 |
#if WITH_UNCOMPRESSED_CODEC |
| 370 |
", unci" |
| 371 |
#endif |
| 372 |
". The default is 'grid'.\n" |
| 373 |
#endif |
| 374 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 375 |
<< " --add-pyramid-group when several images are given, put them into a multi-resolution pyramid group. (experimental)\n" |
| 376 |
#endif |
| 377 |
<< "\n" |
| 378 |
<< "sequences:\n" |
| 379 |
<< " -S, --sequence encode input images as sequence (input filenames with a number will pull in all files with this pattern).\n" |
| 380 |
<< " -V, --video encode as video instead of image sequence\n" |
| 381 |
<< " --timebase # set clock ticks/second for sequence\n" |
| 382 |
<< " --duration # set frame duration (default: 1)\n" |
| 383 |
<< " --fps # set timebase and duration based on fps\n" |
| 384 |
<< " --repetitions # set how often the sequence should be played back (default=1), special value: 'infinite'\n" |
| 385 |
<< " --gop-structure GOP frame types to use in GOP (intra-only, low-delay, unrestricted)\n" |
| 386 |
<< " --min-keyframe-distance # minimum distance of keyframes in sequence (0 = undefined)\n" |
| 387 |
<< " --max-keyframe-distance # maximum distance of keyframes in sequence (0 = undefined)\n" |
| 388 |
<< " --max-frames # limit sequence length to maximum number of frames\n" |
| 389 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 390 |
<< " --vmt-metadata FILE encode metadata track from VMT file (experimental)\n" |
| 391 |
<< " --binary-metadata-track parses VMT data as hex values that are written as raw binary (experimental)\n" |
| 392 |
<< " --metadata-track-uri URI uses the URI identifier for the metadata track (experimental)\n" |
| 393 |
<< " --set-gimi-track-id ID set the GIMI track ID for the visual track (experimental)\n" |
| 394 |
<< " --sai-data-file FILE use the specified FILE as input data for the video frames SAI data\n" |
| 395 |
#endif |
| 396 |
; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
void list_encoder_parameters(heif_encoder* encoder) |
| 401 |
{ |
| 402 |
std::cerr << "Parameters for encoder `" << heif_encoder_get_name(encoder) << "`:\n"; |
| 403 |
|
| 404 |
const heif_encoder_parameter* const* params = heif_encoder_list_parameters(encoder); |
| 405 |
for (int i = 0; params[i]; i++) { |
| 406 |
const char* name = heif_encoder_parameter_get_name(params[i]); |
| 407 |
|
| 408 |
switch (heif_encoder_parameter_get_type(params[i])) { |
| 409 |
case heif_encoder_parameter_type_integer: { |
| 410 |
heif_error error; |
| 411 |
|
| 412 |
std::cerr << " " << name; |
| 413 |
|
| 414 |
if (heif_encoder_has_default(encoder, name)) { |
| 415 |
int value; |
| 416 |
error = heif_encoder_get_parameter_integer(encoder, name, &value); |
| 417 |
(void) error; |
| 418 |
|
| 419 |
std::cerr << ", default=" << value; |
| 420 |
} |
| 421 |
|
| 422 |
int have_minimum, have_maximum, minimum, maximum, num_valid_values; |
| 423 |
const int* valid_values = nullptr; |
| 424 |
error = heif_encoder_parameter_integer_valid_values(encoder, name, |
| 425 |
&have_minimum, &have_maximum, |
| 426 |
&minimum, &maximum, |
| 427 |
&num_valid_values, |
| 428 |
&valid_values); |
| 429 |
|
| 430 |
if (have_minimum || have_maximum) { // TODO: only one is set |
| 431 |
std::cerr << ", [" << minimum << ";" << maximum << "]"; |
| 432 |
} |
| 433 |
|
| 434 |
if (num_valid_values > 0) { |
| 435 |
std::cerr << ", {"; |
| 436 |
|
| 437 |
for (int p = 0; p < num_valid_values; p++) { |
| 438 |
if (p > 0) { |
| 439 |
std::cerr << ", "; |
| 440 |
} |
| 441 |
|
| 442 |
std::cerr << valid_values[p]; |
| 443 |
} |
| 444 |
|
| 445 |
std::cerr << "}"; |
| 446 |
} |
| 447 |
|
| 448 |
std::cerr << "\n"; |
| 449 |
} |
| 450 |
break; |
| 451 |
|
| 452 |
case heif_encoder_parameter_type_boolean: { |
| 453 |
heif_error error; |
| 454 |
std::cerr << " " << name; |
| 455 |
|
| 456 |
if (heif_encoder_has_default(encoder, name)) { |
| 457 |
int value; |
| 458 |
error = heif_encoder_get_parameter_boolean(encoder, name, &value); |
| 459 |
(void) error; |
| 460 |
|
| 461 |
std::cerr << ", default=" << (value ? "true" : "false"); |
| 462 |
} |
| 463 |
|
| 464 |
std::cerr << "\n"; |
| 465 |
} |
| 466 |
break; |
| 467 |
|
| 468 |
case heif_encoder_parameter_type_string: { |
| 469 |
heif_error error; |
| 470 |
std::cerr << " " << name; |
| 471 |
|
| 472 |
if (heif_encoder_has_default(encoder, name)) { |
| 473 |
const int value_size = 50; |
| 474 |
char value[value_size]; |
| 475 |
error = heif_encoder_get_parameter_string(encoder, name, value, value_size); |
| 476 |
(void) error; |
| 477 |
|
| 478 |
std::cerr << ", default=" << value; |
| 479 |
} |
| 480 |
|
| 481 |
const char* const* valid_options; |
| 482 |
error = heif_encoder_parameter_string_valid_values(encoder, name, &valid_options); |
| 483 |
|
| 484 |
if (valid_options) { |
| 485 |
std::cerr << ", { "; |
| 486 |
for (int k = 0; valid_options[k]; k++) { |
| 487 |
if (k > 0) { std::cerr << ","; } |
| 488 |
std::cerr << valid_options[k]; |
| 489 |
} |
| 490 |
std::cerr << " }"; |
| 491 |
} |
| 492 |
|
| 493 |
std::cerr << "\n"; |
| 494 |
} |
| 495 |
break; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
void set_params(heif_encoder* encoder, const std::vector<std::string>& params) |
| 502 |
{ |
| 503 |
for (const std::string& p : params) { |
| 504 |
auto pos = p.find_first_of('='); |
| 505 |
if (pos == std::string::npos || pos == 0 || pos == p.size() - 1) { |
| 506 |
std::cerr << "Encoder parameter must be in the format 'name=value'\n"; |
| 507 |
exit(5); |
| 508 |
} |
| 509 |
|
| 510 |
std::string name = p.substr(0, pos); |
| 511 |
std::string value = p.substr(pos + 1); |
| 512 |
|
| 513 |
struct heif_error error = heif_encoder_set_parameter(encoder, name.c_str(), value.c_str()); |
| 514 |
if (error.code) { |
| 515 |
std::cerr << "Error: " << error.message << "\n"; |
| 516 |
exit(5); |
| 517 |
} |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
static void show_list_of_encoders(const heif_encoder_descriptor* const* encoder_descriptors, |
| 523 |
int count) |
| 524 |
{ |
| 525 |
for (int i = 0; i < count; i++) { |
| 526 |
std::cout << "- " << heif_encoder_descriptor_get_id_name(encoder_descriptors[i]) |
| 527 |
<< " = " |
| 528 |
<< heif_encoder_descriptor_get_name(encoder_descriptors[i]); |
| 529 |
|
| 530 |
if (i == 0) { |
| 531 |
std::cout << " [default]"; |
| 532 |
} |
| 533 |
|
| 534 |
std::cout << "\n"; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
static const char* get_compression_format_name(heif_compression_format format) |
| 540 |
{ |
| 541 |
switch (format) { |
| 542 |
case heif_compression_AV1: |
| 543 |
return "AV1"; |
| 544 |
break; |
| 545 |
case heif_compression_AVC: |
| 546 |
return "AVC"; |
| 547 |
break; |
| 548 |
case heif_compression_VVC: |
| 549 |
return "VVC"; |
| 550 |
break; |
| 551 |
case heif_compression_HEVC: |
| 552 |
return "HEVC"; |
| 553 |
break; |
| 554 |
case heif_compression_JPEG: |
| 555 |
return "JPEG"; |
| 556 |
break; |
| 557 |
case heif_compression_JPEG2000: |
| 558 |
return "JPEG 2000"; |
| 559 |
break; |
| 560 |
case heif_compression_HTJ2K: |
| 561 |
return "HT-J2K"; |
| 562 |
break; |
| 563 |
case heif_compression_uncompressed: |
| 564 |
return "Uncompressed"; |
| 565 |
break; |
| 566 |
default: |
| 567 |
assert(false); |
| 568 |
return "unknown"; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
static void show_list_of_all_encoders() |
| 573 |
{ |
| 574 |
for (auto compression_format: {heif_compression_AVC, heif_compression_AV1, heif_compression_HEVC, |
| 575 |
heif_compression_JPEG, heif_compression_JPEG2000, heif_compression_HTJ2K, |
| 576 |
heif_compression_uncompressed, heif_compression_VVC |
| 577 |
}) { |
| 578 |
|
| 579 |
switch (compression_format) { |
| 580 |
case heif_compression_AVC: |
| 581 |
std::cout << "AVC"; |
| 582 |
break; |
| 583 |
case heif_compression_AV1: |
| 584 |
std::cout << "AVIF"; |
| 585 |
break; |
| 586 |
case heif_compression_HEVC: |
| 587 |
std::cout << "HEIC"; |
| 588 |
break; |
| 589 |
case heif_compression_JPEG: |
| 590 |
std::cout << "JPEG"; |
| 591 |
break; |
| 592 |
case heif_compression_JPEG2000: |
| 593 |
std::cout << "JPEG 2000"; |
| 594 |
break; |
| 595 |
case heif_compression_HTJ2K: |
| 596 |
std::cout << "JPEG 2000 (HT)"; |
| 597 |
break; |
| 598 |
case heif_compression_uncompressed: |
| 599 |
std::cout << "Uncompressed"; |
| 600 |
break; |
| 601 |
case heif_compression_VVC: |
| 602 |
std::cout << "VVIC"; |
| 603 |
break; |
| 604 |
default: |
| 605 |
assert(false); |
| 606 |
} |
| 607 |
|
| 608 |
std::cout << " encoders:\n"; |
| 609 |
|
| 610 |
#define MAX_ENCODERS 10 |
| 611 |
const heif_encoder_descriptor* encoder_descriptors[MAX_ENCODERS]; |
| 612 |
int count = heif_get_encoder_descriptors(compression_format, |
| 613 |
nullptr, |
| 614 |
encoder_descriptors, MAX_ENCODERS); |
| 615 |
#undef MAX_ENCODERS |
| 616 |
|
| 617 |
show_list_of_encoders(encoder_descriptors, count); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
bool ends_with(const std::string& str, const std::string& end) |
| 623 |
{ |
| 624 |
if (str.length() < end.length()) { |
| 625 |
return false; |
| 626 |
} |
| 627 |
else { |
| 628 |
return str.compare(str.length() - end.length(), end.length(), end) == 0; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
heif_compression_format guess_compression_format_from_filename(const std::string& filename) |
| 634 |
{ |
| 635 |
std::string filename_lowercase = filename; |
| 636 |
std::transform(filename_lowercase.begin(), filename_lowercase.end(), filename_lowercase.begin(), ::tolower); |
| 637 |
|
| 638 |
if (ends_with(filename_lowercase, ".avif")) { |
| 639 |
return heif_compression_AV1; |
| 640 |
} |
| 641 |
else if (ends_with(filename_lowercase, ".vvic")) { |
| 642 |
return heif_compression_VVC; |
| 643 |
} |
| 644 |
else if (ends_with(filename_lowercase, ".avci")) { |
| 645 |
return heif_compression_AVC; |
| 646 |
} |
| 647 |
else if (ends_with(filename_lowercase, ".heic")) { |
| 648 |
return heif_compression_HEVC; |
| 649 |
} |
| 650 |
else if (ends_with(filename_lowercase, ".hej2")) { |
| 651 |
return heif_compression_JPEG2000; |
| 652 |
} |
| 653 |
else { |
| 654 |
return heif_compression_undefined; |
| 655 |
} |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
std::string suffix_for_compression_format(heif_compression_format format) |
| 660 |
{ |
| 661 |
switch (format) { |
| 662 |
case heif_compression_AV1: return "avif"; |
| 663 |
case heif_compression_VVC: return "vvic"; |
| 664 |
case heif_compression_AVC: return "avci"; |
| 665 |
case heif_compression_HEVC: return "heic"; |
| 666 |
case heif_compression_JPEG2000: return "hej2"; |
| 667 |
default: return "data"; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
InputImage load_image(const std::string& input_filename, int output_bit_depth) |
| 673 |
{ |
| 674 |
InputImage input_image; |
| 675 |
|
| 676 |
// get file type from file name |
| 677 |
|
| 678 |
std::string suffix; |
| 679 |
auto suffix_pos = input_filename.find_last_of('.'); |
| 680 |
if (suffix_pos != std::string::npos) { |
| 681 |
suffix = input_filename.substr(suffix_pos + 1); |
| 682 |
std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower); |
| 683 |
} |
| 684 |
|
| 685 |
enum |
| 686 |
{ |
| 687 |
PNG, JPEG, Y4M, TIFF |
| 688 |
} filetype = JPEG; |
| 689 |
if (suffix == "png") { |
| 690 |
filetype = PNG; |
| 691 |
} |
| 692 |
else if (suffix == "y4m") { |
| 693 |
filetype = Y4M; |
| 694 |
} |
| 695 |
else if (suffix == "tif" || suffix == "tiff") { |
| 696 |
filetype = TIFF; |
| 697 |
} |
| 698 |
|
| 699 |
if (filetype == PNG) { |
| 700 |
heif_error err = loadPNG(input_filename.c_str(), output_bit_depth, &input_image); |
| 701 |
if (err.code != heif_error_Ok) { |
| 702 |
std::cerr << "Can not load TIFF input_image: " << err.message << '\n'; |
| 703 |
exit(1); |
| 704 |
} |
| 705 |
} |
| 706 |
else if (filetype == Y4M) { |
| 707 |
heif_error err = loadY4M(input_filename.c_str(), &input_image); |
| 708 |
if (err.code != heif_error_Ok) { |
| 709 |
std::cerr << "Can not load TIFF input_image: " << err.message << '\n'; |
| 710 |
exit(1); |
| 711 |
} |
| 712 |
} |
| 713 |
else if (filetype == TIFF) { |
| 714 |
heif_error err = loadTIFF(input_filename.c_str(), &input_image); |
| 715 |
if (err.code != heif_error_Ok) { |
| 716 |
std::cerr << "Can not load TIFF input_image: " << err.message << '\n'; |
| 717 |
exit(1); |
| 718 |
} |
| 719 |
} |
| 720 |
else { |
| 721 |
heif_error err = loadJPEG(input_filename.c_str(), &input_image); |
| 722 |
if (err.code != heif_error_Ok) { |
| 723 |
std::cerr << "Can not load JPEG input_image: " << err.message << '\n'; |
| 724 |
exit(1); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
return input_image; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
heif_error create_output_nclx_profile_and_configure_encoder(heif_encoder* encoder, |
| 733 |
heif_color_profile_nclx** out_nclx, |
| 734 |
std::shared_ptr<heif_image> input_image, |
| 735 |
bool lossless, |
| 736 |
heif_output_nclx_color_profile_preset profile_preset) |
| 737 |
{ |
| 738 |
*out_nclx = heif_nclx_color_profile_alloc(); |
| 739 |
if (!*out_nclx) { |
| 740 |
return {heif_error_Encoding_error, heif_suberror_Unspecified, "Cannot allocate NCLX color profile."}; |
| 741 |
} |
| 742 |
|
| 743 |
heif_color_profile_nclx* nclx = *out_nclx; // abbreviation; |
| 744 |
|
| 745 |
|
| 746 |
// set NCLX based on preset |
| 747 |
|
| 748 |
switch (profile_preset) { |
| 749 |
case heif_output_nclx_color_profile_preset_custom: { |
| 750 |
heif_error error = heif_nclx_color_profile_set_matrix_coefficients(nclx, nclx_matrix_coefficients); |
| 751 |
if (error.code) { |
| 752 |
std::cerr << "Invalid matrix coefficients specified.\n"; |
| 753 |
exit(5); |
| 754 |
} |
| 755 |
|
| 756 |
error = heif_nclx_color_profile_set_transfer_characteristics(nclx, nclx_transfer_characteristic); |
| 757 |
if (error.code) { |
| 758 |
std::cerr << "Invalid transfer characteristics specified.\n"; |
| 759 |
exit(5); |
| 760 |
} |
| 761 |
|
| 762 |
error = heif_nclx_color_profile_set_color_primaries(nclx, nclx_colour_primaries); |
| 763 |
if (error.code) { |
| 764 |
std::cerr << "Invalid color primaries specified.\n"; |
| 765 |
exit(5); |
| 766 |
} |
| 767 |
|
| 768 |
nclx->full_range_flag = (uint8_t) nclx_full_range; |
| 769 |
break; |
| 770 |
} |
| 771 |
|
| 772 |
case heif_output_nclx_color_profile_preset_automatic: { |
| 773 |
heif_color_profile_nclx* input_nclx = nullptr; |
| 774 |
|
| 775 |
// --- use input image color profile, if it exists |
| 776 |
|
| 777 |
heif_error error = heif_image_get_nclx_color_profile(input_image.get(), &input_nclx); |
| 778 |
if (error.code == heif_error_Color_profile_does_not_exist) { |
| 779 |
|
| 780 |
// input image has not color profile, guess one |
| 781 |
|
| 782 |
if (heif_image_get_colorspace(input_image.get()) == heif_colorspace_RGB) { |
| 783 |
// sRGB |
| 784 |
nclx->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_709_5; // will be overwritten below if lossless |
| 785 |
nclx->color_primaries = heif_color_primaries_ITU_R_BT_709_5; |
| 786 |
nclx->transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1; |
| 787 |
} |
| 788 |
else { |
| 789 |
// BT.709 |
| 790 |
nclx->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_709_5; // will be overwritten below if lossless |
| 791 |
nclx->color_primaries = heif_color_primaries_ITU_R_BT_709_5; |
| 792 |
nclx->transfer_characteristics = heif_transfer_characteristic_ITU_R_BT_709_5; |
| 793 |
} |
| 794 |
} |
| 795 |
else if (error.code) { |
| 796 |
std::cerr << "Cannot get input NCLX color profile.\n"; |
| 797 |
return error; |
| 798 |
} |
| 799 |
else { |
| 800 |
// no error, we have an input color profile that we can use for output too |
| 801 |
|
| 802 |
nclx->matrix_coefficients = input_nclx->matrix_coefficients; |
| 803 |
nclx->transfer_characteristics = input_nclx->transfer_characteristics; |
| 804 |
nclx->color_primaries = input_nclx->color_primaries; |
| 805 |
nclx->full_range_flag = input_nclx->full_range_flag; |
| 806 |
|
| 807 |
heif_nclx_color_profile_free(input_nclx); |
| 808 |
input_nclx = nullptr; |
| 809 |
} |
| 810 |
|
| 811 |
assert(!input_nclx); |
| 812 |
break; |
| 813 |
} |
| 814 |
|
| 815 |
case heif_output_nclx_color_profile_preset_Rec_601: |
| 816 |
nclx->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6; |
| 817 |
nclx->color_primaries = heif_color_primaries_ITU_R_BT_601_6; |
| 818 |
nclx->transfer_characteristics = heif_transfer_characteristic_ITU_R_BT_601_6; |
| 819 |
break; |
| 820 |
|
| 821 |
case heif_output_nclx_color_profile_preset_compatible: |
| 822 |
case heif_output_nclx_color_profile_preset_Rec_709: |
| 823 |
nclx->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_709_5; |
| 824 |
nclx->color_primaries = heif_color_primaries_ITU_R_BT_709_5; |
| 825 |
nclx->transfer_characteristics = heif_transfer_characteristic_ITU_R_BT_709_5; |
| 826 |
break; |
| 827 |
|
| 828 |
case heif_output_nclx_color_profile_preset_Rec_2020: |
| 829 |
nclx->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_2020_2_constant_luminance; |
| 830 |
nclx->color_primaries = heif_color_primaries_ITU_R_BT_2020_2_and_2100_0; |
| 831 |
|
| 832 |
if (heif_image_has_channel(input_image.get(), heif_channel_Y) && |
| 833 |
heif_image_get_bits_per_pixel(input_image.get(), heif_channel_Y) <= 10) { |
| 834 |
nclx->transfer_characteristics = heif_transfer_characteristic_ITU_R_BT_2020_2_10bit; |
| 835 |
} |
| 836 |
else { |
| 837 |
nclx->transfer_characteristics = heif_transfer_characteristic_ITU_R_BT_2020_2_12bit; |
| 838 |
} |
| 839 |
break; |
| 840 |
} |
| 841 |
|
| 842 |
// modify NCLX depending on input image |
| 843 |
|
| 844 |
if (lossless) { |
| 845 |
heif_encoder_set_lossless(encoder, true); |
| 846 |
|
| 847 |
if (heif_image_get_colorspace(input_image.get()) == heif_colorspace_RGB) { |
| 848 |
nclx->matrix_coefficients = heif_matrix_coefficients_RGB_GBR; |
| 849 |
nclx->full_range_flag = true; |
| 850 |
|
| 851 |
heif_error error = heif_encoder_set_parameter(encoder, "chroma", "444"); |
| 852 |
if (error.code) { |
| 853 |
return error; |
| 854 |
} |
| 855 |
} |
| 856 |
else { |
| 857 |
heif_error error; |
| 858 |
|
| 859 |
// TODO: this assumes that the encoder plugin has a 'chroma' parameter. Currently, they do, but there should be a better way to set this. |
| 860 |
switch (heif_image_get_chroma_format(input_image.get())) { |
| 861 |
case heif_chroma_420: |
| 862 |
case heif_chroma_monochrome: |
| 863 |
error = heif_encoder_set_parameter(encoder, "chroma", "420"); |
| 864 |
break; |
| 865 |
case heif_chroma_422: |
| 866 |
error = heif_encoder_set_parameter(encoder, "chroma", "422"); |
| 867 |
break; |
| 868 |
case heif_chroma_444: |
| 869 |
error = heif_encoder_set_parameter(encoder, "chroma", "444"); |
| 870 |
break; |
| 871 |
default: |
| 872 |
assert(false); |
| 873 |
exit(5); |
| 874 |
} |
| 875 |
|
| 876 |
if (error.code) { |
| 877 |
return error; |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
return {heif_error_Ok}; |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
struct input_tiles_generator |
| 887 |
{ |
| 888 |
virtual ~input_tiles_generator() = default; |
| 889 |
|
| 890 |
virtual uint32_t nColumns() const = 0; |
| 891 |
virtual uint32_t nRows() const = 0; |
| 892 |
|
| 893 |
virtual uint32_t nTiles() const { return nColumns() * nRows(); } |
| 894 |
|
| 895 |
virtual InputImage get_image(uint32_t tx, uint32_t ty, int output_bit_depth) = 0; |
| 896 |
}; |
| 897 |
|
| 898 |
struct input_tiles_generator_separate_files : public input_tiles_generator |
| 899 |
{ |
| 900 |
uint32_t first_start; |
| 901 |
uint32_t first_end; |
| 902 |
uint32_t first_digits; |
| 903 |
uint32_t second_start; |
| 904 |
uint32_t second_end; |
| 905 |
uint32_t second_digits; |
| 906 |
|
| 907 |
std::filesystem::path directory; |
| 908 |
std::string prefix; |
| 909 |
std::string separator; |
| 910 |
std::string suffix; |
| 911 |
|
| 912 |
bool first_is_x = false; |
| 913 |
|
| 914 |
uint32_t nColumns() const override { return first_is_x ? (first_end - first_start + 1) : (second_end - second_start + 1); } |
| 915 |
uint32_t nRows() const override { return first_is_x ? (second_end - second_start + 1) : (first_end - first_start + 1); } |
| 916 |
|
| 917 |
uint32_t nTiles() const override { return (first_end - first_start + 1) * (second_end - second_start + 1); } |
| 918 |
|
| 919 |
std::filesystem::path filename(uint32_t tx, uint32_t ty) const |
| 920 |
{ |
| 921 |
std::stringstream sstr; |
| 922 |
|
| 923 |
sstr << prefix << std::setw(first_digits) << std::setfill('0') << (first_is_x ? tx : ty) + first_start; |
| 924 |
sstr << separator << std::setw(second_digits) << std::setfill('0') << (first_is_x ? ty : tx) + second_start; |
| 925 |
sstr << suffix; |
| 926 |
|
| 927 |
std::filesystem::path p = directory / sstr.str(); |
| 928 |
return p; |
| 929 |
} |
| 930 |
|
| 931 |
InputImage get_image(uint32_t tx, uint32_t ty, int output_bit_depth) override |
| 932 |
{ |
| 933 |
std::string input_filename = filename(tx, ty).string(); |
| 934 |
InputImage image = load_image(input_filename, output_bit_depth); |
| 935 |
return image; |
| 936 |
} |
| 937 |
}; |
| 938 |
|
| 939 |
std::shared_ptr<input_tiles_generator> determine_input_images_tiling(const std::string& filename, bool first_is_x) |
| 940 |
{ |
| 941 |
std::regex pattern(R"((.*\D)?(\d+)(\D+?)(\d+)(\..+)$)"); |
| 942 |
std::smatch match; |
| 943 |
|
| 944 |
auto generator = std::make_shared<input_tiles_generator_separate_files>(); |
| 945 |
|
| 946 |
if (std::regex_match(filename, match, pattern)) { |
| 947 |
std::string prefix = match[1]; |
| 948 |
|
| 949 |
auto p = std::filesystem::absolute(std::filesystem::path(prefix)); |
| 950 |
generator->directory = p.parent_path(); |
| 951 |
generator->prefix = p.filename().string(); // TODO: we could also use u8string(), but it is not well supported in C++20 |
| 952 |
|
| 953 |
generator->separator = match[3]; |
| 954 |
generator->suffix = match[5]; |
| 955 |
|
| 956 |
generator->first_start = 9999; |
| 957 |
generator->first_end = 0; |
| 958 |
generator->first_digits = 9; |
| 959 |
|
| 960 |
generator->second_start = 9999; |
| 961 |
generator->second_end = 0; |
| 962 |
generator->second_digits = 9; |
| 963 |
} |
| 964 |
else { |
| 965 |
return nullptr; |
| 966 |
} |
| 967 |
|
| 968 |
std::string patternString = generator->prefix + "(\\d+)" + generator->separator + "(\\d+)" + generator->suffix + "$"; |
| 969 |
pattern = patternString; |
| 970 |
|
| 971 |
for (const auto& dirEntry : std::filesystem::directory_iterator(generator->directory)) |
| 972 |
{ |
| 973 |
if (dirEntry.is_regular_file()) { |
| 974 |
std::string s{dirEntry.path().filename().string()}; |
| 975 |
|
| 976 |
if (std::regex_match(s, match, pattern)) { |
| 977 |
uint32_t first = std::stoi(match[1]); |
| 978 |
uint32_t second = std::stoi(match[2]); |
| 979 |
|
| 980 |
generator->first_digits = std::min(generator->first_digits, (uint32_t)match[1].length()); |
| 981 |
generator->second_digits = std::min(generator->second_digits, (uint32_t)match[2].length()); |
| 982 |
|
| 983 |
generator->first_start = std::min(generator->first_start, first); |
| 984 |
generator->first_end = std::max(generator->first_end, first); |
| 985 |
generator->second_start = std::min(generator->second_start, second); |
| 986 |
generator->second_end = std::max(generator->second_end, second); |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
generator->first_is_x = first_is_x; |
| 992 |
|
| 993 |
return generator; |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
class input_tiles_generator_cut_image : public input_tiles_generator |
| 998 |
{ |
| 999 |
public: |
| 1000 |
input_tiles_generator_cut_image(const char* filename, int tile_size, int output_bit_depth) |
| 1001 |
{ |
| 1002 |
mImage = load_image(filename, output_bit_depth); |
| 1003 |
|
| 1004 |
mWidth = heif_image_get_primary_width(mImage.image.get()); |
| 1005 |
mHeight = heif_image_get_primary_height(mImage.image.get()); |
| 1006 |
|
| 1007 |
mTileSize = tile_size; |
| 1008 |
} |
| 1009 |
|
| 1010 |
uint32_t nColumns() const override { return (mWidth + mTileSize - 1)/mTileSize; } |
| 1011 |
uint32_t nRows() const override { return (mHeight + mTileSize - 1)/mTileSize; } |
| 1012 |
|
| 1013 |
InputImage get_image(uint32_t tx, uint32_t ty, int output_bit_depth) override |
| 1014 |
{ |
| 1015 |
heif_image* tileImage; |
| 1016 |
heif_error err = heif_image_extract_area(mImage.image.get(), tx * mTileSize, ty * mTileSize, mTileSize, mTileSize, |
| 1017 |
heif_get_global_security_limits(), |
| 1018 |
&tileImage); |
| 1019 |
if (err.code) { |
| 1020 |
std::cerr << "error extracting tile " << tx << ";" << ty << std::endl; |
| 1021 |
exit(1); |
| 1022 |
} |
| 1023 |
|
| 1024 |
InputImage tile; |
| 1025 |
tile.image = std::shared_ptr<heif_image>(tileImage, |
| 1026 |
[](heif_image* img) { heif_image_release(img); }); |
| 1027 |
return tile; |
| 1028 |
} |
| 1029 |
|
| 1030 |
uint32_t get_image_width() const { return heif_image_get_primary_width(mImage.image.get()); } |
| 1031 |
uint32_t get_image_height() const { return heif_image_get_primary_height(mImage.image.get()); } |
| 1032 |
|
| 1033 |
private: |
| 1034 |
InputImage mImage; |
| 1035 |
uint32_t mWidth, mHeight; |
| 1036 |
int mTileSize; |
| 1037 |
}; |
| 1038 |
|
| 1039 |
|
| 1040 |
// TODO: we have to attach the input image Exif and XMP to the tiled image |
| 1041 |
heif_image_handle* encode_tiled(heif_context* ctx, heif_encoder* encoder, heif_encoding_options* options, |
| 1042 |
int output_bit_depth, |
| 1043 |
const std::shared_ptr<input_tiles_generator>& tile_generator, |
| 1044 |
const heif_image_tiling& tiling) |
| 1045 |
{ |
| 1046 |
heif_image_handle* tiled_image = nullptr; |
| 1047 |
|
| 1048 |
|
| 1049 |
// --- create the main grid image |
| 1050 |
|
| 1051 |
if (tiling_method == "grid") { |
| 1052 |
heif_error error = heif_context_add_grid_image(ctx, tiling.image_width, tiling.image_height, |
| 1053 |
tiling.num_columns, tiling.num_rows, |
| 1054 |
options, |
| 1055 |
&tiled_image); |
| 1056 |
if (error.code != 0) { |
| 1057 |
std::cerr << "Could not generate grid image: " << error.message << "\n"; |
| 1058 |
return nullptr; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 1062 |
else if (tiling_method == "tili") { |
| 1063 |
heif_tiled_image_parameters tiled_params{}; |
| 1064 |
tiled_params.version = 1; |
| 1065 |
tiled_params.image_width = tiling.image_width; |
| 1066 |
tiled_params.image_height = tiling.image_height; |
| 1067 |
tiled_params.tile_width = tiling.tile_width; |
| 1068 |
tiled_params.tile_height = tiling.tile_height; |
| 1069 |
tiled_params.offset_field_length = 32; |
| 1070 |
tiled_params.size_field_length = 24; |
| 1071 |
tiled_params.tiles_are_sequential = 1; |
| 1072 |
|
| 1073 |
heif_error error = heif_context_add_tiled_image(ctx, &tiled_params, options, encoder, &tiled_image); |
| 1074 |
if (error.code != 0) { |
| 1075 |
std::cerr << "Could not generate tili image: " << error.message << "\n"; |
| 1076 |
return nullptr; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
#endif |
| 1080 |
#if WITH_UNCOMPRESSED_CODEC |
| 1081 |
else if (tiling_method == "unci") { |
| 1082 |
heif_unci_image_parameters params{}; |
| 1083 |
params.version = 1; |
| 1084 |
params.image_width = tiling.image_width; |
| 1085 |
params.image_height = tiling.image_height; |
| 1086 |
params.tile_width = tiling.tile_width; |
| 1087 |
params.tile_height = tiling.tile_height; |
| 1088 |
params.compression = unci_compression; |
| 1089 |
|
| 1090 |
InputImage prototype_image = tile_generator->get_image(0,0, output_bit_depth); |
| 1091 |
|
| 1092 |
heif_error error = heif_context_add_empty_unci_image(ctx, ¶ms, options, prototype_image.image.get(), &tiled_image); |
| 1093 |
if (error.code != 0) { |
| 1094 |
std::cerr << "Could not generate unci image: " << error.message << "\n"; |
| 1095 |
return nullptr; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
#endif |
| 1099 |
else { |
| 1100 |
assert(false); |
| 1101 |
exit(10); |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
// --- add all the image tiles |
| 1106 |
|
| 1107 |
std::cout << "encoding tiled image, tile size: " << tiling.tile_width << "x" << tiling.tile_height |
| 1108 |
<< " image size: " << tiling.image_width << "x" << tiling.image_height << "\n"; |
| 1109 |
|
| 1110 |
int tile_width = 0, tile_height = 0; |
| 1111 |
|
| 1112 |
for (uint32_t ty = 0; ty < tile_generator->nRows(); ty++) |
| 1113 |
for (uint32_t tx = 0; tx < tile_generator->nColumns(); tx++) { |
| 1114 |
InputImage input_image = tile_generator->get_image(tx,ty, output_bit_depth); |
| 1115 |
|
| 1116 |
if (tile_width == 0) { |
| 1117 |
tile_width = heif_image_get_primary_width(input_image.image.get()); |
| 1118 |
tile_height = heif_image_get_primary_height(input_image.image.get()); |
| 1119 |
|
| 1120 |
if (tile_width <= 0 || tile_height <= 0) { |
| 1121 |
std::cerr << "Could not read input image size correctly\n"; |
| 1122 |
return nullptr; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
heif_error error; |
| 1127 |
error = heif_image_extend_to_size_fill_with_zero(input_image.image.get(), tile_width, tile_height); |
| 1128 |
if (error.code) { |
| 1129 |
std::cerr << error.message << "\n"; |
| 1130 |
} |
| 1131 |
|
| 1132 |
std::cout << "encoding tile " << ty+1 << " " << tx+1 |
| 1133 |
<< " (of " << tile_generator->nRows() << "x" << tile_generator->nColumns() << ") \r"; |
| 1134 |
std::cout.flush(); |
| 1135 |
|
| 1136 |
error = heif_context_add_image_tile(ctx, tiled_image, tx, ty, |
| 1137 |
input_image.image.get(), |
| 1138 |
encoder); |
| 1139 |
if (error.code != 0) { |
| 1140 |
std::cerr << "Could not encode HEIF/AVIF file: " << error.message << "\n"; |
| 1141 |
return nullptr; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
|
| 1145 |
std::cout << "\n"; |
| 1146 |
|
| 1147 |
return tiled_image; |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
template <typename T> |
| 1152 |
std::vector<T> parse_comma_separated_numeric_arguments(std::string arg, |
| 1153 |
std::vector<T> max_val) |
| 1154 |
{ |
| 1155 |
std::istringstream ss(arg); |
| 1156 |
std::string token; |
| 1157 |
|
| 1158 |
std::vector<T> results; |
| 1159 |
|
| 1160 |
for (size_t i = 0 ; i<max_val.size(); i++) { |
| 1161 |
|
| 1162 |
if (!std::getline(ss, token, ',')) return {}; |
| 1163 |
try { |
| 1164 |
size_t pos; |
| 1165 |
unsigned long val = std::stoul(token, &pos); |
| 1166 |
if (pos != token.size()) return {}; // extra non-numeric characters |
| 1167 |
if (val > max_val[i]) return {}; |
| 1168 |
results.push_back(static_cast<T>(val)); |
| 1169 |
} catch (...) { |
| 1170 |
return {}; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
// There should be no extra tokens |
| 1175 |
if (ss.rdbuf()->in_avail() != 0) return {}; |
| 1176 |
|
| 1177 |
return results; |
| 1178 |
} |
| 1179 |
|
| 1180 |
bool prefix_compare(const char* a, const char* b) |
| 1181 |
{ |
| 1182 |
auto minLen = std::min(strlen(a), strlen(b)); |
| 1183 |
return strncmp(a,b,minLen) == 0; |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
bool set_metadata_compression_method(const std::string& arg) |
| 1188 |
{ |
| 1189 |
if (arg == "auto") { |
| 1190 |
metadata_compression_method = heif_metadata_compression_auto; |
| 1191 |
return true; |
| 1192 |
} |
| 1193 |
else if (arg == "off") { |
| 1194 |
metadata_compression_method = heif_metadata_compression_off; |
| 1195 |
return true; |
| 1196 |
} |
| 1197 |
else if (arg == "brotli") { |
| 1198 |
metadata_compression_method = heif_metadata_compression_brotli; |
| 1199 |
return true; |
| 1200 |
} |
| 1201 |
else if (arg == "deflate") { |
| 1202 |
metadata_compression_method = heif_metadata_compression_deflate; |
| 1203 |
return true; |
| 1204 |
} |
| 1205 |
else if (arg == "zlib") { |
| 1206 |
metadata_compression_method = heif_metadata_compression_zlib; |
| 1207 |
return true; |
| 1208 |
} |
| 1209 |
else { |
| 1210 |
std::cerr << "Unknown metadata compression method '" << arg << "'. Choose between {auto,off,deflate,zlib,brotli}\n"; |
| 1211 |
return false; |
| 1212 |
} |
| 1213 |
} |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
class LibHeifInitializer |
| 1218 |
{ |
| 1219 |
public: |
| 1220 |
LibHeifInitializer() { heif_init(nullptr); } |
| 1221 |
|
| 1222 |
~LibHeifInitializer() { heif_deinit(); } |
| 1223 |
}; |
| 1224 |
|
| 1225 |
|
| 1226 |
int do_encode_images(heif_context*, heif_encoder*, heif_encoding_options* options, const std::vector<std::string>& args); |
| 1227 |
int do_encode_sequence(heif_context*, heif_encoder*, heif_encoding_options* options, std::vector<std::string> args); |
| 1228 |
|
| 1229 |
|
| 1230 |
int main(int argc, char** argv) |
| 1231 |
{ |
| 1232 |
// This takes care of initializing libheif and also deinitializing it at the end to free all resources. |
| 1233 |
LibHeifInitializer initializer; |
| 1234 |
|
| 1235 |
std::vector<std::string> raw_params; |
| 1236 |
|
| 1237 |
|
| 1238 |
while (true) { |
| 1239 |
int option_index = 0; |
| 1240 |
int c = getopt_long(argc, argv, "hq:Lo:vPp:t:b:Ae:C:TSV" |
| 1241 |
#if WITH_UNCOMPRESSED_CODEC |
| 1242 |
"U" |
| 1243 |
#endif |
| 1244 |
, long_options, &option_index); |
| 1245 |
if (c == -1) |
| 1246 |
break; |
| 1247 |
|
| 1248 |
switch (c) { |
| 1249 |
case 'h': |
| 1250 |
show_help(argv[0]); |
| 1251 |
return 0; |
| 1252 |
case 'v': |
| 1253 |
heif_examples::show_version(); |
| 1254 |
return 0; |
| 1255 |
case 'q': |
| 1256 |
quality = atoi(optarg); |
| 1257 |
break; |
| 1258 |
case 'L': |
| 1259 |
lossless = true; |
| 1260 |
break; |
| 1261 |
case 'o': |
| 1262 |
output_filename = optarg; |
| 1263 |
break; |
| 1264 |
case OPTION_VERBOSE: |
| 1265 |
logging_level++; |
| 1266 |
break; |
| 1267 |
case 'P': |
| 1268 |
option_show_parameters = true; |
| 1269 |
break; |
| 1270 |
case 'p': |
| 1271 |
raw_params.push_back(optarg); |
| 1272 |
break; |
| 1273 |
case 't': |
| 1274 |
thumbnail_bbox_size = atoi(optarg); |
| 1275 |
break; |
| 1276 |
case 'b': |
| 1277 |
output_bit_depth = atoi(optarg); |
| 1278 |
if (output_bit_depth < 9 || output_bit_depth > 16) { |
| 1279 |
std::cerr << "Bit depth for input HDR images must be 9-16 bits.\n"; |
| 1280 |
return 5; |
| 1281 |
} |
| 1282 |
break; |
| 1283 |
case 'A': |
| 1284 |
force_enc_av1f = true; |
| 1285 |
break; |
| 1286 |
#if WITH_UNCOMPRESSED_CODEC |
| 1287 |
case 'U': |
| 1288 |
force_enc_uncompressed = true; |
| 1289 |
break; |
| 1290 |
#endif |
| 1291 |
case 'e': |
| 1292 |
encoderId = optarg; |
| 1293 |
break; |
| 1294 |
case OPTION_NCLX_MATRIX_COEFFICIENTS: |
| 1295 |
nclx_matrix_coefficients = (uint16_t) strtoul(optarg, nullptr, 0); |
| 1296 |
break; |
| 1297 |
case OPTION_NCLX_COLOUR_PRIMARIES: |
| 1298 |
nclx_colour_primaries = (uint16_t) strtoul(optarg, nullptr, 0); |
| 1299 |
break; |
| 1300 |
case OPTION_NCLX_TRANSFER_CHARACTERISTIC: |
| 1301 |
nclx_transfer_characteristic = (uint16_t) strtoul(optarg, nullptr, 0); |
| 1302 |
break; |
| 1303 |
case OPTION_NCLX_FULL_RANGE_FLAG: |
| 1304 |
nclx_full_range = atoi(optarg); |
| 1305 |
break; |
| 1306 |
case OPTION_PITM_DESCRIPTION: |
| 1307 |
property_pitm_description = optarg; |
| 1308 |
break; |
| 1309 |
case OPTION_USE_HEVC_COMPRESSION: |
| 1310 |
force_enc_hevc = true; |
| 1311 |
break; |
| 1312 |
case OPTION_USE_VVC_COMPRESSION: |
| 1313 |
force_enc_vvc = true; |
| 1314 |
break; |
| 1315 |
case OPTION_USE_AVC_COMPRESSION: |
| 1316 |
force_enc_avc = true; |
| 1317 |
break; |
| 1318 |
case OPTION_USE_JPEG_COMPRESSION: |
| 1319 |
force_enc_jpeg = true; |
| 1320 |
break; |
| 1321 |
case OPTION_USE_JPEG2000_COMPRESSION: |
| 1322 |
force_enc_jpeg2000 = true; |
| 1323 |
break; |
| 1324 |
case OPTION_USE_HTJ2K_COMPRESSION: |
| 1325 |
force_enc_htj2k = true; |
| 1326 |
break; |
| 1327 |
case OPTION_PLUGIN_DIRECTORY: { |
| 1328 |
int nPlugins; |
| 1329 |
heif_error error = heif_load_plugins(optarg, nullptr, &nPlugins, 0); |
| 1330 |
if (error.code) { |
| 1331 |
std::cerr << "Error loading libheif plugins: " << error.message << "\n"; |
| 1332 |
return 1; |
| 1333 |
} |
| 1334 |
|
| 1335 |
// Note: since we process the option within the loop, we can only consider the '-v' flags coming before the plugin loading option. |
| 1336 |
if (logging_level > 0) { |
| 1337 |
std::cout << nPlugins << " plugins loaded from directory " << optarg << "\n"; |
| 1338 |
} |
| 1339 |
break; |
| 1340 |
} |
| 1341 |
case OPTION_TILED_IMAGE_WIDTH: |
| 1342 |
tiled_image_width = (int) strtol(optarg, nullptr, 0); |
| 1343 |
break; |
| 1344 |
case OPTION_TILED_IMAGE_HEIGHT: |
| 1345 |
tiled_image_height = (int) strtol(optarg, nullptr, 0); |
| 1346 |
break; |
| 1347 |
case OPTION_TILING_METHOD: |
| 1348 |
tiling_method = optarg; |
| 1349 |
if (tiling_method != "grid" |
| 1350 |
#if WITH_UNCOMPRESSED_CODEC |
| 1351 |
&& tiling_method != "unci" |
| 1352 |
#endif |
| 1353 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 1354 |
&& tiling_method != "tili" |
| 1355 |
#endif |
| 1356 |
) { |
| 1357 |
std::cerr << "Invalid tiling method '" << tiling_method << "'\n"; |
| 1358 |
exit(5); |
| 1359 |
} |
| 1360 |
break; |
| 1361 |
case OPTION_CUT_TILES: |
| 1362 |
cut_tiles = atoi(optarg); |
| 1363 |
break; |
| 1364 |
case OPTION_UNCI_COMPRESSION: { |
| 1365 |
std::string option(optarg); |
| 1366 |
if (option == "none") { |
| 1367 |
unci_compression = heif_unci_compression_off; |
| 1368 |
} |
| 1369 |
else if (option == "brotli") { |
| 1370 |
unci_compression = heif_unci_compression_brotli; |
| 1371 |
} |
| 1372 |
else if (option == "deflate") { |
| 1373 |
unci_compression = heif_unci_compression_deflate; |
| 1374 |
} |
| 1375 |
else if (option == "zlib") { |
| 1376 |
unci_compression = heif_unci_compression_zlib; |
| 1377 |
} |
| 1378 |
else { |
| 1379 |
std::cerr << "Invalid unci compression method '" << option << "'\n"; |
| 1380 |
exit(5); |
| 1381 |
} |
| 1382 |
break; |
| 1383 |
} |
| 1384 |
case 'C': |
| 1385 |
chroma_downsampling = optarg; |
| 1386 |
if (chroma_downsampling != "nn" && |
| 1387 |
chroma_downsampling != "nearest-neighbor" && |
| 1388 |
chroma_downsampling != "average" && |
| 1389 |
chroma_downsampling != "sharp-yuv") { |
| 1390 |
fprintf(stderr, "Undefined chroma downsampling algorithm.\n"); |
| 1391 |
exit(5); |
| 1392 |
} |
| 1393 |
if (chroma_downsampling == "nn") { // abbreviation |
| 1394 |
chroma_downsampling = "nearest-neighbor"; |
| 1395 |
} |
| 1396 |
#if !HAVE_LIBSHARPYUV |
| 1397 |
if (chroma_downsampling == "sharp-yuv") { |
| 1398 |
std::cerr << "Error: sharp-yuv chroma downsampling method has not been compiled into libheif.\n"; |
| 1399 |
return 5; |
| 1400 |
} |
| 1401 |
#endif |
| 1402 |
break; |
| 1403 |
case 'T': |
| 1404 |
use_tiling = true; |
| 1405 |
break; |
| 1406 |
case 'S': |
| 1407 |
encode_sequence = true; |
| 1408 |
break; |
| 1409 |
case 'V': |
| 1410 |
use_video_handler = true; |
| 1411 |
break; |
| 1412 |
case OPTION_SEQUENCES_TIMEBASE: |
| 1413 |
sequence_timebase = atoi(optarg); |
| 1414 |
break; |
| 1415 |
case OPTION_SEQUENCES_DURATIONS: |
| 1416 |
sequence_durations = atoi(optarg); |
| 1417 |
break; |
| 1418 |
case OPTION_SEQUENCES_FPS: |
| 1419 |
if (strcmp(optarg,"29.97")==0) { |
| 1420 |
sequence_durations = 1001; |
| 1421 |
sequence_timebase = 30000; |
| 1422 |
} |
| 1423 |
else { |
| 1424 |
double fps = std::atof(optarg); |
| 1425 |
sequence_timebase = 90000; |
| 1426 |
sequence_durations = (uint32_t)(90000 / fps + 0.5); |
| 1427 |
} |
| 1428 |
break; |
| 1429 |
case OPTION_SEQUENCES_REPETITIONS: |
| 1430 |
if (strcmp(optarg, "infinite")==0) { |
| 1431 |
sequence_repetitions = heif_sequence_maximum_number_of_repetitions; |
| 1432 |
} |
| 1433 |
else { |
| 1434 |
sequence_repetitions = atoi(optarg); |
| 1435 |
if (sequence_repetitions == 0) { |
| 1436 |
std::cerr << "Sequence repetitions may not be 0.\n"; |
| 1437 |
return 5; |
| 1438 |
} |
| 1439 |
} |
| 1440 |
break; |
| 1441 |
case OPTION_SEQUENCES_GOP_STRUCTURE: |
| 1442 |
if (prefix_compare(optarg, "intra-only")) { |
| 1443 |
sequence_gop_structure = heif_sequence_gop_structure_intra_only; |
| 1444 |
} |
| 1445 |
else if (prefix_compare(optarg, "low-delay") || prefix_compare(optarg, "p")) { |
| 1446 |
sequence_gop_structure = heif_sequence_gop_structure_lowdelay; |
| 1447 |
} |
| 1448 |
else if (prefix_compare(optarg, "unrestricted") || prefix_compare(optarg, "b")) { |
| 1449 |
sequence_gop_structure = heif_sequence_gop_structure_unrestricted; |
| 1450 |
} |
| 1451 |
else { |
| 1452 |
std::cerr << "Invalid GOP structure argument\n"; |
| 1453 |
return 5; |
| 1454 |
} |
| 1455 |
break; |
| 1456 |
case OPTION_SEQUENCES_MIN_KEYFRAME_DISTANCE: |
| 1457 |
sequence_keyframe_distance_min = atoi(optarg); |
| 1458 |
if (sequence_keyframe_distance_min < 0) { |
| 1459 |
std::cerr << "Keyframe distance must be >= 0\n"; |
| 1460 |
return 5; |
| 1461 |
} |
| 1462 |
break; |
| 1463 |
case OPTION_SEQUENCES_MAX_KEYFRAME_DISTANCE: |
| 1464 |
sequence_keyframe_distance_max = atoi(optarg); |
| 1465 |
if (sequence_keyframe_distance_max < 0) { |
| 1466 |
std::cerr << "Keyframe distance must be >= 0\n"; |
| 1467 |
return 5; |
| 1468 |
} |
| 1469 |
break; |
| 1470 |
case OPTION_SEQUENCES_MAX_FRAMES: |
| 1471 |
sequence_max_frames = atoi(optarg); |
| 1472 |
if (sequence_max_frames <= 0) { |
| 1473 |
std::cerr << "Maximum number of frames must be >= 1\n"; |
| 1474 |
return 5; |
| 1475 |
} |
| 1476 |
break; |
| 1477 |
case OPTION_COLOR_PROFILE_PRESET: |
| 1478 |
if (strcmp(optarg, "auto")==0) { |
| 1479 |
output_color_profile_preset = heif_output_nclx_color_profile_preset_automatic; |
| 1480 |
} |
| 1481 |
else if (strcmp(optarg, "custom")==0) { |
| 1482 |
output_color_profile_preset = heif_output_nclx_color_profile_preset_custom; |
| 1483 |
} |
| 1484 |
else if (strcmp(optarg, "compatible")==0) { |
| 1485 |
output_color_profile_preset = heif_output_nclx_color_profile_preset_compatible; |
| 1486 |
} |
| 1487 |
else if (strcmp(optarg, "601")==0) { |
| 1488 |
output_color_profile_preset = heif_output_nclx_color_profile_preset_Rec_601; |
| 1489 |
} |
| 1490 |
else if (strcmp(optarg, "709")==0) { |
| 1491 |
output_color_profile_preset = heif_output_nclx_color_profile_preset_Rec_709; |
| 1492 |
} |
| 1493 |
else if (strcmp(optarg, "2020")==0) { |
| 1494 |
output_color_profile_preset = heif_output_nclx_color_profile_preset_Rec_2020; |
| 1495 |
} |
| 1496 |
else { |
| 1497 |
std::cerr << "Invalid color-profile preset.\n"; |
| 1498 |
return 5; |
| 1499 |
} |
| 1500 |
break; |
| 1501 |
case OPTION_VMT_METADATA_FILE: |
| 1502 |
vmt_metadata_file = optarg; |
| 1503 |
break; |
| 1504 |
case OPTION_BINARY_METADATA_TRACK: |
| 1505 |
binary_metadata_track = true; |
| 1506 |
break; |
| 1507 |
case OPTION_METADATA_TRACK_URI: |
| 1508 |
metadata_track_uri = optarg; |
| 1509 |
break; |
| 1510 |
case OPTION_SET_CLLI: { |
| 1511 |
auto clli_args = parse_comma_separated_numeric_arguments<uint16_t>(optarg, |
| 1512 |
{ |
| 1513 |
std::numeric_limits<uint16_t>::max(), |
| 1514 |
std::numeric_limits<uint16_t>::max() |
| 1515 |
}); |
| 1516 |
if (clli_args.empty()) { |
| 1517 |
std::cerr << "Invalid arguments for --clli option.\n"; |
| 1518 |
} |
| 1519 |
else { |
| 1520 |
heif_content_light_level clliVal; |
| 1521 |
clliVal.max_content_light_level = clli_args[0]; |
| 1522 |
clliVal.max_pic_average_light_level = clli_args[1]; |
| 1523 |
clli = clliVal; |
| 1524 |
} |
| 1525 |
break; |
| 1526 |
} |
| 1527 |
case OPTION_SET_PASP: { |
| 1528 |
auto pasp_args = parse_comma_separated_numeric_arguments<uint32_t>(optarg, |
| 1529 |
{ |
| 1530 |
std::numeric_limits<uint32_t>::max(), |
| 1531 |
std::numeric_limits<uint32_t>::max() |
| 1532 |
}); |
| 1533 |
if (pasp_args.empty()) { |
| 1534 |
std::cerr << "Invalid arguments for --pasp option.\n"; |
| 1535 |
} |
| 1536 |
else { |
| 1537 |
pixel_aspect_ratio aspect_ratio; |
| 1538 |
aspect_ratio.h = pasp_args[0]; |
| 1539 |
aspect_ratio.v = pasp_args[1]; |
| 1540 |
pasp = aspect_ratio; |
| 1541 |
} |
| 1542 |
break; |
| 1543 |
} |
| 1544 |
case OPTION_ADD_MIME_ITEM: |
| 1545 |
option_mime_item_type = optarg; |
| 1546 |
break; |
| 1547 |
case OPTION_MIME_ITEM_FILE: |
| 1548 |
option_mime_item_file = optarg; |
| 1549 |
break; |
| 1550 |
case OPTION_MIME_ITEM_NAME: |
| 1551 |
option_mime_item_name = optarg; |
| 1552 |
break; |
| 1553 |
case OPTION_METADATA_COMPRESSION: { |
| 1554 |
bool success = set_metadata_compression_method(optarg); |
| 1555 |
if (!success) { |
| 1556 |
exit(5); |
| 1557 |
} |
| 1558 |
break; |
| 1559 |
} |
| 1560 |
case OPTION_SEQUENCES_GIMI_TRACK_ID: |
| 1561 |
option_gimi_track_id = optarg; |
| 1562 |
break; |
| 1563 |
case OPTION_SEQUENCES_SAI_DATA_FILE: |
| 1564 |
option_sai_data_file = optarg; |
| 1565 |
break; |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
if (quality < 0 || quality > 100) { |
| 1570 |
std::cerr << "Invalid quality factor. Must be between 0 and 100.\n"; |
| 1571 |
return 5; |
| 1572 |
} |
| 1573 |
|
| 1574 |
if ((force_enc_av1f ? 1 : 0) + (force_enc_vvc ? 1 : 0) + (force_enc_uncompressed ? 1 : 0) + (force_enc_jpeg ? 1 : 0) + |
| 1575 |
(force_enc_jpeg2000 ? 1 : 0) + (force_enc_avc ? 1 : 0) + (force_enc_hevc ? 1 : 0) > 1) { |
| 1576 |
std::cerr << "Choose at most one output compression format.\n"; |
| 1577 |
return 5; |
| 1578 |
} |
| 1579 |
|
| 1580 |
if (encode_sequence && (use_tiling || cut_tiles)) { |
| 1581 |
std::cerr << "Image sequences cannot be used together with tiling.\n"; |
| 1582 |
return 5; |
| 1583 |
} |
| 1584 |
|
| 1585 |
if (sequence_timebase <= 0) { |
| 1586 |
std::cerr << "Sequence clock tick rate cannot be zero.\n"; |
| 1587 |
return 5; |
| 1588 |
} |
| 1589 |
|
| 1590 |
if (sequence_durations <= 0) { |
| 1591 |
std::cerr << "Sequence frame durations cannot be zero.\n"; |
| 1592 |
return 5; |
| 1593 |
} |
| 1594 |
|
| 1595 |
if (encode_sequence && !option_mime_item_file.empty()) { |
| 1596 |
std::cerr << "MIME item cannot be added to sequence-only files.\n"; |
| 1597 |
return 5; |
| 1598 |
} |
| 1599 |
|
| 1600 |
if (!option_sai_data_file.empty() && !encode_sequence) { |
| 1601 |
std::cerr << "Image SAI data can only be used with sequences.\n"; |
| 1602 |
return 5; |
| 1603 |
} |
| 1604 |
|
| 1605 |
if (logging_level > 0) { |
| 1606 |
logging_level += 2; |
| 1607 |
|
| 1608 |
if (logging_level > 4) { |
| 1609 |
logging_level = 4; |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
// ============================================================================== |
| 1615 |
|
| 1616 |
struct heif_encoder* encoder = nullptr; |
| 1617 |
|
| 1618 |
if (list_encoders) { |
| 1619 |
show_list_of_all_encoders(); |
| 1620 |
return 0; |
| 1621 |
} |
| 1622 |
|
| 1623 |
// --- determine output compression format (from output filename or command line parameter) |
| 1624 |
|
| 1625 |
heif_compression_format compressionFormat; |
| 1626 |
|
| 1627 |
if (force_enc_av1f) { |
| 1628 |
compressionFormat = heif_compression_AV1; |
| 1629 |
} |
| 1630 |
else if (force_enc_vvc) { |
| 1631 |
compressionFormat = heif_compression_VVC; |
| 1632 |
} |
| 1633 |
else if (force_enc_avc) { |
| 1634 |
compressionFormat = heif_compression_AVC; |
| 1635 |
} |
| 1636 |
else if (force_enc_uncompressed) { |
| 1637 |
compressionFormat = heif_compression_uncompressed; |
| 1638 |
} |
| 1639 |
else if (force_enc_jpeg) { |
| 1640 |
compressionFormat = heif_compression_JPEG; |
| 1641 |
} |
| 1642 |
else if (force_enc_jpeg2000) { |
| 1643 |
compressionFormat = heif_compression_JPEG2000; |
| 1644 |
} |
| 1645 |
else if (force_enc_htj2k) { |
| 1646 |
compressionFormat = heif_compression_HTJ2K; |
| 1647 |
} |
| 1648 |
else if (force_enc_hevc) { |
| 1649 |
compressionFormat = heif_compression_HEVC; |
| 1650 |
} |
| 1651 |
else { |
| 1652 |
compressionFormat = guess_compression_format_from_filename(output_filename); |
| 1653 |
} |
| 1654 |
|
| 1655 |
if (compressionFormat == heif_compression_undefined) { |
| 1656 |
compressionFormat = heif_compression_HEVC; |
| 1657 |
} |
| 1658 |
|
| 1659 |
|
| 1660 |
// --- select encoder |
| 1661 |
|
| 1662 |
std::shared_ptr<heif_context> context(heif_context_alloc(), |
| 1663 |
[](heif_context* c) { heif_context_free(c); }); |
| 1664 |
if (!context) { |
| 1665 |
std::cerr << "Could not create context object\n"; |
| 1666 |
return 1; |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
#define MAX_ENCODERS 10 |
| 1671 |
const heif_encoder_descriptor* encoder_descriptors[MAX_ENCODERS]; |
| 1672 |
int count = heif_get_encoder_descriptors(compressionFormat, |
| 1673 |
nullptr, |
| 1674 |
encoder_descriptors, MAX_ENCODERS); |
| 1675 |
#undef MAX_ENCODERS |
| 1676 |
|
| 1677 |
const heif_encoder_descriptor* active_encoder_descriptor = nullptr; |
| 1678 |
if (count > 0) { |
| 1679 |
int idx = 0; |
| 1680 |
if (encoderId != nullptr) { |
| 1681 |
for (int i = 0; i <= count; i++) { |
| 1682 |
if (i == count) { |
| 1683 |
std::cerr << "Unknown encoder ID. Choose one from the list below.\n"; |
| 1684 |
show_list_of_encoders(encoder_descriptors, count); |
| 1685 |
return 5; |
| 1686 |
} |
| 1687 |
|
| 1688 |
if (strcmp(encoderId, heif_encoder_descriptor_get_id_name(encoder_descriptors[i])) == 0) { |
| 1689 |
idx = i; |
| 1690 |
break; |
| 1691 |
} |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
heif_error error = heif_context_get_encoder(context.get(), encoder_descriptors[idx], &encoder); |
| 1696 |
if (error.code) { |
| 1697 |
std::cerr << error.message << "\n"; |
| 1698 |
return 5; |
| 1699 |
} |
| 1700 |
|
| 1701 |
active_encoder_descriptor = encoder_descriptors[idx]; |
| 1702 |
} |
| 1703 |
else { |
| 1704 |
std::cerr << "No " << get_compression_format_name(compressionFormat) << " encoder available.\n"; |
| 1705 |
return 5; |
| 1706 |
} |
| 1707 |
|
| 1708 |
if (option_show_parameters) { |
| 1709 |
list_encoder_parameters(encoder); |
| 1710 |
heif_encoder_release(encoder); |
| 1711 |
return 0; |
| 1712 |
} |
| 1713 |
|
| 1714 |
if (optind > argc - 1) { |
| 1715 |
show_help(argv[0]); |
| 1716 |
return 0; |
| 1717 |
} |
| 1718 |
|
| 1719 |
|
| 1720 |
if (lossless && !heif_encoder_descriptor_supports_lossless_compression(active_encoder_descriptor)) { |
| 1721 |
std::cerr << "Warning: the selected encoder does not support lossless encoding. Encoding in lossy mode.\n"; |
| 1722 |
lossless = false; |
| 1723 |
} |
| 1724 |
|
| 1725 |
// If we were given a list of filenames and no '-o' option, check whether the last filename is the desired output filename. |
| 1726 |
|
| 1727 |
if (output_filename.empty() && argc>1) { |
| 1728 |
if (guess_compression_format_from_filename(argv[argc-1]) != heif_compression_undefined) { |
| 1729 |
output_filename = argv[argc-1]; |
| 1730 |
argc--; |
| 1731 |
} |
| 1732 |
} |
| 1733 |
|
| 1734 |
std::vector<std::string> args; |
| 1735 |
for (; optind < argc; optind++) { |
| 1736 |
args.emplace_back(argv[optind]); |
| 1737 |
} |
| 1738 |
|
| 1739 |
|
| 1740 |
// If we get a list of image filenames, but no '-o' option, assume that the last option |
| 1741 |
// denotes the output filename. |
| 1742 |
|
| 1743 |
if (output_filename.empty() && args.size() > 1) { |
| 1744 |
output_filename = args.back(); |
| 1745 |
args.pop_back(); |
| 1746 |
} |
| 1747 |
|
| 1748 |
|
| 1749 |
if (!lossless) { |
| 1750 |
heif_encoder_set_lossy_quality(encoder, quality); |
| 1751 |
} |
| 1752 |
|
| 1753 |
heif_encoder_set_logging_level(encoder, logging_level); |
| 1754 |
|
| 1755 |
set_params(encoder, raw_params); |
| 1756 |
struct heif_encoding_options* options = heif_encoding_options_alloc(); |
| 1757 |
options->save_two_colr_boxes_when_ICC_and_nclx_available = (uint8_t) two_colr_boxes; |
| 1758 |
|
| 1759 |
if (chroma_downsampling == "average") { |
| 1760 |
options->color_conversion_options.preferred_chroma_downsampling_algorithm = heif_chroma_downsampling_average; |
| 1761 |
options->color_conversion_options.only_use_preferred_chroma_algorithm = true; |
| 1762 |
} |
| 1763 |
else if (chroma_downsampling == "sharp-yuv") { |
| 1764 |
options->color_conversion_options.preferred_chroma_downsampling_algorithm = heif_chroma_downsampling_sharp_yuv; |
| 1765 |
options->color_conversion_options.only_use_preferred_chroma_algorithm = true; |
| 1766 |
} |
| 1767 |
else if (chroma_downsampling == "nearest-neighbor") { |
| 1768 |
options->color_conversion_options.preferred_chroma_downsampling_algorithm = heif_chroma_downsampling_nearest_neighbor; |
| 1769 |
options->color_conversion_options.only_use_preferred_chroma_algorithm = true; |
| 1770 |
} |
| 1771 |
|
| 1772 |
|
| 1773 |
// --- if no output filename was given, synthesize one from the first input image filename |
| 1774 |
|
| 1775 |
if (output_filename.empty()) { |
| 1776 |
const std::string& first_input_filename = args[0]; |
| 1777 |
|
| 1778 |
std::string filename_without_suffix; |
| 1779 |
std::string::size_type dot_position = first_input_filename.find_last_of('.'); |
| 1780 |
if (dot_position != std::string::npos) { |
| 1781 |
filename_without_suffix = first_input_filename.substr(0, dot_position); |
| 1782 |
} |
| 1783 |
else { |
| 1784 |
filename_without_suffix = first_input_filename; |
| 1785 |
} |
| 1786 |
|
| 1787 |
std::string suffix = suffix_for_compression_format(compressionFormat); |
| 1788 |
output_filename = filename_without_suffix + '.' + suffix; |
| 1789 |
} |
| 1790 |
|
| 1791 |
|
| 1792 |
int ret; |
| 1793 |
|
| 1794 |
if (!encode_sequence) { |
| 1795 |
ret = do_encode_images(context.get(), encoder, options, args); |
| 1796 |
} |
| 1797 |
else { |
| 1798 |
ret = do_encode_sequence(context.get(), encoder, options, args); |
| 1799 |
} |
| 1800 |
|
| 1801 |
if (ret != 0) { |
| 1802 |
heif_encoding_options_free(options); |
| 1803 |
heif_encoder_release(encoder); |
| 1804 |
return ret; |
| 1805 |
} |
| 1806 |
|
| 1807 |
|
| 1808 |
// --- write HEIF file |
| 1809 |
|
| 1810 |
heif_error error = heif_context_write_to_file(context.get(), output_filename.c_str()); |
| 1811 |
if (error.code) { |
| 1812 |
std::cerr << error.message << "\n"; |
| 1813 |
return 5; |
| 1814 |
} |
| 1815 |
|
| 1816 |
heif_encoding_options_free(options); |
| 1817 |
heif_encoder_release(encoder); |
| 1818 |
|
| 1819 |
return 0; |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
int do_encode_images(heif_context* context, heif_encoder* encoder, heif_encoding_options* options, const std::vector<std::string>& args) |
| 1824 |
{ |
| 1825 |
std::shared_ptr<heif_image> primary_image; |
| 1826 |
|
| 1827 |
bool is_primary_image = true; |
| 1828 |
|
| 1829 |
std::vector<heif_item_id> encoded_image_ids; |
| 1830 |
|
| 1831 |
for (std::string input_filename : args) { |
| 1832 |
|
| 1833 |
InputImage input_image = load_image(input_filename, output_bit_depth); |
| 1834 |
|
| 1835 |
std::shared_ptr<heif_image> image = input_image.image; |
| 1836 |
|
| 1837 |
heif_image_tiling tiling{}; |
| 1838 |
std::shared_ptr<input_tiles_generator> tile_generator; |
| 1839 |
if (use_tiling) { |
| 1840 |
tile_generator = determine_input_images_tiling(input_filename, tiled_input_x_y); |
| 1841 |
if (tile_generator) { |
| 1842 |
tiling.version = 1; |
| 1843 |
|
| 1844 |
tiling.num_columns = tile_generator->nColumns(); |
| 1845 |
tiling.num_rows = tile_generator->nRows(); |
| 1846 |
tiling.tile_width = heif_image_get_primary_width(image.get()); |
| 1847 |
tiling.tile_height = heif_image_get_primary_height(image.get()); |
| 1848 |
tiling.image_width = tiling.num_columns * tiling.tile_width; |
| 1849 |
tiling.image_height = tiling.num_rows * tiling.tile_height; |
| 1850 |
tiling.number_of_extra_dimensions = 0; |
| 1851 |
} |
| 1852 |
|
| 1853 |
if (tiled_image_width) tiling.image_width = tiled_image_width; |
| 1854 |
if (tiled_image_height) tiling.image_height = tiled_image_height; |
| 1855 |
|
| 1856 |
if (!tile_generator || tile_generator->nTiles()==1) { |
| 1857 |
std::cerr << "Cannot enumerate input tiles. Please use filenames with the two tile coordinates in the name.\n"; |
| 1858 |
return 5; |
| 1859 |
} |
| 1860 |
} |
| 1861 |
else if (cut_tiles != 0) { |
| 1862 |
auto cutting_tile_generator = std::make_shared<input_tiles_generator_cut_image>(input_filename.c_str(), |
| 1863 |
cut_tiles, output_bit_depth); |
| 1864 |
tile_generator = cutting_tile_generator; |
| 1865 |
|
| 1866 |
tiling.num_columns = tile_generator->nColumns(); |
| 1867 |
tiling.num_rows = tile_generator->nRows(); |
| 1868 |
tiling.tile_width = cut_tiles; |
| 1869 |
tiling.tile_height = cut_tiles; |
| 1870 |
tiling.image_width = cutting_tile_generator->get_image_width(); |
| 1871 |
tiling.image_height = cutting_tile_generator->get_image_height(); |
| 1872 |
tiling.number_of_extra_dimensions = 0; |
| 1873 |
} |
| 1874 |
|
| 1875 |
if (!primary_image) { |
| 1876 |
primary_image = image; |
| 1877 |
} |
| 1878 |
|
| 1879 |
#if HAVE_GETTIMEOFDAY |
| 1880 |
if (run_benchmark) { |
| 1881 |
gettimeofday(&time_encoding_start, nullptr); |
| 1882 |
} |
| 1883 |
#endif |
| 1884 |
|
| 1885 |
heif_color_profile_nclx* nclx; |
| 1886 |
heif_error error = create_output_nclx_profile_and_configure_encoder(encoder, &nclx, primary_image, |
| 1887 |
lossless, output_color_profile_preset); |
| 1888 |
if (error.code) { |
| 1889 |
std::cerr << error.message << "\n"; |
| 1890 |
return 5; |
| 1891 |
} |
| 1892 |
|
| 1893 |
options->save_alpha_channel = (uint8_t) master_alpha; |
| 1894 |
options->output_nclx_profile = nclx; |
| 1895 |
options->image_orientation = input_image.orientation; |
| 1896 |
|
| 1897 |
if (premultiplied_alpha) { |
| 1898 |
heif_image_set_premultiplied_alpha(image.get(), premultiplied_alpha); |
| 1899 |
} |
| 1900 |
|
| 1901 |
heif_image_handle* handle; |
| 1902 |
|
| 1903 |
if (use_tiling || cut_tiles > 0) { |
| 1904 |
handle = encode_tiled(context, encoder, options, output_bit_depth, tile_generator, tiling); |
| 1905 |
} |
| 1906 |
else { |
| 1907 |
error = heif_context_encode_image(context, |
| 1908 |
image.get(), |
| 1909 |
encoder, |
| 1910 |
options, |
| 1911 |
&handle); |
| 1912 |
if (error.code != 0) { |
| 1913 |
heif_nclx_color_profile_free(nclx); |
| 1914 |
std::cerr << "Could not encode HEIF/AVIF file: " << error.message << "\n"; |
| 1915 |
return 1; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
|
| 1919 |
if (handle==nullptr) { |
| 1920 |
std::cerr << "Could not encode image\n"; |
| 1921 |
return 1; |
| 1922 |
} |
| 1923 |
|
| 1924 |
if (clli) { |
| 1925 |
heif_image_handle_set_content_light_level(handle, &*clli); |
| 1926 |
} |
| 1927 |
|
| 1928 |
if (pasp) { |
| 1929 |
heif_image_handle_set_pixel_aspect_ratio(handle, pasp->h, pasp->v); |
| 1930 |
} |
| 1931 |
|
| 1932 |
if (is_primary_image) { |
| 1933 |
heif_context_set_primary_image(context, handle); |
| 1934 |
} |
| 1935 |
|
| 1936 |
encoded_image_ids.push_back(heif_image_handle_get_item_id(handle)); |
| 1937 |
|
| 1938 |
// write EXIF to HEIC |
| 1939 |
if (!input_image.exif.empty()) { |
| 1940 |
// Note: we do not modify the EXIF Orientation here because we want it to match the HEIF transforms. |
| 1941 |
// TODO: is this a good choice? Or should we set it to 1 (normal) so that other, faulty software will not transform it once more? |
| 1942 |
|
| 1943 |
error = heif_context_add_exif_metadata(context, handle, |
| 1944 |
input_image.exif.data(), (int) input_image.exif.size()); |
| 1945 |
if (error.code != 0) { |
| 1946 |
heif_nclx_color_profile_free(nclx); |
| 1947 |
std::cerr << "Could not write EXIF metadata: " << error.message << "\n"; |
| 1948 |
return 1; |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
// write XMP to HEIC |
| 1953 |
if (!input_image.xmp.empty()) { |
| 1954 |
error = heif_context_add_XMP_metadata2(context, handle, |
| 1955 |
input_image.xmp.data(), (int) input_image.xmp.size(), |
| 1956 |
metadata_compression_method); |
| 1957 |
if (error.code != 0) { |
| 1958 |
heif_nclx_color_profile_free(nclx); |
| 1959 |
std::cerr << "Could not write XMP metadata: " << error.message << "\n"; |
| 1960 |
return 1; |
| 1961 |
} |
| 1962 |
} |
| 1963 |
|
| 1964 |
if (thumbnail_bbox_size > 0) { |
| 1965 |
// encode thumbnail |
| 1966 |
|
| 1967 |
struct heif_image_handle* thumbnail_handle; |
| 1968 |
|
| 1969 |
options->save_alpha_channel = master_alpha && thumb_alpha; |
| 1970 |
|
| 1971 |
error = heif_context_encode_thumbnail(context, |
| 1972 |
image.get(), |
| 1973 |
handle, |
| 1974 |
encoder, |
| 1975 |
options, |
| 1976 |
thumbnail_bbox_size, |
| 1977 |
&thumbnail_handle); |
| 1978 |
if (error.code) { |
| 1979 |
heif_nclx_color_profile_free(nclx); |
| 1980 |
std::cerr << "Could not generate thumbnail: " << error.message << "\n"; |
| 1981 |
return 5; |
| 1982 |
} |
| 1983 |
|
| 1984 |
if (thumbnail_handle) { |
| 1985 |
heif_image_handle_release(thumbnail_handle); |
| 1986 |
} |
| 1987 |
} |
| 1988 |
|
| 1989 |
#if HAVE_GETTIMEOFDAY |
| 1990 |
if (run_benchmark) { |
| 1991 |
gettimeofday(&time_encoding_end, nullptr); |
| 1992 |
} |
| 1993 |
#endif |
| 1994 |
|
| 1995 |
heif_image_handle_release(handle); |
| 1996 |
heif_nclx_color_profile_free(nclx); |
| 1997 |
|
| 1998 |
is_primary_image = false; |
| 1999 |
} |
| 2000 |
|
| 2001 |
if (!property_pitm_description.empty()) { |
| 2002 |
heif_image_handle* primary_image_handle; |
| 2003 |
struct heif_error err = heif_context_get_primary_image_handle(context, &primary_image_handle); |
| 2004 |
if (err.code) { |
| 2005 |
std::cerr << "No primary image set, cannot set user description\n"; |
| 2006 |
return 5; |
| 2007 |
} |
| 2008 |
|
| 2009 |
heif_item_id pitm_id = heif_image_handle_get_item_id(primary_image_handle); |
| 2010 |
|
| 2011 |
heif_property_user_description udes; |
| 2012 |
udes.lang = nullptr; |
| 2013 |
udes.name = nullptr; |
| 2014 |
udes.tags = nullptr; |
| 2015 |
udes.description = property_pitm_description.c_str(); |
| 2016 |
err = heif_item_add_property_user_description(context, pitm_id, &udes, nullptr); |
| 2017 |
if (err.code) { |
| 2018 |
std::cerr << "Cannot set user description\n"; |
| 2019 |
return 5; |
| 2020 |
} |
| 2021 |
|
| 2022 |
heif_image_handle_release(primary_image_handle); |
| 2023 |
} |
| 2024 |
|
| 2025 |
#if HEIF_ENABLE_EXPERIMENTAL_FEATURES |
| 2026 |
if (add_pyramid_group && encoded_image_ids.size() > 1) { |
| 2027 |
heif_error error = heif_context_add_pyramid_entity_group(context, encoded_image_ids.data(), encoded_image_ids.size(), nullptr); |
| 2028 |
if (error.code) { |
| 2029 |
std::cerr << "Cannot set multi-resolution pyramid: " << error.message << "\n"; |
| 2030 |
return 5; |
| 2031 |
} |
| 2032 |
} |
| 2033 |
#endif |
| 2034 |
|
| 2035 |
// --- add extra MIME item with user data |
| 2036 |
|
| 2037 |
if (!option_mime_item_file.empty() || !option_mime_item_type.empty()) { |
| 2038 |
if (option_mime_item_file.empty() || option_mime_item_type.empty()) { |
| 2039 |
std::cerr << "Options --add-mime-item and --mime-item-file have to be used together\n"; |
| 2040 |
return 5; |
| 2041 |
} |
| 2042 |
|
| 2043 |
std::ifstream istr(option_mime_item_file.c_str(), std::ios::binary | std::ios::ate); |
| 2044 |
if (!istr) { |
| 2045 |
std::cerr << "Failed to open file for MIME item: '" << option_mime_item_file << "'\n"; |
| 2046 |
return 5; |
| 2047 |
} |
| 2048 |
|
| 2049 |
// Get size by seeking to the end (thanks to ios::ate) |
| 2050 |
std::streamsize size = istr.tellg(); |
| 2051 |
if (size < 0) { |
| 2052 |
std::cerr << "Querying size of file '" << option_mime_item_file << "' failed.\n"; |
| 2053 |
return 5; |
| 2054 |
} |
| 2055 |
|
| 2056 |
std::vector<uint8_t> buffer(size); |
| 2057 |
|
| 2058 |
// Seek back to beginning and read |
| 2059 |
istr.seekg(0, std::ios::beg); |
| 2060 |
istr.read(reinterpret_cast<char*>(buffer.data()), size); |
| 2061 |
|
| 2062 |
heif_item_id itemId; |
| 2063 |
heif_context_add_mime_item(context, option_mime_item_type.c_str(), |
| 2064 |
metadata_compression_method, |
| 2065 |
buffer.data(), (int)buffer.size(), |
| 2066 |
&itemId); |
| 2067 |
|
| 2068 |
if (!option_mime_item_name.empty()) { |
| 2069 |
heif_item_set_item_name(context, itemId, option_mime_item_name.c_str()); |
| 2070 |
} |
| 2071 |
} |
| 2072 |
|
| 2073 |
if (run_benchmark) { |
| 2074 |
double psnr = compute_psnr(primary_image.get(), output_filename); |
| 2075 |
std::cout << "PSNR: " << std::setprecision(2) << std::fixed << psnr << " "; |
| 2076 |
|
| 2077 |
#if HAVE_GETTIMEOFDAY |
| 2078 |
double t = (double) (time_encoding_end.tv_sec - time_encoding_start.tv_sec) + (double) (time_encoding_end.tv_usec - time_encoding_start.tv_usec) / 1000000.0; |
| 2079 |
std::cout << "time: " << std::setprecision(1) << std::fixed << t << " "; |
| 2080 |
#endif |
| 2081 |
|
| 2082 |
std::ifstream istr(output_filename.c_str()); |
| 2083 |
istr.seekg(0, std::ios_base::end); |
| 2084 |
std::streamoff size = istr.tellg(); |
| 2085 |
std::cout << "size: " << size << "\n"; |
| 2086 |
} |
| 2087 |
|
| 2088 |
return 0; |
| 2089 |
} |
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
std::vector<std::string> deflate_input_filenames(const std::string& filename_example) |
| 2095 |
{ |
| 2096 |
std::regex pattern(R"((.*\D)?(\d+)(\..+)$)"); |
| 2097 |
std::smatch match; |
| 2098 |
|
| 2099 |
if (!std::regex_match(filename_example, match, pattern)) { |
| 2100 |
return {filename_example}; |
| 2101 |
} |
| 2102 |
|
| 2103 |
std::string prefix = match[1]; |
| 2104 |
|
| 2105 |
auto p = std::filesystem::absolute(std::filesystem::path(prefix)); |
| 2106 |
std::filesystem::path directory = p.parent_path(); |
| 2107 |
std::string filename_prefix = p.filename().string(); // TODO: we could also use u8string(), but it is not well supported in C++20 |
| 2108 |
std::string number = match[2]; |
| 2109 |
std::string suffix = match[3]; |
| 2110 |
|
| 2111 |
|
| 2112 |
std::string patternString = filename_prefix + "(\\d+)" + suffix + "$"; |
| 2113 |
pattern = patternString; |
| 2114 |
|
| 2115 |
uint32_t digits = std::numeric_limits<uint32_t>::max(); |
| 2116 |
uint32_t start = std::numeric_limits<uint32_t>::max(); |
| 2117 |
uint32_t end = 0; |
| 2118 |
|
| 2119 |
for (const auto& dirEntry : std::filesystem::directory_iterator(directory)) |
| 2120 |
{ |
| 2121 |
if (dirEntry.is_regular_file()) { |
| 2122 |
std::string s{dirEntry.path().filename().string()}; |
| 2123 |
|
| 2124 |
if (std::regex_match(s, match, pattern)) { |
| 2125 |
digits = std::min(digits, (uint32_t)match[1].length()); |
| 2126 |
|
| 2127 |
uint32_t number = std::stoi(match[1]); |
| 2128 |
start = std::min(start, number); |
| 2129 |
end = std::max(end, number); |
| 2130 |
} |
| 2131 |
} |
| 2132 |
} |
| 2133 |
|
| 2134 |
|
| 2135 |
std::vector<std::string> files; |
| 2136 |
|
| 2137 |
for (uint32_t i=start;i<=end;i++) |
| 2138 |
{ |
| 2139 |
std::stringstream sstr; |
| 2140 |
|
| 2141 |
sstr << prefix << std::setw(digits) << std::setfill('0') << i << suffix; |
| 2142 |
|
| 2143 |
std::filesystem::path p = directory / sstr.str(); |
| 2144 |
files.emplace_back(p.string()); |
| 2145 |
} |
| 2146 |
|
| 2147 |
return files; |
| 2148 |
} |
| 2149 |
|
| 2150 |
|
| 2151 |
std::optional<uint8_t> nibble_to_val(char c) |
| 2152 |
{ |
| 2153 |
if (c>='0' && c<='9') { |
| 2154 |
return c - '0'; |
| 2155 |
} |
| 2156 |
if (c>='a' && c<='f') { |
| 2157 |
return c - 'a' + 10; |
| 2158 |
} |
| 2159 |
if (c>='A' && c<='F') { |
| 2160 |
return c - 'A' + 10; |
| 2161 |
} |
| 2162 |
|
| 2163 |
return std::nullopt; |
| 2164 |
} |
| 2165 |
|
| 2166 |
// Convert hex data to raw binary. Ignore any non-hex characters. |
| 2167 |
static std::vector<uint8_t> hex_to_binary(const std::string& line) |
| 2168 |
{ |
| 2169 |
std::vector<uint8_t> data; |
| 2170 |
uint8_t current_value = 0; |
| 2171 |
|
| 2172 |
bool high_nibble = true; |
| 2173 |
for (auto c : line) { |
| 2174 |
auto v = nibble_to_val(c); |
| 2175 |
if (v) { |
| 2176 |
if (high_nibble) { |
| 2177 |
current_value = static_cast<uint8_t>(*v << 4); |
| 2178 |
high_nibble = false; |
| 2179 |
} |
| 2180 |
else { |
| 2181 |
current_value |= *v; |
| 2182 |
data.push_back(current_value); |
| 2183 |
high_nibble = true; |
| 2184 |
} |
| 2185 |
} |
| 2186 |
} |
| 2187 |
|
| 2188 |
return data; |
| 2189 |
} |
| 2190 |
|
| 2191 |
|
| 2192 |
int encode_vmt_metadata_track(heif_context* context, heif_track* visual_track, |
| 2193 |
const std::string& track_uri, bool binary) |
| 2194 |
{ |
| 2195 |
// --- add metadata track |
| 2196 |
|
| 2197 |
heif_track* track = nullptr; |
| 2198 |
|
| 2199 |
heif_track_options* track_options = heif_track_options_alloc(); |
| 2200 |
heif_track_options_set_timescale(track_options, 1000); |
| 2201 |
|
| 2202 |
heif_context_add_uri_metadata_sequence_track(context, track_uri.c_str(), track_options, &track); |
| 2203 |
heif_raw_sequence_sample* sample = heif_raw_sequence_sample_alloc(); |
| 2204 |
|
| 2205 |
|
| 2206 |
std::ifstream istr(vmt_metadata_file.c_str()); |
| 2207 |
|
| 2208 |
std::regex pattern(R"((\d\d):(\d\d):(\d\d).(\d\d\d) -->$)"); |
| 2209 |
|
| 2210 |
static std::vector<uint8_t> prev_metadata; |
| 2211 |
static std::optional<uint32_t> prev_ts; |
| 2212 |
|
| 2213 |
std::string line; |
| 2214 |
while (std::getline(istr, line)) |
| 2215 |
{ |
| 2216 |
std::smatch match; |
| 2217 |
|
| 2218 |
if (!std::regex_match(line, match, pattern)) { |
| 2219 |
continue; |
| 2220 |
} |
| 2221 |
|
| 2222 |
std::string hh = match[1]; |
| 2223 |
std::string mm = match[2]; |
| 2224 |
std::string ss = match[3]; |
| 2225 |
std::string mil = match[4]; |
| 2226 |
|
| 2227 |
uint32_t ts = (std::stoi(hh) * 3600 * 1000 + |
| 2228 |
std::stoi(mm) * 60 * 1000 + |
| 2229 |
std::stoi(ss) * 1000 + |
| 2230 |
std::stoi(mil)); |
| 2231 |
|
| 2232 |
std::vector<uint8_t> concat; |
| 2233 |
|
| 2234 |
if (binary) { |
| 2235 |
while (std::getline(istr, line)) { |
| 2236 |
if (line.empty()) { |
| 2237 |
break; |
| 2238 |
} |
| 2239 |
|
| 2240 |
std::vector<uint8_t> binaryData = hex_to_binary(line); |
| 2241 |
concat.insert(concat.end(), binaryData.begin(), binaryData.end()); |
| 2242 |
} |
| 2243 |
|
| 2244 |
} |
| 2245 |
else { |
| 2246 |
while (std::getline(istr, line)) { |
| 2247 |
if (line.empty()) { |
| 2248 |
break; |
| 2249 |
} |
| 2250 |
|
| 2251 |
concat.insert(concat.end(), line.data(), line.data() + line.length()); |
| 2252 |
concat.push_back('\n'); |
| 2253 |
} |
| 2254 |
|
| 2255 |
concat.push_back(0); |
| 2256 |
} |
| 2257 |
|
| 2258 |
if (prev_ts) { |
| 2259 |
heif_raw_sequence_sample_set_data(sample, (const uint8_t*)prev_metadata.data(), prev_metadata.size()); |
| 2260 |
heif_raw_sequence_sample_set_duration(sample, ts - *prev_ts); |
| 2261 |
heif_track_add_raw_sequence_sample(track, sample); |
| 2262 |
} |
| 2263 |
|
| 2264 |
prev_ts = ts; |
| 2265 |
prev_metadata = concat; |
| 2266 |
} |
| 2267 |
|
| 2268 |
// --- flush last metadata packet |
| 2269 |
|
| 2270 |
heif_raw_sequence_sample_set_data(sample, (const uint8_t*)prev_metadata.data(), prev_metadata.size()); |
| 2271 |
heif_raw_sequence_sample_set_duration(sample, 1); |
| 2272 |
heif_track_add_raw_sequence_sample(track, sample); |
| 2273 |
|
| 2274 |
// --- add track reference |
| 2275 |
|
| 2276 |
heif_track_add_reference_to_track(track, heif_track_reference_type_description, visual_track); |
| 2277 |
|
| 2278 |
// --- release all objects |
| 2279 |
|
| 2280 |
heif_raw_sequence_sample_release(sample); |
| 2281 |
heif_track_options_release(track_options); |
| 2282 |
heif_track_release(track); |
| 2283 |
|
| 2284 |
return 0; |
| 2285 |
} |
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
int do_encode_sequence(heif_context* context, heif_encoder* encoder, heif_encoding_options* options, std::vector<std::string> args) |
| 2290 |
{ |
| 2291 |
if (args.size() == 1) { |
| 2292 |
args = deflate_input_filenames(args[0]); |
| 2293 |
} |
| 2294 |
|
| 2295 |
size_t currImage = 0; |
| 2296 |
|
| 2297 |
size_t nImages = args.size(); |
| 2298 |
if (sequence_max_frames && nImages > static_cast<size_t>(sequence_max_frames)) { |
| 2299 |
nImages = sequence_max_frames; |
| 2300 |
} |
| 2301 |
|
| 2302 |
// --- optionally load SAI data to be used for the frames |
| 2303 |
|
| 2304 |
SAI_datafile sai_data; |
| 2305 |
if (!option_sai_data_file.empty()) { |
| 2306 |
sai_data.load_sai_data_from_file(option_sai_data_file.c_str()); |
| 2307 |
} |
| 2308 |
|
| 2309 |
|
| 2310 |
uint16_t image_width=0, image_height=0; |
| 2311 |
|
| 2312 |
bool first_image = true; |
| 2313 |
|
| 2314 |
heif_track* track = nullptr; |
| 2315 |
heif_sequence_encoding_options* encoding_options = nullptr; |
| 2316 |
|
| 2317 |
for (std::string input_filename : args) { |
| 2318 |
currImage++; |
| 2319 |
if (currImage > nImages) { |
| 2320 |
break; |
| 2321 |
} |
| 2322 |
|
| 2323 |
std::cout << "\rencoding sequence image " << currImage << "/" << nImages; |
| 2324 |
std::cout.flush(); |
| 2325 |
|
| 2326 |
InputImage input_image = load_image(input_filename, output_bit_depth); |
| 2327 |
|
| 2328 |
std::shared_ptr<heif_image> image = input_image.image; |
| 2329 |
|
| 2330 |
int w = heif_image_get_primary_width(image.get()); |
| 2331 |
int h = heif_image_get_primary_height(image.get()); |
| 2332 |
|
| 2333 |
if (w > 0xFFFF || h > 0xFFFF) { |
| 2334 |
std::cerr << "maximum image size of 65535x65535 exceeded\n"; |
| 2335 |
return 5; |
| 2336 |
} |
| 2337 |
|
| 2338 |
if (first_image) { |
| 2339 |
heif_track_options* track_options = heif_track_options_alloc(); |
| 2340 |
heif_track_options_set_timescale(track_options, sequence_timebase); |
| 2341 |
|
| 2342 |
if (!option_gimi_track_id.empty()) { |
| 2343 |
heif_track_options_set_gimi_track_id(track_options, option_gimi_track_id.c_str()); |
| 2344 |
} |
| 2345 |
|
| 2346 |
if (sai_data.tai_clock_info) { |
| 2347 |
heif_track_options_enable_sample_tai_timestamps(track_options, |
| 2348 |
sai_data.tai_clock_info, |
| 2349 |
heif_sample_aux_info_presence_optional); |
| 2350 |
} |
| 2351 |
|
| 2352 |
if (!sai_data.gimi_content_ids.empty()) { |
| 2353 |
heif_track_options_enable_sample_gimi_content_ids(track_options, |
| 2354 |
heif_sample_aux_info_presence_optional); |
| 2355 |
} |
| 2356 |
|
| 2357 |
heif_context_set_sequence_timescale(context, sequence_timebase); |
| 2358 |
heif_context_set_number_of_sequence_repetitions(context, sequence_repetitions); |
| 2359 |
|
| 2360 |
encoding_options = heif_sequence_encoding_options_alloc(); |
| 2361 |
encoding_options->gop_structure = sequence_gop_structure; |
| 2362 |
encoding_options->keyframe_distance_min = sequence_keyframe_distance_min; |
| 2363 |
encoding_options->keyframe_distance_max = sequence_keyframe_distance_max; |
| 2364 |
encoding_options->save_alpha_channel = master_alpha; |
| 2365 |
|
| 2366 |
image_width = static_cast<uint16_t>(w); |
| 2367 |
image_height = static_cast<uint16_t>(h); |
| 2368 |
|
| 2369 |
heif_context_add_visual_sequence_track(context, |
| 2370 |
image_width, image_height, |
| 2371 |
use_video_handler ? heif_track_type_video : heif_track_type_image_sequence, |
| 2372 |
track_options, |
| 2373 |
encoding_options, |
| 2374 |
&track); |
| 2375 |
|
| 2376 |
heif_track_options_release(track_options); |
| 2377 |
|
| 2378 |
first_image = false; |
| 2379 |
} |
| 2380 |
|
| 2381 |
if (image_width != static_cast<uint16_t>(w) || |
| 2382 |
image_height != static_cast<uint16_t>(h)) { |
| 2383 |
std::cerr << "image '" << input_filename << "' has size " << w << "x" << h |
| 2384 |
<< " which is different from the first image size " << image_width << "x" << image_height << "\n"; |
| 2385 |
return 5; |
| 2386 |
} |
| 2387 |
|
| 2388 |
heif_color_profile_nclx* nclx; |
| 2389 |
heif_error error = create_output_nclx_profile_and_configure_encoder(encoder, &nclx, image, lossless, output_color_profile_preset); |
| 2390 |
if (error.code) { |
| 2391 |
std::cerr << error.message << "\n"; |
| 2392 |
return 5; |
| 2393 |
} |
| 2394 |
|
| 2395 |
//seq_options->save_alpha_channel = false; // TODO: sequences with alpha ? |
| 2396 |
encoding_options->output_nclx_profile = nclx; |
| 2397 |
//seq_options->image_orientation = heif_orientation_normal; // input_image.orientation; TODO: sequence rotation |
| 2398 |
|
| 2399 |
heif_image_set_duration(image.get(), sequence_durations); |
| 2400 |
|
| 2401 |
// --- set SAI data |
| 2402 |
|
| 2403 |
if (currImage-1 < sai_data.gimi_content_ids.size()) { |
| 2404 |
if (!sai_data.gimi_content_ids[currImage-1].empty()) { |
| 2405 |
heif_image_set_gimi_sample_content_id(image.get(), sai_data.gimi_content_ids[currImage-1].c_str()); |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
if (currImage-1 < sai_data.tai_timestamps.size()) { |
| 2410 |
if (sai_data.tai_timestamps[currImage-1]) { |
| 2411 |
heif_image_set_tai_timestamp(image.get(), sai_data.tai_timestamps[currImage-1]); |
| 2412 |
} |
| 2413 |
} |
| 2414 |
|
| 2415 |
// --- encode image |
| 2416 |
|
| 2417 |
error = heif_track_encode_sequence_image(track, image.get(), encoder, encoding_options); |
| 2418 |
if (error.code) { |
| 2419 |
heif_nclx_color_profile_free(nclx); |
| 2420 |
std::cerr << "Cannot encode sequence image: " << error.message << "\n"; |
| 2421 |
return 5; |
| 2422 |
} |
| 2423 |
|
| 2424 |
heif_nclx_color_profile_free(nclx); |
| 2425 |
} |
| 2426 |
|
| 2427 |
std::cout << "\n"; |
| 2428 |
|
| 2429 |
heif_error error = heif_track_encode_end_of_sequence(track, encoder); |
| 2430 |
if (error.code) { |
| 2431 |
std::cerr << "Cannot end sequence: " << error.message << "\n"; |
| 2432 |
return 5; |
| 2433 |
} |
| 2434 |
|
| 2435 |
if (!vmt_metadata_file.empty()) { |
| 2436 |
int ret = encode_vmt_metadata_track(context, track, metadata_track_uri, binary_metadata_track); |
| 2437 |
if (ret) { |
| 2438 |
return ret; |
| 2439 |
} |
| 2440 |
} |
| 2441 |
|
| 2442 |
heif_track_release(track); |
| 2443 |
heif_sequence_encoding_options_release(encoding_options); |
| 2444 |
|
| 2445 |
|
| 2446 |
// --- add first image as image item |
| 2447 |
|
| 2448 |
if (!use_video_handler) { |
| 2449 |
do_encode_images(context, encoder, options, {args[0]}); |
| 2450 |
} |
| 2451 |
|
| 2452 |
return 0; |
| 2453 |
} |