msm_venc.c 52 KB

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