package wh;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import com.bumptech.glide.load.ImageHeaderParser;
import ei.l;
import hh.a;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Queue;

/* loaded from: classes4.dex */
public class a implements jh.j {

    /* renamed from: f, reason: collision with root package name */
    private static final C0985a f49425f = new C0985a();

    /* renamed from: g, reason: collision with root package name */
    private static final b f49426g = new b();

    /* renamed from: a, reason: collision with root package name */
    private final Context f49427a;

    /* renamed from: b, reason: collision with root package name */
    private final List f49428b;

    /* renamed from: c, reason: collision with root package name */
    private final b f49429c;

    /* renamed from: d, reason: collision with root package name */
    private final C0985a f49430d;

    /* renamed from: e, reason: collision with root package name */
    private final wh.b f49431e;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: wh.a$a, reason: collision with other inner class name */
    /* loaded from: classes4.dex */
    public static class C0985a {
        C0985a() {
        }

        hh.a a(a.InterfaceC0463a interfaceC0463a, hh.c cVar, ByteBuffer byteBuffer, int i10) {
            return new hh.e(interfaceC0463a, cVar, byteBuffer, i10);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static class b {

        /* renamed from: a, reason: collision with root package name */
        private final Queue f49432a = l.e(0);

        b() {
        }

        synchronized hh.d a(ByteBuffer byteBuffer) {
            hh.d dVar;
            dVar = (hh.d) this.f49432a.poll();
            if (dVar == null) {
                dVar = new hh.d();
            }
            return dVar.p(byteBuffer);
        }

        synchronized void b(hh.d dVar) {
            dVar.a();
            this.f49432a.offer(dVar);
        }
    }

    public a(Context context, List list, mh.d dVar, mh.b bVar) {
        this(context, list, dVar, bVar, f49426g, f49425f);
    }

    private e c(ByteBuffer byteBuffer, int i10, int i11, hh.d dVar, jh.h hVar) {
        long b10 = ei.g.b();
        try {
            hh.c c10 = dVar.c();
            if (c10.b() > 0 && c10.c() == 0) {
                Bitmap.Config config = hVar.c(i.f49472a) == jh.b.PREFER_RGB_565 ? Bitmap.Config.RGB_565 : Bitmap.Config.ARGB_8888;
                hh.a a10 = this.f49430d.a(this.f49431e, c10, byteBuffer, e(c10, i10, i11));
                a10.e(config);
                a10.b();
                Bitmap a11 = a10.a();
                if (a11 == null) {
                    return null;
                }
                e eVar = new e(new c(this.f49427a, a10, rh.c.c(), i10, i11, a11));
                if (Log.isLoggable("BufferGifDecoder", 2)) {
                    Log.v("BufferGifDecoder", "Decoded GIF from stream in " + ei.g.a(b10));
                }
                return eVar;
            }
            if (Log.isLoggable("BufferGifDecoder", 2)) {
                Log.v("BufferGifDecoder", "Decoded GIF from stream in " + ei.g.a(b10));
            }
            return null;
        } finally {
            if (Log.isLoggable("BufferGifDecoder", 2)) {
                Log.v("BufferGifDecoder", "Decoded GIF from stream in " + ei.g.a(b10));
            }
        }
    }

    private static int e(hh.c cVar, int i10, int i11) {
        int min = Math.min(cVar.a() / i11, cVar.d() / i10);
        int max = Math.max(1, min == 0 ? 0 : Integer.highestOneBit(min));
        if (Log.isLoggable("BufferGifDecoder", 2) && max > 1) {
            Log.v("BufferGifDecoder", "Downsampling GIF, sampleSize: " + max + ", target dimens: [" + i10 + "x" + i11 + "], actual dimens: [" + cVar.d() + "x" + cVar.a() + "]");
        }
        return max;
    }

    @Override // jh.j
    /* renamed from: d, reason: merged with bridge method [inline-methods] */
    public e a(ByteBuffer byteBuffer, int i10, int i11, jh.h hVar) {
        hh.d a10 = this.f49429c.a(byteBuffer);
        try {
            return c(byteBuffer, i10, i11, a10, hVar);
        } finally {
            this.f49429c.b(a10);
        }
    }

    @Override // jh.j
    /* renamed from: f, reason: merged with bridge method [inline-methods] */
    public boolean b(ByteBuffer byteBuffer, jh.h hVar) {
        return !((Boolean) hVar.c(i.f49473b)).booleanValue() && com.bumptech.glide.load.a.g(this.f49428b, byteBuffer) == ImageHeaderParser.ImageType.GIF;
    }

    a(Context context, List list, mh.d dVar, mh.b bVar, b bVar2, C0985a c0985a) {
        this.f49427a = context.getApplicationContext();
        this.f49428b = list;
        this.f49430d = c0985a;
        this.f49431e = new wh.b(dVar, bVar);
        this.f49429c = bVar2;
    }
}
