package com.bytedance.ies.cutsame.util;

import X.2Pr;
import X.7y5;
import X.C07835Rd;
import X.C537977x;
import X.C538177z;
import X.LWj;
import X.LeY;
import X.LsC;
import X.M12;
import X.Qx3;
import android.content.Context;
import android.net.Uri;
import android.util.LruCache;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.util.Arrays;
import java.util.Locale;
import kotlin.h.b.n;
import kotlin.o.y;

public final class MediaUtil {
    public static final MediaUtil LIZ = new MediaUtil();
    public static final LruCache<String, Object> LIZIZ = new LruCache<>(100);
    public static final LruCache<String, C07835Rd> LIZJ = new LruCache<>(100);
    public static final byte[] LIZLLL = {82, 73, 70, 70};
    public static final byte[] LJ = {87, 69, 66, 80};
    public static final byte[] LJFF = {104, 101, 105, 99};
    public static final byte[] LJI;
    public static final byte[] LJII;
    public static final byte[] LJIIIIZZ;
    public static final byte[] LJIIIZ;
    public static final byte[] LJIIJ;
    public static final byte[][] LJIIJJI;

    static {
        Covode.recordClassIndex(29122);
        byte[] bArr = {-1, -40};
        LJI = bArr;
        byte[] bArr2 = {-119, 80, 78, 71, 13, 10, 26, 10};
        LJII = bArr2;
        byte[] bArr3 = {71, 73, 70, 56, 55, 97};
        LJIIIIZZ = bArr3;
        byte[] bArr4 = {71, 73, 70, 56, 57, 97};
        LJIIIZ = bArr4;
        byte[] bArr5 = {66, 77};
        LJIIJ = bArr5;
        LJIIJJI = new byte[][]{bArr, bArr2, bArr3, bArr4, bArr5};
    }

    public final Size getVideoSize(Context context, String str) {
        M12.LIZ(str);
        C07835Rd LIZIZ2 = LIZIZ(context, str);
        int i = LIZIZ2.LIZJ;
        int i2 = LIZIZ2.LIZLLL;
        if (LIZIZ2.LJ == 90 || LIZIZ2.LJ == 270) {
            i = LIZIZ2.LIZLLL;
            i2 = LIZIZ2.LIZJ;
        }
        return new Size(i, i2);
    }

    public final boolean LIZ(Context context, String str) {
        MethodCollector.i(3262);
        M12.LIZ(str);
        if (!C538177z.LIZ.LIZ(context, str)) {
            MethodCollector.o(3262);
            return false;
        }
        try {
            byte[] bArr = new byte[12];
            if (C538177z.LIZ.LIZ(str)) {
                C538177z r5 = C538177z.LIZ;
                Uri parse = Uri.parse(str);
                M12.LIZ(parse);
                r5.LIZ(context, parse, (LsC<? super FileDescriptor, 2Pr>) new C537977x(bArr));
            } else {
                FileInputStream fileInputStream = new FileInputStream(str);
                try {
                    fileInputStream.read(bArr);
                    7y5.LIZ(fileInputStream, (Throwable) null);
                } catch (Throwable th) {
                    7y5.LIZ(fileInputStream, th);
                    MethodCollector.o(3262);
                    throw th;
                }
            }
            if (!Arrays.equals(LIZLLL, LeY.LIZ(bArr, Qx3.LIZIZ(0, 4))) || !Arrays.equals(LJ, LeY.LIZ(bArr, Qx3.LIZIZ(8, 12)))) {
                byte[][] bArr2 = LJIIJJI;
                int length = ((Object[]) bArr2).length;
                int i = 0;
                while (i < length) {
                    byte[] bArr3 = bArr2[i];
                    i++;
                    if (Arrays.equals(bArr3, LeY.LIZ(bArr, Qx3.LIZIZ(0, bArr3.length)))) {
                        MethodCollector.o(3262);
                        return true;
                    }
                }
                Locale locale = Locale.getDefault();
                M12.LIZ(locale);
                String upperCase = str.toUpperCase(locale);
                n.LIZIZ(upperCase, "");
                if (y.LIZJ(upperCase, ".HEIC", false) || Arrays.equals(LJFF, LeY.LIZ(bArr, Qx3.LIZIZ(8, 12)))) {
                    MethodCollector.o(3262);
                    return true;
                }
                LeY.LIZ(bArr, " ", LWj.LIZ);
                MethodCollector.o(3262);
                return false;
            }
            MethodCollector.o(3262);
            return true;
        } catch (Exception unused) {
        }
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(3:56|57|(2:58|59)) */
    /* JADX WARNING: Code restructure failed: missing block: B:57:?, code lost:
        r7 = new X.C07835Rd(r1, 0, 0, 0, 0, 0, 0, (java.lang.String) null, 992);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:59:?, code lost:
        r0.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:60:0x00ff, code lost:
        r0 = e;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:63:0x0104, code lost:
        r1 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:65:?, code lost:
        r0.release();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:66:0x0109, code lost:
        r0 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:68:?, code lost:
        X.0HH.LIZ(r0);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:69:0x010d, code lost:
        com.bytedance.frameworks.apm.trace.MethodCollector.o(3324);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:70:0x0110, code lost:
        throw r1;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:56:0x00ec */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final synchronized X.C07835Rd LIZIZ(android.content.Context r19, java.lang.String r20) {
        /*
            r18 = this;
            r2 = r18
            monitor-enter(r2)
            r6 = 3324(0xcfc, float:4.658E-42)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r6)     // Catch:{ all -> 0x015b }
            r1 = r20
            X.M12.LIZ(r1)     // Catch:{ all -> 0x015b }
            android.util.LruCache<java.lang.String, X.5Rd> r0 = LIZJ     // Catch:{ all -> 0x015b }
            java.lang.Object r7 = r0.get(r1)     // Catch:{ all -> 0x015b }
            X.5Rd r7 = (X.C07835Rd) r7     // Catch:{ all -> 0x015b }
            if (r7 != 0) goto L_0x013b
            r3 = r19
            boolean r0 = r2.LIZ(r3, r1)     // Catch:{ all -> 0x015b }
            r11 = 0
            if (r0 == 0) goto L_0x0043
            android.graphics.BitmapFactory$Options r5 = new android.graphics.BitmapFactory$Options     // Catch:{ all -> 0x015b }
            r5.<init>()     // Catch:{ all -> 0x015b }
            r0 = 1
            r5.inJustDecodeBounds = r0     // Catch:{ all -> 0x015b }
            X.77z r0 = X.C538177z.LIZ     // Catch:{ all -> 0x015b }
            r0.LIZ((android.content.Context) r3, (java.lang.String) r1, (android.graphics.BitmapFactory.Options) r5)     // Catch:{ all -> 0x015b }
            X.M12.LIZ(r1)     // Catch:{ all -> 0x015b }
            r0 = r1
            boolean r0 = android.text.TextUtils.isEmpty(r0)     // Catch:{ all -> 0x015b }
            if (r0 == 0) goto L_0x0038
            goto L_0x0040
        L_0x0038:
            X.77z r0 = X.C538177z.LIZ     // Catch:{ all -> 0x015b }
            boolean r0 = r0.LIZ((android.content.Context) r3, (java.lang.String) r1)     // Catch:{ all -> 0x015b }
            if (r0 != 0) goto L_0x0111
        L_0x0040:
            r11 = 0
            goto L_0x011a
        L_0x0043:
            com.ss.android.vesdk.VEUtils$VEVideoFileInfo r4 = com.ss.android.vesdk.VEUtils.getVideoFileInfo(r1)     // Catch:{ all -> 0x015b }
            if (r4 == 0) goto L_0x0078
            int r0 = r4.width     // Catch:{ all -> 0x015b }
            if (r0 <= 0) goto L_0x0078
            int r0 = r4.height     // Catch:{ all -> 0x015b }
            if (r0 <= 0) goto L_0x0078
            int r0 = r4.maxDuration     // Catch:{ all -> 0x015b }
            if (r0 <= 0) goto L_0x0078
            X.5Rd r7 = new X.5Rd     // Catch:{ all -> 0x015b }
            int r9 = r4.width     // Catch:{ all -> 0x015b }
            int r10 = r4.height     // Catch:{ all -> 0x015b }
            int r11 = r4.rotation     // Catch:{ all -> 0x015b }
            int r12 = r4.maxDuration     // Catch:{ all -> 0x015b }
            int r13 = r4.bitrate     // Catch:{ all -> 0x015b }
            int r14 = r4.fps     // Catch:{ all -> 0x015b }
            int r15 = r4.codec     // Catch:{ all -> 0x015b }
            int r3 = r4.duration     // Catch:{ all -> 0x015b }
            int r0 = r4.codec     // Catch:{ all -> 0x015b }
            java.lang.String r17 = com.ss.android.vesdk.VEUtils.getVideoEncodeTypeByID(r0)     // Catch:{ all -> 0x015b }
            X.M12.LIZ(r17)     // Catch:{ all -> 0x015b }
            r8 = r1
            r16 = r3
            r7.<init>(r8, r9, r10, r11, r12, r13, r14, r15, r16, r17)     // Catch:{ all -> 0x015b }
            goto L_0x0136
        L_0x0078:
            X.77z r0 = X.C538177z.LIZ     // Catch:{ all -> 0x015b }
            android.media.MediaMetadataRetriever r0 = r0.LIZJ(r3, r1)     // Catch:{ all -> 0x015b }
            r3 = 18
            java.lang.String r3 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r3 != 0) goto L_0x0088
            r9 = 0
            goto L_0x008c
        L_0x0088:
            int r9 = java.lang.Integer.parseInt(r3)     // Catch:{ Exception -> 0x00ec }
        L_0x008c:
            r3 = 19
            java.lang.String r3 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r3 != 0) goto L_0x0096
            r10 = 0
            goto L_0x009a
        L_0x0096:
            int r10 = java.lang.Integer.parseInt(r3)     // Catch:{ Exception -> 0x00ec }
        L_0x009a:
            r3 = 24
            java.lang.String r3 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r3 != 0) goto L_0x00a4
            r11 = 0
            goto L_0x00a8
        L_0x00a4:
            int r11 = java.lang.Integer.parseInt(r3)     // Catch:{ Exception -> 0x00ec }
        L_0x00a8:
            r3 = 9
            java.lang.String r3 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r3 != 0) goto L_0x00b2
            r12 = 0
            goto L_0x00b6
        L_0x00b2:
            int r12 = java.lang.Integer.parseInt(r3)     // Catch:{ Exception -> 0x00ec }
        L_0x00b6:
            r3 = 20
            java.lang.String r3 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r3 != 0) goto L_0x00c0
            r13 = 0
            goto L_0x00c4
        L_0x00c0:
            int r13 = java.lang.Integer.parseInt(r3)     // Catch:{ Exception -> 0x00ec }
        L_0x00c4:
            r3 = 25
            java.lang.String r3 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r3 != 0) goto L_0x00ce
            r14 = 0
            goto L_0x00d2
        L_0x00ce:
            int r14 = java.lang.Integer.parseInt(r3)     // Catch:{ Exception -> 0x00ec }
        L_0x00d2:
            r3 = 12
            java.lang.String r15 = r0.extractMetadata(r3)     // Catch:{ Exception -> 0x00ec }
            if (r15 != 0) goto L_0x00dc
            java.lang.String r15 = "unknown"
        L_0x00dc:
            X.5Rd r7 = new X.5Rd     // Catch:{ Exception -> 0x00ec }
            r3 = 384(0x180, float:5.38E-43)
            r8 = r1
            r16 = 384(0x180, float:5.38E-43)
            r7.<init>(r8, r9, r10, r11, r12, r13, r14, r15, r16)     // Catch:{ Exception -> 0x00ec }
            r0.release()     // Catch:{ Exception -> 0x00ea }
            goto L_0x0136
        L_0x00ea:
            r0 = move-exception
            goto L_0x0100
        L_0x00ec:
            X.5Rd r7 = new X.5Rd     // Catch:{ all -> 0x0104 }
            r9 = 0
            r10 = 0
            r11 = 0
            r12 = 0
            r13 = 0
            r14 = 0
            r15 = 0
            r16 = 992(0x3e0, float:1.39E-42)
            r8 = r1
            r7.<init>(r8, r9, r10, r11, r12, r13, r14, r15, r16)     // Catch:{ all -> 0x0104 }
            r0.release()     // Catch:{ Exception -> 0x00ff }
            goto L_0x0136
        L_0x00ff:
            r0 = move-exception
        L_0x0100:
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x015b }
            goto L_0x0136
        L_0x0104:
            r1 = move-exception
            r0.release()     // Catch:{ Exception -> 0x0109 }
            goto L_0x010d
        L_0x0109:
            r0 = move-exception
            X.0HH.LIZ(r0)     // Catch:{ all -> 0x015b }
        L_0x010d:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)     // Catch:{ all -> 0x015b }
            throw r1     // Catch:{ all -> 0x015b }
        L_0x0111:
            X.77z r0 = X.C538177z.LIZ     // Catch:{ all -> 0x015b }
            android.media.ExifInterface r4 = r0.LIZIZ(r3, r1)     // Catch:{ all -> 0x015b }
            r3 = -1
            if (r4 != 0) goto L_0x013f
        L_0x011a:
            com.bytedance.ies.cutsame.util.Size r4 = new com.bytedance.ies.cutsame.util.Size     // Catch:{ all -> 0x015b }
            int r3 = r5.outWidth     // Catch:{ all -> 0x015b }
            int r0 = r5.outHeight     // Catch:{ all -> 0x015b }
            r4.<init>(r3, r0)     // Catch:{ all -> 0x015b }
            X.5Rd r7 = new X.5Rd     // Catch:{ all -> 0x015b }
            int r9 = r4.width     // Catch:{ all -> 0x015b }
            int r10 = r4.height     // Catch:{ all -> 0x015b }
            r12 = 60000(0xea60, float:8.4078E-41)
            r13 = 0
            r14 = 0
            java.lang.String r15 = "image"
            r16 = 480(0x1e0, float:6.73E-43)
            r8 = r1
            r7.<init>(r8, r9, r10, r11, r12, r13, r14, r15, r16)     // Catch:{ all -> 0x015b }
        L_0x0136:
            android.util.LruCache<java.lang.String, X.5Rd> r0 = LIZJ     // Catch:{ all -> 0x015b }
            r0.put(r1, r7)     // Catch:{ all -> 0x015b }
        L_0x013b:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)     // Catch:{ all -> 0x015b }
            goto L_0x0159
        L_0x013f:
            java.lang.String r0 = "Orientation"
            int r3 = r4.getAttributeInt(r0, r3)     // Catch:{ all -> 0x015b }
            r0 = 3
            if (r3 == r0) goto L_0x0156
            r0 = 6
            if (r3 == r0) goto L_0x0153
            r0 = 8
            if (r3 == r0) goto L_0x0150
            goto L_0x011a
        L_0x0150:
            r11 = 270(0x10e, float:3.78E-43)
            goto L_0x011a
        L_0x0153:
            r11 = 90
            goto L_0x011a
        L_0x0156:
            r11 = 180(0xb4, float:2.52E-43)
            goto L_0x011a
        L_0x0159:
            monitor-exit(r2)
            return r7
        L_0x015b:
            r0 = move-exception
            monitor-exit(r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.bytedance.ies.cutsame.util.MediaUtil.LIZIZ(android.content.Context, java.lang.String):X.5Rd");
    }
}
