bqueue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*------------------------------------------------------------------------------
  2. -- --
  3. -- This software is confidential and proprietary and may be used --
  4. -- only as expressly authorized by a licensing agreement from --
  5. -- --
  6. -- Hantro Products Oy. --
  7. -- --
  8. -- (C) COPYRIGHT 2006 HANTRO PRODUCTS OY --
  9. -- ALL RIGHTS RESERVED --
  10. -- --
  11. -- The entire notice above must be reproduced --
  12. -- on all copies and should not be removed. --
  13. -- --
  14. --------------------------------------------------------------------------------
  15. --
  16. -- Abstract : Stream decoding utilities
  17. --
  18. --------------------------------------------------------------------------------
  19. --
  20. -- Version control information, please leave untouched.
  21. --
  22. -- $RCSfile: bqueue.c,v $
  23. -- $Date: 2010/07/23 09:19:10 $
  24. -- $Revision: 1.4 $
  25. --
  26. ------------------------------------------------------------------------------*/
  27. /*------------------------------------------------------------------------------
  28. Table of context
  29. 1. Include headers
  30. 2. External identifiers
  31. 3. Module defines
  32. 4. Module identifiers
  33. 5. Fuctions
  34. ------------------------------------------------------------------------------*/
  35. /*------------------------------------------------------------------------------
  36. 1. Include headers
  37. ------------------------------------------------------------------------------*/
  38. #include "bqueue.h"
  39. #include "dwl.h"
  40. #ifndef HANTRO_OK
  41. #define HANTRO_OK (0)
  42. #endif /* HANTRO_TRUE */
  43. #ifndef HANTRO_NOK
  44. #define HANTRO_NOK (1)
  45. #endif /* HANTRO_FALSE*/
  46. /*------------------------------------------------------------------------------
  47. 2. External identifiers
  48. ------------------------------------------------------------------------------*/
  49. /*------------------------------------------------------------------------------
  50. 3. Module defines
  51. ------------------------------------------------------------------------------*/
  52. /*------------------------------------------------------------------------------
  53. 4. Module indentifiers
  54. ------------------------------------------------------------------------------*/
  55. /*------------------------------------------------------------------------------
  56. BqueueInit
  57. Initialize buffer queue
  58. ------------------------------------------------------------------------------*/
  59. u32 BqueueInit( bufferQueue_t *bq, u32 numBuffers )
  60. {
  61. u32 i;
  62. if( numBuffers == 0 )
  63. return HANTRO_OK;
  64. bq->picI = (u32*)DWLmalloc( sizeof(u32)*numBuffers);
  65. if( bq->picI == NULL )
  66. {
  67. return HANTRO_NOK;
  68. }
  69. for( i = 0 ; i < numBuffers ; ++i )
  70. {
  71. bq->picI[i] = 0;
  72. }
  73. bq->queueSize = numBuffers;
  74. bq->ctr = 1;
  75. return HANTRO_OK;
  76. }
  77. /*------------------------------------------------------------------------------
  78. BqueueRelease
  79. ------------------------------------------------------------------------------*/
  80. void BqueueRelease( bufferQueue_t *bq )
  81. {
  82. if(bq->picI)
  83. {
  84. DWLfree(bq->picI);
  85. bq->picI = NULL;
  86. }
  87. bq->prevAnchorSlot = 0;
  88. bq->queueSize = 0;
  89. }
  90. /*------------------------------------------------------------------------------
  91. BqueueNext
  92. Return "oldest" available buffer.
  93. ------------------------------------------------------------------------------*/
  94. u32 BqueueNext( bufferQueue_t *bq, u32 ref0, u32 ref1, u32 ref2, u32 bPic )
  95. {
  96. u32 minPicI = 1<<30;
  97. u32 nextOut = (u32)0xFFFFFFFFU;
  98. u32 i;
  99. /* Find available buffer with smallest index number */
  100. i = 0;
  101. while( i < bq->queueSize )
  102. {
  103. if(i == ref0 || i == ref1 || i == ref2) /* Skip reserved anchor pictures */
  104. {
  105. i++;
  106. continue;
  107. }
  108. if( bq->picI[i] < minPicI )
  109. {
  110. minPicI = bq->picI[i];
  111. nextOut = i;
  112. }
  113. i++;
  114. }
  115. if( nextOut == (u32)0xFFFFFFFFU)
  116. {
  117. return 0; /* No buffers available, shouldn't happen */
  118. }
  119. /* Update queue state */
  120. if( bPic )
  121. {
  122. bq->picI[nextOut] = bq->ctr-1;
  123. bq->picI[bq->prevAnchorSlot]++;
  124. }
  125. else
  126. {
  127. bq->picI[nextOut] = bq->ctr;
  128. }
  129. bq->ctr++;
  130. if( !bPic )
  131. {
  132. bq->prevAnchorSlot = nextOut;
  133. }
  134. return nextOut;
  135. }
  136. /*------------------------------------------------------------------------------
  137. BqueueDiscard
  138. "Discard" output buffer, e.g. if error concealment used and buffer
  139. at issue is never going out.
  140. ------------------------------------------------------------------------------*/
  141. void BqueueDiscard( bufferQueue_t *bq, u32 buffer )
  142. {
  143. bq->picI[buffer] = 0;
  144. }