msm_venc.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <media/v4l2_vidc_extensions.h>
  6. #include "msm_media_info.h"
  7. #include "msm_venc.h"
  8. #include "msm_vidc_core.h"
  9. #include "msm_vidc_inst.h"
  10. #include "msm_vidc_driver.h"
  11. #include "msm_vidc_internal.h"
  12. #include "msm_vidc_platform.h"
  13. #include "msm_vidc_control.h"
  14. #include "msm_vidc_debug.h"
  15. #include "msm_vidc_power.h"
  16. #include "venus_hfi.h"
  17. #include "hfi_packet.h"
  18. static const u32 msm_venc_input_set_prop[] = {
  19. HFI_PROP_COLOR_FORMAT,
  20. HFI_PROP_RAW_RESOLUTION,
  21. HFI_PROP_LINEAR_STRIDE_SCANLINE,
  22. HFI_PROP_BUFFER_HOST_MAX_COUNT,
  23. HFI_PROP_SIGNAL_COLOR_INFO,
  24. };
  25. static const u32 msm_venc_output_set_prop[] = {
  26. HFI_PROP_BITSTREAM_RESOLUTION,
  27. HFI_PROP_CROP_OFFSETS,
  28. HFI_PROP_BUFFER_HOST_MAX_COUNT,
  29. HFI_PROP_CSC,
  30. };
  31. static const u32 msm_venc_input_subscribe_for_properties[] = {
  32. HFI_PROP_NO_OUTPUT,
  33. };
  34. static const u32 msm_venc_output_subscribe_for_properties[] = {
  35. HFI_PROP_PICTURE_TYPE,
  36. HFI_PROP_BUFFER_MARK,
  37. HFI_PROP_WORST_COMPRESSION_RATIO,
  38. };
  39. static const u32 msm_venc_output_internal_buffer_type[] = {
  40. MSM_VIDC_BUF_BIN,
  41. MSM_VIDC_BUF_COMV,
  42. MSM_VIDC_BUF_NON_COMV,
  43. MSM_VIDC_BUF_LINE,
  44. MSM_VIDC_BUF_DPB,
  45. };
  46. static const u32 msm_venc_input_internal_buffer_type[] = {
  47. MSM_VIDC_BUF_VPSS,
  48. };
  49. struct msm_venc_prop_type_handle {
  50. u32 type;
  51. int (*handle)(struct msm_vidc_inst *inst, enum msm_vidc_port_type port);
  52. };
  53. static int msm_venc_codec_change(struct msm_vidc_inst *inst, u32 v4l2_codec)
  54. {
  55. int rc = 0;
  56. if (inst->codec && inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat == v4l2_codec)
  57. return 0;
  58. i_vpr_h(inst, "%s: codec changed from %s to %s\n",
  59. __func__, v4l2_pixelfmt_name(inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat),
  60. v4l2_pixelfmt_name(v4l2_codec));
  61. inst->codec = v4l2_codec_to_driver(v4l2_codec, __func__);
  62. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat = v4l2_codec;
  63. rc = msm_vidc_update_debug_str(inst);
  64. if (rc)
  65. goto exit;
  66. rc = msm_vidc_get_inst_capability(inst);
  67. if (rc)
  68. goto exit;
  69. rc = msm_vidc_ctrl_deinit(inst);
  70. if (rc)
  71. goto exit;
  72. rc = msm_vidc_ctrl_init(inst);
  73. if (rc)
  74. goto exit;
  75. rc = msm_vidc_update_buffer_count(inst, INPUT_PORT);
  76. if (rc)
  77. return rc;
  78. rc = msm_vidc_update_buffer_count(inst, OUTPUT_PORT);
  79. if (rc)
  80. return rc;
  81. exit:
  82. return rc;
  83. }
  84. /* todo: add logs for each property once finalised */
  85. static int msm_venc_set_colorformat(struct msm_vidc_inst *inst,
  86. enum msm_vidc_port_type port)
  87. {
  88. int rc = 0;
  89. u32 pixelformat;
  90. enum msm_vidc_colorformat_type colorformat;
  91. u32 hfi_colorformat;
  92. if (port != INPUT_PORT) {
  93. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  94. return -EINVAL;
  95. }
  96. pixelformat = inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat;
  97. colorformat = v4l2_colorformat_to_driver(pixelformat, __func__);
  98. if (!(colorformat & inst->capabilities->cap[PIX_FMTS].step_or_mask)) {
  99. i_vpr_e(inst, "%s: invalid pixelformat %s\n",
  100. __func__, v4l2_pixelfmt_name(pixelformat));
  101. return -EINVAL;
  102. }
  103. hfi_colorformat = get_hfi_colorformat(inst, colorformat);
  104. i_vpr_h(inst, "%s: hfi colorformat: %#x", __func__,
  105. hfi_colorformat);
  106. rc = venus_hfi_session_property(inst,
  107. HFI_PROP_COLOR_FORMAT,
  108. HFI_HOST_FLAGS_NONE,
  109. get_hfi_port(inst, port),
  110. HFI_PAYLOAD_U32_ENUM,
  111. &hfi_colorformat,
  112. sizeof(u32));
  113. if (rc)
  114. return rc;
  115. return 0;
  116. }
  117. static int msm_venc_set_stride_scanline(struct msm_vidc_inst *inst,
  118. enum msm_vidc_port_type port)
  119. {
  120. int rc = 0;
  121. u32 color_format, stride_y, scanline_y;
  122. u32 stride_uv = 0, scanline_uv = 0;
  123. u32 payload[2];
  124. if (port != INPUT_PORT) {
  125. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  126. return -EINVAL;
  127. }
  128. color_format = inst->capabilities->cap[PIX_FMTS].value;
  129. if (!is_linear_colorformat(color_format)) {
  130. i_vpr_h(inst,
  131. "%s: not a linear color fmt, property is not set\n",
  132. __func__);
  133. return 0;
  134. }
  135. stride_y = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  136. scanline_y = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  137. if (color_format == MSM_VIDC_FMT_NV12 ||
  138. color_format == MSM_VIDC_FMT_P010 ||
  139. color_format == MSM_VIDC_FMT_NV21) {
  140. stride_uv = stride_y;
  141. scanline_uv = scanline_y / 2;
  142. }
  143. payload[0] = stride_y << 16 | scanline_y;
  144. payload[1] = stride_uv << 16 | scanline_uv;
  145. i_vpr_h(inst, "%s: stride_y: %d scanline_y: %d "
  146. "stride_uv: %d, scanline_uv: %d", __func__,
  147. stride_y, scanline_y, stride_uv, scanline_uv);
  148. rc = venus_hfi_session_property(inst,
  149. HFI_PROP_LINEAR_STRIDE_SCANLINE,
  150. HFI_HOST_FLAGS_NONE,
  151. get_hfi_port(inst, port),
  152. HFI_PAYLOAD_64_PACKED,
  153. &payload,
  154. sizeof(u64));
  155. if (rc)
  156. return rc;
  157. return 0;
  158. }
  159. static int msm_venc_set_raw_resolution(struct msm_vidc_inst *inst,
  160. enum msm_vidc_port_type port)
  161. {
  162. int rc = 0;
  163. u32 resolution;
  164. if (port != INPUT_PORT) {
  165. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  166. return -EINVAL;
  167. }
  168. resolution = inst->crop.width << 16 | inst->crop.height;
  169. i_vpr_h(inst, "%s: width: %d height: %d\n", __func__,
  170. inst->crop.width, inst->crop.height);
  171. rc = venus_hfi_session_property(inst,
  172. HFI_PROP_RAW_RESOLUTION,
  173. HFI_HOST_FLAGS_NONE,
  174. get_hfi_port(inst, port),
  175. HFI_PAYLOAD_32_PACKED,
  176. &resolution,
  177. sizeof(u32));
  178. if (rc)
  179. return rc;
  180. return 0;
  181. }
  182. static int msm_venc_set_bitstream_resolution(struct msm_vidc_inst *inst,
  183. enum msm_vidc_port_type port)
  184. {
  185. int rc = 0;
  186. u32 resolution;
  187. if (port != OUTPUT_PORT) {
  188. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  189. return -EINVAL;
  190. }
  191. resolution = (inst->fmts[port].fmt.pix_mp.width << 16) |
  192. inst->fmts[port].fmt.pix_mp.height;
  193. i_vpr_h(inst, "%s: width: %d height: %d\n", __func__,
  194. inst->fmts[port].fmt.pix_mp.width,
  195. inst->fmts[port].fmt.pix_mp.height);
  196. rc = venus_hfi_session_property(inst,
  197. HFI_PROP_BITSTREAM_RESOLUTION,
  198. HFI_HOST_FLAGS_NONE,
  199. get_hfi_port(inst, port),
  200. HFI_PAYLOAD_32_PACKED,
  201. &resolution,
  202. sizeof(u32));
  203. if (rc)
  204. return rc;
  205. return 0;
  206. }
  207. static int msm_venc_set_crop_offsets(struct msm_vidc_inst *inst,
  208. enum msm_vidc_port_type port)
  209. {
  210. int rc = 0;
  211. u32 left_offset, top_offset, right_offset, bottom_offset;
  212. u32 crop[2] = {0};
  213. u32 width, height;
  214. if (port != OUTPUT_PORT) {
  215. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  216. return -EINVAL;
  217. }
  218. left_offset = inst->compose.left;
  219. top_offset = inst->compose.top;
  220. width = inst->compose.width;
  221. height = inst->compose.height;
  222. if (is_rotation_90_or_270(inst)) {
  223. width = inst->compose.height;
  224. height = inst->compose.width;
  225. }
  226. right_offset = (inst->fmts[port].fmt.pix_mp.width - width);
  227. bottom_offset = (inst->fmts[port].fmt.pix_mp.height - height);
  228. if (is_image_session(inst))
  229. right_offset = bottom_offset = 0;
  230. crop[0] = left_offset << 16 | top_offset;
  231. crop[1] = right_offset << 16 | bottom_offset;
  232. i_vpr_h(inst, "%s: left_offset: %d top_offset: %d "
  233. "right_offset: %d bottom_offset: %d", __func__,
  234. left_offset, top_offset, right_offset, bottom_offset);
  235. rc = venus_hfi_session_property(inst,
  236. HFI_PROP_CROP_OFFSETS,
  237. HFI_HOST_FLAGS_NONE,
  238. get_hfi_port(inst, port),
  239. HFI_PAYLOAD_64_PACKED,
  240. &crop,
  241. sizeof(u64));
  242. if (rc)
  243. return rc;
  244. return 0;
  245. }
  246. static int msm_venc_set_host_max_buf_count(struct msm_vidc_inst *inst,
  247. enum msm_vidc_port_type port)
  248. {
  249. int rc = 0;
  250. u32 count = DEFAULT_MAX_HOST_BUF_COUNT;
  251. if (msm_vidc_is_super_buffer(inst) || is_image_session(inst))
  252. count = DEFAULT_MAX_HOST_BURST_BUF_COUNT;
  253. i_vpr_h(inst, "%s: count: %u port: %u\n", __func__, count, port);
  254. rc = venus_hfi_session_property(inst,
  255. HFI_PROP_BUFFER_HOST_MAX_COUNT,
  256. HFI_HOST_FLAGS_NONE,
  257. get_hfi_port(inst, port),
  258. HFI_PAYLOAD_U32,
  259. &count,
  260. sizeof(u32));
  261. if (rc)
  262. return rc;
  263. return 0;
  264. }
  265. static int msm_venc_set_colorspace(struct msm_vidc_inst* inst,
  266. enum msm_vidc_port_type port)
  267. {
  268. int rc = 0;
  269. u32 primaries = MSM_VIDC_PRIMARIES_RESERVED;
  270. u32 matrix_coeff = MSM_VIDC_MATRIX_COEFF_RESERVED;
  271. u32 transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  272. u32 full_range = 0;
  273. u32 colour_description_present_flag = 0;
  274. u32 video_signal_type_present_flag = 0, payload = 0;
  275. /* Unspecified video format */
  276. u32 video_format = 5;
  277. struct v4l2_format *input_fmt;
  278. u32 pix_fmt;
  279. if (port != INPUT_PORT) {
  280. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  281. return -EINVAL;
  282. }
  283. input_fmt = &inst->fmts[INPUT_PORT];
  284. pix_fmt = v4l2_colorformat_to_driver(
  285. input_fmt->fmt.pix_mp.pixelformat, __func__);
  286. if (inst->fmts[port].fmt.pix_mp.colorspace != V4L2_COLORSPACE_DEFAULT ||
  287. inst->fmts[port].fmt.pix_mp.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT ||
  288. inst->fmts[port].fmt.pix_mp.xfer_func != V4L2_XFER_FUNC_DEFAULT) {
  289. colour_description_present_flag = 1;
  290. video_signal_type_present_flag = 1;
  291. primaries = v4l2_color_primaries_to_driver(inst,
  292. inst->fmts[port].fmt.pix_mp.colorspace, __func__);
  293. matrix_coeff = v4l2_matrix_coeff_to_driver(inst,
  294. inst->fmts[port].fmt.pix_mp.ycbcr_enc, __func__);
  295. transfer_char = v4l2_transfer_char_to_driver(inst,
  296. inst->fmts[port].fmt.pix_mp.xfer_func, __func__);
  297. } else if (is_rgba_colorformat(pix_fmt)) {
  298. colour_description_present_flag = 1;
  299. video_signal_type_present_flag = 1;
  300. primaries = MSM_VIDC_PRIMARIES_BT709;
  301. matrix_coeff = MSM_VIDC_MATRIX_COEFF_BT709;
  302. transfer_char = MSM_VIDC_TRANSFER_BT709;
  303. full_range = 0;
  304. }
  305. if (inst->fmts[port].fmt.pix_mp.quantization !=
  306. V4L2_QUANTIZATION_DEFAULT) {
  307. video_signal_type_present_flag = 1;
  308. full_range = inst->fmts[port].fmt.pix_mp.quantization ==
  309. V4L2_QUANTIZATION_FULL_RANGE ? 1 : 0;
  310. }
  311. payload = (matrix_coeff & 0xFF) |
  312. ((transfer_char << 8) & 0xFF00) |
  313. ((primaries << 16) & 0xFF0000) |
  314. ((colour_description_present_flag << 24) & 0x1000000) |
  315. ((full_range << 25) & 0x2000000) |
  316. ((video_format << 26) & 0x1C000000) |
  317. ((video_signal_type_present_flag << 29) & 0x20000000);
  318. i_vpr_h(inst, "%s: color info: %#x\n", __func__, payload);
  319. rc = venus_hfi_session_property(inst,
  320. HFI_PROP_SIGNAL_COLOR_INFO,
  321. HFI_HOST_FLAGS_NONE,
  322. get_hfi_port(inst, port),
  323. HFI_PAYLOAD_32_PACKED,
  324. &payload,
  325. sizeof(u32));
  326. if (rc)
  327. return rc;
  328. return 0;
  329. }
  330. static bool msm_venc_csc_required(struct msm_vidc_inst* inst)
  331. {
  332. struct v4l2_format *in_fmt = &inst->fmts[INPUT_PORT];
  333. struct v4l2_format *out_fmt = &inst->fmts[OUTPUT_PORT];
  334. /* video hardware supports conversion to REC709 CSC only */
  335. if (in_fmt->fmt.pix_mp.colorspace != out_fmt->fmt.pix_mp.colorspace &&
  336. out_fmt->fmt.pix_mp.colorspace == V4L2_COLORSPACE_REC709)
  337. return true;
  338. return false;
  339. }
  340. static int msm_venc_set_csc(struct msm_vidc_inst* inst,
  341. enum msm_vidc_port_type port)
  342. {
  343. int rc = 0;
  344. u32 csc = 0;
  345. if (port != OUTPUT_PORT) {
  346. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  347. return -EINVAL;
  348. }
  349. msm_vidc_update_cap_value(inst, CSC,
  350. msm_venc_csc_required(inst) ? 1 : 0, __func__);
  351. csc = inst->capabilities->cap[CSC].value;
  352. i_vpr_h(inst, "%s: csc: %u\n", __func__, csc);
  353. rc = venus_hfi_session_property(inst,
  354. HFI_PROP_CSC,
  355. HFI_HOST_FLAGS_NONE,
  356. get_hfi_port(inst, port),
  357. HFI_PAYLOAD_U32,
  358. &csc,
  359. sizeof(u32));
  360. if (rc)
  361. return rc;
  362. return 0;
  363. }
  364. static int msm_venc_set_quality_mode(struct msm_vidc_inst *inst)
  365. {
  366. int rc = 0;
  367. struct msm_vidc_core* core = inst->core;
  368. struct msm_vidc_inst_capability *capability = inst->capabilities;
  369. u32 mode;
  370. rc = call_session_op(core, decide_quality_mode, inst);
  371. if (rc) {
  372. i_vpr_e(inst, "%s: decide_work_route failed\n",
  373. __func__);
  374. return -EINVAL;
  375. }
  376. mode = capability->cap[QUALITY_MODE].value;
  377. i_vpr_h(inst, "%s: quality_mode: %u\n", __func__, mode);
  378. rc = venus_hfi_session_property(inst,
  379. HFI_PROP_QUALITY_MODE,
  380. HFI_HOST_FLAGS_NONE,
  381. HFI_PORT_BITSTREAM,
  382. HFI_PAYLOAD_U32_ENUM,
  383. &mode,
  384. sizeof(u32));
  385. if (rc)
  386. return rc;
  387. return 0;
  388. }
  389. static int msm_venc_set_input_properties(struct msm_vidc_inst *inst)
  390. {
  391. int i, j, rc = 0;
  392. static const struct msm_venc_prop_type_handle prop_type_handle_arr[] = {
  393. {HFI_PROP_COLOR_FORMAT, msm_venc_set_colorformat },
  394. {HFI_PROP_RAW_RESOLUTION, msm_venc_set_raw_resolution },
  395. {HFI_PROP_LINEAR_STRIDE_SCANLINE, msm_venc_set_stride_scanline },
  396. {HFI_PROP_BUFFER_HOST_MAX_COUNT, msm_venc_set_host_max_buf_count },
  397. {HFI_PROP_SIGNAL_COLOR_INFO, msm_venc_set_colorspace },
  398. };
  399. if (!inst) {
  400. d_vpr_e("%s: invalid params\n", __func__);
  401. return -EINVAL;
  402. }
  403. i_vpr_h(inst, "%s()\n", __func__);
  404. for (i = 0; i < ARRAY_SIZE(msm_venc_input_set_prop); i++) {
  405. /* set session input properties */
  406. for (j = 0; j < ARRAY_SIZE(prop_type_handle_arr); j++) {
  407. if (prop_type_handle_arr[j].type == msm_venc_input_set_prop[i]) {
  408. rc = prop_type_handle_arr[j].handle(inst, INPUT_PORT);
  409. if (rc)
  410. goto exit;
  411. break;
  412. }
  413. }
  414. /* is property type unknown ? */
  415. if (j == ARRAY_SIZE(prop_type_handle_arr))
  416. i_vpr_e(inst, "%s: unknown property %#x\n", __func__,
  417. msm_venc_input_set_prop[i]);
  418. }
  419. exit:
  420. return rc;
  421. }
  422. static int msm_venc_set_output_properties(struct msm_vidc_inst *inst)
  423. {
  424. int i, j, rc = 0;
  425. static const struct msm_venc_prop_type_handle prop_type_handle_arr[] = {
  426. {HFI_PROP_BITSTREAM_RESOLUTION, msm_venc_set_bitstream_resolution },
  427. {HFI_PROP_CROP_OFFSETS, msm_venc_set_crop_offsets },
  428. {HFI_PROP_BUFFER_HOST_MAX_COUNT, msm_venc_set_host_max_buf_count },
  429. {HFI_PROP_CSC, msm_venc_set_csc },
  430. };
  431. if (!inst) {
  432. d_vpr_e("%s: invalid params\n", __func__);
  433. return -EINVAL;
  434. }
  435. i_vpr_h(inst, "%s()\n", __func__);
  436. for (i = 0; i < ARRAY_SIZE(msm_venc_output_set_prop); i++) {
  437. /* set session output properties */
  438. for (j = 0; j < ARRAY_SIZE(prop_type_handle_arr); j++) {
  439. if (prop_type_handle_arr[j].type == msm_venc_output_set_prop[i]) {
  440. rc = prop_type_handle_arr[j].handle(inst, OUTPUT_PORT);
  441. if (rc)
  442. goto exit;
  443. break;
  444. }
  445. }
  446. /* is property type unknown ? */
  447. if (j == ARRAY_SIZE(prop_type_handle_arr))
  448. i_vpr_e(inst, "%s: unknown property %#x\n", __func__,
  449. msm_venc_output_set_prop[i]);
  450. }
  451. exit:
  452. return rc;
  453. }
  454. static int msm_venc_set_internal_properties(struct msm_vidc_inst *inst)
  455. {
  456. int rc = 0;
  457. if (!inst) {
  458. d_vpr_e("%s: invalid params\n", __func__);
  459. return -EINVAL;
  460. }
  461. i_vpr_h(inst, "%s()\n", __func__);
  462. rc = msm_venc_set_quality_mode(inst);
  463. if (rc)
  464. return rc;
  465. return rc;
  466. }
  467. static int msm_venc_get_input_internal_buffers(struct msm_vidc_inst *inst)
  468. {
  469. int i, rc = 0;
  470. if (!inst) {
  471. d_vpr_e("%s: invalid params\n", __func__);
  472. return -EINVAL;
  473. }
  474. for (i = 0; i < ARRAY_SIZE(msm_venc_input_internal_buffer_type); i++) {
  475. rc = msm_vidc_get_internal_buffers(inst,
  476. msm_venc_input_internal_buffer_type[i]);
  477. if (rc)
  478. return rc;
  479. }
  480. return rc;
  481. }
  482. static int msm_venc_create_input_internal_buffers(struct msm_vidc_inst *inst)
  483. {
  484. int i, rc = 0;
  485. if (!inst || !inst->core) {
  486. d_vpr_e("%s: invalid params\n", __func__);
  487. return -EINVAL;
  488. }
  489. for (i = 0; i < ARRAY_SIZE(msm_venc_input_internal_buffer_type); i++) {
  490. rc = msm_vidc_create_internal_buffers(inst,
  491. msm_venc_input_internal_buffer_type[i]);
  492. if (rc)
  493. return rc;
  494. }
  495. return rc;
  496. }
  497. static int msm_venc_queue_input_internal_buffers(struct msm_vidc_inst *inst)
  498. {
  499. int i, rc = 0;
  500. if (!inst || !inst->core) {
  501. d_vpr_e("%s: invalid params\n", __func__);
  502. return -EINVAL;
  503. }
  504. for (i = 0; i < ARRAY_SIZE(msm_venc_input_internal_buffer_type); i++) {
  505. rc = msm_vidc_queue_internal_buffers(inst,
  506. msm_venc_input_internal_buffer_type[i]);
  507. if (rc)
  508. return rc;
  509. }
  510. return rc;
  511. }
  512. static int msm_venc_get_output_internal_buffers(struct msm_vidc_inst *inst)
  513. {
  514. int i, rc = 0;
  515. struct msm_vidc_core *core;
  516. if (!inst || !inst->core) {
  517. d_vpr_e("%s: invalid params\n", __func__);
  518. return -EINVAL;
  519. }
  520. core = inst->core;
  521. for (i = 0; i < ARRAY_SIZE(msm_venc_output_internal_buffer_type); i++) {
  522. rc = msm_vidc_get_internal_buffers(inst,
  523. msm_venc_output_internal_buffer_type[i]);
  524. if (rc)
  525. return rc;
  526. }
  527. return rc;
  528. }
  529. static int msm_venc_create_output_internal_buffers(struct msm_vidc_inst *inst)
  530. {
  531. int i, rc = 0;
  532. if (!inst || !inst->core) {
  533. d_vpr_e("%s: invalid params\n", __func__);
  534. return -EINVAL;
  535. }
  536. for (i = 0; i < ARRAY_SIZE(msm_venc_output_internal_buffer_type); i++) {
  537. rc = msm_vidc_create_internal_buffers(inst,
  538. msm_venc_output_internal_buffer_type[i]);
  539. if (rc)
  540. return rc;
  541. }
  542. return 0;
  543. }
  544. static int msm_venc_queue_output_internal_buffers(struct msm_vidc_inst *inst)
  545. {
  546. int i, rc = 0;
  547. if (!inst || !inst->core) {
  548. d_vpr_e("%s: invalid params\n", __func__);
  549. return -EINVAL;
  550. }
  551. for (i = 0; i < ARRAY_SIZE(msm_venc_output_internal_buffer_type); i++) {
  552. rc = msm_vidc_queue_internal_buffers(inst,
  553. msm_venc_output_internal_buffer_type[i]);
  554. if (rc)
  555. return rc;
  556. }
  557. return 0;
  558. }
  559. static int msm_venc_property_subscription(struct msm_vidc_inst *inst,
  560. enum msm_vidc_port_type port)
  561. {
  562. int rc = 0;
  563. u32 payload[32] = {0};
  564. u32 i;
  565. u32 payload_size = 0;
  566. if (!inst) {
  567. d_vpr_e("%s: invalid params\n", __func__);
  568. return -EINVAL;
  569. }
  570. i_vpr_h(inst, "%s()\n", __func__);
  571. payload[0] = HFI_MODE_PROPERTY;
  572. if (port == INPUT_PORT) {
  573. for (i = 0; i < ARRAY_SIZE(msm_venc_input_subscribe_for_properties); i++)
  574. payload[i + 1] = msm_venc_input_subscribe_for_properties[i];
  575. payload_size = (ARRAY_SIZE(msm_venc_input_subscribe_for_properties) + 1) *
  576. sizeof(u32);
  577. } else if (port == OUTPUT_PORT) {
  578. for (i = 0; i < ARRAY_SIZE(msm_venc_output_subscribe_for_properties); i++)
  579. payload[i + 1] = msm_venc_output_subscribe_for_properties[i];
  580. payload_size = (ARRAY_SIZE(msm_venc_output_subscribe_for_properties) + 1) *
  581. sizeof(u32);
  582. } else {
  583. i_vpr_e(inst, "%s: invalid port: %d\n", __func__, port);
  584. return -EINVAL;
  585. }
  586. rc = venus_hfi_session_command(inst,
  587. HFI_CMD_SUBSCRIBE_MODE,
  588. port,
  589. HFI_PAYLOAD_U32_ARRAY,
  590. &payload[0],
  591. payload_size);
  592. if (rc)
  593. return rc;
  594. return rc;
  595. }
  596. static int msm_venc_metadata_delivery(struct msm_vidc_inst *inst,
  597. enum msm_vidc_port_type port)
  598. {
  599. int rc = 0;
  600. struct msm_vidc_core *core;
  601. u32 payload[32] = {0};
  602. u32 i, count = 0;
  603. struct msm_vidc_inst_capability *capability;
  604. static const u32 metadata_list[] = {
  605. META_SEI_MASTERING_DISP,
  606. META_SEI_CLL,
  607. META_HDR10PLUS,
  608. META_EVA_STATS,
  609. META_BUF_TAG,
  610. META_ROI_INFO,
  611. };
  612. if (!inst || !inst->core) {
  613. d_vpr_e("%s: invalid params\n", __func__);
  614. return -EINVAL;
  615. }
  616. core = inst->core;
  617. i_vpr_h(inst, "%s()\n", __func__);
  618. capability = inst->capabilities;
  619. payload[0] = HFI_MODE_METADATA;
  620. for (i = 0; i < ARRAY_SIZE(metadata_list); i++) {
  621. if (capability->cap[metadata_list[i]].value) {
  622. payload[count + 1] =
  623. capability->cap[metadata_list[i]].hfi_id;
  624. count++;
  625. }
  626. }
  627. rc = venus_hfi_session_command(inst,
  628. HFI_CMD_DELIVERY_MODE,
  629. port,
  630. HFI_PAYLOAD_U32_ARRAY,
  631. &payload[0],
  632. (count + 1) * sizeof(u32));
  633. if (rc)
  634. return rc;
  635. return rc;
  636. }
  637. static int msm_venc_metadata_subscription(struct msm_vidc_inst *inst,
  638. enum msm_vidc_port_type port)
  639. {
  640. int rc = 0;
  641. u32 payload[32] = {0};
  642. u32 i, count = 0;
  643. struct msm_vidc_inst_capability *capability;
  644. static const u32 metadata_list[] = {
  645. META_LTR_MARK_USE,
  646. META_SEQ_HDR_NAL,
  647. META_TIMESTAMP,
  648. META_BUF_TAG,
  649. META_SUBFRAME_OUTPUT,
  650. META_ENC_QP_METADATA,
  651. };
  652. if (!inst) {
  653. d_vpr_e("%s: invalid params\n", __func__);
  654. return -EINVAL;
  655. }
  656. i_vpr_h(inst, "%s()\n", __func__);
  657. capability = inst->capabilities;
  658. payload[0] = HFI_MODE_METADATA;
  659. for (i = 0; i < ARRAY_SIZE(metadata_list); i++) {
  660. if (capability->cap[metadata_list[i]].value) {
  661. payload[count + 1] =
  662. capability->cap[metadata_list[i]].hfi_id;
  663. count++;
  664. }
  665. }
  666. rc = venus_hfi_session_command(inst,
  667. HFI_CMD_SUBSCRIBE_MODE,
  668. port,
  669. HFI_PAYLOAD_U32_ARRAY,
  670. &payload[0],
  671. (count + 1) * sizeof(u32));
  672. if (rc)
  673. return rc;
  674. return rc;
  675. }
  676. int msm_venc_streamoff_input(struct msm_vidc_inst *inst)
  677. {
  678. int rc = 0;
  679. if (!inst || !inst->core || !inst->capabilities) {
  680. d_vpr_e("%s: invalid params\n", __func__);
  681. return -EINVAL;
  682. }
  683. rc = msm_vidc_session_streamoff(inst, INPUT_PORT);
  684. if (rc)
  685. return rc;
  686. return 0;
  687. }
  688. int msm_venc_streamon_input(struct msm_vidc_inst *inst)
  689. {
  690. int rc = 0;
  691. if (!inst || !inst->core || !inst->capabilities) {
  692. d_vpr_e("%s: invalid params\n", __func__);
  693. return -EINVAL;
  694. }
  695. /*
  696. * do not check for input meta port streamon when
  697. * request is enabled
  698. */
  699. if (!inst->capabilities->cap[INPUT_META_VIA_REQUEST].value) {
  700. if (is_input_meta_enabled(inst) &&
  701. !inst->bufq[INPUT_META_PORT].vb2q->streaming) {
  702. i_vpr_e(inst,
  703. "%s: Meta port must be streamed on before data port\n",
  704. __func__);
  705. return -EINVAL;
  706. }
  707. }
  708. rc = msm_vidc_check_session_supported(inst);
  709. if (rc)
  710. goto error;
  711. rc = msm_vidc_check_scaling_supported(inst);
  712. if (rc)
  713. goto error;
  714. rc = msm_venc_set_input_properties(inst);
  715. if (rc)
  716. goto error;
  717. /* Decide bse vpp delay after work mode */
  718. //msm_vidc_set_bse_vpp_delay(inst);
  719. rc = msm_venc_get_input_internal_buffers(inst);
  720. if (rc)
  721. goto error;
  722. rc = msm_venc_create_input_internal_buffers(inst);
  723. if (rc)
  724. goto error;
  725. rc = msm_venc_queue_input_internal_buffers(inst);
  726. if (rc)
  727. goto error;
  728. rc = msm_venc_property_subscription(inst, INPUT_PORT);
  729. if (rc)
  730. return rc;
  731. rc = msm_venc_metadata_delivery(inst, INPUT_PORT);
  732. if (rc)
  733. return rc;
  734. rc = msm_vidc_session_streamon(inst, INPUT_PORT);
  735. if (rc)
  736. goto error;
  737. return 0;
  738. error:
  739. i_vpr_e(inst, "%s: failed\n", __func__);
  740. msm_venc_streamoff_input(inst);
  741. return rc;
  742. }
  743. int msm_venc_qbuf(struct msm_vidc_inst *inst, struct vb2_buffer *vb2)
  744. {
  745. int rc = 0;
  746. if (!inst || !inst->capabilities) {
  747. d_vpr_e("%s: invalid params\n", __func__);
  748. return -EINVAL;
  749. }
  750. if (inst->firmware_priority != (inst->priority_level +
  751. inst->capabilities->cap[PRIORITY].value * 2))
  752. msm_vidc_set_session_priority(inst, PRIORITY);
  753. rc = msm_vidc_queue_buffer_single(inst, vb2);
  754. if (rc)
  755. return rc;
  756. return rc;
  757. }
  758. int msm_venc_process_cmd(struct msm_vidc_inst *inst, u32 cmd)
  759. {
  760. int rc = 0;
  761. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  762. if (!inst || !inst->core) {
  763. d_vpr_e("%s: invalid params\n", __func__);
  764. return -EINVAL;
  765. }
  766. if (cmd == V4L2_ENC_CMD_STOP) {
  767. i_vpr_h(inst, "received cmd: drain\n");
  768. allow = msm_vidc_allow_stop(inst);
  769. if (allow == MSM_VIDC_DISALLOW)
  770. return -EBUSY;
  771. else if (allow == MSM_VIDC_IGNORE)
  772. return 0;
  773. else if (allow != MSM_VIDC_ALLOW)
  774. return -EINVAL;
  775. rc = venus_hfi_session_command(inst,
  776. HFI_CMD_DRAIN,
  777. INPUT_PORT,
  778. HFI_PAYLOAD_NONE,
  779. NULL,
  780. 0);
  781. if (rc)
  782. return rc;
  783. rc = msm_vidc_state_change_stop(inst);
  784. if (rc)
  785. return rc;
  786. } else if (cmd == V4L2_ENC_CMD_START) {
  787. i_vpr_h(inst, "received cmd: resume\n");
  788. if (!msm_vidc_allow_start(inst))
  789. return -EBUSY;
  790. vb2_clear_last_buffer_dequeued(inst->bufq[OUTPUT_META_PORT].vb2q);
  791. vb2_clear_last_buffer_dequeued(inst->bufq[OUTPUT_PORT].vb2q);
  792. rc = msm_vidc_state_change_start(inst);
  793. if (rc)
  794. return rc;
  795. /* tune power features */
  796. msm_vidc_allow_dcvs(inst);
  797. msm_vidc_power_data_reset(inst);
  798. /* print final buffer counts & size details */
  799. msm_vidc_print_buffer_info(inst);
  800. rc = venus_hfi_session_command(inst,
  801. HFI_CMD_RESUME,
  802. INPUT_PORT,
  803. HFI_PAYLOAD_NONE,
  804. NULL,
  805. 0);
  806. if (rc)
  807. return rc;
  808. } else {
  809. i_vpr_e(inst, "%s: unknown cmd %d\n", __func__, cmd);
  810. return -EINVAL;
  811. }
  812. return 0;
  813. }
  814. int msm_venc_streamoff_output(struct msm_vidc_inst *inst)
  815. {
  816. int rc = 0;
  817. struct msm_vidc_core *core;
  818. if (!inst || !inst->core || !inst->capabilities) {
  819. d_vpr_e("%s: invalid params\n", __func__);
  820. return -EINVAL;
  821. }
  822. core = inst->core;
  823. if (!core->capabilities) {
  824. i_vpr_e(inst, "%s: core capabilities is NULL\n", __func__);
  825. return -EINVAL;
  826. }
  827. /* restore LAYER_COUNT max allowed value */
  828. inst->capabilities->cap[ENH_LAYER_COUNT].max =
  829. core->capabilities[MAX_ENH_LAYER_COUNT].value;
  830. rc = msm_vidc_session_streamoff(inst, OUTPUT_PORT);
  831. if (rc)
  832. return rc;
  833. return 0;
  834. }
  835. int msm_venc_streamon_output(struct msm_vidc_inst *inst)
  836. {
  837. int rc = 0;
  838. if (!inst || !inst->core || !inst->capabilities) {
  839. d_vpr_e("%s: invalid params\n", __func__);
  840. return -EINVAL;
  841. }
  842. if (is_output_meta_enabled(inst) &&
  843. !inst->bufq[OUTPUT_META_PORT].vb2q->streaming) {
  844. i_vpr_e(inst,
  845. "%s: Meta port must be streamed on before data port\n",
  846. __func__);
  847. return -EINVAL;
  848. }
  849. rc = msm_vidc_adjust_v4l2_properties(inst);
  850. if (rc)
  851. goto error;
  852. rc = msm_venc_set_output_properties(inst);
  853. if (rc)
  854. goto error;
  855. rc = msm_vidc_set_v4l2_properties(inst);
  856. if (rc)
  857. goto error;
  858. rc = msm_venc_get_output_internal_buffers(inst);
  859. if (rc)
  860. goto error;
  861. rc = msm_venc_create_output_internal_buffers(inst);
  862. if (rc)
  863. goto error;
  864. rc = msm_venc_queue_output_internal_buffers(inst);
  865. if (rc)
  866. goto error;
  867. rc = msm_venc_set_internal_properties(inst);
  868. if (rc)
  869. goto error;
  870. rc = msm_venc_property_subscription(inst, OUTPUT_PORT);
  871. if (rc)
  872. goto error;
  873. rc = msm_venc_metadata_subscription(inst, OUTPUT_PORT);
  874. if (rc)
  875. goto error;
  876. rc = msm_vidc_session_streamon(inst, OUTPUT_PORT);
  877. if (rc)
  878. goto error;
  879. return 0;
  880. error:
  881. i_vpr_e(inst, "%s: failed\n", __func__);
  882. msm_venc_streamoff_output(inst);
  883. return rc;
  884. }
  885. int msm_venc_try_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  886. {
  887. int rc = 0;
  888. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  889. u32 pix_fmt;
  890. if (!inst) {
  891. d_vpr_e("%s: invalid params\n", __func__);
  892. return -EINVAL;
  893. }
  894. memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
  895. if (f->type == INPUT_MPLANE) {
  896. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  897. if (!pix_fmt) {
  898. i_vpr_e(inst, "%s: unsupported format, set current params\n", __func__);
  899. f->fmt.pix_mp.pixelformat = inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat;
  900. f->fmt.pix_mp.width = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  901. f->fmt.pix_mp.height = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  902. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  903. }
  904. } else if (f->type == OUTPUT_MPLANE) {
  905. pix_fmt = v4l2_codec_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  906. if (!pix_fmt) {
  907. i_vpr_e(inst, "%s: unsupported codec, set current params\n", __func__);
  908. f->fmt.pix_mp.width = inst->fmts[OUTPUT_PORT].fmt.pix_mp.width;
  909. f->fmt.pix_mp.height = inst->fmts[OUTPUT_PORT].fmt.pix_mp.height;
  910. f->fmt.pix_mp.pixelformat = inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat;
  911. pix_fmt = v4l2_codec_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  912. }
  913. } else {
  914. i_vpr_e(inst, "%s: invalid type %d\n", __func__, f->type);
  915. return -EINVAL;
  916. }
  917. if (pixmp->field == V4L2_FIELD_ANY)
  918. pixmp->field = V4L2_FIELD_NONE;
  919. pixmp->num_planes = 1;
  920. return rc;
  921. }
  922. int msm_venc_s_fmt_output(struct msm_vidc_inst *inst, struct v4l2_format *f)
  923. {
  924. int rc = 0;
  925. struct v4l2_format *fmt;
  926. struct msm_vidc_core *core;
  927. u32 codec_align;
  928. u32 width, height;
  929. if (!inst || !inst->core || !f) {
  930. d_vpr_e("%s: invalid params\n", __func__);
  931. return -EINVAL;
  932. }
  933. core = inst->core;
  934. msm_venc_try_fmt(inst, f);
  935. fmt = &inst->fmts[OUTPUT_PORT];
  936. if (fmt->fmt.pix_mp.pixelformat != f->fmt.pix_mp.pixelformat) {
  937. rc = msm_venc_codec_change(inst, f->fmt.pix_mp.pixelformat);
  938. if (rc)
  939. return rc;
  940. }
  941. fmt->type = OUTPUT_MPLANE;
  942. codec_align = (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEVC ||
  943. f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_HEIC) ? 32 : 16;
  944. /* use rotated width height if rotation is enabled */
  945. width = inst->compose.width;
  946. height = inst->compose.height;
  947. if (is_rotation_90_or_270(inst)) {
  948. width = inst->compose.height;
  949. height = inst->compose.width;
  950. }
  951. /* width, height is readonly for client */
  952. fmt->fmt.pix_mp.width = ALIGN(width, codec_align);
  953. fmt->fmt.pix_mp.height = ALIGN(height, codec_align);
  954. /* use grid dimension for image session */
  955. if (is_image_session(inst))
  956. fmt->fmt.pix_mp.width = fmt->fmt.pix_mp.height = HEIC_GRID_DIMENSION;
  957. fmt->fmt.pix_mp.num_planes = 1;
  958. fmt->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
  959. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  960. buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  961. /* video hw supports conversion to V4L2_COLORSPACE_REC709 only */
  962. if (f->fmt.pix_mp.colorspace != V4L2_COLORSPACE_DEFAULT &&
  963. f->fmt.pix_mp.colorspace != V4L2_COLORSPACE_REC709)
  964. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  965. fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
  966. fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
  967. fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  968. fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
  969. inst->buffers.output.min_count = call_session_op(core,
  970. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  971. inst->buffers.output.extra_count = call_session_op(core,
  972. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  973. if (inst->buffers.output.actual_count <
  974. inst->buffers.output.min_count +
  975. inst->buffers.output.extra_count) {
  976. inst->buffers.output.actual_count =
  977. inst->buffers.output.min_count +
  978. inst->buffers.output.extra_count;
  979. }
  980. inst->buffers.output.size =
  981. fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
  982. memcpy(f, fmt, sizeof(struct v4l2_format));
  983. /* reset metadata buffer size with updated resolution*/
  984. msm_vidc_update_meta_port_settings(inst);
  985. i_vpr_h(inst,
  986. "%s: type: OUTPUT, codec %s width %d height %d size %u min_count %d extra_count %d\n",
  987. __func__, v4l2_pixelfmt_name(fmt->fmt.pix_mp.pixelformat),
  988. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height,
  989. fmt->fmt.pix_mp.plane_fmt[0].sizeimage,
  990. inst->buffers.output.min_count,
  991. inst->buffers.output.extra_count);
  992. return rc;
  993. }
  994. static int msm_venc_s_fmt_output_meta(struct msm_vidc_inst *inst, struct v4l2_format *f)
  995. {
  996. int rc = 0;
  997. struct v4l2_format *fmt;
  998. struct msm_vidc_core *core;
  999. if (!inst || !inst->core) {
  1000. d_vpr_e("%s: invalid params\n", __func__);
  1001. return -EINVAL;
  1002. }
  1003. core = inst->core;
  1004. fmt = &inst->fmts[OUTPUT_META_PORT];
  1005. fmt->type = OUTPUT_META_PLANE;
  1006. fmt->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  1007. if (is_output_meta_enabled(inst)) {
  1008. fmt->fmt.meta.buffersize = call_session_op(core,
  1009. buffer_size, inst, MSM_VIDC_BUF_OUTPUT_META);
  1010. inst->buffers.output_meta.min_count =
  1011. inst->buffers.output.min_count;
  1012. inst->buffers.output_meta.extra_count =
  1013. inst->buffers.output.extra_count;
  1014. inst->buffers.output_meta.actual_count =
  1015. inst->buffers.output.actual_count;
  1016. inst->buffers.output_meta.size = fmt->fmt.meta.buffersize;
  1017. } else {
  1018. fmt->fmt.meta.buffersize = 0;
  1019. inst->buffers.output_meta.min_count = 0;
  1020. inst->buffers.output_meta.extra_count = 0;
  1021. inst->buffers.output_meta.actual_count = 0;
  1022. inst->buffers.output_meta.size = 0;
  1023. }
  1024. memcpy(f, fmt, sizeof(struct v4l2_format));
  1025. i_vpr_h(inst, "%s: type: OUTPUT_META, size %u min_count %d extra_count %d\n",
  1026. __func__, fmt->fmt.meta.buffersize,
  1027. inst->buffers.output_meta.min_count,
  1028. inst->buffers.output_meta.extra_count);
  1029. return rc;
  1030. }
  1031. static int msm_venc_s_fmt_input(struct msm_vidc_inst *inst, struct v4l2_format *f)
  1032. {
  1033. int rc = 0;
  1034. struct v4l2_format *fmt, *output_fmt;
  1035. struct msm_vidc_core *core;
  1036. u32 pix_fmt, width, height, size, bytesperline,
  1037. crop_width, crop_height;
  1038. if (!inst || !inst->core || !inst->capabilities) {
  1039. d_vpr_e("%s: invalid params\n", __func__);
  1040. return -EINVAL;
  1041. }
  1042. core = inst->core;
  1043. msm_venc_try_fmt(inst, f);
  1044. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  1045. msm_vidc_update_cap_value(inst, PIX_FMTS, pix_fmt, __func__);
  1046. if (is_rgba_colorformat(pix_fmt)) {
  1047. width = VIDEO_RGB_STRIDE_PIX(f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.width);
  1048. height = VIDEO_RGB_SCANLINES(f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.height);
  1049. crop_width = VIDEO_RGB_STRIDE_PIX(f->fmt.pix_mp.pixelformat, inst->crop.width);
  1050. crop_height = VIDEO_RGB_SCANLINES(f->fmt.pix_mp.pixelformat, inst->crop.height);
  1051. bytesperline =
  1052. VIDEO_RGB_STRIDE_BYTES(f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.width);
  1053. } else if (is_image_session(inst)) {
  1054. width = ALIGN(f->fmt.pix_mp.width, HEIC_GRID_DIMENSION);
  1055. height = ALIGN(f->fmt.pix_mp.height, HEIC_GRID_DIMENSION);
  1056. crop_width = ALIGN(inst->crop.width, HEIC_GRID_DIMENSION);
  1057. crop_height = ALIGN(inst->crop.height, HEIC_GRID_DIMENSION);
  1058. bytesperline = width * (is_10bit_colorformat(pix_fmt) ? 2 : 1);
  1059. } else {
  1060. width = VIDEO_Y_STRIDE_PIX(f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.width);
  1061. height = VIDEO_Y_SCANLINES(f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.height);
  1062. crop_width = VIDEO_Y_STRIDE_PIX(f->fmt.pix_mp.pixelformat, inst->crop.width);
  1063. crop_height = VIDEO_Y_SCANLINES(f->fmt.pix_mp.pixelformat, inst->crop.height);
  1064. bytesperline = VIDEO_Y_STRIDE_BYTES(f->fmt.pix_mp.pixelformat, f->fmt.pix_mp.width);
  1065. }
  1066. fmt = &inst->fmts[INPUT_PORT];
  1067. fmt->type = INPUT_MPLANE;
  1068. fmt->fmt.pix_mp.width = width;
  1069. fmt->fmt.pix_mp.height = height;
  1070. fmt->fmt.pix_mp.num_planes = 1;
  1071. fmt->fmt.pix_mp.pixelformat = f->fmt.pix_mp.pixelformat;
  1072. fmt->fmt.pix_mp.plane_fmt[0].bytesperline = bytesperline;
  1073. if (is_image_session(inst))
  1074. size = bytesperline * height * 3 / 2;
  1075. else
  1076. size = call_session_op(core, buffer_size, inst, MSM_VIDC_BUF_INPUT);
  1077. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = size;
  1078. /* update input port colorspace info */
  1079. fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
  1080. fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
  1081. fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  1082. fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
  1083. /*
  1084. * Update output port colorspace info.
  1085. * NOTE: If client needs CSC, then client needs to ensure setting
  1086. * output port color after setting input color info.
  1087. */
  1088. output_fmt = &inst->fmts[OUTPUT_PORT];
  1089. output_fmt->fmt.pix_mp.colorspace = fmt->fmt.pix_mp.colorspace;
  1090. output_fmt->fmt.pix_mp.xfer_func = fmt->fmt.pix_mp.xfer_func;
  1091. output_fmt->fmt.pix_mp.ycbcr_enc = fmt->fmt.pix_mp.ycbcr_enc;
  1092. output_fmt->fmt.pix_mp.quantization = fmt->fmt.pix_mp.quantization;
  1093. inst->buffers.input.min_count = call_session_op(core,
  1094. min_count, inst, MSM_VIDC_BUF_INPUT);
  1095. inst->buffers.input.extra_count = call_session_op(core,
  1096. extra_count, inst, MSM_VIDC_BUF_INPUT);
  1097. if (inst->buffers.input.actual_count <
  1098. inst->buffers.input.min_count +
  1099. inst->buffers.input.extra_count) {
  1100. inst->buffers.input.actual_count =
  1101. inst->buffers.input.min_count +
  1102. inst->buffers.input.extra_count;
  1103. }
  1104. inst->buffers.input.size = size;
  1105. if (fmt->fmt.pix_mp.width != crop_width ||
  1106. fmt->fmt.pix_mp.height != crop_height) {
  1107. struct v4l2_format *output_fmt;
  1108. /* reset crop dimensions with updated resolution */
  1109. inst->crop.top = inst->crop.left = 0;
  1110. inst->crop.width = f->fmt.pix_mp.width;
  1111. inst->crop.height = f->fmt.pix_mp.height;
  1112. /* reset compose dimensions with updated resolution */
  1113. inst->compose.top = inst->compose.left = 0;
  1114. inst->compose.width = f->fmt.pix_mp.width;
  1115. inst->compose.height = f->fmt.pix_mp.height;
  1116. output_fmt = &inst->fmts[OUTPUT_PORT];
  1117. rc = msm_venc_s_fmt_output(inst, output_fmt);
  1118. if (rc)
  1119. return rc;
  1120. }
  1121. memcpy(f, fmt, sizeof(struct v4l2_format));
  1122. /* reset metadata buffer size with updated resolution*/
  1123. msm_vidc_update_meta_port_settings(inst);
  1124. i_vpr_h(inst,
  1125. "%s: type: INPUT, format %s width %d height %d size %u min_count %d extra_count %d\n",
  1126. __func__, v4l2_pixelfmt_name(fmt->fmt.pix_mp.pixelformat),
  1127. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height,
  1128. fmt->fmt.pix_mp.plane_fmt[0].sizeimage,
  1129. inst->buffers.input.min_count,
  1130. inst->buffers.input.extra_count);
  1131. return rc;
  1132. }
  1133. static int msm_venc_s_fmt_input_meta(struct msm_vidc_inst *inst, struct v4l2_format *f)
  1134. {
  1135. int rc = 0;
  1136. struct v4l2_format *fmt;
  1137. struct msm_vidc_core *core;
  1138. if (!inst || !inst->core) {
  1139. d_vpr_e("%s: invalid params\n", __func__);
  1140. return -EINVAL;
  1141. }
  1142. core = inst->core;
  1143. fmt = &inst->fmts[INPUT_META_PORT];
  1144. fmt->type = INPUT_META_PLANE;
  1145. fmt->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  1146. if (is_input_meta_enabled(inst)) {
  1147. fmt->fmt.meta.buffersize = call_session_op(core,
  1148. buffer_size, inst, MSM_VIDC_BUF_INPUT_META);
  1149. inst->buffers.input_meta.min_count =
  1150. inst->buffers.input.min_count;
  1151. inst->buffers.input_meta.extra_count =
  1152. inst->buffers.input.extra_count;
  1153. inst->buffers.input_meta.actual_count =
  1154. inst->buffers.input.actual_count;
  1155. inst->buffers.input_meta.size = fmt->fmt.meta.buffersize;
  1156. } else {
  1157. fmt->fmt.meta.buffersize = 0;
  1158. inst->buffers.input_meta.min_count = 0;
  1159. inst->buffers.input_meta.extra_count = 0;
  1160. inst->buffers.input_meta.actual_count = 0;
  1161. inst->buffers.input_meta.size = 0;
  1162. }
  1163. memcpy(f, fmt, sizeof(struct v4l2_format));
  1164. i_vpr_h(inst, "%s: type: INPUT_META, size %u min_count %d extra_count %d\n",
  1165. __func__, fmt->fmt.meta.buffersize,
  1166. inst->buffers.input_meta.min_count,
  1167. inst->buffers.input_meta.extra_count);
  1168. return rc;
  1169. }
  1170. int msm_venc_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  1171. {
  1172. int rc = 0;
  1173. if (!inst) {
  1174. d_vpr_e("%s: invalid params\n", __func__);
  1175. return -EINVAL;
  1176. }
  1177. if (f->type == INPUT_MPLANE) {
  1178. rc = msm_venc_s_fmt_input(inst, f);
  1179. if (rc)
  1180. goto exit;
  1181. } else if (f->type == INPUT_META_PLANE) {
  1182. rc = msm_venc_s_fmt_input_meta(inst, f);
  1183. if (rc)
  1184. goto exit;
  1185. } else if (f->type == OUTPUT_MPLANE) {
  1186. rc = msm_venc_s_fmt_output(inst, f);
  1187. if (rc)
  1188. goto exit;
  1189. } else if (f->type == OUTPUT_META_PLANE) {
  1190. rc = msm_venc_s_fmt_output_meta(inst, f);
  1191. if (rc)
  1192. goto exit;
  1193. } else {
  1194. i_vpr_e(inst, "%s: invalid type %d\n", __func__, f->type);
  1195. rc = -EINVAL;
  1196. goto exit;
  1197. }
  1198. exit:
  1199. if (rc)
  1200. i_vpr_e(inst, "%s: failed\n", __func__);
  1201. return rc;
  1202. }
  1203. int msm_venc_g_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  1204. {
  1205. int rc = 0;
  1206. int port;
  1207. if (!inst) {
  1208. d_vpr_e("%s: invalid params\n", __func__);
  1209. return -EINVAL;
  1210. }
  1211. port = v4l2_type_to_driver_port(inst, f->type, __func__);
  1212. if (port < 0)
  1213. return -EINVAL;
  1214. memcpy(f, &inst->fmts[port], sizeof(struct v4l2_format));
  1215. return rc;
  1216. }
  1217. int msm_venc_s_selection(struct msm_vidc_inst* inst, struct v4l2_selection* s)
  1218. {
  1219. int rc = 0;
  1220. struct v4l2_format *output_fmt;
  1221. if (!inst || !s) {
  1222. d_vpr_e("%s: invalid params\n", __func__);
  1223. return -EINVAL;
  1224. }
  1225. if (s->type != INPUT_MPLANE && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1226. i_vpr_e(inst, "%s: invalid type %d\n", __func__, s->type);
  1227. return -EINVAL;
  1228. }
  1229. switch (s->target) {
  1230. case V4L2_SEL_TGT_CROP:
  1231. if (s->r.left || s->r.top) {
  1232. i_vpr_h(inst, "%s: unsupported top %d or left %d\n",
  1233. __func__, s->r.left, s->r.top);
  1234. s->r.left = s->r.top = 0;
  1235. }
  1236. if (s->r.width > inst->fmts[INPUT_PORT].fmt.pix_mp.width) {
  1237. i_vpr_h(inst, "%s: unsupported width %d, fmt width %d\n",
  1238. __func__, s->r.width,
  1239. inst->fmts[INPUT_PORT].fmt.pix_mp.width);
  1240. s->r.width = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  1241. }
  1242. if (s->r.height > inst->fmts[INPUT_PORT].fmt.pix_mp.height) {
  1243. i_vpr_h(inst, "%s: unsupported height %d, fmt height %d\n",
  1244. __func__, s->r.height,
  1245. inst->fmts[INPUT_PORT].fmt.pix_mp.height);
  1246. s->r.height = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  1247. }
  1248. inst->crop.left = s->r.left;
  1249. inst->crop.top = s->r.top;
  1250. inst->crop.width = s->r.width;
  1251. inst->crop.height = s->r.height;
  1252. /* adjust compose such that it is within crop */
  1253. inst->compose.left = inst->crop.left;
  1254. inst->compose.top = inst->crop.top;
  1255. inst->compose.width = inst->crop.width;
  1256. inst->compose.height = inst->crop.height;
  1257. /* update output format based on new crop dimensions */
  1258. output_fmt = &inst->fmts[OUTPUT_PORT];
  1259. rc = msm_venc_s_fmt_output(inst, output_fmt);
  1260. if (rc)
  1261. return rc;
  1262. break;
  1263. case V4L2_SEL_TGT_COMPOSE:
  1264. if (s->r.left < inst->crop.left) {
  1265. i_vpr_e(inst,
  1266. "%s: compose left (%d) less than crop left (%d)\n",
  1267. __func__, s->r.left, inst->crop.left);
  1268. s->r.left = inst->crop.left;
  1269. }
  1270. if (s->r.top < inst->crop.top) {
  1271. i_vpr_e(inst,
  1272. "%s: compose top (%d) less than crop top (%d)\n",
  1273. __func__, s->r.top, inst->crop.top);
  1274. s->r.top = inst->crop.top;
  1275. }
  1276. if (s->r.width > inst->crop.width) {
  1277. i_vpr_e(inst,
  1278. "%s: compose width (%d) greate than crop width (%d)\n",
  1279. __func__, s->r.width, inst->crop.width);
  1280. s->r.width = inst->crop.width;
  1281. }
  1282. if (s->r.height > inst->crop.height) {
  1283. i_vpr_e(inst,
  1284. "%s: compose height (%d) greate than crop height (%d)\n",
  1285. __func__, s->r.height, inst->crop.height);
  1286. s->r.height = inst->crop.height;
  1287. }
  1288. inst->compose.left = s->r.left;
  1289. inst->compose.top = s->r.top;
  1290. inst->compose.width = s->r.width;
  1291. inst->compose.height= s->r.height;
  1292. if (is_scaling_enabled(inst)) {
  1293. i_vpr_h(inst,
  1294. "%s: scaling enabled, crop: l %d t %d w %d h %d compose: l %d t %d w %d h %d\n",
  1295. __func__, inst->crop.left, inst->crop.top,
  1296. inst->crop.width, inst->crop.height,
  1297. inst->compose.left, inst->compose.top,
  1298. inst->compose.width, inst->compose.height);
  1299. }
  1300. /* update output format based on new compose dimensions */
  1301. output_fmt = &inst->fmts[OUTPUT_PORT];
  1302. rc = msm_venc_s_fmt_output(inst, output_fmt);
  1303. if (rc)
  1304. return rc;
  1305. break;
  1306. default:
  1307. i_vpr_e(inst, "%s: invalid target %d\n",
  1308. __func__, s->target);
  1309. rc = -EINVAL;
  1310. break;
  1311. }
  1312. if (!rc)
  1313. i_vpr_h(inst, "%s: target %d, r [%d, %d, %d, %d]\n",
  1314. __func__, s->target, s->r.top, s->r.left,
  1315. s->r.width, s->r.height);
  1316. return rc;
  1317. }
  1318. int msm_venc_g_selection(struct msm_vidc_inst* inst, struct v4l2_selection* s)
  1319. {
  1320. int rc = 0;
  1321. if (!inst || !s) {
  1322. d_vpr_e("%s: invalid params\n", __func__);
  1323. return -EINVAL;
  1324. }
  1325. if (s->type != INPUT_MPLANE && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
  1326. i_vpr_e(inst, "%s: invalid type %d\n", __func__, s->type);
  1327. return -EINVAL;
  1328. }
  1329. switch (s->target) {
  1330. case V4L2_SEL_TGT_CROP_BOUNDS:
  1331. case V4L2_SEL_TGT_CROP_DEFAULT:
  1332. case V4L2_SEL_TGT_CROP:
  1333. s->r.left = inst->crop.left;
  1334. s->r.top = inst->crop.top;
  1335. s->r.width = inst->crop.width;
  1336. s->r.height = inst->crop.height;
  1337. break;
  1338. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1339. case V4L2_SEL_TGT_COMPOSE_PADDED:
  1340. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1341. case V4L2_SEL_TGT_COMPOSE:
  1342. s->r.left = inst->compose.left;
  1343. s->r.top = inst->compose.top;
  1344. s->r.width = inst->compose.width;
  1345. s->r.height = inst->compose.height;
  1346. break;
  1347. default:
  1348. i_vpr_e(inst, "%s: invalid target %d\n",
  1349. __func__, s->target);
  1350. rc = -EINVAL;
  1351. break;
  1352. }
  1353. if (!rc)
  1354. i_vpr_h(inst, "%s: target %d, r [%d, %d, %d, %d]\n",
  1355. __func__, s->target, s->r.top, s->r.left,
  1356. s->r.width, s->r.height);
  1357. return rc;
  1358. }
  1359. int msm_venc_s_param(struct msm_vidc_inst *inst,
  1360. struct v4l2_streamparm *s_parm)
  1361. {
  1362. int rc = 0;
  1363. struct msm_vidc_inst_capability *capability = NULL;
  1364. struct v4l2_fract *timeperframe = NULL;
  1365. u32 q16_rate, max_rate, default_rate;
  1366. u64 us_per_frame = 0, input_rate = 0;
  1367. bool is_frame_rate = false;
  1368. if (!inst || !s_parm) {
  1369. d_vpr_e("%s: invalid params\n", __func__);
  1370. return -EINVAL;
  1371. }
  1372. capability = inst->capabilities;
  1373. if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1374. timeperframe = &s_parm->parm.output.timeperframe;
  1375. max_rate = capability->cap[OPERATING_RATE].max >> 16;
  1376. default_rate = capability->cap[OPERATING_RATE].value >> 16;
  1377. s_parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  1378. } else {
  1379. timeperframe = &s_parm->parm.capture.timeperframe;
  1380. is_frame_rate = true;
  1381. max_rate = capability->cap[FRAME_RATE].max >> 16;
  1382. default_rate = capability->cap[FRAME_RATE].value >> 16;
  1383. s_parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1384. }
  1385. if (!timeperframe->denominator || !timeperframe->numerator) {
  1386. i_vpr_e(inst, "%s: type %s, invalid rate, update with default\n",
  1387. __func__, v4l2_type_name(s_parm->type));
  1388. if (!timeperframe->numerator)
  1389. timeperframe->numerator = 1;
  1390. if (!timeperframe->denominator)
  1391. timeperframe->denominator = default_rate;
  1392. }
  1393. us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
  1394. do_div(us_per_frame, timeperframe->denominator);
  1395. if (!us_per_frame) {
  1396. i_vpr_e(inst, "%s: us_per_frame is zero\n", __func__);
  1397. rc = -EINVAL;
  1398. goto exit;
  1399. }
  1400. input_rate = (u64)USEC_PER_SEC;
  1401. do_div(input_rate, us_per_frame);
  1402. i_vpr_h(inst, "%s: type %s, %s value %llu\n",
  1403. __func__, v4l2_type_name(s_parm->type),
  1404. is_frame_rate ? "frame rate" : "operating rate", input_rate);
  1405. q16_rate = (u32)input_rate << 16;
  1406. msm_vidc_update_cap_value(inst, is_frame_rate ? FRAME_RATE : OPERATING_RATE,
  1407. q16_rate, __func__);
  1408. if (is_realtime_session(inst) &&
  1409. ((s_parm->type == INPUT_MPLANE && inst->bufq[INPUT_PORT].vb2q->streaming) ||
  1410. (s_parm->type == OUTPUT_MPLANE && inst->bufq[OUTPUT_PORT].vb2q->streaming))) {
  1411. rc = msm_vidc_check_core_mbps(inst);
  1412. if (rc) {
  1413. i_vpr_e(inst, "%s: unsupported load\n", __func__);
  1414. goto reset_rate;
  1415. }
  1416. rc = input_rate > max_rate;
  1417. if (rc) {
  1418. i_vpr_e(inst, "%s: unsupported rate %llu, max %u\n", __func__,
  1419. input_rate, max_rate);
  1420. rc = -ENOMEM;
  1421. goto reset_rate;
  1422. }
  1423. }
  1424. inst->priority_level = MSM_VIDC_PRIORITY_HIGH;
  1425. if (is_frame_rate)
  1426. capability->cap[FRAME_RATE].flags |= CAP_FLAG_CLIENT_SET;
  1427. else
  1428. capability->cap[OPERATING_RATE].flags |= CAP_FLAG_CLIENT_SET;
  1429. /*
  1430. * In static case, frame rate is set via
  1431. * inst database set function mentioned in
  1432. * FRAME_RATE cap id.
  1433. * In dynamic case, frame rate is set like below.
  1434. */
  1435. if (inst->bufq[OUTPUT_PORT].vb2q->streaming) {
  1436. rc = venus_hfi_session_property(inst,
  1437. HFI_PROP_FRAME_RATE,
  1438. HFI_HOST_FLAGS_NONE,
  1439. HFI_PORT_BITSTREAM,
  1440. HFI_PAYLOAD_Q16,
  1441. &q16_rate,
  1442. sizeof(u32));
  1443. if (rc) {
  1444. i_vpr_e(inst,
  1445. "%s: failed to set frame rate to fw\n", __func__);
  1446. goto exit;
  1447. }
  1448. inst->auto_framerate = q16_rate;
  1449. }
  1450. return 0;
  1451. reset_rate:
  1452. if (rc) {
  1453. i_vpr_e(inst, "%s: setting rate %llu failed, reset to %u\n", __func__,
  1454. input_rate, default_rate);
  1455. msm_vidc_update_cap_value(inst, is_frame_rate ? FRAME_RATE : OPERATING_RATE,
  1456. default_rate << 16, __func__);
  1457. }
  1458. exit:
  1459. return rc;
  1460. }
  1461. int msm_venc_g_param(struct msm_vidc_inst *inst,
  1462. struct v4l2_streamparm *s_parm)
  1463. {
  1464. struct msm_vidc_inst_capability *capability = NULL;
  1465. struct v4l2_fract *timeperframe = NULL;
  1466. if (!inst || !s_parm) {
  1467. d_vpr_e("%s: invalid params\n", __func__);
  1468. return -EINVAL;
  1469. }
  1470. capability = inst->capabilities;
  1471. if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1472. timeperframe = &s_parm->parm.output.timeperframe;
  1473. timeperframe->numerator = 1;
  1474. timeperframe->denominator =
  1475. capability->cap[OPERATING_RATE].value >> 16;
  1476. s_parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
  1477. } else {
  1478. timeperframe = &s_parm->parm.capture.timeperframe;
  1479. timeperframe->numerator = 1;
  1480. timeperframe->denominator =
  1481. capability->cap[FRAME_RATE].value >> 16;
  1482. s_parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1483. }
  1484. i_vpr_h(inst, "%s: type %s, num %u denom %u\n",
  1485. __func__, v4l2_type_name(s_parm->type), timeperframe->numerator,
  1486. timeperframe->denominator);
  1487. return 0;
  1488. }
  1489. int msm_venc_subscribe_event(struct msm_vidc_inst *inst,
  1490. const struct v4l2_event_subscription *sub)
  1491. {
  1492. int rc = 0;
  1493. if (!inst || !sub) {
  1494. d_vpr_e("%s: invalid params\n", __func__);
  1495. return -EINVAL;
  1496. }
  1497. switch (sub->type) {
  1498. case V4L2_EVENT_EOS:
  1499. rc = v4l2_event_subscribe(&inst->event_handler, sub, MAX_EVENTS, NULL);
  1500. break;
  1501. case V4L2_EVENT_CTRL:
  1502. rc = v4l2_ctrl_subscribe_event(&inst->event_handler, sub);
  1503. break;
  1504. default:
  1505. i_vpr_e(inst, "%s: invalid type %d id %d\n", __func__, sub->type, sub->id);
  1506. return -EINVAL;
  1507. }
  1508. if (rc)
  1509. i_vpr_e(inst, "%s: failed, type %d id %d\n",
  1510. __func__, sub->type, sub->id);
  1511. return rc;
  1512. }
  1513. int msm_venc_enum_fmt(struct msm_vidc_inst *inst, struct v4l2_fmtdesc *f)
  1514. {
  1515. int rc = 0;
  1516. struct msm_vidc_core *core;
  1517. u32 array[32] = {0};
  1518. u32 i = 0;
  1519. if (!inst || !inst->core || !inst->capabilities || !f ||
  1520. f->index >= ARRAY_SIZE(array)) {
  1521. d_vpr_e("%s: invalid params\n", __func__);
  1522. return -EINVAL;
  1523. }
  1524. core = inst->core;
  1525. if (f->type == OUTPUT_MPLANE) {
  1526. u32 codecs = core->capabilities[ENC_CODECS].value;
  1527. u32 idx = 0;
  1528. for (i = 0; i <= 31; i++) {
  1529. if (codecs & BIT(i)) {
  1530. if (idx >= ARRAY_SIZE(array))
  1531. break;
  1532. array[idx] = codecs & BIT(i);
  1533. idx++;
  1534. }
  1535. }
  1536. if (!array[f->index])
  1537. return -EINVAL;
  1538. f->pixelformat = v4l2_codec_from_driver(array[f->index],
  1539. __func__);
  1540. if (!f->pixelformat)
  1541. return -EINVAL;
  1542. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  1543. strlcpy(f->description, "codec", sizeof(f->description));
  1544. } else if (f->type == INPUT_MPLANE) {
  1545. u32 formats = inst->capabilities->cap[PIX_FMTS].step_or_mask;
  1546. u32 idx = 0;
  1547. for (i = 0; i <= 31; i++) {
  1548. if (formats & BIT(i)) {
  1549. if (idx >= ARRAY_SIZE(array))
  1550. break;
  1551. array[idx] = formats & BIT(i);
  1552. idx++;
  1553. }
  1554. }
  1555. if (!array[f->index])
  1556. return -EINVAL;
  1557. f->pixelformat = v4l2_colorformat_from_driver(array[f->index],
  1558. __func__);
  1559. if (!f->pixelformat)
  1560. return -EINVAL;
  1561. strlcpy(f->description, "colorformat", sizeof(f->description));
  1562. } else if (f->type == INPUT_META_PLANE || f->type == OUTPUT_META_PLANE) {
  1563. if (!f->index) {
  1564. f->pixelformat = V4L2_META_FMT_VIDC;
  1565. strlcpy(f->description, "metadata", sizeof(f->description));
  1566. } else {
  1567. return -EINVAL;
  1568. }
  1569. }
  1570. memset(f->reserved, 0, sizeof(f->reserved));
  1571. i_vpr_h(inst, "%s: index %d, %s: %s, flags %#x\n",
  1572. __func__, f->index, f->description,
  1573. v4l2_pixelfmt_name(f->pixelformat), f->flags);
  1574. return rc;
  1575. }
  1576. int msm_venc_inst_init(struct msm_vidc_inst *inst)
  1577. {
  1578. int rc = 0;
  1579. struct msm_vidc_core *core;
  1580. struct v4l2_format *f;
  1581. if (!inst || !inst->core) {
  1582. d_vpr_e("%s: invalid params\n", __func__);
  1583. return -EINVAL;
  1584. }
  1585. i_vpr_h(inst, "%s()\n", __func__);
  1586. core = inst->core;
  1587. if (core->capabilities[DCVS].value)
  1588. inst->power.dcvs_mode = true;
  1589. f = &inst->fmts[OUTPUT_PORT];
  1590. f->type = OUTPUT_MPLANE;
  1591. f->fmt.pix_mp.width = DEFAULT_WIDTH;
  1592. f->fmt.pix_mp.height = DEFAULT_HEIGHT;
  1593. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  1594. f->fmt.pix_mp.num_planes = 1;
  1595. f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
  1596. f->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  1597. buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  1598. f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  1599. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  1600. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1601. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1602. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  1603. inst->buffers.output.min_count = call_session_op(core,
  1604. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  1605. inst->buffers.output.extra_count = call_session_op(core,
  1606. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  1607. inst->buffers.output.actual_count =
  1608. inst->buffers.output.min_count +
  1609. inst->buffers.output.extra_count;
  1610. inst->buffers.output.size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
  1611. inst->crop.left = inst->crop.top = 0;
  1612. inst->crop.width = f->fmt.pix_mp.width;
  1613. inst->crop.height = f->fmt.pix_mp.height;
  1614. inst->compose.left = inst->compose.top = 0;
  1615. inst->compose.width = f->fmt.pix_mp.width;
  1616. inst->compose.height = f->fmt.pix_mp.height;
  1617. f = &inst->fmts[OUTPUT_META_PORT];
  1618. f->type = OUTPUT_META_PLANE;
  1619. f->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  1620. f->fmt.meta.buffersize = 0;
  1621. inst->buffers.output_meta.min_count = 0;
  1622. inst->buffers.output_meta.extra_count = 0;
  1623. inst->buffers.output_meta.actual_count = 0;
  1624. inst->buffers.output_meta.size = 0;
  1625. f = &inst->fmts[INPUT_PORT];
  1626. f->type = INPUT_MPLANE;
  1627. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_VIDC_NV12C;
  1628. f->fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(f->fmt.pix_mp.pixelformat,
  1629. DEFAULT_WIDTH);
  1630. f->fmt.pix_mp.height = VIDEO_Y_SCANLINES(f->fmt.pix_mp.pixelformat,
  1631. DEFAULT_HEIGHT);
  1632. f->fmt.pix_mp.num_planes = 1;
  1633. f->fmt.pix_mp.plane_fmt[0].bytesperline =
  1634. VIDEO_Y_STRIDE_BYTES(f->fmt.pix_mp.pixelformat,
  1635. DEFAULT_WIDTH);
  1636. f->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  1637. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  1638. f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  1639. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  1640. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1641. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1642. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  1643. inst->buffers.input.min_count = call_session_op(core,
  1644. min_count, inst, MSM_VIDC_BUF_INPUT);
  1645. inst->buffers.input.extra_count = call_session_op(core,
  1646. extra_count, inst, MSM_VIDC_BUF_INPUT);
  1647. inst->buffers.input.actual_count =
  1648. inst->buffers.input.min_count +
  1649. inst->buffers.input.extra_count;
  1650. inst->buffers.input.size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
  1651. f = &inst->fmts[INPUT_META_PORT];
  1652. f->type = INPUT_META_PLANE;
  1653. f->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  1654. f->fmt.meta.buffersize = 0;
  1655. inst->buffers.input_meta.min_count = 0;
  1656. inst->buffers.input_meta.extra_count = 0;
  1657. inst->buffers.input_meta.actual_count = 0;
  1658. inst->buffers.input_meta.size = 0;
  1659. inst->priority_level = MSM_VIDC_PRIORITY_LOW;
  1660. inst->hfi_rc_type = HFI_RC_VBR_CFR;
  1661. inst->hfi_layer_type = HFI_HIER_P_SLIDING_WINDOW;
  1662. rc = msm_venc_codec_change(inst,
  1663. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat);
  1664. if (rc)
  1665. return rc;
  1666. return rc;
  1667. }
  1668. int msm_venc_inst_deinit(struct msm_vidc_inst *inst)
  1669. {
  1670. int rc = 0;
  1671. if (!inst) {
  1672. d_vpr_e("%s: invalid params\n", __func__);
  1673. return -EINVAL;
  1674. }
  1675. rc = msm_vidc_ctrl_deinit(inst);
  1676. if (rc)
  1677. return rc;
  1678. return rc;
  1679. }