-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathDefaultVideoStrategy.java
371 lines (335 loc) · 14 KB
/
DefaultVideoStrategy.java
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package com.otaliastudios.transcoder.strategy;
import android.media.MediaCodecInfo;
import android.media.MediaFormat;
import android.os.Build;
import com.otaliastudios.transcoder.engine.TrackStatus;
import com.otaliastudios.transcoder.internal.BitRates;
import com.otaliastudios.transcoder.strategy.size.AspectRatioResizer;
import com.otaliastudios.transcoder.strategy.size.AtMostResizer;
import com.otaliastudios.transcoder.strategy.size.ExactResizer;
import com.otaliastudios.transcoder.strategy.size.ExactSize;
import com.otaliastudios.transcoder.strategy.size.FractionResizer;
import com.otaliastudios.transcoder.strategy.size.MultiResizer;
import com.otaliastudios.transcoder.strategy.size.Size;
import com.otaliastudios.transcoder.strategy.size.Resizer;
import com.otaliastudios.transcoder.internal.Logger;
import com.otaliastudios.transcoder.internal.MediaFormatConstants;
import com.otaliastudios.transcoder.transcode.internal.VideoDecoderOutput;
import androidx.annotation.NonNull;
import java.util.List;
/**
* An {@link TrackStrategy} for video that converts it AVC with the given size.
* The input and output aspect ratio must match.
*/
public class DefaultVideoStrategy implements TrackStrategy {
private final static String TAG = DefaultVideoStrategy.class.getSimpleName();
private final static Logger LOG = new Logger(TAG);
@SuppressWarnings("WeakerAccess")
public final static long BITRATE_UNKNOWN = Long.MIN_VALUE;
@SuppressWarnings("WeakerAccess")
public final static float DEFAULT_KEY_FRAME_INTERVAL = 3;
public final static int DEFAULT_FRAME_RATE = 30;
/**
* Holds configuration values.
*/
@SuppressWarnings("WeakerAccess")
public static class Options {
private Options() {}
private Resizer resizer;
private long targetBitRate;
private int targetFrameRate;
private float targetKeyFrameInterval;
private String targetMimeType;
}
/**
* Creates a new {@link Builder} with an {@link ExactResizer}
* using given dimensions.
*
* @param firstSize the exact first size
* @param secondSize the exact second size
* @return a strategy builder
*/
@NonNull
@SuppressWarnings("WeakerAccess")
public static Builder exact(int firstSize, int secondSize) {
return new Builder(new ExactResizer(firstSize, secondSize));
}
/**
* Creates a new {@link Builder} with a {@link FractionResizer}
* using given downscale fraction.
*
* @param fraction the downscale fraction
* @return a strategy builder
*/
@NonNull
@SuppressWarnings("unused")
public static Builder fraction(float fraction) {
return new Builder(new FractionResizer(fraction));
}
/**
* Creates a new {@link Builder} with a {@link AspectRatioResizer}
* using given aspect ratio value.
*
* @param aspectRatio the desired aspect ratio
* @return a strategy builder
*/
@NonNull
@SuppressWarnings("unused")
public static Builder aspectRatio(float aspectRatio) {
return new Builder(new AspectRatioResizer(aspectRatio));
}
/**
* Creates a new {@link Builder} with an {@link AtMostResizer}
* using given constraint.
*
* @param atMostSize size constraint
* @return a strategy builder
*/
@NonNull
@SuppressWarnings("unused")
public static Builder atMost(int atMostSize) {
return new Builder(new AtMostResizer(atMostSize));
}
/**
* Creates a new {@link Builder} with an {@link AtMostResizer}
* using given constraints.
*
* @param atMostMajor constraint for the major dimension
* @param atMostMinor constraint for the minor dimension
* @return a strategy builder
*/
@NonNull
@SuppressWarnings("unused")
public static Builder atMost(int atMostMinor, int atMostMajor) {
return new Builder(new AtMostResizer(atMostMinor, atMostMajor));
}
public static class Builder {
private MultiResizer resizer = new MultiResizer();
private int targetFrameRate = DEFAULT_FRAME_RATE;
private long targetBitRate = BITRATE_UNKNOWN;
private float targetKeyFrameInterval = DEFAULT_KEY_FRAME_INTERVAL;
private String targetMimeType = MediaFormatConstants.MIMETYPE_VIDEO_AVC;
@SuppressWarnings("unused")
public Builder() { }
@SuppressWarnings("WeakerAccess")
public Builder(@NonNull Resizer resizer) {
this.resizer.addResizer(resizer);
}
/**
* Adds another resizer to the resizer chain. By default, we use
* a {@link MultiResizer} so you can add more than one resizer in chain.
* @param resizer new resizer for backed {@link MultiResizer}
* @return this for chaining
*/
@NonNull
@SuppressWarnings("unused")
public Builder addResizer(@NonNull Resizer resizer) {
this.resizer.addResizer(resizer);
return this;
}
/**
* The desired bit rate. Can optionally be {@link #BITRATE_UNKNOWN},
* in which case the strategy will try to estimate the bitrate.
* @param bitRate desired bit rate (bits per second)
* @return this for chaining
*/
@NonNull
@SuppressWarnings("WeakerAccess")
public Builder bitRate(long bitRate) {
targetBitRate = bitRate;
return this;
}
/**
* The desired frame rate. It will never be bigger than
* the input frame rate, if that information is available.
* @param frameRate desired frame rate (frames per second)
* @return this for chaining
*/
@NonNull
public Builder frameRate(int frameRate) {
targetFrameRate = frameRate;
return this;
}
/**
* The interval between key-frames in seconds.
* @param keyFrameInterval desired key-frame interval
* @return this for chaining
*/
@NonNull
@SuppressWarnings("WeakerAccess")
public Builder keyFrameInterval(float keyFrameInterval) {
targetKeyFrameInterval = keyFrameInterval;
return this;
}
@SuppressWarnings("unused")
@NonNull
public Builder mimeType(@NonNull String mimeType) {
this.targetMimeType = mimeType;
return this;
}
@NonNull
@SuppressWarnings("WeakerAccess")
public Options options() {
Options options = new Options();
options.resizer = resizer;
options.targetFrameRate = targetFrameRate;
options.targetBitRate = targetBitRate;
options.targetKeyFrameInterval = targetKeyFrameInterval;
options.targetMimeType = targetMimeType;
return options;
}
@NonNull
public DefaultVideoStrategy build() {
return new DefaultVideoStrategy(options());
}
}
private final Options options;
@SuppressWarnings("WeakerAccess")
public DefaultVideoStrategy(@NonNull Options options) {
this.options = options;
}
@NonNull
@Override
public TrackStatus createOutputFormat(@NonNull List<MediaFormat> inputFormats,
@NonNull MediaFormat outputFormat) {
boolean typeDone = checkMimeType(inputFormats);
// Compute output size in rotation=0 reference.
ExactSize inSize = getBestInputSize(inputFormats);
int inWidth = inSize.getWidth();
int inHeight = inSize.getHeight();
LOG.i("Input width&height: " + inWidth + "x" + inHeight);
Size outSize;
try {
outSize = options.resizer.getOutputSize(inSize);
} catch (Exception e) {
throw new RuntimeException("Resizer error:", e);
}
int outWidth, outHeight;
if (outSize instanceof ExactSize) {
outWidth = ((ExactSize) outSize).getWidth();
outHeight = ((ExactSize) outSize).getHeight();
} else if (inWidth >= inHeight) {
outWidth = outSize.getMajor();
outHeight = outSize.getMinor();
} else {
outWidth = outSize.getMinor();
outHeight = outSize.getMajor();
}
LOG.i("Output width&height: " + outWidth + "x" + outHeight);
boolean sizeDone = inSize.getMinor() <= outSize.getMinor();
// Compute output frame rate. It can't be bigger than input frame rate.
int outFrameRate;
int inputFrameRate = getMinFrameRate(inputFormats);
if (inputFrameRate > 0) {
outFrameRate = Math.min(inputFrameRate, options.targetFrameRate);
} else {
outFrameRate = options.targetFrameRate;
}
boolean frameRateDone = inputFrameRate <= outFrameRate;
// Compute i frame.
int inputIFrameInterval = getAverageIFrameInterval(inputFormats);
boolean frameIntervalDone = inputIFrameInterval >= options.targetKeyFrameInterval;
// See if we should go on or if we're already compressed.
// If we have more than 1 input format, we can't go through this branch,
// or, for example, each part would be copied into output with its own size,
// breaking the muxer.
boolean canPassThrough = inputFormats.size() == 1;
if (canPassThrough && typeDone && sizeDone && frameRateDone && frameIntervalDone) {
LOG.i("Input minSize: " + inSize.getMinor() + ", desired minSize: " + outSize.getMinor() +
"\nInput frameRate: " + inputFrameRate + ", desired frameRate: " + outFrameRate +
"\nInput iFrameInterval: " + inputIFrameInterval + ", desired iFrameInterval: " + options.targetKeyFrameInterval);
return TrackStatus.PASS_THROUGH;
}
// Create the actual format.
outputFormat.setString(MediaFormat.KEY_MIME, options.targetMimeType);
outputFormat.setInteger(MediaFormat.KEY_WIDTH, outWidth);
outputFormat.setInteger(MediaFormat.KEY_HEIGHT, outHeight);
outputFormat.setInteger(MediaFormatConstants.KEY_ROTATION_DEGREES, 0);
outputFormat.setInteger(MediaFormat.KEY_FRAME_RATE, outFrameRate);
if (Build.VERSION.SDK_INT >= 25) {
outputFormat.setFloat(MediaFormat.KEY_I_FRAME_INTERVAL, options.targetKeyFrameInterval);
} else {
outputFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, (int) Math.ceil(options.targetKeyFrameInterval));
}
outputFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatSurface);
int outBitRate = (int) (options.targetBitRate == BITRATE_UNKNOWN
? BitRates.estimateVideoBitRate(outWidth, outHeight, outFrameRate)
: options.targetBitRate);
outputFormat.setInteger(MediaFormat.KEY_BIT_RATE, outBitRate);
return TrackStatus.COMPRESSING;
}
private boolean checkMimeType(@NonNull List<MediaFormat> formats) {
for (MediaFormat format : formats) {
if (!format.getString(MediaFormat.KEY_MIME).equalsIgnoreCase(options.targetMimeType)) {
return false;
}
}
return true;
}
/**
* Chooses one of the input sizes that is considered to be the best.
* After thinking about it, I think the best size is the one that is closer to the
* average aspect ratio.
*
* Of course, we must consider all formats' rotation.
* The size returned is rotated in the reference of a format with rotation = 0.
*
* @param formats input formats
* @return best input size
*/
private ExactSize getBestInputSize(@NonNull List<MediaFormat> formats) {
int count = formats.size();
float averageAspectRatio = 0;
float[] aspectRatio = new float[count];
boolean[] flipSize = new boolean[count];
for (int i = 0; i < count; i++) {
MediaFormat format = formats.get(i);
float width = format.getInteger(MediaFormat.KEY_WIDTH);
float height = format.getInteger(MediaFormat.KEY_HEIGHT);
int rotation = 0;
if (format.containsKey(MediaFormatConstants.KEY_ROTATION_DEGREES)) {
rotation = format.getInteger(MediaFormatConstants.KEY_ROTATION_DEGREES);
}
boolean flip = (rotation % 180) != 0;
flipSize[i] = flip;
aspectRatio[i] = flip ? height / width : width / height;
averageAspectRatio += aspectRatio[i];
}
averageAspectRatio = averageAspectRatio / count;
float bestDelta = Float.MAX_VALUE;
int bestMatch = 0;
for (int i = 0; i < count; i++) {
float delta = Math.abs(aspectRatio[i] - averageAspectRatio);
if (delta < bestDelta) {
bestMatch = i;
bestDelta = delta;
}
}
MediaFormat bestFormat = formats.get(bestMatch);
int bestWidth = bestFormat.getInteger(MediaFormat.KEY_WIDTH);
int bestHeight = bestFormat.getInteger(MediaFormat.KEY_HEIGHT);
return new ExactSize(
flipSize[bestMatch] ? bestHeight : bestWidth,
flipSize[bestMatch] ? bestWidth : bestHeight);
}
private int getMinFrameRate(@NonNull List<MediaFormat> formats) {
int frameRate = Integer.MAX_VALUE;
for (MediaFormat format : formats) {
if (format.containsKey(MediaFormat.KEY_FRAME_RATE)) {
frameRate = Math.min(frameRate, format.getInteger(MediaFormat.KEY_FRAME_RATE));
}
}
return (frameRate == Integer.MAX_VALUE) ? -1 : frameRate;
}
private int getAverageIFrameInterval(@NonNull List<MediaFormat> formats) {
int count = 0;
int sum = 0;
for (MediaFormat format : formats) {
if (format.containsKey(MediaFormat.KEY_I_FRAME_INTERVAL)) {
count++;
sum += format.getInteger(MediaFormat.KEY_I_FRAME_INTERVAL);
}
}
return (count > 0) ? Math.round((float) sum / count) : -1;
}
}