package com.bumptech.glide.load.resource.bitmap;

import a5.k;
import a5.m;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.window.core.h;
import c5.e;
import com.bumptech.glide.load.engine.e0;
import com.google.android.gms.measurement.internal.u1;
import j5.d;
import j5.f0;
import j5.h0;
import j5.l;
import java.io.IOException;

/* JADX INFO: loaded from: classes.dex */
public final class b implements m {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final k f5820d = k.disk("com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.TargetFrame", -1L, new f0(0));

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final k f5821e = k.disk("com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.FrameOption", 2, new f0(1));

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final h f5822f = new h(14);

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final h0 f5823a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final e f5824b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final h f5825c = f5822f;

    public b(e eVar, h0 h0Var) {
        this.f5824b = eVar;
        this.f5823a = h0Var;
    }

    @Nullable
    private static Bitmap decodeFrame(MediaMetadataRetriever mediaMetadataRetriever, long j10, int i10, int i11, int i12, l lVar) {
        Bitmap bitmapDecodeScaledFrame = (Build.VERSION.SDK_INT < 27 || i11 == Integer.MIN_VALUE || i12 == Integer.MIN_VALUE || lVar == l.f28662d) ? null : decodeScaledFrame(mediaMetadataRetriever, j10, i10, i11, i12, lVar);
        if (bitmapDecodeScaledFrame == null) {
            bitmapDecodeScaledFrame = mediaMetadataRetriever.getFrameAtTime(j10, i10);
        }
        if (bitmapDecodeScaledFrame != null) {
            return bitmapDecodeScaledFrame;
        }
        throw new RuntimeException() { // from class: com.bumptech.glide.load.resource.bitmap.VideoDecoder$VideoDecoderException
            private static final long serialVersionUID = -2556382523004027815L;
        };
    }

    @Nullable
    @TargetApi(27)
    private static Bitmap decodeScaledFrame(MediaMetadataRetriever mediaMetadataRetriever, long j10, int i10, int i11, int i12, l lVar) {
        try {
            int i13 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(18));
            int i14 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(19));
            int i15 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(24));
            if (i15 == 90 || i15 == 270) {
                i14 = i13;
                i13 = i14;
            }
            float fB = lVar.b(i13, i14, i11, i12);
            return mediaMetadataRetriever.getScaledFrameAtTime(j10, i10, Math.round(i13 * fB), Math.round(fB * i14));
        } catch (Throwable unused) {
            return null;
        }
    }

    @Override // a5.m
    public final boolean a(Object obj, a5.l lVar) {
        return true;
    }

    @Override // a5.m
    public final e0 decode(Object obj, int i10, int i11, a5.l lVar) throws IOException {
        long jLongValue = ((Long) lVar.get(f5820d)).longValue();
        if (jLongValue < 0 && jLongValue != -1) {
            throw new IllegalArgumentException(u1.i("Requested frame must be non-negative, or DEFAULT_FRAME, given: ", jLongValue));
        }
        Integer num = (Integer) lVar.get(f5821e);
        if (num == null) {
            num = 2;
        }
        l lVar2 = (l) lVar.get(l.f28664f);
        if (lVar2 == null) {
            lVar2 = l.f28663e;
        }
        l lVar3 = lVar2;
        this.f5825c.getClass();
        MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
        try {
            this.f5823a.j(mediaMetadataRetriever, obj);
            return d.obtain(decodeFrame(mediaMetadataRetriever, jLongValue, num.intValue(), i10, i11, lVar3), this.f5824b);
        } finally {
            if (Build.VERSION.SDK_INT >= 29) {
                mediaMetadataRetriever.release();
            } else {
                mediaMetadataRetriever.release();
            }
        }
    }
}
