summaryrefslogtreecommitdiffstats
path: root/common/ihevc_buf_mgr.c
blob: 93340ffd12162d9d221d36b04b6aa4aa599d1939 (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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/******************************************************************************
*
* Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/**
*******************************************************************************
* @file
*  ihevc_buf_mgr.c
*
* @brief
*  Contains function definitions for buffer management
*
* @author
*  Srinivas T
*
* @par List of Functions:
*   - ihevc_buf_mgr_init()
*   - ihevc_buf_mgr_add()
*   - ihevc_buf_mgr_get_next_free()
*   - ihevc_buf_mgr_check_free()
*   - ihevc_buf_mgr_release()
*   - ihevc_buf_mgr_set_status()
*   - ihevc_buf_mgr_get_status()
*   - ihevc_buf_mgr_get_buf()
*   - ihevc_buf_mgr_get_num_active_buf()
*
* @remarks
*  None
*
*******************************************************************************
*/
#include <stdlib.h>
#include <assert.h>
#include "ihevc_typedefs.h"
#include "ihevc_macros.h"
#include "ihevc_func_selector.h"
#include "ihevc_buf_mgr.h"
#include "ihevc_debug.h"


/**
*******************************************************************************
*
* @brief
*      Buffer manager initialization function.
*
* @par Description:
*    Initializes the buffer manager structure
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @returns
*
* @remarks
*  None
*
*******************************************************************************
*/

void ihevc_buf_mgr_init(
                buf_mgr_t *ps_buf_mgr)
{
    WORD32 id;

    ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
    ps_buf_mgr->u4_active_buf_cnt = 0;

    for(id = 0; id < BUF_MGR_MAX_CNT; id++)
    {
        ps_buf_mgr->au4_status[id] = 0;
        ps_buf_mgr->apv_ptr[id] = NULL;
    }
}


/**
*******************************************************************************
*
* @brief
*       Adds and increments the buffer and buffer count.
*
* @par Description:
*     Adds a buffer to the buffer manager if it is not already  present and
*   increments the  active buffer count
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] pv_ptr
*  Pointer to the buffer to be added
*
* @returns  Returns 0 on success, -1 otherwise
*
* @remarks
*  None
*
*******************************************************************************
*/
WORD32 ihevc_buf_mgr_add(
                buf_mgr_t *ps_buf_mgr,
                void *pv_ptr,
                WORD32 buf_id)
{

    /* Check if buffer ID is within allowed range */
    if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
    {
        return (-1);
    }

    /* Check if the current ID is being used to hold some other buffer */
    if((ps_buf_mgr->apv_ptr[buf_id] != NULL) &&
       (ps_buf_mgr->apv_ptr[buf_id] != pv_ptr))
    {
        return (-1);
    }
    ps_buf_mgr->apv_ptr[buf_id] = pv_ptr;

    return 0;
}


/**
*******************************************************************************
*
* @brief
*   Gets the next free buffer.
*
* @par Description:
*     Returns the next free buffer available and sets the  corresponding status
*   to DEC
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] pi4_buf_id
*  Pointer to the id of the free buffer
*
* @returns  Pointer to the free buffer
*
* @remarks
*  None
*
*******************************************************************************
*/
void* ihevc_buf_mgr_get_next_free(
                buf_mgr_t *ps_buf_mgr,
                WORD32 *pi4_buf_id)
{
    WORD32 id;
    void *pv_ret_ptr;

    pv_ret_ptr = NULL;
    for(id = 0; id < (WORD32)ps_buf_mgr->u4_max_buf_cnt; id++)
    {
        ASSERT(ps_buf_mgr->au4_status[id] != 2);

        /* Check if the buffer is non-null and status is zero */
        if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
        {
            *pi4_buf_id = id;
            /* DEC is set to 1 */
            ps_buf_mgr->au4_status[id] = 1;
            pv_ret_ptr = ps_buf_mgr->apv_ptr[id];
            break;
        }
    }

    return pv_ret_ptr;
}


/**
*******************************************************************************
*
* @brief
*      Checks the buffer manager for free buffers available.
*
* @par Description:
*  Checks if there are any free buffers available
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @returns  Returns 0 if available, -1 otherwise
*
* @remarks
*  None
*
*******************************************************************************
*/
WORD32 ihevc_buf_mgr_check_free(
                buf_mgr_t *ps_buf_mgr)
{
    UWORD32 id;

    for(id = 0; id < ps_buf_mgr->u4_max_buf_cnt; id++)
    {
        ASSERT(ps_buf_mgr->au4_status[id] != 2);

        if((ps_buf_mgr->au4_status[id] == 0) &&
           (ps_buf_mgr->apv_ptr[id]))
        {
            return 1;
        }
    }

    return 0;

}


/**
*******************************************************************************
*
* @brief
*       Resets the status bits.
*
* @par Description:
*     resets the status bits that the mask contains (status  corresponding to
*    the id)
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] buf_id
*  ID of the buffer status to be released
*
* @param[in] mask
*  Contains the bits that are to be reset
*
* @returns  0 if success, -1 otherwise
*
* @remarks
*  None
*
*******************************************************************************
*/
WORD32 ihevc_buf_mgr_release(
                buf_mgr_t *ps_buf_mgr,
                WORD32 buf_id,
                UWORD32 mask)
{
    /* If the given id is pointing to an id which is not yet added */
    if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
    {
        return (-1);
    }

    ps_buf_mgr->au4_status[buf_id] &= ~mask;
    ASSERT(ps_buf_mgr->au4_status[buf_id] != 2);

    /* If both the REF and DISP are zero, DEC is set to zero */
    if(ps_buf_mgr->au4_status[buf_id] == 1)
    {
        ps_buf_mgr->au4_status[buf_id] = 0;
    }

    return 0;
}


/**
*******************************************************************************
*
* @brief
*      Sets the status bit.
*
* @par Description:
*     sets the status bits that the mask contains (status  corresponding to the
*    id)
*
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] buf_id
*  ID of the buffer whose status needs to be modified
*
*
* @param[in] mask
*  Contains the bits that are to be set
*
* @returns  0 if success, -1 otherwise
*
* @remarks
*  None
*
*******************************************************************************
*/
WORD32 ihevc_buf_mgr_set_status(
                buf_mgr_t *ps_buf_mgr,
                WORD32 buf_id,
                UWORD32 mask)
{
    if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
    {
        return (-1);
    }


    if((ps_buf_mgr->au4_status[buf_id] & mask) != 0)
    {
        return (-1);
    }

    ps_buf_mgr->au4_status[buf_id] |= mask;
    ASSERT(ps_buf_mgr->au4_status[buf_id] != 2);
    return 0;
}


/**
*******************************************************************************
*
* @brief
*   Returns the status of the buffer.
*
* @par Description:
*  Returns the status of the buffer corresponding to the id
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] buf_id
*  ID of the buffer status required
*
* @returns  Status of the buffer corresponding to the id
*
* @remarks
*  None
*
*******************************************************************************
*/
UWORD32 ihevc_buf_mgr_get_status(
                buf_mgr_t *ps_buf_mgr,
                WORD32 buf_id)
{
    return ps_buf_mgr->au4_status[buf_id];
}


/**
*******************************************************************************
*
* @brief
*      Gets the buffer from the buffer manager
*
* @par Description:
*        Returns the pointer to the buffer corresponding to the id
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] buf_id
*  ID of the buffer required
*
* @returns  Pointer to the buffer required
*
* @remarks
*  None
*
*******************************************************************************
*/
void* ihevc_buf_mgr_get_buf(
                buf_mgr_t *ps_buf_mgr,
                WORD32 buf_id)
{
    return ps_buf_mgr->apv_ptr[buf_id];
}


/**
*******************************************************************************
*
* @brief
*        Gets the no.of active buffer
*
* @par Description:
*      Return the number of active buffers in the buffer manager
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @returns  number of active buffers
*
* @remarks
*  None
*
*******************************************************************************
*/
UWORD32 ihevc_buf_mgr_get_num_active_buf(
                buf_mgr_t *ps_buf_mgr)
{
    return ps_buf_mgr->u4_max_buf_cnt;
}