1 /* GStreamer 2 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com> 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public 15 * License along with this library; if not, write to the 16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #ifndef __GST_MF_VIDEO_BUFFER_H__ 21 #define __GST_MF_VIDEO_BUFFER_H__ 22 23 #include <gst/gst.h> 24 #include <gst/video/video.h> 25 #include <windows.h> 26 #include <mfobjects.h> 27 #include <mferror.h> 28 #include <mutex> 29 30 #ifndef __cplusplus 31 #error IGstMFVideoBuffer interface doesn't provide C API 32 #endif 33 34 /* Define UUID for QueryInterface() */ 35 class DECLSPEC_UUID("ce922806-a8a6-4e1e-871f-e0cdd5fc9899") 36 IGstMFVideoBuffer : public IMFMediaBuffer, public IMF2DBuffer 37 { 38 public: 39 static HRESULT CreateInstance (GstVideoInfo * info, 40 IMFMediaBuffer ** buffer); 41 42 static HRESULT CreateInstanceWrapped (GstVideoInfo * info, 43 BYTE * data, 44 DWORD length, 45 IMFMediaBuffer ** buffer); 46 47 /* notify will be called right after this object is destroyed */ 48 HRESULT SetUserData (gpointer user_data, 49 GDestroyNotify notify); 50 51 HRESULT GetUserData (gpointer * user_data); 52 53 /* IUnknown interface */ 54 STDMETHODIMP_ (ULONG) AddRef (void); 55 STDMETHODIMP_ (ULONG) Release (void); 56 STDMETHODIMP QueryInterface (REFIID riid, 57 void ** object); 58 59 /* IMFMediaBuffer interface 60 * 61 * Caller of this interface expects returned raw memory layout via Lock() 62 * has no padding with default stride. If stored memory layout consists of 63 * non-default stride and/or with some padding, then Lock() / Unlock() would 64 * cause memory copy therefore. 65 * Caller should avoid to use this interface as much as possible 66 * if IMF2DBuffer interface available. 67 */ 68 STDMETHODIMP Lock (BYTE ** buffer, 69 DWORD * max_length, 70 DWORD * current_length); 71 STDMETHODIMP Unlock (void); 72 STDMETHODIMP GetCurrentLength (DWORD * length); 73 STDMETHODIMP SetCurrentLength (DWORD length); 74 STDMETHODIMP GetMaxLength (DWORD * length); 75 76 /* IMF2DBuffer interface 77 * 78 * this interface supports any raw memory layout with non-default stride. 79 * But more complex layout (padding at bottom for instance) is not supported. 80 */ 81 STDMETHODIMP Lock2D (BYTE ** buffer, 82 LONG * pitch); 83 STDMETHODIMP Unlock2D (void); 84 STDMETHODIMP GetScanline0AndPitch (BYTE ** buffer, 85 LONG * pitch); 86 STDMETHODIMP IsContiguousFormat (BOOL * contiguous); 87 STDMETHODIMP GetContiguousLength (DWORD * length); 88 STDMETHODIMP ContiguousCopyTo (BYTE * dest_buffer, 89 DWORD dest_buffer_length); 90 STDMETHODIMP ContiguousCopyFrom (const BYTE * src_buffer, 91 DWORD src_buffer_length); 92 93 private: 94 IGstMFVideoBuffer (void); 95 ~IGstMFVideoBuffer (void); 96 97 HRESULT Initialize (GstVideoInfo * info); 98 HRESULT InitializeWrapped (GstVideoInfo * info, 99 BYTE * data, 100 DWORD length); 101 HRESULT ContiguousCopyToUnlocked (BYTE * dest_buffer, 102 DWORD dest_buffer_length); 103 HRESULT ContiguousCopyFromUnlocked (const BYTE * src_buffer, 104 DWORD src_buffer_length); 105 106 private: 107 ULONG ref_count_; 108 DWORD current_len_; 109 DWORD contiguous_len_; 110 BYTE *data_; 111 BYTE *contiguous_data_; 112 GstVideoInfo *info_; 113 GstVideoInfo *contiguous_info_; 114 BOOL contiguous_; 115 std::mutex lock_; 116 bool locked_; 117 bool wrapped_; 118 119 gpointer user_data_; 120 GDestroyNotify notify_; 121 }; 122 123 #endif /* __GST_MF_VIDEO_BUFFER_H__ */ 124