Windows Phone 8.1 Media Foundation H264 Maximum Resolution
I am trying to encode a video on Windows Phone 8.1 using Media Foundation library and recorder.
I managed to achieve this by setting MFVideoFormat_H264
both MF_MT_SUBTYPE
for my media output and using resolutions such as 720p and 480p.
But when I change the resolution to 1920x1080 (or 1920x1088), I get an error Incorrect Parameter
. so I am assuming my max resolution for H.264 codec is 1280x720.
I tried changing the codec to HVEC or MPEG2, etc., but no luck.
This is the cpp code where I set the output type and add it to the stream:
// Setup the output video type
ComPtr<IMFMediaType> spvideoTypeOut;
CHK(MFCreateMediaType(&spvideoTypeOut));
CHK(spvideoTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
GUID _vformat = MFVideoFormat_H264;
CHK(spvideoTypeOut->SetGUID(MF_MT_SUBTYPE, _vformat));
CHK(spvideoTypeOut->SetUINT32(MF_MT_AVG_BITRATE, _bitrate));
CHK(spvideoTypeOut->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
CHK(MFSetAttributeSize(spvideoTypeOut.Get(), MF_MT_FRAME_SIZE, _width, _height));
CHK(MFSetAttributeRatio(spvideoTypeOut.Get(), MF_MT_FRAME_RATE, framerate, 1));
CHK(MFSetAttributeRatio(spvideoTypeOut.Get(), MF_MT_PIXEL_ASPECT_RATIO, ASPECT_NUM, ASPECT_DENOM));
CHK(_spSinkWriter->AddStream(spvideoTypeOut.Get(), &_streamIndex));
And here I am setting the input type:
// Setup the input video type
ComPtr<IMFMediaType> spvideoTypeIn;
CHK(MFCreateMediaType(&spvideoTypeIn));
CHK(spvideoTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video));
CHK(spvideoTypeIn->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB32));
CHK(spvideoTypeIn->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive));
CHK(MFSetAttributeSize(spvideoTypeIn.Get(), MF_MT_FRAME_SIZE, _width, _height));
CHK(MFSetAttributeRatio(spvideoTypeIn.Get(), MF_MT_FRAME_RATE, framerate, 1));
CHK(MFSetAttributeRatio(spvideoTypeIn.Get(), MF_MT_PIXEL_ASPECT_RATIO, ASPECT_NUM, ASPECT_DENOM));
CHK(_spSinkWriter->SetInputMediaType(_streamIndex, spvideoTypeIn.Get(), nullptr));
CHK(_spSinkWriter->BeginWriting());
To add samples to the recorder, I use this function and here's where the exception is thrown:
void PictureWriter::AddFrame(const Platform::Array<uint8>^ videoFrameBuffer, int imageWidth, int imageHeight)
{
// Create a media sample
ComPtr<IMFSample> spSample;
CHK(MFCreateSample(&spSample));
CHK(spSample->SetSampleDuration(_duration));
CHK(spSample->SetSampleTime(_hnsSampleTime));
_hnsSampleTime += _duration;
// Add a media buffer
ComPtr<IMFMediaBuffer> spBuffer;
CHK(MFCreateMemoryBuffer(_bufferLength, &spBuffer));
CHK(spBuffer->SetCurrentLength(_bufferLength));
CHK(spSample->AddBuffer(spBuffer.Get()));
// Copy the picture into the buffer
unsigned char *pbBuffer = nullptr;
CHK(spBuffer->Lock(&pbBuffer, nullptr, nullptr));
BYTE* buffer = (BYTE*)videoFrameBuffer->begin() + 4 * imageWidth * (imageHeight - 1);
CHK(MFCopyImage(pbBuffer + 4 * _width * (_height - imageHeight),
4 * _width, buffer, -4 * imageWidth, 4 * imageWidth, imageHeight));
CHK(spBuffer->Unlock());
// Write the media sample
CHK(_spSinkWriter->WriteSample(_streamIndex, spSample.Get()));
}
Why do you think I am getting an exception and how can I fix it?
Thank.
source to share