Nit cleanup of SsimHelper.

PiperOrigin-RevId: 433174767
This commit is contained in:
samrobinson 2022-03-08 12:14:54 +00:00 committed by Ian Baker
parent 20282151b5
commit 0770d55c1a

View File

@ -93,7 +93,7 @@ public final class SsimHelper {
public static double calculate(Context context, String expectedVideoPath, String actualVideoPath) public static double calculate(Context context, String expectedVideoPath, String actualVideoPath)
throws IOException, InterruptedException { throws IOException, InterruptedException {
return new SsimHelper(context, expectedVideoPath, actualVideoPath, DEFAULT_COMPARISON_INTERVAL) return new SsimHelper(context, expectedVideoPath, actualVideoPath, DEFAULT_COMPARISON_INTERVAL)
.startCalculation(); .calculateSsim();
} }
private SsimHelper( private SsimHelper(
@ -108,12 +108,10 @@ public final class SsimHelper {
this.height = new AtomicInteger(Format.NO_VALUE); this.height = new AtomicInteger(Format.NO_VALUE);
} }
void ensureDecoderWrappersAreCreated() throws IOException { /** Calculates the SSIM score between the two videos. */
if (expectedDecodingWrapper != null || actualDecodingWrapper != null) { private double calculateSsim() throws InterruptedException, IOException {
return; // The test thread has no looper, so a handler is created on which the
} // ImageReader.OnImageAvailableListener is called.
// This constructor is executed on the test thread, which does not have a looper attached.
// Creates a handler on which the ImageReader.onImageAvailableListener is called.
Handler mainThreadHandler = Util.createHandlerForCurrentOrMainLooper(); Handler mainThreadHandler = Util.createHandlerForCurrentOrMainLooper();
ImageReader.OnImageAvailableListener onImageAvailableListener = this::onImageAvailableListener; ImageReader.OnImageAvailableListener onImageAvailableListener = this::onImageAvailableListener;
expectedDecodingWrapper = expectedDecodingWrapper =
@ -130,13 +128,7 @@ public final class SsimHelper {
onImageAvailableListener, onImageAvailableListener,
mainThreadHandler, mainThreadHandler,
comparisonInterval); comparisonInterval);
}
/** Returns the SSIM score between the two videos. */
private double startCalculation() throws InterruptedException, IOException {
ensureDecoderWrappersAreCreated();
checkStateNotNull(expectedDecodingWrapper);
checkStateNotNull(actualDecodingWrapper);
try { try {
while (!expectedDecodingWrapper.hasEnded() && !actualDecodingWrapper.hasEnded()) { while (!expectedDecodingWrapper.hasEnded() && !actualDecodingWrapper.hasEnded()) {
if (!expectedDecodingWrapper.runUntilComparisonFrameOrEnded() if (!expectedDecodingWrapper.runUntilComparisonFrameOrEnded()
@ -161,13 +153,14 @@ public final class SsimHelper {
expectedLumaBuffer.set(EMPTY_BUFFER); expectedLumaBuffer.set(EMPTY_BUFFER);
actualLumaBuffer.set(EMPTY_BUFFER); actualLumaBuffer.set(EMPTY_BUFFER);
} }
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
} finally { } finally {
expectedDecodingWrapper.close(); expectedDecodingWrapper.close();
actualDecodingWrapper.close(); actualDecodingWrapper.close();
} }
if (comparedImagesCount == 0) {
throw new IOException("Input had no frames.");
}
return accumulatedSsim / comparedImagesCount; return accumulatedSsim / comparedImagesCount;
} }
@ -303,7 +296,7 @@ public final class SsimHelper {
* @return {@code true} when a comparison frame is encountered, or {@code false} if decoding * @return {@code true} when a comparison frame is encountered, or {@code false} if decoding
* {@link #hasEnded() had ended}. * {@link #hasEnded() had ended}.
*/ */
public boolean runUntilComparisonFrameOrEnded() throws InterruptedException { public boolean runUntilComparisonFrameOrEnded() {
while (!hasEnded() && !isCurrentFrameComparisonFrame) { while (!hasEnded() && !isCurrentFrameComparisonFrame) {
while (dequeueOneFrameFromDecoder()) {} while (dequeueOneFrameFromDecoder()) {}
while (queueOneFrameToEncoder()) {} while (queueOneFrameToEncoder()) {}