aom_external_partition.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
  3. *
  4. * This source code is subject to the terms of the BSD 2 Clause License and
  5. * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
  6. * was not distributed with this source code in the LICENSE file, you can
  7. * obtain it at www.aomedia.org/license/software. If the Alliance for Open
  8. * Media Patent License 1.0 was not distributed with this source code in the
  9. * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  10. */
  11. #ifndef AOM_AOM_AOM_EXTERNAL_PARTITION_H_
  12. #define AOM_AOM_AOM_EXTERNAL_PARTITION_H_
  13. /*!\defgroup aom_encoder AOMedia AOM/AV1 Encoder
  14. * \ingroup aom
  15. *
  16. * @{
  17. */
  18. #include <stdint.h>
  19. /*!\file
  20. * \brief Provides function pointer definitions for the external partition.
  21. *
  22. * \note The external partition API should be considered experimental. Until the
  23. * external partition API is declared stable, breaking changes may be made to
  24. * this API in a future libaom release.
  25. */
  26. /*!\brief Current ABI version number
  27. *
  28. * \internal
  29. * If this file is altered in any way that changes the ABI, this value
  30. * must be bumped. Examples include, but are not limited to, changing
  31. * types, removing or reassigning enums, adding/removing/rearranging
  32. * fields to structures.
  33. */
  34. #define AOM_EXT_PART_ABI_VERSION 8
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /*!\brief Abstract external partition model handler
  39. */
  40. typedef void *aom_ext_part_model_t;
  41. /*!\brief Number of features to determine whether to skip partition none and
  42. * do partition split directly. The same as "FEATURE_SIZE_SMS_SPLIT".
  43. */
  44. #define AOM_EXT_PART_SIZE_DIRECT_SPLIT 17
  45. /*!\brief Number of features to use simple motion search to prune out
  46. * rectangular partition in some direction. The same as
  47. * "FEATURE_SIZE_SMS_PRUNE_PART".
  48. */
  49. #define AOM_EXT_PART_SIZE_PRUNE_PART 25
  50. /*!\brief Number of features to prune split and rectangular partition
  51. * after PARTITION_NONE.
  52. */
  53. #define AOM_EXT_PART_SIZE_PRUNE_NONE 4
  54. /*!\brief Number of features to terminates partition after partition none using
  55. * simple_motion_search features and the rate, distortion, and rdcost of
  56. * PARTITION_NONE. The same as "FEATURE_SIZE_SMS_TERM_NONE".
  57. */
  58. #define AOM_EXT_PART_SIZE_TERM_NONE 28
  59. /*!\brief Number of features to terminates partition after partition split.
  60. */
  61. #define AOM_EXT_PART_SIZE_TERM_SPLIT 31
  62. /*!\brief Number of features to prune rectangular partition using stats
  63. * collected after partition split.
  64. */
  65. #define AOM_EXT_PART_SIZE_PRUNE_RECT 9
  66. /*!\brief Number of features to prune AB partition using stats
  67. * collected after rectangular partition..
  68. */
  69. #define AOM_EXT_PART_SIZE_PRUNE_AB 10
  70. /*!\brief Number of features to prune 4-way partition using stats
  71. * collected after AB partition.
  72. */
  73. #define AOM_EXT_PART_SIZE_PRUNE_4_WAY 18
  74. /*!\brief Decision mode of the external partition model.
  75. * AOM_EXT_PART_WHOLE_TREE: the external partition model should provide the
  76. * whole partition tree for the superblock.
  77. *
  78. * AOM_EXT_PART_RECURSIVE: the external partition model provides the partition
  79. * decision of the current block only. The decision process starts from
  80. * the superblock size, down to the smallest block size (4x4) recursively.
  81. */
  82. typedef enum aom_ext_part_decision_mode {
  83. AOM_EXT_PART_WHOLE_TREE = 0,
  84. AOM_EXT_PART_RECURSIVE = 1,
  85. } aom_ext_part_decision_mode_t;
  86. /*!\brief Config information sent to the external partition model.
  87. *
  88. * For example, the maximum superblock size determined by the sequence header.
  89. */
  90. typedef struct aom_ext_part_config {
  91. int superblock_size; ///< super block size (either 64x64 or 128x128)
  92. } aom_ext_part_config_t;
  93. /*!\brief Features pass to the external model to make partition decisions.
  94. * Specifically, features collected before NONE partition.
  95. * Features "f" are used to determine:
  96. * partition_none_allowed, partition_horz_allowed, partition_vert_allowed,
  97. * do_rectangular_split, do_square_split
  98. * Features "f_part2" are used to determine:
  99. * prune_horz, prune_vert.
  100. */
  101. typedef struct aom_partition_features_before_none {
  102. /*! features to determine whether skip partition none and do split directly */
  103. float f[AOM_EXT_PART_SIZE_DIRECT_SPLIT];
  104. /*! features to determine whether to prune rectangular partition */
  105. float f_part2[AOM_EXT_PART_SIZE_PRUNE_PART];
  106. } aom_partition_features_before_none_t;
  107. /*!\brief Features pass to the external model to make partition decisions.
  108. * Specifically, features collected after NONE partition.
  109. */
  110. typedef struct aom_partition_features_none {
  111. /*! features to prune split and rectangular partition */
  112. float f[AOM_EXT_PART_SIZE_PRUNE_NONE];
  113. /*! features to determine termination of partition */
  114. float f_terminate[AOM_EXT_PART_SIZE_TERM_NONE];
  115. } aom_partition_features_none_t;
  116. /*!\brief Features pass to the external model to make partition decisions.
  117. * Specifically, features collected after SPLIT partition.
  118. */
  119. typedef struct aom_partition_features_split {
  120. /*! features to determine termination of partition */
  121. float f_terminate[AOM_EXT_PART_SIZE_TERM_SPLIT];
  122. /*! features to determine pruning rect partition */
  123. float f_prune_rect[AOM_EXT_PART_SIZE_PRUNE_RECT];
  124. } aom_partition_features_split_t;
  125. /*!\brief Features pass to the external model to make partition decisions.
  126. * Specifically, features collected after RECTANGULAR partition.
  127. */
  128. typedef struct aom_partition_features_rect {
  129. /*! features to determine pruning AB partition */
  130. float f[AOM_EXT_PART_SIZE_PRUNE_AB];
  131. } aom_partition_features_rect_t;
  132. /*!\brief Features pass to the external model to make partition decisions.
  133. * Specifically, features collected after AB partition: HORZ_A, HORZ_B, VERT_A,
  134. * VERT_B.
  135. */
  136. typedef struct aom_partition_features_ab {
  137. /*! features to determine pruning 4-way partition */
  138. float f[AOM_EXT_PART_SIZE_PRUNE_4_WAY];
  139. } aom_partition_features_ab_t;
  140. /*!\brief Feature id to tell the external model the current stage in partition
  141. * pruning and what features to use to make decisions accordingly.
  142. */
  143. typedef enum {
  144. AOM_EXT_PART_FEATURE_BEFORE_NONE,
  145. AOM_EXT_PART_FEATURE_BEFORE_NONE_PART2,
  146. AOM_EXT_PART_FEATURE_AFTER_NONE,
  147. AOM_EXT_PART_FEATURE_AFTER_NONE_PART2,
  148. AOM_EXT_PART_FEATURE_AFTER_SPLIT,
  149. AOM_EXT_PART_FEATURE_AFTER_SPLIT_PART2,
  150. AOM_EXT_PART_FEATURE_AFTER_RECT,
  151. AOM_EXT_PART_FEATURE_AFTER_AB
  152. } AOM_EXT_PART_FEATURE_ID;
  153. /*!\brief Features collected from the tpl process.
  154. *
  155. * The tpl process collects information that help measure the inter-frame
  156. * dependency.
  157. * The tpl process is computed in the unit of tpl_bsize_1d (16x16).
  158. * Therefore, the max number of units inside a superblock is
  159. * 128x128 / (16x16) = 64. Change it if the tpl process changes.
  160. */
  161. typedef struct aom_sb_tpl_features {
  162. int available; ///< If tpl stats are available
  163. int tpl_unit_length; ///< The block length of tpl process
  164. int num_units; ///< The number of units inside the current superblock
  165. int64_t intra_cost[64]; ///< The intra cost of each unit
  166. int64_t inter_cost[64]; ///< The inter cost of each unit
  167. int64_t mc_dep_cost[64]; ///< The motion compensated dependency cost
  168. } aom_sb_tpl_features_t;
  169. /*!\brief Features collected from the simple motion process.
  170. *
  171. * The simple motion process collects information by applying motion compensated
  172. * prediction on each block.
  173. * The block size is 16x16, which could be changed. If it is changed, update
  174. * comments and the array size here.
  175. */
  176. typedef struct aom_sb_simple_motion_features {
  177. int unit_length; ///< The block length of the simple motion process
  178. int num_units; ///< The number of units inside the current superblock
  179. int block_sse[64]; ///< Sum of squared error of each unit
  180. int block_var[64]; ///< Variance of each unit
  181. } aom_sb_simple_motion_features_t;
  182. /*!\brief Features of each super block.
  183. *
  184. * Features collected for each super block before partition search.
  185. */
  186. typedef struct aom_sb_features {
  187. /*! Features from motion search */
  188. aom_sb_simple_motion_features_t motion_features;
  189. /*! Features from tpl process */
  190. aom_sb_tpl_features_t tpl_features;
  191. } aom_sb_features_t;
  192. /*!\brief Features pass to the external model to make partition decisions.
  193. *
  194. * The encoder sends these features to the external model through
  195. * "func()" defined in .....
  196. *
  197. * NOTE: new member variables may be added to this structure in the future.
  198. * Once new features are finalized, bump the major version of libaom.
  199. */
  200. typedef struct aom_partition_features {
  201. // Features for the current supervised multi-stage ML model.
  202. /*! Feature ID to indicate active features */
  203. AOM_EXT_PART_FEATURE_ID id;
  204. /*! Features collected before NONE partition */
  205. aom_partition_features_before_none_t before_part_none;
  206. /*! Features collected after NONE partition */
  207. aom_partition_features_none_t after_part_none;
  208. /*! Features collected after SPLIT partition */
  209. aom_partition_features_split_t after_part_split;
  210. /*! Features collected after RECTANGULAR partition */
  211. aom_partition_features_rect_t after_part_rect;
  212. /*! Features collected after AB partition */
  213. aom_partition_features_ab_t after_part_ab;
  214. // Features for a new ML model.
  215. aom_sb_features_t sb_features; ///< Features collected for the super block
  216. int mi_row; ///< Mi_row position of the block
  217. int mi_col; ///< Mi_col position of the block
  218. int frame_width; ///< Frame width
  219. int frame_height; ///< Frame height
  220. int block_size; ///< As "BLOCK_SIZE" in av1/common/enums.h
  221. /*!
  222. * Valid partition types. A bitmask is used. "1" represents the
  223. * corresponding type is valid. The bitmask follows the enum order for
  224. * PARTITION_TYPE in "enums.h" to represent one partition type at a bit.
  225. * For example, 0x01 stands for only PARTITION_NONE is valid,
  226. * 0x09 (00...001001) stands for PARTITION_NONE and PARTITION_SPLIT are valid.
  227. */
  228. int valid_partition_types;
  229. int update_type; ///< Frame update type, defined in ratectrl.h
  230. int qindex; ///< Quantization index, range: [0, 255]
  231. int rdmult; ///< Rate-distortion multiplier
  232. int pyramid_level; ///< The level of this frame in the hierarchical structure
  233. int has_above_block; ///< Has above neighbor block
  234. int above_block_width; ///< Width of the above block, -1 if not exist
  235. int above_block_height; ///< Height of the above block, -1 if not exist
  236. int has_left_block; ///< Has left neighbor block
  237. int left_block_width; ///< Width of the left block, -1 if not exist
  238. int left_block_height; ///< Height of the left block, -1 if not exist
  239. /*!
  240. * The following parameters are collected from applying simple motion search.
  241. * Sum of squared error (SSE) and variance of motion compensated residual
  242. * are good indicators of block partitioning.
  243. * If a block is a square, we also apply motion search for its 4 sub blocks.
  244. * If not a square, their values are -1.
  245. * If a block is able to split horizontally, we apply motion search and get
  246. * stats for horizontal blocks. If not, their values are -1.
  247. * If a block is able to split vertically, we apply motion search and get
  248. * stats for vertical blocks. If not, their values are -1.
  249. */
  250. unsigned int block_sse; ///< SSE of motion compensated residual
  251. unsigned int block_var; ///< Variance of motion compensated residual
  252. unsigned int sub_block_sse[4]; ///< SSE of sub blocks.
  253. unsigned int sub_block_var[4]; ///< Variance of sub blocks.
  254. unsigned int horz_block_sse[2]; ///< SSE of horz sub blocks
  255. unsigned int horz_block_var[2]; ///< Variance of horz sub blocks
  256. unsigned int vert_block_sse[2]; ///< SSE of vert sub blocks
  257. unsigned int vert_block_var[2]; ///< Variance of vert sub blocks
  258. /*!
  259. * The following parameters are calculated from tpl model.
  260. * If tpl model is not available, their values are -1.
  261. */
  262. int64_t tpl_intra_cost; ///< Intra cost, ref to "TplDepStats" in tpl_model.h
  263. int64_t tpl_inter_cost; ///< Inter cost in tpl model
  264. int64_t tpl_mc_dep_cost; ///< Motion compensated dependency cost in tpl model
  265. } aom_partition_features_t;
  266. /*!\brief Partition decisions received from the external model.
  267. *
  268. * The encoder receives partition decisions and encodes the superblock
  269. * with the given partition type.
  270. * The encoder receives it from "func()" define in ....
  271. *
  272. * NOTE: new member variables may be added to this structure in the future.
  273. * Once new features are finalized, bump the major version of libaom.
  274. */
  275. typedef struct aom_partition_decision {
  276. // Decisions for directly set partition types
  277. int is_final_decision; ///< The flag whether it's the final decision
  278. int num_nodes; ///< The number of leaf nodes
  279. int partition_decision[2048]; ///< Partition decisions
  280. int current_decision; ///< Partition decision for the current block
  281. // Decisions for partition type pruning
  282. int terminate_partition_search; ///< Terminate further partition search
  283. int partition_none_allowed; ///< Allow partition none type
  284. int partition_rect_allowed[2]; ///< Allow rectangular partitions
  285. int do_rectangular_split; ///< Try rectangular split partition
  286. int do_square_split; ///< Try square split partition
  287. int prune_rect_part[2]; ///< Prune rectangular partition
  288. int horza_partition_allowed; ///< Allow HORZ_A partition
  289. int horzb_partition_allowed; ///< Allow HORZ_B partition
  290. int verta_partition_allowed; ///< Allow VERT_A partition
  291. int vertb_partition_allowed; ///< Allow VERT_B partition
  292. int partition_horz4_allowed; ///< Allow HORZ4 partition
  293. int partition_vert4_allowed; ///< Allow VERT4 partition
  294. } aom_partition_decision_t;
  295. /*!\brief Encoding stats for the given partition decision.
  296. *
  297. * The encoding stats collected by encoding the superblock with the
  298. * given partition types.
  299. * The encoder sends the stats to the external model for training
  300. * or inference through "func()" defined in ....
  301. */
  302. typedef struct aom_partition_stats {
  303. int rate; ///< Rate cost of the block
  304. int64_t dist; ///< Distortion of the block
  305. int64_t rdcost; ///< Rate-distortion cost of the block
  306. } aom_partition_stats_t;
  307. /*!\brief Enum for return status.
  308. */
  309. typedef enum aom_ext_part_status {
  310. AOM_EXT_PART_OK = 0, ///< Status of success
  311. AOM_EXT_PART_ERROR = 1, ///< Status of failure
  312. AOM_EXT_PART_TEST = 2, ///< Status used for tests
  313. } aom_ext_part_status_t;
  314. /*!\brief Callback of creating an external partition model.
  315. *
  316. * The callback is invoked by the encoder to create an external partition
  317. * model.
  318. *
  319. * \param[in] priv Callback's private data
  320. * \param[in] part_config Config information pointer for model creation
  321. * \param[out] ext_part_model Pointer to the model
  322. */
  323. typedef aom_ext_part_status_t (*aom_ext_part_create_model_fn_t)(
  324. void *priv, const aom_ext_part_config_t *part_config,
  325. aom_ext_part_model_t *ext_part_model);
  326. /*!\brief Callback of sending features to the external partition model.
  327. *
  328. * The callback is invoked by the encoder to send features to the external
  329. * partition model.
  330. *
  331. * \param[in] ext_part_model The external model
  332. * \param[in] part_features Pointer to the features
  333. */
  334. typedef aom_ext_part_status_t (*aom_ext_part_send_features_fn_t)(
  335. aom_ext_part_model_t ext_part_model,
  336. const aom_partition_features_t *part_features);
  337. /*!\brief Callback of receiving partition decisions from the external
  338. * partition model.
  339. *
  340. * The callback is invoked by the encoder to receive partition decisions from
  341. * the external partition model.
  342. *
  343. * \param[in] ext_part_model The external model
  344. * \param[in] ext_part_decision Pointer to the partition decisions
  345. */
  346. typedef aom_ext_part_status_t (*aom_ext_part_get_decision_fn_t)(
  347. aom_ext_part_model_t ext_part_model,
  348. aom_partition_decision_t *ext_part_decision);
  349. /*!\brief Callback of sending stats to the external partition model.
  350. *
  351. * The callback is invoked by the encoder to send encoding stats to
  352. * the external partition model.
  353. *
  354. * \param[in] ext_part_model The external model
  355. * \param[in] ext_part_stats Pointer to the encoding stats
  356. */
  357. typedef aom_ext_part_status_t (*aom_ext_part_send_partition_stats_fn_t)(
  358. aom_ext_part_model_t ext_part_model,
  359. const aom_partition_stats_t *ext_part_stats);
  360. /*!\brief Callback of deleting the external partition model.
  361. *
  362. * The callback is invoked by the encoder to delete the external partition
  363. * model.
  364. *
  365. * \param[in] ext_part_model The external model
  366. */
  367. typedef aom_ext_part_status_t (*aom_ext_part_delete_model_fn_t)(
  368. aom_ext_part_model_t ext_part_model);
  369. /*!\brief Callback function set for external partition model.
  370. *
  371. * Uses can enable external partition model by registering a set of
  372. * callback functions with the flag: AV1E_SET_EXTERNAL_PARTITION_MODEL
  373. */
  374. typedef struct aom_ext_part_funcs {
  375. /*!
  376. * Create an external partition model.
  377. */
  378. aom_ext_part_create_model_fn_t create_model;
  379. /*!
  380. * Send features to the external partition model to make partition decisions.
  381. */
  382. aom_ext_part_send_features_fn_t send_features;
  383. /*!
  384. * Get partition decisions from the external partition model.
  385. */
  386. aom_ext_part_get_decision_fn_t get_partition_decision;
  387. /*!
  388. * Send stats of the current partition to the external model.
  389. */
  390. aom_ext_part_send_partition_stats_fn_t send_partition_stats;
  391. /*!
  392. * Delete the external partition model.
  393. */
  394. aom_ext_part_delete_model_fn_t delete_model;
  395. /*!
  396. * The decision mode of the model.
  397. */
  398. aom_ext_part_decision_mode_t decision_mode;
  399. /*!
  400. * Private data for the external partition model.
  401. */
  402. void *priv;
  403. } aom_ext_part_funcs_t;
  404. /*!@} - end defgroup aom_encoder*/
  405. #ifdef __cplusplus
  406. } // extern "C"
  407. #endif
  408. #endif // AOM_AOM_AOM_EXTERNAL_PARTITION_H_