kb_dc_pool.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:7k
源码类别:

DVD

开发平台:

C/C++

  1. //-----------------------------------------------------------------------------
  2. // Project include files:
  3. //-----------------------------------------------------------------------------
  4. #include "kb_dc_pub.h"
  5. #ifdef STATIC_POOL_ALLOC
  6. SECTION_POOL_DC section_buffer_status[SECTION_POOL_NUM];
  7. #else
  8. SECTION_POOL_DC *section_buffer_status;
  9. static int SECTION_POOL_NUM = 0;
  10. #endif
  11. unsigned char *section_buffer;
  12. unsigned short section_buffer_index_w=0;
  13. unsigned short section_buffer_index_r=0;
  14. unsigned short section_last_read_index=0;
  15. extern unsigned long GetXMLMovieMaxSize(void);
  16.  void Section_Pool_Init(void)  
  17. {
  18. UINT16 i;
  19. #ifndef STATIC_POOL_ALLOC
  20. SECTION_POOL_NUM = DEFAULT_POOL_NUM;
  21. section_buffer_status = (SECTION_POOL_DC *)KBDcMalloc((SECTION_POOL_NUM)*sizeof(SECTION_POOL_DC));
  22. if(section_buffer_status==NULL)
  23. {
  24. DCDebug1("section_buffer_status malloc errorn");
  25. return;
  26. }
  27. #endif
  28. section_buffer = (unsigned char *)KBDcMalloc((SECTION_POOL_NUM+1)*SECTION_LEN*sizeof(unsigned char));
  29. if(section_buffer==NULL)
  30. {
  31. DCDebug1("section_buffer malloc errorn");
  32. return;
  33. }
  34. for(i = 0; i < SECTION_POOL_NUM; i++)
  35. {
  36. KBDcMemset(&section_buffer[i*SECTION_LEN], 0, SECTION_LEN);
  37. section_buffer_status[i].used = 0;
  38. section_buffer_status[i].data_ok = 0;
  39. }
  40. section_buffer_index_w = 0;
  41. section_buffer_index_r = 0;
  42. section_last_read_index = 0;
  43. //DCDebug4("section_buffer=0x%x,*section_buffer=%dn",section_buffer,*section_buffer);
  44. DCDebug3("n-----Section_Pool_Init done-----n");
  45. }
  46. #ifndef STATIC_POOL_ALLOC
  47.  void Section_Pool_Rebuild(void)
  48. {
  49. UINT16 i;
  50. int s_size;
  51. DCDebug3("n -----***--Section_Pool_Rebuild--***-----");
  52. //----free buffer----
  53. if(section_buffer!=NULL)
  54. {
  55. KBDcFree(section_buffer);
  56. section_buffer = NULL;
  57. }
  58. if(section_buffer_status!=NULL)
  59. {
  60. KBDcFree(section_buffer_status);
  61. section_buffer_status = NULL;
  62. }
  63. //---reallocate buffer---
  64. SECTION_POOL_NUM = GetXMLMovieMaxSize()/(SECTION_LEN);
  65. //缓冲池不能太小,以避免产生内存碎片
  66. if(SECTION_POOL_NUM<MIN_POOL_NUM)
  67. SECTION_POOL_NUM = MIN_POOL_NUM;
  68. //对于大文件,缓冲池采用折半方式,节省空间
  69. else if((SECTION_POOL_NUM>MIN_POOL_NUM)&&(SECTION_POOL_NUM<MAX_POOL_NUM))
  70. SECTION_POOL_NUM = (SECTION_POOL_NUM*2)/3;
  71. else
  72. SECTION_POOL_NUM = (SECTION_POOL_NUM)/2;
  73. SECTION_POOL_NUM = 30;//test only
  74. DCDebug4("n [Section_Pool_Rebuild]--SECTION_POOL_NUM=%d", SECTION_POOL_NUM);
  75. section_buffer = (unsigned char *)KBDcMalloc((SECTION_POOL_NUM)*SECTION_LEN);
  76. if(section_buffer==NULL)
  77. {
  78. DCDebug1("section_buffer malloc errorn");
  79. return;
  80. }
  81. section_buffer_status = (SECTION_POOL_DC *)KBDcMalloc((SECTION_POOL_NUM)*sizeof(SECTION_POOL_DC));
  82. if(section_buffer_status==NULL)
  83. {
  84. DCDebug1("section_buffer_status malloc errorn");
  85. return;
  86. }
  87. //----memset buffer----
  88. s_size = sizeof(SECTION_POOL_DC);
  89. for(i = 0; i < SECTION_POOL_NUM; i++)
  90. {
  91. KBDcMemset(&section_buffer[i*SECTION_LEN], 0, SECTION_LEN);
  92. KBDcMemset(&section_buffer_status[i], 0, s_size);
  93. }
  94. section_buffer_index_w = 0;
  95. section_buffer_index_r = 0;
  96. section_last_read_index = 0;
  97. //DCDebug4("section_buffer=0x%x,*section_buffer=%dn",section_buffer,*section_buffer);
  98. DCDebug4("n-----Section_Pool_Rebuild done-----n");
  99. }
  100. #endif
  101.  void Section_Pool_Reset(void)
  102. {
  103. unsigned short i;
  104. for(i = 0; i < SECTION_POOL_NUM; i++)
  105. {
  106. KBDcMemset(&section_buffer[i*SECTION_LEN], 0, SECTION_LEN);
  107. section_buffer_status[i].used = 0;
  108. section_buffer_status[i].data_ok = 0;
  109. }
  110. section_buffer_index_w = 0;
  111. section_buffer_index_r = 0;
  112. section_last_read_index = 0;
  113. DCDebug3("nReset_Pool_Init donen");
  114. }
  115.  void DCGetSectionCallback(unsigned long nChannelID, unsigned char *pData, unsigned long nDataLength)
  116. {
  117. int i;
  118. int sec_len;
  119. //sec_len=(((pData[1] << 8) | pData[2]) & 0x0FFF) +3;
  120. //DCDebug4("rn------DCfilterCallback:%x %x %x %x %x %x %x %x %x  sec_len=%d", pData[0], pData[1],pData[2],pData[3],pData[4],pData[5],pData[6],pData[7],pData[8], sec_len);
  121.            
  122. for(i=section_buffer_index_w;i<SECTION_POOL_NUM;i++)
  123. {
  124. if(section_buffer_status[i].used == 0 && section_buffer_status[i].data_ok == 0)
  125. break;
  126. }
  127. if(i == SECTION_POOL_NUM)
  128. {
  129. for(i=0;i<section_buffer_index_w;i++)
  130. {
  131. if(section_buffer_status[i].used == 0 && section_buffer_status[i].data_ok == 0)
  132. break;
  133. }
  134. if(i==section_buffer_index_r)
  135. {
  136. DCDebug1("rn====[DSMCC]===: Section arrived too fast!rn");
  137. while(1);
  138. }
  139. }
  140. //DCDebug4("ni=%d, w=%d, r=%d",i,section_buffer_index_w, section_buffer_index_r);
  141. KBDcMemcpy(&section_buffer[i*SECTION_LEN], pData, nDataLength);
  142. //DCDebug4("section_buffer=%x %x %x %xnn",section_buffer[i*SECTION_LEN],section_buffer[i*SECTION_LEN+1],section_buffer[i*SECTION_LEN+2],section_buffer[i*SECTION_LEN+3]);
  143. section_buffer_status[i].used = 1;
  144. section_buffer_status[i].data_ok = 1;
  145. section_buffer_index_w = i;
  146. }
  147.  int DCSectionValid(void)
  148. {
  149. //DCDebug4(" w=%d,   r=%dnr", section_buffer_index_w,section_buffer_index_r);
  150. if(section_buffer_index_w==section_buffer_index_r)
  151. return 0;
  152. else 
  153. return 1;
  154. }
  155.  void FreeSectionBuffer(void)
  156. {
  157. section_buffer_status[section_last_read_index].used = 0;
  158. }
  159. void Section_Pool_Free(void)
  160. {
  161. KBDcFree(section_buffer);
  162. section_buffer = NULL;
  163. }
  164.  unsigned char* Filter_GetSection(int *section_size)
  165. {
  166. int i;
  167. unsigned char * ptr;
  168. *section_size=0;
  169. //DCDebug4("nrsection_buffer_index_w[%d],section_buffer_index_r[%d]nr",section_buffer_index_w,section_buffer_index_r));
  170. i = section_buffer_index_r;
  171. if(!section_buffer_status[i].data_ok)
  172. {
  173. DCDebug1("!!!!!!!!!!call GetSection,but no datarn");
  174. while(1);
  175. }
  176. //DCDebug4("n Filter_GetSection:  i=%d,  w=%d,  r=%dn",i,section_buffer_index_w,section_buffer_index_r);
  177. ptr = &section_buffer[i*SECTION_LEN];
  178.        *section_size=(((ptr[1] << 8) | ptr[2]) & 0x0FFF) +3;
  179.  
  180. section_buffer_status[i].data_ok = 0;
  181. section_buffer_status[i].used = 0;
  182. section_last_read_index = i;
  183. section_buffer_index_r++;
  184. if(section_buffer_index_r == SECTION_POOL_NUM)
  185. section_buffer_index_r = 0;
  186. return ptr; 
  187. }
  188. int KB_DC_GetSection( unsigned char* pbBuf, unsigned short* pwLen )
  189. {
  190. int    len;
  191. unsigned char*  pbSect;
  192. unsigned long timeout;
  193. *pwLen = 0;
  194. timeout=0;
  195. while(1)
  196. {
  197. if(DCSectionValid()==1)
  198. {
  199. DCDebug4("n find one!");
  200. break;
  201. }
  202. timeout++;
  203. //延时1  分半,做超时处理
  204. DCDebug4("Wait for sectionnr");
  205. if(timeout>=2*30000000)
  206. //if(timeout>=12*30000000)
  207. {
  208. DCDebug1("Get section  timeout.--stop!!!nr");
  209. return -3;
  210. }
  211. if(KB_DC_FilterStatGet()!=FILTER_RUNNING)
  212. {
  213. DCDebug1("FilterStat==FILTER_COLSED, exit now!!nr");
  214. return -4;
  215. }
  216. }
  217. pbSect = Filter_GetSection(&len);
  218. if( pbSect == NULL )
  219. {
  220. DCDebug1("[LOADER]:Section ptr NULL.rn");
  221. return -1;
  222. }
  223. if( len > SECTION_LEN )
  224. {
  225. DCDebug1("[LOADER]:Section len  error[=%d].rn", len);
  226. FreeSectionBuffer();
  227. return -2;
  228. }
  229. //DCDebug4("n now sec is %d", pbSect[6]);
  230. KBDcMemcpy( pbBuf, pbSect, len );
  231. FreeSectionBuffer();
  232. *pwLen = len;
  233. return 0;
  234. }