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)
throws IOException, InterruptedException {
return new SsimHelper(context, expectedVideoPath, actualVideoPath, DEFAULT_COMPARISON_INTERVAL)
.startCalculation();
.calculateSsim();
}
private SsimHelper(
@ -108,12 +108,10 @@ public final class SsimHelper {
this.height = new AtomicInteger(Format.NO_VALUE);
}
void ensureDecoderWrappersAreCreated() throws IOException {
if (expectedDecodingWrapper != null || actualDecodingWrapper != null) {
return;
}
// 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.
/** Calculates the SSIM score between the two videos. */
private double calculateSsim() throws InterruptedException, IOException {
// The test thread has no looper, so a handler is created on which the
// ImageReader.OnImageAvailableListener is called.
Handler mainThreadHandler = Util.createHandlerForCurrentOrMainLooper();
ImageReader.OnImageAvailableListener onImageAvailableListener = this::onImageAvailableListener;
expectedDecodingWrapper =
@ -130,13 +128,7 @@ public final class SsimHelper {
onImageAvailableListener,
mainThreadHandler,
comparisonInterval);
}
/** Returns the SSIM score between the two videos. */
private double startCalculation() throws InterruptedException, IOException {
ensureDecoderWrappersAreCreated();
checkStateNotNull(expectedDecodingWrapper);
checkStateNotNull(actualDecodingWrapper);
try {
while (!expectedDecodingWrapper.hasEnded() && !actualDecodingWrapper.hasEnded()) {
if (!expectedDecodingWrapper.runUntilComparisonFrameOrEnded()
@ -161,13 +153,14 @@ public final class SsimHelper {
expectedLumaBuffer.set(EMPTY_BUFFER);
actualLumaBuffer.set(EMPTY_BUFFER);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
} finally {
expectedDecodingWrapper.close();
actualDecodingWrapper.close();
}
if (comparedImagesCount == 0) {
throw new IOException("Input had no frames.");
}
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
* {@link #hasEnded() had ended}.
*/
public boolean runUntilComparisonFrameOrEnded() throws InterruptedException {
public boolean runUntilComparisonFrameOrEnded() {
while (!hasEnded() && !isCurrentFrameComparisonFrame) {
while (dequeueOneFrameFromDecoder()) {}
while (queueOneFrameToEncoder()) {}