Commit a9bc111d by andrewlewis Committed by Tofunmi Adigun-Hameed

Tidy color info checking tests

ExoPlayer extractors (backing `MetadataRetriever`) now parse the color format
from the bitstream so using `MetadataRetriever` should be an equivalent but
more lightweight way to verify the color info.

Also remove try/catch blocks in test code calling into these methods, and add
skipping based on decoder capabilities in the cases where it was missing.

PiperOrigin-RevId: 537789483
(cherry picked from commit 88db01116afa54e52f8516533256d447de45e8a4)
parent 2536efcf
......@@ -16,70 +16,57 @@
package com.google.android.exoplayer2.transformer.mh;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.common.truth.Truth.assertThat;
import android.media.MediaFormat;
import android.content.Context;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.DecodeOneFrameUtil;
import com.google.android.exoplayer2.util.MediaFormatUtil;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.MetadataRetriever;
import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.video.ColorInfo;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
/** Utilities for accessing details of media files. */
/* package */ class FileUtil {
/**
* Assert that the file has a certain color transfer, if supported on this device.
*
* <p>This will silently pass if under API 24, or if decoding this file is not supported on this
* device.
* Asserts that the file has a certain color transfer.
*
* @param context The current context.
* @param filePath The path of the input file.
* @param expectedColorTransfer The expected {@link C.ColorTransfer} for the input file.
* @throws IOException If extractor or codec creation fails.
*/
public static void maybeAssertFileHasColorTransfer(
@Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) throws IOException {
if (Util.SDK_INT < 24) {
// MediaFormat#KEY_COLOR_TRANSFER unsupported before API 24.
return;
public static void assertFileHasColorTransfer(
Context context, @Nullable String filePath, @C.ColorTransfer int expectedColorTransfer) {
TrackGroupArray trackGroupArray;
try {
trackGroupArray =
MetadataRetriever.retrieveMetadata(context, MediaItem.fromUri("file://" + filePath))
.get();
} catch (ExecutionException | InterruptedException e) {
throw new IllegalStateException(e);
}
DecodeOneFrameUtil.Listener listener =
new DecodeOneFrameUtil.Listener() {
@Override
public void onContainerExtracted(MediaFormat mediaFormat) {
@Nullable ColorInfo extractedColorInfo = MediaFormatUtil.getColorInfo(mediaFormat);
assertColorInfoHasTransfer(extractedColorInfo, expectedColorTransfer);
}
@Override
public void onFrameDecoded(MediaFormat mediaFormat) {
@Nullable ColorInfo decodedColorInfo = MediaFormatUtil.getColorInfo(mediaFormat);
assertColorInfoHasTransfer(decodedColorInfo, expectedColorTransfer);
}
};
try {
DecodeOneFrameUtil.decodeOneCacheFileFrame(
checkNotNull(filePath), listener, /* surface= */ null);
} catch (UnsupportedOperationException e) {
if (e.getMessage() != null
&& e.getMessage().equals(DecodeOneFrameUtil.NO_DECODER_SUPPORT_ERROR_STRING)) {
int trackGroupCount = trackGroupArray.length;
assertThat(trackGroupCount).isEqualTo(2);
for (int i = 0; i < trackGroupCount; i++) {
TrackGroup trackGroup = trackGroupArray.get(i);
if (trackGroup.type == C.TRACK_TYPE_VIDEO) {
assertThat(trackGroup.length).isEqualTo(1);
@Nullable ColorInfo colorInfo = trackGroup.getFormat(0).colorInfo;
@C.ColorTransfer
int actualColorTransfer =
colorInfo == null || colorInfo.colorTransfer == Format.NO_VALUE
? C.COLOR_TRANSFER_SDR
: colorInfo.colorTransfer;
assertThat(actualColorTransfer).isEqualTo(expectedColorTransfer);
return;
} else {
throw e;
}
}
}
private static void assertColorInfoHasTransfer(
@Nullable ColorInfo colorInfo, @C.ColorTransfer int expectedColorTransfer) {
@C.ColorTransfer
int actualColorTransfer = colorInfo == null ? C.COLOR_TRANSFER_SDR : colorInfo.colorTransfer;
assertThat(actualColorTransfer).isEqualTo(expectedColorTransfer);
throw new IllegalStateException("Couldn't find video track");
}
private FileUtil() {}
......
......@@ -21,7 +21,7 @@ import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSE
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_720P_4_SECOND_HDR10_FORMAT;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.recordTestSkipped;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.skipAndLogIfFormatsUnsupported;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static com.google.android.exoplayer2.util.Util.SDK_INT;
import android.content.Context;
......@@ -46,7 +46,6 @@ import org.junit.runner.RunWith;
*/
@RunWith(AndroidJUnit4.class)
public class ForceInterpretHdrVideoAsSdrTest {
public static final String TAG = "ForceInterpretHdrVideoAsSdrTest";
@Test
public void forceInterpretHdrVideoAsSdrTest_hdr10File_transformsOrThrows() throws Exception {
......@@ -85,7 +84,7 @@ public class ForceInterpretHdrVideoAsSdrTest {
.build()
.run(testId, mediaItem);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
}
@Test
......@@ -125,6 +124,6 @@ public class ForceInterpretHdrVideoAsSdrTest {
.build()
.run(testId, mediaItem);
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
}
}
......@@ -17,9 +17,10 @@ package com.google.android.exoplayer2.transformer.mh;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.FORCE_TRANSCODE_VIDEO_EFFECTS;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_720P_4_SECOND_HDR10;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_720P_4_SECOND_HDR10_FORMAT;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.assertFileHasColorTransfer;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
......@@ -28,13 +29,15 @@ import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.transformer.AndroidTestUtil;
import com.google.android.exoplayer2.transformer.EditedMediaItem;
import com.google.android.exoplayer2.transformer.ExportException;
import com.google.android.exoplayer2.transformer.ExportTestResult;
import com.google.android.exoplayer2.transformer.TransformationRequest;
import com.google.android.exoplayer2.transformer.Transformer;
import com.google.android.exoplayer2.transformer.TransformerAndroidTestRunner;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.video.ColorInfo;
import java.util.Objects;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -45,13 +48,23 @@ import org.junit.runner.RunWith;
*/
@RunWith(AndroidJUnit4.class)
public class ToneMapHdrToSdrUsingMediaCodecTest {
public static final String TAG = "ToneMapHdrToSdrUsingMediaCodecTest";
@Test
public void export_toneMapNoRequestedTranscode_hdr10File_toneMapsOrThrows() throws Exception {
String testId = "export_toneMapNoRequestedTranscode_hdr10File_toneMapsOrThrows";
Context context = ApplicationProvider.getApplicationContext();
if (AndroidTestUtil.skipAndLogIfFormatsUnsupported(
context,
testId,
/* inputFormat= */ MP4_ASSET_720P_4_SECOND_HDR10_FORMAT,
/* outputFormat= */ MP4_ASSET_720P_4_SECOND_HDR10_FORMAT
.buildUpon()
.setColorInfo(ColorInfo.SDR_BT709_LIMITED)
.build())) {
return;
}
Transformer transformer =
new Transformer.Builder(context)
.setTransformationRequest(
......@@ -79,13 +92,19 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, mediaItem);
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
assertThat(exception.errorCode)
.isEqualTo(ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED);
if (exception.getCause() != null
&& (Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping HDR is not supported on this device.")
|| Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping requested but not supported by the decoder."))) {
// Expected on devices without a tone-mapping plugin for this codec.
return;
}
throw exception;
}
}
......@@ -94,6 +113,17 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
String testId = "export_toneMapNoRequestedTranscode_hlg10File_toneMapsOrThrows";
Context context = ApplicationProvider.getApplicationContext();
if (AndroidTestUtil.skipAndLogIfFormatsUnsupported(
context,
testId,
/* inputFormat= */ MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT,
/* outputFormat= */ MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT
.buildUpon()
.setColorInfo(ColorInfo.SDR_BT709_LIMITED)
.build())) {
return;
}
Transformer transformer =
new Transformer.Builder(context)
.setTransformationRequest(
......@@ -121,13 +151,19 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, mediaItem);
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
assertThat(exception.errorCode)
.isEqualTo(ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED);
if (exception.getCause() != null
&& (Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping HDR is not supported on this device.")
|| Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping requested but not supported by the decoder."))) {
// Expected on devices without a tone-mapping plugin for this codec.
return;
}
throw exception;
}
}
......@@ -136,6 +172,17 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
String testId = "export_toneMapAndTranscode_hdr10File_toneMapsOrThrows";
Context context = ApplicationProvider.getApplicationContext();
if (AndroidTestUtil.skipAndLogIfFormatsUnsupported(
context,
testId,
/* inputFormat= */ MP4_ASSET_720P_4_SECOND_HDR10_FORMAT,
/* outputFormat= */ MP4_ASSET_720P_4_SECOND_HDR10_FORMAT
.buildUpon()
.setColorInfo(ColorInfo.SDR_BT709_LIMITED)
.build())) {
return;
}
Transformer transformer =
new Transformer.Builder(context)
.setTransformationRequest(
......@@ -165,13 +212,19 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
assertThat(exception.errorCode)
.isEqualTo(ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED);
if (exception.getCause() != null
&& (Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping HDR is not supported on this device.")
|| Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping requested but not supported by the decoder."))) {
// Expected on devices without a tone-mapping plugin for this codec.
return;
}
throw exception;
}
}
......@@ -180,6 +233,17 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
String testId = "export_toneMapAndTranscode_hlg10File_toneMapsOrThrows";
Context context = ApplicationProvider.getApplicationContext();
if (AndroidTestUtil.skipAndLogIfFormatsUnsupported(
context,
testId,
/* inputFormat= */ MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT,
/* outputFormat= */ MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT
.buildUpon()
.setColorInfo(ColorInfo.SDR_BT709_LIMITED)
.build())) {
return;
}
Transformer transformer =
new Transformer.Builder(context)
.setTransformationRequest(
......@@ -209,13 +273,19 @@ public class ToneMapHdrToSdrUsingMediaCodecTest {
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, editedMediaItem);
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.i(TAG, checkNotNull(exception.getCause()).toString());
assertThat(exception).hasCauseThat().isInstanceOf(IllegalArgumentException.class);
assertThat(exception.errorCode)
.isEqualTo(ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED);
if (exception.getCause() != null
&& (Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping HDR is not supported on this device.")
|| Objects.equals(
exception.getCause().getMessage(),
"Tone-mapping requested but not supported by the decoder."))) {
// Expected on devices without a tone-mapping plugin for this codec.
return;
}
throw exception;
}
}
}
......@@ -23,7 +23,7 @@ import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSE
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_DOLBY_VISION_HDR;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.MP4_ASSET_DOLBY_VISION_HDR_FORMAT;
import static com.google.android.exoplayer2.transformer.AndroidTestUtil.recordTestSkipped;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.maybeAssertFileHasColorTransfer;
import static com.google.android.exoplayer2.transformer.mh.FileUtil.assertFileHasColorTransfer;
import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
......@@ -33,14 +33,13 @@ import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.mediacodec.MediaCodecUtil;
import com.google.android.exoplayer2.transformer.AndroidTestUtil;
import com.google.android.exoplayer2.transformer.ExportException;
import com.google.android.exoplayer2.transformer.ExportTestResult;
import com.google.android.exoplayer2.transformer.TransformationRequest;
import com.google.android.exoplayer2.transformer.Transformer;
import com.google.android.exoplayer2.transformer.TransformerAndroidTestRunner;
import com.google.android.exoplayer2.util.GlUtil;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.ColorInfo;
import java.io.IOException;
import org.json.JSONException;
import org.junit.Test;
......@@ -52,13 +51,12 @@ import org.junit.runner.RunWith;
*/
@RunWith(AndroidJUnit4.class)
public class ToneMapHdrToSdrUsingOpenGlTest {
public static final String TAG = "ToneMapHdrToSdrUsingOpenGlTest";
private final Context context = ApplicationProvider.getApplicationContext();
@Test
public void export_toneMap_hlg10File_toneMapsOrThrows() throws Exception {
String testId = "export_glToneMap_hlg10File_toneMapsOrThrows";
public void export_toneMap_hlg10File_toneMaps() throws Exception {
String testId = "export_glToneMap_hlg10File_toneMaps";
if (!deviceSupportsOpenGlToneMapping(
testId, /* inputFormat= */ MP4_ASSET_1080P_5_SECOND_HLG10_FORMAT)) {
return;
......@@ -68,8 +66,8 @@ public class ToneMapHdrToSdrUsingOpenGlTest {
}
@Test
public void export_toneMap_hdr10File_toneMapsOrThrows() throws Exception {
String testId = "export_glToneMap_hdr10File_toneMapsOrThrows";
public void export_toneMap_hdr10File_toneMaps() throws Exception {
String testId = "export_glToneMap_hdr10File_toneMaps";
if (!deviceSupportsOpenGlToneMapping(
testId, /* inputFormat= */ MP4_ASSET_720P_4_SECOND_HDR10_FORMAT)) {
return;
......@@ -79,8 +77,8 @@ public class ToneMapHdrToSdrUsingOpenGlTest {
}
@Test
public void export_toneMap_dolbyVisionFile_toneMapsOrThrows() throws Exception {
String testId = "export_toneMap_dolbyVisionFile_toneMapsOrThrows";
public void export_toneMap_dolbyVisionFile_toneMaps() throws Exception {
String testId = "export_toneMap_dolbyVisionFile_toneMaps";
if (!deviceSupportsOpenGlToneMapping(
testId, /* inputFormat= */ MP4_ASSET_DOLBY_VISION_HDR_FORMAT)) {
return;
......@@ -97,19 +95,11 @@ public class ToneMapHdrToSdrUsingOpenGlTest {
.setHdrMode(TransformationRequest.HDR_MODE_TONE_MAP_HDR_TO_SDR_USING_OPEN_GL)
.build())
.build();
try {
ExportTestResult exportTestResult =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, MediaItem.fromUri(fileUri));
Log.i(TAG, "Tone mapped.");
maybeAssertFileHasColorTransfer(exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
} catch (ExportException exception) {
Log.e(TAG, "Error during export.", exception);
if (exception.errorCode != ExportException.ERROR_CODE_DECODING_FORMAT_UNSUPPORTED) {
throw exception;
}
}
ExportTestResult exportTestResult =
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, MediaItem.fromUri(fileUri));
assertFileHasColorTransfer(context, exportTestResult.filePath, C.COLOR_TRANSFER_SDR);
}
private static boolean deviceSupportsOpenGlToneMapping(String testId, Format inputFormat)
......@@ -129,6 +119,12 @@ public class ToneMapHdrToSdrUsingOpenGlTest {
}
return !AndroidTestUtil.skipAndLogIfFormatsUnsupported(
getApplicationContext(), testId, /* inputFormat= */ inputFormat, /* outputFormat= */ null);
getApplicationContext(),
testId,
inputFormat,
/* outputFormat= */ inputFormat
.buildUpon()
.setColorInfo(ColorInfo.SDR_BT709_LIMITED)
.build());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment