package z1;

import androidx.annotation.Nullable;
import androidx.media3.common.Format;
import androidx.media3.common.Metadata;
import androidx.media3.common.MimeTypes;
import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.ParsableByteArray;
import androidx.media3.extractor.b0;
import androidx.media3.extractor.i0;
import androidx.media3.extractor.r0;
import androidx.media3.extractor.y;
import androidx.media3.extractor.z;
import java.io.IOException;
import l2.u;
import n2.n;

/* JADX INFO: loaded from: classes.dex */
public final class a implements y {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public b0 f37009b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f37010c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f37011d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f37012e;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public z f37014g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public d f37015h;

    @Nullable
    private g2.a motionPhotoMetadata;

    @Nullable
    private u mp4Extractor;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final ParsableByteArray f37008a = new ParsableByteArray(2);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public long f37013f = -1;

    @Nullable
    private static g2.a getMotionPhotoMetadata(String str, long j10) throws IOException {
        c cVar;
        if (j10 == -1 || (cVar = g.parse(str)) == null) {
            return null;
        }
        return cVar.getMotionPhotoMetadata(j10);
    }

    public final void a() {
        ((b0) Assertions.checkNotNull(this.f37009b)).endTracks();
        this.f37009b.seekMap(new i0(-9223372036854775807L));
        this.f37010c = 6;
    }

    public final int b(z zVar) {
        ParsableByteArray parsableByteArray = this.f37008a;
        parsableByteArray.reset(2);
        zVar.a(0, 2, parsableByteArray.getData());
        return parsableByteArray.readUnsignedShort();
    }

    @Override // androidx.media3.extractor.y
    public final void init(b0 b0Var) {
        this.f37009b = b0Var;
    }

    @Override // androidx.media3.extractor.y
    public final int read(z zVar, r0 r0Var) throws IOException {
        String nullTerminatedString;
        int i10 = this.f37010c;
        ParsableByteArray parsableByteArray = this.f37008a;
        if (i10 == 0) {
            parsableByteArray.reset(2);
            zVar.readFully(parsableByteArray.getData(), 0, 2);
            int unsignedShort = parsableByteArray.readUnsignedShort();
            this.f37011d = unsignedShort;
            if (unsignedShort == 65498) {
                if (this.f37013f != -1) {
                    this.f37010c = 4;
                } else {
                    a();
                }
            } else if ((unsignedShort < 65488 || unsignedShort > 65497) && unsignedShort != 65281) {
                this.f37010c = 1;
            }
            return 0;
        }
        if (i10 == 1) {
            parsableByteArray.reset(2);
            zVar.readFully(parsableByteArray.getData(), 0, 2);
            this.f37012e = parsableByteArray.readUnsignedShort() - 2;
            this.f37010c = 2;
            return 0;
        }
        if (i10 == 2) {
            if (this.f37011d == 65505) {
                ParsableByteArray parsableByteArray2 = new ParsableByteArray(this.f37012e);
                zVar.readFully(parsableByteArray2.getData(), 0, this.f37012e);
                if (this.motionPhotoMetadata == null && "http://ns.adobe.com/xap/1.0/".equals(parsableByteArray2.readNullTerminatedString()) && (nullTerminatedString = parsableByteArray2.readNullTerminatedString()) != null) {
                    g2.a motionPhotoMetadata = getMotionPhotoMetadata(nullTerminatedString, zVar.getLength());
                    this.motionPhotoMetadata = motionPhotoMetadata;
                    if (motionPhotoMetadata != null) {
                        this.f37013f = motionPhotoMetadata.f26157d;
                    }
                }
            } else {
                zVar.k(this.f37012e);
            }
            this.f37010c = 0;
            return 0;
        }
        if (i10 != 4) {
            if (i10 != 5) {
                if (i10 == 6) {
                    return -1;
                }
                throw new IllegalStateException();
            }
            if (this.f37015h == null || zVar != this.f37014g) {
                this.f37014g = zVar;
                this.f37015h = new d(zVar, this.f37013f);
            }
            int i11 = ((u) Assertions.checkNotNull(this.mp4Extractor)).read(this.f37015h, r0Var);
            if (i11 == 1) {
                r0Var.f3463a += this.f37013f;
            }
            return i11;
        }
        long position = zVar.getPosition();
        long j10 = this.f37013f;
        if (position != j10) {
            r0Var.f3463a = j10;
            return 1;
        }
        if (zVar.f(parsableByteArray.getData(), 0, 1, true)) {
            zVar.j();
            if (this.mp4Extractor == null) {
                this.mp4Extractor = new u(8, n.f30781a);
            }
            d dVar = new d(zVar, this.f37013f);
            this.f37015h = dVar;
            if (this.mp4Extractor.sniff(dVar)) {
                this.mp4Extractor.init(new f(this.f37013f, (b0) Assertions.checkNotNull(this.f37009b)));
                ((b0) Assertions.checkNotNull(this.f37009b)).track(1024, 4).format(new Format.Builder().setContainerMimeType(MimeTypes.IMAGE_JPEG).setMetadata(new Metadata((g2.a) Assertions.checkNotNull(this.motionPhotoMetadata))).build());
                this.f37010c = 5;
            } else {
                a();
            }
        } else {
            a();
        }
        return 0;
    }

    @Override // androidx.media3.extractor.y
    public final void release() {
        u uVar = this.mp4Extractor;
        if (uVar != null) {
            uVar.getClass();
        }
    }

    @Override // androidx.media3.extractor.y
    public final void seek(long j10, long j11) {
        if (j10 == 0) {
            this.f37010c = 0;
            this.mp4Extractor = null;
        } else if (this.f37010c == 5) {
            ((u) Assertions.checkNotNull(this.mp4Extractor)).seek(j10, j11);
        }
    }

    @Override // androidx.media3.extractor.y
    public final boolean sniff(z zVar) {
        if (b(zVar) != 65496) {
            return false;
        }
        int iB = b(zVar);
        this.f37011d = iB;
        if (iB == 65504) {
            ParsableByteArray parsableByteArray = this.f37008a;
            parsableByteArray.reset(2);
            zVar.a(0, 2, parsableByteArray.getData());
            zVar.h(parsableByteArray.readUnsignedShort() - 2);
            this.f37011d = b(zVar);
        }
        return this.f37011d == 65505;
    }
}
