You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FFmpeg+MediaCodec解码HEVC流:Android硬件加速有效性确认

Android下FFmpeg MediaCodec硬件解码HEVC流的疑问

背景

目标是在Android应用的C/C++库中,使用编译有MediaCodec的FFmpeg解码RTSP HEVC流。目前已实现解码功能,但无法确认是否真的启用了MediaCodec硬件加速,且测试发现Android上软解码的速度比当前方案更快。

核心问题

解码后获取的帧格式始终为AV_PIX_FMT_NV12,完全无法触发frame->format == AV_PIX_FMT_MEDIACODEC的分支逻辑。而在Windows平台使用hevc_cuvid等硬件加速编解码器时,能正常获取AV_PIX_FMT_CUDA格式的帧,两者表现不一致。当前无法确定这种现象是否正常,也无法确认是否真的在使用硬件加速。

相关代码

初始化代码

enum AVPixelFormat getHwFixelFormat(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts)
{
    return AV_PIX_FMT_MEDIACODEC;
}


AVFormatContext* formatCtx = avformat_alloc_context();
avformat_open_input(&formatCtx, m_link.c_str(), NULL, NULL);
avformat_find_stream_info(&formatCtx, NULL);
avformat_find_stream_info(&formatCtx, NULL);
m_videoStreamIdx = av_find_best_stream(&formatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_decoder, 0);;

const AVCodec* decoderHw = avcodec_find_decoder_by_name("hevc_mediacodec");
m_decoder = decoderHw;

AVCodecContext* decoderCtx = avcodec_alloc_context3(m_decoder);
avcodec_parameters_to_context(decoderCtx, m_decodeParams);

m_hwPixFormat = AV_PIX_FMT_MEDIACODEC;
decoderCtx->get_format = getHwFixelFormat;

AVBufferRef* hwContext = nullptr;
av_hwdevice_ctx_create(&hwContext, m_currHwDecoder->m_devType, nullptr, nullptr, 0);
decoderCtx->hw_device_ctx = av_buffer_ref(hwContext);
avcodec_open2(decoderCtx, m_decoder, nullptr);

帧接收代码

avcodec_receive_frame(&decoder, frame);

if (frame->format == m_hwPixFormat) {
        /* Branch never reached on android */
    if (av_hwframe_transfer_data(swFrame, frame, 0) < 0) {
        /* Error while copying from hw -> continue reading */
    }
    decodedFrame = &swframeDeleterPtr;
} else {
    decodedFrame = &frameDeleterPtr;
}

内容的提问来源于stack exchange,提问作者TheMrianura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 17:23:36