in libheif/plugins/encoder_uvg266.cc [371:681]
static struct heif_error uvg266_encode_image(void* encoder_raw, const struct heif_image* image,
heif_image_input_class input_class)
{
struct encoder_struct_uvg266* encoder = (struct encoder_struct_uvg266*) encoder_raw;
int bit_depth = heif_image_get_bits_per_pixel_range(image, heif_channel_Y);
bool isGreyscale = (heif_image_get_colorspace(image) == heif_colorspace_monochrome);
heif_chroma chroma = heif_image_get_chroma_format(image);
const uvg_api* api = uvg_api_get(bit_depth);
if (api == nullptr) {
struct heif_error err = {
heif_error_Encoder_plugin_error,
heif_suberror_Unsupported_bit_depth,
kError_unsupported_bit_depth
};
return err;
}
uvg_config* config = api->config_alloc();
api->config_init(config); // param, encoder->preset.c_str(), encoder->tune.c_str());
#if HAVE_UVG266_ENABLE_LOGGING
config->enable_logging_output = 0;
#endif
#if !ENABLE_MULTITHREADING_SUPPORT
// 0: Process everything with main thread
// -1 (default): Select automatically.
config->threads = 0;
#endif
#if 1
#if 0
while (ctuSize > 16 &&
(heif_image_get_width(image, heif_channel_Y) < ctuSize ||
heif_image_get_height(image, heif_channel_Y) < ctuSize)) {
ctuSize /= 2;
}
if (ctuSize < 16) {
api->config_destroy(config);
struct heif_error err = {
heif_error_Encoder_plugin_error,
heif_suberror_Invalid_parameter_value,
kError_unsupported_image_size
};
return err;
}
#endif
#else
// TODO: There seems to be a bug in uvg266 where increasing the CTU size between
// multiple encoding jobs causes a segmentation fault. E.g. encoding multiple
// times with a CTU of 16 works, the next encoding with a CTU of 32 crashes.
// Use hardcoded value of 64 and reject images that are too small.
if (heif_image_get_width(image, heif_channel_Y) < ctuSize ||
heif_image_get_height(image, heif_channel_Y) < ctuSize) {
api->param_free(param);
struct heif_error err = {
heif_error_Encoder_plugin_error,
heif_suberror_Invalid_parameter_value,
kError_unsupported_image_size
};
return err;
}
#endif
#if 0
// ctuSize should be a power of 2 in [16;64]
switch (ctuSize) {
case 64:
ctu = "64";
break;
case 32:
ctu = "32";
break;
case 16:
ctu = "16";
break;
default:
struct heif_error err = {
heif_error_Encoder_plugin_error,
heif_suberror_Invalid_parameter_value,
kError_unsupported_image_size
};
return err;
}
(void) ctu;
#endif
int input_width = heif_image_get_width(image, heif_channel_Y);
int input_height = heif_image_get_height(image, heif_channel_Y);
int input_chroma_width = 0;
int input_chroma_height = 0;
uint32_t encoded_width, encoded_height;
uvg266_query_encoded_size(encoder_raw, input_width, input_height, &encoded_width, &encoded_height);
uvg_chroma_format kvzChroma;
int chroma_stride_shift = 0;
int chroma_height_shift = 0;
if (isGreyscale) {
config->input_format = UVG_FORMAT_P400;
kvzChroma = UVG_CSP_400;
}
else if (chroma == heif_chroma_420) {
config->input_format = UVG_FORMAT_P420;
kvzChroma = UVG_CSP_420;
chroma_stride_shift = 1;
chroma_height_shift = 1;
input_chroma_width = (input_width + 1) / 2;
input_chroma_height = (input_height + 1) / 2;
}
else if (chroma == heif_chroma_422) {
config->input_format = UVG_FORMAT_P422;
kvzChroma = UVG_CSP_422;
chroma_stride_shift = 1;
chroma_height_shift = 0;
input_chroma_width = (input_width + 1) / 2;
input_chroma_height = input_height;
}
else if (chroma == heif_chroma_444) {
config->input_format = UVG_FORMAT_P444;
kvzChroma = UVG_CSP_444;
chroma_stride_shift = 0;
chroma_height_shift = 0;
input_chroma_width = input_width;
input_chroma_height = input_height;
}
else {
return heif_error{
heif_error_Encoder_plugin_error,
heif_suberror_Unsupported_image_type,
kError_unsupported_chroma
};
}
if (chroma != heif_chroma_monochrome) {
int w = heif_image_get_width(image, heif_channel_Y);
int h = heif_image_get_height(image, heif_channel_Y);
if (chroma != heif_chroma_444) { w = (w + 1) / 2; }
if (chroma == heif_chroma_420) { h = (h + 1) / 2; }
assert(heif_image_get_width(image, heif_channel_Cb) == w);
assert(heif_image_get_width(image, heif_channel_Cr) == w);
assert(heif_image_get_height(image, heif_channel_Cb) == h);
assert(heif_image_get_height(image, heif_channel_Cr) == h);
(void) w;
(void) h;
}
struct heif_color_profile_nclx* nclx = nullptr;
heif_error err = heif_image_get_nclx_color_profile(image, &nclx);
if (err.code != heif_error_Ok) {
nclx = nullptr;
}
// make sure NCLX profile is deleted at end of function
auto nclx_deleter = std::unique_ptr<heif_color_profile_nclx, void (*)(heif_color_profile_nclx*)>(nclx, heif_nclx_color_profile_free);
if (nclx) {
config->vui.fullrange = nclx->full_range_flag;
}
else {
config->vui.fullrange = 1;
}
if (nclx &&
(input_class == heif_image_input_class_normal ||
input_class == heif_image_input_class_thumbnail)) {
config->vui.colorprim = nclx->color_primaries;
config->vui.transfer = nclx->transfer_characteristics;
config->vui.colormatrix = nclx->matrix_coefficients;
}
config->qp = ((100 - encoder->quality) * 51 + 50) / 100;
config->lossless = encoder->lossless ? 1 : 0;
config->width = encoded_width;
config->height = encoded_height;
// Note: it is ok to cast away the const, as the image content is not changed.
// However, we have to guarantee that there are no plane pointers or stride values kept over calling the svt_encode_image() function.
/*
err = heif_image_extend_padding_to_size(const_cast<struct heif_image*>(image),
param->sourceWidth,
param->sourceHeight);
if (err.code) {
return err;
}
*/
uvg_picture* pic = api->picture_alloc_csp(kvzChroma, encoded_width, encoded_height);
if (!pic) {
api->config_destroy(config);
return heif_error{
heif_error_Encoder_plugin_error,
heif_suberror_Encoder_encoding,
kError_unspecified_error
};
}
if (isGreyscale) {
int stride;
const uint8_t* data = heif_image_get_plane_readonly(image, heif_channel_Y, &stride);
copy_plane(pic->y, pic->stride, data, stride, input_width, input_height, encoded_width, encoded_height);
}
else {
int stride;
const uint8_t* data;
data = heif_image_get_plane_readonly(image, heif_channel_Y, &stride);
copy_plane(pic->y, pic->stride, data, stride, input_width, input_height, encoded_width, encoded_height);
data = heif_image_get_plane_readonly(image, heif_channel_Cb, &stride);
copy_plane(pic->u, pic->stride >> chroma_stride_shift, data, stride, input_chroma_width, input_chroma_height,
encoded_width >> chroma_stride_shift, encoded_height >> chroma_height_shift);
data = heif_image_get_plane_readonly(image, heif_channel_Cr, &stride);
copy_plane(pic->v, pic->stride >> chroma_stride_shift, data, stride, input_chroma_width, input_chroma_height,
encoded_width >> chroma_stride_shift, encoded_height >> chroma_height_shift);
}
uvg_encoder* kvzencoder = api->encoder_open(config);
if (!kvzencoder) {
api->picture_free(pic);
api->config_destroy(config);
return heif_error{
heif_error_Encoder_plugin_error,
heif_suberror_Encoder_encoding,
kError_unspecified_error
};
}
uvg_data_chunk* data = nullptr;
uint32_t data_len;
int success;
success = api->encoder_headers(kvzencoder, &data, &data_len);
if (!success) {
api->picture_free(pic);
api->config_destroy(config);
api->encoder_close(kvzencoder);
return heif_error{
heif_error_Encoder_plugin_error,
heif_suberror_Encoder_encoding,
kError_unspecified_error
};
}
// If we write this, the data is twice in the output
//append_chunk_data(data, encoder->output_data);
success = api->encoder_encode(kvzencoder,
pic,
&data, &data_len,
nullptr, nullptr, nullptr);
if (!success) {
api->chunk_free(data);
api->picture_free(pic);
api->config_destroy(config);
api->encoder_close(kvzencoder);
return heif_error{
heif_error_Encoder_plugin_error,
heif_suberror_Encoder_encoding,
kError_unspecified_error
};
}
append_chunk_data(data, encoder->output_data);
for (;;) {
success = api->encoder_encode(kvzencoder,
nullptr,
&data, &data_len,
nullptr, nullptr, nullptr);
if (!success) {
api->chunk_free(data);
api->picture_free(pic);
api->config_destroy(config);
api->encoder_close(kvzencoder);
return heif_error{
heif_error_Encoder_plugin_error,
heif_suberror_Encoder_encoding,
kError_unspecified_error
};
}
if (data == nullptr || data->len == 0) {
break;
}
append_chunk_data(data, encoder->output_data);
}
(void) success;
api->chunk_free(data);
api->encoder_close(kvzencoder);
api->picture_free(pic);
api->config_destroy(config);
return heif_error_ok;
}