在Android上解码h264 ByteStream总是从dequeueOutputBuffer()获取INFO_TRY_AGAIN_LATER

时间:2014-07-09 11:34:13

标签: android decode h.264 mediacodec

尝试使用媒体编解码器解码编码数据克数据包,但在dequeueOutputBuffer()中始终获得-1。

请帮助我 - 我做错了什么?我试图在没有成功的情况下面对这个问题。

private void decodeVideo(){

    new Thread(new Runnable() {

        @Override
        public void run() {
            int n = 0;
            MediaFormat mediaFormat = new MediaFormat();
            mediaFormat.setString(MediaFormat.KEY_MIME, "video/avc");
            mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 100000);
            mediaFormat.setInteger(MediaFormat.KEY_WIDTH,
                    surfaceView.getWidth());
            mediaFormat.setInteger(MediaFormat.KEY_HEIGHT,
                    surfaceView.getHeight());
            mediaFormat.setInteger(
                    MediaFormat.KEY_PUSH_BLANK_BUFFERS_ON_STOP, 1);
            mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT,
                    MediaCodecInfo.CodecCapabilities.COLOR_FormatL2);// TODO

            byte[] csd_info = { 0, 0, 0, 1, 103, 100, 0, 40, -84, 52, -59,
                    1, -32, 17, 31, 120, 11, 80, 16, 16, 31, 0, 0, 3, 3,
                    -23, 0, 0, -22, 96, -108, 0, 0, 0, 1, 104, -18, 60,
                    -128 };
            mediaFormat.setByteBuffer("csd-0", ByteBuffer.wrap(csd_info));
            mediaFormat.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE,
                    1920 * 1080);
            mediaFormat.setInteger("durationUs", 63446722);

            MediaCodec codec = MediaCodec.createDecoderByType("video/avc");
            codec.configure(mediaFormat, mHolder.getSurface(), null, 0);
            codec.start();

            ByteBuffer[] inputBuffers = codec.getInputBuffers();
            ByteBuffer[] outputBuffers = codec.getOutputBuffers();
            BufferInfo info = new BufferInfo();
            while (flag) {
                int inputBufferIndex = codec.dequeueInputBuffer(-1);
                if (inputBufferIndex >= 0) {
                    while (mPackets.size() <= 0) {
                        try {
                            Log.d(TAG, "nopackets");
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Log.d(TAG, "now I have packets!");
                    inputBuffers[inputBufferIndex].clear();
                    DatagramPacket currentDatagram = mPackets.remove();
                    byte[] byteBuffer = (ByteBuffer.wrap(currentDatagram
                            .getData())).array();
                    inputBuffers[inputBufferIndex].put(byteBuffer, 0,
                            byteBuffer.length);
                    codec.queueInputBuffer(inputBufferIndex, 0,
                            byteBuffer.length, timestamp(), 0);
                }
                int outputBufferIndex = codec.dequeueOutputBuffer(info,-1);//always get-1
                if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
                    outputBuffers = codec.getOutputBuffers();
                    Log.d(TAG, "INFO_OUTPUT_BUFFERS_CHANGED");
                } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
                    // Subsequent data will conform to new format.
                    mediaFormat = codec.getOutputFormat();
                    Log.d(TAG, "INFO_OUTPUT_FORMAT_CHANGED");
                } else if (outputBufferIndex >= 0) {
                    Log.d(TAG, "outputBufferIndex>=0");
                    if (n > 2) {
                        Log.d(TAG, "n>2");
                        // We have successfully encoded and decoded an image
                        // !
                        int length = info.size;
                        codec.releaseOutputBuffer(outputBufferIndex, false);
                        n++;
                    }

                }

            }
            codec.stop();
            codec.release();
            codec = null;
        }
    }).start();
}

非常感谢你抽出时间。

0 个答案:

没有答案