Coverage Report

Created: 2026-08-13 06:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libhevc/common/ihevc_buf_mgr.c
Line
Count
Source
1
/******************************************************************************
2
*
3
* Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at:
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*
17
******************************************************************************/
18
/**
19
*******************************************************************************
20
* @file
21
*  ihevc_buf_mgr.c
22
*
23
* @brief
24
*  Contains function definitions for buffer management
25
*
26
* @author
27
*  Srinivas T
28
*
29
* @par List of Functions:
30
*   - ihevc_buf_mgr_init()
31
*   - ihevc_buf_mgr_add()
32
*   - ihevc_buf_mgr_get_next_free()
33
*   - ihevc_buf_mgr_check_free()
34
*   - ihevc_buf_mgr_release()
35
*   - ihevc_buf_mgr_set_status()
36
*   - ihevc_buf_mgr_get_status()
37
*   - ihevc_buf_mgr_get_buf()
38
*   - ihevc_buf_mgr_get_num_active_buf()
39
*
40
* @remarks
41
*  None
42
*
43
*******************************************************************************
44
*/
45
#include <stdlib.h>
46
#include <assert.h>
47
#include "ihevc_typedefs.h"
48
#include "ihevc_macros.h"
49
#include "ihevc_buf_mgr.h"
50
#include "ihevc_debug.h"
51
52
53
/**
54
*******************************************************************************
55
*
56
* @brief
57
*      Buffer manager initialization function.
58
*
59
* @par Description:
60
*    Initializes the buffer manager structure
61
*
62
* @param[in] ps_buf_mgr
63
*  Pointer to the buffer manager
64
*
65
* @returns
66
*
67
* @remarks
68
*  None
69
*
70
*******************************************************************************
71
*/
72
73
void ihevc_buf_mgr_init(
74
                buf_mgr_t *ps_buf_mgr)
75
0
{
76
0
    WORD32 id;
77
78
0
    ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
79
0
    ps_buf_mgr->u4_active_buf_cnt = 0;
80
81
0
    for(id = 0; id < BUF_MGR_MAX_CNT; id++)
82
0
    {
83
0
        ps_buf_mgr->au4_status[id] = 0;
84
0
        ps_buf_mgr->apv_ptr[id] = NULL;
85
0
    }
86
0
}
87
88
89
/**
90
*******************************************************************************
91
*
92
* @brief
93
*       Adds and increments the buffer and buffer count.
94
*
95
* @par Description:
96
*     Adds a buffer to the buffer manager if it is not already  present and
97
*   increments the  active buffer count
98
*
99
* @param[in] ps_buf_mgr
100
*  Pointer to the buffer manager
101
*
102
* @param[in] pv_ptr
103
*  Pointer to the buffer to be added
104
*
105
* @returns  Returns 0 on success, -1 otherwise
106
*
107
* @remarks
108
*  None
109
*
110
*******************************************************************************
111
*/
112
WORD32 ihevc_buf_mgr_add(
113
                buf_mgr_t *ps_buf_mgr,
114
                void *pv_ptr,
115
                WORD32 buf_id)
116
0
{
117
118
    /* Check if buffer ID is within allowed range */
119
0
    if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
120
0
    {
121
0
        return (-1);
122
0
    }
123
124
    /* Check if the current ID is being used to hold some other buffer */
125
0
    if((ps_buf_mgr->apv_ptr[buf_id] != NULL) &&
126
0
       (ps_buf_mgr->apv_ptr[buf_id] != pv_ptr))
127
0
    {
128
0
        return (-1);
129
0
    }
130
0
    ps_buf_mgr->apv_ptr[buf_id] = pv_ptr;
131
132
0
    return 0;
133
0
}
134
135
136
/**
137
*******************************************************************************
138
*
139
* @brief
140
*   Gets the next free buffer.
141
*
142
* @par Description:
143
*     Returns the next free buffer available and sets the  corresponding status
144
*   to DEC
145
*
146
* @param[in] ps_buf_mgr
147
*  Pointer to the buffer manager
148
*
149
* @param[in] pi4_buf_id
150
*  Pointer to the id of the free buffer
151
*
152
* @returns  Pointer to the free buffer
153
*
154
* @remarks
155
*  None
156
*
157
*******************************************************************************
158
*/
159
void* ihevc_buf_mgr_get_next_free(
160
                buf_mgr_t *ps_buf_mgr,
161
                WORD32 *pi4_buf_id)
162
0
{
163
0
    WORD32 id;
164
0
    void *pv_ret_ptr;
165
166
0
    pv_ret_ptr = NULL;
167
0
    for(id = 0; id < (WORD32)ps_buf_mgr->u4_max_buf_cnt; id++)
168
0
    {
169
0
        ASSERT(ps_buf_mgr->au4_status[id] != 2);
170
171
        /* Check if the buffer is non-null and status is zero */
172
0
        if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
173
0
        {
174
0
            *pi4_buf_id = id;
175
            /* DEC is set to 1 */
176
0
            ps_buf_mgr->au4_status[id] = 1;
177
0
            pv_ret_ptr = ps_buf_mgr->apv_ptr[id];
178
0
            break;
179
0
        }
180
0
    }
181
182
0
    return pv_ret_ptr;
183
0
}
184
185
186
/**
187
*******************************************************************************
188
*
189
* @brief
190
*      Checks the buffer manager for free buffers available.
191
*
192
* @par Description:
193
*  Checks if there are any free buffers available
194
*
195
* @param[in] ps_buf_mgr
196
*  Pointer to the buffer manager
197
*
198
* @returns  Returns 0 if available, -1 otherwise
199
*
200
* @remarks
201
*  None
202
*
203
*******************************************************************************
204
*/
205
WORD32 ihevc_buf_mgr_check_free(
206
                buf_mgr_t *ps_buf_mgr)
207
0
{
208
0
    UWORD32 id;
209
210
0
    for(id = 0; id < ps_buf_mgr->u4_max_buf_cnt; id++)
211
0
    {
212
0
        ASSERT(ps_buf_mgr->au4_status[id] != 2);
213
214
0
        if((ps_buf_mgr->au4_status[id] == 0) &&
215
0
           (ps_buf_mgr->apv_ptr[id]))
216
0
        {
217
0
            return 1;
218
0
        }
219
0
    }
220
221
0
    return 0;
222
223
0
}
224
225
226
/**
227
*******************************************************************************
228
*
229
* @brief
230
*       Resets the status bits.
231
*
232
* @par Description:
233
*     resets the status bits that the mask contains (status  corresponding to
234
*    the id)
235
*
236
* @param[in] ps_buf_mgr
237
*  Pointer to the buffer manager
238
*
239
* @param[in] buf_id
240
*  ID of the buffer status to be released
241
*
242
* @param[in] mask
243
*  Contains the bits that are to be reset
244
*
245
* @returns  0 if success, -1 otherwise
246
*
247
* @remarks
248
*  None
249
*
250
*******************************************************************************
251
*/
252
WORD32 ihevc_buf_mgr_release(
253
                buf_mgr_t *ps_buf_mgr,
254
                WORD32 buf_id,
255
                UWORD32 mask)
256
0
{
257
    /* If the given id is pointing to an id which is not yet added */
258
0
    if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
259
0
    {
260
0
        return (-1);
261
0
    }
262
263
0
    ps_buf_mgr->au4_status[buf_id] &= ~mask;
264
0
    ASSERT(ps_buf_mgr->au4_status[buf_id] != 2);
265
266
    /* If both the REF and DISP are zero, DEC is set to zero */
267
0
    if(ps_buf_mgr->au4_status[buf_id] == 1)
268
0
    {
269
0
        ps_buf_mgr->au4_status[buf_id] = 0;
270
0
    }
271
272
0
    return 0;
273
0
}
274
275
276
/**
277
*******************************************************************************
278
*
279
* @brief
280
*      Sets the status bit.
281
*
282
* @par Description:
283
*     sets the status bits that the mask contains (status  corresponding to the
284
*    id)
285
*
286
*
287
* @param[in] ps_buf_mgr
288
*  Pointer to the buffer manager
289
*
290
* @param[in] buf_id
291
*  ID of the buffer whose status needs to be modified
292
*
293
*
294
* @param[in] mask
295
*  Contains the bits that are to be set
296
*
297
* @returns  0 if success, -1 otherwise
298
*
299
* @remarks
300
*  None
301
*
302
*******************************************************************************
303
*/
304
WORD32 ihevc_buf_mgr_set_status(
305
                buf_mgr_t *ps_buf_mgr,
306
                WORD32 buf_id,
307
                UWORD32 mask)
308
0
{
309
0
    if(buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
310
0
    {
311
0
        return (-1);
312
0
    }
313
314
315
0
    if((ps_buf_mgr->au4_status[buf_id] & mask) != 0)
316
0
    {
317
0
        return (-1);
318
0
    }
319
320
0
    ps_buf_mgr->au4_status[buf_id] |= mask;
321
0
    ASSERT(ps_buf_mgr->au4_status[buf_id] != 2);
322
0
    return 0;
323
0
}
324
325
326
/**
327
*******************************************************************************
328
*
329
* @brief
330
*   Returns the status of the buffer.
331
*
332
* @par Description:
333
*  Returns the status of the buffer corresponding to the id
334
*
335
* @param[in] ps_buf_mgr
336
*  Pointer to the buffer manager
337
*
338
* @param[in] buf_id
339
*  ID of the buffer status required
340
*
341
* @returns  Status of the buffer corresponding to the id
342
*
343
* @remarks
344
*  None
345
*
346
*******************************************************************************
347
*/
348
UWORD32 ihevc_buf_mgr_get_status(
349
                buf_mgr_t *ps_buf_mgr,
350
                WORD32 buf_id)
351
0
{
352
0
    return ps_buf_mgr->au4_status[buf_id];
353
0
}
354
355
356
/**
357
*******************************************************************************
358
*
359
* @brief
360
*      Gets the buffer from the buffer manager
361
*
362
* @par Description:
363
*        Returns the pointer to the buffer corresponding to the id
364
*
365
* @param[in] ps_buf_mgr
366
*  Pointer to the buffer manager
367
*
368
* @param[in] buf_id
369
*  ID of the buffer required
370
*
371
* @returns  Pointer to the buffer required
372
*
373
* @remarks
374
*  None
375
*
376
*******************************************************************************
377
*/
378
void* ihevc_buf_mgr_get_buf(
379
                buf_mgr_t *ps_buf_mgr,
380
                WORD32 buf_id)
381
0
{
382
0
    return ps_buf_mgr->apv_ptr[buf_id];
383
0
}
384
385
386
/**
387
*******************************************************************************
388
*
389
* @brief
390
*        Gets the no.of active buffer
391
*
392
* @par Description:
393
*      Return the number of active buffers in the buffer manager
394
*
395
* @param[in] ps_buf_mgr
396
*  Pointer to the buffer manager
397
*
398
* @returns  number of active buffers
399
*
400
* @remarks
401
*  None
402
*
403
*******************************************************************************
404
*/
405
UWORD32 ihevc_buf_mgr_get_num_active_buf(
406
                buf_mgr_t *ps_buf_mgr)
407
0
{
408
0
    return ps_buf_mgr->u4_max_buf_cnt;
409
0
}