//----------------------------------------------------------------------------- // GetDefaultStride // // Gets the default stride for a video frame, assuming no extra padding bytes. // //----------------------------------------------------------------------------- private static int GetDefaultStride(IMFMediaType pType, out int plStride) { int lStride; plStride = 0; // Try to get the default stride from the media type. var hr = pType.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStride); if (Failed(hr)) { // Attribute not set. Try to calculate the default stride. Guid subtype; var width = 0; // ReSharper disable once TooWideLocalVariableScope // ReSharper disable once RedundantAssignment var height = 0; // Get the subtype and the image size. hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype); if (Succeeded(hr)) { hr = CProcess.MfGetAttributeSize(pType, out width, out height); } if (Succeeded(hr)) { var f = new FourCC(subtype); hr = MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out lStride); } // Set the attribute for later reference. if (Succeeded(hr)) { hr = pType.SetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, lStride); } } if (Succeeded(hr)) { plStride = lStride; } return(hr); }
//------------------------------------------------------------------- // SetVideoType // // Set the video snapFormat. //------------------------------------------------------------------- public int SetVideoType(IMFMediaType pType) { Guid subtype; var PAR = new MFRatio(); // Find the video subtype. var hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype); try { if (Failed(hr)) { throw new Exception(); } // Choose a conversion function. // (This also validates the snapFormat type.) hr = SetConversionFunction(subtype); if (Failed(hr)) { throw new Exception(); } // // Get some video attributes. // // Get the frame size. hr = CProcess.MfGetAttributeSize(pType, out width, out height); if (Failed(hr)) { throw new Exception(); } // Get the image stride. hr = GetDefaultStride(pType, out lDefaultStride); if (Failed(hr)) { throw new Exception(); } // Get the pixel aspect ratio. Default: Assume square pixels (1:1) hr = CProcess.MfGetAttributeRatio(pType, out PAR.Numerator, out PAR.Denominator); if (Succeeded(hr)) { pixelAR = PAR; } else { pixelAR.Numerator = pixelAR.Denominator = 1; } var f = new FourCC(subtype); format = (Format)f.ToInt32(); // Create Direct3D swap chains. hr = CreateSwapChains(); if (Failed(hr)) { throw new Exception(); } // Update the destination rectangle for the correct // aspect ratio. UpdateDestinationRect(); } finally { if (Failed(hr)) { format = Format.Unknown; m_convertFn = null; } } return(hr); }