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