summaryrefslogtreecommitdiffstats
path: root/common/ih264_buf_mgr.c
blob: ea4333eef0237d55f8f315e22c9805257855f0ea (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * 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.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/
/**
*******************************************************************************
* @file
*  ih264_buf_mgr.c
*
* @brief
*  Contains function definitions for buffer management
*
* @author
*  Srinivas T
*
* @par List of Functions:
*   - ih264_buf_mgr_size()
*   - ih264_buf_mgr_lock()
*   - ih264_buf_mgr_unlock()
*   - ih264_buf_mgr_yield()
*   - ih264_buf_mgr_free()
*   - ih264_buf_mgr_init()
*   - ih264_buf_mgr_add()
*   - ih264_buf_mgr_get_next_free()
*   - ih264_buf_mgr_check_free()
*   - ih264_buf_mgr_set_status()
*   - ih264_buf_mgr_get_status()
*   - ih264_buf_mgr_get_buf()
*   - ih264_buf_mgr_get_bufid()
*   - ih264_buf_mgr_get_num_active_buf()
*
* @remarks
*  None
*
*******************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_defs.h"
#include "ih264_error.h"
#include "ih264_buf_mgr.h"

#include "ithread.h"

/**
*******************************************************************************
*
* @brief Returns size for buf queue context. Does not include buf queue buffer
* requirements
*
* @par   Description
* Returns size for buf queue context. Does not include buf queue buffer
* requirements. Buffer size required to store the bufs should be allocated in
* addition to the value returned here.
*
* @returns Size of the buf queue context
*
* @remarks
*
*******************************************************************************
*/
WORD32 ih264_buf_mgr_size(void)
{
    WORD32 size;

    size = sizeof(buf_mgr_t);
    size += ithread_get_mutex_lock_size();

    return size;
}

/**
*******************************************************************************
*
* @brief
*   Locks the buf_mgr context
*
* @par   Description
*   Locks the buf_mgr context by calling ithread_mutex_lock()
*
* @param[in] ps_buf_mgr
*   Job Queue context
*
* @returns IH264_FAIL if mutex lock fails else IH264_SUCCESS
*
* @remarks
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_lock(buf_mgr_t *ps_buf_mgr)
{
    WORD32 retval;
    retval = ithread_mutex_lock(ps_buf_mgr->pv_mutex);
    if(retval)
    {
        return IH264_FAIL;
    }
    return IH264_SUCCESS;
}

/**
*******************************************************************************
*
* @brief
*   Unlocks the buf_mgr context
*
* @par   Description
*   Unlocks the buf_mgr context by calling ithread_mutex_unlock()
*
* @param[in] ps_buf_mgr
*   Job Queue context
*
* @returns IH264_FAIL if mutex unlock fails else IH264_SUCCESS
*
* @remarks
*
*******************************************************************************
*/

IH264_ERROR_T ih264_buf_mgr_unlock(buf_mgr_t *ps_buf_mgr)
{
    WORD32 retval;
    retval = ithread_mutex_unlock(ps_buf_mgr->pv_mutex);
    if(retval)
    {
        return IH264_FAIL;
    }
    return IH264_SUCCESS;

}
/**
*******************************************************************************
*
* @brief
*   Yeilds the thread
*
* @par   Description
*   Unlocks the buf_mgr context by calling
* ih264_buf_mgr_unlock(), ithread_yield() and then ih264_buf_mgr_lock()
* buf_mgr is unlocked before to ensure the buf_mgr can be accessed by other threads
* If unlock is not done before calling yield then no other thread can access
* the buf_mgr functions and update buf_mgr.
*
* @param[in] ps_buf_mgr
*   Job Queue context
*
* @returns IH264_FAIL if mutex lock unlock or yield fails else IH264_SUCCESS
*
* @remarks
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_yield(buf_mgr_t *ps_buf_mgr)
{

    IH264_ERROR_T ret = IH264_SUCCESS;

    IH264_ERROR_T rettmp;
    rettmp = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((rettmp != IH264_SUCCESS), rettmp);

    //ithread_usleep(10);
    ithread_yield();

    rettmp = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((rettmp != IH264_SUCCESS), rettmp);
    return ret;
}


/**
*******************************************************************************
*
* @brief free the buf queue pointers
*
* @par   Description
* Frees the buf_mgr context
*
* @param[in] pv_buf
* Memoy for buf queue buffer and buf queue context
*
* @returns Pointer to buf queue context
*
* @remarks
* Since it will be called only once by master thread this is not thread safe.
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_free(buf_mgr_t *ps_buf_mgr)
{
    WORD32 ret;
    ret = ithread_mutex_destroy(ps_buf_mgr->pv_mutex);

    if(0 == ret)
        return IH264_SUCCESS;
    else
        return IH264_FAIL;
}
/**
*******************************************************************************
*
* @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 *ih264_buf_mgr_init(void *pv_buf)
{
    WORD32 id;
    UWORD8 *pu1_buf;
    buf_mgr_t *ps_buf_mgr;
    pu1_buf = (UWORD8 *)pv_buf;

    ps_buf_mgr = (buf_mgr_t *)pu1_buf;
    pu1_buf += sizeof(buf_mgr_t);

    ps_buf_mgr->pv_mutex = pu1_buf;
    pu1_buf += ithread_get_mutex_lock_size();

    ithread_mutex_init(ps_buf_mgr->pv_mutex);

    ps_buf_mgr->i4_max_buf_cnt = BUF_MGR_MAX_CNT;
    ps_buf_mgr->i4_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;
    }

    return ps_buf_mgr;
}


/**
*******************************************************************************
*
* @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
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_add(buf_mgr_t *ps_buf_mgr,
                                void *pv_ptr,
                                WORD32 buf_id)
{

    IH264_ERROR_T ret = IH264_SUCCESS;
    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    /* Check if buffer ID is within allowed range */
    if(buf_id >= ps_buf_mgr->i4_max_buf_cnt)
    {
        ret = ih264_buf_mgr_unlock(ps_buf_mgr);
        RETURN_IF((ret != IH264_SUCCESS), ret);

        return IH264_FAIL;
    }

    /* 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))
    {
        ret = ih264_buf_mgr_unlock(ps_buf_mgr);
        RETURN_IF((ret != IH264_SUCCESS), ret);

        return IH264_FAIL;
    }
    ps_buf_mgr->apv_ptr[buf_id] = pv_ptr;
    ps_buf_mgr->i4_active_buf_cnt++;

    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    return ret;
}

/**
*******************************************************************************
*
* @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* ih264_buf_mgr_get_next_free(buf_mgr_t *ps_buf_mgr, WORD32 *pi4_buf_id)
{
    WORD32 id;
    void *pv_ret_ptr;
    IH264_ERROR_T ret = IH264_SUCCESS;
    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), NULL);

    pv_ret_ptr = NULL;
    for(id = 0; id < ps_buf_mgr->i4_active_buf_cnt; id++)
    {
        /* 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;
        }
    }
    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), NULL);

    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
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_check_free(buf_mgr_t *ps_buf_mgr)
{
    WORD32 id;
    IH264_ERROR_T ret = IH264_SUCCESS;
    IH264_ERROR_T rettmp = IH264_SUCCESS;
    rettmp = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((rettmp != IH264_SUCCESS), ret);

    ret = IH264_FAIL;
    for(id = 0; id < ps_buf_mgr->i4_active_buf_cnt; id++)
    {
        if((ps_buf_mgr->au4_status[id] == 0) &&
           (ps_buf_mgr->apv_ptr[id]))
        {
            ret = IH264_SUCCESS;
            break;
        }
    }
    rettmp = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((rettmp != IH264_SUCCESS), ret);

    return ret;

}


/**
*******************************************************************************
*
* @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
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_release(buf_mgr_t *ps_buf_mgr,
                                    WORD32 buf_id,
                                    UWORD32 mask)
{
    IH264_ERROR_T ret = IH264_SUCCESS;
    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);


    /* If the given id is pointing to an id which is not yet added */
    if(buf_id >= ps_buf_mgr->i4_active_buf_cnt)
    {
        ret = ih264_buf_mgr_unlock(ps_buf_mgr);
        RETURN_IF((ret != IH264_SUCCESS), ret);
        return IH264_FAIL;
    }

    ps_buf_mgr->au4_status[buf_id] &= ~mask;


/*     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;
    }


    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    return ret;
}


/**
*******************************************************************************
*
* @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
*
*******************************************************************************
*/
IH264_ERROR_T ih264_buf_mgr_set_status(buf_mgr_t *ps_buf_mgr,
                                       WORD32 buf_id,
                                       UWORD32 mask)
{
    IH264_ERROR_T ret = IH264_SUCCESS;
    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    if(buf_id >= ps_buf_mgr->i4_active_buf_cnt)
    {
        ret = ih264_buf_mgr_unlock(ps_buf_mgr);
        RETURN_IF((ret != IH264_SUCCESS), ret);
        return IH264_FAIL;
    }


    if((ps_buf_mgr->au4_status[buf_id] & mask) != 0)
    {
        ret = ih264_buf_mgr_unlock(ps_buf_mgr);
        RETURN_IF((ret != IH264_SUCCESS), ret);
        return IH264_FAIL;
    }

    ps_buf_mgr->au4_status[buf_id] |= mask;
    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    return ret;
}


/**
*******************************************************************************
*
* @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
*
*******************************************************************************
*/
WORD32 ih264_buf_mgr_get_status( buf_mgr_t *ps_buf_mgr, WORD32 buf_id )
{
    IH264_ERROR_T ret = IH264_SUCCESS;
    UWORD32 status;

    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    status = ps_buf_mgr->au4_status[buf_id];

    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    return status;
}


/**
*******************************************************************************
*
* @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* ih264_buf_mgr_get_buf(buf_mgr_t *ps_buf_mgr, WORD32 buf_id)
{
    IH264_ERROR_T ret = IH264_SUCCESS;
    void *pv_buf;
    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), NULL);

    pv_buf = ps_buf_mgr->apv_ptr[buf_id];

    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), NULL);

    return pv_buf;
}


/**
*******************************************************************************
*
* @brief
*  Gets the buffer id from the buffer manager if the buffer is added to the
*  buffer manager
*
* @par Description:
*  Returns the buffer id corresponding to the given buffer if it exists
*
* @param[in] ps_buf_mgr
*  Pointer to the buffer manager
*
* @param[in] pv_buf
*  Pointer to the buffer
*
* @returns  Buffer id if exists, else -1
*
* @remarks
*  None
*
*******************************************************************************
*/
WORD32 ih264_buf_mgr_get_bufid(buf_mgr_t *ps_buf_mgr, void *pv_buf)
{
    WORD32 id;
    WORD32 buf_id = -1;
    IH264_ERROR_T ret = IH264_SUCCESS;
    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    for(id = 0; id < ps_buf_mgr->i4_active_buf_cnt; id++)
    {
        if(ps_buf_mgr->apv_ptr[id] == pv_buf)
        {
            buf_id = id;
            break;
        }
    }
    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    return 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 ih264_buf_mgr_get_num_active_buf(buf_mgr_t *ps_buf_mgr)
{
    UWORD32 u4_buf_cnt;
    IH264_ERROR_T ret = IH264_SUCCESS;

    u4_buf_cnt = 0;

    ret = ih264_buf_mgr_lock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);
    u4_buf_cnt = ps_buf_mgr->i4_active_buf_cnt;

    ret = ih264_buf_mgr_unlock(ps_buf_mgr);
    RETURN_IF((ret != IH264_SUCCESS), ret);

    return u4_buf_cnt;
}