Skip to content

Commit c9c8c74

Browse files
ram-mohanDichenZhang1
authored andcommitted
Update library to use definitions of ultrahdr_api everywhere
This is a major change but maintains bitexactness with previous commit. This change unifies legacy structure definitions with definitions of ultrahdr_api.h. This helps for better extensibility for new features and avoid redundancy. Legacy structures are moved to ultrahdr.h. These are deprecated and only retained for backward compatibility. Briefly, - unify ultrahdr_color_gamut with uhdr_color_gamut_t - unify ultrahdr_transfer_function with uhdr_color_transfer_t - unify ultrahdr_metadata_struct with uhdr_gainmap_metadata_t - unify jpegr_uncompressed_struct with uhdr_raw_image_t - unify jpegr_compressed_struct with uhdr_compressed_image_t - unify jpegr_exif_struct with uhdr_mem_block_t - unify status_t with uhdr_error_info_t - Deprecate ultrahdr_output_format - Added methods to Jpeg*Helper to simplify data translation between helper and its users - Improved error propogation across library Bug fix, - For images with multi channel gainmap, decoded gainmap is not copied completely for getter functions. This is fixed. - Add support for tonemapping linear transfer inputs - Fixes oss-fuzz: 69287 Test: ./ultrahdr_unit_test Test: ./ultrahdr_enc_fuzzer Test: ./ultrahdr_dec_fuzzer
1 parent fb3fb13 commit c9c8c74

28 files changed

Lines changed: 3526 additions & 2968 deletions

benchmark/benchmark_test.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,21 @@ static void BM_Encode_Api4(benchmark::State& s) {
510510
gainmapImg.data = gainmapImgInfo.imgData.data();
511511
gainmapImg.maxLength = gainmapImg.length = gainmapImgInfo.imgData.size();
512512
gainmapImg.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
513-
ultrahdr_metadata_struct uhdr_metadata;
514-
if (!getMetadataFromXMP(gainmapImgInfo.xmpData.data(), gainmapImgInfo.xmpData.size(),
515-
&uhdr_metadata)) {
513+
uhdr_gainmap_metadata_ext_t meta;
514+
if (getMetadataFromXMP(gainmapImgInfo.xmpData.data(), gainmapImgInfo.xmpData.size(), &meta)
515+
.error_code != UHDR_CODEC_OK) {
516516
s.SkipWithError("getMetadataFromXMP returned with error");
517517
return;
518518
}
519+
ultrahdr_metadata_struct uhdr_metadata;
520+
uhdr_metadata.version = meta.version;
521+
uhdr_metadata.hdrCapacityMax = meta.hdr_capacity_max;
522+
uhdr_metadata.hdrCapacityMin = meta.hdr_capacity_min;
523+
uhdr_metadata.gamma = meta.gamma;
524+
uhdr_metadata.offsetSdr = meta.offset_sdr;
525+
uhdr_metadata.offsetHdr = meta.offset_hdr;
526+
uhdr_metadata.maxContentBoost = meta.max_content_boost;
527+
uhdr_metadata.minContentBoost = meta.min_content_boost;
519528
for (auto _ : s) {
520529
status = jpegHdr.encodeJPEGR(&primaryImg, &gainmapImg, &uhdr_metadata, &jpegImgR);
521530
if (JPEGR_NO_ERROR != status) {

fuzzer/ultrahdr_enc_fuzzer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ void UltraHdrEncFuzzer::process() {
248248
yuv420ImgCopy.chroma_stride * yuv420ImgCopy.height / 2};
249249
const size_t strides[3]{yuv420ImgCopy.luma_stride, yuv420ImgCopy.chroma_stride,
250250
yuv420ImgCopy.chroma_stride};
251-
if (encoder.compressImage(planes, strides, yuv420ImgCopy.width, yuv420ImgCopy.height,
252-
UHDR_IMG_FMT_12bppYCbCr420, quality, nullptr, 0)) {
251+
if (encoder
252+
.compressImage(planes, strides, yuv420ImgCopy.width, yuv420ImgCopy.height,
253+
UHDR_IMG_FMT_12bppYCbCr420, quality, nullptr, 0)
254+
.error_code == UHDR_CODEC_OK) {
253255
jpegImg.length = encoder.getCompressedImageSize();
254256
jpegImg.maxLength = jpegImg.length;
255257
jpegImg.data = encoder.getCompressedImagePtr();
@@ -266,8 +268,10 @@ void UltraHdrEncFuzzer::process() {
266268
JpegEncoderHelper gainMapEncoder;
267269
const uint8_t* planeGm[1]{reinterpret_cast<uint8_t*>(grayImg.data)};
268270
const size_t strideGm[1]{grayImg.width};
269-
if (gainMapEncoder.compressImage(planeGm, strideGm, grayImg.width, grayImg.height,
270-
UHDR_IMG_FMT_8bppYCbCr400, quality, nullptr, 0)) {
271+
if (gainMapEncoder
272+
.compressImage(planeGm, strideGm, grayImg.width, grayImg.height,
273+
UHDR_IMG_FMT_8bppYCbCr400, quality, nullptr, 0)
274+
.error_code == UHDR_CODEC_OK) {
271275
jpegGainMap.length = gainMapEncoder.getCompressedImageSize();
272276
jpegGainMap.maxLength = jpegImg.length;
273277
jpegGainMap.data = gainMapEncoder.getCompressedImagePtr();

lib/include/ultrahdr/gainmapmath.h

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include "ultrahdr_api.h"
2525
#include "ultrahdr/ultrahdrcommon.h"
26-
#include "ultrahdr/ultrahdr.h"
2726
#include "ultrahdr/jpegr.h"
2827

2928
#if (defined(UHDR_ENABLE_INTRINSICS) && (defined(__ARM_NEON__) || defined(__ARM_NEON)))
@@ -176,23 +175,23 @@ inline uint16_t floatToHalf(float f) {
176175
constexpr int32_t kGainFactorPrecision = 10;
177176
constexpr int32_t kGainFactorNumEntries = 1 << kGainFactorPrecision;
178177
struct GainLUT {
179-
GainLUT(ultrahdr_metadata_ptr metadata) {
178+
GainLUT(uhdr_gainmap_metadata_ext_t* metadata) {
180179
this->mGammaInv = 1.0f / metadata->gamma;
181180
for (int32_t idx = 0; idx < kGainFactorNumEntries; idx++) {
182181
float value = static_cast<float>(idx) / static_cast<float>(kGainFactorNumEntries - 1);
183-
float logBoost = log2(metadata->minContentBoost) * (1.0f - value) +
184-
log2(metadata->maxContentBoost) * value;
182+
float logBoost = log2(metadata->min_content_boost) * (1.0f - value) +
183+
log2(metadata->max_content_boost) * value;
185184
mGainTable[idx] = exp2(logBoost);
186185
}
187186
}
188187

189-
GainLUT(ultrahdr_metadata_ptr metadata, float displayBoost) {
188+
GainLUT(uhdr_gainmap_metadata_ext_t* metadata, float displayBoost) {
190189
this->mGammaInv = 1.0f / metadata->gamma;
191-
float boostFactor = displayBoost > 0 ? displayBoost / metadata->maxContentBoost : 1.0f;
190+
float boostFactor = displayBoost > 0 ? displayBoost / metadata->max_content_boost : 1.0f;
192191
for (int32_t idx = 0; idx < kGainFactorNumEntries; idx++) {
193192
float value = static_cast<float>(idx) / static_cast<float>(kGainFactorNumEntries - 1);
194-
float logBoost = log2(metadata->minContentBoost) * (1.0f - value) +
195-
log2(metadata->maxContentBoost) * value;
193+
float logBoost = log2(metadata->min_content_boost) * (1.0f - value) +
194+
log2(metadata->max_content_boost) * value;
196195
mGainTable[idx] = exp2(logBoost * boostFactor);
197196
}
198197
}
@@ -432,7 +431,7 @@ inline Color identityConversion(Color e) { return e; }
432431
/*
433432
* Get the conversion to apply to the HDR image for gain map generation
434433
*/
435-
ColorTransformFn getHdrConversionFn(ultrahdr_color_gamut sdr_gamut, ultrahdr_color_gamut hdr_gamut);
434+
ColorTransformFn getHdrConversionFn(uhdr_color_gamut_t sdr_gamut, uhdr_color_gamut_t hdr_gamut);
436435

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

467-
void transformYuv420_neon(jr_uncompressed_ptr image, const int16_t* coeffs_ptr);
466+
void transformYuv420_neon(uhdr_raw_image_t* image, const int16_t* coeffs_ptr);
468467

469-
status_t convertYuv_neon(jr_uncompressed_ptr image, ultrahdr_color_gamut src_encoding,
470-
ultrahdr_color_gamut dst_encoding);
468+
uhdr_error_info_t convertYuv_neon(uhdr_raw_image_t* image, uhdr_color_gamut_t src_encoding,
469+
uhdr_color_gamut_t dst_encoding);
471470
#endif
472471

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

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

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

511510
/*
@@ -516,46 +515,44 @@ Color applyGainLUT(Color e, float gain, GainLUT& gainLUT);
516515
* offsetSdr 0.0, offsetHdr 0.0, hdrCapacityMin 1.0, and hdrCapacityMax equal to
517516
* gainMapMax, as this library encodes.
518517
*/
519-
Color applyGain(Color e, Color gain, ultrahdr_metadata_ptr metadata);
520-
Color applyGain(Color e, Color gain, ultrahdr_metadata_ptr metadata, float displayBoost);
518+
Color applyGain(Color e, Color gain, uhdr_gainmap_metadata_ext_t* metadata);
519+
Color applyGain(Color e, Color gain, uhdr_gainmap_metadata_ext_t* metadata, float displayBoost);
521520
Color applyGainLUT(Color e, Color gain, GainLUT& gainLUT);
522521

523522
/*
524523
* Helper for sampling from YUV 420 images.
525524
*/
526-
Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y);
525+
Color getYuv420Pixel(uhdr_raw_image_t* image, size_t x, size_t y);
527526

528527
/*
529528
* Helper for sampling from P010 images.
530-
*
531-
* Expect narrow-range image data for P010.
532529
*/
533-
Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y);
530+
Color getP010Pixel(uhdr_raw_image_t* image, size_t x, size_t y);
534531

535532
/*
536533
* Sample the image at the provided location, with a weighting based on nearby
537534
* pixels and the map scale factor.
538535
*/
539-
Color sampleYuv420(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y);
536+
Color sampleYuv420(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y);
540537

541538
/*
542539
* Sample the image at the provided location, with a weighting based on nearby
543540
* pixels and the map scale factor.
544541
*
545542
* Expect narrow-range image data for P010.
546543
*/
547-
Color sampleP010(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y);
544+
Color sampleP010(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y);
548545

549546
/*
550547
* Sample the gain value for the map from a given x,y coordinate on a scale
551548
* that is map scale factor larger than the map size.
552549
*/
553-
float sampleMap(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y);
554-
float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
550+
float sampleMap(uhdr_raw_image_t* map, float map_scale_factor, size_t x, size_t y);
551+
float sampleMap(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y,
555552
ShepardsIDW& weightTables);
556-
Color sampleMap3Channel(jr_uncompressed_ptr map, float map_scale_factor, size_t x, size_t y,
553+
Color sampleMap3Channel(uhdr_raw_image_t* map, float map_scale_factor, size_t x, size_t y,
557554
bool has_alpha);
558-
Color sampleMap3Channel(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y,
555+
Color sampleMap3Channel(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y,
559556
ShepardsIDW& weightTables, bool has_alpha);
560557

561558
/*
@@ -572,6 +569,11 @@ uint32_t colorToRgba1010102(Color e_gamma);
572569
*/
573570
uint64_t colorToRgbaF16(Color e_gamma);
574571

572+
/*
573+
* Helper for copying raw image descriptor
574+
*/
575+
uhdr_error_info_t copy_raw_image(uhdr_raw_image_t* src, uhdr_raw_image_t* dst);
576+
575577
/*
576578
* Helper for preparing encoder raw inputs for encoding
577579
*/

lib/include/ultrahdr/gainmapmetadata.h

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,15 @@
1818
#define ULTRAHDR_GAINMAPMETADATA_H
1919

2020
#include "ultrahdr/ultrahdrcommon.h"
21-
#include "ultrahdr/ultrahdr.h"
2221

2322
#include <memory>
2423
#include <vector>
2524

2625
namespace ultrahdr {
2726

28-
#define JPEGR_CHECK(x) \
29-
{ \
30-
status_t status = (x); \
31-
if ((status) != JPEGR_NO_ERROR) { \
32-
return status; \
33-
} \
34-
}
35-
3627
// Gain map metadata, for tone mapping between SDR and HDR.
37-
// This is the fraction version of {@code ultrahdr_metadata_struct}.
38-
struct gain_map_metadata {
28+
// This is the fraction version of {@code uhdr_gainmap_metadata_ext_t}.
29+
struct uhdr_gainmap_metadata_frac {
3930
uint32_t gainMapMinN[3];
4031
uint32_t gainMapMinD[3];
4132
uint32_t gainMapMaxN[3];
@@ -56,17 +47,17 @@ struct gain_map_metadata {
5647
bool backwardDirection;
5748
bool useBaseColorSpace;
5849

59-
static status_t encodeGainmapMetadata(const gain_map_metadata* gain_map_metadata,
60-
std::vector<uint8_t>& out_data);
50+
static uhdr_error_info_t encodeGainmapMetadata(const uhdr_gainmap_metadata_frac* in_metadata,
51+
std::vector<uint8_t>& out_data);
6152

62-
static status_t decodeGainmapMetadata(const std::vector<uint8_t>& data,
63-
gain_map_metadata* out_gain_map_metadata);
53+
static uhdr_error_info_t decodeGainmapMetadata(const std::vector<uint8_t>& in_data,
54+
uhdr_gainmap_metadata_frac* out_metadata);
6455

65-
static status_t gainmapMetadataFractionToFloat(const gain_map_metadata* from,
66-
ultrahdr_metadata_ptr to);
56+
static uhdr_error_info_t gainmapMetadataFractionToFloat(const uhdr_gainmap_metadata_frac* from,
57+
uhdr_gainmap_metadata_ext_t* to);
6758

68-
static status_t gainmapMetadataFloatToFraction(const ultrahdr_metadata_ptr from,
69-
gain_map_metadata* to);
59+
static uhdr_error_info_t gainmapMetadataFloatToFraction(const uhdr_gainmap_metadata_ext_t* from,
60+
uhdr_gainmap_metadata_frac* to);
7061

7162
void dump() const {
7263
ALOGD("GAIN MAP METADATA: \n");
@@ -98,6 +89,7 @@ struct gain_map_metadata {
9889
ALOGD("use base color space: %s\n", useBaseColorSpace ? "true" : "false");
9990
}
10091
};
92+
10193
} // namespace ultrahdr
10294

10395
#endif // ULTRAHDR_GAINMAPMETADATA_H

lib/include/ultrahdr/icc.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#define Endian_SwapBE16(n) (n)
3434
#endif
3535

36-
#include "ultrahdr/ultrahdr.h"
3736
#include "ultrahdr/jpegr.h"
3837
#include "ultrahdr/gainmapmath.h"
3938
#include "ultrahdr/jpegrutils.h"
@@ -227,12 +226,12 @@ class IccHelper {
227226
static constexpr size_t kNumChannels = 3;
228227

229228
static std::shared_ptr<DataStruct> write_text_tag(const char* text);
230-
static std::string get_desc_string(const ultrahdr_transfer_function tf,
231-
const ultrahdr_color_gamut gamut);
229+
static std::string get_desc_string(const uhdr_color_transfer_t tf,
230+
const uhdr_color_gamut_t gamut);
232231
static std::shared_ptr<DataStruct> write_xyz_tag(float x, float y, float z);
233232
static std::shared_ptr<DataStruct> write_trc_tag(const int table_entries, const void* table_16);
234233
static std::shared_ptr<DataStruct> write_trc_tag(const TransferFunction& fn);
235-
static float compute_tone_map_gain(const ultrahdr_transfer_function tf, float L);
234+
static float compute_tone_map_gain(const uhdr_color_transfer_t tf, float L);
236235
static std::shared_ptr<DataStruct> write_cicp_tag(uint32_t color_primaries,
237236
uint32_t transfer_characteristics);
238237
static std::shared_ptr<DataStruct> write_mAB_or_mBA_tag(uint32_t type, bool has_a_curves,
@@ -249,13 +248,14 @@ class IccHelper {
249248
public:
250249
// Output includes JPEG embedding identifier and chunk information, but not
251250
// APPx information.
252-
static std::shared_ptr<DataStruct> writeIccProfile(const ultrahdr_transfer_function tf,
253-
const ultrahdr_color_gamut gamut);
251+
static std::shared_ptr<DataStruct> writeIccProfile(const uhdr_color_transfer_t tf,
252+
const uhdr_color_gamut_t gamut);
254253
// NOTE: this function is not robust; it can infer gamuts that IccHelper
255254
// writes out but should not be considered a reference implementation for
256255
// robust parsing of ICC profiles or their gamuts.
257-
static ultrahdr_color_gamut readIccColorGamut(void* icc_data, size_t icc_size);
256+
static uhdr_color_gamut_t readIccColorGamut(void* icc_data, size_t icc_size);
258257
};
258+
259259
} // namespace ultrahdr
260260

261261
#endif // ULTRAHDR_ICC_H

0 commit comments

Comments
 (0)