无法为硬件加速解码、FFMPEG、Android 初始化“h264_mediacodec”

问题描述 投票:0回答:1

我正在尝试通过 JNI 在 Android 上创建硬件加速解码并遵循此示例,但不幸的是,

avcodec_get_hw_config
返回 nullptr。

我也尝试过使用

avcodec_find_decoder_by_name("h264_mediacodec")
,也返回nullptr。

我使用带有标志的this脚本构建了ffmpeg(版本4.4):

--enable-jni \
--enable-mediacodec \
--enable-decoder=h264_mediacodec \
--enable-hwaccel=h264_mediacodec \

配置构建时我在日志中看到

WARNING: Option --enable-hwaccel=h264_mediacodec did not match anything
,这实际上很奇怪。 FFMPEG 4.4 应该支持使用媒体编解码器的硬件加速解码。

编辑:(提供最小的可重现示例)

在JNI方法中我初始化解码器的输入上下文并初始化解码器:

    void Decoder::initInputContext(
        const std::string& source,
        AVDictionary* options
    ) { // open input, and allocate format context
        if (
            avformat_open_input(
                &m_inputFormatContext,
                source.c_str(),
                NULL,
                options ? &options : nullptr
            ) < 0
        ) {
            throw FFmpegException(
                fmt::format("Decoder: Could not open source {}", source)
            );
        }
 
        // retrieve stream information
        if (avformat_find_stream_info(m_inputFormatContext, NULL) < 0) {
            throw FFmpegException(
                "Decoder: Could not find stream information"
            );
        }

        // get audio and video streams
        for (size_t i = 0; i < m_inputFormatContext->nb_streams; i++) {
            AVStream* inStream = m_inputFormatContext->streams[i];
            AVCodecParameters* inCodecpar = inStream->codecpar;
            if (
                inCodecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
                inCodecpar->codec_type != AVMEDIA_TYPE_VIDEO
            ) {
                continue;
            }

            if (inCodecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
                m_videoStreamIdx = i;
                m_videoStream = inStream;

                m_codecParams.videoCodecId = m_videoStream->codecpar->codec_id;
                m_codecParams.fps = static_cast<int>(av_q2d(m_videoStream->r_frame_rate) + 0.5);
                m_codecParams.clockrate = m_videoStream->time_base.den;

                spdlog::debug(
                    "Decoder: fps: {}, clockrate: {}",
                    m_codecParams.fps,
                    m_codecParams.clockrate
                )
                ;
            }

            if (inCodecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
                m_audioStreamIdx = i;
                m_audioStream = inStream;

                m_codecParams.audioCodecId = m_audioStream->codecpar->codec_id;
                m_codecParams.audioSamplerate = m_audioStream->codecpar->sample_rate;
                m_codecParams.audioChannels = m_audioStream->codecpar->channels;
                m_codecParams.audioProfile = m_audioStream->codecpar->profile;

                spdlog::debug(
                    "Decoder: audio samplerate: {}, audio channels: {}, x: {}",
                    m_codecParams.audioSamplerate,
                    m_codecParams.audioChannels,
                    m_audioStream->codecpar->channels
                )
                ;
            }
        }
    }

    void Decoder::initDecoder() {
        AVCodecParameters* videoStreamCodecParams = m_videoStream->codecpar;

        m_swsContext = sws_getContext(
                videoStreamCodecParams->width, videoStreamCodecParams->height, m_pixFormat,
                videoStreamCodecParams->width, videoStreamCodecParams->height, m_targetPixFormat,
                SWS_BICUBIC, nullptr, nullptr, nullptr);

        // find best video stream info and decoder
        int ret = av_find_best_stream(m_inputFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, &m_decoder, 0);
        if (ret < 0) {
            throw FFmpegException(
                    "Decoder: Cannot find a video stream in the input file"
            );
        }

        if (!m_decoder) {
            throw FFmpegException(
                    "Decoder: Can't find decoder"
            );
        }

        // search for supported HW decoder configuration
        for (size_t i = 0;; i++) {
            const AVCodecHWConfig* config = avcodec_get_hw_config(m_decoder, i);
            if (!config) {
                spdlog::error(
                    "Decoder {} does not support device type {}. "
                    "Will use SW decoder...",
                    m_decoder->name,
                    av_hwdevice_get_type_name(m_deviceType)
                );
                break;
            }

            if (
                config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
                config->device_type == m_deviceType
            ) {
                // set up pixel format for HW decoder
                g_hwPixFmt = config->pix_fmt;
                m_hwDecoderSupported = true;
                break;
            }
        }
    }

我有

AVHWDeviceType m_deviceType{AV_HWDEVICE_TYPE_MEDIACODEC};

avcodec_get_hw_config
返回 nullptr。

如有任何帮助,我们将不胜感激。

android ffmpeg hardware-acceleration
1个回答
0
投票

我找到了 this 示例,按照它设置 CMakeLists.txt 并初始化解码器的方式进行操作,它对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.