msm_venc.c 50 KB

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