heif_plugin.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * HEIF codec.
  3. * Copyright (c) 2017 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. #ifndef LIBHEIF_HEIF_PLUGIN_H
  21. #define LIBHEIF_HEIF_PLUGIN_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #include <libheif/heif.h>
  26. // ====================================================================================================
  27. // This file is for codec plugin developers only.
  28. // ====================================================================================================
  29. // API versions table
  30. //
  31. // release decoder encoder enc.params
  32. // -----------------------------------------
  33. // 1.0 1 N/A N/A
  34. // 1.1 1 1 1
  35. // 1.4 1 1 2
  36. // 1.8 1 2 2
  37. // 1.13 2 3 2
  38. // 1.15 3 3 2
  39. // ====================================================================================================
  40. // Decoder plugin API
  41. // In order to decode images in other formats than HEVC, additional compression codecs can be
  42. // added as plugins. A plugin has to implement the functions specified in heif_decoder_plugin
  43. // and the plugin has to be registered to the libheif library using heif_register_decoder().
  44. struct heif_decoder_plugin
  45. {
  46. // API version supported by this plugin (see table above for supported versions)
  47. int plugin_api_version;
  48. // --- version 1 functions ---
  49. // Human-readable name of the plugin
  50. const char* (* get_plugin_name)();
  51. // Global plugin initialization (may be NULL)
  52. void (* init_plugin)();
  53. // Global plugin deinitialization (may be NULL)
  54. void (* deinit_plugin)();
  55. // Query whether the plugin supports decoding of the given format
  56. // Result is a priority value. The plugin with the largest value wins.
  57. // Default priority is 100. Returning 0 indicates that the plugin cannot decode this format.
  58. int (* does_support_format)(enum heif_compression_format format);
  59. // Create a new decoder context for decoding an image
  60. struct heif_error (* new_decoder)(void** decoder);
  61. // Free the decoder context (heif_image can still be used after destruction)
  62. void (* free_decoder)(void* decoder);
  63. // Push more data into the decoder. This can be called multiple times.
  64. // This may not be called after any decode_*() function has been called.
  65. struct heif_error (* push_data)(void* decoder, const void* data, size_t size);
  66. // --- After pushing the data into the decoder, the decode functions may be called only once.
  67. struct heif_error (* decode_image)(void* decoder, struct heif_image** out_img);
  68. // --- version 2 functions will follow below ... ---
  69. void (*set_strict_decoding)(void* decoder, int flag);
  70. // If not NULL, this can provide a specialized function to convert YCbCr to sRGB, because
  71. // only the codec itself knows how to interpret the chroma samples and their locations.
  72. /*
  73. struct heif_error (*convert_YCbCr_to_sRGB)(void* decoder,
  74. struct heif_image* in_YCbCr_img,
  75. struct heif_image** out_sRGB_img);
  76. */
  77. // Reset decoder, such that we can feed in new data for another image.
  78. // void (*reset_image)(void* decoder);
  79. // --- version 3 functions will follow below ... ---
  80. const char* id_name;
  81. // --- version 4 functions will follow below ... ---
  82. };
  83. enum heif_encoded_data_type
  84. {
  85. heif_encoded_data_type_HEVC_header = 1,
  86. heif_encoded_data_type_HEVC_image = 2,
  87. heif_encoded_data_type_HEVC_depth_SEI = 3
  88. };
  89. // Specifies the class of the input image content.
  90. // The encoder may want to encode different classes with different parameters
  91. // (e.g. always encode alpha lossless)
  92. enum heif_image_input_class
  93. {
  94. heif_image_input_class_normal = 1,
  95. heif_image_input_class_alpha = 2,
  96. heif_image_input_class_depth = 3,
  97. heif_image_input_class_thumbnail = 4
  98. };
  99. struct heif_encoder_plugin
  100. {
  101. // API version supported by this plugin (see table above for supported versions)
  102. int plugin_api_version;
  103. // --- version 1 functions ---
  104. // The compression format generated by this plugin.
  105. enum heif_compression_format compression_format;
  106. // Short name of the encoder that can be used as command line parameter when selecting an encoder.
  107. // Hence, it should stay stable and not contain any version numbers that will change.
  108. const char* id_name;
  109. // Default priority is 100.
  110. int priority;
  111. // Feature support
  112. int supports_lossy_compression;
  113. int supports_lossless_compression;
  114. // Human-readable name of the plugin
  115. const char* (* get_plugin_name)();
  116. // Global plugin initialization (may be NULL)
  117. void (* init_plugin)();
  118. // Global plugin cleanup (may be NULL).
  119. // Free data that was allocated in init_plugin()
  120. void (* cleanup_plugin)();
  121. // Create a new decoder context for decoding an image
  122. struct heif_error (* new_encoder)(void** encoder);
  123. // Free the decoder context (heif_image can still be used after destruction)
  124. void (* free_encoder)(void* encoder);
  125. struct heif_error (* set_parameter_quality)(void* encoder, int quality);
  126. struct heif_error (* get_parameter_quality)(void* encoder, int* quality);
  127. struct heif_error (* set_parameter_lossless)(void* encoder, int lossless);
  128. struct heif_error (* get_parameter_lossless)(void* encoder, int* lossless);
  129. struct heif_error (* set_parameter_logging_level)(void* encoder, int logging);
  130. struct heif_error (* get_parameter_logging_level)(void* encoder, int* logging);
  131. const struct heif_encoder_parameter** (* list_parameters)(void* encoder);
  132. struct heif_error (* set_parameter_integer)(void* encoder, const char* name, int value);
  133. struct heif_error (* get_parameter_integer)(void* encoder, const char* name, int* value);
  134. struct heif_error (* set_parameter_boolean)(void* encoder, const char* name, int value);
  135. struct heif_error (* get_parameter_boolean)(void* encoder, const char* name, int* value);
  136. struct heif_error (* set_parameter_string)(void* encoder, const char* name, const char* value);
  137. struct heif_error (* get_parameter_string)(void* encoder, const char* name, char* value, int value_size);
  138. // Replace the input colorspace/chroma with the one that is supported by the encoder and that
  139. // comes as close to the input colorspace/chroma as possible.
  140. void (* query_input_colorspace)(enum heif_colorspace* inout_colorspace,
  141. enum heif_chroma* inout_chroma);
  142. // Encode an image.
  143. // After pushing an image into the encoder, you should call get_compressed_data() to
  144. // get compressed data until it returns a NULL data pointer.
  145. struct heif_error (* encode_image)(void* encoder, const struct heif_image* image,
  146. enum heif_image_input_class image_class);
  147. // Get a packet of decoded data. The data format depends on the codec.
  148. // For HEVC, each packet shall contain exactly one NAL, starting with the NAL header without startcode.
  149. struct heif_error (* get_compressed_data)(void* encoder, uint8_t** data, int* size,
  150. enum heif_encoded_data_type* type);
  151. // --- version 2 ---
  152. void (* query_input_colorspace2)(void* encoder,
  153. enum heif_colorspace* inout_colorspace,
  154. enum heif_chroma* inout_chroma);
  155. // --- version 3 ---
  156. // The encoded image size may be different from the input frame size, e.g. because
  157. // of required rounding, or a required minimum size. Use this function to return
  158. // the encoded size for a given input image size.
  159. // You may set this to NULL if no padding is required for any image size.
  160. void (* query_encoded_size)(void* encoder, uint32_t input_width, uint32_t input_height,
  161. uint32_t* encoded_width, uint32_t* encoded_height);
  162. // --- version 4 functions will follow below ... ---
  163. };
  164. // Names for standard parameters. These should only be used by the encoder plugins.
  165. #define heif_encoder_parameter_name_quality "quality"
  166. #define heif_encoder_parameter_name_lossless "lossless"
  167. // For use only by the encoder plugins.
  168. // Application programs should use the access functions.
  169. // NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding)
  170. struct heif_encoder_parameter
  171. {
  172. int version; // current version: 2
  173. // --- version 1 fields ---
  174. const char* name;
  175. enum heif_encoder_parameter_type type;
  176. union
  177. {
  178. struct
  179. {
  180. int default_value;
  181. uint8_t have_minimum_maximum; // bool
  182. int minimum;
  183. int maximum;
  184. int* valid_values;
  185. int num_valid_values;
  186. } integer;
  187. struct
  188. {
  189. const char* default_value;
  190. const char* const* valid_values;
  191. } string; // NOLINT
  192. struct
  193. {
  194. int default_value;
  195. } boolean;
  196. };
  197. // --- version 2 fields
  198. int has_default;
  199. };
  200. extern struct heif_error heif_error_ok;
  201. extern struct heif_error heif_error_unsupported_parameter;
  202. extern struct heif_error heif_error_invalid_parameter_value;
  203. #define HEIF_WARN_OR_FAIL(strict, image, cmd, cleanupBlock) \
  204. { struct heif_error e = cmd; \
  205. if (e.code != heif_error_Ok) { \
  206. if (strict) { \
  207. cleanupBlock \
  208. return e; \
  209. } \
  210. else { \
  211. heif_image_add_decoding_warning(image, e); \
  212. } \
  213. } \
  214. }
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. #endif