Skip to content

Commit

Permalink
Default to matrix_coefficients=0 in lossless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Nov 19, 2023
1 parent 9a053e1 commit d3cf7ae
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
15 changes: 5 additions & 10 deletions examples/heif_enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,17 @@ void show_help(const char* argv0)
<< " -e, --encoder ID select encoder to use (the IDs can be listed with --list-encoders)\n"
<< " --plugin-directory DIR load all codec plugins in the directory\n"
<< " -E, --even-size [deprecated] crop images to even width and height (odd sizes are not decoded correctly by some software)\n"
<< " --matrix_coefficients nclx profile: color conversion matrix coefficients, default=6 (see h.273)\n"
<< " --colour_primaries nclx profile: color primaries (see h.273)\n"
<< " --transfer_characteristic nclx profile: transfer characteristics (see h.273)\n"
<< " --colour_primaries nclx profile: color primaries, default: 1 (see h.273)\n"
<< " --transfer_characteristic nclx profile: transfer characteristics, default: 13 (see h.273)\n"
<< " --matrix_coefficients nclx profile: color conversion matrix coefficients, default: 6 for lossy or 0 for lossless (see h.273)\n"
<< " --full_range_flag nclx profile: full range flag, default: 1\n"
<< " --enable-two-colr-boxes will write both an ICC and an nclx color profile if both are present\n"
<< " --premultiplied-alpha input image has premultiplied alpha\n"
<< " --enable-metadata-compression enable XMP metadata compression (experimental)\n"
<< " -C,--chroma-downsampling ALGO force chroma downsampling algorithm (nn = nearest-neighbor / average / sharp-yuv)\n"
<< " (sharp-yuv makes edges look sharper when using YUV420 with bilinear chroma upsampling)\n"
<< " --benchmark measure encoding time, PSNR, and output file size\n"
<< " --pitm-description TEXT (EXPERIMENTAL) set user description for primary image\n"

<< "\n"
<< "Note: to get lossless encoding, you need this set of options:\n"
<< " -L switch encoder to lossless mode\n"
<< " -p chroma=444 switch off chroma subsampling\n"
<< " --matrix_coefficients=0 encode in RGB color-space\n";
<< " --pitm-description TEXT (EXPERIMENTAL) set user description for primary image\n";
}


Expand Down Expand Up @@ -509,6 +503,7 @@ int main(int argc, char** argv)
break;
case 'L':
lossless = true;
nclx_matrix_coefficients = 0;
break;
case 'o':
output_filename = optarg;
Expand Down
20 changes: 16 additions & 4 deletions libheif/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,9 @@ static bool nclx_profile_matches_spec(heif_colorspace colorspace,
}


static std::shared_ptr<color_profile_nclx> compute_target_nclx_profile(const std::shared_ptr<HeifPixelImage>& image, const heif_color_profile_nclx* output_nclx_profile)
static std::shared_ptr<color_profile_nclx> compute_target_nclx_profile(const std::shared_ptr<HeifPixelImage>& image,
const heif_color_profile_nclx* output_nclx_profile,
bool lossless = false)
{
auto target_nclx_profile = std::make_shared<color_profile_nclx>();

Expand All @@ -2446,7 +2448,7 @@ static std::shared_ptr<color_profile_nclx> compute_target_nclx_profile(const std
target_nclx_profile->set_undefined();
}

target_nclx_profile->replace_undefined_values_with_sRGB_defaults();
target_nclx_profile->replace_undefined_values_with_sRGB_defaults(lossless);

return target_nclx_profile;
}
Expand All @@ -2466,8 +2468,13 @@ Error HeifContext::encode_image_as_hevc(const std::shared_ptr<HeifPixelImage>& i

heif_colorspace colorspace = image->get_colorspace();
heif_chroma chroma = image->get_chroma_format();
int lossless = 0;

auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile);
if (encoder->plugin->supports_lossless_compression) {
encoder->plugin->get_parameter_lossless(encoder->encoder, &lossless);
}

auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile, static_cast<bool>(lossless));

if (encoder->plugin->plugin_api_version >= 2) {
encoder->plugin->query_input_colorspace2(encoder->encoder, &colorspace, &chroma);
Expand Down Expand Up @@ -2688,8 +2695,13 @@ Error HeifContext::encode_image_as_av1(const std::shared_ptr<HeifPixelImage>& im

heif_colorspace colorspace = image->get_colorspace();
heif_chroma chroma = image->get_chroma_format();
int lossless = 0;

auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile);
if (encoder->plugin->supports_lossless_compression) {
encoder->plugin->get_parameter_lossless(encoder->encoder, &lossless);
}

auto target_nclx_profile = compute_target_nclx_profile(image, options.output_nclx_profile, static_cast<bool>(lossless));

if (encoder->plugin->plugin_api_version >= 2) {
encoder->plugin->query_input_colorspace2(encoder->encoder, &colorspace, &chroma);
Expand Down
4 changes: 2 additions & 2 deletions libheif/nclx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ void color_profile_nclx::set_from_heif_color_profile_nclx(const struct heif_colo
}


void color_profile_nclx::replace_undefined_values_with_sRGB_defaults()
void color_profile_nclx::replace_undefined_values_with_sRGB_defaults(bool lossless)
{
if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) {
m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6;
m_matrix_coefficients = lossless ? heif_matrix_coefficients_RGB_GBR : heif_matrix_coefficients_ITU_R_BT_601_6;
}

if (m_colour_primaries == heif_color_primaries_unspecified) {
Expand Down
2 changes: 1 addition & 1 deletion libheif/nclx.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class color_profile_nclx : public color_profile

void set_from_heif_color_profile_nclx(const struct heif_color_profile_nclx* nclx);

void replace_undefined_values_with_sRGB_defaults();
void replace_undefined_values_with_sRGB_defaults(bool lossless=false);

private:
uint16_t m_colour_primaries = heif_color_primaries_unspecified;
Expand Down
2 changes: 1 addition & 1 deletion libheif/plugins/encoder_rav1e.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ static const struct heif_encoder_plugin encoder_plugin_rav1e
/* id_name */ "rav1e",
/* priority */ RAV1E_PLUGIN_PRIORITY,
/* supports_lossy_compression */ true,
/* supports_lossless_compression */ false,
/* supports_lossless_compression */ false, // https://github.com/xiph/rav1e/issues/151
/* get_plugin_name */ rav1e_plugin_name,
/* init_plugin */ rav1e_init_plugin,
/* cleanup_plugin */ rav1e_cleanup_plugin,
Expand Down
2 changes: 1 addition & 1 deletion libheif/plugins/encoder_svt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ static const struct heif_encoder_plugin encoder_plugin_svt
/* id_name */ "svt",
/* priority */ SVT_PLUGIN_PRIORITY,
/* supports_lossy_compression */ true,
/* supports_lossless_compression */ false,
/* supports_lossless_compression */ false, // https://gitlab.com/AOMediaCodec/SVT-AV1/-/issues/1636
/* get_plugin_name */ svt_plugin_name,
/* init_plugin */ svt_init_plugin,
/* cleanup_plugin */ svt_cleanup_plugin,
Expand Down

0 comments on commit d3cf7ae

Please sign in to comment.