mirror of
https://github.com/androidx/media.git
synced 2025-05-09 00:20:45 +08:00
MediaFormatUtil: Reduce Color API 29 restriction to API 24.
Implement getMediaFormatInteger, a helper method simulating mediaformat.getInteger(name, defaultValue). This reduces the API 29 restriction from MediaFormatUtil.getColorInfo to API 24, in particular removing the method-based restriction to a constant-based restriction, so that we can reduce usage of the API 29 class. This also allows us to slightly simplify prior use-cases where we'd check containsKey and getInteger to have a default value. PiperOrigin-RevId: 511184301
This commit is contained in:
parent
faad46318b
commit
465301da7c
@ -197,18 +197,24 @@ public final class MediaFormatUtil {
|
|||||||
/**
|
/**
|
||||||
* Creates and returns a {@code ColorInfo}, if a valid instance is described in the {@link
|
* Creates and returns a {@code ColorInfo}, if a valid instance is described in the {@link
|
||||||
* MediaFormat}.
|
* MediaFormat}.
|
||||||
|
*
|
||||||
|
* <p>Under API 24, {@code null} will always be returned, because {@link MediaFormat} color keys
|
||||||
|
* like {@link MediaFormat#KEY_COLOR_STANDARD} were only added in API 24.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public static ColorInfo getColorInfo(MediaFormat mediaFormat) {
|
public static ColorInfo getColorInfo(MediaFormat mediaFormat) {
|
||||||
if (SDK_INT < 29) {
|
if (SDK_INT < 24) {
|
||||||
|
// MediaFormat KEY_COLOR_TRANSFER and other KEY_COLOR values available from API 24.
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
int colorSpace =
|
int colorSpace =
|
||||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_STANDARD, /* defaultValue= */ Format.NO_VALUE);
|
getInteger(
|
||||||
|
mediaFormat, MediaFormat.KEY_COLOR_STANDARD, /* defaultValue= */ Format.NO_VALUE);
|
||||||
int colorRange =
|
int colorRange =
|
||||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_RANGE, /* defaultValue= */ Format.NO_VALUE);
|
getInteger(mediaFormat, MediaFormat.KEY_COLOR_RANGE, /* defaultValue= */ Format.NO_VALUE);
|
||||||
int colorTransfer =
|
int colorTransfer =
|
||||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER, /* defaultValue= */ Format.NO_VALUE);
|
getInteger(
|
||||||
|
mediaFormat, MediaFormat.KEY_COLOR_TRANSFER, /* defaultValue= */ Format.NO_VALUE);
|
||||||
@Nullable
|
@Nullable
|
||||||
ByteBuffer hdrStaticInfoByteBuffer = mediaFormat.getByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO);
|
ByteBuffer hdrStaticInfoByteBuffer = mediaFormat.getByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO);
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -240,6 +246,16 @@ public final class MediaFormatUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the same functionality as {@link MediaFormat#getInteger(String, int)}.
|
||||||
|
*
|
||||||
|
* <p>{@link MediaFormat#getInteger(String, int)} is only available from API 29. This convenience
|
||||||
|
* method provides support on lower API versions.
|
||||||
|
*/
|
||||||
|
public static int getInteger(MediaFormat mediaFormat, String name, int defaultValue) {
|
||||||
|
return mediaFormat.containsKey(name) ? mediaFormat.getInteger(name) : defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] getArray(ByteBuffer byteBuffer) {
|
public static byte[] getArray(ByteBuffer byteBuffer) {
|
||||||
byte[] array = new byte[byteBuffer.remaining()];
|
byte[] array = new byte[byteBuffer.remaining()];
|
||||||
byteBuffer.get(array);
|
byteBuffer.get(array);
|
||||||
|
@ -34,7 +34,7 @@ import java.io.IOException;
|
|||||||
/**
|
/**
|
||||||
* Assert that the file has a certain color transfer, if supported on this device.
|
* Assert that the file has a certain color transfer, if supported on this device.
|
||||||
*
|
*
|
||||||
* <p>This will silently pass if under API 29, or if decoding this file is not supported on this
|
* <p>This will silently pass if under API 24, or if decoding this file is not supported on this
|
||||||
* device.
|
* device.
|
||||||
*
|
*
|
||||||
* @param filePath The path of the input file.
|
* @param filePath The path of the input file.
|
||||||
@ -43,9 +43,8 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
public static void maybeAssertFileHasColorTransfer(
|
public static void maybeAssertFileHasColorTransfer(
|
||||||
@Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) throws IOException {
|
@Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) throws IOException {
|
||||||
if (Util.SDK_INT < 29) {
|
if (Util.SDK_INT < 24) {
|
||||||
// Skipping on this API version due to lack of support for MediaFormat#getInteger, which is
|
// MediaFormat#KEY_COLOR_TRANSFER unsupported before API 24.
|
||||||
// required for MediaFormatUtil#getColorInfo.
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DecodeOneFrameUtil.Listener listener =
|
DecodeOneFrameUtil.Listener listener =
|
||||||
|
@ -31,7 +31,6 @@ import androidx.media3.common.ColorInfo;
|
|||||||
import androidx.media3.common.Effect;
|
import androidx.media3.common.Effect;
|
||||||
import androidx.media3.common.MediaItem;
|
import androidx.media3.common.MediaItem;
|
||||||
import androidx.media3.common.util.Log;
|
import androidx.media3.common.util.Log;
|
||||||
import androidx.media3.common.util.Util;
|
|
||||||
import androidx.media3.effect.ScaleAndRotateTransformation;
|
import androidx.media3.effect.ScaleAndRotateTransformation;
|
||||||
import androidx.media3.transformer.EditedMediaItem;
|
import androidx.media3.transformer.EditedMediaItem;
|
||||||
import androidx.media3.transformer.Effects;
|
import androidx.media3.transformer.Effects;
|
||||||
@ -282,13 +281,6 @@ public class HdrEditingTest {
|
|||||||
public void exportUnexpectedColorInfo() throws Exception {
|
public void exportUnexpectedColorInfo() throws Exception {
|
||||||
String testId = "exportUnexpectedColorInfo";
|
String testId = "exportUnexpectedColorInfo";
|
||||||
Context context = ApplicationProvider.getApplicationContext();
|
Context context = ApplicationProvider.getApplicationContext();
|
||||||
if (Util.SDK_INT < 29) {
|
|
||||||
recordTestSkipped(
|
|
||||||
context,
|
|
||||||
testId,
|
|
||||||
/* reason= */ "API version lacks support for MediaFormat#getInteger(String, int).");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Transformer transformer = new Transformer.Builder(context).build();
|
Transformer transformer = new Transformer.Builder(context).build();
|
||||||
MediaItem mediaItem =
|
MediaItem mediaItem =
|
||||||
|
@ -109,18 +109,17 @@ public final class DefaultCodec implements Codec {
|
|||||||
|
|
||||||
@Nullable MediaCodec mediaCodec = null;
|
@Nullable MediaCodec mediaCodec = null;
|
||||||
@Nullable Surface inputSurface = null;
|
@Nullable Surface inputSurface = null;
|
||||||
boolean requestedHdrToneMapping =
|
boolean requestedHdrToneMapping = isSdrToneMappingEnabled(configurationMediaFormat);
|
||||||
SDK_INT >= 29 && Api29.isSdrToneMappingEnabled(configurationMediaFormat);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
mediaCodec = MediaCodec.createByCodecName(mediaCodecName);
|
mediaCodec = MediaCodec.createByCodecName(mediaCodecName);
|
||||||
configureCodec(mediaCodec, configurationMediaFormat, isDecoder, outputSurface);
|
configureCodec(mediaCodec, configurationMediaFormat, isDecoder, outputSurface);
|
||||||
if (SDK_INT >= 29 && requestedHdrToneMapping) {
|
if (requestedHdrToneMapping) {
|
||||||
// The MediaCodec input format reflects whether tone-mapping is possible after configure().
|
// The MediaCodec input format reflects whether tone-mapping is possible after configure().
|
||||||
// See
|
// See
|
||||||
// https://developer.android.com/reference/android/media/MediaFormat#KEY_COLOR_TRANSFER_REQUEST.
|
// https://developer.android.com/reference/android/media/MediaFormat#KEY_COLOR_TRANSFER_REQUEST.
|
||||||
checkArgument(
|
checkArgument(
|
||||||
Api29.isSdrToneMappingEnabled(mediaCodec.getInputFormat()),
|
isSdrToneMappingEnabled(mediaCodec.getInputFormat()),
|
||||||
"Tone-mapping requested but not supported by the decoder.");
|
"Tone-mapping requested but not supported by the decoder.");
|
||||||
}
|
}
|
||||||
if (isVideo && !isDecoder) {
|
if (isVideo && !isDecoder) {
|
||||||
@ -327,10 +326,10 @@ public final class DefaultCodec implements Codec {
|
|||||||
if (outputBufferIndex < 0) {
|
if (outputBufferIndex < 0) {
|
||||||
if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
|
if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
|
||||||
outputFormat = convertToFormat(mediaCodec.getOutputFormat());
|
outputFormat = convertToFormat(mediaCodec.getOutputFormat());
|
||||||
boolean isToneMappingEnabled =
|
|
||||||
SDK_INT >= 29 && Api29.isSdrToneMappingEnabled(configurationMediaFormat);
|
|
||||||
ColorInfo expectedColorInfo =
|
ColorInfo expectedColorInfo =
|
||||||
isToneMappingEnabled ? ColorInfo.SDR_BT709_LIMITED : configurationFormat.colorInfo;
|
isSdrToneMappingEnabled(configurationMediaFormat)
|
||||||
|
? ColorInfo.SDR_BT709_LIMITED
|
||||||
|
: configurationFormat.colorInfo;
|
||||||
if (!areColorTransfersEqual(expectedColorInfo, outputFormat.colorInfo)) {
|
if (!areColorTransfersEqual(expectedColorInfo, outputFormat.colorInfo)) {
|
||||||
// TODO(b/237674316): The container ColorInfo's transfer doesn't match the decoder output
|
// TODO(b/237674316): The container ColorInfo's transfer doesn't match the decoder output
|
||||||
// MediaFormat, or we requested tone-mapping but it hasn't been applied. We should
|
// MediaFormat, or we requested tone-mapping but it hasn't been applied. We should
|
||||||
@ -457,20 +456,19 @@ public final class DefaultCodec implements Codec {
|
|||||||
TraceUtil.endSection();
|
TraceUtil.endSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isSdrToneMappingEnabled(MediaFormat mediaFormat) {
|
||||||
|
// MediaFormat.KEY_COLOR_TRANSFER_REQUEST was added in API 31.
|
||||||
|
return SDK_INT >= 31
|
||||||
|
&& MediaFormatUtil.getInteger(
|
||||||
|
mediaFormat, MediaFormat.KEY_COLOR_TRANSFER_REQUEST, /* defaultValue= */ 0)
|
||||||
|
== MediaFormat.COLOR_TRANSFER_SDR_VIDEO;
|
||||||
|
}
|
||||||
|
|
||||||
@RequiresApi(29)
|
@RequiresApi(29)
|
||||||
private static final class Api29 {
|
private static final class Api29 {
|
||||||
@DoNotInline
|
@DoNotInline
|
||||||
public static String getCanonicalName(MediaCodec mediaCodec) {
|
public static String getCanonicalName(MediaCodec mediaCodec) {
|
||||||
return mediaCodec.getCanonicalName();
|
return mediaCodec.getCanonicalName();
|
||||||
}
|
}
|
||||||
|
|
||||||
@DoNotInline
|
|
||||||
public static boolean isSdrToneMappingEnabled(MediaFormat mediaFormat) {
|
|
||||||
// MediaFormat.getInteger(String, int) was added in API 29 but applying a color transfer
|
|
||||||
// request is only possible from API 31.
|
|
||||||
return SDK_INT >= 31
|
|
||||||
&& mediaFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER_REQUEST, /* defaultValue= */ 0)
|
|
||||||
== MediaFormat.COLOR_TRANSFER_SDR_VIDEO;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user