|
@@ -0,0 +1,199 @@
|
|
1
|
+package com.cfmlg.mlg;
|
|
2
|
+
|
|
3
|
+import android.media.MediaCodec;
|
|
4
|
+import android.media.MediaCodecInfo;
|
|
5
|
+import android.media.MediaFormat;
|
|
6
|
+import android.os.Environment;
|
|
7
|
+
|
|
8
|
+import java.io.BufferedOutputStream;
|
|
9
|
+import java.io.File;
|
|
10
|
+import java.io.FileOutputStream;
|
|
11
|
+import java.io.IOException;
|
|
12
|
+import java.nio.ByteBuffer;
|
|
13
|
+
|
|
14
|
+import static android.media.MediaCodec.BUFFER_FLAG_CODEC_CONFIG;
|
|
15
|
+import static android.media.MediaCodec.BUFFER_FLAG_KEY_FRAME;
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+public class AvcEncoder
|
|
19
|
+{
|
|
20
|
+ private final static String TAG = "MeidaCodec";
|
|
21
|
+
|
|
22
|
+ private int TIMEOUT_USEC = 12000;
|
|
23
|
+
|
|
24
|
+ private MediaCodec mediaCodec;
|
|
25
|
+ int m_width;
|
|
26
|
+ int m_height;
|
|
27
|
+ int m_framerate;
|
|
28
|
+
|
|
29
|
+ public byte[] configbyte;
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+ public AvcEncoder(int width, int height, int framerate, int bitrate) {
|
|
33
|
+
|
|
34
|
+ m_width = width;
|
|
35
|
+ m_height = height;
|
|
36
|
+ m_framerate = framerate;
|
|
37
|
+ MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", width, height);
|
|
38
|
+ mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar);
|
|
39
|
+ mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, width*height*5);
|
|
40
|
+ mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 30);
|
|
41
|
+ mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1);
|
|
42
|
+ try {
|
|
43
|
+ mediaCodec = MediaCodec.createEncoderByType("video/avc");
|
|
44
|
+ } catch (IOException e) {
|
|
45
|
+ e.printStackTrace();
|
|
46
|
+ }
|
|
47
|
+ //配置编码器参数
|
|
48
|
+ mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
|
|
49
|
+ //启动编码器
|
|
50
|
+ mediaCodec.start();
|
|
51
|
+ //创建保存编码后数据的文件
|
|
52
|
+ createfile();
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ private static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test1.h264";
|
|
56
|
+ private BufferedOutputStream outputStream;
|
|
57
|
+
|
|
58
|
+ private void createfile(){
|
|
59
|
+ File file = new File(path);
|
|
60
|
+ if(file.exists()){
|
|
61
|
+ file.delete();
|
|
62
|
+ }
|
|
63
|
+ try {
|
|
64
|
+ outputStream = new BufferedOutputStream(new FileOutputStream(file));
|
|
65
|
+ } catch (Exception e){
|
|
66
|
+ e.printStackTrace();
|
|
67
|
+ }
|
|
68
|
+ }
|
|
69
|
+
|
|
70
|
+ private void StopEncoder() {
|
|
71
|
+ try {
|
|
72
|
+ mediaCodec.stop();
|
|
73
|
+ mediaCodec.release();
|
|
74
|
+ } catch (Exception e){
|
|
75
|
+ e.printStackTrace();
|
|
76
|
+ }
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public boolean isRuning = false;
|
|
80
|
+
|
|
81
|
+ public void StopThread(){
|
|
82
|
+ isRuning = false;
|
|
83
|
+ try {
|
|
84
|
+ StopEncoder();
|
|
85
|
+ outputStream.flush();
|
|
86
|
+ outputStream.close();
|
|
87
|
+ } catch (IOException e) {
|
|
88
|
+ e.printStackTrace();
|
|
89
|
+ }
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ int count = 0;
|
|
93
|
+
|
|
94
|
+ public void StartEncoderThread(){
|
|
95
|
+ Thread EncoderThread = new Thread(new Runnable() {
|
|
96
|
+
|
|
97
|
+ @Override
|
|
98
|
+ public void run() {
|
|
99
|
+ isRuning = true;
|
|
100
|
+ byte[] input = null;
|
|
101
|
+ long pts = 0;
|
|
102
|
+ long generateIndex = 0;
|
|
103
|
+
|
|
104
|
+ while (isRuning) {
|
|
105
|
+ //访问MainActivity用来缓冲待解码数据的队列
|
|
106
|
+ if (GatherActivity.YUVQueue.size() >0){
|
|
107
|
+ //从缓冲队列中取出一帧
|
|
108
|
+ input = GatherActivity.YUVQueue.poll();
|
|
109
|
+ byte[] yuv420sp = new byte[m_width*m_height*3/2];
|
|
110
|
+ //把待编码的视频帧转换为YUV420格式
|
|
111
|
+ NV21ToNV12(input,yuv420sp,m_width,m_height);
|
|
112
|
+ input = yuv420sp;
|
|
113
|
+ }
|
|
114
|
+ if (input != null) {
|
|
115
|
+ try {
|
|
116
|
+ long startMs = System.currentTimeMillis();
|
|
117
|
+ //编码器输入缓冲区
|
|
118
|
+ ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
|
|
119
|
+ //编码器输出缓冲区
|
|
120
|
+ ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
|
|
121
|
+ int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
|
|
122
|
+ if (inputBufferIndex >= 0) {
|
|
123
|
+ pts = computePresentationTime(generateIndex);
|
|
124
|
+ ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
|
|
125
|
+ inputBuffer.clear();
|
|
126
|
+ //把转换后的YUV420格式的视频帧放到编码器输入缓冲区中
|
|
127
|
+ inputBuffer.put(input);
|
|
128
|
+ mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, pts, 0);
|
|
129
|
+ generateIndex += 1;
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
|
|
133
|
+ int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_USEC);
|
|
134
|
+ while (outputBufferIndex >= 0) {
|
|
135
|
+ //Log.i("AvcEncoder", "Get H264 Buffer Success! flag = "+bufferInfo.flags+",pts = "+bufferInfo.presentationTimeUs+"");
|
|
136
|
+ ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
|
|
137
|
+ byte[] outData = new byte[bufferInfo.size];
|
|
138
|
+ outputBuffer.get(outData);
|
|
139
|
+ if(bufferInfo.flags == BUFFER_FLAG_CODEC_CONFIG){
|
|
140
|
+ configbyte = new byte[bufferInfo.size];
|
|
141
|
+ configbyte = outData;
|
|
142
|
+ }else if(bufferInfo.flags == BUFFER_FLAG_KEY_FRAME){
|
|
143
|
+ byte[] keyframe = new byte[bufferInfo.size + configbyte.length];
|
|
144
|
+ System.arraycopy(configbyte, 0, keyframe, 0, configbyte.length);
|
|
145
|
+ //把编码后的视频帧从编码器输出缓冲区中拷贝出来
|
|
146
|
+ System.arraycopy(outData, 0, keyframe, configbyte.length, outData.length);
|
|
147
|
+
|
|
148
|
+ outputStream.write(keyframe, 0, keyframe.length);
|
|
149
|
+ }else{
|
|
150
|
+ //写到文件中
|
|
151
|
+ outputStream.write(outData, 0, outData.length);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
|
|
155
|
+ outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, TIMEOUT_USEC);
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ } catch (Throwable t) {
|
|
159
|
+ t.printStackTrace();
|
|
160
|
+ }
|
|
161
|
+ } else {
|
|
162
|
+ try {
|
|
163
|
+ Thread.sleep(500);
|
|
164
|
+ } catch (InterruptedException e) {
|
|
165
|
+ e.printStackTrace();
|
|
166
|
+ }
|
|
167
|
+ }
|
|
168
|
+ }
|
|
169
|
+ }
|
|
170
|
+ });
|
|
171
|
+ EncoderThread.start();
|
|
172
|
+
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ private void NV21ToNV12(byte[] nv21,byte[] nv12,int width,int height){
|
|
176
|
+ if(nv21 == null || nv12 == null)return;
|
|
177
|
+ int framesize = width*height;
|
|
178
|
+ int i = 0,j = 0;
|
|
179
|
+ System.arraycopy(nv21, 0, nv12, 0, framesize);
|
|
180
|
+ for(i = 0; i < framesize; i++){
|
|
181
|
+ nv12[i] = nv21[i];
|
|
182
|
+ }
|
|
183
|
+ for (j = 0; j < framesize/2; j+=2)
|
|
184
|
+ {
|
|
185
|
+ nv12[framesize + j-1] = nv21[j+framesize];
|
|
186
|
+ }
|
|
187
|
+ for (j = 0; j < framesize/2; j+=2)
|
|
188
|
+ {
|
|
189
|
+ nv12[framesize + j] = nv21[j+framesize-1];
|
|
190
|
+ }
|
|
191
|
+ }
|
|
192
|
+
|
|
193
|
+ /**
|
|
194
|
+ * Generates the presentation time for frame N, in microseconds.
|
|
195
|
+ */
|
|
196
|
+ private long computePresentationTime(long frameIndex) {
|
|
197
|
+ return 132 + frameIndex * 1000000 / m_framerate;
|
|
198
|
+ }
|
|
199
|
+}
|