msm_venc.c 51 KB

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