andeoid硬件解码
Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding and encoding APIs are finally available. It was a thing since Android 4.1 in Google IO 2012, but until now, when a new Android version 4.2 has been released, those low-level APIs are still too hard to use. There're so many Android vendors, we can't even get the same results from all Google's Nexus devices.
Despite the annoyings, I still tried these APIs and prayed them to get better in next Android release, and next Android release would swallow market quikly.
I just finished a simple video player with the combination of MediaExtractor and MediaCodec, you may find the full project from my Github.
private MediaExtractor extractor;
private MediaCodec decoder;
private Surface surface; public void run() {
extractor = new MediaExtractor();
extractor.setDataSource(SAMPLE); for (int i = 0; i < extractor.getTrackCount(); i++) {
MediaFormat format = extractor.getTrackFormat(i);
String mime = format.getString(MediaFormat.KEY_MIME);
if (mime.startsWith("video/")) {
extractor.selectTrack(i);
decoder = MediaCodec.createDecoderByType(mime);
decoder.configure(format, surface, null, 0);
break;
}
} if (decoder == null) {
Log.e("DecodeActivity", "Can't find video info!");
return;
} 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(10000);
if (inIndex >= 0) {
ByteBuffer buffer = inputBuffers[inIndex];
int sampleSize = extractor.readSampleData(buffer, 0);
if (sampleSize < 0) {
Log.d("DecodeActivity", "InputBuffer BUFFER_FLAG_END_OF_STREAM");
decoder.queueInputBuffer(inIndex, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
isEOS = true;
} else {
decoder.queueInputBuffer(inIndex, 0, sampleSize, extractor.getSampleTime(), 0);
extractor.advance();
}
}
} 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!");
break;
default:
ByteBuffer buffer = outputBuffers[outIndex];
Log.v("DecodeActivity"
, "We can't use this buffer but render it due to the API limit, " + buffer); // We use a very simple clock to keep the video FPS, or the video
// playback will be too fast
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();
extractor.release();
}
andeoid硬件解码的更多相关文章
- andeoid硬件解码学习 (二)
Finally, I must say, finally, we get low-level media APIs in Android, the Android hardware decoding ...
- android Mediaplayer硬件解码浅探
在讨论stagefright如何调用硬件解码之前,我们要先清楚几个问题. 我不展开这几个结论是如何得来的,因为这部分属于进程间通信binder的理解,和多媒体本身无关. 一.问题空间 这个有点像方法学 ...
- Android中使用MediaCodec硬件解码,高效率得到YUV格式帧,快速保存JPEG图片(不使用OpenGL)(附Demo)
MediaCodec的使用demo: https://github.com/vecio/MediaCodecDemo https://github.com/taehwandev/MediaCodecE ...
- Android的音频解码原来是直接调用的本地C方法直接通过硬件解码
Android就是披着JAVA外衣的C啊~音频解码原来是直接调用的本地C方法直接通过硬件解码的,JAVA和C的字节数组存放模式不同(java是大端,C根据不同平台不同),不同格式需要转化以后才能用. ...
- 使用C#+FFmpeg+DirectX+dxva2硬件解码播放h264流
本文门槛较高,因此行文看起来会乱一些,如果你看到某处能会心一笑请马上联系我开始摆龙门阵 如果你跟随这篇文章实现了播放器,那你会得到一个高效率,低cpu占用(单路720p视频解码播放占用1%左右cpu) ...
- 阿里云 RTC QoS 弱网对抗之 LTR 及其硬件解码支持
LTR 弱网对抗由于需要解码器的反馈,因此用硬件解码器实现时需要做一些特殊处理.另外,一些硬件解码器对 LTR 的实现不是特别完善,会导致出现解码错误.本文为 QoS 弱网优化系列的第三篇,将为您详解 ...
- 解密H264、AAC硬件解码的关键扩展数据处理
通过上一篇文章,我们用ffmpeg分离出一个多媒体容器中的音视频数据,但是很可能这些数据是不能被正确解码的.为什么呢?因为在解码这些数据之前,需要对解码器做一些配置,典型的就是目前流行的高清编码“黄金 ...
- (转) 解密H264、AAC硬件解码的关键扩展数据处理
出自:http://blog.itpub.net/30168498/viewspace-1576794/ 通过上一篇文章,我们用ffmpeg分离出一个多媒体容器中的音视频数据,但是很可能这 ...
- android 硬件解码学习
FileInputStream in = new FileInputStream("/sdcard/sample.ts"); String mimeType = "vid ...
随机推荐
- 【nginx】nginx的介绍
Nginx 反向代理初印象 Nginx (“engine x”) 是一个高性能的HTTP和反向代理 服务器,也是一个IMAP/POP3/SMTP服务器.其特点是占有内存少,并发能力强,事实上nginx ...
- FZU2187 回家种地(矩形面积并)
矩形面积并(只覆盖一次的面积)的裸题.好久没写代码debug了我太久,太辛酸了. #pragma warning(disable:4996) #include <iostream> #in ...
- LeetCode OJ-- Remove Nth Node From End of List
https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ remove倒数第n个节点 一般list remove node的 ...
- Cryptography I 学习笔记 --- 基于陷门置换的公钥加密
RSA算法的工作流程 1. 生成公钥私钥 生成两个素数p和q,计算n=p*q,计算φ(n)=n-p-q+1,然后生成e与d,使 e * d = 1 mod φ(n). 然后以(n, e)作为公钥,(n ...
- FZU 1077 铁皮容器 【枚举/二分】
Accept: 1040 Submit: 2314Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 使用白 ...
- java JIT AOT
作者:ETIN链接:https://zhuanlan.zhihu.com/p/27393316来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. I guess anyon ...
- Java使用HttpURLConnection调用WebService(原始方法)
说明:使用Java原生的HttpURLConnection调用WebService可以免去引入SOA的框架,比如一些CXF框架等.可以使代码足够精简,比如对于一些只调用一两个接口的,这种方式是最适合的 ...
- 【redis】java操作redis时,StringRedisTemplate的expire()方法的作用,什么时候使用
java操作redis时,StringRedisTemplate的expire()方法的作用,什么时候使用 //重新设置过期时间为30分钟,刷新时间 redisTemplate.expire(MsOp ...
- Android Studio和MAT结合使用来分析内存问题
Android开发中时常会遇到内存泄漏的问题,而Android系统对单个App又有一定的内存限制,此值可以通过一下方式获取: ActivityManager am = (ActivityManager ...
- node/webpack/react
node是运行引擎,通过他可以直接在后端运行js语法 webpack是打包工具 react是前端框架 通过 npm 使用 React 我们建议在 React 中使用 CommonJS 模块系统,比如 ...