Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PacketAllocator.cpp « LAVSplitter « demuxer - github.com/mpc-hc/LAVFilters.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d04f584c9f65b9e2c1820b69da9aa35cafd140a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *      Copyright (C) 2010-2016 Hendrik Leppkes
 *      http://www.1f0.de
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Initial design and concept by Gabest and the MPC-HC Team, copyright under GPLv2
 *  Contributions by Ti-BEN from the XBMC DSPlayer Project, also under GPLv2
 */

#include "stdafx.h"
#include "PacketAllocator.h"

CMediaPacketSample::CMediaPacketSample(LPCTSTR pName, CBaseAllocator *pAllocator, HRESULT *phr)
  : CMediaSample(pName, pAllocator, phr)
{
}

STDMETHODIMP CMediaPacketSample::QueryInterface(REFIID riid, void **ppv)
{
  if (riid == __uuidof(ILAVMediaSample)) {
    return GetInterface((ILAVMediaSample *) this, ppv);
  }
  return CMediaSample::QueryInterface(riid, ppv);
}

STDMETHODIMP_(ULONG) CMediaPacketSample::AddRef()
{
  return CMediaSample::AddRef();
}

STDMETHODIMP_(ULONG) CMediaPacketSample::Release()
{
  /* Decrement our own private reference count */
  LONG lRef;
  if (m_cRef == 1) {
    lRef = 0;
    m_cRef = 0;
  } else {
    lRef = InterlockedDecrement(&m_cRef);
  }
  ASSERT(lRef >= 0);

  DbgLog((LOG_MEMORY,3,TEXT("    Unknown %X ref-- = %d"),
    this, m_cRef));

  /* Did we release our final reference count */
  if (lRef == 0) {
    /* Free all resources */
    if (m_dwFlags & Sample_TypeChanged) {
      SetMediaType(nullptr);
    }
    ASSERT(m_pMediaType == nullptr);
    m_dwFlags = 0;
    m_dwTypeSpecificFlags = 0;
    m_dwStreamId = AM_STREAM_MEDIA;
    
    SAFE_DELETE(m_pPacket);
    SetPointer(nullptr, 0);

    SAFE_DELETE(m_pSideData);

    /* This may cause us to be deleted */
    // Our refcount is reliably 0 thus no-one will mess with us
    m_pAllocator->ReleaseBuffer(this);
  }
  return (ULONG)lRef;
}

STDMETHODIMP CMediaPacketSample::SetPacket(Packet *pPacket)
{
  SAFE_DELETE(m_pPacket);
  m_pPacket = pPacket;
  SetPointer(pPacket->GetData(), (LONG)pPacket->GetDataSize());

  SAFE_DELETE(m_pSideData);

  if (pPacket->GetNumSideData() > 0) {
    m_pSideData = new MediaSideDataFFMpeg();
    m_pSideData->side_data = pPacket->GetSideData();
    m_pSideData->side_data_elems = pPacket->GetNumSideData();
  }

  return S_OK;
}

STDMETHODIMP CMediaPacketSample::SetSideData(GUID guidType, const BYTE *pData, size_t size)
{
  return E_NOTIMPL;
}

STDMETHODIMP CMediaPacketSample::GetSideData(GUID guidType, const BYTE **pData, size_t *pSize)
{
  if (guidType == IID_MediaSideDataFFMpeg && m_pSideData) {
    *pData = (const BYTE *)m_pSideData;
    *pSize = sizeof(MediaSideDataFFMpeg);

    return S_OK;
  }

  return E_INVALIDARG;
}

CPacketAllocator::CPacketAllocator(LPCTSTR pName, LPUNKNOWN pUnk, HRESULT *phr)
  : CBaseAllocator(pName, pUnk, phr, TRUE, TRUE)
{
}

CPacketAllocator::~CPacketAllocator(void)
{
  Decommit();
  ReallyFree();
}

STDMETHODIMP CPacketAllocator::SetProperties(ALLOCATOR_PROPERTIES* pRequest, ALLOCATOR_PROPERTIES* pActual)
{
  CheckPointer(pActual,E_POINTER);
  ValidateReadWritePtr(pActual,sizeof(ALLOCATOR_PROPERTIES));
  CAutoLock cObjectLock(this);

  ZeroMemory(pActual, sizeof(ALLOCATOR_PROPERTIES));

  ASSERT(pRequest->cbBuffer > 0);

  SYSTEM_INFO SysInfo;
  GetSystemInfo(&SysInfo);

  /*  Check the alignment request is a power of 2 */
  if ((-pRequest->cbAlign & pRequest->cbAlign) != pRequest->cbAlign) {
    DbgLog((LOG_ERROR, 1, TEXT("Alignment requested 0x%x not a power of 2!"),
      pRequest->cbAlign));
  }
  /*  Check the alignment requested */
  if (pRequest->cbAlign == 0 ||
    (SysInfo.dwAllocationGranularity & (pRequest->cbAlign - 1)) != 0) {
      DbgLog((LOG_ERROR, 1, TEXT("Invalid alignment 0x%x requested - granularity = 0x%x"),
        pRequest->cbAlign, SysInfo.dwAllocationGranularity));
      return VFW_E_BADALIGN;
  }

  /* Can't do this if already committed, there is an argument that says we
  should not reject the SetProperties call if there are buffers still
  active. However this is called by the source filter, which is the same
  person who is holding the samples. Therefore it is not unreasonable
  for them to free all their samples before changing the requirements */

  if (m_bCommitted == TRUE) {
    return VFW_E_ALREADY_COMMITTED;
  }

  /* Must be no outstanding buffers */

  if (m_lFree.GetCount() < m_lAllocated) {
    return VFW_E_BUFFERS_OUTSTANDING;
  }

  /* There isn't any real need to check the parameters as they
  will just be rejected when the user finally calls Commit */

  // round length up to alignment - remember that prefix is included in
  // the alignment
  LONG lSize = pRequest->cbBuffer + pRequest->cbPrefix;
  LONG lRemainder = lSize % pRequest->cbAlign;
  if (lRemainder != 0) {
    lSize = lSize - lRemainder + pRequest->cbAlign;
  }
  pActual->cbBuffer = m_lSize = (lSize - pRequest->cbPrefix);

  pActual->cBuffers = m_lCount = pRequest->cBuffers;
  pActual->cbAlign = m_lAlignment = pRequest->cbAlign;
  pActual->cbPrefix = m_lPrefix = pRequest->cbPrefix;

  m_bChanged = TRUE;
  return NOERROR;
}

HRESULT CPacketAllocator::Alloc(void)
{
  CAutoLock lck(this);

  /* Check he has called SetProperties */
  HRESULT hr = CBaseAllocator::Alloc();
  if (FAILED(hr)) {
    return hr;
  }

  /* If the requirements haven't changed then don't reallocate */
  if (hr == S_FALSE) {
    return NOERROR;
  }
  ASSERT(hr == S_OK); // we use this fact in the loop below

  /* Free the old resources */
  if (m_bAllocated) {
    ReallyFree();
  }

  /* Make sure we've got reasonable values */
  if ( m_lSize < 0 || m_lPrefix < 0 || m_lCount < 0 ) {
    return E_OUTOFMEMORY;
  }

  /* Compute the aligned size */
  LONG lAlignedSize = m_lSize + m_lPrefix;

  /*  Check overflow */
  if (lAlignedSize < m_lSize) {
    return E_OUTOFMEMORY;
  }

  if (m_lAlignment > 1) {
    LONG lRemainder = lAlignedSize % m_lAlignment;
    if (lRemainder != 0) {
      LONG lNewSize = lAlignedSize + m_lAlignment - lRemainder;
      if (lNewSize < lAlignedSize) {
        return E_OUTOFMEMORY;
      }
      lAlignedSize = lNewSize;
    }
  }

  /* Create the contiguous memory block for the samples
  making sure it's properly aligned (64K should be enough!)
  */
  ASSERT(lAlignedSize % m_lAlignment == 0);

  LONGLONG lToAllocate = m_lCount * (LONGLONG)lAlignedSize;

  /*  Check overflow */
  if (lToAllocate > MAXLONG) {
    return E_OUTOFMEMORY;
  }

  m_bAllocated = TRUE;

  CMediaPacketSample *pSample = nullptr;

  ASSERT(m_lAllocated == 0);

  // Create the new samples - we have allocated m_lSize bytes for each sample
  // plus m_lPrefix bytes per sample as a prefix. We set the pointer to
  // the memory after the prefix - so that GetPointer() will return a pointer
  // to m_lSize bytes.
  for (; m_lAllocated < m_lCount; m_lAllocated++) {
    pSample = new CMediaPacketSample(NAME("LAV Package media sample"), this, &hr);

    ASSERT(SUCCEEDED(hr));
    if (pSample == nullptr) {
      return E_OUTOFMEMORY;
    }

    // This CANNOT fail
    m_lFree.Add(pSample);
  }

  m_bChanged = FALSE;
  return NOERROR;
}


// override this to free up any resources we have allocated.
// called from the base class on Decommit when all buffers have been
// returned to the free list.
//
// caller has already locked the object.

// in our case, we keep the memory until we are deleted, so
// we do nothing here. The memory is deleted in the destructor by
// calling ReallyFree()
void CPacketAllocator::Free(void)
{
  return;
}


// called from the destructor (and from Alloc if changing size/count) to
// actually free up the memory
void CPacketAllocator::ReallyFree(void)
{
  /* Should never be deleting this unless all buffers are freed */
  ASSERT(m_lAllocated == m_lFree.GetCount());

  /* Free up all the CMediaSamples */
  CMediaSample *pSample;
  for (;;) {
    pSample = m_lFree.RemoveHead();
    if (pSample != nullptr) {
      delete pSample;
    } else {
      break;
    }
  }

  m_lAllocated = 0;
  m_bAllocated = FALSE;
}