public static byte[] GetDataFromMediaSample(IMFSample sample) { IMFMediaBuffer pMediaBuffer = null; IntPtr pBuffer; int maxLen = 0, curLen = 0; //HResult hr = sample.LockStore(); HResult hr = sample.ConvertToContiguousBuffer(out pMediaBuffer); MFError.ThrowExceptionForHR(hr); try { hr = pMediaBuffer.Lock(out pBuffer, out maxLen, out curLen); MFError.ThrowExceptionForHR(hr); try { byte[] data = new byte[curLen]; Marshal.Copy(pBuffer, data, 0, curLen); return(data); } finally { hr = pMediaBuffer.Unlock(); } } finally { //hr = sample.UnlockStore(); COMBase.SafeRelease(pMediaBuffer); } }
// Get the buffers and sizes to be modified, then pass them // to the transform routine. private void DoWork(IMFMediaBuffer pIn, IMFMediaBuffer pOut) { IntPtr pSrc = IntPtr.Zero; int lSrcStride = 0; IMF2DBuffer pIn2D = null; // Lock the input buffer. Use IMF2DBuffer if available. Lockit(pIn, out pIn2D, out lSrcStride, out pSrc); try { IntPtr pDest; int mlo; int cb; MFError throwonhr = pOut.Lock(out pDest, out mlo, out cb); try { // Invoke the image transform function. YUY2_TO_RGB32(pSrc, pDest, m_imageWidthInPixels, m_imageHeightInPixels, lSrcStride); } finally { pOut.Unlock(); } } finally { // Ignore error UnlockIt(pSrc, pIn2D, pIn); } }
// Token: 0x06000A59 RID: 2649 RVA: 0x0001E1A8 File Offset: 0x0001C3A8 private IMFSample ReadFromSource() { int num = this.sourceProvider.Read(this.sourceBuffer, 0, this.sourceBuffer.Length); if (num == 0) { return(null); } IMFMediaBuffer imfmediaBuffer = MediaFoundationApi.CreateMemoryBuffer(num); IntPtr destination; int num2; int num3; imfmediaBuffer.Lock(out destination, out num2, out num3); Marshal.Copy(this.sourceBuffer, 0, destination, num); imfmediaBuffer.Unlock(); imfmediaBuffer.SetCurrentLength(num); IMFSample imfsample = MediaFoundationApi.CreateSample(); imfsample.AddBuffer(imfmediaBuffer); imfsample.SetSampleTime(this.inputPosition); long num4 = MediaFoundationTransform.BytesToNsPosition(num, this.sourceProvider.WaveFormat); imfsample.SetSampleDuration(num4); this.inputPosition += num4; Marshal.ReleaseComObject(imfmediaBuffer); return(imfsample); }
// Token: 0x06000945 RID: 2373 RVA: 0x0001B0A0 File Offset: 0x000192A0 private long ConvertOneBuffer(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer) { long num = 0L; IMFMediaBuffer imfmediaBuffer = MediaFoundationApi.CreateMemoryBuffer(managedBuffer.Length); int count; imfmediaBuffer.GetMaxLength(out count); IMFSample imfsample = MediaFoundationApi.CreateSample(); imfsample.AddBuffer(imfmediaBuffer); IntPtr destination; int num2; imfmediaBuffer.Lock(out destination, out count, out num2); int num3 = inputProvider.Read(managedBuffer, 0, count); if (num3 > 0) { num = MediaFoundationEncoder.BytesToNsPosition(num3, inputProvider.WaveFormat); Marshal.Copy(managedBuffer, 0, destination, num3); imfmediaBuffer.SetCurrentLength(num3); imfmediaBuffer.Unlock(); imfsample.SetSampleTime(position); imfsample.SetSampleDuration(num); writer.WriteSample(streamIndex, imfsample); } else { imfmediaBuffer.Unlock(); } Marshal.ReleaseComObject(imfsample); Marshal.ReleaseComObject(imfmediaBuffer); return(num); }
// Get the buffers and sizes to be modified, then pass them // to the appropriate Update_* routine. private HResult DoWork(IMFMediaBuffer pIn) { MFError throwonhr; int cb; IntPtr pSrc; // Source buffer. int lSrcStride = 0; // Source stride. bool bLockedInputBuffer = false; IMF2DBuffer pIn2D; // While there are exceptions thrown here, they should never happen // in real life. // Lock the input buffer. Use IMF2DBuffer if available. pIn2D = pIn as IMF2DBuffer; if (pIn2D != null) { throwonhr = pIn2D.Lock2D(out pSrc, out lSrcStride); } else { int ml; throwonhr = pIn.Lock(out pSrc, out ml, out cb); lSrcStride = m_lStrideIfContiguous; } bLockedInputBuffer = true; // Invoke the image transform function. if (m_pTransformFn != null) { m_pTransformFn(pSrc, lSrcStride, m_imageWidthInPixels, m_imageHeightInPixels); } else { return(HResult.E_UNEXPECTED); } if (bLockedInputBuffer) { if (pIn2D != null) { throwonhr = pIn2D.Unlock2D(); } else { throwonhr = pIn.Unlock(); } } // Set the data size on the output buffer. throwonhr = pIn.SetCurrentLength(m_cbImageSize); return(HResult.S_OK); }
private long ConvertOneBuffer(IMFSinkWriter writer, int streamIndex, IWaveProvider inputProvider, long position, byte[] managedBuffer, int seconds, ref bool flag) { long durationConverted = 0; int maxLength; IMFMediaBuffer buffer = MediaFoundationApi.CreateMemoryBuffer(managedBuffer.Length); buffer.GetMaxLength(out maxLength); IMFSample sample = MediaFoundationApi.CreateSample(); sample.AddBuffer(buffer); IntPtr ptr; int currentLength; buffer.Lock(out ptr, out maxLength, out currentLength); int oneLength = inputProvider.WaveFormat.AverageBytesPerSecond; int read = 0; if (flag) { for (int i = 0; i < seconds; i++) { read = inputProvider.Read(managedBuffer, 0, oneLength); } flag = false; } else { read = inputProvider.Read(managedBuffer, 0, oneLength); } if (read > 0) { durationConverted = BytesToNsPosition(read, inputProvider.WaveFormat); Marshal.Copy(managedBuffer, 0, ptr, read); buffer.SetCurrentLength(read); buffer.Unlock(); sample.SetSampleTime(position); sample.SetSampleDuration(durationConverted); writer.WriteSample(streamIndex, sample); //writer.Flush(streamIndex); } else { buffer.Unlock(); } Marshal.ReleaseComObject(sample); Marshal.ReleaseComObject(buffer); return(durationConverted); }
//------------------------------------------------------------------- // LockBuffer // // Locks the buffer. Returns a pointer to scan line 0 and returns the stride. // // The caller must provide the default stride as an input parameter, in case // the buffer does not expose IMF2DBuffer. You can calculate the default stride // from the media type. //------------------------------------------------------------------- public int LockBuffer( int lDefaultStride, // Minimum stride (with no padding). int dwHeightInPixels, // Height of the image, in pixels. out IntPtr ppbScanLine0, // Receives a pointer to the start of scan line 0. out int plStride // Receives the actual stride. ) { int hr; ppbScanLine0 = IntPtr.Zero; plStride = 0; // Use the 2-D version if available. if (p2DBuffer != null) { hr = p2DBuffer.Lock2D(out ppbScanLine0, out plStride); } else { // Use non-2D version. IntPtr pData; int pcbMaxLength; int pcbCurrentLength; hr = pBuffer.Lock(out pData, out pcbMaxLength, out pcbCurrentLength); if (Succeeded(hr)) { plStride = lDefaultStride; if (lDefaultStride < 0) { // Bottom-up orientation. Return a pointer to the start of the // last row *in memory* which is the top row of the image. ppbScanLine0 += lDefaultStride * (dwHeightInPixels - 1); } else { // Top-down orientation. Return a pointer to the start of the // buffer. ppbScanLine0 = pData; } } } bLocked = (Succeeded(hr)); return(hr); }
private void Lockit(IMFMediaBuffer pOut, out IMF2DBuffer pOut2D, out int lDestStride, out IntPtr pDest) { MFError throwonhr; pOut2D = pOut as IMF2DBuffer; if (pOut2D != null) { throwonhr = pOut2D.Lock2D(out pDest, out lDestStride); } else { int ml; int cb; throwonhr = pOut.Lock(out pDest, out ml, out cb); lDestStride = m_lStrideIfContiguous; } }
/// <summary> /// Gives the caller access to the memory in the value, for reading or writing. /// </summary> /// <param name="mediaBuffer">A valid IMFMediaBuffer instance.</param> /// <param name="streamIndex">Receives a memory streamIndex that wrap the media value memory.</param> /// <returns>If this function succeeds, it returns the S_OK member. Otherwise, it returns another HResult's member that describe the error.</returns> /// <remarks>The media value must be unlocked with IMFMediaBuffer.Unlock.</remarks> public static HResult Lock(this IMFMediaBuffer mediaBuffer, out UnmanagedMemoryStream stream) { if (mediaBuffer == null) { throw new ArgumentNullException("mediaBuffer"); } stream = null; IntPtr bufferPtr; int maxLength, currentLength; HResult hr = mediaBuffer.Lock(out bufferPtr, out maxLength, out currentLength); if (hr.Succeeded()) { unsafe { stream = new UnmanagedMemoryStream((byte *)bufferPtr, currentLength, maxLength, FileAccess.ReadWrite); } } return(hr); }
///////////////////////////////////////////////////////////////////// // Name: ReadDataIntoBuffer // // Reads data from a byte stream and returns a media buffer that // contains the data. // // pStream: Pointer to the byte stream // cbToRead: Number of bytes to read // ppBuffer: Receives a pointer to the buffer. ///////////////////////////////////////////////////////////////////// void ReadDataIntoBuffer( IMFByteStream pStream, // Pointer to the byte stream. int cbToRead, // Number of bytes to read out IMFMediaBuffer ppBuffer // Receives a pointer to the buffer. ) { HResult hr; IntPtr pData; int cbRead; // Actual amount of data read int iMax, iCur; // Create the media buffer. This function allocates the memory. hr = MFExtern.MFCreateMemoryBuffer(cbToRead, out ppBuffer); MFError.ThrowExceptionForHR(hr); // Access the buffer. hr = ppBuffer.Lock(out pData, out iMax, out iCur); MFError.ThrowExceptionForHR(hr); try { // Read the data from the byte stream. hr = pStream.Read(pData, cbToRead, out cbRead); MFError.ThrowExceptionForHR(hr); } finally { hr = ppBuffer.Unlock(); MFError.ThrowExceptionForHR(hr); pData = IntPtr.Zero; } // Update the size of the valid data. hr = ppBuffer.SetCurrentLength(cbRead); MFError.ThrowExceptionForHR(hr); }
/// <summary>Generates the "tail" of the audio effect.</summary> /// <param name="InputMessageNumber">Message number to use with OutputSample.</param> /// <remarks> /// Generates the "tail" of the audio effect. The tail is the portion /// of the delay effect that is heard after the input stream ends. /// /// To generate the tail, the client must drain the MFT by sending /// the MFT_MESSAGE_COMMAND_DRAIN message and then call ProcessOutput /// to get the tail samples. /// </remarks> private void ProcessEffectTail(int InputMessageNumber) { IMFMediaBuffer pOutputBuffer = null; MFError throwonhr; IntPtr pbOutputData = IntPtr.Zero; // Pointer to the memory in the output buffer. int cbOutputLength = 0; // Size of the output buffer. int cbBytesProcessed = 0; // How much data we processed. IMFSample pOutSample = null; // Allocate an output buffer. throwonhr = MFExtern.MFCreateMemoryBuffer(m_cbTailSamples, out pOutputBuffer); try { throwonhr = MFExtern.MFCreateSample(out pOutSample); throwonhr = pOutSample.AddBuffer(pOutputBuffer); // Lock the output buffer. int cb; throwonhr = pOutputBuffer.Lock(out pbOutputData, out cbOutputLength, out cb); // Calculate how many audio samples we can process. cbBytesProcessed = Math.Min(m_cbTailSamples, cbOutputLength); // Round to the next lowest multiple of nBlockAlign. cbBytesProcessed -= (cbBytesProcessed % m_Alignment); // Fill the output buffer with silence, because we are also using it as the input buffer. FillBufferWithSilence(pbOutputData, cbBytesProcessed); // Process the data. ProcessAudio(pbOutputData, pbOutputData, cbBytesProcessed / m_Alignment); // Set the data length on the output buffer. throwonhr = pOutputBuffer.SetCurrentLength(cbBytesProcessed); if (m_rtTimestamp >= 0) { long hnsDuration = (cbBytesProcessed / m_AvgBytesPerSec) * UNITS; // Set the time stamp and duration on the output sample. throwonhr = pOutSample.SetSampleTime(m_rtTimestamp); throwonhr = pOutSample.SetSampleDuration(hnsDuration); } // Done. m_cbTailSamples = 0; OutputSample(pOutSample, InputMessageNumber); } catch { SafeRelease(pOutSample); throw; } finally { if (pbOutputData != IntPtr.Zero) { pOutputBuffer.Unlock(); } SafeRelease(pOutputBuffer); } }
///////////////////////////////////////////////////////////////////// // Name: ReadDataIntoBuffer // // Reads data from a byte stream and returns a media buffer that // contains the data. // // pStream: Pointer to the byte stream // cbToRead: Number of bytes to read // ppBuffer: Receives a pointer to the buffer. ///////////////////////////////////////////////////////////////////// void ReadDataIntoBuffer( IMFByteStream pStream, // Pointer to the byte stream. int cbToRead, // Number of bytes to read out IMFMediaBuffer ppBuffer // Receives a pointer to the buffer. ) { IntPtr pData; int cbRead; // Actual amount of data read int iMax, iCur; // Create the media buffer. This function allocates the memory. int hr = MFExtern.MFCreateMemoryBuffer(cbToRead, out ppBuffer); MFError.ThrowExceptionForHR(hr); // Access the buffer. hr = ppBuffer.Lock(out pData, out iMax, out iCur); MFError.ThrowExceptionForHR(hr); try { // Read the data from the byte stream. hr = pStream.Read(pData, cbToRead, out cbRead); MFError.ThrowExceptionForHR(hr); } finally { hr = ppBuffer.Unlock(); MFError.ThrowExceptionForHR(hr); pData = IntPtr.Zero; } // Update the size of the valid data. hr = ppBuffer.SetCurrentLength(cbRead); MFError.ThrowExceptionForHR(hr); }