From 07d1970f3560aa584abda7f57bcfd87af57b6f70 Mon Sep 17 00:00:00 2001 From: huangdarwin Date: Fri, 25 Nov 2022 15:59:49 +0000 Subject: [PATCH] HDR: Add COLOR_TRANSFER_LINEAR in C.java. This is more clear than using Format.NO_VALUE, when we do actually intend for an output value. Also, fix @see formatting by using summary fragments instead, and add an error output for OETF and EOTF transfer functions. PiperOrigin-RevId: 490910229 --- .../java/com/google/android/exoplayer2/C.java | 25 +++++++++++-------- .../exoplayer2/util/MediaFormatUtil.java | 3 ++- .../android/exoplayer2/video/ColorInfo.java | 17 ++++++++----- .../shaders/fragment_shader_oetf_es3.glsl | 12 +++++++-- ...hader_transformation_external_yuv_es3.glsl | 7 +++++- 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/C.java b/library/common/src/main/java/com/google/android/exoplayer2/C.java index c2ce271b7a..d6d074c736 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/C.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/C.java @@ -1048,24 +1048,27 @@ public final class C { // LINT.IfChange(color_transfer) /** * Video color transfer characteristics. One of {@link Format#NO_VALUE}, {@link - * #COLOR_TRANSFER_SDR}, {@link #COLOR_TRANSFER_ST2084} or {@link #COLOR_TRANSFER_HLG}. + * #COLOR_TRANSFER_LINEAR}, {@link #COLOR_TRANSFER_SDR}, {@link #COLOR_TRANSFER_ST2084} or {@link + * #COLOR_TRANSFER_HLG}. */ @Documented @Retention(RetentionPolicy.SOURCE) @Target(TYPE_USE) - @IntDef({Format.NO_VALUE, COLOR_TRANSFER_SDR, COLOR_TRANSFER_ST2084, COLOR_TRANSFER_HLG}) + @IntDef({ + Format.NO_VALUE, + COLOR_TRANSFER_LINEAR, + COLOR_TRANSFER_SDR, + COLOR_TRANSFER_ST2084, + COLOR_TRANSFER_HLG + }) public @interface ColorTransfer {} - /** - * @see MediaFormat#COLOR_TRANSFER_SDR_VIDEO - */ + /** See {@link MediaFormat#COLOR_TRANSFER_LINEAR}. */ + public static final int COLOR_TRANSFER_LINEAR = MediaFormat.COLOR_TRANSFER_LINEAR; + /** See {@link MediaFormat#COLOR_TRANSFER_SDR_VIDEO}. */ public static final int COLOR_TRANSFER_SDR = MediaFormat.COLOR_TRANSFER_SDR_VIDEO; - /** - * @see MediaFormat#COLOR_TRANSFER_ST2084 - */ + /** See {@link MediaFormat#COLOR_TRANSFER_ST2084}. */ public static final int COLOR_TRANSFER_ST2084 = MediaFormat.COLOR_TRANSFER_ST2084; - /** - * @see MediaFormat#COLOR_TRANSFER_HLG - */ + /** See {@link MediaFormat#COLOR_TRANSFER_HLG}. */ public static final int COLOR_TRANSFER_HLG = MediaFormat.COLOR_TRANSFER_HLG; // LINT.IfChange(color_range) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/MediaFormatUtil.java b/library/common/src/main/java/com/google/android/exoplayer2/util/MediaFormatUtil.java index 758f535dd8..903692c439 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/MediaFormatUtil.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/MediaFormatUtil.java @@ -321,7 +321,8 @@ public final class MediaFormatUtil { /** Whether this is a valid {@link C.ColorTransfer} instance. */ private static boolean isValidColorTransfer(int colorTransfer) { // LINT.IfChange(color_transfer) - return colorTransfer == C.COLOR_TRANSFER_SDR + return colorTransfer == C.COLOR_TRANSFER_LINEAR + || colorTransfer == C.COLOR_TRANSFER_SDR || colorTransfer == C.COLOR_TRANSFER_ST2084 || colorTransfer == C.COLOR_TRANSFER_HLG || colorTransfer == Format.NO_VALUE; diff --git a/library/common/src/main/java/com/google/android/exoplayer2/video/ColorInfo.java b/library/common/src/main/java/com/google/android/exoplayer2/video/ColorInfo.java index 9215907a39..58213832bf 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/video/ColorInfo.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/video/ColorInfo.java @@ -90,11 +90,16 @@ public final class ColorInfo implements Bundleable { } } - /** Returns whether the {@code ColorInfo} uses an HDR {@link C.ColorTransfer}. */ + /** + * Returns whether the {@code ColorInfo} uses an HDR {@link C.ColorTransfer}. + * + *

{@link C#COLOR_TRANSFER_LINEAR} is not considered to be an HDR {@link C.ColorTransfer}, + * because it may represent either SDR or HDR contents. + */ public static boolean isTransferHdr(@Nullable ColorInfo colorInfo) { return colorInfo != null - && colorInfo.colorTransfer != Format.NO_VALUE - && colorInfo.colorTransfer != C.COLOR_TRANSFER_SDR; + && (colorInfo.colorTransfer == C.COLOR_TRANSFER_HLG + || colorInfo.colorTransfer == C.COLOR_TRANSFER_ST2084); } /** @@ -110,9 +115,9 @@ public final class ColorInfo implements Bundleable { public final @C.ColorRange int colorRange; /** - * The color transfer characteristics of the video. Valid values are {@link C#COLOR_TRANSFER_HLG}, - * {@link C#COLOR_TRANSFER_ST2084}, {@link C#COLOR_TRANSFER_SDR} or {@link Format#NO_VALUE} if - * unknown. + * The color transfer characteristics of the video. Valid values are {@link + * C#COLOR_TRANSFER_LINEAR}, {@link C#COLOR_TRANSFER_HLG}, {@link C#COLOR_TRANSFER_ST2084}, {@link + * C#COLOR_TRANSFER_SDR} or {@link Format#NO_VALUE} if unknown. */ public final @C.ColorTransfer int colorTransfer; diff --git a/library/effect/src/main/assets/shaders/fragment_shader_oetf_es3.glsl b/library/effect/src/main/assets/shaders/fragment_shader_oetf_es3.glsl index 42e14e0eb8..31dd05b80a 100644 --- a/library/effect/src/main/assets/shaders/fragment_shader_oetf_es3.glsl +++ b/library/effect/src/main/assets/shaders/fragment_shader_oetf_es3.glsl @@ -79,12 +79,20 @@ highp vec3 pqOetf(highp vec3 linearColor) { highp vec3 getElectricalColor(highp vec3 linearColor) { // LINT.IfChange(color_transfer) const int COLOR_TRANSFER_ST2084 = 6; - return (uOetfColorTransfer == COLOR_TRANSFER_ST2084) ? - pqOetf(linearColor) : hlgOetf(linearColor); + const int COLOR_TRANSFER_HLG = 7; + if (uOetfColorTransfer == COLOR_TRANSFER_ST2084) { + return pqOetf(linearColor); + } else if (uOetfColorTransfer == COLOR_TRANSFER_HLG) { + return hlgOetf(linearColor); + } else { + // Output red as an obviously visible error. + return vec3(1.0, 0.0, 0.0); + } } void main() { vec4 inputColor = texture(uTexSampler, vTexSamplingCoord); + // transformedColors is an optical color. vec4 transformedColors = uRgbMatrix * vec4(inputColor.rgb, 1); outColor = vec4(getElectricalColor(transformedColors.rgb), inputColor.a); } diff --git a/library/effect/src/main/assets/shaders/fragment_shader_transformation_external_yuv_es3.glsl b/library/effect/src/main/assets/shaders/fragment_shader_transformation_external_yuv_es3.glsl index aa238028fc..f06e6967e1 100644 --- a/library/effect/src/main/assets/shaders/fragment_shader_transformation_external_yuv_es3.glsl +++ b/library/effect/src/main/assets/shaders/fragment_shader_transformation_external_yuv_es3.glsl @@ -86,12 +86,17 @@ highp vec3 getOpticalColor(highp vec3 electricalColor) { const int COLOR_TRANSFER_ST2084 = 6; const int COLOR_TRANSFER_HLG = 7; + const int FORMAT_NO_VALUE = -1; + if (uEotfColorTransfer == COLOR_TRANSFER_ST2084) { return pqEotf(electricalColor); } else if (uEotfColorTransfer == COLOR_TRANSFER_HLG) { return hlgEotf(electricalColor); - } else { + } else if (uEotfColorTransfer == FORMAT_NO_VALUE) { return electricalColor; + } else { + // Output red as an obviously visible error. + return vec3(1.0, 0.0, 0.0); } }