package androidx.media3.exoplayer.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Point;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.media3.common.Format;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.ParserException;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import androidx.media3.datasource.BitmapUtil;
import androidx.media3.exoplayer.RendererCapabilities;
import androidx.media3.exoplayer.image.ImageDecoder;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.nio.ByteBuffer;
import s1.f;
import s1.i;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class BitmapFactoryImageDecoder extends i implements ImageDecoder {

    @Nullable
    private final BitmapDecoder bitmapDecoder;

    @Nullable
    private final Context context;
    private final int maxOutputSize;

    @VisibleForTesting
    @Deprecated
    public interface BitmapDecoder {
        Bitmap decode(byte[] bArr, int i10) throws ImageDecoderException;
    }

    public static final class Factory implements ImageDecoder.Factory {

        @Nullable
        private final BitmapDecoder bitmapDecoder;

        @Nullable
        private final Context context;
        private int maxOutputSize;

        @Deprecated
        public Factory() {
            this(null, null);
        }

        @CanIgnoreReturnValue
        public Factory setMaxOutputSize(int i10) {
            Assertions.checkArgument(i10 == -1 || i10 > 0);
            this.maxOutputSize = i10;
            return this;
        }

        @Override // androidx.media3.exoplayer.image.ImageDecoder.Factory
        public int supportsFormat(Format format) {
            String str = format.sampleMimeType;
            return (str == null || !MimeTypes.isImage(str)) ? RendererCapabilities.create(0) : Util.isBitmapFactorySupportedMimeType(format.sampleMimeType) ? RendererCapabilities.create(4) : RendererCapabilities.create(1);
        }

        public Factory(Context context) {
            this(context, null);
        }

        @Override // androidx.media3.exoplayer.image.ImageDecoder.Factory
        public BitmapFactoryImageDecoder createImageDecoder() {
            return new BitmapFactoryImageDecoder(this.context, this.bitmapDecoder, this.maxOutputSize);
        }

        @Deprecated
        public Factory(BitmapDecoder bitmapDecoder) {
            this(null, bitmapDecoder);
        }

        private Factory(@Nullable Context context, @Nullable BitmapDecoder bitmapDecoder) {
            this.context = context;
            this.bitmapDecoder = bitmapDecoder;
            this.maxOutputSize = -1;
        }
    }

    @Override // s1.i
    public f createInputBuffer() {
        return new f(1);
    }

    @Override // s1.i, s1.e, androidx.media3.exoplayer.image.ImageDecoder
    @Nullable
    public /* bridge */ /* synthetic */ ImageOutputBuffer dequeueOutputBuffer() throws ImageDecoderException {
        return (ImageOutputBuffer) super.dequeueOutputBuffer();
    }

    @Override // s1.e, androidx.media3.exoplayer.image.ImageDecoder
    public String getName() {
        return "BitmapFactoryImageDecoder";
    }

    private BitmapFactoryImageDecoder(@Nullable Context context, @Nullable BitmapDecoder bitmapDecoder, int i10) {
        super(new f[1], new ImageOutputBuffer[1]);
        this.context = context;
        this.bitmapDecoder = bitmapDecoder;
        this.maxOutputSize = i10;
    }

    @Override // s1.i
    public ImageOutputBuffer createOutputBuffer() {
        return new ImageOutputBuffer() { // from class: androidx.media3.exoplayer.image.BitmapFactoryImageDecoder.1
            @Override // s1.g
            public void release() {
                BitmapFactoryImageDecoder.this.releaseOutputBuffer(this);
            }
        };
    }

    @Override // s1.i
    public ImageDecoderException createUnexpectedDecodeException(Throwable th) {
        return new ImageDecoderException("Unexpected decode error", th);
    }

    @Override // s1.i
    @Nullable
    public ImageDecoderException decode(f fVar, ImageOutputBuffer imageOutputBuffer, boolean z10) {
        ByteBuffer byteBuffer = (ByteBuffer) Assertions.checkNotNull(fVar.data);
        Assertions.checkState(byteBuffer.hasArray());
        Assertions.checkArgument(byteBuffer.arrayOffset() == 0);
        BitmapDecoder bitmapDecoder = this.bitmapDecoder;
        if (bitmapDecoder != null) {
            try {
                imageOutputBuffer.bitmap = bitmapDecoder.decode(byteBuffer.array(), byteBuffer.remaining());
            } catch (ImageDecoderException e10) {
                return e10;
            }
        } else {
            try {
                int iMax = this.maxOutputSize;
                if (iMax == -1) {
                    Context context = this.context;
                    if (context != null) {
                        Point currentDisplayModeSize = Util.getCurrentDisplayModeSize(context);
                        int i10 = currentDisplayModeSize.x;
                        int i11 = currentDisplayModeSize.y;
                        Format format = fVar.format;
                        if (format != null) {
                            int i12 = format.tileCountHorizontal;
                            if (i12 != -1) {
                                i10 *= i12;
                            }
                            int i13 = format.tileCountVertical;
                            if (i13 != -1) {
                                i11 *= i13;
                            }
                        }
                        iMax = (Math.max(i10, i11) * 2) - 1;
                    } else {
                        iMax = 4096;
                    }
                }
                imageOutputBuffer.bitmap = BitmapUtil.decode(byteBuffer.array(), byteBuffer.remaining(), null, iMax);
            } catch (ParserException e11) {
                return new ImageDecoderException("Could not decode image data with BitmapFactory.", e11);
            } catch (IOException e12) {
                return new ImageDecoderException(e12);
            }
        }
        imageOutputBuffer.timeUs = fVar.timeUs;
        return null;
    }
}
