asfheader, demux_audio: Remove some pointless be2me/le2me
[mplayer/glamo.git] / loader / dshow / cmediasample.c
blobefa3ef89cdd1c4ee6d4954f8c0ce7379a75a16a6
1 /*
2 * Modified for use with MPlayer, detailed changelog at
3 * http://svn.mplayerhq.hu/mplayer/trunk/
4 */
6 #include "cmediasample.h"
7 #include "mediatype.h"
8 #include "loader/wine/winerror.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
14 * currently hack to make some extra room for DS Acel codec which
15 * seems to overwrite allocated memory - FIXME better later
16 * check the buffer allocation
18 static const int SAFETY_ACEL = 1024;
20 /**
21 * \brief IPin::QueryInternalConnections (retries pin's internal connections)
23 * \param[in] This pointer to IPin interface
24 * \param[out] apPin Array that receives pins, internally connected to this
25 * \param[in,out] nPint Size of an array
27 * \return S_OK - success
28 * \return S_FALSE - pin rejects media type
29 * \return E_NOTIMPL - not implemented
32 static long STDCALL CMediaSample_QueryInterface(IUnknown* This,
33 /* [in] */ const GUID* iid,
34 /* [iid_is][out] */ void **ppv)
36 Debug printf("CMediaSample_QueryInterface(%p) called\n", This);
37 if (!ppv)
38 return E_INVALIDARG;
39 if (memcmp(iid, &IID_IUnknown, sizeof(*iid)) == 0)
41 *ppv = (void*)This;
42 ((IMediaSample*) This)->vt->AddRef(This);
43 return 0;
45 if (memcmp(iid, &IID_IMediaSample, sizeof(*iid)) == 0)
47 *ppv = (void*)This;
48 ((IMediaSample*) This)->vt->AddRef(This);
49 return 0;
51 return E_NOINTERFACE;
54 /**
55 * \brief IUnknown::AddRef (increases reference counter for interface)
57 * \param[in] This pointer to IUnknown class
59 * \return new value of reference counter
61 * \remarks
62 * Return value should be used only for debug purposes
65 static long STDCALL CMediaSample_AddRef(IUnknown* This)
67 Debug printf("CMediaSample_AddRef(%p) called\n", This);
68 ((CMediaSample*)This)->refcount++;
69 return 0;
72 /**
73 * \brief CMediaSample destructor
75 * \param[in] This pointer to CMediaSample object
78 void CMediaSample_Destroy(CMediaSample* This)
81 Debug printf("CMediaSample_Destroy(%p) called (ref:%d)\n", This, This->refcount);
82 free(This->vt);
83 free(This->own_block);
84 if(((CMediaSample*)This)->type_valid)
85 FreeMediaType(&(This->media_type));
86 free(This);
89 /**
90 * \brief IUnknown::Release (desreases reference counter for interface)
92 * \param[in] This pointer to IUnknown class
94 * \return new value of reference counter
96 * \remarks
97 * When reference counter reaches zero calls destructor
98 * Return value should be used only for debug purposes
101 static long STDCALL CMediaSample_Release(IUnknown* This)
103 CMediaSample* parent = (CMediaSample*)This;
104 Debug printf("CMediaSample_Release(%p) called (new ref:%d)\n",
105 This, ((CMediaSample*)This)->refcount-1);
107 if (--((CMediaSample*) This)->refcount == 0)
109 parent->all->vt->ReleaseBuffer((IMemAllocator*)(parent->all),
110 (IMediaSample*)This);
112 return 0;
116 * \brief IMediaSample::GetPointer (retrieves a read/write pointer to the media sample's buffer)
118 * \param[in] This pointer to CMediaSample object
119 * \param[out] address of variable that receives pointer to sample's buffer
121 * \return S_OK success
122 * \return apropriate error otherwise
124 * \note The calles should not free or reallocate buffer
127 static HRESULT STDCALL CMediaSample_GetPointer(IMediaSample* This,
128 /* [out] */ BYTE** ppBuffer)
130 Debug printf("CMediaSample_GetPointer(%p) called -> %p, size: %d %d\n", This, ((CMediaSample*) This)->block, ((CMediaSample*)This)->actual_size, ((CMediaSample*)This)->size);
131 if (!ppBuffer)
132 return E_INVALIDARG;
133 *ppBuffer = (BYTE*) ((CMediaSample*) This)->block;
134 return 0;
138 * \brief IMediaSample::GetSize (retrieves a size of buffer in bytes)
140 * \param[in] This pointer to CMediaSample object
142 * \return size of buffer in bytes
145 static long STDCALL CMediaSample_GetSize(IMediaSample * This)
147 Debug printf("CMediaSample_GetSize(%p) called -> %d\n", This, ((CMediaSample*) This)->size);
148 return ((CMediaSample*) This)->size;
152 * \brief IMediaSample::GetTime (retrieves a stream time at wich sample sould start and finish)
154 * \param[in] This pointer to CMediaSample object
155 * \param[out] pTimeStart pointer to variable that receives start time
156 * \param[out] pTimeEnd pointer to variable that receives end time
158 * \return S_OK success
159 * \return VFW_E_NO_STOP_TIME The sample has valid start time, but no stop time
160 * \return VFW_E_SAMPLE_TIME_NOT_SET The sample is not time-stamped
162 * \remarks
163 * Both values are relative to stream time
166 static HRESULT STDCALL CMediaSample_GetTime(IMediaSample * This,
167 /* [out] */ REFERENCE_TIME *pTimeStart,
168 /* [out] */ REFERENCE_TIME *pTimeEnd)
170 Debug printf("CMediaSample_GetTime(%p) called (UNIMPLEMENTED)\n", This);
171 return E_NOTIMPL;
175 * \brief IMediaSample::SetTime (sets a stream time at wich sample sould start and finish)
177 * \param[in] This pointer to CMediaSample object
178 * \param[out] pTimeStart pointer to variable that contains start time
179 * \param[out] pTimeEnd pointer to variable that contains end time
181 * \return S_OK success
182 * \return apropriate error otherwise
184 * \remarks
185 * Both values are relative to stream time
186 * To invalidate the stream times set pTimeStart and pTimeEnd to NULL. this will cause
187 * IMEdiaSample::GetTime to return VFW_E_SAMPLE_TIME_NOT_SET
190 static HRESULT STDCALL CMediaSample_SetTime(IMediaSample * This,
191 /* [in] */ REFERENCE_TIME *pTimeStart,
192 /* [in] */ REFERENCE_TIME *pTimeEnd)
194 Debug printf("CMediaSample_SetTime(%p) called (UNIMPLEMENTED)\n", This);
195 return E_NOTIMPL;
199 * \brief IMediaSample::IsSyncPoint (determines if start of this sample is sync point)
201 * \param[in] This pointer to CMediaSample object
203 * \return S_OK start of this sample is sync point
204 * \return S_FALSE start of this sample is not sync point
206 * \remarks
207 * If bTemporalCompression of AM_MEDIA_TYPE is FALSE, all samples are sync points.
210 static HRESULT STDCALL CMediaSample_IsSyncPoint(IMediaSample * This)
212 Debug printf("CMediaSample_IsSyncPoint(%p) called\n", This);
213 if (((CMediaSample*)This)->isSyncPoint)
214 return 0;
215 return 1;
219 * \brief IMediaSample::SetSyncPoint (specifies if start of this sample is sync point)
221 * \param[in] This pointer to CMediaSample object
222 * \param[in] bIsSyncPoint specifies whether this is sync point or not
224 * \return S_OK success
225 * \return apropriate error code otherwise
228 static HRESULT STDCALL CMediaSample_SetSyncPoint(IMediaSample * This,
229 long bIsSyncPoint)
231 Debug printf("CMediaSample_SetSyncPoint(%p) called\n", This);
232 ((CMediaSample*)This)->isSyncPoint = bIsSyncPoint;
233 return 0;
237 * \brief IMediaSample::IsPreroll (determines if this sample is preroll sample)
239 * \param[in] This pointer to CMediaSample object
241 * \return S_OK if this sample is preroll sample
242 * \return S_FALSE if this sample is not preroll sample
244 * \remarks
245 * Preroll samples are processed but not displayed. They are lokated in media stream
246 * before displayable samples.
249 static HRESULT STDCALL CMediaSample_IsPreroll(IMediaSample * This)
251 Debug printf("CMediaSample_IsPreroll(%p) called\n", This);
253 if (((CMediaSample*)This)->isPreroll)
254 return 0;//S_OK
256 return 1;//S_FALSE
260 * \brief IMediaSample::SetPreroll (specifies if this sample is preroll sample)
262 * \param[in] This pointer to CMediaSample object
263 * \param[in] bIsPreroll specifies whether this sample is preroll sample or not
265 * \return S_OK success
266 * \return apropriate error code otherwise
268 * \remarks
269 * Preroll samples are processed but not displayed. They are lokated in media stream
270 * before displayable samples.
273 static HRESULT STDCALL CMediaSample_SetPreroll(IMediaSample * This,
274 long bIsPreroll)
276 Debug printf("CMediaSample_SetPreroll(%p) called\n", This);
277 ((CMediaSample*)This)->isPreroll=bIsPreroll;
278 return 0;
282 * \brief IMediaSample::GetActualDataLength (retrieves the length of valid data in the buffer)
284 * \param[in] This pointer to CMediaSample object
286 * \return length of valid data in buffer in bytes
289 static long STDCALL CMediaSample_GetActualDataLength(IMediaSample* This)
291 Debug printf("CMediaSample_GetActualDataLength(%p) called -> %d\n", This, ((CMediaSample*)This)->actual_size);
292 return ((CMediaSample*)This)->actual_size;
296 * \brief IMediaSample::SetActualDataLength (specifies the length of valid data in the buffer)
298 * \param[in] This pointer to CMediaSample object
299 * \param[in] __MIDL_0010 length of data in sample in bytes
301 * \return S_OK success
302 * \return VFW_E_BUFFER_OVERFLOW length specified by parameter is larger than buffer size
305 static HRESULT STDCALL CMediaSample_SetActualDataLength(IMediaSample* This,
306 long __MIDL_0010)
308 CMediaSample* cms = (CMediaSample*)This;
309 Debug printf("CMediaSample_SetActualDataLength(%p, %ld) called\n", This, __MIDL_0010);
311 if (__MIDL_0010 > cms->size)
313 char* c = cms->own_block;
314 Debug printf("CMediaSample - buffer overflow %ld %d %p %p\n",
315 __MIDL_0010, ((CMediaSample*)This)->size, cms->own_block, cms->block);
316 cms->own_block = realloc(cms->own_block, (size_t) __MIDL_0010 + SAFETY_ACEL);
317 if (c == cms->block)
318 cms->block = cms->own_block;
319 cms->size = __MIDL_0010;
321 cms->actual_size = __MIDL_0010;
322 return 0;
326 * \brief IMediaSample::GetMediaType (retrieves media type, if it changed from previous sample)
328 * \param[in] This pointer to CMediaSample object
329 * \param[out] ppMediaType address of variable that receives pointer to AM_MEDIA_TYPE.
331 * \return S_OK success
332 * \return S_FALSE Media type was not changed from previous sample
333 * \return E_OUTOFMEMORY Insufficient memory
335 * \remarks
336 * If media type is not changed from previous sample, ppMediaType is null
337 * If method returns S_OK caller should free memory allocated for structure
338 * including pbFormat block
340 static HRESULT STDCALL CMediaSample_GetMediaType(IMediaSample* This,
341 AM_MEDIA_TYPE** ppMediaType)
343 AM_MEDIA_TYPE* t;
344 Debug printf("CMediaSample_GetMediaType(%p) called\n", This);
345 if(!ppMediaType)
346 return E_INVALIDARG;
347 if(!((CMediaSample*)This)->type_valid)
349 *ppMediaType=0;
350 return 1;
353 t = &((CMediaSample*)This)->media_type;
354 // if(t.pbFormat)free(t.pbFormat);
355 *ppMediaType=CreateMediaType(t);
356 // *ppMediaType=0; //media type was not changed
357 return 0;
361 * \brief IMediaType::SetMediaType (specifies media type for sample)
363 * \param[in] This pointer to CMediaSample object
364 * \param[in] pMediaType pointer to AM_MEDIA_TYPE specifies new media type
366 * \return S_OK success
367 * \return E_OUTOFMEMORY insufficient memory
370 static HRESULT STDCALL CMediaSample_SetMediaType(IMediaSample * This,
371 AM_MEDIA_TYPE *pMediaType)
373 AM_MEDIA_TYPE* t;
374 Debug printf("CMediaSample_SetMediaType(%p) called\n", This);
375 if (!pMediaType)
376 return E_INVALIDARG;
377 t = &((CMediaSample*)This)->media_type;
378 if(((CMediaSample*)This)->type_valid)
379 FreeMediaType(t);
380 CopyMediaType(t,pMediaType);
381 ((CMediaSample*) This)->type_valid=1;
383 return 0;
387 * \brief IMediaSample::IsDiscontinuity (determines if this sample represents data break
388 * in stream)
390 * \param[in] This pointer to CMediaSample object
392 * \return S_OK if this sample is break in data stream
393 * \return S_FALSE otherwise
395 * \remarks
396 * Discontinuity occures when filter seeks to different place in the stream or when drops
397 * samples.
400 static HRESULT STDCALL CMediaSample_IsDiscontinuity(IMediaSample * This)
402 Debug printf("CMediaSample_IsDiscontinuity(%p) called\n", This);
403 return ((CMediaSample*) This)->isDiscontinuity;
407 * \brief IMediaSample::IsDiscontinuity (specifies whether this sample represents data break
408 * in stream)
410 * \param[in] This pointer to CMediaSample object
411 * \param[in] bDiscontinuity if TRUE this sample represents discontinuity with previous sample
413 * \return S_OK success
414 * \return apropriate error code otherwise
417 static HRESULT STDCALL CMediaSample_SetDiscontinuity(IMediaSample * This,
418 long bDiscontinuity)
420 Debug printf("CMediaSample_SetDiscontinuity(%p) called (%ld)\n", This, bDiscontinuity);
421 ((CMediaSample*) This)->isDiscontinuity = bDiscontinuity;
422 return 0;
426 * \brief IMediaSample::GetMediaTime (retrieves the media times of this sample)
428 * \param[in] This pointer to CMediaSample object
429 * \param[out] pTimeStart pointer to variable that receives start time
430 * \param[out] pTimeEnd pointer to variable that receives end time
432 * \return S_OK success
433 * \return VFW_E_MEDIA_TIME_NOT_SET The sample is not time-stamped
436 static HRESULT STDCALL CMediaSample_GetMediaTime(IMediaSample * This,
437 /* [out] */ LONGLONG *pTimeStart,
438 /* [out] */ LONGLONG *pTimeEnd)
440 Debug printf("CMediaSample_GetMediaTime(%p) called\n", This);
441 if (pTimeStart)
442 *pTimeStart = ((CMediaSample*) This)->time_start;
443 if (pTimeEnd)
444 *pTimeEnd = ((CMediaSample*) This)->time_end;
445 return 0;
449 * \brief IMediaSample::GetMediaTime (retrieves the media times of this sample)
451 * \param[in] This pointer to CMediaSample object
452 * \param[out] pTimeStart pointer to variable that specifies start time
453 * \param[out] pTimeEnd pointer to variable that specifies end time
455 * \return S_OK success
456 * \return apropriate error code otherwise
458 * \remarks
459 * To invalidate the media times set pTimeStart and pTimeEnd to NULL. this will cause
460 * IMEdiaSample::GetTime to return VFW_E_MEDIA_TIME_NOT_SET
462 static HRESULT STDCALL CMediaSample_SetMediaTime(IMediaSample * This,
463 /* [in] */ LONGLONG *pTimeStart,
464 /* [in] */ LONGLONG *pTimeEnd)
466 Debug printf("CMediaSample_SetMediaTime(%p) called\n", This);
467 if (pTimeStart)
468 ((CMediaSample*) This)->time_start = *pTimeStart;
469 if (pTimeEnd)
470 ((CMediaSample*) This)->time_end = *pTimeEnd;
471 return 0;
475 * \brief CMediaSample::SetPointer (extension for direct memory write of decompressed data)
477 * \param[in] This pointer to CMediaSample object
478 * \param[in] pointer pointer to an external buffer to store data to
481 static void CMediaSample_SetPointer(CMediaSample* This, char* pointer)
483 Debug printf("CMediaSample_SetPointer(%p) called -> %p\n", This, pointer);
484 if (pointer)
485 This->block = pointer;
486 else
487 This->block = This->own_block;
491 * \brief CMediaSample::SetPointer (resets pointer to external buffer with internal one)
493 * \param[in] This pointer to CMediaSample object
496 static void CMediaSample_ResetPointer(CMediaSample* This)
498 Debug printf("CMediaSample_ResetPointer(%p) called\n", This);
499 This->block = This->own_block;
503 * \brief CMediaSample constructor
505 * \param[in] allocator IMemallocator interface of allocator to use
506 * \param[in] size size of internal buffer
508 * \return pointer to CMediaSample object of NULL if error occured
511 CMediaSample* CMediaSampleCreate(IMemAllocator* allocator, int size)
513 CMediaSample* This = malloc(sizeof(CMediaSample));
514 if (!This)
515 return NULL;
517 // some hack here!
518 // it looks like Acelp decoder is actually accessing
519 // the allocated memory before it sets the new size for it ???
520 // -- maybe it's being initialized with wrong parameters
521 // anyway this is fixes the problem somehow with some reserves
523 // using different trick for now - in DS_Audio modify sample size
524 //if (size < 0x1000)
525 // size = (size + 0xfff) & ~0xfff;
527 This->vt = malloc(sizeof(IMediaSample_vt));
528 This->own_block = malloc((size_t)size + SAFETY_ACEL);
529 This->media_type.pbFormat = 0;
530 This->media_type.pUnk = 0;
532 if (!This->vt || !This->own_block)
534 CMediaSample_Destroy(This);
535 return NULL;
538 This->vt->QueryInterface = CMediaSample_QueryInterface;
539 This->vt->AddRef = CMediaSample_AddRef;
540 This->vt->Release = CMediaSample_Release;
541 This->vt->GetPointer = CMediaSample_GetPointer;
542 This->vt->GetSize = CMediaSample_GetSize;
543 This->vt->GetTime = CMediaSample_GetTime;
544 This->vt->SetTime = CMediaSample_SetTime;
545 This->vt->IsSyncPoint = CMediaSample_IsSyncPoint;
546 This->vt->SetSyncPoint = CMediaSample_SetSyncPoint;
547 This->vt->IsPreroll = CMediaSample_IsPreroll;
548 This->vt->SetPreroll = CMediaSample_SetPreroll;
549 This->vt->GetActualDataLength = CMediaSample_GetActualDataLength;
550 This->vt->SetActualDataLength = CMediaSample_SetActualDataLength;
551 This->vt->GetMediaType = CMediaSample_GetMediaType;
552 This->vt->SetMediaType = CMediaSample_SetMediaType;
553 This->vt->IsDiscontinuity = CMediaSample_IsDiscontinuity;
554 This->vt->SetDiscontinuity = CMediaSample_SetDiscontinuity;
555 This->vt->GetMediaTime = CMediaSample_GetMediaTime;
556 This->vt->SetMediaTime = CMediaSample_SetMediaTime;
558 This->all = allocator;
559 This->size = size;
560 This->refcount = 0; // increased by MemAllocator
561 This->actual_size = 0;
562 This->isPreroll = 0;
563 This->isDiscontinuity = 1;
564 This->time_start = 0;
565 This->time_end = 0;
566 This->type_valid = 0;
567 This->block = This->own_block;
569 This->SetPointer = CMediaSample_SetPointer;
570 This->ResetPointer = CMediaSample_ResetPointer;
572 Debug printf("CMediaSample_Create(%p) called - sample size %d, buffer %p\n",
573 This, This->size, This->block);
575 return This;