170 lines
7.5 KiB
Java
170 lines
7.5 KiB
Java
package com.bumptech.glide.load.resource.bitmap;
|
|
|
|
import android.annotation.TargetApi;
|
|
import android.content.res.AssetFileDescriptor;
|
|
import android.graphics.Bitmap;
|
|
import android.media.MediaMetadataRetriever;
|
|
import android.os.Build;
|
|
import android.os.ParcelFileDescriptor;
|
|
import android.util.Log;
|
|
import com.bumptech.glide.load.Option;
|
|
import com.bumptech.glide.load.Options;
|
|
import com.bumptech.glide.load.ResourceDecoder;
|
|
import com.bumptech.glide.load.engine.Resource;
|
|
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
|
|
import java.io.IOException;
|
|
import java.nio.ByteBuffer;
|
|
import java.security.MessageDigest;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class VideoDecoder<T> implements ResourceDecoder<T, Bitmap> {
|
|
public static final Option<Long> d = Option.a("com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.TargetFrame", -1L, new Option.CacheKeyUpdater<Long>() { // from class: com.bumptech.glide.load.resource.bitmap.VideoDecoder.1
|
|
private final ByteBuffer a = ByteBuffer.allocate(8);
|
|
|
|
@Override // com.bumptech.glide.load.Option.CacheKeyUpdater
|
|
public void a(byte[] bArr, Long l, MessageDigest messageDigest) {
|
|
messageDigest.update(bArr);
|
|
synchronized (this.a) {
|
|
this.a.position(0);
|
|
messageDigest.update(this.a.putLong(l.longValue()).array());
|
|
}
|
|
}
|
|
});
|
|
public static final Option<Integer> e = Option.a("com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.FrameOption", 2, new Option.CacheKeyUpdater<Integer>() { // from class: com.bumptech.glide.load.resource.bitmap.VideoDecoder.2
|
|
private final ByteBuffer a = ByteBuffer.allocate(4);
|
|
|
|
@Override // com.bumptech.glide.load.Option.CacheKeyUpdater
|
|
public void a(byte[] bArr, Integer num, MessageDigest messageDigest) {
|
|
if (num == null) {
|
|
return;
|
|
}
|
|
messageDigest.update(bArr);
|
|
synchronized (this.a) {
|
|
this.a.position(0);
|
|
messageDigest.update(this.a.putInt(num.intValue()).array());
|
|
}
|
|
}
|
|
});
|
|
private static final MediaMetadataRetrieverFactory f = new MediaMetadataRetrieverFactory();
|
|
private final MediaMetadataRetrieverInitializer<T> a;
|
|
private final BitmapPool b;
|
|
private final MediaMetadataRetrieverFactory c;
|
|
|
|
private static final class AssetFileDescriptorInitializer implements MediaMetadataRetrieverInitializer<AssetFileDescriptor> {
|
|
private AssetFileDescriptorInitializer() {
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.resource.bitmap.VideoDecoder.MediaMetadataRetrieverInitializer
|
|
public void a(MediaMetadataRetriever mediaMetadataRetriever, AssetFileDescriptor assetFileDescriptor) {
|
|
mediaMetadataRetriever.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
|
|
}
|
|
}
|
|
|
|
static class MediaMetadataRetrieverFactory {
|
|
MediaMetadataRetrieverFactory() {
|
|
}
|
|
|
|
public MediaMetadataRetriever a() {
|
|
return new MediaMetadataRetriever();
|
|
}
|
|
}
|
|
|
|
interface MediaMetadataRetrieverInitializer<T> {
|
|
void a(MediaMetadataRetriever mediaMetadataRetriever, T t);
|
|
}
|
|
|
|
static final class ParcelFileDescriptorInitializer implements MediaMetadataRetrieverInitializer<ParcelFileDescriptor> {
|
|
ParcelFileDescriptorInitializer() {
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.resource.bitmap.VideoDecoder.MediaMetadataRetrieverInitializer
|
|
public void a(MediaMetadataRetriever mediaMetadataRetriever, ParcelFileDescriptor parcelFileDescriptor) {
|
|
mediaMetadataRetriever.setDataSource(parcelFileDescriptor.getFileDescriptor());
|
|
}
|
|
}
|
|
|
|
VideoDecoder(BitmapPool bitmapPool, MediaMetadataRetrieverInitializer<T> mediaMetadataRetrieverInitializer) {
|
|
this(bitmapPool, mediaMetadataRetrieverInitializer, f);
|
|
}
|
|
|
|
public static ResourceDecoder<AssetFileDescriptor, Bitmap> a(BitmapPool bitmapPool) {
|
|
return new VideoDecoder(bitmapPool, new AssetFileDescriptorInitializer());
|
|
}
|
|
|
|
public static ResourceDecoder<ParcelFileDescriptor, Bitmap> b(BitmapPool bitmapPool) {
|
|
return new VideoDecoder(bitmapPool, new ParcelFileDescriptorInitializer());
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.ResourceDecoder
|
|
public boolean a(T t, Options options) {
|
|
return true;
|
|
}
|
|
|
|
VideoDecoder(BitmapPool bitmapPool, MediaMetadataRetrieverInitializer<T> mediaMetadataRetrieverInitializer, MediaMetadataRetrieverFactory mediaMetadataRetrieverFactory) {
|
|
this.b = bitmapPool;
|
|
this.a = mediaMetadataRetrieverInitializer;
|
|
this.c = mediaMetadataRetrieverFactory;
|
|
}
|
|
|
|
@TargetApi(27)
|
|
private static Bitmap b(MediaMetadataRetriever mediaMetadataRetriever, long j, int i, int i2, int i3, DownsampleStrategy downsampleStrategy) {
|
|
try {
|
|
int parseInt = Integer.parseInt(mediaMetadataRetriever.extractMetadata(18));
|
|
int parseInt2 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(19));
|
|
int parseInt3 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(24));
|
|
if (parseInt3 == 90 || parseInt3 == 270) {
|
|
parseInt2 = parseInt;
|
|
parseInt = parseInt2;
|
|
}
|
|
float b = downsampleStrategy.b(parseInt, parseInt2, i2, i3);
|
|
return mediaMetadataRetriever.getScaledFrameAtTime(j, i, Math.round(parseInt * b), Math.round(b * parseInt2));
|
|
} catch (Throwable th) {
|
|
if (!Log.isLoggable("VideoDecoder", 3)) {
|
|
return null;
|
|
}
|
|
Log.d("VideoDecoder", "Exception trying to decode frame on oreo+", th);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override // com.bumptech.glide.load.ResourceDecoder
|
|
public Resource<Bitmap> a(T t, int i, int i2, Options options) throws IOException {
|
|
long longValue = ((Long) options.a(d)).longValue();
|
|
if (longValue < 0 && longValue != -1) {
|
|
throw new IllegalArgumentException("Requested frame must be non-negative, or DEFAULT_FRAME, given: " + longValue);
|
|
}
|
|
Integer num = (Integer) options.a(e);
|
|
if (num == null) {
|
|
num = 2;
|
|
}
|
|
DownsampleStrategy downsampleStrategy = (DownsampleStrategy) options.a(DownsampleStrategy.f);
|
|
if (downsampleStrategy == null) {
|
|
downsampleStrategy = DownsampleStrategy.e;
|
|
}
|
|
DownsampleStrategy downsampleStrategy2 = downsampleStrategy;
|
|
MediaMetadataRetriever a = this.c.a();
|
|
try {
|
|
try {
|
|
this.a.a(a, t);
|
|
Bitmap a2 = a(a, longValue, num.intValue(), i, i2, downsampleStrategy2);
|
|
a.release();
|
|
return BitmapResource.a(a2, this.b);
|
|
} catch (RuntimeException e2) {
|
|
throw new IOException(e2);
|
|
}
|
|
} catch (Throwable th) {
|
|
a.release();
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
private static Bitmap a(MediaMetadataRetriever mediaMetadataRetriever, long j, int i, int i2, int i3, DownsampleStrategy downsampleStrategy) {
|
|
Bitmap b = (Build.VERSION.SDK_INT < 27 || i2 == Integer.MIN_VALUE || i3 == Integer.MIN_VALUE || downsampleStrategy == DownsampleStrategy.d) ? null : b(mediaMetadataRetriever, j, i, i2, i3, downsampleStrategy);
|
|
return b == null ? a(mediaMetadataRetriever, j, i) : b;
|
|
}
|
|
|
|
private static Bitmap a(MediaMetadataRetriever mediaMetadataRetriever, long j, int i) {
|
|
return mediaMetadataRetriever.getFrameAtTime(j, i);
|
|
}
|
|
}
|