msm_venc.c 53 KB

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