BufferLock.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. #pragma once
  8. //////////////////////////////////////////////////////////////////////////
  9. // VideoBufferLock
  10. //
  11. // Description:
  12. // Locks a video buffer that might or might not support IMF2DBuffer.
  13. //
  14. //////////////////////////////////////////////////////////////////////////
  15. class VideoBufferLock
  16. {
  17. public:
  18. VideoBufferLock(IMFMediaBuffer *pBuffer) : m_p2DBuffer(NULL)
  19. {
  20. m_pBuffer = pBuffer;
  21. m_pBuffer->AddRef();
  22. // Query for the 2-D buffer interface. OK if this fails.
  23. m_pBuffer->QueryInterface(IID_PPV_ARGS(&m_p2DBuffer));
  24. }
  25. ~VideoBufferLock()
  26. {
  27. UnlockBuffer();
  28. SafeRelease(&m_pBuffer);
  29. SafeRelease(&m_p2DBuffer);
  30. }
  31. // LockBuffer:
  32. // Locks the buffer. Returns a pointer to scan line 0 and returns the stride.
  33. // The caller must provide the default stride as an input parameter, in case
  34. // the buffer does not expose IMF2DBuffer. You can calculate the default stride
  35. // from the media type.
  36. HRESULT LockBuffer(
  37. LONG lDefaultStride, // Minimum stride (with no padding).
  38. DWORD dwHeightInPixels, // Height of the image, in pixels.
  39. BYTE **ppbScanLine0, // Receives a pointer to the start of scan line 0.
  40. LONG *plStride // Receives the actual stride.
  41. )
  42. {
  43. HRESULT hr = S_OK;
  44. // Use the 2-D version if available.
  45. if (m_p2DBuffer)
  46. {
  47. hr = m_p2DBuffer->Lock2D(ppbScanLine0, plStride);
  48. }
  49. else
  50. {
  51. // Use non-2D version.
  52. BYTE *pData = NULL;
  53. hr = m_pBuffer->Lock(&pData, NULL, NULL);
  54. if (SUCCEEDED(hr))
  55. {
  56. *plStride = lDefaultStride;
  57. if (lDefaultStride < 0)
  58. {
  59. // Bottom-up orientation. Return a pointer to the start of the
  60. // last row *in memory* which is the top row of the image.
  61. *ppbScanLine0 = pData + abs(lDefaultStride) * (dwHeightInPixels - 1);
  62. }
  63. else
  64. {
  65. // Top-down orientation. Return a pointer to the start of the
  66. // buffer.
  67. *ppbScanLine0 = pData;
  68. }
  69. }
  70. }
  71. return hr;
  72. }
  73. HRESULT UnlockBuffer()
  74. {
  75. if (m_p2DBuffer)
  76. {
  77. return m_p2DBuffer->Unlock2D();
  78. }
  79. else
  80. {
  81. return m_pBuffer->Unlock();
  82. }
  83. }
  84. private:
  85. IMFMediaBuffer *m_pBuffer;
  86. IMF2DBuffer *m_p2DBuffer;
  87. };