Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update library to use definitions of ultrahdr_api everywhere #176

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions benchmark/benchmark_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,21 @@ static void BM_Encode_Api4(benchmark::State& s) {
gainmapImg.data = gainmapImgInfo.imgData.data();
gainmapImg.maxLength = gainmapImg.length = gainmapImgInfo.imgData.size();
gainmapImg.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
ultrahdr_metadata_struct uhdr_metadata;
if (!getMetadataFromXMP(gainmapImgInfo.xmpData.data(), gainmapImgInfo.xmpData.size(),
&uhdr_metadata)) {
uhdr_gainmap_metadata_ext_t meta;
if (getMetadataFromXMP(gainmapImgInfo.xmpData.data(), gainmapImgInfo.xmpData.size(), &meta)
.error_code != UHDR_CODEC_OK) {
s.SkipWithError("getMetadataFromXMP returned with error");
return;
}
ultrahdr_metadata_struct uhdr_metadata;
uhdr_metadata.version = meta.version;
uhdr_metadata.hdrCapacityMax = meta.hdr_capacity_max;
uhdr_metadata.hdrCapacityMin = meta.hdr_capacity_min;
uhdr_metadata.gamma = meta.gamma;
uhdr_metadata.offsetSdr = meta.offset_sdr;
uhdr_metadata.offsetHdr = meta.offset_hdr;
uhdr_metadata.maxContentBoost = meta.max_content_boost;
uhdr_metadata.minContentBoost = meta.min_content_boost;
for (auto _ : s) {
status = jpegHdr.encodeJPEGR(&primaryImg, &gainmapImg, &uhdr_metadata, &jpegImgR);
if (JPEGR_NO_ERROR != status) {
Expand Down
75 changes: 38 additions & 37 deletions examples/ultrahdr_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,9 @@ class UltraHdrAppInput {
uhdr_color_gamut_t sdrCg = UHDR_CG_BT_709,
uhdr_color_transfer_t hdrTf = UHDR_CT_HLG, int quality = 95,
uhdr_color_transfer_t oTf = UHDR_CT_HLG,
uhdr_img_fmt_t oFmt = UHDR_IMG_FMT_32bppRGBA1010102,
bool use_full_range_color_hdr = false,
int gainmap_scale_factor = 4,
bool use_multi_channel_gainmap = false,
int gainmap_compression_quality = 85)
uhdr_img_fmt_t oFmt = UHDR_IMG_FMT_32bppRGBA1010102, bool isHdrCrFull = false,
int gainmapScaleFactor = 4, int gainmapQuality = 85,
bool enableMultiChannelGainMap = false)
: mHdrIntentRawFile(hdrIntentRawFile),
mSdrIntentRawFile(sdrIntentRawFile),
mSdrIntentCompressedFile(sdrIntentCompressedFile),
Expand All @@ -275,10 +273,10 @@ class UltraHdrAppInput {
mQuality(quality),
mOTf(oTf),
mOfmt(oFmt),
mFullRange(use_full_range_color_hdr),
mMapDimensionScaleFactor(gainmap_scale_factor),
mMapCompressQuality(gainmap_compression_quality),
mUseMultiChannelGainMap(use_multi_channel_gainmap),
mFullRange(isHdrCrFull),
mMapDimensionScaleFactor(gainmapScaleFactor),
mMapCompressQuality(gainmapQuality),
mUseMultiChannelGainMap(enableMultiChannelGainMap),
mMode(0){};

UltraHdrAppInput(const char* uhdrFile, const char* outputFile,
Expand All @@ -298,6 +296,10 @@ class UltraHdrAppInput {
mQuality(95),
mOTf(oTf),
mOfmt(oFmt),
mFullRange(UHDR_CR_UNSPECIFIED),
mMapDimensionScaleFactor(4),
mMapCompressQuality(85),
mUseMultiChannelGainMap(false),
mMode(1){};

~UltraHdrAppInput() {
Expand Down Expand Up @@ -367,11 +369,11 @@ class UltraHdrAppInput {
const int mQuality;
const uhdr_color_transfer_t mOTf;
const uhdr_img_fmt_t mOfmt;
const bool mFullRange;
const size_t mMapDimensionScaleFactor;
const int mMapCompressQuality;
const bool mUseMultiChannelGainMap;
const int mMode;
bool mFullRange;
size_t mMapDimensionScaleFactor;
int mMapCompressQuality;
bool mUseMultiChannelGainMap;

uhdr_raw_image_t mRawP010Image{};
uhdr_raw_image_t mRawRgba1010102Image{};
Expand Down Expand Up @@ -631,16 +633,13 @@ bool UltraHdrAppInput::encode() {
RET_IF_ERR(uhdr_enc_set_using_multi_channel_gainmap(handle, mUseMultiChannelGainMap))
RET_IF_ERR(uhdr_enc_set_gainmap_scale_factor(handle, mMapDimensionScaleFactor))
#ifdef PROFILE_ENABLE
const int profileCount = 10;
Profiler profileEncode;
profileEncode.timerStart();
for (auto i = 0; i < profileCount; i++) {
#endif
RET_IF_ERR(uhdr_encode(handle))
RET_IF_ERR(uhdr_encode(handle))
#ifdef PROFILE_ENABLE
}
profileEncode.timerStop();
auto avgEncTime = profileEncode.elapsedTime() / (profileCount * 1000.f);
auto avgEncTime = profileEncode.elapsedTime() / 1000.f;
printf("Average encode time for res %d x %d is %f ms \n", mWidth, mHeight, avgEncTime);
#endif

Expand All @@ -655,10 +654,9 @@ bool UltraHdrAppInput::encode() {
mUhdrImage.cg = output->cg;
mUhdrImage.ct = output->ct;
mUhdrImage.range = output->range;
writeFile(mOutputFile, output->data, output->data_sz);
uhdr_release_encoder(handle);

return true;
return writeFile(mOutputFile, mUhdrImage.data, mUhdrImage.data_sz);
}

bool UltraHdrAppInput::decode() {
Expand All @@ -685,17 +683,15 @@ bool UltraHdrAppInput::decode() {
RET_IF_ERR(uhdr_dec_set_out_img_format(handle, mOfmt))

#ifdef PROFILE_ENABLE
const int profileCount = 10;
Profiler profileDecode;
profileDecode.timerStart();
for (auto i = 0; i < profileCount; i++) {
#endif
RET_IF_ERR(uhdr_decode(handle))
RET_IF_ERR(uhdr_decode(handle))
#ifdef PROFILE_ENABLE
}
profileDecode.timerStop();
auto avgDecTime = profileDecode.elapsedTime() / (profileCount * 1000.f);
printf("Average decode time for res %ld x %ld is %f ms \n", info.width, info.height, avgDecTime);
auto avgDecTime = profileDecode.elapsedTime() / 1000.f;
printf("Average decode time for res %ld x %ld is %f ms \n", uhdr_dec_get_image_width(handle),
uhdr_dec_get_image_height(handle), avgDecTime);
#endif

#undef RET_IF_ERR
Expand All @@ -719,10 +715,9 @@ bool UltraHdrAppInput::decode() {
for (unsigned i = 0; i < output->h; i++, inData += inStride, outData += outStride) {
memcpy(outData, inData, length);
}
writeFile(mOutputFile, output);
uhdr_release_decoder(handle);

return true;
return mMode == 1 ? writeFile(mOutputFile, &mDecodedUhdrRgbImage) : true;
}

#define CLIP3(x, min, max) ((x) < (min)) ? (min) : ((x) > (max)) ? (max) : (x)
Expand Down Expand Up @@ -1240,10 +1235,18 @@ static void usage(const char* name) {
" -t 10 bit input transfer function, optional. [0:linear, 1:hlg (default), 2:pq] \n");
fprintf(stderr,
" -q quality factor to be used while encoding 8 bit image, optional. [0-100], 95 : "
"default.\n"
" gain map image does not use this quality factor. \n"
" for now gain map image quality factor is not configurable. \n");
"default.\n");
fprintf(stderr, " -e compute psnr, optional. [0:no (default), 1:yes] \n");
fprintf(stderr,
" -R color range for hdr intent, optional. [0:narrow-range (default), "
"1:full-range]. \n");
fprintf(stderr, " -s gain map scale factor, optional. [factor > 0 (4 : default)]. \n");
fprintf(stderr,
" -Q quality factor to be used while encoding gain map image, optional. [0-100], "
"85 : default. \n");
fprintf(
stderr,
" -M enable / disable multi channel gain map, optional. [0:no (default), 1:yes]. \n");
fprintf(stderr, "\n## decoder options : \n");
fprintf(stderr, " -j ultra hdr compressed input resource. \n");
fprintf(
Expand All @@ -1258,8 +1261,6 @@ static void usage(const char* name) {
" srgb output color transfer shall be paired with rgba8888 only. \n"
" hlg, pq shall be paired with rgba1010102. \n"
" linear shall be paired with rgbahalffloat. \n");
fprintf(stderr,
" -R color range for the HDR YUV input. [0:narrow-range (default), 1:full-range]. \n");
fprintf(stderr, "\n## common options : \n");
fprintf(stderr,
" -z output filename, optional. \n"
Expand Down Expand Up @@ -1393,9 +1394,9 @@ int main(int argc, char* argv[]) {
use_full_range_color_hdr = atoi(optarg_s) == 1 ? true : false;
break;
// TODO
// case 'r':
// use_full_range_color_sdr = atoi(optarg_s) == 1 ? true : false;
// break;
/*case 'r':
use_full_range_color_sdr = atoi(optarg_s) == 1 ? true : false;
break;*/
case 's':
gainmap_scale_factor = atoi(optarg_s);
break;
Expand Down Expand Up @@ -1441,7 +1442,7 @@ int main(int argc, char* argv[]) {
output_file ? output_file : "out.jpeg", width, height, hdr_cf, sdr_cf,
hdr_cg, sdr_cg, hdr_tf, quality, out_tf, out_cf,
use_full_range_color_hdr, gainmap_scale_factor,
use_multi_channel_gainmap, gainmap_compression_quality);
gainmap_compression_quality, use_multi_channel_gainmap);
if (!appInput.encode()) return -1;
if (compute_psnr == 1) {
if (!appInput.decode()) return -1;
Expand Down
12 changes: 8 additions & 4 deletions fuzzer/ultrahdr_enc_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,10 @@ void UltraHdrEncFuzzer::process() {
yuv420ImgCopy.chroma_stride * yuv420ImgCopy.height / 2};
const size_t strides[3]{yuv420ImgCopy.luma_stride, yuv420ImgCopy.chroma_stride,
yuv420ImgCopy.chroma_stride};
if (encoder.compressImage(planes, strides, yuv420ImgCopy.width, yuv420ImgCopy.height,
UHDR_IMG_FMT_12bppYCbCr420, quality, nullptr, 0)) {
if (encoder
.compressImage(planes, strides, yuv420ImgCopy.width, yuv420ImgCopy.height,
UHDR_IMG_FMT_12bppYCbCr420, quality, nullptr, 0)
.error_code == UHDR_CODEC_OK) {
jpegImg.length = encoder.getCompressedImageSize();
jpegImg.maxLength = jpegImg.length;
jpegImg.data = encoder.getCompressedImagePtr();
Expand All @@ -266,8 +268,10 @@ void UltraHdrEncFuzzer::process() {
JpegEncoderHelper gainMapEncoder;
const uint8_t* planeGm[1]{reinterpret_cast<uint8_t*>(grayImg.data)};
const size_t strideGm[1]{grayImg.width};
if (gainMapEncoder.compressImage(planeGm, strideGm, grayImg.width, grayImg.height,
UHDR_IMG_FMT_8bppYCbCr400, quality, nullptr, 0)) {
if (gainMapEncoder
.compressImage(planeGm, strideGm, grayImg.width, grayImg.height,
UHDR_IMG_FMT_8bppYCbCr400, quality, nullptr, 0)
.error_code == UHDR_CODEC_OK) {
jpegGainMap.length = gainMapEncoder.getCompressedImageSize();
jpegGainMap.maxLength = jpegImg.length;
jpegGainMap.data = gainMapEncoder.getCompressedImagePtr();
Expand Down
60 changes: 31 additions & 29 deletions lib/include/ultrahdr/gainmapmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "ultrahdr_api.h"
#include "ultrahdr/ultrahdrcommon.h"
#include "ultrahdr/ultrahdr.h"
#include "ultrahdr/jpegr.h"

#if (defined(UHDR_ENABLE_INTRINSICS) && (defined(__ARM_NEON__) || defined(__ARM_NEON)))
Expand Down Expand Up @@ -176,23 +175,23 @@ inline uint16_t floatToHalf(float f) {
constexpr int32_t kGainFactorPrecision = 10;
constexpr int32_t kGainFactorNumEntries = 1 << kGainFactorPrecision;
struct GainLUT {
GainLUT(ultrahdr_metadata_ptr metadata) {
GainLUT(uhdr_gainmap_metadata_ext_t* metadata) {
this->mGammaInv = 1.0f / metadata->gamma;
for (int32_t idx = 0; idx < kGainFactorNumEntries; idx++) {
float value = static_cast<float>(idx) / static_cast<float>(kGainFactorNumEntries - 1);
float logBoost = log2(metadata->minContentBoost) * (1.0f - value) +
log2(metadata->maxContentBoost) * value;
float logBoost = log2(metadata->min_content_boost) * (1.0f - value) +
log2(metadata->max_content_boost) * value;
mGainTable[idx] = exp2(logBoost);
}
}

GainLUT(ultrahdr_metadata_ptr metadata, float displayBoost) {
GainLUT(uhdr_gainmap_metadata_ext_t* metadata, float displayBoost) {
this->mGammaInv = 1.0f / metadata->gamma;
float boostFactor = displayBoost > 0 ? displayBoost / metadata->maxContentBoost : 1.0f;
float boostFactor = displayBoost > 0 ? displayBoost / metadata->max_content_boost : 1.0f;
for (int32_t idx = 0; idx < kGainFactorNumEntries; idx++) {
float value = static_cast<float>(idx) / static_cast<float>(kGainFactorNumEntries - 1);
float logBoost = log2(metadata->minContentBoost) * (1.0f - value) +
log2(metadata->maxContentBoost) * value;
float logBoost = log2(metadata->min_content_boost) * (1.0f - value) +
log2(metadata->max_content_boost) * value;
mGainTable[idx] = exp2(logBoost * boostFactor);
}
}
Expand Down Expand Up @@ -432,7 +431,7 @@ inline Color identityConversion(Color e) { return e; }
/*
* Get the conversion to apply to the HDR image for gain map generation
*/
ColorTransformFn getHdrConversionFn(ultrahdr_color_gamut sdr_gamut, ultrahdr_color_gamut hdr_gamut);
ColorTransformFn getHdrConversionFn(uhdr_color_gamut_t sdr_gamut, uhdr_color_gamut_t hdr_gamut);

/*
* Convert between YUV encodings, according to ITU-R BT.709-6, ITU-R BT.601-7, and ITU-R BT.2100-2.
Expand Down Expand Up @@ -464,10 +463,10 @@ extern const int16_t kYuv2100To601_coeffs_neon[8];
*/
int16x8x3_t yuvConversion_neon(uint8x8_t y, int16x8_t u, int16x8_t v, int16x8_t coeffs);

void transformYuv420_neon(jr_uncompressed_ptr image, const int16_t* coeffs_ptr);
void transformYuv420_neon(uhdr_raw_image_t* image, const int16_t* coeffs_ptr);

status_t convertYuv_neon(jr_uncompressed_ptr image, ultrahdr_color_gamut src_encoding,
ultrahdr_color_gamut dst_encoding);
uhdr_error_info_t convertYuv_neon(uhdr_raw_image_t* image, uhdr_color_gamut_t src_encoding,
uhdr_color_gamut_t dst_encoding);
#endif

/*
Expand All @@ -479,7 +478,7 @@ status_t convertYuv_neon(jr_uncompressed_ptr image, ultrahdr_color_gamut src_enc
* The chroma channels should be less than or equal to half the image's width and height
* respectively, since input is 4:2:0 subsampled.
*/
void transformYuv420(jr_uncompressed_ptr image, const std::array<float, 9>& coeffs);
void transformYuv420(uhdr_raw_image_t* image, const std::array<float, 9>& coeffs);

////////////////////////////////////////////////////////////////////////////////
// Gain map calculations
Expand All @@ -492,8 +491,8 @@ void transformYuv420(jr_uncompressed_ptr image, const std::array<float, 9>& coef
* offsetHdr of 0.0, this function doesn't handle different metadata values for
* these fields.
*/
uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata);
uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata,
uint8_t encodeGain(float y_sdr, float y_hdr, uhdr_gainmap_metadata_ext_t* metadata);
uint8_t encodeGain(float y_sdr, float y_hdr, uhdr_gainmap_metadata_ext_t* metadata,
float log2MinContentBoost, float log2MaxContentBoost);

/*
Expand All @@ -504,8 +503,8 @@ uint8_t encodeGain(float y_sdr, float y_hdr, ultrahdr_metadata_ptr metadata,
* offsetSdr 0.0, offsetHdr 0.0, hdrCapacityMin 1.0, and hdrCapacityMax equal to
* gainMapMax, as this library encodes.
*/
Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata);
Color applyGain(Color e, float gain, ultrahdr_metadata_ptr metadata, float displayBoost);
Color applyGain(Color e, float gain, uhdr_gainmap_metadata_ext_t* metadata);
Color applyGain(Color e, float gain, uhdr_gainmap_metadata_ext_t* metadata, float displayBoost);
Color applyGainLUT(Color e, float gain, GainLUT& gainLUT);

/*
Expand All @@ -516,46 +515,44 @@ Color applyGainLUT(Color e, float gain, GainLUT& gainLUT);
* offsetSdr 0.0, offsetHdr 0.0, hdrCapacityMin 1.0, and hdrCapacityMax equal to
* gainMapMax, as this library encodes.
*/
Color applyGain(Color e, Color gain, ultrahdr_metadata_ptr metadata);
Color applyGain(Color e, Color gain, ultrahdr_metadata_ptr metadata, float displayBoost);
Color applyGain(Color e, Color gain, uhdr_gainmap_metadata_ext_t* metadata);
Color applyGain(Color e, Color gain, uhdr_gainmap_metadata_ext_t* metadata, float displayBoost);
Color applyGainLUT(Color e, Color gain, GainLUT& gainLUT);

/*
* Helper for sampling from YUV 420 images.
*/
Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y);
Color getYuv420Pixel(uhdr_raw_image_t* image, size_t x, size_t y);

/*
* Helper for sampling from P010 images.
*
* Expect narrow-range image data for P010.
*/
Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y);
Color getP010Pixel(uhdr_raw_image_t* image, size_t x, size_t y);

/*
* Sample the image at the provided location, with a weighting based on nearby
* pixels and the map scale factor.
*/
Color sampleYuv420(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y);
Color sampleYuv420(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y);

/*
* Sample the image at the provided location, with a weighting based on nearby
* pixels and the map scale factor.
*
* Expect narrow-range image data for P010.
*/
Color sampleP010(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y);
Color sampleP010(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y);

/*
* Sample the gain value for the map from a given x,y coordinate on a scale
* that is map scale factor larger than the map size.
*/
float sampleMap(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y);
float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
float sampleMap(uhdr_raw_image_t* map, float map_scale_factor, size_t x, size_t y);
float sampleMap(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y,
ShepardsIDW& weightTables);
Color sampleMap3Channel(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y,
Color sampleMap3Channel(uhdr_raw_image_t* map, float map_scale_factor, size_t x, size_t y,
bool has_alpha);
Color sampleMap3Channel(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
Color sampleMap3Channel(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y,
ShepardsIDW& weightTables, bool has_alpha);

/*
Expand All @@ -572,6 +569,11 @@ uint32_t colorToRgba1010102(Color e_gamma);
*/
uint64_t colorToRgbaF16(Color e_gamma);

/*
* Helper for copying raw image descriptor
*/
uhdr_error_info_t copy_raw_image(uhdr_raw_image_t* src, uhdr_raw_image_t* dst);

/*
* Helper for preparing encoder raw inputs for encoding
*/
Expand Down
Loading
Loading