fmo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*!
  2. *****************************************************************************
  3. *
  4. * \file fmo.c
  5. *
  6. * \brief
  7. * Support for Flexible Macroblock Ordering (FMO)
  8. *
  9. * \author
  10. * Main contributors (see contributors.h for copyright, address and affiliation details)
  11. * - Stephan Wenger [email protected]
  12. * - Karsten Suehring [email protected]
  13. ******************************************************************************
  14. */
  15. #include "global.h"
  16. #include "elements.h"
  17. #include "defines.h"
  18. #include "header.h"
  19. #include "fmo.h"
  20. //#define PRINT_FMO_MAPS
  21. static void FmoGenerateType0MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  22. static void FmoGenerateType1MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  23. static void FmoGenerateType2MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  24. static void FmoGenerateType3MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  25. static void FmoGenerateType4MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  26. static void FmoGenerateType5MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  27. static void FmoGenerateType6MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits );
  28. /*!
  29. ************************************************************************
  30. * \brief
  31. * Generates p_Vid->MapUnitToSliceGroupMap
  32. * Has to be called every time a new Picture Parameter Set is used
  33. *
  34. * \param p_Vid
  35. * image encoding parameters for current picture
  36. *
  37. ************************************************************************
  38. */
  39. static int FmoGenerateMapUnitToSliceGroupMap (VideoParameters *p_Vid)
  40. {
  41. seq_parameter_set_rbsp_t* sps = p_Vid->active_sps;
  42. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  43. unsigned int NumSliceGroupMapUnits;
  44. NumSliceGroupMapUnits = (sps->pic_height_in_map_units_minus1+1)* (sps->pic_width_in_mbs_minus1+1);
  45. if (pps->slice_group_map_type == 6)
  46. {
  47. if ((pps->pic_size_in_map_units_minus1 + 1) != NumSliceGroupMapUnits)
  48. {
  49. error ("wrong pps->pic_size_in_map_units_minus1 for used SPS and FMO type 6", 500);
  50. }
  51. }
  52. // allocate memory for p_Vid->MapUnitToSliceGroupMap
  53. if (p_Vid->MapUnitToSliceGroupMap)
  54. free (p_Vid->MapUnitToSliceGroupMap);
  55. if ((p_Vid->MapUnitToSliceGroupMap = malloc ((NumSliceGroupMapUnits) * sizeof (int))) == NULL)
  56. {
  57. printf ("cannot allocated %d bytes for p_Vid->MapUnitToSliceGroupMap, exit\n", (int) ( (pps->pic_size_in_map_units_minus1+1) * sizeof (int)));
  58. exit (-1);
  59. }
  60. if (pps->num_slice_groups_minus1 == 0) // only one slice group
  61. {
  62. memset (p_Vid->MapUnitToSliceGroupMap, 0, NumSliceGroupMapUnits * sizeof (int));
  63. return 0;
  64. }
  65. switch (pps->slice_group_map_type)
  66. {
  67. case 0:
  68. FmoGenerateType0MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  69. break;
  70. case 1:
  71. FmoGenerateType1MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  72. break;
  73. case 2:
  74. FmoGenerateType2MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  75. break;
  76. case 3:
  77. FmoGenerateType3MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  78. break;
  79. case 4:
  80. FmoGenerateType4MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  81. break;
  82. case 5:
  83. FmoGenerateType5MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  84. break;
  85. case 6:
  86. FmoGenerateType6MapUnitMap (p_Vid, NumSliceGroupMapUnits);
  87. break;
  88. default:
  89. printf ("Illegal slice_group_map_type %d , exit \n", (int) pps->slice_group_map_type);
  90. exit (-1);
  91. }
  92. return 0;
  93. }
  94. /*!
  95. ************************************************************************
  96. * \brief
  97. * Generates p_Vid->MbToSliceGroupMap from p_Vid->MapUnitToSliceGroupMap
  98. *
  99. * \param p_Vid
  100. * image encoding parameters for current picture
  101. *
  102. ************************************************************************
  103. */
  104. static int FmoGenerateMbToSliceGroupMap (VideoParameters *p_Vid)
  105. {
  106. seq_parameter_set_rbsp_t* sps = p_Vid->active_sps;
  107. unsigned i;
  108. // allocate memory for p_Vid->MbToSliceGroupMap
  109. if (p_Vid->MbToSliceGroupMap)
  110. free (p_Vid->MbToSliceGroupMap);
  111. if ((p_Vid->MbToSliceGroupMap = malloc ((p_Vid->PicSizeInMbs) * sizeof (int))) == NULL)
  112. {
  113. printf ("cannot allocate %d bytes for p_Vid->MbToSliceGroupMap, exit\n", (int) ((p_Vid->PicSizeInMbs) * sizeof (int)));
  114. exit (-1);
  115. }
  116. if ((sps->frame_mbs_only_flag)|| p_Vid->field_pic_flag)
  117. {
  118. for (i=0; i<p_Vid->PicSizeInMbs; i++)
  119. {
  120. p_Vid->MbToSliceGroupMap[i] = p_Vid->MapUnitToSliceGroupMap[i];
  121. }
  122. }
  123. else
  124. if (sps->mb_adaptive_frame_field_flag && (!p_Vid->field_pic_flag))
  125. {
  126. for (i=0; i<p_Vid->PicSizeInMbs; i++)
  127. {
  128. p_Vid->MbToSliceGroupMap[i] = p_Vid->MapUnitToSliceGroupMap[i/2];
  129. }
  130. }
  131. else
  132. {
  133. for (i=0; i<p_Vid->PicSizeInMbs; i++)
  134. {
  135. p_Vid->MbToSliceGroupMap[i] = p_Vid->MapUnitToSliceGroupMap[(i/(2*p_Vid->PicWidthInMbs))*p_Vid->PicWidthInMbs+(i%p_Vid->PicWidthInMbs)];
  136. }
  137. }
  138. return 0;
  139. }
  140. /*!
  141. ************************************************************************
  142. * \brief
  143. * FMO initialization: Generates p_Vid->MapUnitToSliceGroupMap and p_Vid->MbToSliceGroupMap.
  144. *
  145. * \param p_Vid
  146. * image encoding parameters for current picture
  147. ************************************************************************
  148. */
  149. int fmo_init(VideoParameters *p_Vid)
  150. {
  151. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  152. #ifdef PRINT_FMO_MAPS
  153. unsigned i,j;
  154. #endif
  155. FmoGenerateMapUnitToSliceGroupMap(p_Vid);
  156. FmoGenerateMbToSliceGroupMap(p_Vid);
  157. p_Vid->NumberOfSliceGroups = pps->num_slice_groups_minus1 + 1;
  158. #ifdef PRINT_FMO_MAPS
  159. printf("\n");
  160. printf("FMO Map (Units):\n");
  161. for (j=0; j<p_Vid->PicHeightInMapUnits; j++)
  162. {
  163. for (i=0; i<p_Vid->PicWidthInMbs; i++)
  164. {
  165. printf("%c",48+p_Vid->MapUnitToSliceGroupMap[i+j*p_Vid->PicWidthInMbs]);
  166. }
  167. printf("\n");
  168. }
  169. printf("\n");
  170. printf("FMO Map (Mb):\n");
  171. for (j=0; j<p_Vid->PicHeightInMbs; j++)
  172. {
  173. for (i=0; i<p_Vid->PicWidthInMbs; i++)
  174. {
  175. printf("%c",48 + p_Vid->MbToSliceGroupMap[i + j * p_Vid->PicWidthInMbs]);
  176. }
  177. printf("\n");
  178. }
  179. printf("\n");
  180. #endif
  181. return 0;
  182. }
  183. /*!
  184. ************************************************************************
  185. * \brief
  186. * Free memory allocated by FMO functions
  187. ************************************************************************
  188. */
  189. int FmoFinit(VideoParameters *p_Vid)
  190. {
  191. if (p_Vid->MbToSliceGroupMap)
  192. {
  193. free (p_Vid->MbToSliceGroupMap);
  194. p_Vid->MbToSliceGroupMap = NULL;
  195. }
  196. if (p_Vid->MapUnitToSliceGroupMap)
  197. {
  198. free (p_Vid->MapUnitToSliceGroupMap);
  199. p_Vid->MapUnitToSliceGroupMap = NULL;
  200. }
  201. return 0;
  202. }
  203. /*!
  204. ************************************************************************
  205. * \brief
  206. * FmoGetNumberOfSliceGroup(p_Vid)
  207. *
  208. * \par p_Vid:
  209. * VideoParameters
  210. ************************************************************************
  211. */
  212. int FmoGetNumberOfSliceGroup(VideoParameters *p_Vid)
  213. {
  214. return p_Vid->NumberOfSliceGroups;
  215. }
  216. /*!
  217. ************************************************************************
  218. * \brief
  219. * FmoGetLastMBOfPicture(p_Vid)
  220. * returns the macroblock number of the last MB in a picture. This
  221. * mb happens to be the last macroblock of the picture if there is only
  222. * one slice group
  223. *
  224. * \par Input:
  225. * None
  226. ************************************************************************
  227. */
  228. int FmoGetLastMBOfPicture(VideoParameters *p_Vid)
  229. {
  230. return FmoGetLastMBInSliceGroup (p_Vid, FmoGetNumberOfSliceGroup(p_Vid)-1);
  231. }
  232. /*!
  233. ************************************************************************
  234. * \brief
  235. * FmoGetLastMBInSliceGroup: Returns MB number of last MB in SG
  236. *
  237. * \par Input:
  238. * SliceGroupID (0 to 7)
  239. ************************************************************************
  240. */
  241. int FmoGetLastMBInSliceGroup (VideoParameters *p_Vid, int SliceGroup)
  242. {
  243. int i;
  244. for (i=p_Vid->PicSizeInMbs-1; i>=0; i--)
  245. if (FmoGetSliceGroupId (p_Vid, i) == SliceGroup)
  246. return i;
  247. return -1;
  248. }
  249. /*!
  250. ************************************************************************
  251. * \brief
  252. * Returns SliceGroupID for a given MB
  253. *
  254. * \param p_Vid
  255. * image encoding parameters for current picture
  256. * \param mb
  257. * Macroblock number (in scan order)
  258. ************************************************************************
  259. */
  260. int FmoGetSliceGroupId (VideoParameters *p_Vid, int mb)
  261. {
  262. assert (mb < (int) p_Vid->PicSizeInMbs);
  263. assert (p_Vid->MbToSliceGroupMap != NULL);
  264. return p_Vid->MbToSliceGroupMap[mb];
  265. }
  266. /*!
  267. ************************************************************************
  268. * \brief
  269. * FmoGetNextMBBr: Returns the MB-Nr (in scan order) of the next
  270. * MB in the (scattered) Slice, -1 if the slice is finished
  271. * \param p_Vid
  272. * image encoding parameters for current picture
  273. *
  274. * \param CurrentMbNr
  275. * number of the current macroblock
  276. ************************************************************************
  277. */
  278. int FmoGetNextMBNr (VideoParameters *p_Vid, int CurrentMbNr)
  279. {
  280. int SliceGroup = FmoGetSliceGroupId (p_Vid, CurrentMbNr);
  281. while (++CurrentMbNr<(int)p_Vid->PicSizeInMbs && p_Vid->MbToSliceGroupMap [CurrentMbNr] != SliceGroup)
  282. ;
  283. if (CurrentMbNr >= (int)p_Vid->PicSizeInMbs)
  284. return -1; // No further MB in this slice (could be end of picture)
  285. else
  286. return CurrentMbNr;
  287. }
  288. /*!
  289. ************************************************************************
  290. * \brief
  291. * Generate interleaved slice group map type MapUnit map (type 0)
  292. *
  293. ************************************************************************
  294. */
  295. static void FmoGenerateType0MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  296. {
  297. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  298. unsigned iGroup, j;
  299. unsigned i = 0;
  300. do
  301. {
  302. for( iGroup = 0;
  303. (iGroup <= pps->num_slice_groups_minus1) && (i < PicSizeInMapUnits);
  304. i += pps->run_length_minus1[iGroup++] + 1 )
  305. {
  306. for( j = 0; j <= pps->run_length_minus1[ iGroup ] && i + j < PicSizeInMapUnits; j++ )
  307. p_Vid->MapUnitToSliceGroupMap[i+j] = iGroup;
  308. }
  309. }
  310. while( i < PicSizeInMapUnits );
  311. }
  312. /*!
  313. ************************************************************************
  314. * \brief
  315. * Generate dispersed slice group map type MapUnit map (type 1)
  316. *
  317. ************************************************************************
  318. */
  319. static void FmoGenerateType1MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  320. {
  321. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  322. unsigned i;
  323. for( i = 0; i < PicSizeInMapUnits; i++ )
  324. {
  325. p_Vid->MapUnitToSliceGroupMap[i] = ((i%p_Vid->PicWidthInMbs)+(((i/p_Vid->PicWidthInMbs)*(pps->num_slice_groups_minus1+1))/2))
  326. %(pps->num_slice_groups_minus1+1);
  327. }
  328. }
  329. /*!
  330. ************************************************************************
  331. * \brief
  332. * Generate foreground with left-over slice group map type MapUnit map (type 2)
  333. *
  334. ************************************************************************
  335. */
  336. static void FmoGenerateType2MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  337. {
  338. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  339. int iGroup;
  340. unsigned i, x, y;
  341. unsigned yTopLeft, xTopLeft, yBottomRight, xBottomRight;
  342. for( i = 0; i < PicSizeInMapUnits; i++ )
  343. p_Vid->MapUnitToSliceGroupMap[ i ] = pps->num_slice_groups_minus1;
  344. for( iGroup = pps->num_slice_groups_minus1 - 1 ; iGroup >= 0; iGroup-- )
  345. {
  346. yTopLeft = pps->top_left[ iGroup ] / p_Vid->PicWidthInMbs;
  347. xTopLeft = pps->top_left[ iGroup ] % p_Vid->PicWidthInMbs;
  348. yBottomRight = pps->bottom_right[ iGroup ] / p_Vid->PicWidthInMbs;
  349. xBottomRight = pps->bottom_right[ iGroup ] % p_Vid->PicWidthInMbs;
  350. for( y = yTopLeft; y <= yBottomRight; y++ )
  351. for( x = xTopLeft; x <= xBottomRight; x++ )
  352. p_Vid->MapUnitToSliceGroupMap[ y * p_Vid->PicWidthInMbs + x ] = iGroup;
  353. }
  354. }
  355. /*!
  356. ************************************************************************
  357. * \brief
  358. * Generate box-out slice group map type MapUnit map (type 3)
  359. *
  360. ************************************************************************
  361. */
  362. static void FmoGenerateType3MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  363. {
  364. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  365. unsigned i, k;
  366. int leftBound, topBound, rightBound, bottomBound;
  367. int x, y, xDir, yDir;
  368. int mapUnitVacant;
  369. unsigned mapUnitsInSliceGroup0 = imin((pps->slice_group_change_rate_minus1 + 1) * p_Vid->slice_group_change_cycle, PicSizeInMapUnits);
  370. for( i = 0; i < PicSizeInMapUnits; i++ )
  371. p_Vid->MapUnitToSliceGroupMap[ i ] = 2;
  372. x = ( p_Vid->PicWidthInMbs - pps->slice_group_change_direction_flag ) / 2;
  373. y = ( p_Vid->PicHeightInMapUnits - pps->slice_group_change_direction_flag ) / 2;
  374. leftBound = x;
  375. topBound = y;
  376. rightBound = x;
  377. bottomBound = y;
  378. xDir = pps->slice_group_change_direction_flag - 1;
  379. yDir = pps->slice_group_change_direction_flag;
  380. for( k = 0; k < PicSizeInMapUnits; k += mapUnitVacant )
  381. {
  382. mapUnitVacant = ( p_Vid->MapUnitToSliceGroupMap[ y * p_Vid->PicWidthInMbs + x ] == 2 );
  383. if( mapUnitVacant )
  384. p_Vid->MapUnitToSliceGroupMap[ y * p_Vid->PicWidthInMbs + x ] = ( k >= mapUnitsInSliceGroup0 );
  385. if( xDir == -1 && x == leftBound )
  386. {
  387. leftBound = imax( leftBound - 1, 0 );
  388. x = leftBound;
  389. xDir = 0;
  390. yDir = 2 * pps->slice_group_change_direction_flag - 1;
  391. }
  392. else
  393. if( xDir == 1 && x == rightBound )
  394. {
  395. rightBound = imin( rightBound + 1, (int)p_Vid->PicWidthInMbs - 1 );
  396. x = rightBound;
  397. xDir = 0;
  398. yDir = 1 - 2 * pps->slice_group_change_direction_flag;
  399. }
  400. else
  401. if( yDir == -1 && y == topBound )
  402. {
  403. topBound = imax( topBound - 1, 0 );
  404. y = topBound;
  405. xDir = 1 - 2 * pps->slice_group_change_direction_flag;
  406. yDir = 0;
  407. }
  408. else
  409. if( yDir == 1 && y == bottomBound )
  410. {
  411. bottomBound = imin( bottomBound + 1, (int)p_Vid->PicHeightInMapUnits - 1 );
  412. y = bottomBound;
  413. xDir = 2 * pps->slice_group_change_direction_flag - 1;
  414. yDir = 0;
  415. }
  416. else
  417. {
  418. x = x + xDir;
  419. y = y + yDir;
  420. }
  421. }
  422. }
  423. /*!
  424. ************************************************************************
  425. * \brief
  426. * Generate raster scan slice group map type MapUnit map (type 4)
  427. *
  428. ************************************************************************
  429. */
  430. static void FmoGenerateType4MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  431. {
  432. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  433. unsigned mapUnitsInSliceGroup0 = imin((pps->slice_group_change_rate_minus1 + 1) * p_Vid->slice_group_change_cycle, PicSizeInMapUnits);
  434. unsigned sizeOfUpperLeftGroup = pps->slice_group_change_direction_flag ? ( PicSizeInMapUnits - mapUnitsInSliceGroup0 ) : mapUnitsInSliceGroup0;
  435. unsigned i;
  436. for( i = 0; i < PicSizeInMapUnits; i++ )
  437. if( i < sizeOfUpperLeftGroup )
  438. p_Vid->MapUnitToSliceGroupMap[ i ] = pps->slice_group_change_direction_flag;
  439. else
  440. p_Vid->MapUnitToSliceGroupMap[ i ] = 1 - pps->slice_group_change_direction_flag;
  441. }
  442. /*!
  443. ************************************************************************
  444. * \brief
  445. * Generate wipe slice group map type MapUnit map (type 5)
  446. *
  447. ************************************************************************
  448. */
  449. static void FmoGenerateType5MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  450. {
  451. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  452. unsigned mapUnitsInSliceGroup0 = imin((pps->slice_group_change_rate_minus1 + 1) * p_Vid->slice_group_change_cycle, PicSizeInMapUnits);
  453. unsigned sizeOfUpperLeftGroup = pps->slice_group_change_direction_flag ? ( PicSizeInMapUnits - mapUnitsInSliceGroup0 ) : mapUnitsInSliceGroup0;
  454. unsigned i,j, k = 0;
  455. for( j = 0; j < p_Vid->PicWidthInMbs; j++ )
  456. for( i = 0; i < p_Vid->PicHeightInMapUnits; i++ )
  457. if( k++ < sizeOfUpperLeftGroup )
  458. p_Vid->MapUnitToSliceGroupMap[ i * p_Vid->PicWidthInMbs + j ] = pps->slice_group_change_direction_flag;
  459. else
  460. p_Vid->MapUnitToSliceGroupMap[ i * p_Vid->PicWidthInMbs + j ] = 1 - pps->slice_group_change_direction_flag;
  461. }
  462. /*!
  463. ************************************************************************
  464. * \brief
  465. * Generate explicit slice group map type MapUnit map (type 6)
  466. *
  467. ************************************************************************
  468. */
  469. static void FmoGenerateType6MapUnitMap (VideoParameters *p_Vid, unsigned PicSizeInMapUnits )
  470. {
  471. pic_parameter_set_rbsp_t* pps = p_Vid->active_pps;
  472. unsigned i;
  473. for (i=0; i<PicSizeInMapUnits; i++)
  474. {
  475. p_Vid->MapUnitToSliceGroupMap[i] = pps->slice_group_id[i];
  476. }
  477. }