blockmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /****************************************************************************
  2. *
  3. * Module Title : BlockMap.c
  4. *
  5. * Description : Contains functions used to create the block map
  6. *
  7. * AUTHOR : Paul Wilkins
  8. *
  9. *****************************************************************************
  10. * Revision History
  11. *
  12. * 1.08 PGW 28 Feb 01 Removal of history buffer mechanism.
  13. * 1.07 PGW 04 Oct 00 Changes to RowBarEnhBlockMap()
  14. * 1.06 JBB 03 Aug 00 Fixed Problem in which rownumber was compared to
  15. * PlaneHFragments instead of PlaneVFragments, added
  16. * statistic output functions
  17. * 1.05 PGW 27/07/00 Experiments with motion score.
  18. * 1.04 JBB 30/05/00 Removed hard coded size limits
  19. * 1.03 PGW 18/02/00 Changed weighting for History blocks.
  20. * Redundant functions deleted.
  21. * Deglobalization.
  22. * 1.02 PGW 12/07/99 Changes to reduce uneccessary dependancies.
  23. * 1.01 PGW 21/06/99 Alter function of RowBarEnhBlockMap() for VFW codec.
  24. * 1.00 PGW 14/06/99 Configuration baseline
  25. *
  26. *****************************************************************************
  27. */
  28. /****************************************************************************
  29. * Header Frames
  30. *****************************************************************************
  31. */
  32. #define STRICT /* Strict type checking. */
  33. #include <string.h>
  34. #include "preproc.h"
  35. /****************************************************************************
  36. * Module constants.
  37. *****************************************************************************
  38. */
  39. /****************************************************************************
  40. * Module Types
  41. *****************************************************************************
  42. */
  43. /****************************************************************************
  44. * Imported Global Variables
  45. *****************************************************************************
  46. */
  47. /****************************************************************************
  48. * Exported Global Variables
  49. *****************************************************************************
  50. */
  51. /****************************************************************************
  52. * Foreward References
  53. *****************************************************************************
  54. */
  55. /****************************************************************************
  56. * Module Statics
  57. *****************************************************************************
  58. */
  59. /****************************************************************************
  60. *
  61. * ROUTINE : RowBarEnhBlockMap
  62. *
  63. * INPUTS : UINT32 * FragNoiseScorePtr
  64. * INT8 * FragSgcPtr
  65. * UINT32 RowNumber
  66. *
  67. * OUTPUTS : INT8 * UpdatedBlockMapPtr
  68. * INT8 * BarBlockMapPtr
  69. *
  70. * RETURNS : None.
  71. *
  72. * FUNCTION : BAR Enhances block map on a row by row basis.
  73. *
  74. * SPECIAL NOTES : Note special cases for first and last row and first and last
  75. * block in each row.
  76. *
  77. *
  78. * ERRORS : None.
  79. *
  80. ****************************************************************************/
  81. void RowBarEnhBlockMap( PP_INSTANCE *ppi,
  82. UINT32 * FragScorePtr,
  83. INT8 * FragSgcPtr,
  84. INT8 * UpdatedBlockMapPtr,
  85. INT8 * BarBlockMapPtr,
  86. UINT32 RowNumber )
  87. {
  88. // For boundary blocks relax thresholds
  89. UINT32 BarBlockThresh = ppi->BlockThreshold / 10;
  90. UINT32 BarSGCThresh = ppi->BlockSgcThresh / 2;
  91. INT32 i;
  92. // Start by blanking the row in the bar block map structure.
  93. memset( BarBlockMapPtr, BLOCK_NOT_CODED, ppi->PlaneHFragments );
  94. // First row
  95. if ( RowNumber == 0 )
  96. {
  97. // For each fragment in the row.
  98. for ( i = 0; i < ppi->PlaneHFragments; i ++ )
  99. {
  100. // Test for CANDIDATE_BLOCK or CANDIDATE_BLOCK_LOW
  101. // Uncoded or coded blocks will be ignored.
  102. if ( UpdatedBlockMapPtr[i] <= CANDIDATE_BLOCK )
  103. {
  104. // Is one of the immediate neighbours updated in the main map.
  105. // Note special cases for blocks at the start and end of rows.
  106. if ( i == 0 )
  107. {
  108. if ( (UpdatedBlockMapPtr[i+1] > BLOCK_NOT_CODED ) ||
  109. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  110. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) )
  111. {
  112. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  113. }
  114. }
  115. else if ( i == (ppi->PlaneHFragments - 1) )
  116. {
  117. if ( (UpdatedBlockMapPtr[i-1] > BLOCK_NOT_CODED ) ||
  118. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  119. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments] > BLOCK_NOT_CODED ) )
  120. {
  121. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  122. }
  123. }
  124. else
  125. {
  126. if ( (UpdatedBlockMapPtr[i-1] > BLOCK_NOT_CODED ) ||
  127. (UpdatedBlockMapPtr[i+1] > BLOCK_NOT_CODED ) ||
  128. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  129. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  130. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) )
  131. {
  132. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. // Last row
  139. // Used to read PlaneHFragments
  140. else if ( RowNumber == (UINT32)(ppi->PlaneVFragments-1))
  141. {
  142. // For each fragment in the row.
  143. for ( i = 0; i < ppi->PlaneHFragments; i ++ )
  144. {
  145. // Test for CANDIDATE_BLOCK or CANDIDATE_BLOCK_LOW
  146. // Uncoded or coded blocks will be ignored.
  147. if ( UpdatedBlockMapPtr[i] <= CANDIDATE_BLOCK )
  148. {
  149. // Is one of the immediate neighbours updated in the main map.
  150. // Note special cases for blocks at the start and end of rows.
  151. if ( i == 0 )
  152. {
  153. if ( (UpdatedBlockMapPtr[i+1] > BLOCK_NOT_CODED ) ||
  154. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  155. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) )
  156. {
  157. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  158. }
  159. }
  160. else if ( i == (ppi->PlaneHFragments - 1) )
  161. {
  162. if ( (UpdatedBlockMapPtr[i-1] > BLOCK_NOT_CODED ) ||
  163. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  164. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments] > BLOCK_NOT_CODED ) )
  165. {
  166. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  167. }
  168. }
  169. else
  170. {
  171. if ( (UpdatedBlockMapPtr[i-1] > BLOCK_NOT_CODED ) ||
  172. (UpdatedBlockMapPtr[i+1] > BLOCK_NOT_CODED ) ||
  173. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  174. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  175. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) )
  176. {
  177. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. // All other rows
  184. else
  185. {
  186. // For each fragment in the row.
  187. for ( i = 0; i < ppi->PlaneHFragments; i ++ )
  188. {
  189. // Test for CANDIDATE_BLOCK or CANDIDATE_BLOCK_LOW
  190. // Uncoded or coded blocks will be ignored.
  191. if ( UpdatedBlockMapPtr[i] <= CANDIDATE_BLOCK )
  192. {
  193. // Is one of the immediate neighbours updated in the main map.
  194. // Note special cases for blocks at the start and end of rows.
  195. if ( i == 0 )
  196. {
  197. if ( (UpdatedBlockMapPtr[i+1] > BLOCK_NOT_CODED ) ||
  198. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  199. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) ||
  200. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  201. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) )
  202. {
  203. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  204. }
  205. }
  206. else if ( i == (ppi->PlaneHFragments - 1) )
  207. {
  208. if ( (UpdatedBlockMapPtr[i-1] > BLOCK_NOT_CODED ) ||
  209. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  210. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  211. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  212. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments] > BLOCK_NOT_CODED ) )
  213. {
  214. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  215. }
  216. }
  217. else
  218. {
  219. if ( (UpdatedBlockMapPtr[i-1] > BLOCK_NOT_CODED ) ||
  220. (UpdatedBlockMapPtr[i+1] > BLOCK_NOT_CODED ) ||
  221. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  222. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  223. (UpdatedBlockMapPtr[i-ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) ||
  224. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments-1] > BLOCK_NOT_CODED ) ||
  225. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments] > BLOCK_NOT_CODED ) ||
  226. (UpdatedBlockMapPtr[i+ppi->PlaneHFragments+1] > BLOCK_NOT_CODED ) )
  227. {
  228. BarBlockMapPtr[i] = BLOCK_CODED_BAR;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. /****************************************************************************
  236. *
  237. * ROUTINE : BarCopyBack
  238. *
  239. * INPUTS : INT8 * BarBlockMapPtr
  240. *
  241. * OUTPUTS : INT8 * UpdatedBlockMapPtr
  242. *
  243. * RETURNS : None.
  244. *
  245. * FUNCTION : Copies BAR blocks back into main block map.
  246. *
  247. * SPECIAL NOTES : None.
  248. *
  249. *
  250. * ERRORS : None.
  251. *
  252. ****************************************************************************/
  253. void BarCopyBack( PP_INSTANCE *ppi,
  254. INT8 * UpdatedBlockMapPtr,
  255. INT8 * BarBlockMapPtr )
  256. {
  257. INT32 i;
  258. // For each fragment in the row.
  259. for ( i = 0; i < ppi->PlaneHFragments; i ++ )
  260. {
  261. if ( BarBlockMapPtr[i] > BLOCK_NOT_CODED )
  262. {
  263. UpdatedBlockMapPtr[i] = BarBlockMapPtr[i];
  264. }
  265. }
  266. }
  267. /****************************************************************************
  268. *
  269. * ROUTINE : CreateOutputDisplayMap
  270. *
  271. * INPUTS : INT8 * InternalFragmentsPtr
  272. * Fragment list using internal format.
  273. * INT8 * RecentHistoryPtr
  274. * List of blocks that have been marked for update int he last few frames.
  275. *
  276. * UINT8 * ExternalFragmentsPtr
  277. * Fragment list using external format.
  278. *
  279. * OUTPUTS : None.
  280. *
  281. * RETURNS : None.
  282. *
  283. * FUNCTION : Creates a block update map in the format expected by the caller.
  284. *
  285. * SPECIAL NOTES : The output block height and width must be an integer
  286. * multiple of the internal value.
  287. *
  288. *
  289. * ERRORS : None.
  290. *
  291. ****************************************************************************/
  292. void CreateOutputDisplayMap
  293. (
  294. PP_INSTANCE *ppi,
  295. INT8 *InternalFragmentsPtr
  296. )
  297. {
  298. UINT32 i;
  299. UINT32 KFScore = 0;
  300. UINT32 YBand = (ppi->ScanYPlaneFragments/8); // 1/8th of Y image.
  301. //#define DISPLAY_STATS
  302. #ifdef DISPLAY_STATS
  303. #include <stdio.h>
  304. {
  305. FILE * StatsFilePtr;
  306. StatsFilePtr = fopen( "c:\\display_stats.stt", "a" );
  307. if ( StatsFilePtr )
  308. {
  309. int i;
  310. for(i=0;i<ppi->ScanYPlaneFragments;i++)
  311. {
  312. if(i%ppi->ScanHFragments == 0 )
  313. fprintf( StatsFilePtr , "\n");
  314. fprintf( StatsFilePtr, "%2d",
  315. InternalFragmentsPtr[i]);
  316. }
  317. fprintf( StatsFilePtr , "\n");
  318. fclose( StatsFilePtr );
  319. }
  320. }
  321. #endif
  322. ppi->OutputBlocksUpdated = 0;
  323. for ( i = 0; i < ppi->ScanFrameFragments; i++ )
  324. {
  325. if ( InternalFragmentsPtr[i] > BLOCK_NOT_CODED )
  326. {
  327. ppi->OutputBlocksUpdated ++;
  328. setBlockCoded(i)
  329. }
  330. else
  331. {
  332. setBlockUncoded(i);
  333. }
  334. }
  335. // Now calculate a key frame candidate indicator.
  336. // This is based upon Y data only and only ignores the top and bottom 1/8 of the image.
  337. // Also ignore history blocks and BAR blocks.
  338. ppi->KFIndicator = 0;
  339. for ( i = YBand; i < (ppi->ScanYPlaneFragments - YBand); i++ )
  340. {
  341. if ( InternalFragmentsPtr[i] > BLOCK_CODED_BAR )
  342. {
  343. ppi->KFIndicator ++;
  344. }
  345. }
  346. // Convert the KF score to a range 0-100
  347. ppi->KFIndicator = ((ppi->KFIndicator*100)/((ppi->ScanYPlaneFragments*3)/4));
  348. }