Windows Phone 8.1 Media Foundation H264 max resolution

时间:2017-04-06 17:17:49

标签: windows-phone-8.1 h.264 ms-media-foundation

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.

1 个答案:

答案 0 :(得分:2)

通过搜索每个分辨率的默认比特率找到解决方案,

1080p的比特率为5.0 Mbps,

1600x900的比特率为2.5 Mbps,

720p的比特率为1.25 Mbps ...