PageRenderTime 68ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/H264Dec/source/h264bsd_slice_group_map.c

http://github.com/mbebenita/Broadway
C | 589 lines | 278 code | 98 blank | 213 comment | 51 complexity | 131c784c8f14a43d9fc79b055ee2341f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*------------------------------------------------------------------------------
  17. Table of contents
  18. 1. Include headers
  19. 2. External compiler flags
  20. 3. Module defines
  21. 4. Local function prototypes
  22. 5. Functions
  23. DecodeInterleavedMap
  24. DecodeDispersedMap
  25. DecodeForegroundLeftOverMap
  26. DecodeBoxOutMap
  27. DecodeRasterScanMap
  28. DecodeWipeMap
  29. h264bsdDecodeSliceGroupMap
  30. ------------------------------------------------------------------------------*/
  31. /*------------------------------------------------------------------------------
  32. 1. Include headers
  33. ------------------------------------------------------------------------------*/
  34. #include "basetype.h"
  35. #include "h264bsd_slice_group_map.h"
  36. #include "h264bsd_cfg.h"
  37. #include "h264bsd_pic_param_set.h"
  38. #include "h264bsd_util.h"
  39. /*------------------------------------------------------------------------------
  40. 2. External compiler flags
  41. --------------------------------------------------------------------------------
  42. --------------------------------------------------------------------------------
  43. 3. Module defines
  44. ------------------------------------------------------------------------------*/
  45. /*------------------------------------------------------------------------------
  46. 4. Local function prototypes
  47. ------------------------------------------------------------------------------*/
  48. static void DecodeInterleavedMap(
  49. u32 *map,
  50. u32 numSliceGroups,
  51. u32 *runLength,
  52. u32 picSize);
  53. static void DecodeDispersedMap(
  54. u32 *map,
  55. u32 numSliceGroups,
  56. u32 picWidth,
  57. u32 picHeight);
  58. static void DecodeForegroundLeftOverMap(
  59. u32 *map,
  60. u32 numSliceGroups,
  61. u32 *topLeft,
  62. u32 *bottomRight,
  63. u32 picWidth,
  64. u32 picHeight);
  65. static void DecodeBoxOutMap(
  66. u32 *map,
  67. u32 sliceGroupChangeDirectionFlag,
  68. u32 unitsInSliceGroup0,
  69. u32 picWidth,
  70. u32 picHeight);
  71. static void DecodeRasterScanMap(
  72. u32 *map,
  73. u32 sliceGroupChangeDirectionFlag,
  74. u32 sizeOfUpperLeftGroup,
  75. u32 picSize);
  76. static void DecodeWipeMap(
  77. u32 *map,
  78. u32 sliceGroupChangeDirectionFlag,
  79. u32 sizeOfUpperLeftGroup,
  80. u32 picWidth,
  81. u32 picHeight);
  82. /*------------------------------------------------------------------------------
  83. Function: DecodeInterleavedMap
  84. Functional description:
  85. Function to decode interleaved slice group map type, i.e. slice
  86. group map type 0.
  87. Inputs:
  88. map pointer to the map
  89. numSliceGroups number of slice groups
  90. runLength run_length[] values for each slice group
  91. picSize picture size in macroblocks
  92. Outputs:
  93. map slice group map is stored here
  94. Returns:
  95. none
  96. ------------------------------------------------------------------------------*/
  97. void DecodeInterleavedMap(
  98. u32 *map,
  99. u32 numSliceGroups,
  100. u32 *runLength,
  101. u32 picSize)
  102. {
  103. /* Variables */
  104. u32 i,j, group;
  105. /* Code */
  106. ASSERT(map);
  107. ASSERT(numSliceGroups >= 1 && numSliceGroups <= MAX_NUM_SLICE_GROUPS);
  108. ASSERT(runLength);
  109. i = 0;
  110. do {
  111. for (group = 0; group < numSliceGroups && i < picSize;
  112. i += runLength[group++])
  113. {
  114. ASSERT(runLength[group] <= picSize);
  115. for (j = 0; j < runLength[group] && i + j < picSize; j++)
  116. map[i+j] = group;
  117. }
  118. } while (i < picSize);
  119. }
  120. /*------------------------------------------------------------------------------
  121. Function: DecodeDispersedMap
  122. Functional description:
  123. Function to decode dispersed slice group map type, i.e. slice
  124. group map type 1.
  125. Inputs:
  126. map pointer to the map
  127. numSliceGroups number of slice groups
  128. picWidth picture width in macroblocks
  129. picHeight picture height in macroblocks
  130. Outputs:
  131. map slice group map is stored here
  132. Returns:
  133. none
  134. ------------------------------------------------------------------------------*/
  135. void DecodeDispersedMap(
  136. u32 *map,
  137. u32 numSliceGroups,
  138. u32 picWidth,
  139. u32 picHeight)
  140. {
  141. /* Variables */
  142. u32 i, picSize;
  143. /* Code */
  144. ASSERT(map);
  145. ASSERT(numSliceGroups >= 1 && numSliceGroups <= MAX_NUM_SLICE_GROUPS);
  146. ASSERT(picWidth);
  147. ASSERT(picHeight);
  148. picSize = picWidth * picHeight;
  149. for (i = 0; i < picSize; i++)
  150. map[i] = ((i % picWidth) + (((i / picWidth) * numSliceGroups) >> 1)) %
  151. numSliceGroups;
  152. }
  153. /*------------------------------------------------------------------------------
  154. Function: DecodeForegroundLeftOverMap
  155. Functional description:
  156. Function to decode foreground with left-over slice group map type,
  157. i.e. slice group map type 2.
  158. Inputs:
  159. map pointer to the map
  160. numSliceGroups number of slice groups
  161. topLeft top_left[] values
  162. bottomRight bottom_right[] values
  163. picWidth picture width in macroblocks
  164. picHeight picture height in macroblocks
  165. Outputs:
  166. map slice group map is stored here
  167. Returns:
  168. none
  169. ------------------------------------------------------------------------------*/
  170. void DecodeForegroundLeftOverMap(
  171. u32 *map,
  172. u32 numSliceGroups,
  173. u32 *topLeft,
  174. u32 *bottomRight,
  175. u32 picWidth,
  176. u32 picHeight)
  177. {
  178. /* Variables */
  179. u32 i,y,x,yTopLeft,yBottomRight,xTopLeft,xBottomRight, picSize;
  180. u32 group;
  181. /* Code */
  182. ASSERT(map);
  183. ASSERT(numSliceGroups >= 1 && numSliceGroups <= MAX_NUM_SLICE_GROUPS);
  184. ASSERT(topLeft);
  185. ASSERT(bottomRight);
  186. ASSERT(picWidth);
  187. ASSERT(picHeight);
  188. picSize = picWidth * picHeight;
  189. for (i = 0; i < picSize; i++)
  190. map[i] = numSliceGroups - 1;
  191. for (group = numSliceGroups - 1; group--; )
  192. {
  193. ASSERT( topLeft[group] <= bottomRight[group] &&
  194. bottomRight[group] < picSize );
  195. yTopLeft = topLeft[group] / picWidth;
  196. xTopLeft = topLeft[group] % picWidth;
  197. yBottomRight = bottomRight[group] / picWidth;
  198. xBottomRight = bottomRight[group] % picWidth;
  199. ASSERT(xTopLeft <= xBottomRight);
  200. for (y = yTopLeft; y <= yBottomRight; y++)
  201. for (x = xTopLeft; x <= xBottomRight; x++)
  202. map[ y * picWidth + x ] = group;
  203. }
  204. }
  205. /*------------------------------------------------------------------------------
  206. Function: DecodeBoxOutMap
  207. Functional description:
  208. Function to decode box-out slice group map type, i.e. slice group
  209. map type 3.
  210. Inputs:
  211. map pointer to the map
  212. sliceGroupChangeDirectionFlag slice_group_change_direction_flag
  213. unitsInSliceGroup0 mbs on slice group 0
  214. picWidth picture width in macroblocks
  215. picHeight picture height in macroblocks
  216. Outputs:
  217. map slice group map is stored here
  218. Returns:
  219. none
  220. ------------------------------------------------------------------------------*/
  221. void DecodeBoxOutMap(
  222. u32 *map,
  223. u32 sliceGroupChangeDirectionFlag,
  224. u32 unitsInSliceGroup0,
  225. u32 picWidth,
  226. u32 picHeight)
  227. {
  228. /* Variables */
  229. u32 i, k, picSize;
  230. i32 x, y, xDir, yDir, leftBound, topBound, rightBound, bottomBound;
  231. u32 mapUnitVacant;
  232. /* Code */
  233. ASSERT(map);
  234. ASSERT(picWidth);
  235. ASSERT(picHeight);
  236. picSize = picWidth * picHeight;
  237. ASSERT(unitsInSliceGroup0 <= picSize);
  238. for (i = 0; i < picSize; i++)
  239. map[i] = 1;
  240. x = (picWidth - (u32)sliceGroupChangeDirectionFlag) >> 1;
  241. y = (picHeight - (u32)sliceGroupChangeDirectionFlag) >> 1;
  242. leftBound = x;
  243. topBound = y;
  244. rightBound = x;
  245. bottomBound = y;
  246. xDir = (i32)sliceGroupChangeDirectionFlag - 1;
  247. yDir = (i32)sliceGroupChangeDirectionFlag;
  248. for (k = 0; k < unitsInSliceGroup0; k += mapUnitVacant ? 1 : 0)
  249. {
  250. mapUnitVacant = (map[ (u32)y * picWidth + (u32)x ] == 1) ?
  251. HANTRO_TRUE : HANTRO_FALSE;
  252. if (mapUnitVacant)
  253. map[ (u32)y * picWidth + (u32)x ] = 0;
  254. if (xDir == -1 && x == leftBound)
  255. {
  256. leftBound = MAX(leftBound - 1, 0);
  257. x = leftBound;
  258. xDir = 0;
  259. yDir = 2 * (i32)sliceGroupChangeDirectionFlag - 1;
  260. }
  261. else if (xDir == 1 && x == rightBound)
  262. {
  263. rightBound = MIN(rightBound + 1, (i32)picWidth - 1);
  264. x = rightBound;
  265. xDir = 0;
  266. yDir = 1 - 2 * (i32)sliceGroupChangeDirectionFlag;
  267. }
  268. else if (yDir == -1 && y == topBound)
  269. {
  270. topBound = MAX(topBound - 1, 0);
  271. y = topBound;
  272. xDir = 1 - 2 * (i32)sliceGroupChangeDirectionFlag;
  273. yDir = 0;
  274. }
  275. else if (yDir == 1 && y == bottomBound)
  276. {
  277. bottomBound = MIN(bottomBound + 1, (i32)picHeight - 1);
  278. y = bottomBound;
  279. xDir = 2 * (i32)sliceGroupChangeDirectionFlag - 1;
  280. yDir = 0;
  281. }
  282. else
  283. {
  284. x += xDir;
  285. y += yDir;
  286. }
  287. }
  288. }
  289. /*------------------------------------------------------------------------------
  290. Function: DecodeRasterScanMap
  291. Functional description:
  292. Function to decode raster scan slice group map type, i.e. slice
  293. group map type 4.
  294. Inputs:
  295. map pointer to the map
  296. sliceGroupChangeDirectionFlag slice_group_change_direction_flag
  297. sizeOfUpperLeftGroup mbs in upperLeftGroup
  298. picSize picture size in macroblocks
  299. Outputs:
  300. map slice group map is stored here
  301. Returns:
  302. none
  303. ------------------------------------------------------------------------------*/
  304. void DecodeRasterScanMap(
  305. u32 *map,
  306. u32 sliceGroupChangeDirectionFlag,
  307. u32 sizeOfUpperLeftGroup,
  308. u32 picSize)
  309. {
  310. /* Variables */
  311. u32 i;
  312. /* Code */
  313. ASSERT(map);
  314. ASSERT(picSize);
  315. ASSERT(sizeOfUpperLeftGroup <= picSize);
  316. for (i = 0; i < picSize; i++)
  317. if (i < sizeOfUpperLeftGroup)
  318. map[i] = (u32)sliceGroupChangeDirectionFlag;
  319. else
  320. map[i] = 1 - (u32)sliceGroupChangeDirectionFlag;
  321. }
  322. /*------------------------------------------------------------------------------
  323. Function: DecodeWipeMap
  324. Functional description:
  325. Function to decode wipe slice group map type, i.e. slice group map
  326. type 5.
  327. Inputs:
  328. sliceGroupChangeDirectionFlag slice_group_change_direction_flag
  329. sizeOfUpperLeftGroup mbs in upperLeftGroup
  330. picWidth picture width in macroblocks
  331. picHeight picture height in macroblocks
  332. Outputs:
  333. map slice group map is stored here
  334. Returns:
  335. none
  336. ------------------------------------------------------------------------------*/
  337. void DecodeWipeMap(
  338. u32 *map,
  339. u32 sliceGroupChangeDirectionFlag,
  340. u32 sizeOfUpperLeftGroup,
  341. u32 picWidth,
  342. u32 picHeight)
  343. {
  344. /* Variables */
  345. u32 i,j,k;
  346. /* Code */
  347. ASSERT(map);
  348. ASSERT(picWidth);
  349. ASSERT(picHeight);
  350. ASSERT(sizeOfUpperLeftGroup <= picWidth * picHeight);
  351. k = 0;
  352. for (j = 0; j < picWidth; j++)
  353. for (i = 0; i < picHeight; i++)
  354. if (k++ < sizeOfUpperLeftGroup)
  355. map[ i * picWidth + j ] = (u32)sliceGroupChangeDirectionFlag;
  356. else
  357. map[ i * picWidth + j ] = 1 -
  358. (u32)sliceGroupChangeDirectionFlag;
  359. }
  360. /*------------------------------------------------------------------------------
  361. Function: h264bsdDecodeSliceGroupMap
  362. Functional description:
  363. Function to decode macroblock to slice group map. Construction
  364. of different slice group map types is handled by separate
  365. functions defined above. See standard for details how slice group
  366. maps are computed.
  367. Inputs:
  368. pps active picture parameter set
  369. sliceGroupChangeCycle slice_group_change_cycle
  370. picWidth picture width in macroblocks
  371. picHeight picture height in macroblocks
  372. Outputs:
  373. map slice group map is stored here
  374. Returns:
  375. none
  376. ------------------------------------------------------------------------------*/
  377. void h264bsdDecodeSliceGroupMap(
  378. u32 *map,
  379. picParamSet_t *pps,
  380. u32 sliceGroupChangeCycle,
  381. u32 picWidth,
  382. u32 picHeight)
  383. {
  384. /* Variables */
  385. u32 i, picSize, unitsInSliceGroup0 = 0, sizeOfUpperLeftGroup = 0;
  386. /* Code */
  387. ASSERT(map);
  388. ASSERT(pps);
  389. ASSERT(picWidth);
  390. ASSERT(picHeight);
  391. ASSERT(pps->sliceGroupMapType < 7);
  392. picSize = picWidth * picHeight;
  393. /* just one slice group -> all macroblocks belong to group 0 */
  394. if (pps->numSliceGroups == 1)
  395. {
  396. H264SwDecMemset(map, 0, picSize * sizeof(u32));
  397. return;
  398. }
  399. if (pps->sliceGroupMapType > 2 && pps->sliceGroupMapType < 6)
  400. {
  401. ASSERT(pps->sliceGroupChangeRate &&
  402. pps->sliceGroupChangeRate <= picSize);
  403. unitsInSliceGroup0 =
  404. MIN(sliceGroupChangeCycle * pps->sliceGroupChangeRate, picSize);
  405. if (pps->sliceGroupMapType == 4 || pps->sliceGroupMapType == 5)
  406. sizeOfUpperLeftGroup = pps->sliceGroupChangeDirectionFlag ?
  407. (picSize - unitsInSliceGroup0) : unitsInSliceGroup0;
  408. }
  409. switch (pps->sliceGroupMapType)
  410. {
  411. case 0:
  412. DecodeInterleavedMap(map, pps->numSliceGroups,
  413. pps->runLength, picSize);
  414. break;
  415. case 1:
  416. DecodeDispersedMap(map, pps->numSliceGroups, picWidth,
  417. picHeight);
  418. break;
  419. case 2:
  420. DecodeForegroundLeftOverMap(map, pps->numSliceGroups,
  421. pps->topLeft, pps->bottomRight, picWidth, picHeight);
  422. break;
  423. case 3:
  424. DecodeBoxOutMap(map, pps->sliceGroupChangeDirectionFlag,
  425. unitsInSliceGroup0, picWidth, picHeight);
  426. break;
  427. case 4:
  428. DecodeRasterScanMap(map,
  429. pps->sliceGroupChangeDirectionFlag, sizeOfUpperLeftGroup,
  430. picSize);
  431. break;
  432. case 5:
  433. DecodeWipeMap(map, pps->sliceGroupChangeDirectionFlag,
  434. sizeOfUpperLeftGroup, picWidth, picHeight);
  435. break;
  436. default:
  437. ASSERT(pps->sliceGroupId);
  438. for (i = 0; i < picSize; i++)
  439. {
  440. ASSERT(pps->sliceGroupId[i] < pps->numSliceGroups);
  441. map[i] = pps->sliceGroupId[i];
  442. }
  443. break;
  444. }
  445. }