我正在尝试将 MediaCodec 用于三星平板电脑 GT-P5100 上的硬件编码器。但我只能得到几帧。预览可以得到恒定的视频信息并显示出来,但在几帧之后编码器功能出现问题。
这是日志
11-21 11:06:47.746: V/TAG(2869): FRAME NUM145
11-21 11:06:47.746: V/myCamera(2869): encodec2 called
11-21 11:06:47.746: E/TAG(2869): exception in encode2 msg=null
11-21 11:06:47.753: D/dalvikvm(2869): GC_CONCURRENT freed 17K, 51% free 3817K/7696K, paused 2ms+1ms, total 21ms
11-21 11:06:47.800: D/dalvikvm(2869): GC_FOR_ALLOC freed 1350K, 68% free 2467K/7696K, paused 13ms, total 14ms
11-21 11:06:47.800: I/dalvikvm-heap(2869): Grow heap (frag case) to 4.453MB for 1382416-byte allocation
11-21 11:06:47.816: V/TAG(2869): FRAME NUM146
11-21 11:06:47.816: V/myCamera(2869): encodec2 called
11-21 11:06:47.816: E/TAG(2869): exception in encode2 msg=null
11-21 11:06:47.824: D/dalvikvm(2869): GC_CONCURRENT freed 17K, 51% free 3817K/7696K, paused 3ms+2ms, total 22ms
11-21 11:06:47.871: D/dalvikvm(2869): GC_FOR_ALLOC freed 1350K, 68% free 2467K/7696K, paused 13ms, total 13ms
11-21 11:06:47.878: I/dalvikvm-heap(2869): Grow heap (frag case) to 4.453MB for 1382416-byte allocation
我的部分代码如下:
codec = MediaCodec.createEncoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 1280, 720);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 125000);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 15);
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_TI_FormatYUV420PackedSemiPlanar );
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);
codec.configure(mediaFormat, null /* surface */, null /* crypto */, MediaCodec.CONFIGURE_FLAG_ENCODE /* flags */);
codec.start();
和编码器功能:
public void encodec2(File file,byte[] src)
{
Log.v("myCamera", "encodec2 called");
int len = 0;
try{
// TODO Auto-generated method stub
ByteBuffer[] inputBuffer ;
ByteBuffer[] outputBuffer;
inputBuffer = HWEncoder.codec.getInputBuffers();
outputBuffer = HWEncoder.codec.getOutputBuffers();
int indexInput;
int indexOutput = 0;
BufferInfo info = new BufferInfo();
byte[] inData = src;// = buffer.poll();
indexInput = HWEncoder.codec.dequeueInputBuffer(-1);
Log.v("myCamera", "encodec2 Input: "+String.valueOf(indexInput));
if(indexInput >= 0){
int xxx = inData.length;
inputBuffer[indexInput].clear();
inputBuffer[indexInput].put(inData);
HWEncoder.codec.queueInputBuffer(indexInput, 0, inData.length, 0, MediaCodec. BUFFER_FLAG_SYNC_FRAME);
// codec.queueInputBuffer(indexInput, 0, inData.length, 0, MediaCodec.BUFFER_FLAG_CODEC_CONFIG);
//inData = null;
}
indexOutput = HWEncoder.codec.dequeueOutputBuffer(info, 0);
if(indexOutput >= 0){
indexOut++;
}else{
// Log.e(TAG,"empty out frame numOut = "+indexOut);
}
Log.v("myCamera", "encodec2 Output: "+String.valueOf(indexOutput));
while(indexOutput >= 0){
//get a nalu
int size_buf = info.size;
byte [] h264 = new byte[size_buf];
outputBuffer[indexOutput].get(h264);
int kkk = h264.length;
Log.v("myCamera", "encodec2 Output size: "+String.valueOf(kkk));
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file,true);
fos.write(h264);
fos.flush();
fos.close();
}catch (Exception ex) {
Log.v("System.out", ex.toString());
}
len += info.size;
HWEncoder.codec.releaseOutputBuffer(indexOutput, false);
indexOutput = HWEncoder.codec.dequeueOutputBuffer(info,100);
}
}catch(Exception e){
Log.e("TAG","exception in encode2 msg="+e.getMessage()+"");
}
}
有人知道吗?谢谢!