msm_vidc_buffer.c 12 KB

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