android 硬體解碼學習

來源:互聯網
上載者:User

標籤:android   io   ar   os   sp   for   on   art   log   

FileInputStream in = new FileInputStream("/sdcard/sample.ts");String mimeType = "video/avc";MediaCodec decoder = MediaCodec.createDecoderByType(mimeType);MediaFormat format = MediaFormat.createVideoFormat(mimeType, 1920, 1080);byte[] header_sps = { 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 };byte[] header_pps = { 0, 0, 0, 1, 104, -18, 60, -128 };format.setByteBuffer("csd-0", ByteBuffer.wrap(header_sps));format.setByteBuffer("csd-1", ByteBuffer.wrap(header_pps));format.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, 1920 * 1080);format.setInteger("durationUs", 63446722);decoder.configure(format, surface, null, 0);decoder.start();ByteBuffer[] inputBuffers = decoder.getInputBuffers();ByteBuffer[] outputBuffers = decoder.getOutputBuffers();BufferInfo info = new BufferInfo();boolean isEOS = false;long startMs = System.currentTimeMillis();while (!Thread.interrupted()) {    if (!isEOS) {        int inIndex = decoder.dequeueInputBuffer(1000);        if (inIndex >= 0) {            byte buffer2[] = new byte[18800 * 8 * 8 * 8];            ByteBuffer buffer = inputBuffers[inIndex];            int sampleSize;            sampleSize = in.read(buffer2, 0, 18800 * 4);            buffer.clear();            buffer.put(buffer2, 0, sampleSize);            buffer.clear();            if (sampleSize < 0) {                decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);                isEOS = true;            } else {                decoder.queueInputBuffer(inIndex, 0, sampleSize, 0, 0);            }        }    }    int outIndex = decoder.dequeueOutputBuffer(info, 10000);    switch (outIndex) {    case MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED:        Log.d("DecodeActivity", "INFO_OUTPUT_BUFFERS_CHANGED");        outputBuffers = decoder.getOutputBuffers();        break;    case MediaCodec.INFO_OUTPUT_FORMAT_CHANGED:        Log.d("DecodeActivity", "New format " + decoder.getOutputFormat());        break;    case MediaCodec.INFO_TRY_AGAIN_LATER:        Log.d("DecodeActivity", "dequeueOutputBuffer timed out! " + info);        break;    default:        ByteBuffer buffer = outputBuffers[outIndex];        Log.v("DecodeActivity", "We can‘t use this buffer but render it due to the API limit, " + buffer);        while (info.presentationTimeUs / 1000 > System.currentTimeMillis() - startMs) {            try {                sleep(10);            } catch (InterruptedException e) {                e.printStackTrace();                break;            }        }        decoder.releaseOutputBuffer(outIndex, true);        break;    }    if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) {        Log.d("DecodeActivity", "OutputBuffer BUFFER_FLAG_END_OF_STREAM");        break;    }}decoder.stop();decoder.release();

android 硬體解碼學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.