8

Trying to use the media codec to decode the encoded data gram packets but always gets -1 in dequeueOutputBuffer().

Please help me-what I'm doing wrong? I'm trying to face this problem for days without success.

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();
}

Thank you very much for giving your time.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
dvrm
  • 3,749
  • 4
  • 34
  • 43
  • Could be that it doesn't like the input it's receiving. Do you see anything suspicious in the logcat output? – fadden Jul 09 '14 at 16:44

0 Answers0