Android: MediaCodec視頻文件硬件解碼,高效率獲得YUV格式幀,快速保存JPEG圖片(不使用OpenGL)

如下文章轉載自:html

https://www.polarxiong.com/archives/Android-MediaCodec%E8%A7%86%E9%A2%91%E6%96%87%E4%BB%B6%E7%A1%AC%E4%BB%B6%E8%A7%A3%E7%A0%81-%E9%AB%98%E6%95%88%E7%8E%87%E5%BE%97%E5%88%B0YUV%E6%A0%BC%E5%BC%8F%E5%B8%A7-%E5%BF%AB%E9%80%9F%E4%BF%9D%E5%AD%98JPEG%E5%9B%BE%E7%89%87-%E4%B8%8D%E4%BD%BF%E7%94%A8OpenGL.htmljava

 

Android: hardware decode video file through MediaCodec, get YUV format video frames directly (without OpenGL), efficiently save frames as YUV/JEPG format to file.android

特色

以H.264編碼分辨率1920x1080視頻文件爲例git

  • 須要Android API 21
  • 直接將視頻解碼爲YUV格式幀,不通過OpenGL,不轉換爲RGB
  • 對絕大多數設備和絕大多數視頻編碼格式,均可以解碼獲得NV21或I420格式幀數據
  • 30ms內得到NV21或I420格式幀數據
  • 10ms內將NV21或I420格式幀數據寫入到文件
  • 對獲得的NV21格式幀數據,在110ms內完成JPEG格式的轉換和寫入到文件

背景

由於實驗須要在Android上高效率解碼視頻文件,並得到YUV格式幀數據,遂搜索尋找解決方法。最初找到bigflake的Android MediaCodec stuff,硬件解碼視頻不可多得的示例代碼,其中提供告終合MediaCodec和OpenGL硬件解碼視頻並獲得RGB格式幀數據,以及寫入bitmap圖片到文件的方法,測試發現效果不錯,但我想要的是獲得YUV格式的幀數據;在繼續尋找RGB轉YUV的方法時,苦於沒有找到高效實現這個轉換的方法,遂做罷。github

後來發現MediaCodec解碼獲得的原始幀數據應當就是YUV格式,而後看到stackoverflow上的討論Why doesn't the decoder of MediaCodec output a unified YUV format(like YUV420P)?,發現有人和我有同樣的須要,但他已經發現了不一樣設備MediaCodec解碼獲得的YUV格式不相同這個問題,且因爲各類格式繁雜,很難寫出高效的格式轉換方法。而後又發現了來自加州理工學院的一篇文章Android MediaCodec Formats,別人統計了市面上Android設備MediaCodec解碼獲得的不一樣YUV格式所佔的比例,表格中顯示出格式之繁多,且以COLOR_QCOM_FormatYUV420SemiPlanar32m,OMX_QCOM_COLOR_FormatYUV420PackedSemiPlanar64x32Tile2m8ka和COLOR_FormatYUV420SemiPlanar佔據絕大多數。考慮放棄MediaCodec直接獲得統一格式的YUV格式幀數據。數組

再後來不死心繼續找,偶然找到了一份Android CTS測試ImageImageReader類的代碼,發現了由MediaCodec解碼直接獲得指定YUV格式(如NV21,I420)視頻幀的方法,遂有了此文。框架

概述

簡單來講,整個過程是,MediaCodec將編碼後的視頻文件解碼獲得YUV420類的視頻幀,而後將視頻幀格式轉換爲NV21或I420格式,由用戶進行後續處理;若須要寫入.yuv文件,直接將轉換後的數據寫入便可。若須要保存爲JPEG格式圖片,將NV21格式幀數據轉換爲JPEG格式並寫入。ide

詳細來講,CTS測試中透露出能夠指定硬件解碼獲得幀編碼格式,雖然不一樣設備支持的編碼格式都不盡相同,但得益於API 21加入的COLOR_FormatYUV420Flexible格式,MediaCodec的全部硬件解碼都支持這種格式。但這樣解碼後獲得的YUV420的具體格式又會因設備而異,如YUV420Planar,YUV420SemiPlanar,YUV420PackedSemiPlanar等。然而又得益於API 21對MediaCodec加入的Image類的支持,能夠實現簡單且高效的任意YUV420格式向如NV21,I420等格式的轉換,這樣就獲得了一個統一的、能夠預先指定的YUV格式視頻幀。再進一步,YuvImage類提供了一種高效的NV21格式轉換爲JPEG格式並寫入文件的方法,能夠實現將解碼獲得的視頻幀保存爲JPEG格式圖片的功能,且整個過程相比bigflake中提供的YUV經OpenGL轉換爲RGB格式,而後經過Bitmap類保存爲圖片,效率高不少。測試

MediaCodec指定幀格式

實際上,MediaCodec不只在編碼,並且在解碼是也可以指定幀格式。可以指定的緣由是,解碼獲得的幀的格式,並非由如H.264編碼的視頻文件提早肯定的,而是由解碼器肯定的,解碼器支持哪些幀格式,就能夠解碼出哪些格式的幀。google

獲取支持的格式

MediaCodec雖然能夠指定幀格式,但也不是能指定爲任意格式,是須要硬件支持的。首先看看對於特定視頻編碼格式的MediaCodec解碼器,支持哪些幀格式。

Java
private static int selectTrack(MediaExtractor extractor) { int numTracks = extractor.getTrackCount(); for (int i = 0; i < numTracks; i++) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { if (VERBOSE) { Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format); } return i; } } return -1; } private void showSupportedColorFormat(MediaCodecInfo.CodecCapabilities caps) { System.out.print("supported color format: "); for (int c : caps.colorFormats) { System.out.print(c + "\t"); } System.out.println(); } MediaExtractor extractor = null; MediaCodec decoder = null; File videoFile = new File(videoFilePath); extractor = new MediaExtractor(); extractor.setDataSource(videoFile.toString()); int trackIndex = selectTrack(extractor); if (trackIndex < 0) { throw new RuntimeException("No video track found in " + videoFilePath); } extractor.selectTrack(trackIndex); MediaFormat mediaFormat = extractor.getTrackFormat(trackIndex); String mime = mediaFormat.getString(MediaFormat.KEY_MIME); decoder = MediaCodec.createDecoderByType(mime); showSupportedColorFormat(decoder.getCodecInfo().getCapabilitiesForType(mime)); 

MediaExtractor負責讀取視頻文件,得到視頻文件信息,以及提供 視頻編碼後的幀數據(如H.264)selectTrack()獲取視頻所在的軌道號,getTrackFormat()得到視頻的編碼信息。再以此編碼信息經過createDecoderByType()得到一個解碼器,而後經過showSupportedColorFormat()就能夠獲得這個解碼器支持的幀格式了。

好比對於個人設備,對於支持video/avc的解碼器,支持的幀格式是

supported color format: 2135033992  21  47  25  27  35  40  52  2130706433  2130706434  20

這裏的數字對應MediaCodecInfo.CodecCapabilities定義的幀格式,如2135033992對應COLOR_FormatYUV420Flexible,21對應COLOR_FormatYUV420SemiPlanar,25對應COLOR_FormatYCbYCr,27對應COLOR_FormatCbYCrY,35對應COLOR_FormatL8,40對應COLOR_FormatYUV422PackedSemiPlanar,20對應COLOR_FormatYUV420PackedPlanar。

COLOR_FormatYUV420Flexible

這裏簡單談談COLOR_FormatYUV420Flexible,YUV420Flexible並非一種肯定的YUV420格式,而是包含COLOR_FormatYUV411Planar, COLOR_FormatYUV411PackedPlanar, COLOR_FormatYUV420Planar, COLOR_FormatYUV420PackedPlanar, COLOR_FormatYUV420SemiPlanar和COLOR_FormatYUV420PackedSemiPlanar。在API 21引入YUV420Flexible的同時,它所包含的這些格式都deprecated掉了。

那麼爲何全部的解碼器都支持YUV420Flexible呢?官方沒有說明這點,但我猜想,只要解碼器支持YUV420Flexible中的任意一種格式,就會被認爲支持YUV420Flexible格式。也就是說,幾乎全部的解碼器都支持YUV420Flexible表明的格式中的一種或幾種。

指定幀格式

日常初始化MediaCodec並啓動解碼器是用以下代碼

Java
decoder.configure(mediaFormat, null, null, 0); decoder.start(); 

其中mediaFormat是以前獲得的視頻編碼信息,這樣向解碼器肯定了各類參數後,就能正常解碼了。

而指定幀格式是在上述代碼前增長

Java
mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible); 

僅此一行,用來指定解碼後的幀格式,換句話說,解碼器將編碼的幀解碼爲這種指定的格式。前面說到YUV420Flexible是幾乎全部解碼器都支持的,因此能夠直接寫死。

這個指定方法就是我在CTS中發現的,由於官方文檔對KEY_COLOR_FORMAT的描述是set by the user for encoders, readable in the output format of decoders,也就是說只用在編碼器中,而不是咱們如今用的解碼器中!

轉換格式和寫入文件

主體框架

先貼主體部分的代碼

Java
final int width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH); final int height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); int outputFrameCount = 0; while (!sawOutputEOS) { if (!sawInputEOS) { int inputBufferId = decoder.dequeueInputBuffer(DEFAULT_TIMEOUT_US); if (inputBufferId >= 0) { ByteBuffer inputBuffer = decoder.getInputBuffer(inputBufferId); int sampleSize = extractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { decoder.queueInputBuffer(inputBufferId, 0, 0, 0L, MediaCodec.BUFFER_FLAG_END_OF_STREAM); sawInputEOS = true; } else { long presentationTimeUs = extractor.getSampleTime(); decoder.queueInputBuffer(inputBufferId, 0, sampleSize, presentationTimeUs, 0); extractor.advance(); } } } int outputBufferId = decoder.dequeueOutputBuffer(info, DEFAULT_TIMEOUT_US); if (outputBufferId >= 0) { if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { sawOutputEOS = true; } boolean doRender = (info.size != 0); if (doRender) { outputFrameCount++; Image image = decoder.getOutputImage(outputBufferId); if (outputImageFileType != -1) { String fileName; switch (outputImageFileType) { case FILE_TypeI420: fileName = OUTPUT_DIR + String.format("frame_%05d_I420_%dx%d.yuv", outputFrameCount, width, height); dumpFile(fileName, getDataFromImage(image, COLOR_FormatI420)); break; case FILE_TypeNV21: fileName = OUTPUT_DIR + String.format("frame_%05d_NV21_%dx%d.yuv", outputFrameCount, width, height); dumpFile(fileName, getDataFromImage(image, COLOR_FormatNV21)); break; case FILE_TypeJPEG: fileName = OUTPUT_DIR + String.format("frame_%05d.jpg", outputFrameCount); compressToJpeg(fileName, image); break; } } image.close(); decoder.releaseOutputBuffer(outputBufferId, true); } } } 

上述代碼是MediaCodec解碼的通常框架,不做過多解釋。 不一樣於bigflake的是MediaCodec解碼的輸出沒有指定一個Surface,而是利用API 21新功能,直接經過getOutputImage()將視頻幀以Image的形式取出。

而咱們如今獲得的Image就能夠肯定是YUV420Flexible格式,而得益於Image類的抽象,咱們又能夠很是方便地將其轉換爲NV21或I420格式。關於具體的轉換和寫入文件的細節,參見個人另外一篇文章Android: YUV_420_888編碼Image轉換爲I420和NV21格式byte數組

總結

這篇文章餅畫的很大,但寫的很短,由於還有一大部份內容在如上連接中的文章中講到。對於僅僅須要將視頻切分爲一幀一幀並保存爲圖片的用戶來講,使用這種方法比bigflake的方法會快10倍左右,由於沒有OpenGL渲染,以及轉換爲Bitmap的開銷。而對於須要得到視頻幀YUV格式數據的用戶來講,這種方法可以直接獲得YUV格式數據,中間沒有數學運算,不會出現沒必要要的精度損失,並且,也是效率最高的。

此方法的核心原理就是經過指定解碼器參數,保證瞭解碼獲得的幀格式必定是YUV420Flexible;經過Image實現了健壯且高效的YUV格式轉換方法;經過YuvImage實現了快速的JPEG格式圖片生成和寫入的方法。

Demo

依照上面的描述,本文附帶了一個Android APP Demo,指定輸入視頻文件和輸出文件夾名,此APP可將視頻幀保存爲I420、NV21或JPEG格式。若有須要,請點擊zhantong/Android-VideoToImages

demo screenshot

主要代碼

Java
import android.graphics.ImageFormat; import android.graphics.Rect; import android.graphics.YuvImage; import android.media.Image; import android.media.MediaCodec; import android.media.MediaCodecInfo; import android.media.MediaExtractor; import android.media.MediaFormat; import android.util.Log; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.concurrent.LinkedBlockingQueue; public class New { private static final String TAG = "VideoToFrames"; private static final boolean VERBOSE = true; private static final long DEFAULT_TIMEOUT_US = 10000; private static final int COLOR_FormatI420 = 1; private static final int COLOR_FormatNV21 = 2; public static final int FILE_TypeI420 = 1; public static final int FILE_TypeNV21 = 2; public static final int FILE_TypeJPEG = 3; private final int decodeColorFormat = MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible; private int outputImageFileType = -1; private String OUTPUT_DIR; public void setSaveFrames(String dir, int fileType) throws IOException { if (fileType != FILE_TypeI420 && fileType != FILE_TypeNV21 && fileType != FILE_TypeJPEG) { throw new IllegalArgumentException("only support FILE_TypeI420 " + "and FILE_TypeNV21 " + "and FILE_TypeJPEG"); } outputImageFileType = fileType; File theDir = new File(dir); if (!theDir.exists()) { theDir.mkdirs(); } else if (!theDir.isDirectory()) { throw new IOException("Not a directory"); } OUTPUT_DIR = theDir.getAbsolutePath() + "/"; } public void videoDecode(String videoFilePath) throws IOException { MediaExtractor extractor = null; MediaCodec decoder = null; try { File videoFile = new File(videoFilePath); extractor = new MediaExtractor(); extractor.setDataSource(videoFile.toString()); int trackIndex = selectTrack(extractor); if (trackIndex < 0) { throw new RuntimeException("No video track found in " + videoFilePath); } extractor.selectTrack(trackIndex); MediaFormat mediaFormat = extractor.getTrackFormat(trackIndex); String mime = mediaFormat.getString(MediaFormat.KEY_MIME); decoder = MediaCodec.createDecoderByType(mime); showSupportedColorFormat(decoder.getCodecInfo().getCapabilitiesForType(mime)); if (isColorFormatSupported(decodeColorFormat, decoder.getCodecInfo().getCapabilitiesForType(mime))) { mediaFormat.setInteger(MediaFormat.KEY_COLOR_FORMAT, decodeColorFormat); Log.i(TAG, "set decode color format to type " + decodeColorFormat); } else { Log.i(TAG, "unable to set decode color format, color format type " + decodeColorFormat + " not supported"); } decodeFramesToImage(decoder, extractor, mediaFormat); decoder.stop(); } finally { if (decoder != null) { decoder.stop(); decoder.release(); decoder = null; } if (extractor != null) { extractor.release(); extractor = null; } } } private void showSupportedColorFormat(MediaCodecInfo.CodecCapabilities caps) { System.out.print("supported color format: "); for (int c : caps.colorFormats) { System.out.print(c + "\t"); } System.out.println(); } private boolean isColorFormatSupported(int colorFormat, MediaCodecInfo.CodecCapabilities caps) { for (int c : caps.colorFormats) { if (c == colorFormat) { return true; } } return false; } private void decodeFramesToImage(MediaCodec decoder, MediaExtractor extractor, MediaFormat mediaFormat) { MediaCodec.BufferInfo info = new MediaCodec.BufferInfo(); boolean sawInputEOS = false; boolean sawOutputEOS = false; decoder.configure(mediaFormat, null, null, 0); decoder.start(); final int width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH); final int height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT); int outputFrameCount = 0; while (!sawOutputEOS) { if (!sawInputEOS) { int inputBufferId = decoder.dequeueInputBuffer(DEFAULT_TIMEOUT_US); if (inputBufferId >= 0) { ByteBuffer inputBuffer = decoder.getInputBuffer(inputBufferId); int sampleSize = extractor.readSampleData(inputBuffer, 0); if (sampleSize < 0) { decoder.queueInputBuffer(inputBufferId, 0, 0, 0L, MediaCodec.BUFFER_FLAG_END_OF_STREAM); sawInputEOS = true; } else { long presentationTimeUs = extractor.getSampleTime(); decoder.queueInputBuffer(inputBufferId, 0, sampleSize, presentationTimeUs, 0); extractor.advance(); } } } int outputBufferId = decoder.dequeueOutputBuffer(info, DEFAULT_TIMEOUT_US); if (outputBufferId >= 0) { if ((info.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0) { sawOutputEOS = true; } boolean doRender = (info.size != 0); if (doRender) { outputFrameCount++; Image image = decoder.getOutputImage(outputBufferId); System.out.println("image format: " + image.getFormat()); if (outputImageFileType != -1) { String fileName; switch (outputImageFileType) { case FILE_TypeI420: fileName = OUTPUT_DIR + String.format("frame_%05d_I420_%dx%d.yuv", outputFrameCount, width, height); dumpFile(fileName, getDataFromImage(image, COLOR_FormatI420)); break; case FILE_TypeNV21: fileName = OUTPUT_DIR + String.format("frame_%05d_NV21_%dx%d.yuv", outputFrameCount, width, height); dumpFile(fileName, getDataFromImage(image, COLOR_FormatNV21)); break; case FILE_TypeJPEG: fileName = OUTPUT_DIR + String.format("frame_%05d.jpg", outputFrameCount); compressToJpeg(fileName, image); break; } } image.close(); decoder.releaseOutputBuffer(outputBufferId, true); } } } } private static int selectTrack(MediaExtractor extractor) { int numTracks = extractor.getTrackCount(); for (int i = 0; i < numTracks; i++) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (mime.startsWith("video/")) { if (VERBOSE) { Log.d(TAG, "Extractor selected track " + i + " (" + mime + "): " + format); } return i; } } return -1; } private static boolean isImageFormatSupported(Image image) { int format = image.getFormat(); switch (format) { case ImageFormat.YUV_420_888: case ImageFormat.NV21: case ImageFormat.YV12: return true; } return false; } private static byte[] getDataFromImage(Image image, int colorFormat) { if (colorFormat != COLOR_FormatI420 && colorFormat != COLOR_FormatNV21) { throw new IllegalArgumentException("only support COLOR_FormatI420 " + "and COLOR_FormatNV21"); } if (!isImageFormatSupported(image)) { throw new RuntimeException("can't convert Image to byte array, format " + image.getFormat()); } Rect crop = image.getCropRect(); int format = image.getFormat(); int width = crop.width(); int height = crop.height(); Image.Plane[] planes = image.getPlanes(); byte[] data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8]; byte[] rowData = new byte[planes[0].getRowStride()]; if (VERBOSE) Log.v(TAG, "get data from " + planes.length + " planes"); int channelOffset = 0; int outputStride = 1; for (int i = 0; i < planes.length; i++) { switch (i) { case 0: channelOffset = 0; outputStride = 1; break; case 1: if (colorFormat == COLOR_FormatI420) { channelOffset = width * height; outputStride = 1; } else if (colorFormat == COLOR_FormatNV21) { channelOffset = width * height + 1; outputStride = 2; } break; case 2: if (colorFormat == COLOR_FormatI420) { channelOffset = (int) (width * height * 1.25); outputStride = 1; } else if (colorFormat == COLOR_FormatNV21) { channelOffset = width * height; outputStride = 2; } break; } ByteBuffer buffer = planes[i].getBuffer(); int rowStride = planes[i].getRowStride(); int pixelStride = planes[i].getPixelStride(); if (VERBOSE) { Log.v(TAG, "pixelStride " + pixelStride); Log.v(TAG, "rowStride " + rowStride); Log.v(TAG, "width " + width); Log.v(TAG, "height " + height); Log.v(TAG, "buffer size " + buffer.remaining()); } int shift = (i == 0) ? 0 : 1; int w = width >> shift; int h = height >> shift; buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift)); for (int row = 0; row < h; row++) { int length; if (pixelStride == 1 && outputStride == 1) { length = w; buffer.get(data, channelOffset, length); channelOffset += length; } else { length = (w - 1) * pixelStride + 1; buffer.get(rowData, 0, length); for (int col = 0; col < w; col++) { data[channelOffset] = rowData[col * pixelStride]; channelOffset += outputStride; } } if (row < h - 1) { buffer.position(buffer.position() + rowStride - length); } } if (VERBOSE) Log.v(TAG, "Finished reading data from plane " + i); } return data; } private static void dumpFile(String fileName, byte[] data) { FileOutputStream outStream; try { outStream = new FileOutputStream(fileName); } catch (IOException ioe) { throw new RuntimeException("Unable to create output file " + fileName, ioe); } try { outStream.write(data); outStream.close(); } catch (IOException ioe) { throw new RuntimeException("failed writing data to file " + fileName, ioe); } } private void compressToJpeg(String fileName, Image image) { FileOutputStream outStream; try { outStream = new FileOutputStream(fileName); } catch (IOException ioe) { throw new RuntimeException("Unable to create output file " + fileName, ioe); } Rect rect = image.getCropRect(); YuvImage yuvImage = new YuvImage(getDataFromImage(image, COLOR_FormatNV21), ImageFormat.NV21, rect.width(), rect.height(), null); yuvImage.compressToJpeg(rect, 100, outStream); } } 

參考

相關文章
相關標籤/搜索