msm_venc.c 50 KB

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