mirror of
https://github.com/samsonjs/media.git
synced 2026-04-03 10:55:48 +00:00
Only apply a MediaFormat-generated ColorInfo if it's valid.
Otherwise, invalid ColorInfo instances generated using faulty MediaFormat#getInteger values could cause exceptions. Confirmed that b/239435670 reproduces without this CL, and does not reproduce with this CL. PiperOrigin-RevId: 461862191
This commit is contained in:
parent
080db2a09b
commit
5533ad1713
1 changed files with 32 additions and 10 deletions
|
|
@ -429,16 +429,8 @@ public final class DefaultCodec implements Codec {
|
|||
if (MimeTypes.isVideo(mimeType)) {
|
||||
formatBuilder
|
||||
.setWidth(mediaFormat.getInteger(MediaFormat.KEY_WIDTH))
|
||||
.setHeight(mediaFormat.getInteger(MediaFormat.KEY_HEIGHT));
|
||||
if (SDK_INT >= 24) {
|
||||
// TODO(b/227624622): Set hdrStaticInfo accordingly using KEY_HDR_STATIC_INFO.
|
||||
formatBuilder.setColorInfo(
|
||||
new ColorInfo(
|
||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_STANDARD),
|
||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_RANGE),
|
||||
mediaFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER),
|
||||
/* hdrStaticInfo= */ null));
|
||||
}
|
||||
.setHeight(mediaFormat.getInteger(MediaFormat.KEY_HEIGHT))
|
||||
.setColorInfo(getColorInfo(mediaFormat));
|
||||
} else if (MimeTypes.isAudio(mimeType)) {
|
||||
// TODO(b/178685617): Only set the PCM encoding for audio/raw, once we have a way to
|
||||
// simulate more realistic codec input/output formats in tests.
|
||||
|
|
@ -478,4 +470,34 @@ public final class DefaultCodec implements Codec {
|
|||
return SDK_INT < 29
|
||||
|| context.getApplicationContext().getApplicationInfo().targetSdkVersion < 29;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static ColorInfo getColorInfo(MediaFormat mediaFormat) {
|
||||
if (SDK_INT < 29) {
|
||||
return null;
|
||||
}
|
||||
// TODO(b/227624622): Set hdrStaticInfo accordingly using KEY_HDR_STATIC_INFO.
|
||||
int colorSpace = mediaFormat.getInteger(MediaFormat.KEY_COLOR_STANDARD, Format.NO_VALUE);
|
||||
int colorRange = mediaFormat.getInteger(MediaFormat.KEY_COLOR_RANGE, Format.NO_VALUE);
|
||||
int colorTransfer = mediaFormat.getInteger(MediaFormat.KEY_COLOR_TRANSFER, Format.NO_VALUE);
|
||||
if (colorSpace != C.COLOR_SPACE_BT709
|
||||
&& colorSpace != C.COLOR_SPACE_BT601
|
||||
&& colorSpace != C.COLOR_SPACE_BT2020
|
||||
&& colorSpace != Format.NO_VALUE) {
|
||||
return null;
|
||||
}
|
||||
if (colorRange != C.COLOR_RANGE_LIMITED
|
||||
&& colorRange != C.COLOR_RANGE_FULL
|
||||
&& colorRange != Format.NO_VALUE) {
|
||||
return null;
|
||||
}
|
||||
if (colorTransfer != C.COLOR_TRANSFER_SDR
|
||||
&& colorTransfer != C.COLOR_TRANSFER_ST2084
|
||||
&& colorTransfer != C.COLOR_TRANSFER_HLG
|
||||
&& colorTransfer != Format.NO_VALUE) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ColorInfo(colorSpace, colorRange, colorTransfer, /* hdrStaticInfo= */ null);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue