Android硬編解碼接口MediaCodec使用徹底解析(一)

本文概述

MediaCodec是anroid api 16之後開發的硬編解碼接口,英文文檔參照這個連接,中文翻譯能夠參考這個連接。本文主要記錄的是如何使用MediaCodec對視頻進行編解碼,最後會以實例的方式展現如何將Camera預覽數據編碼成H264,再把編碼後的h264解碼而且顯示在SurfaceView中。本例不涉及音頻的編解碼。html

一、MediaCodec編碼視頻

使用MediaCodec實現視頻編碼的步驟以下: 
1.初始化MediaCodec,方法有兩種,分別是經過名稱和類型來建立,對應的方法爲:java

MediaCodec createByCodecName (String name); MediaCodec createDecoderByType (String type);
  • 1
  • 2

具體可用的name和type參考文檔便可。這裏咱們經過後者來初始化一個視頻編碼器。android

mMC = MediaCodec.createDecoderByType(MIME_TYPE);
  • 1

2.配置MediaCodec,這一步須要配置的是MediaFormat,這個類包含了比特率、幀率、關鍵幀間隔時間等,其中比特率若是過低就會形成相似馬賽克的現象。api

mMF = MediaFormat.createVideoFormat(MIME_TYPE, width, height); mMF.setInteger(MediaFormat.KEY_BIT_RATE, bitrate); mMF.setInteger(MediaFormat.KEY_FRAME_RATE, framerate); if (mPrimeColorFormat != 0){ mMF.setInteger(MediaFormat.KEY_COLOR_FORMAT, mPrimeColorFormat); } mMF.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, 1); //關鍵幀間隔時間 單位s mMC.configure(mMF, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其中mPrimeColorFormat爲本機支持的顏色空間。通常是yuv420p或者yuv420sp,Camera預覽格式通常是yv12或者NV21,因此在編碼以前須要進行格式轉換,實例可參照文末代碼。代碼是最好的老師嘛。 
3.打開編碼器,獲取輸入輸出緩衝區數組

mMC.start(); mInputBuffers = mMC.getInputBuffers(); mOutputBuffers = mMC.getOutputBuffers();
  • 1
  • 2
  • 3

4.輸入數據,過程能夠分爲如下幾個小步: 
1)獲取可以使用緩衝區位置獲得索引緩存

int inputbufferindex = mMC.dequeueInputBuffer(BUFFER_TIMEOUT);
  • 1

若是存在可用的緩衝區,此方法會返回其位置索引,不然返回-1,參數爲超時時間,單位是毫秒,若是此參數是0,則當即返回,若是參數小於0,則無限等待直到有可以使用的緩衝區,若是參數大於0,則等待時間爲傳入的毫秒值。 
2)傳入原始數據bash

ByteBuffer inputBuffer = mInputBuffers[inputbufferindex];
inputBuffer.clear();//清除原來的內容以接收新的內容 inputBuffer.put(bytes, 0, len);//len是傳進來的有效數據長度 mMC.queueInputBuffer(inputbufferindex, 0, len, timestamp, 0);
  • 1
  • 2
  • 3
  • 4

此緩衝區一旦使用,只有在dequeueInputBuffer返回其索引位置才表明它能夠再次使用。 
5.獲取其輸出數據,獲取輸入原始數據和獲取輸出數據最好是異步進行,由於輸入一幀數據不表明編碼器立刻就會輸出對應的編碼數據,可能輸入好幾幀纔會輸出一幀。獲取輸出數據的步驟與輸入數據的步驟類似: 
1)獲取可用的輸出緩衝區app

int outputbufferindex = mMC.dequeueOutputBuffer(mBI, BUFFER_TIMEOUT);
  • 1

其中參數一是一個BufferInfo類型的實例,參數二爲超時時間,負數表明無限等待(可見,不要在主線程進行操做)。 
2)獲取輸出數據異步

mOutputBuffers[outputbufferindex].get(bytes, 0, mBI.size);
  • 1

3)釋放緩衝區socket

mMC.releaseOutputBuffer(outputbufferindex, false);
  • 1

二、MediaCodec解碼視頻

解碼視頻的步驟跟編碼的相似,配置不同: 
1.實例化解碼器

mMC = MediaCodec.createDecoderByType(MIME_TYPE);
  • 1

2.配置解碼器,此處須要配置用於顯示圖像的Surface、MediaFormat包含視頻的pps和sps(包含在編碼出來的第一幀數據)

int[] width = new int[1]; int[] height = new int[1]; AvcUtils.parseSPS(sps, width, height);//從sps中解析出視頻寬高 mMF = MediaFormat.createVideoFormat(MIME_TYPE, width[0], height[0]); mMF.setByteBuffer("csd-0", ByteBuffer.wrap(sps)); mMF.setByteBuffer("csd-1", ByteBuffer.wrap(pps)); mMF.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, width[0] * height[0]); mMC.configure(mMF, surface, null, 0);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3.開啓編碼器並獲取輸入輸出緩衝區

mMC.start(); mInputBuffers = mMC.getInputBuffers(); mOutputBuffers = mMC.getOutputBuffers();
  • 1
  • 2
  • 3

4.輸入數據 
1)獲取可用的輸入緩衝區

int inputbufferindex = mMC.dequeueInputBuffer(BUFFER_TIMEOUT);
  • 1

返回值爲可用緩衝區的索引

ByteBuffer inputBuffer = mInputBuffers[inputbufferindex]; inputBuffer.clear();
  • 1
  • 2

2)而後輸入數據

inputBuffer.put(bytes, 0, len); mMC.queueInputBuffer(inputbufferindex, 0, len, timestamp, 0);
  • 1
  • 2

5.獲取輸出數據,這一步與4一樣應該異步進行,其具體步驟與上面解碼的基本相同,在釋放緩衝區的時候須要注意第二個參數設置爲true,表示解碼顯示在Surface上

mMC.releaseOutputBuffer(outputbufferindex, true);
  • 1

三、編解碼實例

下面是一個MediaCodec編解碼實例,此例子Camera預覽數據(yv12)編碼成H264,再把編碼後的h264解碼而且顯示在SurfaceView中。

3.1佈局文件

佈局文件很是簡單,兩個SurfaceView分別用於顯示編解碼的圖像,兩個按鈕控制開始和中止,一個TextView用於顯示捕捉幀率。佈局文件代碼就不展現了,界面以下 

3.2編碼器類Encoder

package com.example.mediacodecpro; import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.media.MediaFormat; import android.util.Log; import java.io.IOException; import java.nio.ByteBuffer; /** * Created by chuibai on 2017/3/10.<br /> */ public class Encoder { public static final int TRY_AGAIN_LATER = -1; public static final int BUFFER_OK = 0; public static final int BUFFER_TOO_SMALL = 1; public static final int OUTPUT_UPDATE = 2; private int format = 0; private final String MIME_TYPE = "video/avc"; private MediaCodec mMC = null; private MediaFormat mMF; private ByteBuffer[] inputBuffers; private ByteBuffer[] outputBuffers; private long BUFFER_TIMEOUT = 0; private MediaCodec.BufferInfo mBI; /** * 初始化編碼器 * @throws IOException 建立編碼器失敗會拋出異常 */ public void init() throws IOException { mMC = MediaCodec.createEncoderByType(MIME_TYPE); format = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar; mBI = new MediaCodec.BufferInfo(); } /** * 配置編碼器,須要配置顏色、幀率、比特率以及視頻寬高 * @param width 視頻的寬 * @param height 視頻的高 * @param bitrate 視頻比特率 * @param framerate 視頻幀率 */ public void configure(int width,int height,int bitrate,int framerate){ if(mMF == null){ mMF = MediaFormat.createVideoFormat(MIME_TYPE, width, height); mMF.setInteger(MediaFormat.KEY_BIT_RATE, bitrate); mMF.setInteger(MediaFormat.KEY_FRAME_RATE, framerate); if (format != 0){ mMF.setInteger(MediaFormat.KEY_COLOR_FORMAT, format); } mMF.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, -1); //關鍵幀間隔時間 單位s } mMC.configure(mMF,null,null,MediaCodec.CONFIGURE_FLAG_ENCODE); } /** * 開啓編碼器,獲取輸入輸出緩衝區 */ public void start(){ mMC.start(); inputBuffers = mMC.getInputBuffers(); outputBuffers = mMC.getOutputBuffers(); } /** * 向編碼器輸入數據,此處要求輸入YUV420P的數據 * @param data YUV數據 * @param len 數據長度 * @param timestamp 時間戳 * @return */ public int input(byte[] data,int len,long timestamp){ int index = mMC.dequeueInputBuffer(BUFFER_TIMEOUT); Log.e("...","" + index); if(index >= 0){ ByteBuffer inputBuffer = inputBuffers[index]; inputBuffer.clear(); if(inputBuffer.capacity() < len){ mMC.queueInputBuffer(index, 0, 0, timestamp, 0); return BUFFER_TOO_SMALL; } inputBuffer.put(data,0,len); mMC.queueInputBuffer(index,0,len,timestamp,0); }else{ return index; } return BUFFER_OK; } /** * 輸出編碼後的數據 * @param data 數據 * @param len 有效數據長度 * @param ts 時間戳 * @return */ public int output(/*out*/byte[] data,/* out */int[] len,/* out */long[] ts){ int i = mMC.dequeueOutputBuffer(mBI, BUFFER_TIMEOUT); if(i >= 0){ if(mBI.size > data.length) return BUFFER_TOO_SMALL; outputBuffers[i].position(mBI.offset); outputBuffers[i].limit(mBI.offset + mBI.size); outputBuffers[i].get(data, 0, mBI.size); len[0] = mBI.size ; ts[0] = mBI.presentationTimeUs; mMC.releaseOutputBuffer(i, false); } else if (i == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) { outputBuffers = mMC.getOutputBuffers(); return OUTPUT_UPDATE; } else if (i == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) { mMF = mMC.getOutputFormat(); return OUTPUT_UPDATE; } else if (i == MediaCodec.INFO_TRY_AGAIN_LATER) { return TRY_AGAIN_LATER; } return BUFFER_OK; } public void release(){ mMC.stop(); mMC.release(); mMC = null; outputBuffers = null; inputBuffers = null; } public void flush() { mMC.flush(); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136

3.3解碼器類Decoder

package com.example.mediacodecpro; import android.media.MediaCodec; import android.media.MediaFormat; import android.view.Surface; import java.io.IOException; import java.nio.ByteBuffer; /** * Created by chuibai on 2017/3/10.<br /> */ public class Decoder { public static final int TRY_AGAIN_LATER = -1; public static final int BUFFER_OK = 0; public static final int BUFFER_TOO_SMALL = 1; public static final int OUTPUT_UPDATE = 2; private final String MIME_TYPE = "video/avc"; private MediaCodec mMC = null; private MediaFormat mMF; private long BUFFER_TIMEOUT = 0; private MediaCodec.BufferInfo mBI; private ByteBuffer[] mInputBuffers; private ByteBuffer[] mOutputBuffers; /** * 初始化編碼器 * @throws IOException 建立編碼器失敗會拋出異常 */ public void init() throws IOException { mMC = MediaCodec.createDecoderByType(MIME_TYPE); mBI = new MediaCodec.BufferInfo(); } /** * 配置解碼器 * @param sps 用於配置的sps參數 * @param pps 用於配置的pps參數 * @param surface 用於解碼顯示的Surface */ public void configure(byte[] sps, byte[] pps, Surface surface){ int[] width = new int[1]; int[] height = new int[1]; AvcUtils.parseSPS(sps, width, height);//從sps中解析出視頻寬高 mMF = MediaFormat.createVideoFormat(MIME_TYPE, width[0], height[0]); mMF.setByteBuffer("csd-0", ByteBuffer.wrap(sps)); mMF.setByteBuffer("csd-1", ByteBuffer.wrap(pps)); mMF.setInteger(MediaFormat.KEY_MAX_INPUT_SIZE, width[0] * height[0]); mMC.configure(mMF, surface, null, 0); } /** * 開啓解碼器,獲取輸入輸出緩衝區 */ public void start(){ mMC.start(); mInputBuffers = mMC.getInputBuffers(); mOutputBuffers = mMC.getOutputBuffers(); } /** * 輸入數據 * @param data 輸入的數據 * @param len 數據有效長度 * @param timestamp 時間戳 * @return 成功則返回{@link #BUFFER_OK} 不然返回{@link #TRY_AGAIN_LATER} */ public int input(byte[] data,int len,long timestamp){ int i = mMC.dequeueInputBuffer(BUFFER_TIMEOUT); if(i >= 0){ ByteBuffer inputBuffer = mInputBuffers[i]; inputBuffer.clear(); inputBuffer.put(data, 0, len); mMC.queueInputBuffer(i, 0, len, timestamp, 0); }else { return TRY_AGAIN_LATER; } return BUFFER_OK; } public int output(byte[] data,int[] len,long[] ts){ int i = mMC.dequeueOutputBuffer(mBI, BUFFER_TIMEOUT); if(i >= 0){ if (mOutputBuffers[i] != null) { mOutputBuffers[i].position(mBI.offset); mOutputBuffers[i].limit(mBI.offset + mBI.size); if (data != null) mOutputBuffers[i].get(data, 0, mBI.size); len[0] = mBI.size; ts[0] = mBI.presentationTimeUs; } mMC.releaseOutputBuffer(i, true); }else{ return TRY_AGAIN_LATER; } return BUFFER_OK; } public void flush(){ mMC.flush(); } public void release() { flush(); mMC.stop(); mMC.release(); mMC = null; mInputBuffers = null; mOutputBuffers = null; } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

3.4MainAcitivity

package com.example.mediacodecpro; import android.content.pm.ActivityInfo; import android.graphics.ImageFormat; import android.hardware.Camera; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.IOException; import java.io.OutputStream; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.Socket; import java.nio.ByteBuffer; import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements View.OnClickListener, Camera.PreviewCallback { @BindView(R.id.surfaceView_encode) SurfaceView surfaceViewEncode; @BindView(R.id.surfaceView_decode) SurfaceView surfaceViewDecode; @BindView(R.id.btnStart) Button btnStart; @BindView(R.id.btnStop) Button btnStop; @BindView(R.id.capture) TextView capture; private int width; private int height; private int bitrate; private int framerate; private int captureFrame; private Camera mCamera; private Queue<PreviewBufferInfo> mPreviewBuffers_clean; private Queue<PreviewBufferInfo> mPreviewBuffers_dirty; private Queue<PreviewBufferInfo> mDecodeBuffers_clean; private Queue<PreviewBufferInfo> mDecodeBuffers_dirty; private int PREVIEW_POOL_CAPACITY = 5; private int format; private int DECODE_UNI_SIZE = 1024 * 1024; private byte[] mAvcBuf = new byte[1024 * 1024]; private final int MSG_ENCODE = 0; private final int MSG_DECODE = 1; private String TAG = "MainActivity"; private long mLastTestTick = 0; private Object mAvcEncLock; private Object mDecEncLock; private Decoder mDecoder; private Handler codecHandler; private byte[] mRawData; private Encoder mEncoder; private CodecThread codecThread; private DatagramSocket socket; private DatagramPacket packet; private byte[] sps_pps; private byte[] mPacketBuf = new byte[1024 * 1024]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //初始化參數 initParams(); //設置監聽事件 btnStart.setOnClickListener(this); btnStop.setOnClickListener(this); } /** * 初始化參數,包括幀率、顏色、比特率,視頻寬高等 */ private void initParams() { width = 352; height = 288; bitrate = 1500000; framerate = 30; captureFrame = 0; format = ImageFormat.YV12; mAvcEncLock = new Object(); mDecEncLock = new Object(); } @Override protected void onResume() { if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } super.onResume(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnStart: mCamera = Camera.open(0); initQueues(); initEncoder(); initCodecThread(); startPreview(); break; case R.id.btnStop: releaseCodecThread(); releseEncoderAndDecoder(); releaseCamera(); releaseQueue(); break; } } /** * 釋放隊列資源 */ private void releaseQueue() { if (mPreviewBuffers_clean != null){ mPreviewBuffers_clean.clear(); mPreviewBuffers_clean = null; } if (mPreviewBuffers_dirty != null){ mPreviewBuffers_dirty.clear(); mPreviewBuffers_dirty = null; } if (mDecodeBuffers_clean != null){ mDecodeBuffers_clean.clear(); mDecodeBuffers_clean = null; } if (mDecodeBuffers_dirty != null){ mDecodeBuffers_dirty.clear(); mDecodeBuffers_dirty = null; } } /** * 釋放攝像頭資源 */ private void releaseCamera() { if(mCamera != null){ mCamera.setPreviewCallbackWithBuffer(null); mCamera.stopPreview(); mCamera.release(); mCamera = null; } } private void releseEncoderAndDecoder() { if(mEncoder != null){ mEncoder.flush(); mEncoder.release(); mEncoder = null; } if(mDecoder != null){ mDecoder.release(); mDecoder = null; } } private void releaseCodecThread() { codecHandler.getLooper().quit(); codecHandler = null; codecThread = null; } private void initCodecThread() { codecThread = new CodecThread(); codecThread.start(); } /** * 開啓預覽 */ private void startPreview() { Camera.Parameters parameters = mCamera.getParameters(); parameters.setPreviewFormat(format); parameters.setPreviewFrameRate(framerate); parameters.setPreviewSize(width,height); mCamera.setParameters(parameters); try { mCamera.setPreviewDisplay(surfaceViewEncode.getHolder()); } catch (IOException e) { e.printStackTrace(); } mCamera.setPreviewCallbackWithBuffer(this); mCamera.startPreview(); } @Override public void onPreviewFrame(byte[] data, Camera camera) { /** 預覽的data爲null */ if(data == null) { Log.e(TAG,"預覽的data爲null"); return; } long curTick = System.currentTimeMillis(); if (mLastTestTick == 0) { mLastTestTick = curTick; } if (curTick > mLastTestTick + 1000) { setCaptureFPSTextView(captureFrame); captureFrame = 0; mLastTestTick = curTick; } else captureFrame++; synchronized(mAvcEncLock) { PreviewBufferInfo info = mPreviewBuffers_clean.poll(); //remove the head of queue info.buffer = data; info.size = getPreviewBufferSize(width, height, format); info.timestamp = System.currentTimeMillis(); mPreviewBuffers_dirty.add(info); if(mDecoder == null){ codecHandler.sendEmptyMessage(MSG_ENCODE); } } } private void setCaptureFPSTextView(int captureFrame) { capture.setText("當前幀率:" + captureFrame); } private void initEncoder() { mEncoder = new Encoder(); try { mEncoder.init(); mEncoder.configure(width,height,bitrate,framerate); mEncoder.start(); } catch (IOException e) { e.printStackTrace(); } } /** * 初始化各類隊列 */ private void initQueues() { if (mPreviewBuffers_clean == null) mPreviewBuffers_clean = new LinkedList<>(); if (mPreviewBuffers_dirty == null) mPreviewBuffers_dirty = new LinkedList<>(); int size = getPreviewBufferSize(width, height, format); for (int i = 0; i < PREVIEW_POOL_CAPACITY; i++) { byte[] mem = new byte[size]; mCamera.addCallbackBuffer(mem); //ByteBuffer.array is a reference, not a copy PreviewBufferInfo info = new PreviewBufferInfo(); info.buffer = null; info.size = 0; info.timestamp = 0; mPreviewBuffers_clean.add(info); } if (mDecodeBuffers_clean == null) mDecodeBuffers_clean = new LinkedList<>(); if (mDecodeBuffers_dirty == null) mDecodeBuffers_dirty = new LinkedList<>(); for (int i = 0; i < PREVIEW_POOL_CAPACITY; i++) { PreviewBufferInfo info = new PreviewBufferInfo(); info.buffer = new byte[DECODE_UNI_SIZE]; info.size = 0; info.timestamp = 0; mDecodeBuffers_clean.add(info); } } /** * 獲取預覽buffer的大小 * @param width 預覽寬 * @param height 預覽高 * @param format 預覽顏色格式 * @return 預覽buffer的大小 */ private int getPreviewBufferSize(int width, int height, int format) { int size = 0; switch (format) { case ImageFormat.YV12: { int yStride = (int) Math.ceil(width / 16.0) * 16; int uvStride = (int) Math.ceil((yStride / 2) / 16.0) * 16; int ySize = yStride * height; int uvSize = uvStride * height / 2; size = ySize + uvSize * 2; } break; case ImageFormat.NV21: { float bytesPerPix = (float) ImageFormat.getBitsPerPixel(format) / 8; size = (int) (width * height * bytesPerPix); } break; } return size; } private void swapYV12toI420(byte[] yv12bytes, byte[] i420bytes, int width, int height) { System.arraycopy(yv12bytes, 0, i420bytes, 0, width * height); System.arraycopy(yv12bytes, width * height + width * height / 4, i420bytes, width * height, width * height / 4); System.arraycopy(yv12bytes, width * height, i420bytes, width * height + width * height / 4, width * height / 4); } private class PreviewBufferInfo { public byte[] buffer; public int size; public long timestamp; } private class CodecThread extends Thread { @Override public void run() { Looper.prepare(); codecHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_ENCODE: int res = Encoder.BUFFER_OK; synchronized (mAvcEncLock) { if (mPreviewBuffers_dirty != null && mPreviewBuffers_clean != null) { Iterator<PreviewBufferInfo> ite = mPreviewBuffers_dirty.iterator(); while (ite.hasNext()) { PreviewBufferInfo info = ite.next(); byte[] data = info.buffer; int data_size = info.size; if (format == ImageFormat.YV12) { if (mRawData == null || mRawData.length < data_size) { mRawData = new byte[data_size]; } swapYV12toI420(data, mRawData, width, height); } else { Log.e(TAG, "preview size MUST be YV12, cur is " + format); mRawData = data; } res = mEncoder.input(mRawData, data_size, info.timestamp); if (res != Encoder.BUFFER_OK) { // Log.e(TAG, "mEncoder.input, maybe wrong:" + res); break; //the rest buffers shouldn't go into encoder, if the previous one get problem } else { ite.remove(); mPreviewBuffers_clean.add(info); if (mCamera != null) { mCamera.addCallbackBuffer(data); } } } } } while (res == Encoder.BUFFER_OK) { int[] len = new int[1]; long[] ts = new long[1]; synchronized (mAvcEncLock) { res = mEncoder.output(mAvcBuf, len, ts); } if (res == Encoder.BUFFER_OK) { //發送h264 if(sps_pps != null){ send(len[0]); } if (mDecodeBuffers_clean != null && mDecodeBuffers_dirty != null) { synchronized (mAvcEncLock) { Iterator<PreviewBufferInfo> ite = mDecodeBuffers_clean.iterator(); if (ite.hasNext()) { PreviewBufferInfo bufferInfo = ite.next(); if (bufferInfo.buffer.length >= len[0]) { bufferInfo.timestamp = ts[0]; bufferInfo.size = len[0]; System.arraycopy(mAvcBuf, 0, bufferInfo.buffer, 0, len[0]); ite.remove(); mDecodeBuffers_dirty.add(bufferInfo); } else { Log.e(TAG, "decoder uni buffer too small, need " + len[0] + " but has " + bufferInfo.buffer.length); } } } initDecoder(len); } } } codecHandler.sendEmptyMessageDelayed(MSG_ENCODE, 30); break; case MSG_DECODE: synchronized (mDecEncLock) { int result = Decoder.BUFFER_OK; //STEP 1: handle input buffer if (mDecodeBuffers_dirty != null && mDecodeBuffers_clean != null) { Iterator<PreviewBufferInfo> ite = mDecodeBuffers_dirty.iterator(); while (ite.hasNext()) { PreviewBufferInfo info = ite.next(); result = mDecoder.input(info.buffer, info.size, info.timestamp); if (result != Decoder.BUFFER_OK) { break; //the rest buffers shouldn't go into encoder, if the previous one get problem } else { ite.remove(); mDecodeBuffers_clean.add(info); } } } int[] len = new int[1]; long[] ts = new long[1]; while (result == Decoder.BUFFER_OK) { result = mDecoder.output(null, len, ts); } } codecHandler.sendEmptyMessageDelayed(MSG_DECODE, 30); break; } } }; Looper.loop(); } } private void send(int len) { try { if(socket == null) socket = new DatagramSocket(); if(packet == null){ packet = new DatagramPacket(mPacketBuf,0,sps_pps.length + len); packet.setAddress(InetAddress.getByName("192.168.43.1")); packet.setPort(5006); } if(mAvcBuf[4] == 0x65){ System.arraycopy(sps_pps,0,mPacketBuf,0,sps_pps.length); System.arraycopy(mAvcBuf,0,mPacketBuf,sps_pps.length,len); len += sps_pps.length; }else{ System.arraycopy(mAvcBuf,0,mPacketBuf,0,len); } packet.setLength(len); socket.send(packet); } catch (IOException e) { e.printStackTrace(); } } private void initDecoder(int[] len) { if(sps_pps == null){ sps_pps = new byte[len[0]]; System.arraycopy(mAvcBuf,0,sps_pps,0,len[0]); } if(mDecoder == null){ mDecoder = new Decoder(); try { mDecoder.init(); } catch (IOException e) { e.printStackTrace(); } byte[] sps_nal = null; int sps_len = 0; byte[] pps_nal = null; int pps_len = 0; ByteBuffer byteb = ByteBuffer.wrap(mAvcBuf, 0, len[0]); //SPS if (true == AvcUtils.goToPrefix(byteb)) { int sps_position = 0; int pps_position = 0; int nal_type = AvcUtils.getNalType(byteb); if (AvcUtils.NAL_TYPE_SPS == nal_type) { Log.d(TAG, "OutputAvcBuffer, AVC NAL type: SPS"); sps_position = byteb.position() - AvcUtils.START_PREFIX_LENGTH - AvcUtils.NAL_UNIT_HEADER_LENGTH; //PPS if (true == AvcUtils.goToPrefix(byteb)) { nal_type = AvcUtils.getNalType(byteb); if (AvcUtils.NAL_TYPE_PPS == nal_type) { pps_position = byteb.position() - AvcUtils.START_PREFIX_LENGTH - AvcUtils.NAL_UNIT_HEADER_LENGTH; sps_len = pps_position - sps_position; sps_nal = new byte[sps_len]; int cur_pos = byteb.position(); byteb.position(sps_position); byteb.get(sps_nal, 0, sps_len); byteb.position(cur_pos); //slice if (true == AvcUtils.goToPrefix(byteb)) { nal_type = AvcUtils.getNalType(byteb); int pps_end_position = byteb.position() - AvcUtils.START_PREFIX_LENGTH - AvcUtils.NAL_UNIT_HEADER_LENGTH; pps_len = pps_end_position - pps_position; } else { pps_len = byteb.position() - pps_position; //pps_len = byteb.limit() - pps_position + 1; } if (pps_len > 0) { pps_nal = new byte[pps_len]; cur_pos = byteb.position(); byteb.position(pps_position); byteb.get(pps_nal, 0, pps_len); byteb.position(cur_pos); } } else { //Log.d(log_tag, "OutputAvcBuffer, AVC NAL type: "+nal_type); throw new UnsupportedOperationException("SPS is not followed by PPS, nal type :" + nal_type); } } } else { //Log.d(log_tag, "OutputAvcBuffer, AVC NAL type: "+nal_type); } //2. configure AVC decoder with SPS/PPS if (sps_nal != null && pps_nal != null) { int[] width = new int[1]; int[] height = new int[1]; AvcUtils.parseSPS(sps_nal, width, height); mDecoder.configure(sps_nal, pps_nal,surfaceViewDecode.getHolder().getSurface()); mDecoder.start(); if (codecHandler != null) { codecHandler.sendEmptyMessage(MSG_DECODE); } } } } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533

上面的send方法能夠把手機捕捉並編碼的視頻數據發送到電腦上使用ffplay播放,使用ffplay的時候記得加上參數-analyzeduration 200000減少視頻延遲。

4.要點總結

我的總結使用MediaCodec編解碼的時候主要須要注意如下事項: 
- 數據的輸入和輸出要異步進行,不要採用阻塞的方式等待輸出數據 
- 編碼Camera預覽數據的時候使用帶buffer的預覽回調,避免幀率太低 
- 適當使用緩存隊列,不要每一幀都new一個byte數組,避免頻繁的GC 
- 不要在主線程進行操做,在我學習的過程當中看到有些朋友直接在預覽回調裏進行編解碼,在dequeueOutputBuffer方法還傳遞-1,也就是無限等待程序返回 
上述實例地址:點擊這裏,因爲我一直沒有積分去下載東西,因此下載收1個積分。若是有任何問題,歡迎指正,相互學習

相關文章
相關標籤/搜索