libheif
heif_encoding.h
Go to the documentation of this file.
1 /*
2  * HEIF codec.
3  * Copyright (c) 2017-2025 Dirk Farin <dirk.farin@gmail.com>
4  *
5  * This file is part of libheif.
6  *
7  * libheif is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * libheif is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with libheif. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef LIBHEIF_HEIF_ENCODING_H
22 #define LIBHEIF_HEIF_ENCODING_H
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 #include <libheif/heif_library.h>
32 #include <libheif/heif_image.h>
33 #include <libheif/heif_context.h>
34 #include <libheif/heif_brands.h>
35 #include <libheif/heif_color.h>
36 
37 // Forward declaration. The full definition lives in heif_uncompressed.h.
38 // heif_encoding_options only stores a pointer, so a forward typedef is enough here
39 // and avoids a circular include (heif_uncompressed.h pulls in heif.h, which pulls in this header).
41 
42 
43 // ----- encoder -----
44 
48 typedef struct heif_encoder heif_encoder;
49 
50 // A description of the encoder's capabilities and name.
52 
53 // A configuration parameter of the encoder. Each encoder implementation may have a different
54 // set of parameters. For the most common settings (e.q. quality), special functions to set
55 // the parameters are provided.
57 
58 
59 // Quick check whether there is an enoder available for the given format.
60 // Note that the encoder may be limited to a certain subset of features (e.g. only 8 bit, only lossy).
61 // You will have to query the specific capabilities further.
62 LIBHEIF_API
63 int heif_have_encoder_for_format(heif_compression_format format);
64 
65 // Get a list of available encoders. You can filter the encoders by compression format and name.
66 // Use format_filter==heif_compression_undefined and name_filter==NULL as wildcards.
67 // The returned list of encoders is sorted by their priority (which is a plugin property).
68 // The number of encoders is returned, which are not more than 'count' if (out_encoders != nullptr).
69 // By setting out_encoders==nullptr, you can query the number of encoders, 'count' is ignored.
70 // Note: to get the actual encoder from the descriptors returned here, use heif_context_get_encoder().
71 LIBHEIF_API
72 int heif_get_encoder_descriptors(heif_compression_format format_filter,
73  const char* name_filter,
74  const heif_encoder_descriptor** out_encoders,
75  int count);
76 
77 // Return a long, descriptive name of the encoder (including version information).
78 LIBHEIF_API
80 
81 // Return a short, symbolic name for identifying the encoder.
82 // This name should stay constant over different encoder versions.
83 LIBHEIF_API
85 
86 LIBHEIF_API
87 heif_compression_format
89 
90 LIBHEIF_API
92 
93 LIBHEIF_API
95 
96 
97 // Get an encoder instance that can be used to actually encode images from a descriptor.
98 LIBHEIF_API
101  heif_encoder** out_encoder);
102 
103 // Get an encoder for the given compression format. If there are several encoder plugins
104 // for this format, the encoder with the highest plugin priority will be returned.
105 LIBHEIF_API
107  heif_compression_format format,
108  heif_encoder**);
109 
113 LIBHEIF_API
115 
116 // Get the encoder name from the encoder itself.
117 LIBHEIF_API
119 
120 
121 // --- Encoder Parameters ---
122 
123 // Libheif supports settings parameters through specialized functions and through
124 // generic functions by parameter name. Sometimes, the same parameter can be set
125 // in both ways.
126 // We consider it best practice to use the generic parameter functions only in
127 // dynamically generated user interfaces, as no guarantees are made that some specific
128 // parameter names are supported by all plugins.
129 
130 
131 // Set a 'quality' factor (0-100). How this is mapped to actual encoding parameters is
132 // encoder dependent.
133 LIBHEIF_API
135 
136 LIBHEIF_API
137 heif_error heif_encoder_set_lossless(heif_encoder*, int enable);
138 
139 // level should be between 0 (= none) to 4 (= full)
140 LIBHEIF_API
142 
143 // Get a generic list of encoder parameters.
144 // Each encoder may define its own, additional set of parameters.
145 // You do not have to free the returned list.
146 LIBHEIF_API
148 
149 // Return the parameter name.
150 LIBHEIF_API
152 
153 
155 {
160 
161 // Return the parameter type.
162 LIBHEIF_API
164 
165 // DEPRECATED. Use heif_encoder_parameter_get_valid_integer_values() instead.
166 LIBHEIF_API
168  int* have_minimum_maximum,
169  int* minimum, int* maximum);
170 
171 // If integer is limited by a range, have_minimum and/or have_maximum will be != 0 and *minimum, *maximum is set.
172 // If integer is limited by a fixed set of values, *num_valid_values will be >0 and *out_integer_array is set.
173 LIBHEIF_API
175  int* have_minimum, int* have_maximum,
176  int* minimum, int* maximum,
177  int* num_valid_values,
178  const int** out_integer_array);
179 
180 LIBHEIF_API
182  const char* const** out_stringarray);
183 
184 
185 LIBHEIF_API
187  const char* parameter_name,
188  int value);
189 
190 LIBHEIF_API
192  const char* parameter_name,
193  int* value);
194 
195 // TODO: name should be changed to heif_encoder_get_valid_integer_parameter_range
196 LIBHEIF_API // DEPRECATED.
198  const char* parameter_name,
199  int* have_minimum_maximum,
200  int* minimum, int* maximum);
201 
202 LIBHEIF_API
204  const char* parameter_name,
205  int value);
206 
207 LIBHEIF_API
209  const char* parameter_name,
210  int* value);
211 
212 LIBHEIF_API
214  const char* parameter_name,
215  const char* value);
216 
217 LIBHEIF_API
219  const char* parameter_name,
220  char* value, int value_size);
221 
222 // returns a NULL-terminated list of valid strings or NULL if all values are allowed
223 LIBHEIF_API
225  const char* parameter_name,
226  const char* const** out_stringarray);
227 
228 LIBHEIF_API
230  const char* parameter_name,
231  int* have_minimum, int* have_maximum,
232  int* minimum, int* maximum,
233  int* num_valid_values,
234  const int** out_integer_array);
235 
236 // Set a parameter of any type to the string value.
237 // Integer values are parsed from the string.
238 // Boolean values can be "true"/"false"/"1"/"0"
239 //
240 // x265 encoder specific note:
241 // When using the x265 encoder, you may pass any of its parameters by
242 // prefixing the parameter name with 'x265:'. Hence, to set the 'ctu' parameter,
243 // you will have to set 'x265:ctu' in libheif.
244 // Note that there is no checking for valid parameters when using the prefix.
245 LIBHEIF_API
247  const char* parameter_name,
248  const char* value);
249 
250 // Get the current value of a parameter of any type as a human readable string.
251 // The returned string is compatible with heif_encoder_set_parameter().
252 LIBHEIF_API
254  const char* parameter_name,
255  char* value_ptr, int value_size);
256 
257 // Query whether a specific parameter has a default value.
258 LIBHEIF_API
260  const char* parameter_name);
261 
262 
263 // The orientation values are defined equal to the EXIF Orientation tag.
264 typedef enum heif_orientation
265 {
275 
276 
277 LIBHEIF_API
279 
280 
281 typedef struct heif_encoding_options
282 {
283  uint8_t version;
284 
285  // version 1 options
286 
287  uint8_t save_alpha_channel; // default: true
288 
289  // version 2 options
290 
291  // DEPRECATED. This option is not required anymore. Its value will be ignored.
293 
294  // version 3 options
295 
297 
298  // version 4 options
299 
300  // Set this to the NCLX parameters to be used in the output image or set to NULL
301  // when the same parameters as in the input image should be used.
302  heif_color_profile_nclx* output_nclx_profile;
303 
305 
306  // version 5 options
307 
308  // libheif will generate irot/imir boxes to match these orientations
310 
311  // version 6 options
312 
313  heif_color_conversion_options color_conversion_options;
314 
315  // version 7 options
316 
317  // Set this to true to use compressed form of uncC where possible.
319 
320  // version 8 options
321 
322  // Optional 'unci'-specific encoding parameters (compression method, and future fields
323  // such as interleave type and padding).
324  //
325  // Default: nullptr
327 
328  // TODO: we should add a flag to force MIAF compatible outputs. E.g. this will put restrictions on grid tile sizes and
329  // might add a clap box when the grid output size does not match the color subsampling factors.
330  // Since some of these constraints have to be known before actually encoding the image, "forcing MIAF compatibility"
331  // could also be a flag in the heif_context.
333 
334 LIBHEIF_API
336 
337 LIBHEIF_API
339 
340 LIBHEIF_API
342 
343 
344 // Compress the input image.
345 // Returns a handle to the coded image in 'out_image_handle' unless out_image_handle = NULL.
346 // 'options' should be NULL for now.
347 // The first image added to the context is also automatically set the primary image, but
348 // you can change the primary image later with heif_context_set_primary_image().
349 //
350 // The returned handle refers to an image that is being written. Use it to attach thumbnails,
351 // metadata or properties to the image, or to make it the primary image. It cannot be used
352 // to decode the image, and the functions that report the coded format of an image (bit
353 // depths, preferred colorspace, alpha channel) return an error or "unknown" for it.
354 LIBHEIF_API
356  const heif_image* image,
357  heif_encoder* encoder,
358  const heif_encoding_options* options,
359  heif_image_handle** out_image_handle);
360 
361 // offsets[] should either be NULL (all offsets==0) or an array of size 2*nImages with x;y offset pairs.
362 // If background_rgba is NULL, the background is transparent.
363 LIBHEIF_API
365  uint32_t image_width,
366  uint32_t image_height,
367  uint16_t nImages,
368  const heif_item_id* image_ids,
369  int32_t* offsets,
370  const uint16_t background_rgba[4],
371  heif_image_handle** out_iovl_image_handle);
372 
373 LIBHEIF_API
375  heif_image_handle* image_handle);
376 
377 // Set the major brand of the file.
378 // If this function is not called, the major brand is determined automatically from
379 // the image or sequence content.
380 LIBHEIF_API
382  heif_brand2 major_brand);
383 
384 // Add a compatible brand that is now added automatically by libheif when encoding images (e.g. some application brands like 'geo1').
385 LIBHEIF_API
387  heif_brand2 compatible_brand);
388 
399 LIBHEIF_API
400 void heif_context_set_unif(heif_context* ctx, int flag);
401 
402 // --- deprecated functions ---
403 
404 // DEPRECATED, typo in function name
405 LIBHEIF_API
407 
408 // DEPRECATED, typo in function name
409 LIBHEIF_API
411 
412 // DEPRECATED: use heif_get_encoder_descriptors() instead.
413 // Get a list of available encoders. You can filter the encoders by compression format and name.
414 // Use format_filter==heif_compression_undefined and name_filter==NULL as wildcards.
415 // The returned list of encoders is sorted by their priority (which is a plugin property).
416 // The number of encoders is returned, which are not more than 'count' if (out_encoders != nullptr).
417 // By setting out_encoders==nullptr, you can query the number of encoders, 'count' is ignored.
418 // Note: to get the actual encoder from the descriptors returned here, use heif_context_get_encoder().
419 LIBHEIF_API
420 int heif_context_get_encoder_descriptors(heif_context*, // TODO: why do we need this parameter?
421  heif_compression_format format_filter,
422  const char* name_filter,
423  const heif_encoder_descriptor** out_encoders,
424  int count);
425 
426 #ifdef __cplusplus
427 }
428 #endif
429 
430 #endif
void heif_context_add_compatible_brand(heif_context *ctx, heif_brand2 compatible_brand)
heif_error heif_encoder_parameter_get_valid_integer_range(const heif_encoder_parameter *, int *have_minimum_maximum, int *minimum, int *maximum)
heif_encoding_options * heif_encoding_options_alloc(void)
struct heif_encoding_options heif_encoding_options
heif_error heif_context_add_overlay_image(heif_context *ctx, uint32_t image_width, uint32_t image_height, uint16_t nImages, const heif_item_id *image_ids, int32_t *offsets, const uint16_t background_rgba[4], heif_image_handle **out_iovl_image_handle)
void heif_context_set_major_brand(heif_context *ctx, heif_brand2 major_brand)
struct heif_encoder_parameter heif_encoder_parameter
Definition: heif_encoding.h:56
heif_error heif_encoder_get_parameter(heif_encoder *, const char *parameter_name, char *value_ptr, int value_size)
const heif_encoder_parameter *const * heif_encoder_list_parameters(heif_encoder *)
int heif_have_encoder_for_format(heif_compression_format format)
heif_error heif_encoder_parameter_integer_valid_values(heif_encoder *, const char *parameter_name, int *have_minimum, int *have_maximum, int *minimum, int *maximum, int *num_valid_values, const int **out_integer_array)
heif_error heif_context_set_primary_image(heif_context *, heif_image_handle *image_handle)
void heif_encoding_options_free(heif_encoding_options *)
int heif_get_encoder_descriptors(heif_compression_format format_filter, const char *name_filter, const heif_encoder_descriptor **out_encoders, int count)
heif_error heif_encoder_get_parameter_string(heif_encoder *, const char *parameter_name, char *value, int value_size)
struct heif_unci_image_parameters heif_unci_image_parameters
Definition: heif_encoding.h:40
const char * heif_encoder_descriptor_get_id_name(const heif_encoder_descriptor *)
heif_error heif_encoder_get_parameter_integer(heif_encoder *, const char *parameter_name, int *value)
heif_error heif_context_encode_image(heif_context *, const heif_image *image, heif_encoder *encoder, const heif_encoding_options *options, heif_image_handle **out_image_handle)
const char * heif_encoder_descriptor_get_name(const heif_encoder_descriptor *)
heif_orientation
Definition: heif_encoding.h:265
@ heif_orientation_rotate_180
Definition: heif_encoding.h:268
@ heif_orientation_rotate_90_cw_then_flip_horizontally
Definition: heif_encoding.h:270
@ heif_orientation_rotate_90_cw_then_flip_vertically
Definition: heif_encoding.h:272
@ heif_orientation_flip_vertically
Definition: heif_encoding.h:269
@ heif_orientation_rotate_270_cw
Definition: heif_encoding.h:273
@ heif_orientation_flip_horizontally
Definition: heif_encoding.h:267
@ heif_orientation_normal
Definition: heif_encoding.h:266
@ heif_orientation_rotate_90_cw
Definition: heif_encoding.h:271
const char * heif_encoder_parameter_get_name(const heif_encoder_parameter *)
heif_error heif_encoder_set_lossless(heif_encoder *, int enable)
heif_error heif_encoder_set_logging_level(heif_encoder *, int level)
struct heif_encoder_descriptor heif_encoder_descriptor
Definition: heif_encoding.h:51
heif_error heif_encoder_parameter_get_valid_string_values(const heif_encoder_parameter *, const char *const **out_stringarray)
int heif_encoder_descriptor_supportes_lossy_compression(const heif_encoder_descriptor *)
heif_error heif_encoder_parameter_integer_valid_range(heif_encoder *, const char *parameter_name, int *have_minimum_maximum, int *minimum, int *maximum)
int heif_encoder_has_default(heif_encoder *, const char *parameter_name)
heif_error heif_encoder_set_parameter(heif_encoder *, const char *parameter_name, const char *value)
const char * heif_encoder_get_name(const heif_encoder *)
heif_error heif_encoder_set_parameter_string(heif_encoder *, const char *parameter_name, const char *value)
void heif_context_set_unif(heif_context *ctx, int flag)
Enable the unified ID namespace ('unif' brand).
int heif_context_get_encoder_descriptors(heif_context *, heif_compression_format format_filter, const char *name_filter, const heif_encoder_descriptor **out_encoders, int count)
void heif_encoding_options_copy(heif_encoding_options *dst, const heif_encoding_options *src)
void heif_encoder_release(heif_encoder *)
Release the encoder object after use.
int heif_encoder_descriptor_supportes_lossless_compression(const heif_encoder_descriptor *)
int heif_encoder_descriptor_supports_lossy_compression(const heif_encoder_descriptor *)
heif_error heif_encoder_set_lossy_quality(heif_encoder *, int quality)
int heif_encoder_descriptor_supports_lossless_compression(const heif_encoder_descriptor *)
heif_error heif_encoder_parameter_string_valid_values(heif_encoder *, const char *parameter_name, const char *const **out_stringarray)
heif_orientation heif_orientation_concat(heif_orientation first, heif_orientation second)
heif_compression_format heif_encoder_descriptor_get_compression_format(const heif_encoder_descriptor *)
heif_error heif_encoder_set_parameter_boolean(heif_encoder *, const char *parameter_name, int value)
struct heif_encoder heif_encoder
Opaque object that represents the encoder used to code the images.
Definition: heif_encoding.h:48
heif_error heif_context_get_encoder_for_format(heif_context *context, heif_compression_format format, heif_encoder **)
heif_error heif_encoder_set_parameter_integer(heif_encoder *, const char *parameter_name, int value)
heif_error heif_encoder_get_parameter_boolean(heif_encoder *, const char *parameter_name, int *value)
heif_error heif_encoder_parameter_get_valid_integer_values(const heif_encoder_parameter *, int *have_minimum, int *have_maximum, int *minimum, int *maximum, int *num_valid_values, const int **out_integer_array)
heif_error heif_context_get_encoder(heif_context *context, const heif_encoder_descriptor *, heif_encoder **out_encoder)
enum heif_encoder_parameter_type heif_encoder_parameter_get_type(const heif_encoder_parameter *)
heif_encoder_parameter_type
Definition: heif_encoding.h:155
@ heif_encoder_parameter_type_boolean
Definition: heif_encoding.h:157
@ heif_encoder_parameter_type_integer
Definition: heif_encoding.h:156
@ heif_encoder_parameter_type_string
Definition: heif_encoding.h:158
struct heif_image_handle heif_image_handle
Definition: heif_library.h:90
uint32_t heif_item_id
Definition: heif_library.h:92
struct heif_context heif_context
Definition: heif_library.h:89
Definition: heif_encoding.h:282
uint8_t prefer_uncC_short_form
Definition: heif_encoding.h:318
uint8_t save_alpha_channel
Definition: heif_encoding.h:287
heif_color_profile_nclx * output_nclx_profile
Definition: heif_encoding.h:302
uint8_t version
Definition: heif_encoding.h:283
uint8_t macOS_compatibility_workaround_no_nclx_profile
Definition: heif_encoding.h:304
uint8_t macOS_compatibility_workaround
Definition: heif_encoding.h:292
heif_orientation image_orientation
Definition: heif_encoding.h:309
uint8_t save_two_colr_boxes_when_ICC_and_nclx_available
Definition: heif_encoding.h:296
const heif_unci_image_parameters * unci_parameters
Definition: heif_encoding.h:326
heif_color_conversion_options color_conversion_options
Definition: heif_encoding.h:313