msm_vidc_buffer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <media/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. s_vpr_e(inst->sid, "%s: invalid domain\n",
  26. __func__, inst->domain);
  27. return 0;
  28. }
  29. if (is_thumbnail_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))
  50. return inst->codec == MSM_VIDC_VP9 ? 8 : 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. default:
  61. output_min_count = 4;
  62. }
  63. } else {
  64. output_min_count = MIN_ENC_OUTPUT_BUFFERS;
  65. }
  66. //if (is_vpp_delay_allowed(inst)) {
  67. // output_min_count =
  68. // max(output_min_count, (u32)MAX_BSE_VPP_DELAY);
  69. // output_min_count =
  70. // max(output_min_count, (u32)(msm_vidc_vpp_delay & 0x1F));
  71. //}
  72. return output_min_count;
  73. }
  74. u32 msm_vidc_input_extra_count(struct msm_vidc_inst *inst)
  75. {
  76. u32 count = 0;
  77. struct msm_vidc_core *core;
  78. if (!inst || !inst->core) {
  79. d_vpr_e("%s: invalid params %pK\n", __func__, inst);
  80. return 0;
  81. }
  82. core = inst->core;
  83. /*
  84. * no extra buffers for thumbnail session because
  85. * neither dcvs nor batching will be enabled
  86. */
  87. if (is_thumbnail_session(inst))
  88. return 0;
  89. if (is_decode_session(inst)) {
  90. /*
  91. * if decode batching enabled, ensure minimum batch size
  92. * count of input buffers present on input port
  93. */
  94. if (core->capabilities[DECODE_BATCH].value &&
  95. inst->decode_batch.enable) {
  96. if (inst->buffers.input.min_count < inst->decode_batch.size) {
  97. count = inst->decode_batch.size -
  98. inst->buffers.input.min_count;
  99. }
  100. }
  101. } else if (is_encode_session(inst)) {
  102. /* add dcvs buffers */
  103. count = DCVS_ENC_EXTRA_INPUT_BUFFERS;
  104. }
  105. return count;
  106. }
  107. u32 msm_vidc_output_extra_count(struct msm_vidc_inst *inst)
  108. {
  109. u32 count = 0;
  110. struct msm_vidc_core *core;
  111. if (!inst || !inst->core) {
  112. d_vpr_e("%s: invalid params %pK\n", __func__, inst);
  113. return 0;
  114. }
  115. core = inst->core;
  116. /*
  117. * no extra buffers for thumbnail session because
  118. * neither dcvs nor batching will be enabled
  119. */
  120. if (is_thumbnail_session(inst))
  121. return 0;
  122. if (is_decode_session(inst)) {
  123. /* add dcvs buffers */
  124. count = DCVS_DEC_EXTRA_OUTPUT_BUFFERS;
  125. /*
  126. * if decode batching enabled, ensure minimum batch size
  127. * count of extra output buffers added on output port
  128. */
  129. if (core->capabilities[DECODE_BATCH].value &&
  130. inst->decode_batch.enable &&
  131. count < inst->decode_batch.size)
  132. count = inst->decode_batch.size;
  133. } else if (is_encode_session(inst)) {
  134. /* add heif buffers */
  135. //count = 8
  136. }
  137. return count;
  138. }
  139. u32 msm_vidc_decoder_input_size(struct msm_vidc_inst *inst)
  140. {
  141. u32 frame_size, num_mbs;
  142. u32 div_factor = 1;
  143. u32 base_res_mbs = NUM_MBS_4k;
  144. struct v4l2_format *f;
  145. u32 buffer_size_limit = 0; // TODO: fix me
  146. if (!inst || !inst->capabilities) {
  147. d_vpr_e("%s: invalid params\n");
  148. return 0;
  149. }
  150. /*
  151. * Decoder input size calculation:
  152. * For 8k resolution, buffer size is calculated as 8k mbs / 4 and
  153. * for 8k cases we expect width/height to be set always.
  154. * In all other cases, buffer size is calculated as
  155. * 4k mbs for VP8/VP9 and 4k / 2 for remaining codecs.
  156. */
  157. f = &inst->fmts[INPUT_PORT];
  158. num_mbs = msm_vidc_get_mbs_per_frame(inst);
  159. if (num_mbs > NUM_MBS_4k) {
  160. div_factor = 4;
  161. base_res_mbs = inst->capabilities->cap[MBPF].value;
  162. } else {
  163. base_res_mbs = NUM_MBS_4k;
  164. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9)
  165. div_factor = 1;
  166. else
  167. div_factor = 2;
  168. }
  169. if (is_secure_session(inst))
  170. div_factor = div_factor << 1;
  171. /* For HEIF image, use the actual resolution to calc buffer size */
  172. /* TODO: fix me
  173. if (is_heif_decoder(inst)) {
  174. base_res_mbs = num_mbs;
  175. div_factor = 1;
  176. }
  177. */
  178. frame_size = base_res_mbs * MB_SIZE_IN_PIXEL * 3 / 2 / div_factor;
  179. /* multiply by 10/8 (1.25) to get size for 10 bit case */
  180. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9 ||
  181. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC)
  182. frame_size = frame_size + (frame_size >> 2);
  183. if (buffer_size_limit && (buffer_size_limit < frame_size)) {
  184. frame_size = buffer_size_limit;
  185. s_vpr_h(inst->sid, "input buffer size limited to %d\n",
  186. frame_size);
  187. } else {
  188. s_vpr_h(inst->sid, "set input buffer size to %d\n",
  189. frame_size);
  190. }
  191. return ALIGN(frame_size, SZ_4K);
  192. }
  193. u32 msm_vidc_decoder_output_size(struct msm_vidc_inst *inst)
  194. {
  195. u32 size;
  196. u32 format;
  197. struct v4l2_format *f;
  198. f = &inst->fmts[OUTPUT_PORT];
  199. format = v4l2_colorformat_to_media(f->fmt.pix_mp.pixelformat, __func__);
  200. size = VENUS_BUFFER_SIZE(format, f->fmt.pix_mp.width,
  201. f->fmt.pix_mp.height);
  202. return size;
  203. }
  204. u32 msm_vidc_decoder_input_meta_size(struct msm_vidc_inst *inst)
  205. {
  206. return ALIGN(16 * 1024, SZ_4K);
  207. }
  208. u32 msm_vidc_decoder_output_meta_size(struct msm_vidc_inst *inst)
  209. {
  210. return ALIGN(16 * 1024, SZ_4K);
  211. }
  212. u32 msm_vidc_encoder_input_size(struct msm_vidc_inst *inst)
  213. {
  214. u32 size;
  215. u32 format;
  216. struct v4l2_format *f;
  217. f = &inst->fmts[INPUT_PORT];
  218. format = v4l2_colorformat_to_media(f->fmt.pix_mp.pixelformat, __func__);
  219. size = VENUS_BUFFER_SIZE(format, f->fmt.pix_mp.width,
  220. f->fmt.pix_mp.height);
  221. return size;
  222. }
  223. u32 msm_vidc_encoder_output_size(struct msm_vidc_inst *inst)
  224. {
  225. u32 frame_size;
  226. u32 mbs_per_frame;
  227. u32 width, height;
  228. struct v4l2_format *f;
  229. f = &inst->fmts[OUTPUT_PORT];
  230. /*
  231. * Encoder output size calculation: 32 Align width/height
  232. * For resolution < 720p : YUVsize * 4
  233. * For resolution > 720p & <= 4K : YUVsize / 2
  234. * For resolution > 4k : YUVsize / 4
  235. * Initially frame_size = YUVsize * 2;
  236. */
  237. /* if (is_grid_session(inst)) {
  238. f->fmt.pix_mp.width = f->fmt.pix_mp.height = HEIC_GRID_DIMENSION;
  239. } */
  240. width = ALIGN(f->fmt.pix_mp.width, BUFFER_ALIGNMENT_SIZE(32));
  241. height = ALIGN(f->fmt.pix_mp.height, BUFFER_ALIGNMENT_SIZE(32));
  242. mbs_per_frame = NUM_MBS_PER_FRAME(width, height);
  243. frame_size = (width * height * 3);
  244. if (mbs_per_frame < NUM_MBS_720P)
  245. frame_size = frame_size << 1;
  246. else if (mbs_per_frame <= NUM_MBS_4k)
  247. frame_size = frame_size >> 2;
  248. else
  249. frame_size = frame_size >> 3;
  250. /*if ((inst->rc_type == RATE_CONTROL_OFF) ||
  251. (inst->rc_type == V4L2_MPEG_VIDEO_BITRATE_MODE_CQ))
  252. frame_size = frame_size << 1;
  253. if (inst->rc_type == RATE_CONTROL_LOSSLESS)
  254. frame_size = (width * height * 9) >> 2; */
  255. /* multiply by 10/8 (1.25) to get size for 10 bit case */
  256. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC)
  257. frame_size = frame_size + (frame_size >> 2);
  258. return ALIGN(frame_size, SZ_4K);
  259. }
  260. u32 msm_vidc_encoder_input_meta_size(struct msm_vidc_inst *inst)
  261. {
  262. return ALIGN(1 * 1024 * 1024, SZ_4K);
  263. }
  264. u32 msm_vidc_encoder_output_meta_size(struct msm_vidc_inst *inst)
  265. {
  266. return ALIGN(16 * 1024, SZ_4K);
  267. }