msm_vidc_buffer.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. u32 msm_vidc_input_min_count(struct msm_vidc_inst* inst)
  13. {
  14. u32 input_min_count = 0;
  15. //struct v4l2_ctrl *max_layer = NULL;
  16. if (!inst) {
  17. d_vpr_e("%s: invalid params\n", __func__);
  18. return 0;
  19. }
  20. if (is_decode_session(inst)) {
  21. input_min_count = MIN_DEC_INPUT_BUFFERS;
  22. } else if (is_encode_session(inst)) {
  23. input_min_count = MIN_ENC_INPUT_BUFFERS;
  24. } else {
  25. i_vpr_e(inst, "%s: invalid domain\n",
  26. __func__, inst->domain);
  27. return 0;
  28. }
  29. if (is_thumbnail_session(inst) || is_image_session(inst))
  30. input_min_count = 1;
  31. //if (is_grid_session(inst))
  32. // input_min_count = 2;
  33. //if (is_hier_b_session(inst)) {
  34. //max_layer = get_ctrl(inst,
  35. // V4L2_CID_MPEG_VIDC_VIDEO_HEVC_MAX_HIER_CODING_LAYER);
  36. //input_min_count = (1 << (max_layer->val - 1)) + 2;
  37. //}
  38. return input_min_count;
  39. }
  40. u32 msm_vidc_output_min_count(struct msm_vidc_inst *inst)
  41. {
  42. u32 output_min_count;
  43. if (!inst) {
  44. d_vpr_e("%s: invalid params\n", __func__);
  45. return 0;
  46. }
  47. if (!is_decode_session(inst) && !is_encode_session(inst))
  48. return 0;
  49. if (is_thumbnail_session(inst) || is_image_session(inst))
  50. return 1;
  51. if (is_decode_session(inst)) {
  52. switch (inst->codec) {
  53. case MSM_VIDC_H264:
  54. case MSM_VIDC_HEVC:
  55. output_min_count = 4;
  56. break;
  57. case MSM_VIDC_VP9:
  58. output_min_count = 9;
  59. break;
  60. case MSM_VIDC_HEIC:
  61. output_min_count = 1;
  62. break;
  63. default:
  64. output_min_count = 4;
  65. }
  66. } else {
  67. output_min_count = MIN_ENC_OUTPUT_BUFFERS;
  68. }
  69. //if (is_vpp_delay_allowed(inst)) {
  70. // output_min_count =
  71. // max(output_min_count, (u32)MAX_BSE_VPP_DELAY);
  72. // output_min_count =
  73. // max(output_min_count, (u32)(msm_vidc_vpp_delay & 0x1F));
  74. //}
  75. return output_min_count;
  76. }
  77. u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst)
  78. {
  79. u32 count = 0;
  80. struct msm_vidc_core *core;
  81. if (!inst || !inst->core) {
  82. d_vpr_e("%s: invalid params %pK\n", __func__, inst);
  83. return 0;
  84. }
  85. core = inst->core;
  86. /*
  87. * no extra buffers for thumbnail session because
  88. * neither dcvs nor batching will be enabled
  89. */
  90. if (is_thumbnail_session(inst) || is_image_session(inst))
  91. return 0;
  92. if (is_decode_session(inst)) {
  93. /*
  94. * if decode batching enabled, ensure minimum batch size
  95. * count of input buffers present on input port
  96. */
  97. if (core->capabilities[DECODE_BATCH].value &&
  98. inst->decode_batch.enable) {
  99. if (inst->buffers.input.min_count < inst->decode_batch.size) {
  100. count = inst->decode_batch.size -
  101. inst->buffers.input.min_count;
  102. }
  103. }
  104. } else if (is_encode_session(inst)) {
  105. /* add dcvs buffers */
  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 */
  127. count = DCVS_DEC_EXTRA_OUTPUT_BUFFERS;
  128. /*
  129. * if decode batching enabled, ensure minimum batch size
  130. * count of extra output buffers added on output port
  131. */
  132. if (core->capabilities[DECODE_BATCH].value &&
  133. inst->decode_batch.enable &&
  134. count < inst->decode_batch.size)
  135. count = inst->decode_batch.size;
  136. } else if (is_encode_session(inst)) {
  137. /* add heif buffers */
  138. //count = 8
  139. }
  140. return count;
  141. }
  142. u32 msm_vidc_internal_buffer_count(struct msm_vidc_inst *inst,
  143. enum msm_vidc_buffer_type buffer_type)
  144. {
  145. u32 count = 0;
  146. if (!inst) {
  147. d_vpr_e("%s: invalid params\n", __func__);
  148. return 0;
  149. }
  150. if (is_encode_session(inst))
  151. return 1;
  152. if (is_decode_session(inst)) {
  153. if (buffer_type == MSM_VIDC_BUF_BIN ||
  154. buffer_type == MSM_VIDC_BUF_LINE ||
  155. buffer_type == MSM_VIDC_BUF_PERSIST) {
  156. count = 1;
  157. } else if (buffer_type == MSM_VIDC_BUF_COMV ||
  158. buffer_type == MSM_VIDC_BUF_NON_COMV) {
  159. if (inst->codec == MSM_VIDC_H264 ||
  160. inst->codec == MSM_VIDC_HEVC ||
  161. inst->codec == MSM_VIDC_HEIC)
  162. count = 1;
  163. else
  164. count = 0;
  165. } else {
  166. i_vpr_e(inst, "%s: unsupported buffer type %#x\n",
  167. __func__, buffer_type);
  168. count = 0;
  169. }
  170. }
  171. //todo: add enc support if needed
  172. return count;
  173. }
  174. u32 msm_vidc_decoder_input_size(struct msm_vidc_inst *inst)
  175. {
  176. u32 frame_size, num_mbs;
  177. u32 div_factor = 1;
  178. u32 base_res_mbs = NUM_MBS_4k;
  179. struct v4l2_format *f;
  180. u32 bitstream_size_overwrite = 0;
  181. if (!inst || !inst->capabilities) {
  182. d_vpr_e("%s: invalid params\n");
  183. return 0;
  184. }
  185. bitstream_size_overwrite =
  186. inst->capabilities->cap[BITSTREAM_SIZE_OVERWRITE].value;
  187. if (bitstream_size_overwrite) {
  188. frame_size = bitstream_size_overwrite;
  189. i_vpr_h(inst, "client configured bitstream buffer size %d\n",
  190. frame_size);
  191. return frame_size;
  192. }
  193. /*
  194. * Decoder input size calculation:
  195. * For 8k resolution, buffer size is calculated as 8k mbs / 4 and
  196. * for 8k cases we expect width/height to be set always.
  197. * In all other cases, buffer size is calculated as
  198. * 4k mbs for VP8/VP9 and 4k / 2 for remaining codecs.
  199. */
  200. f = &inst->fmts[INPUT_PORT];
  201. num_mbs = msm_vidc_get_mbs_per_frame(inst);
  202. if (num_mbs > NUM_MBS_4k) {
  203. div_factor = 4;
  204. base_res_mbs = inst->capabilities->cap[MBPF].value;
  205. } else {
  206. base_res_mbs = NUM_MBS_4k;
  207. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9)
  208. div_factor = 1;
  209. else
  210. div_factor = 2;
  211. }
  212. if (is_secure_session(inst))
  213. div_factor = div_factor << 1;
  214. /* For image session, use the actual resolution to calc buffer size */
  215. if (is_image_session(inst)) {
  216. base_res_mbs = num_mbs;
  217. div_factor = 1;
  218. }
  219. frame_size = base_res_mbs * MB_SIZE_IN_PIXEL * 3 / 2 / div_factor;
  220. /* multiply by 10/8 (1.25) to get size for 10 bit case */
  221. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9 ||
  222. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC ||
  223. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEIC)
  224. frame_size = frame_size + (frame_size >> 2);
  225. i_vpr_h(inst, "set input buffer size to %d\n", frame_size);
  226. return ALIGN(frame_size, SZ_4K);
  227. }
  228. u32 msm_vidc_decoder_output_size(struct msm_vidc_inst *inst)
  229. {
  230. u32 size;
  231. struct v4l2_format *f;
  232. f = &inst->fmts[OUTPUT_PORT];
  233. size = VIDEO_RAW_BUFFER_SIZE(f->fmt.pix_mp.pixelformat,
  234. f->fmt.pix_mp.width,
  235. f->fmt.pix_mp.height, true);
  236. return size;
  237. }
  238. u32 msm_vidc_decoder_input_meta_size(struct msm_vidc_inst *inst)
  239. {
  240. return ALIGN(16 * 1024, SZ_4K);
  241. }
  242. u32 msm_vidc_decoder_output_meta_size(struct msm_vidc_inst *inst)
  243. {
  244. return ALIGN(16 * 1024, SZ_4K);
  245. }
  246. u32 msm_vidc_encoder_input_size(struct msm_vidc_inst *inst)
  247. {
  248. u32 size;
  249. struct v4l2_format *f;
  250. f = &inst->fmts[INPUT_PORT];
  251. size = VIDEO_RAW_BUFFER_SIZE(f->fmt.pix_mp.pixelformat,
  252. f->fmt.pix_mp.width,
  253. f->fmt.pix_mp.height, false);
  254. return size;
  255. }
  256. u32 msm_vidc_encoder_output_size(struct msm_vidc_inst *inst)
  257. {
  258. u32 frame_size;
  259. u32 mbs_per_frame;
  260. u32 width, height;
  261. struct v4l2_format *f;
  262. f = &inst->fmts[OUTPUT_PORT];
  263. /*
  264. * Encoder output size calculation: 32 Align width/height
  265. * For resolution < 720p : YUVsize * 4
  266. * For resolution > 720p & <= 4K : YUVsize / 2
  267. * For resolution > 4k : YUVsize / 4
  268. * Initially frame_size = YUVsize * 2;
  269. */
  270. /* if (is_grid_session(inst)) {
  271. f->fmt.pix_mp.width = f->fmt.pix_mp.height = HEIC_GRID_DIMENSION;
  272. } */
  273. width = ALIGN(f->fmt.pix_mp.width, BUFFER_ALIGNMENT_SIZE(32));
  274. height = ALIGN(f->fmt.pix_mp.height, BUFFER_ALIGNMENT_SIZE(32));
  275. mbs_per_frame = NUM_MBS_PER_FRAME(width, height);
  276. frame_size = (width * height * 3);
  277. /* Image session: 2 x yuv size */
  278. if (is_image_session(inst))
  279. goto skip_calc;
  280. if (mbs_per_frame < NUM_MBS_720P)
  281. frame_size = frame_size << 1;
  282. else if (mbs_per_frame <= NUM_MBS_4k)
  283. frame_size = frame_size >> 2;
  284. else
  285. frame_size = frame_size >> 3;
  286. /*if ((inst->rc_type == RATE_CONTROL_OFF) ||
  287. (inst->rc_type == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ))
  288. frame_size = frame_size << 1;
  289. if (inst->rc_type == RATE_CONTROL_LOSSLESS)
  290. frame_size = (width * height * 9) >> 2; */
  291. skip_calc:
  292. /* multiply by 10/8 (1.25) to get size for 10 bit case */
  293. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC ||
  294. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEIC)
  295. frame_size = frame_size + (frame_size >> 2);
  296. return ALIGN(frame_size, SZ_4K);
  297. }
  298. u32 msm_vidc_encoder_input_meta_size(struct msm_vidc_inst *inst)
  299. {
  300. return ALIGN(1 * 1024 * 1024, SZ_4K);
  301. }
  302. u32 msm_vidc_encoder_output_meta_size(struct msm_vidc_inst *inst)
  303. {
  304. return ALIGN(16 * 1024, SZ_4K);
  305. }