rk_venc_ref.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 2015 Rockchip Electronics Co. LTD
  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. #ifndef __RK_VENC_REF_H__
  17. #define __RK_VENC_REF_H__
  18. #include "rk_type.h"
  19. #include "mpp_err.h"
  20. /*
  21. * MPP reference management system follows the model of H.264/H.265 reference
  22. * frame mangement.
  23. *
  24. * The reference frame is defined into two type: long-term reference frame and
  25. * short-refernce frame (lt_ref and st_ref).
  26. *
  27. * The lt_ref can be only indexed by long-term reference frame index (lt_idx).
  28. * The st_ref can be indexed by its temporal id (tid) and previous count.
  29. *
  30. * MppEncRefMode defined the way for user to reference the required frame.
  31. *
  32. * Normal reference mode without argument
  33. * REF_TO_PREV_REF_FRM - refer to previous reference frame in encode order (No matter Lt or St)
  34. * REF_TO_PREV_ST_REF - refer to previous short-term reference frame
  35. * REF_TO_PREV_LT_REF - refer to previous long-term reference frame
  36. * REF_TO_PREV_INTRA - refer to previous Intra / IDR frame
  37. * REF_TO_ST_REF_SETUP - refer to refernce frame defined in StRefSetup
  38. *
  39. * Normal reference mode with argument
  40. * REF_TO_TEMPORAL_LAYER - refer to previous reference frame with temporal id argument
  41. * REF_TO_LT_REF_IDX - refer to long-term reference frame with lt_ref_idx argument
  42. * REF_TO_ST_PREV_N_REF - refer to short-term reference frame with diff frame_num argument
  43. *
  44. * Long-term reference only mode
  45. * REF_TO_ST_REF_SETUP - use corresponding mode of original short-term reference frame
  46. *
  47. * Short-term reference only mode
  48. * REF_TO_LT_REF_SETUP - indicate that this frame will be overwrited by long-term config
  49. *
  50. * By combining frames with these modes user can define many kinds of reference hierarchy
  51. * structure. But normally user should use simplified preset hierarchy pattern.
  52. *
  53. * The rules for virtual cpb management is similiar to H.264/H.265
  54. * 1. When one frame is marked as long-term reference frame it will be kept in cpb until
  55. * it is replaced by other frame with the same lt_idx or IDR frame.
  56. * 2. When one frame is marked as short-term reference frame it will be inert into cpb when
  57. * there is enough storage space. When the number of total sum of long-term and short-term
  58. * reference frame excess the cpb size limit the oldest short-term frame will be removed.
  59. * This is call sliding window in H.264.
  60. */
  61. /* max 4 temporal layer */
  62. #define MPP_ENC_MAX_TEMPORAL_LAYER_NUM 4
  63. /* max 4 long-term reference frame */
  64. #define MPP_ENC_MAX_LT_REF_NUM 16
  65. /*
  66. * Group Of Picture (GOP) config is separated into three parts:
  67. *
  68. * 1. Intra / IDR frame config
  69. * igop - the interval of two intra / IDR frames
  70. *
  71. * 2. Long-term reference config (MppEncRefLtFrmCfg)
  72. *
  73. * Setup long-term reference index max lt_idx, loop interval and reference
  74. * mode for auto long-term reference frame generation. The encoder will
  75. * mark frame to be long-term reference frame with given interval.
  76. *
  77. * 2.1 lt_idx
  78. * The long-term reference frame index is unique identifier for a long-term
  79. * reference frame.
  80. * The max long-term reference frame index should NOT larger than
  81. * max_num_ref_frames in sps.
  82. *
  83. * 2.2 lt_gap
  84. * When lt_gap is zero the long-term reference frame generation is disabled.
  85. * When lt_gap is non-zero (usually 2~3 second interval) then the long-term
  86. * reference frame will be generated for error recovery or smart hierarchy.
  87. *
  88. * 2.2 lt_delay
  89. * The lt_delay is the delay time for generation of long-term reference frame.
  90. * The start point of lt_delay is the IDR/intra frame genertaed by igop.
  91. *
  92. * 2.4 ref_mode: Long-term refernce frame reference mode
  93. * NOTE: temporal id of longterm reference frame is always zero.
  94. *
  95. * Examples:
  96. * Sequence has only one lt_ref 0 and setup one long-term reference frame
  97. * every 300 frame.
  98. * {
  99. * .lt_idx = 0,
  100. * .lt_gap = 300,
  101. * .lt_delay = 0,
  102. * }
  103. * result:
  104. * frame 0 ...... 299 300 301 ...... 599 600 601
  105. * lt_idx 0 xxxxxx x 0 x xxxxxx x 0 x
  106. *
  107. * Sequence has lt_ref from 0 to 2 and setup a long-term reference frame
  108. * every 100 frame.
  109. * {
  110. * .lt_idx = 0,
  111. * .lt_gap = 300,
  112. * .lt_delay = 0,
  113. * }
  114. * {
  115. * .lt_idx = 1,
  116. * .lt_gap = 300,
  117. * .lt_delay = 100,
  118. * }
  119. * {
  120. * .lt_idx = 2,
  121. * .lt_gap = 300,
  122. * .lt_delay = 200,
  123. * }
  124. * result:
  125. * frame 0 ... 99 100 101 ... 199 200 201 ... 299 300 301
  126. * lt_idx 0 xxx x 1 x xxx x 2 x xxx x 0 x
  127. *
  128. * 3. Short-term reference config (MppEncStRefSetup)
  129. *
  130. * 3.1 is_non_ref
  131. * The is_non_ref indicated the current frame is reference frame or not.
  132. *
  133. * 3.2 temporal_id
  134. * The temporal id of the current frame configure.
  135. *
  136. * 3.3 ref_mode: short-term refernce frame reference mode
  137. *
  138. * 3.4 repeat
  139. * The repeat time of the short-term reference frame configure.
  140. * The overall frame count with the same config is repeat + 1.
  141. *
  142. * Examples:
  143. *
  144. */
  145. #define REF_MODE_MODE_MASK (0x1F)
  146. #define REF_MODE_ARG_MASK (0xFFFF0000)
  147. typedef enum MppEncRefMode_e {
  148. /* max 32 mode in 32-bit */
  149. /* for default ref global config */
  150. REF_MODE_GLOBAL,
  151. REF_TO_PREV_REF_FRM = REF_MODE_GLOBAL,
  152. REF_TO_PREV_ST_REF,
  153. REF_TO_PREV_LT_REF,
  154. REF_TO_PREV_INTRA,
  155. /* for global config with args */
  156. REF_MODE_GLOBAL_WITH_ARG = 0x4,
  157. /* with ref arg as temporal layer id */
  158. REF_TO_TEMPORAL_LAYER = REF_MODE_GLOBAL_WITH_ARG,
  159. /* with ref arg as long-term reference picture index */
  160. REF_TO_LT_REF_IDX,
  161. /* with ref arg as short-term reference picture difference frame_num */
  162. REF_TO_ST_PREV_N_REF,
  163. REF_MODE_GLOBAL_BUTT,
  164. /* for lt-ref */
  165. REF_MODE_LT = 0x18,
  166. REF_TO_ST_REF_SETUP,
  167. REF_MODE_LT_BUTT,
  168. /* for st-ref */
  169. REF_MODE_ST = 0x1C,
  170. REF_TO_LT_REF_SETUP,
  171. REF_MODE_ST_BUTT,
  172. } MppEncRefMode;
  173. typedef struct MppEncRefLtFrmCfg_t {
  174. RK_S32 lt_idx; /* lt_idx of the reference frame */
  175. RK_S32 temporal_id; /* temporal_id of the reference frame */
  176. MppEncRefMode ref_mode;
  177. RK_S32 ref_arg;
  178. RK_S32 lt_gap; /* gap between two lt-ref with same lt_idx */
  179. RK_S32 lt_delay; /* delay offset to igop start frame */
  180. } MppEncRefLtFrmCfg;
  181. typedef struct MppEncRefStFrmCfg_t {
  182. RK_S32 is_non_ref;
  183. RK_S32 temporal_id;
  184. MppEncRefMode ref_mode;
  185. RK_S32 ref_arg;
  186. RK_S32 repeat; /* repeat times */
  187. } MppEncRefStFrmCfg;
  188. typedef struct MppEncRefPreset_t {
  189. /* input parameter for query */
  190. const char *name;
  191. RK_S32 max_lt_cnt;
  192. RK_S32 max_st_cnt;
  193. MppEncRefLtFrmCfg *lt_cfg;
  194. MppEncRefStFrmCfg *st_cfg;
  195. /* output parameter */
  196. RK_S32 lt_cnt;
  197. RK_S32 st_cnt;
  198. } MppEncRefPreset;
  199. typedef void* MppEncRefCfg;
  200. /* for kmpp enc ref cfg */
  201. typedef enum MppEncRefCfgMode_e {
  202. REF_IPPP,
  203. REF_TSVC1,
  204. REF_TSVC2,
  205. REF_TSVC3,
  206. REF_VI,
  207. REF_HIR_SKIP,
  208. REF_BUTT,
  209. } MppEncRefCfgMode;
  210. typedef struct MppEncRefParam_t {
  211. MppEncRefCfgMode cfg_mode;
  212. RK_S32 gop_len;
  213. /*used for smartp ref*/
  214. RK_S32 vi_len;
  215. /*used for skip reg*/
  216. RK_U32 base_N;
  217. RK_U32 enh_M;
  218. RK_U32 pre_en;
  219. } MppEncRefParam;
  220. #ifdef __cplusplus
  221. extern "C" {
  222. #endif
  223. MPP_RET mpp_enc_ref_cfg_init(MppEncRefCfg *ref);
  224. MPP_RET mpp_enc_ref_cfg_deinit(MppEncRefCfg *ref);
  225. MPP_RET mpp_enc_ref_cfg_reset(MppEncRefCfg ref);
  226. MPP_RET mpp_enc_ref_cfg_set_cfg_cnt(MppEncRefCfg ref, RK_S32 lt_cnt, RK_S32 st_cnt);
  227. MPP_RET mpp_enc_ref_cfg_add_lt_cfg(MppEncRefCfg ref, RK_S32 cnt, MppEncRefLtFrmCfg *frm);
  228. MPP_RET mpp_enc_ref_cfg_add_st_cfg(MppEncRefCfg ref, RK_S32 cnt, MppEncRefStFrmCfg *frm);
  229. MPP_RET mpp_enc_ref_cfg_check(MppEncRefCfg ref);
  230. /*
  231. * A new reference configure will restart a new gop and clear cpb by default.
  232. * The keep cpb function will let encoder keeps the current cpb status and do NOT
  233. * reset all the reference frame in cpb.
  234. */
  235. MPP_RET mpp_enc_ref_cfg_set_keep_cpb(MppEncRefCfg ref, RK_S32 keep);
  236. MPP_RET mpp_enc_ref_cfg_get_preset(MppEncRefPreset *preset);
  237. MPP_RET mpp_enc_ref_cfg_show(MppEncRefCfg ref);
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif /*__RK_VENC_REF_H__*/