package androidx.media3.common.audio;

import Z0.o;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class ChannelMixingMatrix {
    private final float[] coefficients;
    private final int inputChannelCount;
    private final boolean isDiagonal;
    private final boolean isIdentity;
    private final boolean isZero;
    private final int outputChannelCount;

    public ChannelMixingMatrix(int i6, int i7, float[] fArr) {
        boolean z6 = false;
        Assertions.checkArgument(i6 > 0, "Input channel count must be positive.");
        Assertions.checkArgument(i7 > 0, "Output channel count must be positive.");
        Assertions.checkArgument(fArr.length == i6 * i7, "Coefficient array length is invalid.");
        this.inputChannelCount = i6;
        this.outputChannelCount = i7;
        this.coefficients = checkCoefficientsValid(fArr);
        int i8 = 0;
        boolean z7 = true;
        boolean z8 = true;
        boolean z9 = true;
        while (i8 < i6) {
            int i9 = 0;
            while (i9 < i7) {
                float mixingCoefficient = getMixingCoefficient(i8, i9);
                boolean z10 = i8 == i9;
                if (mixingCoefficient != 1.0f && z10) {
                    z9 = false;
                }
                if (mixingCoefficient != 0.0f) {
                    z7 = false;
                    if (!z10) {
                        z8 = false;
                    }
                }
                i9++;
            }
            i8++;
        }
        this.isZero = z7;
        boolean z11 = isSquare() && z8;
        this.isDiagonal = z11;
        if (z11 && z9) {
            z6 = true;
        }
        this.isIdentity = z6;
    }

    private static float[] checkCoefficientsValid(float[] fArr) {
        for (int i6 = 0; i6 < fArr.length; i6++) {
            if (fArr[i6] < 0.0f) {
                throw new IllegalArgumentException(o.j(i6, "Coefficient at index ", " is negative."));
            }
        }
        return fArr;
    }

    public static ChannelMixingMatrix create(int i6, int i7) {
        return new ChannelMixingMatrix(i6, i7, createMixingCoefficients(i6, i7));
    }

    private static float[] createMixingCoefficients(int i6, int i7) {
        if (i6 == i7) {
            return initializeIdentityMatrix(i7);
        }
        if (i6 == 1 && i7 == 2) {
            return new float[]{1.0f, 1.0f};
        }
        if (i6 == 2 && i7 == 1) {
            return new float[]{0.5f, 0.5f};
        }
        throw new UnsupportedOperationException("Default channel mixing coefficients for " + i6 + "->" + i7 + " are not yet implemented.");
    }

    private static float[] initializeIdentityMatrix(int i6) {
        float[] fArr = new float[i6 * i6];
        for (int i7 = 0; i7 < i6; i7++) {
            fArr[(i6 * i7) + i7] = 1.0f;
        }
        return fArr;
    }

    public int getInputChannelCount() {
        return this.inputChannelCount;
    }

    public float getMixingCoefficient(int i6, int i7) {
        return this.coefficients[(i6 * this.outputChannelCount) + i7];
    }

    public int getOutputChannelCount() {
        return this.outputChannelCount;
    }

    public boolean isDiagonal() {
        return this.isDiagonal;
    }

    public boolean isIdentity() {
        return this.isIdentity;
    }

    public boolean isSquare() {
        return this.inputChannelCount == this.outputChannelCount;
    }

    public boolean isZero() {
        return this.isZero;
    }

    public ChannelMixingMatrix scaleBy(float f6) {
        float[] fArr = new float[this.coefficients.length];
        int i6 = 0;
        while (true) {
            float[] fArr2 = this.coefficients;
            if (i6 >= fArr2.length) {
                return new ChannelMixingMatrix(this.inputChannelCount, this.outputChannelCount, fArr);
            }
            fArr[i6] = fArr2[i6] * f6;
            i6++;
        }
    }
}
