msm_venc.c 51 KB

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