问题
I'm able to successfully encode H264 video using MediaFoundation.
Now I want to export HEVC which is, according to official documentation, is supported.
I am doing everything as described there but whenever I want to set input media type to the writer it ends up with an error:
No suitable transform was found to encode or decode the content.
Here is a short code of what i do:
/* Set output media type */
IMFMediaType* pMediaTypeOut = nullptr;
MFCreateMediaType(&pMediaTypeOut);
pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_HEVC);
MFSetAttributeSize(pMediaTypeOut, MF_MT_FRAME_SIZE, 640, 480);
MFSetAttributeRatio(pMediaTypeOut, MF_MT_FRAME_RATE, 30, 1);
pMediaTypeOut->SetUINT32(MF_MT_MPEG2_PROFILE, eAVEncH265VProfile_Main_420_8);
pMediaTypeOut->SetUINT32(MF_MT_MPEG2_LEVEL, eAVEncH265VLevel1);
// Add it to the sink writer
m_pWriter->AddStream(pMediaTypeOut, &m_streamIndex);
/* Set input media type */
IMFMediaType* pMediaTypeIn = nullptr;
MFCreateMediaType(&pMediaTypeIn);
pMediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
pMediaTypeIn->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12);
MFSetAttributeSize(pMediaTypeIn, MF_MT_FRAME_SIZE, 640, 480);
MFSetAttributeRatio(pMediaTypeIn, MF_MT_FRAME_RATE, 30, 1),
MFSetAttributeRatio(pMediaTypeIn, MF_MT_PIXEL_ASPECT_RATIO, 1, 1),
// Here it ends up with an error
m_pWriter->SetInputMediaType(m_streamIndex, pMediaTypeIn, nullptr);
Does anyone know if the HEVC encoding is really supported or it is just a documentation. And if it is really supported by Microsoft, then what am I doing wrong?
BTW: I am using latest version of Windows 10. Error checking is omitted for code simplicity
回答1:
Maybe you can check the existence of the h265 encoder first by DXVA Checker tool or programmatically use MFEnumEx(MFT_CATEGORY_VIDEO_ENCODER, ...) to lookup.
If not found, you can try installing the HEVC Video Extension on MS App Store. It seems MS has separated the MF HEVC codec into app package after 2018 Fall Creator update. Also, the HW HEVC Encoder needed be installed via vendor's VGA driver.
回答2:
Your output media type is incomplete:
MF_MT_AVG_BITRATE
Average encoded bit rate, in bits per second. Must be greater than zero.
Similar problem with MF_MT_INTERLACE_MODE
.
Code snippet that works:
Check(MFStartup(MF_VERSION));
ComPtr<IMFSinkWriter> SinkWriter;
Check(MFCreateSinkWriterFromURL(L"D:\\temp.mp4", nullptr, nullptr, SinkWriter.GetAddressOf()));
ComPtr<IMFMediaType> OutputMediaType;
Check(MFCreateMediaType(OutputMediaType.GetAddressOf()));
Check(OutputMediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
Check(OutputMediaType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_HEVC));
Check(MFSetAttributeSize(OutputMediaType.Get(), MF_MT_FRAME_SIZE, 640, 480));
Check(OutputMediaType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
Check(MFSetAttributeRatio(OutputMediaType.Get(), MF_MT_FRAME_RATE, 30, 1));
//Check(OutputMediaType->SetUINT32(MF_MT_MPEG2_PROFILE, eAVEncH265VProfile_Main_420_8));
//Check(OutputMediaType->SetUINT32(MF_MT_MPEG2_LEVEL, eAVEncH265VLevel1));
Check(OutputMediaType->SetUINT32(MF_MT_AVG_BITRATE, 1000000));
DWORD StreamIndex;
Check(SinkWriter->AddStream(OutputMediaType.Get(), &StreamIndex));
ComPtr<IMFMediaType> InputMediaType;
Check(MFCreateMediaType(InputMediaType.GetAddressOf()));
Check(InputMediaType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
Check(InputMediaType->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_NV12));
Check(MFSetAttributeSize(InputMediaType.Get(), MF_MT_FRAME_SIZE, 640, 480));
Check(InputMediaType->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
Check(MFSetAttributeRatio(InputMediaType.Get(), MF_MT_FRAME_RATE, 30, 1)),
//Check(MFSetAttributeRatio(InputMediaType.Get(), MF_MT_PIXEL_ASPECT_RATIO, 1, 1)),
Check(SinkWriter->SetInputMediaType(StreamIndex, InputMediaType.Get(), nullptr));
来源:https://stackoverflow.com/questions/53291761/mediafoundation-hevc-h265-encoding