/
VideoFrameBitmapDecoder.kt
259 lines (240 loc) · 10.4 KB
/
VideoFrameBitmapDecoder.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (C) 2022 panpf <panpfpanpf@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.panpf.sketch.decode
import android.annotation.TargetApi
import android.graphics.Bitmap
import android.media.MediaMetadataRetriever
import android.media.MediaMetadataRetriever.BitmapParams
import android.os.Build
import androidx.annotation.WorkerThread
import androidx.exifinterface.media.ExifInterface
import com.github.panpf.sketch.ComponentRegistry
import com.github.panpf.sketch.Sketch
import com.github.panpf.sketch.datasource.BasedFileDataSource
import com.github.panpf.sketch.datasource.ContentDataSource
import com.github.panpf.sketch.datasource.DataSource
import com.github.panpf.sketch.decode.internal.appliedExifOrientation
import com.github.panpf.sketch.decode.internal.appliedResize
import com.github.panpf.sketch.decode.internal.logString
import com.github.panpf.sketch.decode.internal.realDecode
import com.github.panpf.sketch.fetch.FetchResult
import com.github.panpf.sketch.request.internal.RequestContext
import com.github.panpf.sketch.request.videoFrameMicros
import com.github.panpf.sketch.request.videoFrameOption
import com.github.panpf.sketch.request.videoFramePercent
import kotlin.math.roundToInt
/**
* Adds video frame support
*/
@TargetApi(Build.VERSION_CODES.O_MR1)
fun ComponentRegistry.Builder.supportVideoFrame(): ComponentRegistry.Builder = apply {
addBitmapDecoder(VideoFrameBitmapDecoder.Factory())
}
/**
* Decode a frame of a video file and convert it to Bitmap
*
* Notes: Android O(26/8.0) and before versions do not support scale to read frames,
* resulting in slow decoding speed and large memory consumption in the case of large videos and causes memory jitter
*
* Notes:LoadRequest's preferQualityOverSpeed, colorSpace attributes will not take effect;
* The bitmapConfig attribute takes effect only on Android 30 or later
*/
@TargetApi(Build.VERSION_CODES.O_MR1)
class VideoFrameBitmapDecoder(
private val sketch: Sketch,
private val requestContext: RequestContext,
private val dataSource: DataSource,
private val mimeType: String,
) : BitmapDecoder {
companion object {
const val MODULE = "VideoFrameBitmapDecoder"
}
@WorkerThread
override suspend fun decode(): Result<BitmapDecodeResult> = kotlin.runCatching {
val request = requestContext.request
val mediaMetadataRetriever = MediaMetadataRetriever().apply {
when (dataSource) {
is ContentDataSource -> {
setDataSource(request.context, dataSource.contentUri)
}
is BasedFileDataSource -> {
setDataSource(dataSource.getFile().path)
}
else -> {
throw Exception("Unsupported DataSource: ${dataSource.javaClass}")
}
}
}
try {
val imageInfo = readImageInfo(mediaMetadataRetriever)
realDecode(
requestContext = requestContext,
dataFrom = dataSource.dataFrom,
imageInfo = imageInfo,
decodeFull = {
realDecodeFull(mediaMetadataRetriever, imageInfo, it)
},
decodeRegion = null
).appliedExifOrientation(sketch, requestContext)
.appliedResize(sketch, requestContext)
} finally {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
mediaMetadataRetriever.close()
} else {
mediaMetadataRetriever.release()
}
}
}
private fun readImageInfo(mediaMetadataRetriever: MediaMetadataRetriever): ImageInfo {
val srcWidth = mediaMetadataRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toIntOrNull() ?: 0
val srcHeight = mediaMetadataRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toIntOrNull() ?: 0
if (srcWidth <= 1 || srcHeight <= 1) {
val message = "Invalid video file. size=${srcWidth}x${srcHeight}"
throw ImageInvalidException(message)
}
val exifOrientation = if (!requestContext.request.ignoreExifOrientation) {
readExifOrientation(mediaMetadataRetriever)
} else {
ExifInterface.ORIENTATION_UNDEFINED
}
return ImageInfo(srcWidth, srcHeight, mimeType, exifOrientation)
}
private fun readExifOrientation(mediaMetadataRetriever: MediaMetadataRetriever): Int =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
val videoRotation = mediaMetadataRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)
?.toIntOrNull() ?: 0
videoRotation.run {
when (this) {
90 -> ExifInterface.ORIENTATION_ROTATE_90
180 -> ExifInterface.ORIENTATION_ROTATE_180
270 -> ExifInterface.ORIENTATION_ROTATE_270
else -> ExifInterface.ORIENTATION_UNDEFINED
}
}
} else {
ExifInterface.ORIENTATION_UNDEFINED
}
private fun realDecodeFull(
mediaMetadataRetriever: MediaMetadataRetriever,
imageInfo: ImageInfo,
decodeConfig: DecodeConfig
): Bitmap {
val request = requestContext.request
val option = request.videoFrameOption ?: MediaMetadataRetriever.OPTION_CLOSEST_SYNC
val frameMicros = request.videoFrameMicros
?: request.videoFramePercent?.let { percentDuration ->
val duration = mediaMetadataRetriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
?.toLongOrNull() ?: 0L
(duration * percentDuration * 1000).toLong()
}
?: 0L
val inSampleSize = decodeConfig.inSampleSize?.toFloat()
val dstWidth = if (inSampleSize != null) {
(imageInfo.width / inSampleSize).roundToInt()
} else {
imageInfo.width
}
val dstHeight = if (inSampleSize != null) {
(imageInfo.height / inSampleSize).roundToInt()
} else {
imageInfo.height
}
val bitmap = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
val bitmapParams = BitmapParams().apply {
val inPreferredConfigFromRequest = decodeConfig.inPreferredConfig
if (inPreferredConfigFromRequest != null) {
preferredConfig = inPreferredConfigFromRequest
}
}
mediaMetadataRetriever
.getScaledFrameAtTime(frameMicros, option, dstWidth, dstHeight, bitmapParams)
?: throw BitmapDecodeException(
"Failed to getScaledFrameAtTime. frameMicros=%d, option=%s, dst=%dx%d, image=%dx%d, preferredConfig=%s.".format(
frameMicros, optionToName(option), dstWidth, dstHeight,
imageInfo.width, imageInfo.height, decodeConfig.inPreferredConfig
)
)
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 -> {
mediaMetadataRetriever
.getScaledFrameAtTime(frameMicros, option, dstWidth, dstHeight)
?: throw BitmapDecodeException(
"Failed to getScaledFrameAtTime. frameMicros=%d, option=%s, dst=%dx%d, image=%dx%d.".format(
frameMicros, optionToName(option), dstWidth, dstHeight,
imageInfo.width, imageInfo.height
)
)
}
else -> {
mediaMetadataRetriever.getFrameAtTime(frameMicros, option)
?: throw BitmapDecodeException(
"Failed to getFrameAtTime. frameMicros=%d, option=%s, image=%dx%d.".format(
frameMicros, optionToName(option), imageInfo.width, imageInfo.height
)
)
}
}
sketch.logger.d(MODULE) {
"realDecodeFull. successful. ${bitmap.logString}. ${imageInfo}. '${requestContext.key}'"
}
return bitmap
}
private fun optionToName(option: Int): String {
return when (option) {
MediaMetadataRetriever.OPTION_CLOSEST -> "CLOSEST"
MediaMetadataRetriever.OPTION_CLOSEST_SYNC -> "CLOSEST_SYNC"
MediaMetadataRetriever.OPTION_NEXT_SYNC -> "NEXT_SYNC"
MediaMetadataRetriever.OPTION_PREVIOUS_SYNC -> "PREVIOUS_SYNC"
else -> "Unknown($option)"
}
}
@TargetApi(Build.VERSION_CODES.O_MR1)
class Factory : BitmapDecoder.Factory {
override fun create(
sketch: Sketch,
requestContext: RequestContext,
fetchResult: FetchResult
): VideoFrameBitmapDecoder? {
val dataSource = fetchResult.dataSource
val mimeType = fetchResult.mimeType
if (mimeType?.startsWith("video/") == true
&& (dataSource is ContentDataSource || dataSource is BasedFileDataSource)
) {
return VideoFrameBitmapDecoder(
sketch = sketch,
requestContext = requestContext,
dataSource = dataSource,
mimeType = mimeType
)
}
return null
}
override fun toString(): String = "VideoFrameBitmapDecoder"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
return true
}
override fun hashCode(): Int {
return javaClass.hashCode()
}
}
}