我有一个 ImageReader,它的表面连接到 MediaCodec 解码器以进行渲染。
AMediaCodec *videoDecoder = nullptr;
ANativeWindow* surface = nullptr;
AImageReader* imageReader = nullptr;
AImageReader_ImageListener* imageListener = nullptr;
if ((videoDecoder = AMediaCodec_createDecoderByType(mime)))
{
if (AImageReader_new(mWidth, mHeight, AIMAGE_FORMAT_YUV_420_888, 2, &imageReader) == AMEDIA_OK)
{
if (AImageReader_getWindow(imageReader, &surface) == AMEDIA_OK)
{
if (AMediaCodec_configure(videoDecoder, mediaFormat, surface, NULL, 0) == AMEDIA_OK)
{
int32_t outputFormat{};
AMediaFormat_getInt32(AMediaCodec_getOutputFormat(videoDecoder), AMEDIAFORMAT_KEY_COLOR_FORMAT, &outputFormat);
imageListener = new AImageReader_ImageListener();
imageListener->onImageAvailable = &onImageAvailableCallback;
AImageReader_setImageListener(imageReader, imageListener);
if (AMediaCodec_start(videoDecoder) == AMEDIA_OK)
{
configCompleted = true;
}
else
{
TRACE("ImporterMP4Android", 0, "Failed to Start Video Decoder");
}
}
else
{
TRACE("ImporterMP4Android", 0, "Failed to Configure Video Decoder");
}
}
else
{
TRACE("ImporterMP4Android", 0, "Failed to Fetch Surface owned by the ImageReader");
}
}
else
{
TRACE("ImporterMP4Android", 0, "Failed to Create ImageReader");
}
}
else
{
TRACE("ImporterMP4Android", 0, "Failed to Create Decoder");
}
onImageAvailableCallback
看起来像这样的自动取款机:
void onImageAvailableCallback(void *context, AImageReader *reader)
{
int32_t format;
media_status_t status = AImageReader_getFormat (reader, &format);
AImage *image;
status = AImageReader_acquireLatestImage(reader, &image);
status = AImage_getFormat(image, &format);
// TODO: copy *raw data somewhere for downstream processing
AImage_delete(image);
}
如 TODO 评论中所示,我想复制所获取的原始数据以进行进一步处理。Image 类提供的接口允许我查询平面的数量并获取单个平面数据,但我有兴趣一次抓取整个帧。关于我如何做到这一点的任何建议?Image
ImageReader
简而言之,我正在使用 MediaCodec 视频解码器渲染到 ImageReader 拥有的 Surface 中,并最终希望以YUV420NV21 格式从 ImageReader 中获取解码后的视频帧以进行进一步处理。