hc
2023-12-06 d38611ca164021d018c1b23eee65bbebc09c63e0
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/******************************************************************************
 *
 * Copyright 2007, Silicon Image, Inc.  All rights reserved.
 * No part of this work may be reproduced, modified, distributed, transmitted,
 * transcribed, or translated into any language or computer format, in any form
 * or by any means without written permission of: Silicon Image, Inc., 1060
 * East Arques Avenue, Sunnyvale, California 94085
 *
 *****************************************************************************/
/**
 * @file media_buffer_pool.c
 *
 * @brief
 *          Buffer Pool implementation
 *
 * <pre>
 *
 *   Principal Author: Joerg Detert
 *   Creation date:    Feb 28, 2008
 *
 * </pre>
 *
 *****************************************************************************/
 
#include <string.h>
 
#include "media_buffer_pool.h"
 
 
 
#define MEDIA_BUF_ALIGN(buf, align) (((buf)+((align)-1U)) & ~((align)-1U))
 
 
/******************************************************************************
 * MediaBufPoolGetSize
 *****************************************************************************/
RESULT MediaBufPoolGetSize(MediaBufPoolConfig_t* pPoolConfig)
{
    RESULT  ret = RET_SUCCESS;
 
    if(pPoolConfig == NULL)
    {
        return RET_WRONG_HANDLE;
    }
 
    if(pPoolConfig->bufNum > pPoolConfig->maxBufNum)
    {
        return RET_WRONG_CONFIG;
    }
 
    /* size of extra metadata */
    pPoolConfig->metaDataMemSize = pPoolConfig->maxBufNum * pPoolConfig->metaDataSizeMediaBuf;
 
    /* size of media buffer array */
    pPoolConfig->metaDataMemSize += (uint32_t)(pPoolConfig->maxBufNum * sizeof(MediaBuffer_t));
 
    /* calculate size of buffer array*/
    if(pPoolConfig->flags & (uint32_t)BUFPOOL_RINGBUFFER)
    {
        /* results in no gaps between buffers but an offset depending on the alignment */
        pPoolConfig->bufMemSize = (pPoolConfig->maxBufNum * pPoolConfig->bufSize) +
                                   pPoolConfig->bufAlign;
 
        /* the buffer size must be correct to the alignment otherwise we would have
         * unaligned buffers after the first which is always aligned */
        if((pPoolConfig->bufSize & ((uint32_t)pPoolConfig->bufAlign - 1U)) != 0U)
        {
            return RET_WRONG_CONFIG;
        }
    }
    else
    {
        /* this leads to gaps between the buffers dependend on the alignment */
        pPoolConfig->bufMemSize = pPoolConfig->maxBufNum * (pPoolConfig->bufSize +
                                  pPoolConfig->bufAlign);
    }
 
    return ret;
}
 
 
/******************************************************************************
 * MediaBufPoolCreate
 *****************************************************************************/
RESULT MediaBufPoolCreate(MediaBufPool_t*       pBufPool,
                          MediaBufPoolConfig_t* pPoolConfig,
                          MediaBufPoolMemory_t  poolMemory)
{
    uint32_t        i;
    uint32_t        offset;
    void*           pBuf;
    RESULT  ret = RET_SUCCESS;
    if(pBufPool == NULL)
    {
        return RET_WRONG_HANDLE;
    }
 
    if((poolMemory.pMetaDataMemory  == NULL) ||
       (poolMemory.pBufferMemory    == NULL))
    {
        return RET_INVALID_PARM;
    }
 
    if((pPoolConfig->bufNum == 0U) || (pPoolConfig->bufSize == 0U) ||
       (pPoolConfig->maxBufNum < pPoolConfig->bufNum))
    {
        return RET_WRONG_CONFIG;
    }
 
    /* If buffer pool is switched to ringbuffer mode, there must not be any gap*/
    /* between individual buffers inside static buffer pool, introduced by data*/
    /* alignement issues (e.g. rouding up of internal buffer sizes).*/
    /* That is, the buffer size must be a multiple of the data alignement.*/
    if((pPoolConfig->flags & BUFPOOL_RINGBUFFER) &&
       (pPoolConfig->bufSize % pPoolConfig->bufAlign))
    {
        return RET_WRONG_CONFIG;
    }
 
    /* initialize buffer pool object*/
    (void) memset(pBufPool, 0, sizeof(MediaBufPool_t));
 
    pBufPool->bufSize              = pPoolConfig->bufSize;
    pBufPool->metaDataSizeMediaBuf = pPoolConfig->metaDataSizeMediaBuf;
    pBufPool->bufNum               = pPoolConfig->bufNum;
    pBufPool->freeBufNum           = pPoolConfig->bufNum;
    pBufPool->maxBufNum            = pPoolConfig->maxBufNum;
    pBufPool->poolSize             = pPoolConfig->bufNum * pPoolConfig->bufSize;
    pBufPool->flags                = pPoolConfig->flags;
 
    /* The memory given by the caller is assigned to the buffer pool */
    pBufPool->pBaseAddress = poolMemory.pMetaDataMemory;
 
    /* Make sure that sizes in pPoolConfig are written */
    (void) MediaBufPoolGetSize(pPoolConfig);
    /* initialize buffer memory to zero */
    (void) memset(poolMemory.pMetaDataMemory, 0, (size_t) pPoolConfig->metaDataMemSize);
 
    /* We use the first partition of memory for the media buffer array */
    pBufPool->pBufArray = (MediaBuffer_t*) pBufPool->pBaseAddress;
 
    /* The second partition of memory is used for the buffer meta data array for Media Buffers */
    offset = (pPoolConfig->maxBufNum * sizeof(MediaBuffer_t));
    pBufPool->pMetaDataMediaBufBase = (void*) (((ulong_t) pBufPool->pBaseAddress) + offset);
 
    /* The memory pointed to by pBufferMemory is used for
     * the real buffer memory. This memory is assigned to the pointers stored in the corresponding
     * MediaBuffer_t[]. Due to the alignment restrictions given by the user it must be kept in mind
     * that the alignment is added. */
    pBuf = poolMemory.pBufferMemory;
    for(i = 0U; i < pBufPool->maxBufNum; i++)
    {
        /* set base address of buffer
           (will be changed in case that buffer pool is resized) */
        pBufPool->pBufArray[i].pBaseAddress = (uint8_t*) MEDIA_BUF_ALIGN((ulong_t)pBuf +
                        (i * pPoolConfig->bufSize), (uint32_t)pPoolConfig->bufAlign);
        /* set base size of buffer
           (will be changed in case that buffer pool is resized) */
        pBufPool->pBufArray[i].baseSize = pBufPool->bufSize;
 
        if ( pPoolConfig->metaDataSizeMediaBuf != 0 )
        {
            /* set Media Buffer meta data structure address for
               (stays the same during lifetime of buffer pool) */
            pBufPool->pBufArray[i].pMetaData = (void *)((ulong_t) pBufPool->pMetaDataMediaBufBase +
                                               (i * pPoolConfig->metaDataSizeMediaBuf));
        }
        else
        {
            pBufPool->pBufArray[i].pMetaData = NULL;
        }
 
        MediaBufInit(&pBufPool->pBufArray[i]);
    }
 
    AtomicMutexInit();
 
    return ret;
}
 
 
/******************************************************************************
 * MediaBufPoolDestroy
 *****************************************************************************/
RESULT MediaBufPoolDestroy(MediaBufPool_t *pBufPool)
{
    DCT_ASSERT(pBufPool != NULL);
 
    (void) memset(pBufPool, 0, sizeof(MediaBufPool_t));
    AtomicMutexDestory();
    return RET_SUCCESS;
}
 
 
/******************************************************************************
 * MediaBufPoolReset
 *****************************************************************************/
RESULT MediaBufPoolReset(MediaBufPool_t* pBufPool)
{
    uint32_t i;
 
    DCT_ASSERT(pBufPool != NULL);
 
    /* reset state variables */
    pBufPool->freeBufNum = pBufPool->bufNum;
    pBufPool->fillLevel  = 0;
    pBufPool->index      = 0;
 
    /* re-init media buffers */
    for(i = 0U; i < pBufPool->maxBufNum; i++)
    {
        MediaBufInit(&pBufPool->pBufArray[i]);
    }
 
    return RET_SUCCESS;
}
 
 
/******************************************************************************
 * MediaBufPoolRegisterCb
 *****************************************************************************/
RESULT MediaBufPoolRegisterCb(MediaBufPool_t*         pBufPool,
                              MediaBufPoolCbNotify_t  fpCallback,
                              void*                   pUserContext)
{
    RESULT ret = RET_NOTAVAILABLE;
    int32_t i;
 
    if(pBufPool == NULL)
    {
        return RET_WRONG_HANDLE;
    }
 
    if(fpCallback == NULL)
    {
        return RET_INVALID_PARM;
    }
 
    if(pBufPool->notify.fpCallback == NULL)
    {
        pBufPool->notify.fpCallback    = fpCallback;
        pBufPool->notify.pUserContext  = pUserContext;
        ret = RET_SUCCESS;
    }
 
    return ret;
}
 
 
/******************************************************************************
 * MediaBufPoolDeregisterCb
 *****************************************************************************/
RESULT MediaBufPoolDeregisterCb(MediaBufPool_t*         pBufPool,
                                MediaBufPoolCbNotify_t  fpCallback)
{
    RESULT ret = RET_NOTAVAILABLE;
    int32_t i;
 
    if(pBufPool == NULL)
    {
        return RET_WRONG_HANDLE;
    }
 
    if(fpCallback == NULL)
    {
        return RET_INVALID_PARM;
    }
 
    if(pBufPool->notify.fpCallback == fpCallback)
    {
        pBufPool->notify.fpCallback   = NULL;
        pBufPool->notify.pUserContext = NULL;
        ret = RET_SUCCESS;
    }
 
    return ret;
}
 
 
 
/******************************************************************************
 * MediaBufPoolGetBuffer
 *****************************************************************************/
MediaBuffer_t* MediaBufPoolGetBuffer(MediaBufPool_t* pBufPool)
{
    MediaBuffer_t *pMediaBuffer;
 
    DCT_ASSERT(pBufPool != NULL);
 
    for(;;)
    {
        /* if the requested number of buffers is greater than the maximum then wait*/
        while( pBufPool->freeBufNum == 0U )
        {
            return  NULL;
        }
 
        /* at least one buffer is free*/
        for(;;)
        {
            if(!pBufPool->pBufArray[pBufPool->index].lockCount)
            {
                /* adjust the resources count*/
                pBufPool->freeBufNum--;
                pBufPool->pBufArray[pBufPool->index].lockCount = 1;
                pBufPool->pBufArray[pBufPool->index].pOwner = pBufPool;
                pMediaBuffer = &pBufPool->pBufArray[pBufPool->index];
 
                if(++(pBufPool->index) >= pBufPool->bufNum)
                {
                    pBufPool->index = 0U;
                }
 
                return pMediaBuffer;
            }
            else
            {
                /* pBufPool->uiIndex points to the next buffer to be checked for a get empty request.*/
                /* If that buffer is locked, no media buffer is returned, because random access is*/
                /* not allowed in ringbuffer mode.*/
                if(pBufPool->flags & BUFPOOL_RINGBUFFER)
                {
                    return  NULL;
                }
            }
            if(++(pBufPool->index) >= pBufPool->bufNum)
            {
                pBufPool->index = 0U;
            }
        }
    } /* for( ;;)*/
}
 
 
/******************************************************************************
 * MediaBufPoolFreeBuffer
 *****************************************************************************/
void MediaBufPoolFreeBuffer(MediaBufPool_t* pBufPool, MediaBuffer_t *pBuf)
{
    int32_t i;
 
    DCT_ASSERT(pBufPool != NULL);
    DCT_ASSERT(pBuf != NULL);
 
    pBuf->lockCount = 0;
    pBufPool->freeBufNum++;
 
    if(pBufPool->notify.fpCallback != NULL)
    {
        (pBufPool->notify.fpCallback)(pBufPool->notify.pUserContext, pBuf);
    }
}