Android自带硬编解码器MediaCodec使用必看

一、背景

随着Android系统手机性能的不断提升,现阶段大部分手机都自带GPU(承担图形显示的专门硬件),大幅度提高手机显示性能,在视频显示、游戏画面刷新,和高分辨图像显示方面必须使用GPU。GOOGLE在API 16 -4.1版本中增加MediaCodec类,专用于编解码多媒体数据,

二、MediaCodec使用方式

     MediaCodec总共有三种使用方法,如下图所示:

          同步数据处理(使用buffer arrays) 从Android4.1 api 16即可以使用;

          同步数据处理 (使用buffers ) 从Android5.0 api 21 即可以使用;

          异步数据处理(使用buffers ) 从Android5.0 api 21 即可以使用;

同步与异步处理数据的主要不同点是:对于同步处理是循环的将待处理数据交给编解码器(不用理会编解码器是否已经准备好接收数据),而异步处理则是每次都会去等待编解码器已经能够处理数据时,才将待处理数据送给相应编解码器。

MediaCodec is typically used like this in asynchronous mode:

[java]  view plain  copy
  1. MediaCodec codec = MediaCodec.createByCodecName(name);  
  2.  MediaFormat mOutputFormat; // member variable  
  3.  codec.setCallback(new MediaCodec.Callback() {  
  4.    @Override  
  5.    void onInputBufferAvailable(MediaCodec mc, int inputBufferId) {  
  6.      ByteBuffer inputBuffer = codec.getInputBuffer(inputBufferId);  
  7.      // fill inputBuffer with valid data  
  8.      …  
  9.      codec.queueInputBuffer(inputBufferId, …);  
  10.    }  
  11.   
  12.    @Override  
  13.    void onOutputBufferAvailable(MediaCodec mc, int outputBufferId, …) {  
  14.      ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);  
  15.      MediaFormat bufferFormat = codec.getOutputFormat(outputBufferId); // option A  
  16.      // bufferFormat is equivalent to mOutputFormat  
  17.      // outputBuffer is ready to be processed or rendered.  
  18.      …  
  19.      codec.releaseOutputBuffer(outputBufferId, …);  
  20.    }  
  21.   
  22.    @Override  
  23.    void onOutputFormatChanged(MediaCodec mc, MediaFormat format) {  
  24.      // Subsequent data will conform to new format.  
  25.      // Can ignore if using getOutputFormat(outputBufferId)  
  26.      mOutputFormat = format; // option B  
  27.    }  
  28.   
  29.    @Override  
  30.    void onError(…) {  
  31.      …  
  32.    }  
  33.  });  
  34.  codec.configure(format, …);  
  35.  mOutputFormat = codec.getOutputFormat(); // option B  
  36.  codec.start();  
  37.  // wait for processing to complete  
  38.  codec.stop();  
  39.  codec.release();  
Synchronous Processing using Buffers
[java]  view plain  copy
  1. MediaCodec codec = MediaCodec.createByCodecName(name);  
  2. codec.configure(format, …);  
  3. MediaFormat outputFormat = codec.getOutputFormat(); // option B  
  4. codec.start();  
  5. for (;;) {  
  6.   int inputBufferId = codec.dequeueInputBuffer(timeoutUs);  
  7.   if (inputBufferId >= 0) {  
  8.     ByteBuffer inputBuffer = codec.getInputBuffer(…);  
  9.     // fill inputBuffer with valid data  
  10.     …  
  11.     codec.queueInputBuffer(inputBufferId, …);  
  12.   }  
  13.   int outputBufferId = codec.dequeueOutputBuffer(…);  
  14.   if (outputBufferId >= 0) {  
  15.     ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);  
  16.     MediaFormat bufferFormat = codec.getOutputFormat(outputBufferId); // option A  
  17.     // bufferFormat is identical to outputFormat  
  18.     // outputBuffer is ready to be processed or rendered.  
  19.     …  
  20.     codec.releaseOutputBuffer(outputBufferId, …);  
  21.   } else if (outputBufferId == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {  
  22.     // Subsequent data will conform to new format.  
  23.     // Can ignore if using getOutputFormat(outputBufferId)  
  24.     outputFormat = codec.getOutputFormat(); // option B  
  25.   }  
  26. }  
  27. codec.stop();  
  28. codec.release();  

三、MediaCodec能够硬编解码的类型



其中对于视频格式,MediaCodec支持的有vp8 、VP9 、H.264、H.265、MPEG4、H.263基本上涵盖了现今流行的所有视频格式,

对于音频来说,MediaCodec支持的音频格式有3gppamr-wb、amr-wb、amr-wb、g711-A、g711-U 、AAC(not packaged in LATM)

  • "video/x-vnd.on2.vp8" - VP8 video (i.e. video in .webm)
  • "video/x-vnd.on2.vp9" - VP9 video (i.e. video in .webm)
  • "video/avc" - H.264/AVC video
  • "video/hevc" - H.265/HEVC video
  • "video/mp4v-es" - MPEG4 video
  • "video/3gpp" - H.263 video


  • "audio/3gpp" - AMR narrowband audio
  • "audio/amr-wb" - AMR wideband audio
  • "audio/amr-wb" - MPEG1/2 audio layer III
  • "audio/mp4a-latm" - AAC audio (note, this is raw AAC packets, not packaged in LATM!)
  • "audio/amr-wb" - vorbis audio
  • "audio/g711-alaw" - G.711 alaw audio
  • "audio/g711-mlaw" - G.711 ulaw audio

FormatSuitable key frame
VP9/VP8a suitable intraframe where no subsequent frames refer to frames prior to this frame.
(There is no specific name for such key frame.)
H.265 HEVCIDR or CRA
H.264 AVCIDR
MPEG-4
H.263
MPEG-2
a suitable I-frame where no subsequent frames refer to frames prior to this frame.
(There is no specific name for such key frame.)
转自:http://blog.csdn.net/guofengpu/article/details/51544454
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C++ MediaCodec 进行视频解码的示例代码: ```c++ #include <jni.h> #include <android/log.h> #include <android/native_window_jni.h> #include <media/NdkMediaCodec.h> #include <media/NdkMediaExtractor.h> #define LOG_TAG "MediaCodec" #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) extern "C" JNIEXPORT void JNICALL Java_com_example_mediadecoder_MainActivity_decode(JNIEnv *env, jobject thiz, jstring filePath, jobject surface) { const char *path = env->GetStringUTFChars(filePath, nullptr); AMediaExtractor *extractor = AMediaExtractor_new(); AMediaCodec *codec = nullptr; int32_t trackIndex = -1; bool sawInputEOS = false; bool sawOutputEOS = false; // 设置文件路径 media_status_t result = AMediaExtractor_setDataSource(extractor, path); if (result != AMEDIA_OK) { LOGE("AMediaExtractor_setDataSource error: %d", result); goto end; } // 查找视频轨道 int32_t numTracks = AMediaExtractor_getTrackCount(extractor); for (int32_t i = 0; i < numTracks; ++i) { AMediaFormat *format = AMediaExtractor_getTrackFormat(extractor, i); const char *mime = nullptr; AMediaFormat_getString(format, AMEDIAFORMAT_KEY_MIME, &mime); if (mime && !strncmp(mime, "video/", 6)) { trackIndex = i; break; } AMediaFormat_delete(format); } if (trackIndex == -1) { LOGE("No video track found in %s", path); goto end; } // 获取视频格式 AMediaFormat *format = AMediaExtractor_getTrackFormat(extractor, trackIndex); int32_t width = 0, height = 0; AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_WIDTH, &width); AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_HEIGHT, &height); LOGI("Video size: %dx%d", width, height); // 创建 MediaCodec 解码器 codec = AMediaCodec_createDecoderByType("video/avc"); result = AMediaCodec_configure(codec, format, ANativeWindow_fromSurface(env, surface), nullptr, 0); if (result != AMEDIA_OK) { LOGE("AMediaCodec_configure error: %d", result); goto end; } // 启动解码器 result = AMediaCodec_start(codec); if (result != AMEDIA_OK) { LOGE("AMediaCodec_start error: %d", result); goto end; } // 解码视频帧 AMediaCodecBufferInfo bufferInfo; int64_t presentationTimeUs; size_t bufIdx; while (!sawOutputEOS) { if (!sawInputEOS) { bufIdx = AMediaExtractor_getSampleTrackIndex(extractor); if (bufIdx == trackIndex) { result = AMediaExtractor_readSampleData(extractor, AMediaCodec_getInputBuffer(codec, bufIdx), 0); if (result == AMEDIA_OK) { presentationTimeUs = AMediaExtractor_getSampleTime(extractor); AMediaCodec_queueInputBuffer(codec, bufIdx, 0, AMediaExtractor_getSampleSize(extractor), presentationTimeUs, AMediaExtractor_getSampleFlags(extractor)); AMediaExtractor_advance(extractor); } else if (result == AMEDIA_ERROR_END_OF_STREAM) { sawInputEOS = true; AMediaCodec_queueInputBuffer(codec, bufIdx, 0, 0, 0, AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM); } else { LOGE("AMediaExtractor_readSampleData error: %d", result); goto end; } } } result = AMediaCodec_dequeueOutputBuffer(codec, &bufferInfo, 5000); if (result == AMEDIA_OK) { if (bufferInfo.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) { sawOutputEOS = true; } AMediaCodec_releaseOutputBuffer(codec, result, true); } else if (result == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) { AMediaFormat *newFormat = nullptr; newFormat = AMediaCodec_getOutputFormat(codec); AMediaFormat_getInt32(newFormat, AMEDIAFORMAT_KEY_WIDTH, &width); AMediaFormat_getInt32(newFormat, AMEDIAFORMAT_KEY_HEIGHT, &height); LOGI("Video output format changed to %dx%d", width, height); AMediaFormat_delete(newFormat); } else if (result == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED) { LOGI("AMediaCodec_dequeueOutputBuffer returned AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED"); } else { LOGE("AMediaCodec_dequeueOutputBuffer error: %d", result); goto end; } } end: if (codec) { AMediaCodec_stop(codec); AMediaCodec_delete(codec); } if (extractor) { AMediaExtractor_delete(extractor); } env->ReleaseStringUTFChars(filePath, path); } ``` 在此示例代码中,我们使用 `AMediaExtractor` 获取视频文件中的视频轨道,并使用 `AMediaCodec` 进行解码。注意,此代码仅支持解码 H.264 编码视频。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值