// This returns the default or attempts to compute it, in its absence. public void GetDefaultStride(out int pnStride) { int nStride = 0; bool bFailed = false; // First try to get it from the attribute. try { int hr = GetMediaType().GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out nStride); MFError.ThrowExceptionForHR(hr); } catch { bFailed = true; } if (bFailed) { // Attribute not set. See if we can calculate the default stride. Guid subtype; int width = 0; int height = 0; // First we need the subtype . GetSubType(out subtype); // Now we need the image width and height. GetFrameDimensions(out width, out height); // Now compute the stride for a particular bitmap type FourCC f = new FourCC(subtype); int hr = MFExtern.MFGetStrideForBitmapInfoHeader(f.ToInt32(), width, out nStride); MFError.ThrowExceptionForHR(hr); // Set the attribute for later reference. SetDefaultStride(nStride); } pnStride = nStride; }
// Extracts the FOURCC code from the subtype. // Not all subtypes follow this pattern. public void GetFourCC(out int pFourCC) { Debug.Assert(IsValid()); Guid guidSubType; GetSubType(out guidSubType); FourCC f = new FourCC(guidSubType); pFourCC = f.ToInt32(); }
//----------------------------------------------------------------------------- // 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 = 0; plStride = 0; // Try to get the default stride from the media type. int hr = pType.GetUINT32(MFAttributesClsid.MF_MT_DEFAULT_STRIDE, out lStride); if (Failed(hr)) { // Attribute not set. Try to calculate the default stride. Guid subtype = Guid.Empty; int width = 0; int height = 0; // Get the subtype and the image size. hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype); if (Succeeded(hr)) { hr = MFGetAttributeSize(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out width, out height); } if (Succeeded(hr)) { FourCC f = new FourCC(subtype); hr = MediaFoundation.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 format. //------------------------------------------------------------------- public int SetVideoType(IMFMediaType pType) { int hr = S_Ok; Guid subtype; MFRatio PAR = new MFRatio(); // Find the video subtype. hr = pType.GetGUID(MFAttributesClsid.MF_MT_SUBTYPE, out subtype); if (Failed(hr)) { goto done; } // Choose a conversion function. // (This also validates the format type.) hr = SetConversionFunction(subtype); if (Failed(hr)) { goto done; } // // Get some video attributes. // // Get the frame size. hr = MFGetAttributeSize(pType, MFAttributesClsid.MF_MT_FRAME_SIZE, out m_width, out m_height); if (Failed(hr)) { goto done; } // Get the image stride. hr = GetDefaultStride(pType, out m_lDefaultStride); if (Failed(hr)) { goto done; } // Get the pixel aspect ratio. Default: Assume square pixels (1:1) hr = MFGetAttributeRatio(pType, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, out PAR.Numerator, out PAR.Denominator); if (Succeeded(hr)) { m_PixelAR = PAR; } else { m_PixelAR.Numerator = m_PixelAR.Denominator = 1; } FourCC f = new FourCC(subtype); m_format = (Format)f.ToInt32(); // Create Direct3D swap chains. hr = CreateSwapChains(); if (Failed(hr)) { goto done; } // Update the destination rectangle for the correct // aspect ratio. UpdateDestinationRect(); done: if (Failed(hr)) { m_format = Format.Unknown; m_convertFn = null; m_bmpconvertFn = null; } return hr; }
//----------------------------------------------------------------------------- // 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; }