-
Notifications
You must be signed in to change notification settings - Fork 638
/
VideoFrameDecoder.kt
224 lines (198 loc) · 8.69 KB
/
VideoFrameDecoder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package coil.decode
import android.graphics.Bitmap
import android.graphics.Paint
import android.media.MediaMetadataRetriever
import android.media.MediaMetadataRetriever.METADATA_KEY_DURATION
import android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT
import android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION
import android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH
import android.media.MediaMetadataRetriever.OPTION_CLOSEST_SYNC
import android.os.Build.VERSION.SDK_INT
import androidx.core.graphics.applyCanvas
import androidx.core.graphics.createBitmap
import androidx.core.graphics.drawable.toDrawable
import coil.ImageLoader
import coil.fetch.SourceResult
import coil.request.Options
import coil.request.videoFrameMicros
import coil.request.videoFrameOption
import coil.request.videoFramePercent
import coil.size.Dimension.Pixels
import coil.size.Size
import coil.size.pxOrElse
import coil.util.getFrameAtTime
import coil.util.getScaledFrameAtTime
import coil.util.heightPx
import coil.util.use
import coil.util.widthPx
import kotlin.math.roundToInt
import kotlin.math.roundToLong
/**
* A [Decoder] that uses [MediaMetadataRetriever] to fetch and decode a frame from a video.
*/
class VideoFrameDecoder(
private val source: ImageSource,
private val options: Options
) : Decoder {
override suspend fun decode() = MediaMetadataRetriever().use { retriever ->
retriever.setDataSource(source)
val option = options.parameters.videoFrameOption() ?: OPTION_CLOSEST_SYNC
val frameMicros = computeFrameMicros(retriever)
// Resolve the dimensions to decode the video frame at accounting
// for the source's aspect ratio and the target's size.
var srcWidth: Int
var srcHeight: Int
val rotation = retriever.extractMetadata(METADATA_KEY_VIDEO_ROTATION)?.toIntOrNull() ?: 0
if (rotation == 90 || rotation == 270) {
srcWidth = retriever.extractMetadata(METADATA_KEY_VIDEO_HEIGHT)?.toIntOrNull() ?: 0
srcHeight = retriever.extractMetadata(METADATA_KEY_VIDEO_WIDTH)?.toIntOrNull() ?: 0
} else {
srcWidth = retriever.extractMetadata(METADATA_KEY_VIDEO_WIDTH)?.toIntOrNull() ?: 0
srcHeight = retriever.extractMetadata(METADATA_KEY_VIDEO_HEIGHT)?.toIntOrNull() ?: 0
}
val dstSize = if (srcWidth > 0 && srcHeight > 0) {
val dstWidth = options.size.widthPx(options.scale) { srcWidth }
val dstHeight = options.size.heightPx(options.scale) { srcHeight }
val rawScale = DecodeUtils.computeSizeMultiplier(
srcWidth = srcWidth,
srcHeight = srcHeight,
dstWidth = dstWidth,
dstHeight = dstHeight,
scale = options.scale
)
val scale = if (options.allowInexactSize) {
rawScale.coerceAtMost(1.0)
} else {
rawScale
}
val width = (scale * srcWidth).roundToInt()
val height = (scale * srcHeight).roundToInt()
Size(width, height)
} else {
// We were unable to decode the video's dimensions.
// Fall back to decoding the video frame at the original size.
// We'll scale the resulting bitmap after decoding if necessary.
Size.ORIGINAL
}
val (dstWidth, dstHeight) = dstSize
val rawBitmap: Bitmap? = if (SDK_INT >= 27 && dstWidth is Pixels && dstHeight is Pixels) {
retriever.getScaledFrameAtTime(frameMicros, option, dstWidth.px, dstHeight.px, options.config)
} else {
retriever.getFrameAtTime(frameMicros, option, options.config)?.also {
srcWidth = it.width
srcHeight = it.height
}
}
// If you encounter this exception make sure your video is encoded in a supported codec.
// https://developer.android.com/guide/topics/media/media-formats#video-formats
checkNotNull(rawBitmap) { "Failed to decode frame at $frameMicros microseconds." }
val bitmap = normalizeBitmap(rawBitmap, dstSize)
val isSampled = if (srcWidth > 0 && srcHeight > 0) {
DecodeUtils.computeSizeMultiplier(
srcWidth = srcWidth,
srcHeight = srcHeight,
dstWidth = bitmap.width,
dstHeight = bitmap.height,
scale = options.scale
) < 1.0
} else {
// We were unable to determine the original size of the video. Assume it is sampled.
true
}
DecodeResult(
drawable = bitmap.toDrawable(options.context.resources),
isSampled = isSampled
)
}
private fun computeFrameMicros(retriever: MediaMetadataRetriever): Long {
val frameMicros = options.parameters.videoFrameMicros()
if (frameMicros != null) {
return frameMicros
}
val framePercent = options.parameters.videoFramePercent()
if (framePercent != null) {
val durationMillis = retriever.extractMetadata(METADATA_KEY_DURATION)?.toLongOrNull() ?: 0L
return 1000 * (framePercent * durationMillis).roundToLong()
}
return 0
}
/** Return [inBitmap] or a copy of [inBitmap] that is valid for the input [options] and [size]. */
private fun normalizeBitmap(inBitmap: Bitmap, size: Size): Bitmap {
// Fast path: if the input bitmap is valid, return it.
if (isConfigValid(inBitmap, options) && isSizeValid(inBitmap, options, size)) {
return inBitmap
}
// Slow path: re-render the bitmap with the correct size + config.
val scale = DecodeUtils.computeSizeMultiplier(
srcWidth = inBitmap.width,
srcHeight = inBitmap.height,
dstWidth = size.width.pxOrElse { inBitmap.width },
dstHeight = size.height.pxOrElse { inBitmap.height },
scale = options.scale
).toFloat()
val dstWidth = (scale * inBitmap.width).roundToInt()
val dstHeight = (scale * inBitmap.height).roundToInt()
val safeConfig = when {
SDK_INT >= 26 && options.config == Bitmap.Config.HARDWARE -> Bitmap.Config.ARGB_8888
else -> options.config
}
val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG)
val outBitmap = createBitmap(dstWidth, dstHeight, safeConfig)
outBitmap.applyCanvas {
scale(scale, scale)
drawBitmap(inBitmap, 0f, 0f, paint)
}
inBitmap.recycle()
return outBitmap
}
private fun isConfigValid(bitmap: Bitmap, options: Options): Boolean {
return SDK_INT < 26 ||
bitmap.config != Bitmap.Config.HARDWARE ||
options.config == Bitmap.Config.HARDWARE
}
private fun isSizeValid(bitmap: Bitmap, options: Options, size: Size): Boolean {
if (options.allowInexactSize) return true
val multiplier = DecodeUtils.computeSizeMultiplier(
srcWidth = bitmap.width,
srcHeight = bitmap.height,
dstWidth = size.width.pxOrElse { bitmap.width },
dstHeight = size.height.pxOrElse { bitmap.height },
scale = options.scale
)
return multiplier == 1.0
}
private fun MediaMetadataRetriever.setDataSource(source: ImageSource) {
when (val metadata = source.metadata) {
is AssetMetadata -> {
options.context.assets.openFd(metadata.filePath).use {
setDataSource(it.fileDescriptor, it.startOffset, it.length)
}
}
is ContentMetadata -> {
setDataSource(options.context, metadata.uri)
}
is ResourceMetadata -> {
setDataSource("android.resource://${metadata.packageName}/${metadata.resId}")
}
else -> {
setDataSource(source.file().toFile().path)
}
}
}
class Factory : Decoder.Factory {
override fun create(result: SourceResult, options: Options, imageLoader: ImageLoader): Decoder? {
if (!isApplicable(result.mimeType)) return null
return VideoFrameDecoder(result.source, options)
}
private fun isApplicable(mimeType: String?): Boolean {
return mimeType != null && mimeType.startsWith("video/")
}
override fun equals(other: Any?) = other is Factory
override fun hashCode() = javaClass.hashCode()
}
companion object {
const val VIDEO_FRAME_MICROS_KEY = "coil#video_frame_micros"
const val VIDEO_FRAME_PERCENT_KEY = "coil#video_frame_percent"
const val VIDEO_FRAME_OPTION_KEY = "coil#video_frame_option"
}
}