Issue enabling hw Decode DXVA2

For the developers that use FFmpeg in Windows software.
Post Reply
luigif1
Posts: 2
Joined: Thu Oct 05, 2017 9:32 am

Issue enabling hw Decode DXVA2

Post by luigif1 » Thu Oct 05, 2017 10:17 am

Hi,
I'm trying to enable DXVA2 hw decoding in my code.
I'm following the example hw_decode.c, so the steps to perform are:
  • create and attach hw_device_ctx before calling avcodec_open2
  • set the callback get_format
  • return the desired pixel format (AV_PIX_FMT_DXVA2_VLD) inside get_format callback
The issue here is avcodec_send_packet returns AVERROR_INVALIDDATA.
I cannot figure out what is the problem.. if I set-up get_hw_format to return AV_PIX_FMT_YUVJ420P (a supported pixel format), everything works fine..
Any help will be appretiated.

Thanks!
Luigi

Code: Select all

AVCodecContext* pDecoder = avcodec_alloc_context3(m_codec);
pDecoder->workaround_bugs = FF_BUG_AUTODETECT;
pDecoder->flags2 = CODEC_FLAG2_FAST;
pDecoder->thread_count = get_decoder_thread_count();
if (!m_decoderConfig.empty())
{
	pDecoder->extradata_size = m_decoderConfig.size();
	pDecoder->extradata = &m_decoderConfig[0];
}

pDecoder->get_format = get_hw_format;
AVBufferRef *hw_device_ctx = NULL;
if ((av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_DXVA2, NULL, NULL, 0)) >= 0)
	pDecoder->hw_device_ctx = av_buffer_ref(hw_device_ctx);

AVDictionary* opts = NULL;
avcodec_open2(pDecoder, m_codec, &opts);

Code: Select all

static enum AVPixelFormat get_hw_format(AVCodecContext *pDecoder, const enum AVPixelFormat *pix_fmts)
{
	const enum AVPixelFormat *p;
	for (p = pix_fmts; *p != -1; p++)
	{
		if (*p == AV_PIX_FMT_DXVA2_VLD)
			return *p;
	}
	return AV_PIX_FMT_NONE;
}

Code: Select all

AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = reinterpret_cast<uint8_t*>(slice.inBuf);
avpkt.size = slice.inBufSize;
if ((ret = avcodec_send_packet(decoder, &avpkt)) != 0)
	return -1;
...

luigif1
Posts: 2
Joined: Thu Oct 05, 2017 9:32 am

Re: Issue enabling hw Decode DXVA2

Post by luigif1 » Wed Oct 11, 2017 4:06 pm

Adding the following code make the difference:

Code: Select all

av_opt_set_int(pDecoder, "refcounted_frames", 1, 0);

Post Reply