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
/******************************************************************************
 *
 * 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.h
 *
 * @brief
 *          Media Buffer Pool interface
 *
 * <pre>
 *
 *   Principal Author: Joerg Detert
 *   Creation date:    Feb 28, 2008
 *
 * </pre>
 *
 *****************************************************************************/
/**
 * @mainpage Welcome to the MediaBufferPool Interface Documentation
 *
 * Doc-Id: xx-xxx-xxx-xxx (MediaBufferPool Implementation Spec)\n
 * Author: Klaus Kaiser
 *
 * @image html silicon_image_logo.gif
 * \n
 *
 *  This buffer service is the central instance for all multimedia modules, which
 *  transmit data through buffers. All buffers are grouped into buffer pools.
 *  Each buffer pool contains one or multiple buffers with the same size. The buffer
 *  service usually manages buffers with uncompressed data (e.g. YUV, RGB, PCM) or
 *  compressed data (Audio-PES, Video-PES) but could be used for any type of data.
 *  Beside the plain data each buffer holds sideband information, e.g. timestamp
 *  and frame resolution. The following picture shows the buffer pool structure.
 *
 * @image html MediaBufferPool.png
 * \n
 *
 * Allocation and management of memory is done through those Media Buffer Pools.
 * The Buffer Pool is created with a bounded size of a contiguous physical memory block,
 * separated into equally sized smaller blocks, the so called Media Buffer. Furthermore
 * it is possible during initialization to specify a random access or ring buffer like
 * access pattern, supporting different types of applications. For example hardware blocks,
 * like decoders, which accesses content in memory through a ring buffer scheme must be
 * supported. The Media Buffer Pool interface provides functionality to request empty
 * buffers, to put or release filled buffers and to request filled buffers. This is the
 * core functionality made available to connected Media Modules.
 *
 *
 * - @subpage media_buffer_queue
 *
 */
 
#ifndef MEDIA_BUFFER_POOL_H
#define MEDIA_BUFFER_POOL_H
 
 
#if defined (__cplusplus)
extern "C" {
#endif
 
#include "media_buffer.h"
 
 
/**
 * Definition of Media Buffer Pool Flags.
 */
 
/** Buffer pool acts like a ring buffer, that means no random access */
#define BUFPOOL_RINGBUFFER    0x00000001U
 
/**
 * @brief   Callback function that can be registered to receive
 *          Media Buffer Pool events.
 */
typedef void (*MediaBufPoolCbNotify_t)
(
    void*                pUserContext,
    const MediaBuffer_t* pMediaBuffer    //!< Just for information purposes, may be used to handle chained ancillary buffers.
);
 
 
/**
 * @brief Structure holding all information to notify a registered user
 */
typedef struct MediaBufPoolNotify_s
{
    MediaBufPoolCbNotify_t fpCallback;      /**< Notification callback */
    void*                  pUserContext;    /**< Pointer to user context to pass to callback */
} MediaBufPoolNotify_t;
 
 
/**
 * @brief MediaBufPool pool object, used to maintain a number of
 *        MediaBuffer_t elements. Each MediaBuffer_t can be automatically
 *        associated to a memory chunk. The pool has to be initialized via
 *        MediaBufPoolCreate() and released via MediaBufPoolDestroy().
 */
typedef struct MediaBufPool_s
{
    void*                pBaseAddress;          /**< Base address of buffer pool. Note that */
                                                /**< a buffer pool is a contingous chunk of */
                                                /**< memory. */
    void*                pMetaDataMediaBufBase; /**< Base address of meta data buffer pool for Media Buffers.
                                                     These meta data structures are linked to the Media Buffers. */
    uint32_t             bufSize;               /**< Size of each buffer in bytes. */
    uint32_t             metaDataSizeMediaBuf;  /**< Size of meta data associated with media buffer. */
 
    uint16_t             bufNum;                /**< Number of buffers in buffer pool. */
    uint16_t             maxBufNum;
    uint32_t             poolSize;              /**< Size of complete buffer pool in bytes. */
    MediaBuffer_t*       pBufArray;             /**< Array used to manage each buffer in the pool */
    uint32_t             flags;                 /**< Buffer Pool flags. */
    uint16_t             freeBufNum;            /**< Resources count: Number of free buffers */
    uint32_t             fillLevel;             /**< How many buffers are filled with data */
    uint16_t             highWatermark;         /**< if value is reached high watermark callback is triggered */
    uint16_t             lowWatermark;          /**< if value is reached low watermark callback is triggered */
    uint32_t             index;                 /**< Pointer to current index in buffer array (internal use) */
    MediaBufPoolNotify_t notify;   /**< Array with info about users registered for notification */
} MediaBufPool_t;
 
 
/**
 * @brief The MediaBufPoolConfig is used to calculate the amount of memory needed to use
 *        pool. The call to @ref MediaBufPoolGetSize will fill the sizes of the needed memory.
 *        The user is then responsible to allocate the memory and pass the structure to the
 *        @ref MediaBufPoolCreate function.
 *
 */
typedef struct MediaBufPoolConfig_s
{
    /* Input parameters */
    uint32_t    bufSize;               /**< Size of memory chunk a buffer represents */
    uint32_t    metaDataSizeMediaBuf;  /**< Size of extra meta data strutucture attached to each Media buffer (pointed to by MediaBuffer_t.pMetaData) */
    uint32_t    flags;                 /**< Configuration flags */
    uint16_t    bufNum;                /**< Initial number of buffers */
    uint16_t    bufAlign;              /**< Needed alignment of each memory chunk the buffers represent */
    uint16_t    maxBufNum;             /**< Maximum number of buffers */
 
    /* Output parameters */
    uint32_t    metaDataMemSize; /**< Size of memory for buffer structures plus extra meta data.
                                      This needs to be directly accesable by CPU. */
    uint32_t    bufMemSize;      /**< Size of complete data memory which is represented by the buffers. */
} MediaBufPoolConfig_t;
 
 
/**
 * @brief MediaBufPoolMemory is used to pass memory to the Media Buffer Pool.
 */
typedef struct MediaBufPoolMemory_s
{
    void*   pMetaDataMemory; /**< Memory for administrative and extra meta data structures (for MediaBuffer),
                                  must be of size MediaBufPoolConfig_t.metaDataMemSize as reported by MediaBufPoolGetSize. */
    void*   pBufferMemory;   /**< Actual memory where the data is going to be placed,
                                  must be of size MediaBufPoolConfig_t.bufMemSize as reported by MediaBufPoolGetSize.
                                  Furthermore, must be aligned as specified by MediaBufPoolConfig_t.bufAlign. */
} MediaBufPoolMemory_t;
 
 
/**
 * @brief   Get a free buffer from the MediaBufferPool.
 *
 * @param   pBufPool Pointer to the MediaBufferPool.
 *
 *****************************************************************************/
MediaBuffer_t* MediaBufPoolGetBuffer(MediaBufPool_t* pBufPool);
 
 
/**
 * @brief   Inform MediaBufferPool that buffer has been filled.
 *
 * @param   pBuf  Full buffer to add.
 *
 *****************************************************************************/
void MediaBufPoolBufferFilled(MediaBufPool_t* pBufPool, MediaBuffer_t* pBuf);
 
 
/**
 * @brief   Free a buffer from the bufferpool.
 *
 * @param   pBuf    Buffer to free
 *
 *****************************************************************************/
void MediaBufPoolFreeBuffer(MediaBufPool_t* pBufPool, MediaBuffer_t* pBuf);
 
 
/**
 * @brief   Calculate the size of the memory the MediaBufferPool needs
 *          for the metadata the internal structure and the array of buffers.
 *          The caller has to assure to fill the structure with the corresponding
 *          information need. He has to fill bufSize, metaDataSize, bufNum,
 *          bufAlign and maxBufNum. With these information the function can
 *          calculate the amount of memory need.
 *          When the function returns the values for bufPoolMemSize and
 *          bufMemSize are filled.
 *
 * @param   pPoolConfig     Configuration parameters for the media buffer pool.
 *
 * @return  Status of operation.
 *****************************************************************************/
RESULT MediaBufPoolGetSize(MediaBufPoolConfig_t* pPoolConfig);
 
 
/**
 * @brief   Create a buffer pool.
 *
 * @param   pBufPool        Pointer to buffer pool object to be created.
 * @param   pPoolConfig     Pointer to config params for buffer pool.
 * @param   poolMemory      Pool memory information.
 *
 * @return  Status of operation.
 *****************************************************************************/
extern RESULT MediaBufPoolCreate(MediaBufPool_t*         pBufPool,
                                 MediaBufPoolConfig_t*   pPoolConfig,
                                 MediaBufPoolMemory_t    poolMemory);
 
 
/*****************************************************************************/
/**
 * @brief   Destroy a buffer pool.
 *
 * @param   pBufPool        Pointer to buffer pool object to be destroyed.
 *
 * @return  Status of operation.
 *****************************************************************************/
extern RESULT MediaBufPoolDestroy(MediaBufPool_t* pBufPool);
 
 
/*****************************************************************************/
/**
 * @brief   Reset a buffer pool.
 *
 * @param   pBufPool        Pointer to buffer pool object to be reset.
 *
 * @return  Status of operation.
 *****************************************************************************/
extern RESULT MediaBufPoolReset(MediaBufPool_t* pBufPool);
 
 
/* ***************************************************************************/
/**
 * @brief   Register a callback to get informed about MediaBufPool status.
 *
 * @param   pBufPool        Pointer to buffer pool object.
 * @param   fpCallback      Function to be triggered in case of event.
 * @param   pUserContext    Pointer passed in each call of the callback.
 *
 * @return  Status of operation.
 *****************************************************************************/
extern RESULT MediaBufPoolRegisterCb(MediaBufPool_t*        pBufPool,
                                     MediaBufPoolCbNotify_t fpCallback,
                                     void*                  pUserContext);
 
 
/****************************************************************************/
/**
 * @brief   Deregister a callback from MediaBufPool
 *
 * @param   pBufPool    Pointer to buffer pool object.
 * @param   fpCallback  Function to be deregistered.
 *
 * @return  Status of operation.
 *****************************************************************************/
extern RESULT MediaBufPoolDeregisterCb(MediaBufPool_t*        pBufPool,
                                       MediaBufPoolCbNotify_t fpCallback);
 
 
#if defined (__cplusplus)
}
#endif
 
 
#endif  /* MEDIA_BUFFER_POOL_H*/