msm_vidc_buffer.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include "msm_media_info.h"
  6. #include "msm_vidc_buffer.h"
  7. #include "msm_vidc_inst.h"
  8. #include "msm_vidc_core.h"
  9. #include "msm_vidc_driver.h"
  10. #include "msm_vidc_debug.h"
  11. #include "msm_vidc_internal.h"
  12. /* Generic function for all targets. Not being used for iris2 */
  13. u32 msm_vidc_input_min_count(struct msm_vidc_inst* inst)
  14. {
  15. u32 input_min_count = 0;
  16. u32 hb_enh_layer = 0;
  17. if (!inst || !inst->capabilities) {
  18. d_vpr_e("%s: invalid params\n", __func__);
  19. return 0;
  20. }
  21. if (is_decode_session(inst)) {
  22. input_min_count = MIN_DEC_INPUT_BUFFERS;
  23. } else if (is_encode_session(inst)) {
  24. input_min_count = MIN_ENC_INPUT_BUFFERS;
  25. if (is_hierb_type_requested(inst)) {
  26. hb_enh_layer =
  27. inst->capabilities->cap[ENH_LAYER_COUNT].value;
  28. if (inst->codec == MSM_VIDC_H264 &&
  29. !inst->capabilities->cap[LAYER_ENABLE].value) {
  30. hb_enh_layer = 0;
  31. }
  32. if (hb_enh_layer)
  33. input_min_count = (1 << hb_enh_layer) + 2;
  34. }
  35. } else {
  36. i_vpr_e(inst, "%s: invalid domain\n",
  37. __func__, inst->domain);
  38. return 0;
  39. }
  40. if (is_thumbnail_session(inst) || is_image_session(inst))
  41. input_min_count = 1;
  42. return input_min_count;
  43. }
  44. u32 msm_vidc_output_min_count(struct msm_vidc_inst *inst)
  45. {
  46. u32 output_min_count;
  47. if (!inst) {
  48. d_vpr_e("%s: invalid params\n", __func__);
  49. return 0;
  50. }
  51. if (!is_decode_session(inst) && !is_encode_session(inst))
  52. return 0;
  53. if (is_thumbnail_session(inst))
  54. return 1;
  55. if (is_decode_session(inst)) {
  56. switch (inst->codec) {
  57. case MSM_VIDC_H264:
  58. case MSM_VIDC_HEVC:
  59. output_min_count = 4;
  60. break;
  61. case MSM_VIDC_VP9:
  62. output_min_count = 9;
  63. break;
  64. case MSM_VIDC_HEIC:
  65. output_min_count = 3;
  66. break;
  67. default:
  68. output_min_count = 4;
  69. }
  70. } else {
  71. output_min_count = MIN_ENC_OUTPUT_BUFFERS;
  72. //todo: reduce heic count to 2, once HAL side cushion is added
  73. }
  74. return output_min_count;
  75. }
  76. u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst)
  77. {
  78. u32 count = 0;
  79. struct msm_vidc_core *core;
  80. if (!inst || !inst->core) {
  81. d_vpr_e("%s: invalid params %pK\n", __func__, inst);
  82. return 0;
  83. }
  84. core = inst->core;
  85. /*
  86. * no extra buffers for thumbnail session because
  87. * neither dcvs nor batching will be enabled
  88. */
  89. if (is_thumbnail_session(inst) || is_image_session(inst))
  90. return 0;
  91. if (is_decode_session(inst)) {
  92. /*
  93. * if decode batching enabled, ensure minimum batch size
  94. * count of input buffers present on input port
  95. */
  96. if (core->capabilities[DECODE_BATCH].value &&
  97. inst->decode_batch.enable) {
  98. if (inst->buffers.input.min_count < inst->decode_batch.size) {
  99. count = inst->decode_batch.size -
  100. inst->buffers.input.min_count;
  101. }
  102. }
  103. } else if (is_encode_session(inst)) {
  104. /* add dcvs buffers, if platform supports dcvs */
  105. if (core->capabilities[DCVS].value)
  106. count = DCVS_ENC_EXTRA_INPUT_BUFFERS;
  107. }
  108. return count;
  109. }
  110. u32 msm_vidc_output_extra_count(struct msm_vidc_inst *inst)
  111. {
  112. u32 count = 0;
  113. struct msm_vidc_core *core;
  114. if (!inst || !inst->core) {
  115. d_vpr_e("%s: invalid params %pK\n", __func__, inst);
  116. return 0;
  117. }
  118. core = inst->core;
  119. /*
  120. * no extra buffers for thumbnail session because
  121. * neither dcvs nor batching will be enabled
  122. */
  123. if (is_thumbnail_session(inst) || is_image_session(inst))
  124. return 0;
  125. if (is_decode_session(inst)) {
  126. /* add dcvs buffers, if platform supports dcvs */
  127. if (core->capabilities[DCVS].value)
  128. count = DCVS_DEC_EXTRA_OUTPUT_BUFFERS;
  129. /*
  130. * if decode batching enabled, ensure minimum batch size
  131. * count of extra output buffers added on output port
  132. */
  133. if (core->capabilities[DECODE_BATCH].value &&
  134. inst->decode_batch.enable &&
  135. count < inst->decode_batch.size)
  136. count = inst->decode_batch.size;
  137. }
  138. return count;
  139. }
  140. u32 msm_vidc_internal_buffer_count(struct msm_vidc_inst *inst,
  141. enum msm_vidc_buffer_type buffer_type)
  142. {
  143. u32 count = 0;
  144. if (!inst) {
  145. d_vpr_e("%s: invalid params\n", __func__);
  146. return 0;
  147. }
  148. if (is_encode_session(inst))
  149. return 1;
  150. if (is_decode_session(inst)) {
  151. if (buffer_type == MSM_VIDC_BUF_BIN ||
  152. buffer_type == MSM_VIDC_BUF_LINE ||
  153. buffer_type == MSM_VIDC_BUF_PERSIST) {
  154. count = 1;
  155. } else if (buffer_type == MSM_VIDC_BUF_COMV ||
  156. buffer_type == MSM_VIDC_BUF_NON_COMV) {
  157. if (inst->codec == MSM_VIDC_H264 ||
  158. inst->codec == MSM_VIDC_HEVC ||
  159. inst->codec == MSM_VIDC_HEIC)
  160. count = 1;
  161. else
  162. count = 0;
  163. } else {
  164. i_vpr_e(inst, "%s: unsupported buffer type %s\n",
  165. __func__, buf_name(buffer_type));
  166. count = 0;
  167. }
  168. }
  169. return count;
  170. }
  171. u32 msm_vidc_decoder_input_size(struct msm_vidc_inst *inst)
  172. {
  173. u32 frame_size, num_mbs;
  174. u32 div_factor = 1;
  175. u32 base_res_mbs = NUM_MBS_4k;
  176. struct v4l2_format *f;
  177. u32 bitstream_size_overwrite = 0;
  178. if (!inst || !inst->capabilities) {
  179. d_vpr_e("%s: invalid params\n");
  180. return 0;
  181. }
  182. bitstream_size_overwrite =
  183. inst->capabilities->cap[BITSTREAM_SIZE_OVERWRITE].value;
  184. if (bitstream_size_overwrite) {
  185. frame_size = bitstream_size_overwrite;
  186. i_vpr_h(inst, "client configured bitstream buffer size %d\n",
  187. frame_size);
  188. return frame_size;
  189. }
  190. /*
  191. * Decoder input size calculation:
  192. * For 8k resolution, buffer size is calculated as 8k mbs / 4 and
  193. * for 8k cases we expect width/height to be set always.
  194. * In all other cases, buffer size is calculated as
  195. * 4k mbs for VP8/VP9 and 4k / 2 for remaining codecs.
  196. */
  197. f = &inst->fmts[INPUT_PORT];
  198. num_mbs = msm_vidc_get_mbs_per_frame(inst);
  199. if (num_mbs > NUM_MBS_4k) {
  200. div_factor = 4;
  201. base_res_mbs = inst->capabilities->cap[MBPF].value;
  202. } else {
  203. base_res_mbs = NUM_MBS_4k;
  204. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9)
  205. div_factor = 1;
  206. else
  207. div_factor = 2;
  208. }
  209. if (is_secure_session(inst))
  210. div_factor = div_factor << 1;
  211. /* For image session, use the actual resolution to calc buffer size */
  212. if (is_image_session(inst)) {
  213. base_res_mbs = num_mbs;
  214. div_factor = 1;
  215. }
  216. frame_size = base_res_mbs * MB_SIZE_IN_PIXEL * 3 / 2 / div_factor;
  217. /* multiply by 10/8 (1.25) to get size for 10 bit case */
  218. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9 ||
  219. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC ||
  220. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEIC)
  221. frame_size = frame_size + (frame_size >> 2);
  222. i_vpr_h(inst, "set input buffer size to %d\n", frame_size);
  223. return ALIGN(frame_size, SZ_4K);
  224. }
  225. u32 msm_vidc_decoder_output_size(struct msm_vidc_inst *inst)
  226. {
  227. u32 size;
  228. struct v4l2_format *f;
  229. f = &inst->fmts[OUTPUT_PORT];
  230. size = VIDEO_RAW_BUFFER_SIZE(f->fmt.pix_mp.pixelformat,
  231. f->fmt.pix_mp.width,
  232. f->fmt.pix_mp.height, true);
  233. return size;
  234. }
  235. u32 msm_vidc_decoder_input_meta_size(struct msm_vidc_inst *inst)
  236. {
  237. return ALIGN(16 * 1024, SZ_4K);
  238. }
  239. u32 msm_vidc_decoder_output_meta_size(struct msm_vidc_inst *inst)
  240. {
  241. return ALIGN(16 * 1024, SZ_4K);
  242. }
  243. u32 msm_vidc_encoder_input_size(struct msm_vidc_inst *inst)
  244. {
  245. u32 size;
  246. struct v4l2_format *f;
  247. f = &inst->fmts[INPUT_PORT];
  248. size = VIDEO_RAW_BUFFER_SIZE(f->fmt.pix_mp.pixelformat,
  249. f->fmt.pix_mp.width,
  250. f->fmt.pix_mp.height, true);
  251. return size;
  252. }
  253. u32 msm_vidc_encoder_output_size(struct msm_vidc_inst *inst)
  254. {
  255. u32 frame_size;
  256. u32 mbs_per_frame;
  257. u32 width, height;
  258. struct v4l2_format *f;
  259. f = &inst->fmts[OUTPUT_PORT];
  260. /*
  261. * Encoder output size calculation: 32 Align width/height
  262. * For resolution < 720p : YUVsize * 4
  263. * For resolution > 720p & <= 4K : YUVsize / 2
  264. * For resolution > 4k : YUVsize / 4
  265. * Initially frame_size = YUVsize * 2;
  266. */
  267. width = ALIGN(f->fmt.pix_mp.width, BUFFER_ALIGNMENT_SIZE(32));
  268. height = ALIGN(f->fmt.pix_mp.height, BUFFER_ALIGNMENT_SIZE(32));
  269. mbs_per_frame = NUM_MBS_PER_FRAME(width, height);
  270. frame_size = (width * height * 3);
  271. /* Image session: 2 x yuv size */
  272. if (is_image_session(inst))
  273. goto skip_calc;
  274. if (mbs_per_frame < NUM_MBS_720P)
  275. frame_size = frame_size << 1;
  276. else if (mbs_per_frame <= NUM_MBS_4k)
  277. frame_size = frame_size >> 2;
  278. else
  279. frame_size = frame_size >> 3;
  280. /*if ((inst->rc_type == RATE_CONTROL_OFF) ||
  281. (inst->rc_type == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ))
  282. frame_size = frame_size << 1;
  283. if (inst->rc_type == RATE_CONTROL_LOSSLESS)
  284. frame_size = (width * height * 9) >> 2; */
  285. skip_calc:
  286. /* multiply by 10/8 (1.25) to get size for 10 bit case */
  287. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC ||
  288. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEIC)
  289. frame_size = frame_size + (frame_size >> 2);
  290. return ALIGN(frame_size, SZ_4K);
  291. }
  292. static inline u32 ROI_METADATA_SIZE(
  293. u32 width, u32 height, u32 lcu_size) {
  294. u32 lcu_width = 0;
  295. u32 lcu_height = 0;
  296. u32 n_shift = 0;
  297. while (lcu_size && !(lcu_size & 0x1)) {
  298. n_shift++;
  299. lcu_size = lcu_size >> 1;
  300. }
  301. lcu_width = (width + (lcu_size - 1)) >> n_shift;
  302. lcu_height = (height + (lcu_size - 1)) >> n_shift;
  303. return (((lcu_width + 7) >> 3) << 3) * lcu_height * 2;
  304. }
  305. u32 msm_vidc_encoder_input_meta_size(struct msm_vidc_inst *inst)
  306. {
  307. u32 size = 0;
  308. u32 lcu_size = 0;
  309. struct v4l2_format *f;
  310. if (!inst || !inst->capabilities) {
  311. d_vpr_e("%s: invalid params\n", __func__);
  312. return 0;
  313. }
  314. size = ALIGN(16 * 1024, SZ_4K);
  315. if (inst->capabilities->cap[META_ROI_INFO].value) {
  316. lcu_size = 16;
  317. f = &inst->fmts[OUTPUT_PORT];
  318. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC)
  319. lcu_size = 32;
  320. f = &inst->fmts[INPUT_PORT];
  321. size += ROI_METADATA_SIZE(f->fmt.pix_mp.width,
  322. f->fmt.pix_mp.height, lcu_size);
  323. size = ALIGN(size, SZ_4K);
  324. }
  325. return size;
  326. }
  327. u32 msm_vidc_encoder_output_meta_size(struct msm_vidc_inst *inst)
  328. {
  329. return ALIGN(16 * 1024, SZ_4K);
  330. }