I am trying to create hw accelerated decoding on Android through JNI and following this example but, unfortunately, avcodec_get_hw_config
returns nullptr.
I have also tried using avcodec_find_decoder_by_name("h264_mediacodec")
, also returns nullptr.
I built ffmpeg (version 4.4) using this script with the flags:
--enable-jni \
--enable-mediacodec \
--enable-decoder=h264_mediacodec \
--enable-hwaccel=h264_mediacodec \
When configuring build I saw in logs WARNING: Option --enable-hwaccel=h264_mediacodec did not match anything
, which is actually strange. FFMPEG 4.4 should support hw accelerated decoding using mediacodec.
Edit: (providing minimal reproducible example)
In the JNI method I init input context of decoder and init decoder:
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;
}
}
}
And I have AVHWDeviceType m_deviceType{AV_HWDEVICE_TYPE_MEDIACODEC};
avcodec_get_hw_config
returns nullptr.
Any help is appreciated.
I found this example, followed the way it sets up CMakeLists.txt and initializes decoder and it worked for me.