Search code examples
windows-phone-8.1h.264ms-media-foundation

Windows Phone 8.1 Media Foundation H264 max resolution


I'm trying to encode a video in Windows Phone 8.1 using Media Foundation library and sink writer.

I have been able to achieve this by setting MFVideoFormat_H264 as MF_MT_SUBTYPE for my media output and using resolutions like 720p and 480p..

But when I change the resolution to 1920x1080 (or 1920x1088) I get an Incorrect Parameter error. so I guess my maximum 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 setup the output type and Add it to 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 this is where I setup 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 sink writer I am using this function, and this is where the exception occurs:

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 get the exception and how can I fix this?

Thank you.


Solution

  • Found the solution by searching for default bitrates for each resolution,

    1080p Works with bitrate of 5.0 Mbps,

    1600x900 works with bitrate of 2.5 Mbps,

    720p works with bitrate of 1.25 Mbps...