gpt4 book ai didi

java - Android MediaCodec 的 MediaCodecBufferedOutput 索引为负

转载 作者:行者123 更新时间:2023-12-01 09:20:41 24 4
gpt4 key购买 nike

下面是一个代码 fragment ,它应该从图像文件夹中读取所有图像并将它们编码为 h.262 视频并存储在 SD 卡中。我遵循android文档(获取缓冲区,填充缓冲区,用于编码的队列缓冲区,使输出缓冲区出列然后写入文件)。问题是当我使输出缓冲区出列时,我得到负索引,而它应该返回输出数据的索引。输出文件为 0 字节,未写入任何内容。我对 mediaCodec 非常陌生。任何建议将不胜感激。

MediaCodec mediaCodec=null;
byte[] input = new byte[2000];
BufferedOutputStream outputStream = null;

try {
//TODO
//adjust parameters by consulting with hari sir

mediaCodec = MediaCodec.createEncoderByType("video/avc");
MediaFormat mediaFormat = MediaFormat.createVideoFormat("video/avc", 320, 240);
mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, 700000);
mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, 10);

//not all phones support given color format, if color format is not supported app will crash with mediaCodec exception
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar);
mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 5);

mediaCodec.configure(mediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);


mediaCodec.start();
//after the mediaCodec is started we don't have ownership of input or output buffers

Log.i("Codecinfo",""+mediaCodec.getCodecInfo());
Log.i("Codecname",""+mediaCodec.getName());


}
catch (Exception e)
{
Log.e("ExceptionMediaCodec","Some exception in media codec");
}

//reached here
System.out.println("mediacodec info="+mediaCodec.getCodecInfo());


try {
File ff = new File(Environment.getExternalStorageDirectory(), "Download/video_encoded.264");
if (!ff.exists()) ff.createNewFile();
System.out.println("H.264 output file initialized");

outputStream = new BufferedOutputStream(new FileOutputStream(ff));
Log.i("H264 avc Encoder", "outputStream initialized");
} catch (Exception e){
e.printStackTrace();
}


String path = Environment.getExternalStorageDirectory().toString()+"/images";
File f = new File(path);
Log.i("ExternalFileInfo",path.toString());
//read image files onto an array
File[] files = f.listFiles();
System.out.println(files.getClass().getName());
int NUM_IMAGES = files.length;
String[] images = new String[NUM_IMAGES];
for (int i=0;i<NUM_IMAGES;i++)
images[i]=files[i].getName();



for (String eachimage: images) {
System.out.println(eachimage);
byte[] eachByte = eachimage.getBytes();
input = eachByte; //demo
System.out.println("input byte initialized"+input.toString());
try {
System.out.println("Following is the content of byte array input");
System.out.write(input);
}catch (Exception e)
{
e.printStackTrace();
}


}

//reached here
System.out.println("image byte size="+input.length);
//all images converted to bytearray


ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();

//System.out.println("inputBuffers="+(inputBuffers));
//System.out.println("outputBuffers="+(outputBuffers));
//reached here

//returns the index of input buffer to be filled for encoding
int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1); //-1 => wait indefinitely

System.out.println("inputBufferedIndex="+inputBufferIndex); //0

if (inputBufferIndex >= 0) {
ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();

ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];


inputBuffer.clear();

System.out.println("input byte placed in input buffer");
inputBuffer.put(input);



System.out.println("inputBuffer after filling up" + inputBuffer);


mediaCodec.queueInputBuffer(inputBufferIndex, 0, input.length, System.nanoTime(), 0); //send each request with different timestamp


System.out.println("mediacodec input queued");

}

MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
System.out.println("buffer info="+bufferInfo);

int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, -1); //-ve value for indefinite waiting

//reached here
System.out.println("buffer info meta data=" + bufferInfo);


System.out.println("outputBufferedIndex=" + outputBufferIndex);

try {
while (outputBufferIndex >= 0) {
ByteBuffer outputBuffer = outputBuffers[outputBufferIndex];
byte[] outData = new byte[bufferInfo.size];
outputBuffer.get(outData);
outputStream.write(outData, 0, outData.length);
outputStream.flush();
Log.i("AvcEncoder", outData.length + " bytes written");

mediaCodec.releaseOutputBuffer(outputBufferIndex, false);
outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, -1);

}
} catch (Throwable t) {
t.printStackTrace();
}


try {
mediaCodec.stop();
mediaCodec.release();

outputStream.flush();
outputStream.close();
} catch (Exception e){
e.printStackTrace();
}

System.out.println("Mediacodec="+mediaCodec);

最佳答案

请参阅 MediaCodec.dequeueOutputBuffer() 方法的文档,其中显示:

Returns the index of an output buffer that has been successfully decoded or one of the INFO_* constants.

负值是 INFO_* 常量,可能是以下之一:

  • INFO_OUTPUT_BUFFERS_CHANGED
  • INFO_OUTPUT_FORMAT_CHANGED
  • INFO_TRY_AGAIN_LATER

最后一个可能性不大,因为你要无限期地等待。

此外:在将一个缓冲区作为输入后,您不能总是依赖于等待一个输出缓冲区。只要编码器有空闲的输入缓冲区,您就需要提供输入缓冲区,并消耗它为您提供的任何输出缓冲区。

仅当您通过设置标志 BUFFER_FLAG_END_OF_STREAM 表示不再提交任何输入缓冲区时,才会输出最后几个输出缓冲区。

关于java - Android MediaCodec 的 MediaCodecBufferedOutput 索引为负,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40182551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com