forked from Denislyl/AndroidMediaCodec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvcEncoder.java
202 lines (179 loc) · 6.59 KB
/
AvcEncoder.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
package com.avc.demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import android.annotation.SuppressLint;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.util.Log;
@SuppressLint("NewApi") public class AvcEncoder {
private MediaCodec mediaCodec;
int m_width;
int m_height;
//boolean RecordEncDataFlag = true;
boolean RecordEncDataFlag = false;
byte[] m_info = null;
FileOutputStream FileOut = null;
private byte[] yuv420 = null;
public AvcEncoder(int width, int height, int framerate, int bitrate) {
Log.d("Codec", "AvcEncoder IN");
m_width = width;
m_height = height;
yuv420 = new byte[width*height*3/2];
getSupportColorFormat();
try {
mediaCodec = MediaCodec.createEncoderByType("video/avc");
} catch (IOException e) {
e.printStackTrace();
}
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", width, height);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, framerate);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 30);
mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
mediaCodec.start();
if(RecordEncDataFlag)
{
try {
FileOut = new FileOutputStream(new File("/sdcard/app_camera_enc.h264"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private int getSupportColorFormat() {
int numCodecs = MediaCodecList.getCodecCount();
MediaCodecInfo codecInfo = null;
for (int i = 0; i < numCodecs && codecInfo == null; i++) {
MediaCodecInfo info = MediaCodecList.getCodecInfoAt(i);
if (!info.isEncoder()) {
continue;
}
String[] types = info.getSupportedTypes();
boolean found = false;
for (int j = 0; j < types.length && !found; j++) {
if (types[j].equals("video/avc")) {
System.out.println("found");
found = true;
}
}
if (!found)
continue;
codecInfo = info;
}
Log.e("AvcEncoder", "Found " + codecInfo.getName() + " supporting " + "video/avc");
// Find a color profile that the codec supports
MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType("video/avc");
Log.e("AvcEncoder",
"length-" + capabilities.colorFormats.length + "==" + Arrays.toString(capabilities.colorFormats));
for (int i = 0; i < capabilities.colorFormats.length; i++) {
switch (capabilities.colorFormats[i]) {
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar:
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar:
case MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible:
Log.e("AvcEncoder", "supported color format::" + capabilities.colorFormats[i]);
break;//return capabilities.colorFormats[i];
default:
Log.e("AvcEncoder", "other color format " + capabilities.colorFormats[i]);
break;
}
}
return 0;
}
public void close() {
try {
mediaCodec.stop();
mediaCodec.release();
} catch (Exception e){
e.printStackTrace();
}
try {
FileOut.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int offerEncoder(byte[] input, byte[] output)
{
Log.d("......................................................Codec", "Encoder in");
int pos = 0;
yuv420= input;
try {
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
Log.d("......................................................Codec", "inputBufferIndex = " +inputBufferIndex);
if (inputBufferIndex >= 0)
{
ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
inputBuffer.clear();
inputBuffer.put(yuv420);
mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, 0, 0);
}
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo,0);
Log.d("......................................................Codec", "outputBufferIndex = " +outputBufferIndex);
while (outputBufferIndex >= 0)
{
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
if(m_info != null)
{
System.arraycopy(outData, 0, output, 0, outData.length);
pos += outData.length;
Log.d("Encoder", "m_info: " + pos);
}
else
{
ByteBuffer spsPpsBuffer = ByteBuffer.wrap(outData);
//if (spsPpsBuffer.getInt() == 0x00000001)
if(bufferInfo.flags == 2)
{
m_info = new byte[outData.length];
System.arraycopy(outData, 0, m_info, 0, outData.length);
System.arraycopy(outData, 0, output, pos, outData.length);
pos+=outData.length;
}
else
{
Log.d("Encoder", "errrrr: ");
return -1;
}
Log.d("Encoder", "m_info: " + Arrays.toString(m_info));
}
mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
}
if(bufferInfo.flags == 1)// if( nv12[4] == 0x65) //key frame
{
Log.d("Encoder", "Key frame");
System.arraycopy(output, 0, yuv420, 0, pos);
System.arraycopy(m_info, 0, output, 0, m_info.length);
System.arraycopy(yuv420, 0, output, m_info.length, pos);
pos += m_info.length;
}
} catch (Throwable t) {
t.printStackTrace();
}
//Log.d("......................................................Codec", "Encoder out");
// if(RecordEncDataFlag)
// {
// try {
// FileOut.write(nv12,0,pos);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
return pos;
}
}