msm_venc.c 49 KB

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