|
【Android】
RK3288 MediaCodec ndk 下解码错误 求助
发表于 2020-7-16 12:11:46
浏览:6537
|
回复:1
打印
只看该作者
[复制链接]
楼主
解码H264 代码如下
AMediaCodec_queueInputBuffer 先是正确返回>=0 一两分钟后
后面就是返回-1,一直解码不成功
#include "DecodeOp.h"
#include <media/NdkMediaCodec.h>
//#include "CommonOp.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <android/log.h>
#include <pthread.h>
#define LOG_TAG "C_MDA"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define MIN(_a,_b) ((_a) > (_b) ? (_b) : (_a))
int DecodeOp::init(int width, int height)
{
const char *mine = "video/avc";
mMediaCodec = AMediaCodec_createDecoderByType(mine);
//注意这里,如果系统不存在对应的mine,mMediaCodec也会返回对象,
//不过之后涉及到mMediaCodec的任何函数都会直接导致程序崩掉。
if (NULL == mMediaCodec)
{
return 0;
}
AMediaFormat *videoFormat = AMediaFormat_new();
AMediaFormat_setString(videoFormat, "mime", "video/avc");
AMediaFormat_setInt32(videoFormat, AMEDIAFORMAT_KEY_WIDTH, width);
AMediaFormat_setInt32(videoFormat, AMEDIAFORMAT_KEY_HEIGHT, height);
media_status_t status = AMediaCodec_configure(mMediaCodec, videoFormat, NULL, NULL, 0);
if (status != AMEDIA_OK)
{
AMediaCodec_delete(mMediaCodec);
mMediaCodec = NULL;
return 0;
}
mWidth = width;
mHeight = height;
mFramecnt=0;
status = AMediaCodec_start(mMediaCodec);
if (status != AMEDIA_OK)
{
AMediaCodec_delete(mMediaCodec);
mMediaCodec = NULL;
return 0;
}
return 1;
}
int DecodeOp::fini()
{
AMediaCodec_stop(mMediaCodec);
AMediaCodec_delete(mMediaCodec);
mMediaCodec = NULL;
return 1;
}
int DecodeOp::decode(char *bufData, int bufSize, char *outBuf,pthread_mutex_t *mutex) {
if (NULL == mMediaCodec)
return 0;
ssize_t bufidx = AMediaCodec_dequeueInputBuffer(mMediaCodec, 10000); //-1);
if (bufidx >= 0) {
// 获取buffer的索引
size_t outsize;
uint8_t *inputBuf = AMediaCodec_getInputBuffer(mMediaCodec, bufidx, &outsize);
if (inputBuf != nullptr && bufSize <= outsize) {
// 将待解码的数据copy到硬件中
memcpy(inputBuf, bufData, bufSize);
media_status_t status = AMediaCodec_queueInputBuffer(mMediaCodec, bufidx, 0, bufSize,5*mFramecnt++ /* pts */, 0);
}
//LOGD("ie:%lld",mFramecnt);
}else
{
LOGD("ib:%d",bufidx);
AMediaCodec_flush(mMediaCodec);
AMediaCodec_start(mMediaCodec);
}
int ret=0;
AMediaCodecBufferInfo info;
ssize_t outbufidx = AMediaCodec_dequeueOutputBuffer(mMediaCodec, &info, 0);
//LOGD("ob:%d",outbufidx);
if (outbufidx >= 0) {
size_t outsize;
uint8_t *outputBuf = AMediaCodec_getOutputBuffer(mMediaCodec, outbufidx, &outsize);
LOGD("obs:%d",outsize);
if (outputBuf != nullptr) {
ret= 1;
}
AMediaCodec_releaseOutputBuffer(mMediaCodec, outbufidx, false);
} else {
switch (outbufidx) {
case AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED:
// 解码输出的格式发生变化
{
auto format = AMediaCodec_getOutputFormat(mMediaCodec);
AMediaFormat_getInt32(format, "width", &mWidth);
AMediaFormat_getInt32(format, "height", &mHeight);
int32_t localColorFMT;
AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_COLOR_FORMAT, &localColorFMT);
}
case AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED:
break;
default:
break;
}
}
return ret;
} |
|