msm_vidc_buffer.c 8.3 KB

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