-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMP4Encoder.java
More file actions
281 lines (266 loc) · 9.73 KB
/
MP4Encoder.java
File metadata and controls
281 lines (266 loc) · 9.73 KB
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
package com.ctech.bitmp4;
import static android.media.MediaCodec.CONFIGURE_FLAG_ENCODE;
import static android.media.MediaCodec.INFO_OUTPUT_FORMAT_CHANGED;
import static android.media.MediaCodec.INFO_TRY_AGAIN_LATER;
import static android.media.MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar;
import static android.media.MediaCodecInfo.CodecProfileLevel.MPEG2ProfileHigh;
import static android.media.MediaFormat.KEY_AAC_PROFILE;
import static android.media.MediaFormat.KEY_BIT_RATE;
import static android.media.MediaFormat.KEY_COLOR_FORMAT;
import static android.media.MediaFormat.KEY_FRAME_RATE;
import static android.media.MediaFormat.KEY_I_FRAME_INTERVAL;
import static android.media.MediaFormat.MIMETYPE_AUDIO_AAC;
import static android.media.MediaFormat.MIMETYPE_VIDEO_AVC;
import static android.media.MediaMuxer.OutputFormat.MUXER_OUTPUT_MPEG_4;
import android.graphics.Bitmap;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaFormat;
import android.media.MediaMuxer;
import android.os.Build;
import java.io.IOException;
import java.nio.ByteBuffer;
import timber.log.Timber;
public class MP4Encoder extends Encoder {
private static final int BIT_RATE = 2000000;
private static final int FRAME_RATE = 20;
private static final int I_FRAME_INTERVAL = 5;
private static final long ONE_SEC = 1000000;
private static final String TAG = MP4Encoder.class.getSimpleName();
private static final int TIMEOUT_US = 10000;
private int addedFrameCount;
private byte[] audioArray = new byte[4096];
private MediaCodec audioCodec;
private int audioTrackIndex;
private BufferInfo bufferInfo;
private int encodedFrameCount;
private boolean isMuxerStarted = false;
private boolean isStarted = false;
private MediaMuxer mediaMuxer;
private int trackCount = 0;
private MediaCodec videoCodec;
private int videoTrackIndex;
@Override
protected void onInit() {
}
@Override
protected void onStart() {
isStarted = true;
addedFrameCount = 0;
encodedFrameCount = 0;
int width = getWidth();
int height = getHeight();
try {
bufferInfo = new BufferInfo();
videoCodec = MediaCodec.createEncoderByType(MIMETYPE_VIDEO_AVC);
MediaFormat videoFormat = MediaFormat.createVideoFormat(MIMETYPE_VIDEO_AVC, width, height);
videoFormat.setInteger(KEY_BIT_RATE, BIT_RATE);
videoFormat.setInteger(KEY_FRAME_RATE, FRAME_RATE);
videoFormat.setInteger(KEY_I_FRAME_INTERVAL, I_FRAME_INTERVAL);
videoFormat.setInteger(KEY_COLOR_FORMAT, COLOR_FormatYUV420SemiPlanar);
videoCodec.configure(videoFormat, null, null, CONFIGURE_FLAG_ENCODE);
videoCodec.start();
audioCodec = MediaCodec.createEncoderByType(MIMETYPE_AUDIO_AAC);
MediaFormat audioFormat = MediaFormat.createAudioFormat(MIMETYPE_AUDIO_AAC, 44100, 1);
int profile;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
profile = MPEG2ProfileHigh;
} else {
profile = 5;
}
audioFormat.setInteger(KEY_AAC_PROFILE, profile);
audioFormat.setInteger(KEY_BIT_RATE, 65536);
audioCodec.configure(audioFormat, null, null, CONFIGURE_FLAG_ENCODE);
audioCodec.start();
mediaMuxer = new MediaMuxer(outputFilePath, MUXER_OUTPUT_MPEG_4);
} catch (IOException ioe) {
throw new RuntimeException("MediaMuxer creation failed", ioe);
}
}
@Override
protected void onStop() {
if (isStarted) {
encode();
if (this.addedFrameCount > 0) {
Timber.i(TAG, "Total frame count = %s", this.addedFrameCount);
if (videoCodec != null) {
videoCodec.stop();
videoCodec.release();
videoCodec = null;
Timber.i(TAG, "RELEASE VIDEO CODEC");
}
if (audioCodec != null) {
audioCodec.stop();
audioCodec.release();
audioCodec = null;
Timber.i(TAG, "RELEASE AUDIO CODEC");
}
if (mediaMuxer != null) {
mediaMuxer.stop();
mediaMuxer.release();
mediaMuxer = null;
Timber.i(TAG, "RELEASE MUXER");
}
} else {
Timber.e(TAG, "not added any frame");
}
isStarted = false;
}
}
@Override
protected void onAddFrame(Bitmap bitmap) {
if (!isStarted) {
Timber.d(TAG, "already finished. can't add Frame ");
} else if (bitmap == null) {
Timber.e(TAG, "Bitmap is null");
} else {
int inputBufIndex = videoCodec.dequeueInputBuffer(TIMEOUT_US);
if (inputBufIndex >= 0) {
byte[] input = getNV12(bitmap.getWidth(), bitmap.getHeight(), bitmap);
ByteBuffer inputBuffer = videoCodec.getInputBuffer(inputBufIndex);
inputBuffer.clear();
inputBuffer.put(input);
videoCodec.queueInputBuffer(inputBufIndex, 0, input.length,
getPresentationTimeUsec(addedFrameCount), 0);
}
int audioInputBufferIndex = audioCodec.dequeueInputBuffer(TIMEOUT_US);
if (audioInputBufferIndex >= -1) {
ByteBuffer encoderInputBuffer = audioCodec.getInputBuffer(audioInputBufferIndex);
encoderInputBuffer.clear();
encoderInputBuffer.put(audioArray);
audioCodec.queueInputBuffer(audioInputBufferIndex, 0, audioArray.length,
getPresentationTimeUsec(addedFrameCount), 0);
}
addedFrameCount++;
while (addedFrameCount > encodedFrameCount) {
encode();
}
}
}
private void encode() {
encodeVideo();
encodeAudio();
}
private void encodeAudio() {
int audioStatus = audioCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
Timber.i("Audio encoderStatus = " + audioStatus + ", presentationTimeUs = "
+ bufferInfo.presentationTimeUs);
if (audioStatus == INFO_OUTPUT_FORMAT_CHANGED) {
MediaFormat audioFormat = audioCodec.getOutputFormat();
Timber.i("output format changed. audio format: %s", audioFormat.toString());
audioTrackIndex = mediaMuxer.addTrack(audioFormat);
trackCount++;
if (trackCount == 2) {
Timber.i("started media muxer.");
mediaMuxer.start();
isMuxerStarted = true;
}
} else if (audioStatus == INFO_TRY_AGAIN_LATER) {
Timber.d("no output from audio encoder available");
} else {
ByteBuffer audioData = audioCodec.getOutputBuffer(audioStatus);
if (audioData != null) {
audioData.position(bufferInfo.offset);
audioData.limit(bufferInfo.offset + bufferInfo.size);
if (isMuxerStarted) {
mediaMuxer.writeSampleData(audioTrackIndex, audioData, bufferInfo);
}
audioCodec.releaseOutputBuffer(audioStatus, false);
}
}
}
private void encodeVideo() {
int encoderStatus = videoCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_US);
Timber.i("Video encoderStatus = " + encoderStatus + ", presentationTimeUs = "
+ bufferInfo.presentationTimeUs);
if (encoderStatus == INFO_OUTPUT_FORMAT_CHANGED) {
MediaFormat videoFormat = videoCodec.getOutputFormat();
Timber.i("output format changed. video format: %s", videoFormat.toString());
videoTrackIndex = mediaMuxer.addTrack(videoFormat);
trackCount++;
if (trackCount == 2) {
Timber.i("started media muxer.");
mediaMuxer.start();
isMuxerStarted = true;
}
} else if (encoderStatus == INFO_TRY_AGAIN_LATER) {
Timber.d("no output from video encoder available");
} else {
ByteBuffer encodedData = videoCodec.getOutputBuffer(encoderStatus);
if (encodedData != null) {
encodedData.position(bufferInfo.offset);
encodedData.limit(bufferInfo.offset + bufferInfo.size);
if (isMuxerStarted) {
mediaMuxer.writeSampleData(videoTrackIndex, encodedData, bufferInfo);
}
videoCodec.releaseOutputBuffer(encoderStatus, false);
encodedFrameCount++;
}
Timber.i("encoderOutputBuffer " + encoderStatus + " was null");
}
}
private static long getPresentationTimeUsec(int frameIndex) {
return (((long) frameIndex) * ONE_SEC) / 20;
}
private byte[] getNV12(int inputWidth, int inputHeight, Bitmap scaled) {
int[] argb = new int[(inputWidth * inputHeight)];
scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
byte[] yuv = new byte[(((inputWidth * inputHeight) * 3) / 2)];
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
scaled.recycle();
return yuv;
}
private void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
int yIndex = 0;
int uvIndex = width * height;
int index = 0;
int j = 0;
while (j < height) {
int uvIndex2;
int yIndex2;
int i = 0;
while (true) {
uvIndex2 = uvIndex;
yIndex2 = yIndex;
if (i >= width) {
break;
}
int R = (argb[index] & 0xFF0000) >> 16;
int G = (argb[index] & 0xFF00) >> 8;
int B = (argb[index] & 0x0000FF) >> 0;
int Y = ((((R * 77) + (G * 150)) + (B * 29)) + 128) >> 8;
int V = (((((R * -43) - (G * 84)) + (B * 127)) + 128) >> 8) + 128;
int U = (((((R * 127) - (G * 106)) - (B * 21)) + 128) >> 8) + 128;
yIndex = yIndex2 + 1;
if (Y < 0) {
Y = 0;
} else if (Y > 255) {
Y = 255;
}
yuv420sp[yIndex2] = (byte) Y;
if (j % 2 == 0 && index % 2 == 0) {
uvIndex = uvIndex2 + 1;
if (V < 0) {
V = 0;
} else if (V > 255) {
V = 255;
}
yuv420sp[uvIndex2] = (byte) V;
uvIndex2 = uvIndex + 1;
if (U < 0) {
U = 0;
} else if (U > 255) {
U = 255;
}
yuv420sp[uvIndex] = (byte) U;
}
uvIndex = uvIndex2;
index++;
i++;
}
j++;
uvIndex = uvIndex2;
yIndex = yIndex2;
}
}
}