Coverage Report

Created: 2025-11-24 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libmpeg2/common/impeg2_buf_mgr.c
Line
Count
Source
1
/******************************************************************************
2
 *
3
 * Copyright (C) 2015 The Android Open Source Project
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
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19
*/
20
21
/**
22
*******************************************************************************
23
* @file
24
*  impeg2_buf_mgr.c
25
*
26
* @brief
27
*  Contains function definitions for buffer management
28
*
29
* @author
30
*  Srinivas T
31
*
32
* @par List of Functions:
33
*   - impeg2_buf_mgr_init()
34
*   - impeg2_buf_mgr_add()
35
*   - impeg2_buf_mgr_get_next_free()
36
*   - impeg2_buf_mgr_check_free()
37
*   - impeg2_buf_mgr_release()
38
*   - impeg2_buf_mgr_set_status()
39
*   - impeg2_buf_mgr_get_status()
40
*   - impeg2_buf_mgr_get_buf()
41
*   - impeg2_buf_mgr_get_num_active_buf()
42
*
43
* @remarks
44
*  None
45
*
46
*******************************************************************************
47
*/
48
#include <stdio.h>
49
#include <stdlib.h>
50
#include "iv_datatypedef.h"
51
#include "impeg2_defs.h"
52
#include "impeg2_buf_mgr.h"
53
54
55
56
/**
57
*******************************************************************************
58
*
59
* @brief
60
*      Buffer manager initialization function.
61
*
62
* @par Description:
63
*    Initializes the buffer manager structure
64
*
65
* @param[in] ps_buf_mgr
66
*  Pointer to the buffer manager
67
*
68
* @returns
69
*
70
* @remarks
71
*  None
72
*
73
*******************************************************************************
74
*/
75
76
void impeg2_buf_mgr_init(
77
                buf_mgr_t *ps_buf_mgr)
78
5.94k
{
79
5.94k
    WORD32 id;
80
81
5.94k
    ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
82
5.94k
    ps_buf_mgr->u4_active_buf_cnt = 0;
83
84
386k
    for(id = 0; id < BUF_MGR_MAX_CNT; id++)
85
380k
    {
86
380k
        ps_buf_mgr->au4_status[id] = 0;
87
380k
        ps_buf_mgr->apv_ptr[id] = NULL;
88
380k
    }
89
5.94k
}
90
91
/**
92
*******************************************************************************
93
*
94
* @brief
95
*      Buffer manager reset function.
96
*
97
* @par Description:
98
*    Resets the buffer manager structure
99
*
100
* @param[in] ps_buf_mgr
101
*  Pointer to the buffer manager
102
*
103
* @returns
104
*
105
* @remarks
106
*  None
107
*
108
*******************************************************************************
109
*/
110
111
void impeg2_buf_mgr_reset(
112
                buf_mgr_t *ps_buf_mgr)
113
9.71k
{
114
9.71k
    WORD32 id;
115
116
9.71k
    ps_buf_mgr->u4_max_buf_cnt = BUF_MGR_MAX_CNT;
117
9.71k
    ps_buf_mgr->u4_active_buf_cnt = 0;
118
119
631k
    for(id = 0; id < BUF_MGR_MAX_CNT; id++)
120
621k
    {
121
621k
        ps_buf_mgr->au4_status[id] = 0;
122
621k
    }
123
9.71k
}
124
125
/**
126
*******************************************************************************
127
*
128
* @brief
129
*       Adds and increments the buffer and buffer count.
130
*
131
* @par Description:
132
*     Adds a buffer to the buffer manager if it is not already  present and
133
*   increments the  active buffer count
134
*
135
* @param[in] ps_buf_mgr
136
*  Pointer to the buffer manager
137
*
138
* @param[in] pv_ptr
139
*  Pointer to the buffer to be added
140
*
141
* @returns  Returns 0 on success, -1 otherwise
142
*
143
* @remarks
144
*  None
145
*
146
*******************************************************************************
147
*/
148
WORD32 impeg2_buf_mgr_add(
149
                buf_mgr_t *ps_buf_mgr,
150
                void *pv_ptr,
151
                WORD32 i4_buf_id)
152
29.7k
{
153
154
    /* Check if buffer ID is within allowed range */
155
29.7k
    if(i4_buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
156
0
    {
157
0
        return (-1);
158
0
    }
159
160
    /* Check if the current ID is being used to hold some other buffer */
161
29.7k
    if((ps_buf_mgr->apv_ptr[i4_buf_id] != NULL) &&
162
0
       (ps_buf_mgr->apv_ptr[i4_buf_id] != pv_ptr))
163
0
    {
164
0
        return (-1);
165
0
    }
166
29.7k
    ps_buf_mgr->apv_ptr[i4_buf_id] = pv_ptr;
167
168
29.7k
    return 0;
169
29.7k
}
170
171
172
/**
173
*******************************************************************************
174
*
175
* @brief
176
*   Gets the next free buffer.
177
*
178
* @par Description:
179
*     Returns the next free buffer available and sets the  corresponding status
180
*   to DEC
181
*
182
* @param[in] ps_buf_mgr
183
*  Pointer to the buffer manager
184
*
185
* @param[in] pi4_buf_id
186
*  Pointer to the id of the free buffer
187
*
188
* @returns  Pointer to the free buffer
189
*
190
* @remarks
191
*  None
192
*
193
*******************************************************************************
194
*/
195
void* impeg2_buf_mgr_get_next_free(
196
                buf_mgr_t *ps_buf_mgr,
197
                WORD32 *pi4_buf_id)
198
27.5k
{
199
27.5k
    WORD32 id;
200
27.5k
    void *pv_ret_ptr;
201
202
27.5k
    pv_ret_ptr = NULL;
203
90.7k
    for(id = 0; id < (WORD32)ps_buf_mgr->u4_max_buf_cnt; id++)
204
90.1k
    {
205
        /* Check if the buffer is non-null and status is zero */
206
90.1k
        if((ps_buf_mgr->au4_status[id] == 0) && (ps_buf_mgr->apv_ptr[id]))
207
27.0k
        {
208
27.0k
            *pi4_buf_id = id;
209
            /* DEC is set to 1 */
210
27.0k
            ps_buf_mgr->au4_status[id] = 1;
211
27.0k
            pv_ret_ptr = ps_buf_mgr->apv_ptr[id];
212
27.0k
            break;
213
27.0k
        }
214
90.1k
    }
215
216
27.5k
    return pv_ret_ptr;
217
27.5k
}
218
219
220
/**
221
*******************************************************************************
222
*
223
* @brief
224
*      Checks the buffer manager for free buffers available.
225
*
226
* @par Description:
227
*  Checks if there are any free buffers available
228
*
229
* @param[in] ps_buf_mgr
230
*  Pointer to the buffer manager
231
*
232
* @returns  Returns 0 if available, -1 otherwise
233
*
234
* @remarks
235
*  None
236
*
237
*******************************************************************************
238
*/
239
WORD32 impeg2_buf_mgr_check_free(
240
                buf_mgr_t *ps_buf_mgr)
241
0
{
242
0
    UWORD32 id;
243
244
0
    for(id = 0; id < ps_buf_mgr->u4_max_buf_cnt; id++)
245
0
    {
246
0
        if((ps_buf_mgr->au4_status[id] == 0) &&
247
0
           (ps_buf_mgr->apv_ptr[id]))
248
0
        {
249
0
            return 1;
250
0
        }
251
0
    }
252
253
0
    return 0;
254
255
0
}
256
257
258
/**
259
*******************************************************************************
260
*
261
* @brief
262
*       Resets the status bits.
263
*
264
* @par Description:
265
*     resets the status bits that the mask contains (status  corresponding to
266
*    the id)
267
*
268
* @param[in] ps_buf_mgr
269
*  Pointer to the buffer manager
270
*
271
* @param[in] buf_id
272
*  ID of the buffer status to be released
273
*
274
* @param[in] mask
275
*  Contains the bits that are to be reset
276
*
277
* @returns  0 if success, -1 otherwise
278
*
279
* @remarks
280
*  None
281
*
282
*******************************************************************************
283
*/
284
WORD32 impeg2_buf_mgr_release(
285
                buf_mgr_t *ps_buf_mgr,
286
                WORD32 i4_buf_id,
287
                UWORD32 u4_mask)
288
52.8k
{
289
    /* If the given id is pointing to an id which is not yet added */
290
52.8k
    if(i4_buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
291
0
    {
292
0
        return (-1);
293
0
    }
294
295
52.8k
    if(0 == (ps_buf_mgr->au4_status[i4_buf_id] & u4_mask))
296
272
    {
297
272
        return (-1);
298
272
    }
299
300
52.5k
    ps_buf_mgr->au4_status[i4_buf_id] &= ~u4_mask;
301
302
    /* If both the REF and DISP are zero, DEC is set to zero */
303
52.5k
    if(ps_buf_mgr->au4_status[i4_buf_id] == 1)
304
16.3k
    {
305
16.3k
        ps_buf_mgr->au4_status[i4_buf_id] = 0;
306
16.3k
    }
307
308
52.5k
    return 0;
309
52.8k
}
310
311
312
/**
313
*******************************************************************************
314
*
315
* @brief
316
*      Sets the status bit.
317
*
318
* @par Description:
319
*     sets the status bits that the mask contains (status  corresponding to the
320
*    id)
321
*
322
*
323
* @param[in] ps_buf_mgr
324
*  Pointer to the buffer manager
325
*
326
* @param[in] buf_id
327
*  ID of the buffer whose status needs to be modified
328
*
329
*
330
* @param[in] mask
331
*  Contains the bits that are to be set
332
*
333
* @returns  0 if success, -1 otherwise
334
*
335
* @remarks
336
*  None
337
*
338
*******************************************************************************
339
*/
340
WORD32 impeg2_buf_mgr_set_status(
341
                buf_mgr_t *ps_buf_mgr,
342
                WORD32 i4_buf_id,
343
                UWORD32 u4_mask)
344
81.2k
{
345
81.2k
    if(i4_buf_id >= (WORD32)ps_buf_mgr->u4_max_buf_cnt)
346
0
    {
347
0
        return (-1);
348
0
    }
349
350
351
81.2k
    if((ps_buf_mgr->au4_status[i4_buf_id] & u4_mask) != 0)
352
0
    {
353
0
        return (-1);
354
0
    }
355
356
81.2k
    ps_buf_mgr->au4_status[i4_buf_id] |= u4_mask;
357
81.2k
    return 0;
358
81.2k
}
359
360
361
/**
362
*******************************************************************************
363
*
364
* @brief
365
*   Returns the status of the buffer.
366
*
367
* @par Description:
368
*  Returns the status of the buffer corresponding to the id
369
*
370
* @param[in] ps_buf_mgr
371
*  Pointer to the buffer manager
372
*
373
* @param[in] buf_id
374
*  ID of the buffer status required
375
*
376
* @returns  Status of the buffer corresponding to the id
377
*
378
* @remarks
379
*  None
380
*
381
*******************************************************************************
382
*/
383
UWORD32 impeg2_buf_mgr_get_status(
384
                buf_mgr_t *ps_buf_mgr,
385
                WORD32 i4_buf_id)
386
0
{
387
0
    return ps_buf_mgr->au4_status[i4_buf_id];
388
0
}
389
390
391
/**
392
*******************************************************************************
393
*
394
* @brief
395
*      Gets the buffer from the buffer manager
396
*
397
* @par Description:
398
*        Returns the pointer to the buffer corresponding to the id
399
*
400
* @param[in] ps_buf_mgr
401
*  Pointer to the buffer manager
402
*
403
* @param[in] buf_id
404
*  ID of the buffer required
405
*
406
* @returns  Pointer to the buffer required
407
*
408
* @remarks
409
*  None
410
*
411
*******************************************************************************
412
*/
413
void* impeg2_buf_mgr_get_buf(
414
                buf_mgr_t *ps_buf_mgr,
415
                WORD32 i4_buf_id)
416
0
{
417
0
    return ps_buf_mgr->apv_ptr[i4_buf_id];
418
0
}
419
420
421
/**
422
*******************************************************************************
423
*
424
* @brief
425
*        Gets the no.of active buffer
426
*
427
* @par Description:
428
*      Return the number of active buffers in the buffer manager
429
*
430
* @param[in] ps_buf_mgr
431
*  Pointer to the buffer manager
432
*
433
* @returns  number of active buffers
434
*
435
* @remarks
436
*  None
437
*
438
*******************************************************************************
439
*/
440
UWORD32 impeg2_buf_mgr_get_num_active_buf(
441
                buf_mgr_t *ps_buf_mgr)
442
0
{
443
0
    return ps_buf_mgr->u4_max_buf_cnt;
444
0
}