Skip to content

Commit

Permalink
add support for handling multichannel metadata
Browse files Browse the repository at this point in the history
This change introduces support for encoding and decoding content with
gainmap metadata signalled independently for all channels in a
multi-channel gainmap image.

Test: ./ultrahdr_unit_test

Co-authored-by: Vivek R Jadhav <[email protected]>
Change-Id: I9306fe13c4651c7d55a830d0597924a8d25d2743
  • Loading branch information
ram-mohan and vivekrj1806 committed Dec 6, 2024
1 parent 168f297 commit 7543ae7
Show file tree
Hide file tree
Showing 21 changed files with 657 additions and 465 deletions.
16 changes: 8 additions & 8 deletions examples/metadata.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
--maxContentBoost 6.0
--minContentBoost 1.0
--gamma 1.0
--offsetSdr 0.0
--offsetHdr 0.0
--hdrCapacityMin 1.0
--hdrCapacityMax 6.0
--useBaseColorSpace 1
--maxContentBoost 264.18 22.2026 857.325
--minContentBoost 1 1.30101 1
--gamma 1 1 1
--offsetSdr 1e-07 1e-07 1e-07
--offsetHdr 1e-07 1e-07 1e-07
--hdrCapacityMin 1
--hdrCapacityMax 49.2611
--useBaseColorSpace 1
54 changes: 40 additions & 14 deletions examples/ultrahdr_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,15 @@ bool UltraHdrAppInput::fillGainMapCompressedImageHandle() {

void parse_argument(uhdr_gainmap_metadata* metadata, char* argument, float* value) {
if (!strcmp(argument, "maxContentBoost"))
metadata->max_content_boost = *value;
std::copy(value, value + 3, metadata->max_content_boost);
else if (!strcmp(argument, "minContentBoost"))
metadata->min_content_boost = *value;
std::copy(value, value + 3, metadata->min_content_boost);
else if (!strcmp(argument, "gamma"))
metadata->gamma = *value;
std::copy(value, value + 3, metadata->gamma);
else if (!strcmp(argument, "offsetSdr"))
metadata->offset_sdr = *value;
std::copy(value, value + 3, metadata->offset_sdr);
else if (!strcmp(argument, "offsetHdr"))
metadata->offset_hdr = *value;
std::copy(value, value + 3, metadata->offset_hdr);
else if (!strcmp(argument, "hdrCapacityMin"))
metadata->hdr_capacity_min = *value;
else if (!strcmp(argument, "hdrCapacityMax"))
Expand All @@ -589,11 +589,14 @@ bool UltraHdrAppInput::fillGainMapMetadataDescriptor() {
}
std::string line;
char argument[128];
float value;
float value[3];
while (std::getline(file, line)) {
if (sscanf(line.c_str(), "--%s %f", argument, &value) == 2) {
parse_argument(&mGainMapMetadata, argument, &value);
}
int count = sscanf(line.c_str(), "--%s %f %f %f", argument, &value[0], &value[1], &value[2]);
if (count == 2) value[1] = value[2] = value[0];
if (count == 2 || count == 4)
parse_argument(&mGainMapMetadata, argument, value);
else
std::cout << " Ignoring line " << line << std::endl;
}
file.close();
return true;
Expand All @@ -614,11 +617,34 @@ bool UltraHdrAppInput::writeGainMapMetadataToFile(uhdr_gainmap_metadata_t* metad
if (!file.is_open()) {
return false;
}
file << "--maxContentBoost " << metadata->max_content_boost << std::endl;
file << "--minContentBoost " << metadata->min_content_boost << std::endl;
file << "--gamma " << metadata->gamma << std::endl;
file << "--offsetSdr " << metadata->offset_sdr << std::endl;
file << "--offsetHdr " << metadata->offset_hdr << std::endl;
bool allChannelsIdentical = metadata->max_content_boost[0] == metadata->max_content_boost[1] &&
metadata->max_content_boost[0] == metadata->max_content_boost[2] &&
metadata->min_content_boost[0] == metadata->min_content_boost[1] &&
metadata->min_content_boost[0] == metadata->min_content_boost[2] &&
metadata->gamma[0] == metadata->gamma[1] &&
metadata->gamma[0] == metadata->gamma[2] &&
metadata->offset_sdr[0] == metadata->offset_sdr[1] &&
metadata->offset_sdr[0] == metadata->offset_sdr[2] &&
metadata->offset_hdr[0] == metadata->offset_hdr[1] &&
metadata->offset_hdr[0] == metadata->offset_hdr[2];
if (allChannelsIdentical) {
file << "--maxContentBoost " << metadata->max_content_boost[0] << std::endl;
file << "--minContentBoost " << metadata->min_content_boost[0] << std::endl;
file << "--gamma " << metadata->gamma[0] << std::endl;
file << "--offsetSdr " << metadata->offset_sdr[0] << std::endl;
file << "--offsetHdr " << metadata->offset_hdr[0] << std::endl;
} else {
file << "--maxContentBoost " << metadata->max_content_boost[0] << " "
<< metadata->max_content_boost[1] << " " << metadata->max_content_boost[2] << std::endl;
file << "--minContentBoost " << metadata->min_content_boost[0] << " "
<< metadata->min_content_boost[1] << " " << metadata->min_content_boost[2] << std::endl;
file << "--gamma " << metadata->gamma[0] << " " << metadata->gamma[1] << " "
<< metadata->gamma[2] << std::endl;
file << "--offsetSdr " << metadata->offset_sdr[0] << " " << metadata->offset_sdr[1] << " "
<< metadata->offset_sdr[2] << std::endl;
file << "--offsetHdr " << metadata->offset_hdr[0] << " " << metadata->offset_hdr[1] << " "
<< metadata->offset_hdr[2] << std::endl;
}
file << "--hdrCapacityMin " << metadata->hdr_capacity_min << std::endl;
file << "--hdrCapacityMax " << metadata->hdr_capacity_max << std::endl;
file << "--useBaseColorSpace " << metadata->use_base_cg << std::endl;
Expand Down
62 changes: 42 additions & 20 deletions fuzzer/ultrahdr_enc_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ constexpr int kTfMax = UHDR_CT_SRGB;

class UltraHdrEncFuzzer {
public:
UltraHdrEncFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {};
UltraHdrEncFuzzer(const uint8_t* data, size_t size) : mFdp(data, size){};
void process();
template <typename T>
void fillBuffer(T* data, int width, int height, int stride);
Expand Down Expand Up @@ -69,9 +69,11 @@ void UltraHdrEncFuzzer::fillBuffer(T* data, int width, int height, int stride) {

void UltraHdrEncFuzzer::process() {
if (mFdp.remaining_bytes()) {
struct uhdr_raw_image hdrImg{};
struct uhdr_raw_image sdrImg{};
struct uhdr_raw_image gainmapImg{};
struct uhdr_raw_image hdrImg {};
struct uhdr_raw_image sdrImg {};
struct uhdr_raw_image gainmapImg {};

float maxBoost[3], minBoost[3], gamma[3], offsetSdr[3], offsetHdr[3];

// which encode api to select
int muxSwitch = mFdp.ConsumeIntegralInRange<int8_t>(0, 4);
Expand Down Expand Up @@ -129,12 +131,28 @@ void UltraHdrEncFuzzer::process() {
// encoding speed preset
auto enc_preset = mFdp.ConsumeBool() ? UHDR_USAGE_REALTIME : UHDR_USAGE_BEST_QUALITY;

bool are_all_channels_identical = mFdp.ConsumeBool();

// gainmap metadata
auto minBoost = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
auto maxBoost = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
auto gamma = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 5);
auto offsetSdr = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
auto offsetHdr = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
if (are_all_channels_identical) {
minBoost[0] = minBoost[1] = minBoost[2] =
mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
maxBoost[0] = maxBoost[1] = maxBoost[2] =
mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
gamma[0] = gamma[1] = gamma[2] = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 5);
offsetSdr[0] = offsetSdr[1] = offsetSdr[2] =
mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
offsetHdr[0] = offsetHdr[1] = offsetHdr[2] =
mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
} else {
for (int i = 0; i < 3; i++) {
minBoost[i] = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
maxBoost[i] = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
gamma[i] = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 5);
offsetSdr[i] = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
offsetHdr[i] = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
}
}
auto minCapacity = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 48.0f);
auto maxCapacity = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 48.0f);
auto useBaseCg = mFdp.ConsumeBool();
Expand Down Expand Up @@ -194,10 +212,14 @@ void UltraHdrEncFuzzer::process() {
ALOGV("base image quality %d ", (int)base_quality);
ALOGV("encoding preset %d ", (int)enc_preset);
ALOGV(
"gainmap metadata: min content boost %f, max content boost %f, gamma %f, offset sdr %f, "
"offset hdr %f, hdr min capacity %f, hdr max capacity %f, useBaseCg %d",
(float)minBoost, (float)maxBoost, (float)gamma, (float)offsetSdr, (float)offsetHdr,
(float)minCapacity, (float)maxCapacity, (int)useBaseCg);
"gainmap metadata: min content boost %f %f %f, max content boost %f %f %f, gamma %f %f %f, "
"offset sdr %f %f %f, offset hdr %f %f %f, hdr min capacity %f, hdr max capacity %f, "
"useBaseCg %d",
(float)minBoost[0], (float)minBoost[1], (float)minBoost[2], (float)maxBoost[0],
(float)maxBoost[1], (float)maxBoost[2], (float)gamma[0], (float)gamma[1], (float)gamma[2],
(float)offsetSdr[0], (float)offsetSdr[1], offsetSdr[2], (float)offsetHdr[0],
(float)offsetHdr[1], (float)offsetHdr[2], (float)minCapacity, (float)maxCapacity,
(int)useBaseCg);
ALOGV("hdr intent luma stride %d, chroma stride %d", yHdrStride, uvHdrStride);
ALOGV("sdr intent luma stride %d, chroma stride %d", ySdrStride, uvSdrStride);
if (applyMirror) ALOGV("added mirror effect, direction %d", (int)direction);
Expand Down Expand Up @@ -362,8 +384,8 @@ void UltraHdrEncFuzzer::process() {
ON_ERR(uhdr_enc_set_exif_data(enc_handle, &exif))
ON_ERR(uhdr_enc_set_using_multi_channel_gainmap(enc_handle, multi_channel_gainmap))
ON_ERR(uhdr_enc_set_gainmap_scale_factor(enc_handle, gm_scale_factor))
ON_ERR(uhdr_enc_set_gainmap_gamma(enc_handle, gamma))
ON_ERR(uhdr_enc_set_min_max_content_boost(enc_handle, minBoost, maxBoost))
ON_ERR(uhdr_enc_set_gainmap_gamma(enc_handle, gamma[0]))
ON_ERR(uhdr_enc_set_min_max_content_boost(enc_handle, minBoost[0], maxBoost[0]))
ON_ERR(uhdr_enc_set_target_display_peak_brightness(enc_handle, targetDispPeakBrightness))
ON_ERR(uhdr_enc_set_preset(enc_handle, enc_preset))
ON_ERR(uhdr_enable_gpu_acceleration(enc_handle, 1))
Expand Down Expand Up @@ -393,11 +415,11 @@ void UltraHdrEncFuzzer::process() {
UHDR_CODEC_OK) {
struct uhdr_compressed_image jpegGainMap = gainMapEncoder.getCompressedImage();
uhdr_gainmap_metadata metadata;
metadata.max_content_boost = maxBoost;
metadata.min_content_boost = minBoost;
metadata.gamma = gamma;
metadata.offset_sdr = offsetSdr;
metadata.offset_hdr = offsetHdr;
std::copy(maxBoost, maxBoost + 3, metadata.max_content_boost);
std::copy(minBoost, minBoost + 3, metadata.min_content_boost);
std::copy(gamma, gamma + 3, metadata.gamma);
std::copy(offsetSdr, offsetSdr + 3, metadata.offset_sdr);
std::copy(offsetHdr, offsetHdr + 3, metadata.offset_hdr);
metadata.hdr_capacity_min = minCapacity;
metadata.hdr_capacity_max = maxCapacity;
metadata.use_base_cg = useBaseCg;
Expand Down
68 changes: 53 additions & 15 deletions java/UltraHdrApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

import com.google.media.codecs.ultrahdr.UltraHDRDecoder;
import com.google.media.codecs.ultrahdr.UltraHDREncoder;
Expand Down Expand Up @@ -278,33 +279,44 @@ public void fillGainMapMetadataDescriptor() throws IOException {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\s+");
if (parts.length == 2 && parts[0].startsWith("--")) {
if (parts.length >= 2 && parts[0].startsWith("--")) {
String option = parts[0].substring(2); // remove the "--" prefix
float value = Float.parseFloat(parts[1]);
float[] values = new float[3];
int count = Math.min(parts.length - 1, 3);
if (count != 1 && count != 3) {
System.err.println("ignoring line: " + line);
continue;
}
for (int i = 0; i < count; i++) {
values[i] = Float.parseFloat(parts[i + 1]);
}
if (count == 1) {
values[1] = values[2] = values[0];
}
switch (option) {
case "maxContentBoost":
mMetadata.maxContentBoost = value;
System.arraycopy(values, 0, mMetadata.maxContentBoost, 0, 3);
break;
case "minContentBoost":
mMetadata.minContentBoost = value;
System.arraycopy(values, 0, mMetadata.minContentBoost, 0, 3);
break;
case "gamma":
mMetadata.gamma = value;
System.arraycopy(values, 0, mMetadata.gamma, 0, 3);
break;
case "offsetSdr":
mMetadata.offsetSdr = value;
System.arraycopy(values, 0, mMetadata.offsetSdr, 0, 3);
break;
case "offsetHdr":
mMetadata.offsetHdr = value;
System.arraycopy(values, 0, mMetadata.offsetHdr, 0, 3);
break;
case "hdrCapacityMin":
mMetadata.hdrCapacityMin = value;
mMetadata.hdrCapacityMin = values[0];
break;
case "hdrCapacityMax":
mMetadata.hdrCapacityMax = value;
mMetadata.hdrCapacityMax = values[0];
break;
case "useBaseColorSpace":
mMetadata.useBaseColorSpace = value != 0.0f;
mMetadata.useBaseColorSpace = values[0] != 0.0f;
break;
default:
System.err.println("ignoring option: " + option);
Expand All @@ -319,11 +331,37 @@ public void fillGainMapMetadataDescriptor() throws IOException {

public void writeGainMapMetadataToFile(GainMapMetadata metadata) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(mGainMapMetadaCfgFile))) {
writer.write("--maxContentBoost " + metadata.maxContentBoost + "\n");
writer.write("--minContentBoost " + metadata.minContentBoost + "\n");
writer.write("--gamma " + metadata.gamma + "\n");
writer.write("--offsetSdr " + metadata.offsetSdr + "\n");
writer.write("--offsetHdr " + metadata.offsetHdr + "\n");
boolean allChannelsIdentical =
metadata.maxContentBoost[0] == metadata.maxContentBoost[1]
&& metadata.maxContentBoost[0] == metadata.maxContentBoost[2]
&& metadata.minContentBoost[0] == metadata.minContentBoost[1]
&& metadata.minContentBoost[0] == metadata.minContentBoost[2]
&& metadata.gamma[0] == metadata.gamma[1]
&& metadata.gamma[0] == metadata.gamma[2]
&& metadata.offsetSdr[0] == metadata.offsetSdr[1]
&& metadata.offsetSdr[0] == metadata.offsetSdr[2]
&& metadata.offsetHdr[0] == metadata.offsetHdr[1]
&& metadata.offsetHdr[0] == metadata.offsetHdr[2];
if (allChannelsIdentical) {
writer.write("--maxContentBoost " + metadata.maxContentBoost[0] + "\n");
writer.write("--minContentBoost " + metadata.minContentBoost[0] + "\n");
writer.write("--gamma " + metadata.gamma[0] + "\n");
writer.write("--offsetSdr " + metadata.offsetSdr[0] + "\n");
writer.write("--offsetHdr " + metadata.offsetHdr[0] + "\n");
} else {
writer.write("--maxContentBoost " + metadata.maxContentBoost[0] + " "
+ metadata.maxContentBoost[1] + " " + metadata.maxContentBoost[2] + "\n");
writer.write("--minContentBoost " + metadata.minContentBoost[0] + " "
+ metadata.minContentBoost[1] + " " + metadata.minContentBoost[2] + "\n");
writer.write("--gamma " + metadata.gamma[0] + " " + metadata.gamma[1] + " "
+ metadata.gamma[2] + "\n");
writer.write(
"--offsetSdr " + metadata.offsetSdr[0] + " " + metadata.offsetSdr[1] + " "
+ metadata.offsetSdr[2] + "\n");
writer.write(
"--offsetHdr " + metadata.offsetHdr[0] + " " + metadata.offsetHdr[1] + " "
+ metadata.offsetHdr[2] + "\n");
}
writer.write("--hdrCapacityMin " + metadata.hdrCapacityMin + "\n");
writer.write("--hdrCapacityMax " + metadata.hdrCapacityMax + "\n");
writer.write("--useBaseColorSpace " + (metadata.useBaseColorSpace ? "1" : "0") + "\n");
Expand Down
Loading

0 comments on commit 7543ae7

Please sign in to comment.