package androidx.media3.exoplayer.video.spherical;

import android.opengl.Matrix;
import androidx.media3.common.util.GlUtil;
import androidx.media3.common.util.TimedValueQueue;

/* JADX INFO: loaded from: classes.dex */
final class FrameRotationQueue {
    private boolean recenterMatrixComputed;
    private final float[] recenterMatrix = new float[16];
    private final float[] rotationMatrix = new float[16];
    private final TimedValueQueue<float[]> rotations = new TimedValueQueue<>();

    public static void computeRecenterMatrix(float[] fArr, float[] fArr2) {
        GlUtil.setToIdentity(fArr);
        float f6 = fArr2[10];
        float f7 = fArr2[8];
        float fSqrt = (float) Math.sqrt((f7 * f7) + (f6 * f6));
        float f8 = fArr2[10];
        fArr[0] = f8 / fSqrt;
        float f9 = fArr2[8];
        fArr[2] = f9 / fSqrt;
        fArr[8] = (-f9) / fSqrt;
        fArr[10] = f8 / fSqrt;
    }

    private static void getRotationMatrixFromAngleAxis(float[] fArr, float[] fArr2) {
        float f6 = fArr2[0];
        float f7 = -fArr2[1];
        float f8 = -fArr2[2];
        float length = Matrix.length(f6, f7, f8);
        if (length != 0.0f) {
            Matrix.setRotateM(fArr, 0, (float) Math.toDegrees(length), f6 / length, f7 / length, f8 / length);
        } else {
            GlUtil.setToIdentity(fArr);
        }
    }

    public boolean pollRotationMatrix(float[] fArr, long j4) {
        float[] fArrPollFloor = this.rotations.pollFloor(j4);
        if (fArrPollFloor == null) {
            return false;
        }
        getRotationMatrixFromAngleAxis(this.rotationMatrix, fArrPollFloor);
        if (!this.recenterMatrixComputed) {
            computeRecenterMatrix(this.recenterMatrix, this.rotationMatrix);
            this.recenterMatrixComputed = true;
        }
        Matrix.multiplyMM(fArr, 0, this.recenterMatrix, 0, this.rotationMatrix, 0);
        return true;
    }

    public void reset() {
        this.rotations.clear();
        this.recenterMatrixComputed = false;
    }

    public void setRotation(long j4, float[] fArr) {
        this.rotations.add(j4, fArr);
    }
}
