Twitch SDK (Internal)
intelbaseallocator.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * Twitch Broadcasting SDK
3 *
4 * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5 * may not be copied or used except in accordance with the terms of that agreement
6 * Copyright (c) 2012-2016 Twitch Interactive, Inc.
7 *********************************************************************************************/
8 
9 
10 /* ****************************************************************************** *\
11 
12 INTEL CORPORATION PROPRIETARY INFORMATION
13 This software is supplied under the terms of a license agreement or nondisclosure
14 agreement with Intel Corporation and may not be copied or disclosed except in
15 accordance with the terms of that agreement
16 Copyright(c) 2008-2012 Intel Corporation. All Rights Reserved.
17 
18 \* ****************************************************************************** */
19 
20 #pragma once
21 
22 #include <list>
23 #include "mfxvideo.h"
24 
25 namespace ttv
26 {
27  namespace broadcast
28  {
29  class MFXFrameAllocator;
30  class BaseFrameAllocator;
31  class MFXBufferAllocator;
32  struct mfxAllocatorParams;
33  }
34 }
35 
37 {
38  virtual ~mfxAllocatorParams(){};
39 };
40 
41 // this class implements methods declared in mfxFrameAllocator structure
42 // simply redirecting them to virtual methods which should be overridden in derived classes
43 class ttv::broadcast::MFXFrameAllocator : public mfxFrameAllocator
44 {
45 public:
47  virtual ~MFXFrameAllocator();
48 
49  // optional method, override if need to pass some parameters to allocator from application
50  virtual mfxStatus Init(mfxAllocatorParams *pParams) = 0;
51  virtual mfxStatus Close() = 0;
52 
53 protected:
54  virtual mfxStatus AllocFrames(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) = 0;
55  virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr) = 0;
56  virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr) = 0;
57  virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle) = 0;
58  virtual mfxStatus FreeFrames(mfxFrameAllocResponse *response) = 0;
59 
60 private:
61  static mfxStatus MFX_CDECL Alloc_(mfxHDL pthis, mfxFrameAllocRequest *request, mfxFrameAllocResponse *response);
62  static mfxStatus MFX_CDECL Lock_(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr);
63  static mfxStatus MFX_CDECL Unlock_(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr);
64  static mfxStatus MFX_CDECL GetHDL_(mfxHDL pthis, mfxMemId mid, mfxHDL *handle);
65  static mfxStatus MFX_CDECL Free_(mfxHDL pthis, mfxFrameAllocResponse *response);
66 };
67 
68 // This class implements basic logic of memory allocator
69 // Manages responses for different components according to allocation request type
70 // External frames of a particular component-related type are allocated in one call
71 // Further calls return previously allocated response.
72 // Ex. Preallocated frame chain with type=FROM_ENCODE | FROM_VPPIN will be returned when
73 // request type contains either FROM_ENCODE or FROM_VPPIN
74 
75 // This class does not allocate any actual memory
77 {
78 public:
80  virtual ~BaseFrameAllocator();
81 
82  virtual mfxStatus Init(mfxAllocatorParams *pParams) = 0;
83  virtual mfxStatus Close();
84 
85 protected:
86  virtual mfxStatus AllocFrames(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response);
87  virtual mfxStatus FreeFrames(mfxFrameAllocResponse *response);
88 
89  typedef std::list<mfxFrameAllocResponse>::iterator Iter;
90  static const mfxU32 MEMTYPE_FROM_MASK = MFX_MEMTYPE_FROM_ENCODE | MFX_MEMTYPE_FROM_DECODE | MFX_MEMTYPE_FROM_VPPIN | MFX_MEMTYPE_FROM_VPPOUT;
91 
93  {
94  mfxU32 m_refCount;
95  mfxFrameAllocResponse m_response;
96  mfxU16 m_type;
97 
98  UniqueResponse() { Reset(); }
99  void Reset() { memset(this, 0, sizeof(*this)); }
100  };
101 
102  std::list<mfxFrameAllocResponse> m_responses;
104 
105  // checks responses for identity
106  virtual bool IsSame(const mfxFrameAllocResponse& l, const mfxFrameAllocResponse& r);
107 
108  // checks if request is supported
109  virtual mfxStatus CheckRequestType(mfxFrameAllocRequest *request);
110 
111  // memory type specific methods, must be overridden in derived classes
112 
113  virtual mfxStatus LockFrame(mfxMemId mid, mfxFrameData *ptr) = 0;
114  virtual mfxStatus UnlockFrame(mfxMemId mid, mfxFrameData *ptr) = 0;
115  virtual mfxStatus GetFrameHDL(mfxMemId mid, mfxHDL *handle) = 0;
116 
117  // frees memory attached to response
118  virtual mfxStatus ReleaseResponse(mfxFrameAllocResponse *response) = 0;
119  // allocates memory
120  virtual mfxStatus AllocImpl(mfxFrameAllocRequest *request, mfxFrameAllocResponse *response) = 0;
121 
122  template <class T>
124  {
125  public:
126  safe_array(T *ptr = 0):m_ptr(ptr)
127  { // construct from object pointer
128  };
130  {
131  reset(0);
132  }
133  T* get()
134  { // return wrapped pointer
135  return m_ptr;
136  }
137  T* release()
138  { // return wrapped pointer and give up ownership
139  T* ptr = m_ptr;
140  m_ptr = 0;
141  return ptr;
142  }
143  void reset(T* ptr)
144  { // destroy designated object and store new pointer
145  if (m_ptr)
146  {
147  delete[] m_ptr;
148  }
149  m_ptr = ptr;
150  }
151  protected:
152  T* m_ptr; // the wrapped object pointer
153  };
154 };
155 
156 class ttv::broadcast::MFXBufferAllocator : public mfxBufferAllocator
157 {
158 public:
160  virtual ~MFXBufferAllocator();
161 
162 protected:
163  virtual mfxStatus AllocBuffer(mfxU32 nbytes, mfxU16 type, mfxMemId *mid) = 0;
164  virtual mfxStatus LockBuffer(mfxMemId mid, mfxU8 **ptr) = 0;
165  virtual mfxStatus UnlockBuffer(mfxMemId mid) = 0;
166  virtual mfxStatus FreeBuffer(mfxMemId mid) = 0;
167 
168 private:
169  static mfxStatus MFX_CDECL Alloc_(mfxHDL pthis, mfxU32 nbytes, mfxU16 type, mfxMemId *mid);
170  static mfxStatus MFX_CDECL Lock_(mfxHDL pthis, mfxMemId mid, mfxU8 **ptr);
171  static mfxStatus MFX_CDECL Unlock_(mfxHDL pthis, mfxMemId mid);
172  static mfxStatus MFX_CDECL Free_(mfxHDL pthis, mfxMemId mid);
173 };
mfxU16 m_type
Definition: intelbaseallocator.h:96
Definition: intelbaseallocator.h:36
std::list< mfxFrameAllocResponse > m_responses
Definition: intelbaseallocator.h:102
safe_array(T *ptr=0)
Definition: intelbaseallocator.h:126
void Reset()
Definition: intelbaseallocator.h:99
Definition: intelbaseallocator.h:43
Definition: intelbaseallocator.h:123
std::list< mfxFrameAllocResponse >::iterator Iter
Definition: intelbaseallocator.h:89
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
mfxFrameAllocResponse m_response
Definition: intelbaseallocator.h:95
Definition: intelbaseallocator.h:92
~safe_array()
Definition: intelbaseallocator.h:129
Definition: intelbaseallocator.h:156
virtual ~mfxAllocatorParams()
Definition: intelbaseallocator.h:38
void reset(T *ptr)
Definition: intelbaseallocator.h:143
UniqueResponse m_externalDecoderResponse
Definition: intelbaseallocator.h:103
T * release()
Definition: intelbaseallocator.h:137
Definition: intelbaseallocator.h:76
mfxU32 m_refCount
Definition: intelbaseallocator.h:94
T * m_ptr
Definition: intelbaseallocator.h:152
UniqueResponse()
Definition: intelbaseallocator.h:98