mirror of
https://github.com/androidx/media.git
synced 2025-05-07 23:50:44 +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
|
||||
* 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
|
||||
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;
|
||||
}
|
||||
int colorSpace =
|
||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_STANDARD, /* defaultValue= */ Format.NO_VALUE);
|
||||
getInteger(
|
||||
mediaFormat, MediaFormat.KEY_COLOR_STANDARD, /* defaultValue= */ Format.NO_VALUE);
|
||||
int colorRange =
|
||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_RANGE, /* defaultValue= */ Format.NO_VALUE);
|
||||
getInteger(mediaFormat, MediaFormat.KEY_COLOR_RANGE, /* defaultValue= */ Format.NO_VALUE);
|
||||
int colorTransfer =
|
||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER, /* defaultValue= */ Format.NO_VALUE);
|
||||
getInteger(
|
||||
mediaFormat, MediaFormat.KEY_COLOR_TRANSFER, /* defaultValue= */ Format.NO_VALUE);
|
||||
@Nullable
|
||||
ByteBuffer hdrStaticInfoByteBuffer = mediaFormat.getByteBuffer(MediaFormat.KEY_HDR_STATIC_INFO);
|
||||
@Nullable
|
||||
@ -240,6 +246,16 @@ public final class MediaFormatUtil {
|
||||
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) {
|
||||
byte[] array = new byte[byteBuffer.remaining()];
|
||||
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.
|
||||
*
|
||||
* <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.
|
||||
*
|
||||
* @param filePath The path of the input file.
|
||||
@ -43,9 +43,8 @@ import java.io.IOException;
|
||||
*/
|
||||
public static void maybeAssertFileHasColorTransfer(
|
||||
@Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) throws IOException {
|
||||
if (Util.SDK_INT < 29) {
|
||||
// Skipping on this API version due to lack of support for MediaFormat#getInteger, which is
|
||||
// required for MediaFormatUtil#getColorInfo.
|
||||
if (Util.SDK_INT < 24) {
|
||||
// MediaFormat#KEY_COLOR_TRANSFER unsupported before API 24.
|
||||
return;
|
||||
}
|
||||
DecodeOneFrameUtil.Listener listener =
|
||||
|
@ -31,7 +31,6 @@ import androidx.media3.common.ColorInfo;
|
||||
import androidx.media3.common.Effect;
|
||||
import androidx.media3.common.MediaItem;
|
||||
import androidx.media3.common.util.Log;
|
||||
import androidx.media3.common.util.Util;
|
||||
import androidx.media3.effect.ScaleAndRotateTransformation;
|
||||
import androidx.media3.transformer.EditedMediaItem;
|
||||
import androidx.media3.transformer.Effects;
|
||||
@ -282,13 +281,6 @@ public class HdrEditingTest {
|
||||
public void exportUnexpectedColorInfo() throws Exception {
|
||||
String testId = "exportUnexpectedColorInfo";
|
||||
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();
|
||||
MediaItem mediaItem =
|
||||
|
@ -109,18 +109,17 @@ public final class DefaultCodec implements Codec {
|
||||
|
||||
@Nullable MediaCodec mediaCodec = null;
|
||||
@Nullable Surface inputSurface = null;
|
||||
boolean requestedHdrToneMapping =
|
||||
SDK_INT >= 29 && Api29.isSdrToneMappingEnabled(configurationMediaFormat);
|
||||
boolean requestedHdrToneMapping = isSdrToneMappingEnabled(configurationMediaFormat);
|
||||
|
||||
try {
|
||||
mediaCodec = MediaCodec.createByCodecName(mediaCodecName);
|
||||
configureCodec(mediaCodec, configurationMediaFormat, isDecoder, outputSurface);
|
||||
if (SDK_INT >= 29 && requestedHdrToneMapping) {
|
||||
if (requestedHdrToneMapping) {
|
||||
// The MediaCodec input format reflects whether tone-mapping is possible after configure().
|
||||
// See
|
||||
// https://developer.android.com/reference/android/media/MediaFormat#KEY_COLOR_TRANSFER_REQUEST.
|
||||
checkArgument(
|
||||
Api29.isSdrToneMappingEnabled(mediaCodec.getInputFormat()),
|
||||
isSdrToneMappingEnabled(mediaCodec.getInputFormat()),
|
||||
"Tone-mapping requested but not supported by the decoder.");
|
||||
}
|
||||
if (isVideo && !isDecoder) {
|
||||
@ -327,10 +326,10 @@ public final class DefaultCodec implements Codec {
|
||||
if (outputBufferIndex < 0) {
|
||||
if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
|
||||
outputFormat = convertToFormat(mediaCodec.getOutputFormat());
|
||||
boolean isToneMappingEnabled =
|
||||
SDK_INT >= 29 && Api29.isSdrToneMappingEnabled(configurationMediaFormat);
|
||||
ColorInfo expectedColorInfo =
|
||||
isToneMappingEnabled ? ColorInfo.SDR_BT709_LIMITED : configurationFormat.colorInfo;
|
||||
isSdrToneMappingEnabled(configurationMediaFormat)
|
||||
? ColorInfo.SDR_BT709_LIMITED
|
||||
: configurationFormat.colorInfo;
|
||||
if (!areColorTransfersEqual(expectedColorInfo, outputFormat.colorInfo)) {
|
||||
// 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
|
||||
@ -457,20 +456,19 @@ public final class DefaultCodec implements Codec {
|
||||
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)
|
||||
private static final class Api29 {
|
||||
@DoNotInline
|
||||
public static String getCanonicalName(MediaCodec mediaCodec) {
|
||||
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