msm_vdec.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2022, The Linux Foundation. All rights reserved.
  4. */
  5. #include <media/v4l2_vidc_extensions.h>
  6. #include "msm_media_info.h"
  7. #include <linux/v4l2-common.h>
  8. #include "msm_vdec.h"
  9. #include "msm_vidc_core.h"
  10. #include "msm_vidc_inst.h"
  11. #include "msm_vidc_driver.h"
  12. #include "msm_vidc_internal.h"
  13. #include "msm_vidc_platform.h"
  14. #include "msm_vidc_control.h"
  15. #include "msm_vidc_debug.h"
  16. #include "msm_vidc_power.h"
  17. #include "msm_vidc_control.h"
  18. #include "msm_vidc_memory.h"
  19. #include "venus_hfi.h"
  20. #include "hfi_packet.h"
  21. /* TODO: update based on clips */
  22. #define MAX_DEC_BATCH_SIZE 6
  23. #define SKIP_BATCH_WINDOW 100
  24. static const u32 msm_vdec_subscribe_for_psc_avc[] = {
  25. HFI_PROP_BITSTREAM_RESOLUTION,
  26. HFI_PROP_CROP_OFFSETS,
  27. HFI_PROP_CODED_FRAMES,
  28. HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT,
  29. HFI_PROP_PIC_ORDER_CNT_TYPE,
  30. HFI_PROP_PROFILE,
  31. HFI_PROP_LEVEL,
  32. HFI_PROP_SIGNAL_COLOR_INFO,
  33. };
  34. static const u32 msm_vdec_subscribe_for_psc_hevc[] = {
  35. HFI_PROP_BITSTREAM_RESOLUTION,
  36. HFI_PROP_CROP_OFFSETS,
  37. HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
  38. HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT,
  39. HFI_PROP_PROFILE,
  40. HFI_PROP_LEVEL,
  41. HFI_PROP_TIER,
  42. HFI_PROP_SIGNAL_COLOR_INFO,
  43. };
  44. static const u32 msm_vdec_subscribe_for_psc_vp9[] = {
  45. HFI_PROP_BITSTREAM_RESOLUTION,
  46. HFI_PROP_CROP_OFFSETS,
  47. HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
  48. HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT,
  49. HFI_PROP_PROFILE,
  50. HFI_PROP_LEVEL,
  51. };
  52. static const u32 msm_vdec_subscribe_for_psc_av1[] = {
  53. HFI_PROP_BITSTREAM_RESOLUTION,
  54. HFI_PROP_CROP_OFFSETS,
  55. HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
  56. HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT,
  57. HFI_PROP_AV1_FILM_GRAIN_PRESENT,
  58. HFI_PROP_AV1_SUPER_BLOCK_ENABLED,
  59. HFI_PROP_PROFILE,
  60. HFI_PROP_LEVEL,
  61. HFI_PROP_TIER,
  62. };
  63. static const u32 msm_vdec_input_subscribe_for_properties[] = {
  64. HFI_PROP_NO_OUTPUT,
  65. };
  66. static const u32 msm_vdec_output_subscribe_for_properties[] = {
  67. HFI_PROP_WORST_COMPRESSION_RATIO,
  68. HFI_PROP_WORST_COMPLEXITY_FACTOR,
  69. HFI_PROP_PICTURE_TYPE,
  70. HFI_PROP_DPB_LIST,
  71. HFI_PROP_CABAC_SESSION,
  72. };
  73. static const u32 msm_vdec_internal_buffer_type[] = {
  74. MSM_VIDC_BUF_BIN,
  75. MSM_VIDC_BUF_COMV,
  76. MSM_VIDC_BUF_NON_COMV,
  77. MSM_VIDC_BUF_LINE,
  78. };
  79. struct msm_vdec_prop_type_handle {
  80. u32 type;
  81. int (*handle)(struct msm_vidc_inst *inst, enum msm_vidc_port_type port);
  82. };
  83. static int msm_vdec_codec_change(struct msm_vidc_inst *inst, u32 v4l2_codec)
  84. {
  85. int rc = 0;
  86. if (inst->codec && inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat == v4l2_codec)
  87. return 0;
  88. i_vpr_h(inst, "%s: codec changed from %s to %s\n",
  89. __func__, v4l2_pixelfmt_name(inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat),
  90. v4l2_pixelfmt_name(v4l2_codec));
  91. inst->codec = v4l2_codec_to_driver(v4l2_codec, __func__);
  92. inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat = v4l2_codec;
  93. rc = msm_vidc_update_debug_str(inst);
  94. if (rc)
  95. goto exit;
  96. rc = msm_vidc_get_inst_capability(inst);
  97. if (rc)
  98. goto exit;
  99. rc = msm_vidc_ctrl_deinit(inst);
  100. if (rc)
  101. goto exit;
  102. rc = msm_vidc_ctrl_init(inst);
  103. if(rc)
  104. goto exit;
  105. rc = msm_vidc_update_buffer_count(inst, INPUT_PORT);
  106. if (rc)
  107. return rc;
  108. rc = msm_vidc_update_buffer_count(inst, OUTPUT_PORT);
  109. if (rc)
  110. return rc;
  111. exit:
  112. return rc;
  113. }
  114. static int msm_vdec_set_bitstream_resolution(struct msm_vidc_inst *inst,
  115. enum msm_vidc_port_type port)
  116. {
  117. int rc = 0;
  118. u32 resolution;
  119. resolution = inst->fmts[INPUT_PORT].fmt.pix_mp.width << 16 |
  120. inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  121. i_vpr_h(inst, "%s: width: %d height: %d\n", __func__,
  122. inst->fmts[INPUT_PORT].fmt.pix_mp.width,
  123. inst->fmts[INPUT_PORT].fmt.pix_mp.height);
  124. inst->subcr_params[port].bitstream_resolution = resolution;
  125. rc = venus_hfi_session_property(inst,
  126. HFI_PROP_BITSTREAM_RESOLUTION,
  127. HFI_HOST_FLAGS_NONE,
  128. get_hfi_port(inst, port),
  129. HFI_PAYLOAD_U32,
  130. &resolution,
  131. sizeof(u32));
  132. if (rc) {
  133. i_vpr_e(inst, "%s: set property failed\n", __func__);
  134. return rc;
  135. }
  136. return rc;
  137. }
  138. static int msm_vdec_set_linear_stride_scanline(struct msm_vidc_inst *inst)
  139. {
  140. int rc = 0;
  141. u32 stride_y, scanline_y, stride_uv, scanline_uv;
  142. u32 payload[2];
  143. if (inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat !=
  144. V4L2_PIX_FMT_NV12 &&
  145. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat !=
  146. V4L2_PIX_FMT_VIDC_P010 &&
  147. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat !=
  148. V4L2_PIX_FMT_NV21)
  149. return 0;
  150. stride_y = inst->fmts[OUTPUT_PORT].fmt.pix_mp.width;
  151. scanline_y = inst->fmts[OUTPUT_PORT].fmt.pix_mp.height;
  152. stride_uv = stride_y;
  153. scanline_uv = scanline_y / 2;
  154. payload[0] = stride_y << 16 | scanline_y;
  155. payload[1] = stride_uv << 16 | scanline_uv;
  156. i_vpr_h(inst, "%s: stride_y: %d scanline_y: %d "
  157. "stride_uv: %d, scanline_uv: %d", __func__,
  158. stride_y, scanline_y, stride_uv, scanline_uv);
  159. rc = venus_hfi_session_property(inst,
  160. HFI_PROP_LINEAR_STRIDE_SCANLINE,
  161. HFI_HOST_FLAGS_NONE,
  162. get_hfi_port(inst, OUTPUT_PORT),
  163. HFI_PAYLOAD_U64,
  164. &payload,
  165. sizeof(u64));
  166. if (rc) {
  167. i_vpr_e(inst, "%s: set property failed\n", __func__);
  168. return rc;
  169. }
  170. return rc;
  171. }
  172. static int msm_vdec_set_crop_offsets(struct msm_vidc_inst *inst,
  173. enum msm_vidc_port_type port)
  174. {
  175. int rc = 0;
  176. u32 left_offset, top_offset, right_offset, bottom_offset;
  177. u32 payload[2] = {0};
  178. left_offset = inst->crop.left;
  179. top_offset = inst->crop.top;
  180. right_offset = (inst->fmts[INPUT_PORT].fmt.pix_mp.width -
  181. inst->crop.width);
  182. bottom_offset = (inst->fmts[INPUT_PORT].fmt.pix_mp.height -
  183. inst->crop.height);
  184. payload[0] = left_offset << 16 | top_offset;
  185. payload[1] = right_offset << 16 | bottom_offset;
  186. i_vpr_h(inst, "%s: left_offset: %d top_offset: %d "
  187. "right_offset: %d bottom_offset: %d", __func__,
  188. left_offset, top_offset, right_offset, bottom_offset);
  189. inst->subcr_params[port].crop_offsets[0] = payload[0];
  190. inst->subcr_params[port].crop_offsets[1] = payload[1];
  191. rc = venus_hfi_session_property(inst,
  192. HFI_PROP_CROP_OFFSETS,
  193. HFI_HOST_FLAGS_NONE,
  194. get_hfi_port(inst, port),
  195. HFI_PAYLOAD_64_PACKED,
  196. &payload,
  197. sizeof(u64));
  198. if (rc) {
  199. i_vpr_e(inst, "%s: set property failed\n", __func__);
  200. return rc;
  201. }
  202. return rc;
  203. }
  204. static int msm_vdec_set_bit_depth(struct msm_vidc_inst *inst,
  205. enum msm_vidc_port_type port)
  206. {
  207. int rc = 0;
  208. u32 colorformat;
  209. u32 bitdepth = 8 << 16 | 8;
  210. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  211. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  212. return -EINVAL;
  213. }
  214. colorformat = inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat;
  215. if (colorformat == V4L2_PIX_FMT_VIDC_P010 ||
  216. colorformat == V4L2_PIX_FMT_VIDC_TP10C)
  217. bitdepth = 10 << 16 | 10;
  218. inst->subcr_params[port].bit_depth = bitdepth;
  219. msm_vidc_update_cap_value(inst, BIT_DEPTH, bitdepth, __func__);
  220. i_vpr_h(inst, "%s: bit depth: %#x", __func__, bitdepth);
  221. rc = venus_hfi_session_property(inst,
  222. HFI_PROP_LUMA_CHROMA_BIT_DEPTH,
  223. HFI_HOST_FLAGS_NONE,
  224. get_hfi_port(inst, port),
  225. HFI_PAYLOAD_U32,
  226. &bitdepth,
  227. sizeof(u32));
  228. if (rc) {
  229. i_vpr_e(inst, "%s: set property failed\n", __func__);
  230. return rc;
  231. }
  232. return rc;
  233. }
  234. //todo: enable when needed
  235. /*
  236. static int msm_vdec_set_cabac(struct msm_vidc_inst *inst,
  237. enum msm_vidc_port_type port)
  238. {
  239. int rc = 0;
  240. u32 cabac = 0;
  241. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  242. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  243. return -EINVAL;
  244. }
  245. cabac = inst->capabilities->cap[ENTROPY_MODE].value;
  246. inst->subcr_params[port].cabac = cabac;
  247. i_vpr_h(inst, "%s: entropy mode: %d", __func__, cabac);
  248. rc = venus_hfi_session_property(inst,
  249. HFI_PROP_CABAC_SESSION,
  250. HFI_HOST_FLAGS_NONE,
  251. get_hfi_port(inst, port),
  252. HFI_PAYLOAD_U32,
  253. &cabac,
  254. sizeof(u32));
  255. if (rc)
  256. i_vpr_e(inst, "%s: set property failed\n", __func__);
  257. return rc;
  258. }
  259. */
  260. static int msm_vdec_set_coded_frames(struct msm_vidc_inst *inst,
  261. enum msm_vidc_port_type port)
  262. {
  263. int rc = 0;
  264. u32 coded_frames = 0;
  265. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  266. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  267. return -EINVAL;
  268. }
  269. if (inst->capabilities->cap[CODED_FRAMES].value ==
  270. CODED_FRAMES_PROGRESSIVE)
  271. coded_frames = HFI_BITMASK_FRAME_MBS_ONLY_FLAG;
  272. inst->subcr_params[port].coded_frames = coded_frames;
  273. i_vpr_h(inst, "%s: coded frames: %d", __func__, coded_frames);
  274. rc = venus_hfi_session_property(inst,
  275. HFI_PROP_CODED_FRAMES,
  276. HFI_HOST_FLAGS_NONE,
  277. get_hfi_port(inst, port),
  278. HFI_PAYLOAD_U32,
  279. &coded_frames,
  280. sizeof(u32));
  281. if (rc) {
  282. i_vpr_e(inst, "%s: set property failed\n", __func__);
  283. return rc;
  284. }
  285. return rc;
  286. }
  287. static int msm_vdec_set_min_output_count(struct msm_vidc_inst *inst,
  288. enum msm_vidc_port_type port)
  289. {
  290. int rc = 0;
  291. u32 min_output;
  292. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  293. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  294. return -EINVAL;
  295. }
  296. min_output = inst->buffers.output.min_count;
  297. inst->subcr_params[port].fw_min_count = min_output;
  298. i_vpr_h(inst, "%s: firmware min output count: %d",
  299. __func__, min_output);
  300. rc = venus_hfi_session_property(inst,
  301. HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT,
  302. HFI_HOST_FLAGS_NONE,
  303. get_hfi_port(inst, port),
  304. HFI_PAYLOAD_U32,
  305. &min_output,
  306. sizeof(u32));
  307. if (rc) {
  308. i_vpr_e(inst, "%s: set property failed\n", __func__);
  309. return rc;
  310. }
  311. return rc;
  312. }
  313. static int msm_vdec_set_picture_order_count(struct msm_vidc_inst *inst,
  314. enum msm_vidc_port_type port)
  315. {
  316. int rc = 0;
  317. u32 poc = 0;
  318. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  319. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  320. return -EINVAL;
  321. }
  322. inst->subcr_params[port].pic_order_cnt = poc;
  323. i_vpr_h(inst, "%s: picture order count: %d", __func__, poc);
  324. rc = venus_hfi_session_property(inst,
  325. HFI_PROP_PIC_ORDER_CNT_TYPE,
  326. HFI_HOST_FLAGS_NONE,
  327. get_hfi_port(inst, port),
  328. HFI_PAYLOAD_U32,
  329. &poc,
  330. sizeof(u32));
  331. if (rc) {
  332. i_vpr_e(inst, "%s: set property failed\n", __func__);
  333. return rc;
  334. }
  335. return rc;
  336. }
  337. static int msm_vdec_set_colorspace(struct msm_vidc_inst *inst,
  338. enum msm_vidc_port_type port)
  339. {
  340. int rc = 0;
  341. u32 primaries = MSM_VIDC_PRIMARIES_RESERVED;
  342. u32 matrix_coeff = MSM_VIDC_MATRIX_COEFF_RESERVED;
  343. u32 transfer_char = MSM_VIDC_TRANSFER_RESERVED;
  344. u32 full_range = V4L2_QUANTIZATION_DEFAULT;
  345. u32 colour_description_present_flag = 0;
  346. u32 video_signal_type_present_flag = 0, color_info = 0;
  347. /* Unspecified video format */
  348. u32 video_format = 5;
  349. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  350. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  351. return -EINVAL;
  352. }
  353. if (inst->codec != MSM_VIDC_H264 &&
  354. inst->codec != MSM_VIDC_HEVC &&
  355. inst->codec != MSM_VIDC_HEIC)
  356. return 0;
  357. if (inst->fmts[port].fmt.pix_mp.colorspace != V4L2_COLORSPACE_DEFAULT ||
  358. inst->fmts[port].fmt.pix_mp.ycbcr_enc != V4L2_YCBCR_ENC_DEFAULT ||
  359. inst->fmts[port].fmt.pix_mp.xfer_func != V4L2_XFER_FUNC_DEFAULT) {
  360. colour_description_present_flag = 1;
  361. video_signal_type_present_flag = 1;
  362. primaries = v4l2_color_primaries_to_driver(inst,
  363. inst->fmts[port].fmt.pix_mp.colorspace, __func__);
  364. matrix_coeff = v4l2_matrix_coeff_to_driver(inst,
  365. inst->fmts[port].fmt.pix_mp.ycbcr_enc, __func__);
  366. transfer_char = v4l2_transfer_char_to_driver(inst,
  367. inst->fmts[port].fmt.pix_mp.xfer_func, __func__);
  368. }
  369. if (inst->fmts[port].fmt.pix_mp.quantization !=
  370. V4L2_QUANTIZATION_DEFAULT) {
  371. video_signal_type_present_flag = 1;
  372. full_range = inst->fmts[port].fmt.pix_mp.quantization ==
  373. V4L2_QUANTIZATION_FULL_RANGE ? 1 : 0;
  374. }
  375. color_info = (matrix_coeff & 0xFF) |
  376. ((transfer_char << 8) & 0xFF00) |
  377. ((primaries << 16) & 0xFF0000) |
  378. ((colour_description_present_flag << 24) & 0x1000000) |
  379. ((full_range << 25) & 0x2000000) |
  380. ((video_format << 26) & 0x1C000000) |
  381. ((video_signal_type_present_flag << 29) & 0x20000000);
  382. inst->subcr_params[port].color_info = color_info;
  383. i_vpr_h(inst, "%s: color info: %#x\n", __func__, color_info);
  384. rc = venus_hfi_session_property(inst,
  385. HFI_PROP_SIGNAL_COLOR_INFO,
  386. HFI_HOST_FLAGS_NONE,
  387. get_hfi_port(inst, port),
  388. HFI_PAYLOAD_32_PACKED,
  389. &color_info,
  390. sizeof(u32));
  391. if (rc) {
  392. i_vpr_e(inst, "%s: set property failed\n", __func__);
  393. return rc;
  394. }
  395. return rc;
  396. }
  397. static int msm_vdec_set_profile(struct msm_vidc_inst *inst,
  398. enum msm_vidc_port_type port)
  399. {
  400. int rc = 0;
  401. u32 profile;
  402. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  403. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  404. return -EINVAL;
  405. }
  406. profile = inst->capabilities->cap[PROFILE].value;
  407. inst->subcr_params[port].profile = profile;
  408. i_vpr_h(inst, "%s: profile: %d", __func__, profile);
  409. rc = venus_hfi_session_property(inst,
  410. HFI_PROP_PROFILE,
  411. HFI_HOST_FLAGS_NONE,
  412. get_hfi_port(inst, port),
  413. HFI_PAYLOAD_U32_ENUM,
  414. &profile,
  415. sizeof(u32));
  416. if (rc) {
  417. i_vpr_e(inst, "%s: set property failed\n", __func__);
  418. return rc;
  419. }
  420. return rc;
  421. }
  422. static int msm_vdec_set_level(struct msm_vidc_inst *inst,
  423. enum msm_vidc_port_type port)
  424. {
  425. int rc = 0;
  426. u32 level;
  427. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  428. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  429. return -EINVAL;
  430. }
  431. level = inst->capabilities->cap[LEVEL].value;
  432. inst->subcr_params[port].level = level;
  433. i_vpr_h(inst, "%s: level: %d", __func__, level);
  434. rc = venus_hfi_session_property(inst,
  435. HFI_PROP_LEVEL,
  436. HFI_HOST_FLAGS_NONE,
  437. get_hfi_port(inst, port),
  438. HFI_PAYLOAD_U32_ENUM,
  439. &level,
  440. sizeof(u32));
  441. if (rc) {
  442. i_vpr_e(inst, "%s: set property failed\n", __func__);
  443. return rc;
  444. }
  445. return rc;
  446. }
  447. static int msm_vdec_set_tier(struct msm_vidc_inst *inst,
  448. enum msm_vidc_port_type port)
  449. {
  450. int rc = 0;
  451. u32 tier;
  452. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  453. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  454. return -EINVAL;
  455. }
  456. tier = inst->capabilities->cap[HEVC_TIER].value;
  457. inst->subcr_params[port].tier = tier;
  458. i_vpr_h(inst, "%s: tier: %d", __func__, tier);
  459. rc = venus_hfi_session_property(inst,
  460. HFI_PROP_TIER,
  461. HFI_HOST_FLAGS_NONE,
  462. get_hfi_port(inst, port),
  463. HFI_PAYLOAD_U32_ENUM,
  464. &tier,
  465. sizeof(u32));
  466. if (rc) {
  467. i_vpr_e(inst, "%s: set property failed\n", __func__);
  468. return rc;
  469. }
  470. return rc;
  471. }
  472. static int msm_vdec_set_av1_film_grain_present(struct msm_vidc_inst *inst,
  473. enum msm_vidc_port_type port)
  474. {
  475. int rc = 0;
  476. u32 fg_present;
  477. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  478. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  479. return -EINVAL;
  480. }
  481. inst->subcr_params[port].av1_film_grain_present =
  482. inst->capabilities->cap[FILM_GRAIN].value;
  483. fg_present = inst->subcr_params[port].av1_film_grain_present;
  484. i_vpr_h(inst, "%s: film grain present: %d", __func__, fg_present);
  485. rc = venus_hfi_session_property(inst,
  486. HFI_PROP_AV1_FILM_GRAIN_PRESENT,
  487. HFI_HOST_FLAGS_NONE,
  488. get_hfi_port(inst, port),
  489. HFI_PAYLOAD_U32_ENUM,
  490. &fg_present,
  491. sizeof(u32));
  492. if (rc) {
  493. i_vpr_e(inst, "%s: set property failed\n", __func__);
  494. return rc;
  495. }
  496. return rc;
  497. }
  498. static int msm_vdec_set_av1_superblock_enabled(struct msm_vidc_inst *inst,
  499. enum msm_vidc_port_type port)
  500. {
  501. int rc = 0;
  502. u32 sb_enabled;
  503. if (port != INPUT_PORT && port != OUTPUT_PORT) {
  504. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  505. return -EINVAL;
  506. }
  507. inst->subcr_params[port].av1_super_block_enabled =
  508. inst->capabilities->cap[SUPER_BLOCK].value;
  509. sb_enabled = inst->subcr_params[port].av1_super_block_enabled;
  510. i_vpr_h(inst, "%s: super block enabled: %d", __func__, sb_enabled);
  511. rc = venus_hfi_session_property(inst,
  512. HFI_PROP_AV1_SUPER_BLOCK_ENABLED,
  513. HFI_HOST_FLAGS_NONE,
  514. get_hfi_port(inst, port),
  515. HFI_PAYLOAD_U32_ENUM,
  516. &sb_enabled,
  517. sizeof(u32));
  518. if (rc) {
  519. i_vpr_e(inst, "%s: set property failed\n", __func__);
  520. return rc;
  521. }
  522. return rc;
  523. }
  524. static int msm_vdec_set_opb_enable(struct msm_vidc_inst *inst)
  525. {
  526. int rc = 0;
  527. u32 color_fmt;
  528. u32 opb_enable = 0;
  529. if (inst->codec != MSM_VIDC_AV1)
  530. return 0;
  531. color_fmt = inst->capabilities->cap[PIX_FMTS].value;
  532. if (is_linear_colorformat(color_fmt) ||
  533. inst->capabilities->cap[FILM_GRAIN].value)
  534. opb_enable = 1;
  535. i_vpr_h(inst, "%s: OPB enable: %d", __func__, opb_enable);
  536. rc = venus_hfi_session_property(inst,
  537. HFI_PROP_OPB_ENABLE,
  538. HFI_HOST_FLAGS_NONE,
  539. get_hfi_port(inst, OUTPUT_PORT),
  540. HFI_PAYLOAD_U32,
  541. &opb_enable,
  542. sizeof(u32));
  543. if (rc) {
  544. i_vpr_e(inst, "%s: set property failed\n", __func__);
  545. return rc;
  546. }
  547. return rc;
  548. }
  549. static int msm_vdec_set_colorformat(struct msm_vidc_inst *inst)
  550. {
  551. int rc = 0;
  552. u32 pixelformat;
  553. enum msm_vidc_colorformat_type colorformat;
  554. u32 hfi_colorformat;
  555. pixelformat = inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat;
  556. colorformat = v4l2_colorformat_to_driver(pixelformat, __func__);
  557. hfi_colorformat = get_hfi_colorformat(inst, colorformat);
  558. i_vpr_h(inst, "%s: hfi colorformat: %d",
  559. __func__, hfi_colorformat);
  560. rc = venus_hfi_session_property(inst,
  561. HFI_PROP_COLOR_FORMAT,
  562. HFI_HOST_FLAGS_NONE,
  563. get_hfi_port(inst, OUTPUT_PORT),
  564. HFI_PAYLOAD_U32,
  565. &hfi_colorformat,
  566. sizeof(u32));
  567. if (rc) {
  568. i_vpr_e(inst, "%s: set property failed\n", __func__);
  569. return rc;
  570. }
  571. return rc;
  572. }
  573. static int msm_vdec_set_output_order(struct msm_vidc_inst *inst,
  574. enum msm_vidc_port_type port)
  575. {
  576. int rc = 0;
  577. u32 output_order = 0;
  578. if (port != INPUT_PORT) {
  579. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  580. return -EINVAL;
  581. }
  582. if (inst->capabilities->cap[THUMBNAIL_MODE].value ||
  583. (inst->capabilities->cap[DISPLAY_DELAY_ENABLE].value &&
  584. !inst->capabilities->cap[DISPLAY_DELAY].value))
  585. output_order = 1;
  586. i_vpr_h(inst, "%s: output order: %d", __func__, output_order);
  587. rc = venus_hfi_session_property(inst,
  588. HFI_PROP_DECODE_ORDER_OUTPUT,
  589. HFI_HOST_FLAGS_NONE,
  590. get_hfi_port(inst, port),
  591. HFI_PAYLOAD_U32,
  592. &output_order,
  593. sizeof(u32));
  594. if (rc) {
  595. i_vpr_e(inst, "%s: set property failed\n", __func__);
  596. return rc;
  597. }
  598. return rc;
  599. }
  600. static int msm_vdec_set_rap_frame(struct msm_vidc_inst *inst,
  601. enum msm_vidc_port_type port)
  602. {
  603. int rc = 0;
  604. u32 rap_frame = true;
  605. if (port != INPUT_PORT) {
  606. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  607. return -EINVAL;
  608. }
  609. rap_frame = inst->capabilities->cap[RAP_FRAME].value;
  610. i_vpr_h(inst, "%s: start from rap frame: %d", __func__, rap_frame);
  611. rc = venus_hfi_session_property(inst,
  612. HFI_PROP_DEC_START_FROM_RAP_FRAME,
  613. HFI_HOST_FLAGS_NONE,
  614. get_hfi_port(inst, port),
  615. HFI_PAYLOAD_U32,
  616. &rap_frame,
  617. sizeof(u32));
  618. if (rc) {
  619. i_vpr_e(inst, "%s: set property failed\n", __func__);
  620. return rc;
  621. }
  622. return rc;
  623. }
  624. static int msm_vdec_set_thumbnail_mode(struct msm_vidc_inst *inst,
  625. enum msm_vidc_port_type port)
  626. {
  627. int rc = 0;
  628. u32 thumbnail_mode = 0;
  629. if (port != INPUT_PORT) {
  630. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  631. return -EINVAL;
  632. }
  633. thumbnail_mode = inst->capabilities->cap[THUMBNAIL_MODE].value;
  634. i_vpr_h(inst, "%s: thumbnail mode: %d", __func__, thumbnail_mode);
  635. rc = venus_hfi_session_property(inst,
  636. HFI_PROP_THUMBNAIL_MODE,
  637. HFI_HOST_FLAGS_NONE,
  638. get_hfi_port(inst, port),
  639. HFI_PAYLOAD_U32,
  640. &thumbnail_mode,
  641. sizeof(u32));
  642. if (rc) {
  643. i_vpr_e(inst, "%s: set property failed\n", __func__);
  644. return rc;
  645. }
  646. return rc;
  647. }
  648. static int msm_vdec_set_conceal_color_8bit(struct msm_vidc_inst *inst,
  649. enum msm_vidc_port_type port)
  650. {
  651. int rc = 0;
  652. u32 conceal_color_8bit;
  653. if (port != INPUT_PORT) {
  654. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  655. return -EINVAL;
  656. }
  657. conceal_color_8bit = inst->capabilities->cap[CONCEAL_COLOR_8BIT].value;
  658. i_vpr_h(inst, "%s: conceal color 8bit: %#x",
  659. __func__, conceal_color_8bit);
  660. rc = venus_hfi_session_property(inst,
  661. HFI_PROP_CONCEAL_COLOR_8BIT,
  662. HFI_HOST_FLAGS_NONE,
  663. get_hfi_port(inst, port),
  664. HFI_PAYLOAD_32_PACKED,
  665. &conceal_color_8bit,
  666. sizeof(u32));
  667. if (rc) {
  668. i_vpr_e(inst, "%s: set property failed\n", __func__);
  669. return rc;
  670. }
  671. return rc;
  672. }
  673. static int msm_vdec_set_conceal_color_10bit(struct msm_vidc_inst *inst,
  674. enum msm_vidc_port_type port)
  675. {
  676. int rc = 0;
  677. u32 conceal_color_10bit;
  678. if (port != INPUT_PORT) {
  679. i_vpr_e(inst, "%s: invalid port %d\n", __func__, port);
  680. return -EINVAL;
  681. }
  682. conceal_color_10bit = inst->capabilities->cap[CONCEAL_COLOR_10BIT].value;
  683. i_vpr_h(inst, "%s: conceal color 10bit: %#x",
  684. __func__, conceal_color_10bit);
  685. rc = venus_hfi_session_property(inst,
  686. HFI_PROP_CONCEAL_COLOR_10BIT,
  687. HFI_HOST_FLAGS_NONE,
  688. get_hfi_port(inst, port),
  689. HFI_PAYLOAD_32_PACKED,
  690. &conceal_color_10bit,
  691. sizeof(u32));
  692. if (rc) {
  693. i_vpr_e(inst, "%s: set property failed\n", __func__);
  694. return rc;
  695. }
  696. return rc;
  697. }
  698. static int msm_vdec_set_host_max_buf_count(struct msm_vidc_inst *inst,
  699. enum msm_vidc_port_type port)
  700. {
  701. int rc = 0;
  702. u32 count = DEFAULT_MAX_HOST_BUF_COUNT;
  703. if (is_image_session(inst))
  704. count = DEFAULT_MAX_HOST_BURST_BUF_COUNT;
  705. i_vpr_h(inst, "%s: count: %u port: %u\n", __func__, count, port);
  706. rc = venus_hfi_session_property(inst,
  707. HFI_PROP_BUFFER_HOST_MAX_COUNT,
  708. HFI_HOST_FLAGS_NONE,
  709. get_hfi_port(inst, port),
  710. HFI_PAYLOAD_U32,
  711. &count,
  712. sizeof(u32));
  713. if (rc) {
  714. i_vpr_e(inst, "%s: set property failed\n", __func__);
  715. return rc;
  716. }
  717. return 0;
  718. }
  719. static int msm_vdec_set_av1_bitstream_format(struct msm_vidc_inst *inst,
  720. enum msm_vidc_port_type port)
  721. {
  722. int rc = 0;
  723. u32 annex_b;
  724. if (inst->codec != MSM_VIDC_AV1)
  725. return 0;
  726. if (inst->capabilities->cap[WITHOUT_STARTCODE].value) {
  727. i_vpr_e(inst,
  728. "%s: Annex-B format is not supported\n", __func__);
  729. return -EINVAL;
  730. }
  731. annex_b = 0;
  732. i_vpr_h(inst, "%s: annex_b: %u\n", __func__, annex_b);
  733. rc = venus_hfi_session_property(inst,
  734. HFI_PROP_NAL_LENGTH_FIELD,
  735. HFI_HOST_FLAGS_NONE,
  736. get_hfi_port(inst, port),
  737. HFI_PAYLOAD_U32,
  738. &annex_b,
  739. sizeof(u32));
  740. if (rc) {
  741. i_vpr_e(inst, "%s: set property failed\n", __func__);
  742. return rc;
  743. }
  744. return rc;
  745. }
  746. static int msm_vdec_set_av1_operating_point(struct msm_vidc_inst *inst,
  747. enum msm_vidc_port_type port)
  748. {
  749. int rc = 0;
  750. u32 op_point;
  751. if (inst->codec != MSM_VIDC_AV1)
  752. return 0;
  753. op_point = inst->capabilities->cap[ENH_LAYER_COUNT].value;
  754. i_vpr_h(inst, "%s: op_point: %u\n", __func__, op_point);
  755. rc = venus_hfi_session_property(inst,
  756. HFI_PROP_AV1_OP_POINT,
  757. HFI_HOST_FLAGS_NONE,
  758. get_hfi_port(inst, port),
  759. HFI_PAYLOAD_U32,
  760. &op_point,
  761. sizeof(u32));
  762. if (rc) {
  763. i_vpr_e(inst, "%s: set property failed\n", __func__);
  764. return rc;
  765. }
  766. return rc;
  767. }
  768. static int msm_vdec_set_input_properties(struct msm_vidc_inst *inst)
  769. {
  770. int rc = 0;
  771. if (!inst) {
  772. d_vpr_e("%s: invalid params\n", __func__);
  773. return -EINVAL;
  774. }
  775. rc = msm_vidc_set_stage(inst, STAGE);
  776. if (rc)
  777. return rc;
  778. rc = msm_vidc_set_pipe(inst, PIPE);
  779. if (rc)
  780. return rc;
  781. rc = msm_vdec_set_output_order(inst, INPUT_PORT);
  782. if (rc)
  783. return rc;
  784. rc = msm_vdec_set_thumbnail_mode(inst, INPUT_PORT);
  785. if (rc)
  786. return rc;
  787. rc = msm_vdec_set_rap_frame(inst, INPUT_PORT);
  788. if (rc)
  789. return rc;
  790. rc = msm_vdec_set_conceal_color_8bit(inst, INPUT_PORT);
  791. if (rc)
  792. return rc;
  793. rc = msm_vdec_set_conceal_color_10bit(inst, INPUT_PORT);
  794. if (rc)
  795. return rc;
  796. rc = msm_vdec_set_host_max_buf_count(inst, INPUT_PORT);
  797. if (rc)
  798. return rc;
  799. rc = msm_vdec_set_av1_bitstream_format(inst, INPUT_PORT);
  800. if (rc)
  801. return rc;
  802. rc = msm_vdec_set_av1_operating_point(inst, INPUT_PORT);
  803. if (rc)
  804. return rc;
  805. return rc;
  806. }
  807. static int msm_vdec_set_output_properties(struct msm_vidc_inst *inst)
  808. {
  809. int rc = 0;
  810. if (!inst) {
  811. d_vpr_e("%s: invalid params\n", __func__);
  812. return -EINVAL;
  813. }
  814. rc = msm_vdec_set_opb_enable(inst);
  815. if (rc)
  816. return rc;
  817. rc = msm_vdec_set_colorformat(inst);
  818. if (rc)
  819. return rc;
  820. rc = msm_vdec_set_linear_stride_scanline(inst);
  821. if (rc)
  822. return rc;
  823. rc = msm_vdec_set_host_max_buf_count(inst, OUTPUT_PORT);
  824. if (rc)
  825. return rc;
  826. rc = msm_vidc_set_session_priority(inst, PRIORITY);
  827. if (rc)
  828. return rc;
  829. return rc;
  830. }
  831. static int msm_vdec_get_input_internal_buffers(struct msm_vidc_inst *inst)
  832. {
  833. int rc = 0;
  834. u32 i = 0;
  835. if (!inst) {
  836. d_vpr_e("%s: invalid params\n", __func__);
  837. return -EINVAL;
  838. }
  839. for (i = 0; i < ARRAY_SIZE(msm_vdec_internal_buffer_type); i++) {
  840. rc = msm_vidc_get_internal_buffers(inst, msm_vdec_internal_buffer_type[i]);
  841. if (rc)
  842. return rc;
  843. }
  844. return rc;
  845. }
  846. static int msm_vdec_get_output_internal_buffers(struct msm_vidc_inst *inst)
  847. {
  848. int rc = 0;
  849. if (!inst) {
  850. d_vpr_e("%s: invalid params\n", __func__);
  851. return -EINVAL;
  852. }
  853. rc = msm_vidc_get_internal_buffers(inst, MSM_VIDC_BUF_DPB);
  854. if (rc)
  855. return rc;
  856. return rc;
  857. }
  858. static int msm_vdec_create_input_internal_buffers(struct msm_vidc_inst *inst)
  859. {
  860. int rc = 0;
  861. u32 i = 0;
  862. for (i = 0; i < ARRAY_SIZE(msm_vdec_internal_buffer_type); i++) {
  863. rc = msm_vidc_create_internal_buffers(inst, msm_vdec_internal_buffer_type[i]);
  864. if (rc)
  865. return rc;
  866. }
  867. return 0;
  868. }
  869. static int msm_vdec_create_output_internal_buffers(struct msm_vidc_inst *inst)
  870. {
  871. int rc = 0;
  872. rc = msm_vidc_create_internal_buffers(inst, MSM_VIDC_BUF_DPB);
  873. if (rc)
  874. return rc;
  875. return 0;
  876. }
  877. static int msm_vdec_queue_input_internal_buffers(struct msm_vidc_inst *inst)
  878. {
  879. int rc = 0;
  880. u32 i = 0;
  881. for (i = 0; i < ARRAY_SIZE(msm_vdec_internal_buffer_type); i++) {
  882. rc = msm_vidc_queue_internal_buffers(inst, msm_vdec_internal_buffer_type[i]);
  883. if (rc)
  884. return rc;
  885. }
  886. return 0;
  887. }
  888. static int msm_vdec_queue_output_internal_buffers(struct msm_vidc_inst *inst)
  889. {
  890. int rc = 0;
  891. rc = msm_vidc_queue_internal_buffers(inst, MSM_VIDC_BUF_DPB);
  892. if (rc)
  893. return rc;
  894. return 0;
  895. }
  896. static int msm_vdec_release_input_internal_buffers(struct msm_vidc_inst *inst)
  897. {
  898. int rc = 0;
  899. u32 i = 0;
  900. i_vpr_h(inst, "%s()\n",__func__);
  901. for (i = 0; i < ARRAY_SIZE(msm_vdec_internal_buffer_type); i++) {
  902. rc = msm_vidc_release_internal_buffers(inst, msm_vdec_internal_buffer_type[i]);
  903. if (rc)
  904. return rc;
  905. }
  906. return 0;
  907. }
  908. static int msm_vdec_subscribe_input_port_settings_change(struct msm_vidc_inst *inst,
  909. enum msm_vidc_port_type port)
  910. {
  911. int rc = 0;
  912. struct msm_vidc_core *core;
  913. u32 payload[32] = {0};
  914. u32 i, j;
  915. u32 subscribe_psc_size;
  916. const u32 *psc;
  917. static const struct msm_vdec_prop_type_handle prop_type_handle_arr[] = {
  918. {HFI_PROP_BITSTREAM_RESOLUTION, msm_vdec_set_bitstream_resolution },
  919. {HFI_PROP_CROP_OFFSETS, msm_vdec_set_crop_offsets },
  920. {HFI_PROP_LUMA_CHROMA_BIT_DEPTH, msm_vdec_set_bit_depth },
  921. {HFI_PROP_CODED_FRAMES, msm_vdec_set_coded_frames },
  922. {HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT, msm_vdec_set_min_output_count },
  923. {HFI_PROP_PIC_ORDER_CNT_TYPE, msm_vdec_set_picture_order_count },
  924. {HFI_PROP_SIGNAL_COLOR_INFO, msm_vdec_set_colorspace },
  925. {HFI_PROP_PROFILE, msm_vdec_set_profile },
  926. {HFI_PROP_LEVEL, msm_vdec_set_level },
  927. {HFI_PROP_TIER, msm_vdec_set_tier },
  928. {HFI_PROP_AV1_FILM_GRAIN_PRESENT, msm_vdec_set_av1_film_grain_present },
  929. {HFI_PROP_AV1_SUPER_BLOCK_ENABLED, msm_vdec_set_av1_superblock_enabled },
  930. };
  931. if (!inst || !inst->core) {
  932. d_vpr_e("%s: invalid params\n", __func__);
  933. return -EINVAL;
  934. }
  935. core = inst->core;
  936. i_vpr_h(inst, "%s()\n", __func__);
  937. payload[0] = HFI_MODE_PORT_SETTINGS_CHANGE;
  938. if (inst->codec == MSM_VIDC_H264) {
  939. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_avc);
  940. psc = msm_vdec_subscribe_for_psc_avc;
  941. } else if (inst->codec == MSM_VIDC_HEVC || inst->codec == MSM_VIDC_HEIC) {
  942. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_hevc);
  943. psc = msm_vdec_subscribe_for_psc_hevc;
  944. } else if (inst->codec == MSM_VIDC_VP9) {
  945. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_vp9);
  946. psc = msm_vdec_subscribe_for_psc_vp9;
  947. } else if (inst->codec == MSM_VIDC_AV1) {
  948. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_av1);
  949. psc = msm_vdec_subscribe_for_psc_av1;
  950. } else {
  951. i_vpr_e(inst, "%s: unsupported codec: %d\n", __func__, inst->codec);
  952. psc = NULL;
  953. return -EINVAL;
  954. }
  955. if (!psc || !subscribe_psc_size) {
  956. i_vpr_e(inst, "%s: invalid params\n", __func__);
  957. return -EINVAL;
  958. }
  959. payload[0] = HFI_MODE_PORT_SETTINGS_CHANGE;
  960. for (i = 0; i < subscribe_psc_size; i++)
  961. payload[i + 1] = psc[i];
  962. rc = venus_hfi_session_command(inst,
  963. HFI_CMD_SUBSCRIBE_MODE,
  964. port,
  965. HFI_PAYLOAD_U32_ARRAY,
  966. &payload[0],
  967. ((subscribe_psc_size + 1) *
  968. sizeof(u32)));
  969. for (i = 0; i < subscribe_psc_size; i++) {
  970. /* set session properties */
  971. for (j = 0; j < ARRAY_SIZE(prop_type_handle_arr); j++) {
  972. if (prop_type_handle_arr[j].type == psc[i]) {
  973. rc = prop_type_handle_arr[j].handle(inst, port);
  974. if (rc)
  975. goto exit;
  976. break;
  977. }
  978. }
  979. /* is property type unknown ? */
  980. if (j == ARRAY_SIZE(prop_type_handle_arr))
  981. i_vpr_e(inst, "%s: unknown property %#x\n", __func__, psc[i]);
  982. }
  983. exit:
  984. return rc;
  985. }
  986. static int msm_vdec_subscribe_property(struct msm_vidc_inst *inst,
  987. enum msm_vidc_port_type port)
  988. {
  989. int rc = 0;
  990. u32 payload[32] = {0};
  991. u32 i, count = 0;
  992. bool allow = false;
  993. if (!inst) {
  994. d_vpr_e("%s: invalid params\n", __func__);
  995. return -EINVAL;
  996. }
  997. i_vpr_h(inst, "%s()\n", __func__);
  998. payload[0] = HFI_MODE_PROPERTY;
  999. if (port == INPUT_PORT) {
  1000. for (i = 0; i < ARRAY_SIZE(msm_vdec_input_subscribe_for_properties); i++) {
  1001. payload[count + 1] = msm_vdec_input_subscribe_for_properties[i];
  1002. count++;
  1003. }
  1004. } else if (port == OUTPUT_PORT) {
  1005. for (i = 0; i < ARRAY_SIZE(msm_vdec_output_subscribe_for_properties); i++) {
  1006. allow = msm_vidc_allow_property(inst,
  1007. msm_vdec_output_subscribe_for_properties[i]);
  1008. if (allow) {
  1009. payload[count + 1] = msm_vdec_output_subscribe_for_properties[i];
  1010. count++;
  1011. }
  1012. msm_vidc_update_property_cap(inst,
  1013. msm_vdec_output_subscribe_for_properties[i], allow);
  1014. }
  1015. } else {
  1016. i_vpr_e(inst, "%s: invalid port: %d\n", __func__, port);
  1017. return -EINVAL;
  1018. }
  1019. rc = venus_hfi_session_command(inst,
  1020. HFI_CMD_SUBSCRIBE_MODE,
  1021. port,
  1022. HFI_PAYLOAD_U32_ARRAY,
  1023. &payload[0],
  1024. (count + 1) * sizeof(u32));
  1025. return rc;
  1026. }
  1027. static int msm_vdec_subscribe_metadata(struct msm_vidc_inst *inst,
  1028. enum msm_vidc_port_type port)
  1029. {
  1030. int rc = 0;
  1031. u32 payload[32] = {0};
  1032. u32 i, count = 0;
  1033. struct msm_vidc_inst_capability *capability;
  1034. static const u32 metadata_list[] = {
  1035. META_BITSTREAM_RESOLUTION,
  1036. META_CROP_OFFSETS,
  1037. META_DPB_MISR,
  1038. META_OPB_MISR,
  1039. META_INTERLACE,
  1040. META_TIMESTAMP,
  1041. META_CONCEALED_MB_CNT,
  1042. META_HIST_INFO,
  1043. META_SEI_MASTERING_DISP,
  1044. META_SEI_CLL,
  1045. META_HDR10PLUS,
  1046. META_BUF_TAG,
  1047. META_DPB_TAG_LIST,
  1048. META_SUBFRAME_OUTPUT,
  1049. META_DEC_QP_METADATA,
  1050. META_MAX_NUM_REORDER_FRAMES,
  1051. };
  1052. if (!inst || !inst->capabilities) {
  1053. d_vpr_e("%s: invalid params\n", __func__);
  1054. return -EINVAL;
  1055. }
  1056. i_vpr_h(inst, "%s()\n", __func__);
  1057. capability = inst->capabilities;
  1058. payload[0] = HFI_MODE_METADATA;
  1059. for (i = 0; i < ARRAY_SIZE(metadata_list); i++) {
  1060. if (capability->cap[metadata_list[i]].value &&
  1061. msm_vidc_allow_metadata(inst, metadata_list[i])) {
  1062. payload[count + 1] =
  1063. capability->cap[metadata_list[i]].hfi_id;
  1064. count++;
  1065. }
  1066. };
  1067. rc = venus_hfi_session_command(inst,
  1068. HFI_CMD_SUBSCRIBE_MODE,
  1069. port,
  1070. HFI_PAYLOAD_U32_ARRAY,
  1071. &payload[0],
  1072. (count + 1) * sizeof(u32));
  1073. return rc;
  1074. }
  1075. static int msm_vdec_set_delivery_mode_metadata(struct msm_vidc_inst *inst,
  1076. enum msm_vidc_port_type port)
  1077. {
  1078. int rc = 0;
  1079. u32 payload[32] = {0};
  1080. u32 i, count = 0;
  1081. struct msm_vidc_inst_capability *capability;
  1082. static const u32 metadata_input_list[] = {
  1083. META_BUF_TAG,
  1084. };
  1085. static const u32 metadata_output_list[] = {
  1086. META_OUTPUT_BUF_TAG,
  1087. };
  1088. if (!inst || !inst->capabilities) {
  1089. d_vpr_e("%s: invalid params\n", __func__);
  1090. return -EINVAL;
  1091. }
  1092. i_vpr_h(inst, "%s()\n", __func__);
  1093. capability = inst->capabilities;
  1094. payload[0] = HFI_MODE_METADATA;
  1095. if (port == INPUT_PORT) {
  1096. for (i = 0; i < ARRAY_SIZE(metadata_input_list); i++) {
  1097. if (capability->cap[metadata_input_list[i]].value) {
  1098. payload[count + 1] =
  1099. capability->cap[metadata_input_list[i]].hfi_id;
  1100. count++;
  1101. }
  1102. }
  1103. } else if (port == OUTPUT_PORT) {
  1104. for (i = 0; i < ARRAY_SIZE(metadata_output_list); i++) {
  1105. if (capability->cap[metadata_output_list[i]].value &&
  1106. msm_vidc_allow_metadata(inst, metadata_output_list[i])) {
  1107. payload[count + 1] =
  1108. capability->cap[metadata_output_list[i]].hfi_id;
  1109. count++;
  1110. }
  1111. }
  1112. } else {
  1113. i_vpr_e(inst, "%s: invalid port: %d\n", __func__, port);
  1114. return -EINVAL;
  1115. }
  1116. rc = venus_hfi_session_command(inst,
  1117. HFI_CMD_DELIVERY_MODE,
  1118. port,
  1119. HFI_PAYLOAD_U32_ARRAY,
  1120. &payload[0],
  1121. (count + 1) * sizeof(u32));
  1122. return rc;
  1123. }
  1124. static int msm_vdec_session_resume(struct msm_vidc_inst *inst,
  1125. enum msm_vidc_port_type port)
  1126. {
  1127. int rc = 0;
  1128. i_vpr_h(inst, "%s()\n", __func__);
  1129. rc = venus_hfi_session_command(inst,
  1130. HFI_CMD_RESUME,
  1131. port,
  1132. HFI_PAYLOAD_NONE,
  1133. NULL,
  1134. 0);
  1135. return rc;
  1136. }
  1137. int msm_vdec_init_input_subcr_params(struct msm_vidc_inst *inst)
  1138. {
  1139. struct msm_vidc_subscription_params *subsc_params;
  1140. u32 left_offset, top_offset, right_offset, bottom_offset;
  1141. u32 primaries, matrix_coeff, transfer_char;
  1142. u32 full_range = 0, video_format = 0;
  1143. u32 colour_description_present_flag = 0;
  1144. u32 video_signal_type_present_flag = 0;
  1145. if (!inst || !inst->capabilities) {
  1146. d_vpr_e("%s: invalid params\n", __func__);
  1147. return -EINVAL;
  1148. }
  1149. subsc_params = &inst->subcr_params[INPUT_PORT];
  1150. subsc_params->bitstream_resolution =
  1151. inst->fmts[INPUT_PORT].fmt.pix_mp.width << 16 |
  1152. inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  1153. left_offset = inst->crop.left;
  1154. top_offset = inst->crop.top;
  1155. right_offset = (inst->fmts[INPUT_PORT].fmt.pix_mp.width -
  1156. inst->crop.width);
  1157. bottom_offset = (inst->fmts[INPUT_PORT].fmt.pix_mp.height -
  1158. inst->crop.height);
  1159. subsc_params->crop_offsets[0] =
  1160. left_offset << 16 | top_offset;
  1161. subsc_params->crop_offsets[1] =
  1162. right_offset << 16 | bottom_offset;
  1163. subsc_params->fw_min_count = inst->buffers.output.min_count;
  1164. primaries = v4l2_color_primaries_to_driver(inst,
  1165. inst->fmts[OUTPUT_PORT].fmt.pix_mp.colorspace, __func__);
  1166. matrix_coeff = v4l2_matrix_coeff_to_driver(inst,
  1167. inst->fmts[OUTPUT_PORT].fmt.pix_mp.ycbcr_enc, __func__);
  1168. transfer_char = v4l2_transfer_char_to_driver(inst,
  1169. inst->fmts[OUTPUT_PORT].fmt.pix_mp.xfer_func, __func__);
  1170. full_range = inst->fmts[OUTPUT_PORT].fmt.pix_mp.quantization ==
  1171. V4L2_QUANTIZATION_FULL_RANGE ? 1 : 0;
  1172. subsc_params->color_info =
  1173. (matrix_coeff & 0xFF) |
  1174. ((transfer_char << 8) & 0xFF00) |
  1175. ((primaries << 16) & 0xFF0000) |
  1176. ((colour_description_present_flag << 24) & 0x1000000) |
  1177. ((full_range << 25) & 0x2000000) |
  1178. ((video_format << 26) & 0x1C000000) |
  1179. ((video_signal_type_present_flag << 29) & 0x20000000);
  1180. subsc_params->profile = inst->capabilities->cap[PROFILE].value;
  1181. subsc_params->level = inst->capabilities->cap[LEVEL].value;
  1182. subsc_params->tier = inst->capabilities->cap[HEVC_TIER].value;
  1183. subsc_params->pic_order_cnt = inst->capabilities->cap[POC].value;
  1184. subsc_params->bit_depth = inst->capabilities->cap[BIT_DEPTH].value;
  1185. if (inst->capabilities->cap[CODED_FRAMES].value ==
  1186. CODED_FRAMES_PROGRESSIVE)
  1187. subsc_params->coded_frames = HFI_BITMASK_FRAME_MBS_ONLY_FLAG;
  1188. else
  1189. subsc_params->coded_frames = 0;
  1190. return 0;
  1191. }
  1192. static int msm_vdec_read_input_subcr_params(struct msm_vidc_inst *inst)
  1193. {
  1194. struct msm_vidc_subscription_params subsc_params;
  1195. struct msm_vidc_core *core;
  1196. u32 width, height;
  1197. u32 primaries, matrix_coeff, transfer_char;
  1198. u32 full_range = 0;
  1199. u32 colour_description_present_flag = 0;
  1200. u32 video_signal_type_present_flag = 0;
  1201. if (!inst || !inst->core) {
  1202. d_vpr_e("%s: invalid params\n", __func__);
  1203. return -EINVAL;
  1204. }
  1205. core = inst->core;
  1206. subsc_params = inst->subcr_params[INPUT_PORT];
  1207. width = (subsc_params.bitstream_resolution &
  1208. HFI_BITMASK_BITSTREAM_WIDTH) >> 16;
  1209. height = subsc_params.bitstream_resolution &
  1210. HFI_BITMASK_BITSTREAM_HEIGHT;
  1211. inst->fmts[INPUT_PORT].fmt.pix_mp.width = width;
  1212. inst->fmts[INPUT_PORT].fmt.pix_mp.height = height;
  1213. inst->fmts[OUTPUT_PORT].fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(
  1214. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat, width);
  1215. inst->fmts[OUTPUT_PORT].fmt.pix_mp.height = VIDEO_Y_SCANLINES(
  1216. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat, height);
  1217. inst->fmts[OUTPUT_PORT].fmt.pix_mp.plane_fmt[0].bytesperline =
  1218. VIDEO_Y_STRIDE_BYTES(
  1219. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat, width);
  1220. inst->fmts[OUTPUT_PORT].fmt.pix_mp.plane_fmt[0].sizeimage =
  1221. call_session_op(core, buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  1222. //inst->buffers.output.size = inst->fmts[OUTPUT_PORT].fmt.pix_mp.plane_fmt[0].sizeimage;
  1223. matrix_coeff = subsc_params.color_info & 0xFF;
  1224. transfer_char = (subsc_params.color_info & 0xFF00) >> 8;
  1225. primaries = (subsc_params.color_info & 0xFF0000) >> 16;
  1226. colour_description_present_flag =
  1227. (subsc_params.color_info & 0x1000000) >> 24;
  1228. full_range = (subsc_params.color_info & 0x2000000) >> 25;
  1229. video_signal_type_present_flag =
  1230. (subsc_params.color_info & 0x20000000) >> 29;
  1231. inst->fmts[OUTPUT_PORT].fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  1232. inst->fmts[OUTPUT_PORT].fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  1233. inst->fmts[OUTPUT_PORT].fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  1234. inst->fmts[OUTPUT_PORT].fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  1235. if (video_signal_type_present_flag) {
  1236. inst->fmts[OUTPUT_PORT].fmt.pix_mp.quantization =
  1237. full_range ?
  1238. V4L2_QUANTIZATION_FULL_RANGE :
  1239. V4L2_QUANTIZATION_LIM_RANGE;
  1240. if (colour_description_present_flag) {
  1241. inst->fmts[OUTPUT_PORT].fmt.pix_mp.colorspace =
  1242. v4l2_color_primaries_from_driver(inst, primaries, __func__);
  1243. inst->fmts[OUTPUT_PORT].fmt.pix_mp.xfer_func =
  1244. v4l2_transfer_char_from_driver(inst, transfer_char, __func__);
  1245. inst->fmts[OUTPUT_PORT].fmt.pix_mp.ycbcr_enc =
  1246. v4l2_matrix_coeff_from_driver(inst, matrix_coeff, __func__);
  1247. } else {
  1248. i_vpr_h(inst,
  1249. "%s: color description flag is not present\n",
  1250. __func__);
  1251. }
  1252. } else {
  1253. i_vpr_h(inst, "%s: video_signal type is not present\n",
  1254. __func__);
  1255. }
  1256. inst->buffers.output.min_count = subsc_params.fw_min_count;
  1257. inst->buffers.output.extra_count = call_session_op(core,
  1258. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  1259. if (is_thumbnail_session(inst) && inst->codec != MSM_VIDC_VP9) {
  1260. if (inst->buffers.output.min_count != 1) {
  1261. i_vpr_e(inst, "%s: invalid min count %d in thumbnail case\n",
  1262. __func__, inst->buffers.output.min_count);
  1263. msm_vidc_change_inst_state(inst, MSM_VIDC_ERROR, __func__);
  1264. }
  1265. }
  1266. inst->crop.top = subsc_params.crop_offsets[0] & 0xFFFF;
  1267. inst->crop.left = (subsc_params.crop_offsets[0] >> 16) & 0xFFFF;
  1268. inst->crop.height = inst->fmts[INPUT_PORT].fmt.pix_mp.height -
  1269. (subsc_params.crop_offsets[1] & 0xFFFF);
  1270. inst->crop.width = inst->fmts[INPUT_PORT].fmt.pix_mp.width -
  1271. ((subsc_params.crop_offsets[1] >> 16) & 0xFFFF);
  1272. msm_vidc_update_cap_value(inst, PROFILE, subsc_params.profile, __func__);
  1273. msm_vidc_update_cap_value(inst, LEVEL, subsc_params.level, __func__);
  1274. msm_vidc_update_cap_value(inst, HEVC_TIER, subsc_params.tier, __func__);
  1275. msm_vidc_update_cap_value(inst, POC, subsc_params.pic_order_cnt, __func__);
  1276. if (subsc_params.bit_depth == BIT_DEPTH_8)
  1277. msm_vidc_update_cap_value(inst, BIT_DEPTH, BIT_DEPTH_8, __func__);
  1278. else
  1279. msm_vidc_update_cap_value(inst, BIT_DEPTH, BIT_DEPTH_10, __func__);
  1280. if (subsc_params.coded_frames & HFI_BITMASK_FRAME_MBS_ONLY_FLAG)
  1281. msm_vidc_update_cap_value(inst, CODED_FRAMES, CODED_FRAMES_PROGRESSIVE, __func__);
  1282. else
  1283. msm_vidc_update_cap_value(inst, CODED_FRAMES, CODED_FRAMES_INTERLACE, __func__);
  1284. if (inst->codec == MSM_VIDC_AV1) {
  1285. msm_vidc_update_cap_value(inst, FILM_GRAIN,
  1286. subsc_params.av1_film_grain_present, __func__);
  1287. msm_vidc_update_cap_value(inst, SUPER_BLOCK,
  1288. subsc_params.av1_super_block_enabled, __func__);
  1289. }
  1290. return 0;
  1291. }
  1292. int msm_vdec_input_port_settings_change(struct msm_vidc_inst *inst)
  1293. {
  1294. u32 rc = 0;
  1295. struct v4l2_event event = {0};
  1296. if (!inst->bufq[INPUT_PORT].vb2q->streaming) {
  1297. i_vpr_e(inst, "%s: input port not streaming\n",
  1298. __func__);
  1299. return 0;
  1300. }
  1301. rc = msm_vdec_read_input_subcr_params(inst);
  1302. if (rc)
  1303. return rc;
  1304. event.type = V4L2_EVENT_SOURCE_CHANGE;
  1305. event.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION;
  1306. v4l2_event_queue_fh(&inst->event_handler, &event);
  1307. rc = msm_vdec_get_input_internal_buffers(inst);
  1308. if (rc)
  1309. return rc;
  1310. rc = msm_vdec_release_input_internal_buffers(inst);
  1311. if (rc)
  1312. return rc;
  1313. rc = msm_vdec_create_input_internal_buffers(inst);
  1314. if (rc)
  1315. return rc;
  1316. rc = msm_vdec_queue_input_internal_buffers(inst);
  1317. if (rc)
  1318. return rc;
  1319. rc = msm_vidc_set_stage(inst, STAGE);
  1320. if (rc)
  1321. return rc;
  1322. rc = msm_vidc_set_pipe(inst, PIPE);
  1323. if (rc)
  1324. return rc;
  1325. rc = msm_vdec_session_resume(inst, INPUT_PORT);
  1326. if (rc)
  1327. return rc;
  1328. return rc;
  1329. }
  1330. int msm_vdec_output_port_settings_change(struct msm_vidc_inst *inst)
  1331. {
  1332. //todo
  1333. return 0;
  1334. }
  1335. int msm_vdec_streamoff_input(struct msm_vidc_inst *inst)
  1336. {
  1337. int rc = 0;
  1338. if (!inst || !inst->core) {
  1339. d_vpr_e("%s: invalid params\n", __func__);
  1340. return -EINVAL;
  1341. }
  1342. rc = msm_vidc_session_streamoff(inst, INPUT_PORT);
  1343. if (rc)
  1344. return rc;
  1345. return 0;
  1346. }
  1347. int msm_vdec_streamon_input(struct msm_vidc_inst *inst)
  1348. {
  1349. int rc = 0;
  1350. if (!inst || !inst->core) {
  1351. d_vpr_e("%s: invalid params\n", __func__);
  1352. return -EINVAL;
  1353. }
  1354. if (is_input_meta_enabled(inst) &&
  1355. !inst->bufq[INPUT_META_PORT].vb2q->streaming) {
  1356. i_vpr_e(inst,
  1357. "%s: Meta port must be streamed on before data port\n",
  1358. __func__);
  1359. return -EINVAL;
  1360. }
  1361. rc = msm_vidc_check_session_supported(inst);
  1362. if (rc)
  1363. goto error;
  1364. rc = msm_vdec_set_input_properties(inst);
  1365. if (rc)
  1366. goto error;
  1367. /* Decide bse vpp delay after work mode */
  1368. //msm_vidc_set_bse_vpp_delay(inst);
  1369. rc = msm_vdec_get_input_internal_buffers(inst);
  1370. if (rc)
  1371. goto error;
  1372. /* check for memory after all buffers calculation */
  1373. //rc = msm_vidc_check_memory_supported(inst);
  1374. if (rc)
  1375. goto error;
  1376. rc = msm_vdec_create_input_internal_buffers(inst);
  1377. if (rc)
  1378. goto error;
  1379. rc = msm_vdec_queue_input_internal_buffers(inst);
  1380. if (rc)
  1381. goto error;
  1382. if (!inst->ipsc_properties_set) {
  1383. rc = msm_vdec_subscribe_input_port_settings_change(
  1384. inst, INPUT_PORT);
  1385. if (rc)
  1386. return rc;
  1387. inst->ipsc_properties_set = true;
  1388. }
  1389. rc = msm_vdec_subscribe_property(inst, INPUT_PORT);
  1390. if (rc)
  1391. return rc;
  1392. rc = msm_vdec_set_delivery_mode_metadata(inst, INPUT_PORT);
  1393. if (rc)
  1394. return rc;
  1395. rc = msm_vidc_session_streamon(inst, INPUT_PORT);
  1396. if (rc)
  1397. goto error;
  1398. rc = msm_vidc_flush_ts(inst);
  1399. if (rc)
  1400. goto error;
  1401. rc = msm_vidc_ts_reorder_flush(inst);
  1402. if (rc)
  1403. goto error;
  1404. return 0;
  1405. error:
  1406. i_vpr_e(inst, "%s: failed\n", __func__);
  1407. msm_vdec_streamoff_input(inst);
  1408. return rc;
  1409. }
  1410. static int schedule_batch_work(struct msm_vidc_inst *inst)
  1411. {
  1412. struct msm_vidc_core *core;
  1413. if (!inst || !inst->core) {
  1414. d_vpr_e("%s: invalid params\n", __func__);
  1415. return -EINVAL;
  1416. }
  1417. core = inst->core;
  1418. mod_delayed_work(core->batch_workq, &inst->decode_batch.work,
  1419. msecs_to_jiffies(core->capabilities[DECODE_BATCH_TIMEOUT].value));
  1420. return 0;
  1421. }
  1422. static int cancel_batch_work(struct msm_vidc_inst *inst)
  1423. {
  1424. if (!inst) {
  1425. d_vpr_e("%s: Invalid arguments\n", __func__);
  1426. return -EINVAL;
  1427. }
  1428. cancel_delayed_work(&inst->decode_batch.work);
  1429. return 0;
  1430. }
  1431. int msm_vdec_streamoff_output(struct msm_vidc_inst *inst)
  1432. {
  1433. int rc = 0;
  1434. if (!inst || !inst->core) {
  1435. d_vpr_e("%s: invalid params\n", __func__);
  1436. return -EINVAL;
  1437. }
  1438. /* cancel pending batch work */
  1439. cancel_batch_work(inst);
  1440. rc = msm_vidc_session_streamoff(inst, OUTPUT_PORT);
  1441. if (rc)
  1442. return rc;
  1443. return 0;
  1444. }
  1445. static int msm_vdec_subscribe_output_port_settings_change(struct msm_vidc_inst *inst,
  1446. enum msm_vidc_port_type port)
  1447. {
  1448. int rc = 0;
  1449. u32 payload[32] = {0};
  1450. u32 prop_type, payload_size, payload_type;
  1451. u32 i;
  1452. struct msm_vidc_subscription_params subsc_params;
  1453. u32 subscribe_psc_size = 0;
  1454. const u32 *psc = NULL;
  1455. if (!inst) {
  1456. d_vpr_e("%s: invalid params\n", __func__);
  1457. return -EINVAL;
  1458. }
  1459. i_vpr_h(inst, "%s()\n", __func__);
  1460. payload[0] = HFI_MODE_PORT_SETTINGS_CHANGE;
  1461. if (inst->codec == MSM_VIDC_H264) {
  1462. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_avc);
  1463. psc = msm_vdec_subscribe_for_psc_avc;
  1464. } else if (inst->codec == MSM_VIDC_HEVC || inst->codec == MSM_VIDC_HEIC) {
  1465. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_hevc);
  1466. psc = msm_vdec_subscribe_for_psc_hevc;
  1467. } else if (inst->codec == MSM_VIDC_VP9) {
  1468. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_vp9);
  1469. psc = msm_vdec_subscribe_for_psc_vp9;
  1470. } else if (inst->codec == MSM_VIDC_AV1) {
  1471. subscribe_psc_size = ARRAY_SIZE(msm_vdec_subscribe_for_psc_av1);
  1472. psc = msm_vdec_subscribe_for_psc_av1;
  1473. } else {
  1474. i_vpr_e(inst, "%s: unsupported codec: %d\n", __func__, inst->codec);
  1475. psc = NULL;
  1476. return -EINVAL;
  1477. }
  1478. if (!psc || !subscribe_psc_size) {
  1479. i_vpr_e(inst, "%s: invalid params\n", __func__);
  1480. return -EINVAL;
  1481. }
  1482. payload[0] = HFI_MODE_PORT_SETTINGS_CHANGE;
  1483. for (i = 0; i < subscribe_psc_size; i++)
  1484. payload[i + 1] = psc[i];
  1485. rc = venus_hfi_session_command(inst,
  1486. HFI_CMD_SUBSCRIBE_MODE,
  1487. port,
  1488. HFI_PAYLOAD_U32_ARRAY,
  1489. &payload[0],
  1490. ((subscribe_psc_size + 1) *
  1491. sizeof(u32)));
  1492. subsc_params = inst->subcr_params[port];
  1493. for (i = 0; i < subscribe_psc_size; i++) {
  1494. payload[0] = 0;
  1495. payload[1] = 0;
  1496. payload_size = 0;
  1497. payload_type = 0;
  1498. prop_type = psc[i];
  1499. switch (prop_type) {
  1500. case HFI_PROP_BITSTREAM_RESOLUTION:
  1501. payload[0] = subsc_params.bitstream_resolution;
  1502. payload_size = sizeof(u32);
  1503. payload_type = HFI_PAYLOAD_U32;
  1504. break;
  1505. case HFI_PROP_CROP_OFFSETS:
  1506. payload[0] = subsc_params.crop_offsets[0];
  1507. payload[1] = subsc_params.crop_offsets[1];
  1508. payload_size = sizeof(u64);
  1509. payload_type = HFI_PAYLOAD_64_PACKED;
  1510. break;
  1511. case HFI_PROP_LUMA_CHROMA_BIT_DEPTH:
  1512. payload[0] = subsc_params.bit_depth;
  1513. payload_size = sizeof(u32);
  1514. payload_type = HFI_PAYLOAD_U32;
  1515. break;
  1516. case HFI_PROP_CODED_FRAMES:
  1517. payload[0] = subsc_params.coded_frames;
  1518. payload_size = sizeof(u32);
  1519. payload_type = HFI_PAYLOAD_U32;
  1520. break;
  1521. case HFI_PROP_BUFFER_FW_MIN_OUTPUT_COUNT:
  1522. payload[0] = subsc_params.fw_min_count;
  1523. payload_size = sizeof(u32);
  1524. payload_type = HFI_PAYLOAD_U32;
  1525. break;
  1526. case HFI_PROP_PIC_ORDER_CNT_TYPE:
  1527. payload[0] = subsc_params.pic_order_cnt;
  1528. payload_size = sizeof(u32);
  1529. payload_type = HFI_PAYLOAD_U32;
  1530. break;
  1531. case HFI_PROP_SIGNAL_COLOR_INFO:
  1532. payload[0] = subsc_params.color_info;
  1533. payload_size = sizeof(u32);
  1534. payload_type = HFI_PAYLOAD_U32;
  1535. break;
  1536. case HFI_PROP_PROFILE:
  1537. payload[0] = subsc_params.profile;
  1538. payload_size = sizeof(u32);
  1539. payload_type = HFI_PAYLOAD_U32;
  1540. break;
  1541. case HFI_PROP_LEVEL:
  1542. payload[0] = subsc_params.level;
  1543. payload_size = sizeof(u32);
  1544. payload_type = HFI_PAYLOAD_U32;
  1545. break;
  1546. case HFI_PROP_TIER:
  1547. payload[0] = subsc_params.tier;
  1548. payload_size = sizeof(u32);
  1549. payload_type = HFI_PAYLOAD_U32;
  1550. break;
  1551. case HFI_PROP_AV1_FILM_GRAIN_PRESENT:
  1552. payload[0] = subsc_params.av1_film_grain_present;
  1553. payload_size = sizeof(u32);
  1554. payload_type = HFI_PAYLOAD_U32;
  1555. break;
  1556. case HFI_PROP_AV1_SUPER_BLOCK_ENABLED:
  1557. payload[0] = subsc_params.av1_super_block_enabled;
  1558. payload_size = sizeof(u32);
  1559. payload_type = HFI_PAYLOAD_U32;
  1560. break;
  1561. default:
  1562. i_vpr_e(inst, "%s: unknown property %#x\n", __func__,
  1563. prop_type);
  1564. prop_type = 0;
  1565. rc = -EINVAL;
  1566. break;
  1567. }
  1568. if (prop_type) {
  1569. rc = venus_hfi_session_property(inst,
  1570. prop_type,
  1571. HFI_HOST_FLAGS_NONE,
  1572. get_hfi_port(inst, port),
  1573. payload_type,
  1574. &payload,
  1575. payload_size);
  1576. if (rc)
  1577. return rc;
  1578. }
  1579. }
  1580. return rc;
  1581. }
  1582. static int msm_vdec_update_max_map_output_count(struct msm_vidc_inst *inst)
  1583. {
  1584. int rc = 0;
  1585. struct v4l2_format *f;
  1586. u32 width, height, count;
  1587. if (!inst) {
  1588. d_vpr_e("%s: invalid params\n", __func__);
  1589. return -EINVAL;
  1590. }
  1591. f = &inst->fmts[OUTPUT_PORT];
  1592. width = f->fmt.pix_mp.width;
  1593. height = f->fmt.pix_mp.height;
  1594. /*
  1595. * adjust max map output count based on resolution
  1596. * to enhance performance.
  1597. * For 8K session: count = 20
  1598. * For 4K session: count = 32
  1599. * For 1080p session: count = 48
  1600. * For all remaining sessions: count = 64
  1601. */
  1602. if (res_is_greater_than(width, height, 4096, 2160))
  1603. count = 20;
  1604. else if (res_is_greater_than(width, height, 1920, 1080))
  1605. count = 32;
  1606. else if (res_is_greater_than(width, height, 1280, 720))
  1607. count = 48;
  1608. else
  1609. count = 64;
  1610. inst->max_map_output_count = count;
  1611. i_vpr_h(inst, "%s: count: %d\n", __func__, inst->max_map_output_count);
  1612. return rc;
  1613. }
  1614. int msm_vdec_streamon_output(struct msm_vidc_inst *inst)
  1615. {
  1616. int rc = 0;
  1617. struct msm_vidc_inst_capability *capability;
  1618. if (!inst || !inst->core || !inst->capabilities) {
  1619. d_vpr_e("%s: invalid params\n", __func__);
  1620. return -EINVAL;
  1621. }
  1622. capability = inst->capabilities;
  1623. if (is_output_meta_enabled(inst) &&
  1624. !inst->bufq[OUTPUT_META_PORT].vb2q->streaming) {
  1625. i_vpr_e(inst,
  1626. "%s: Meta port must be streamed on before data port\n",
  1627. __func__);
  1628. return -EINVAL;
  1629. }
  1630. if (capability->cap[CODED_FRAMES].value == CODED_FRAMES_INTERLACE &&
  1631. !is_ubwc_colorformat(capability->cap[PIX_FMTS].value)) {
  1632. i_vpr_e(inst,
  1633. "%s: interlace with non-ubwc color format is unsupported\n",
  1634. __func__);
  1635. return -EINVAL;
  1636. }
  1637. rc = msm_vidc_check_session_supported(inst);
  1638. if (rc)
  1639. return rc;
  1640. rc = msm_vdec_update_max_map_output_count(inst);
  1641. if (rc)
  1642. return rc;
  1643. rc = msm_vdec_set_output_properties(inst);
  1644. if (rc)
  1645. goto error;
  1646. if (!inst->opsc_properties_set) {
  1647. memcpy(&inst->subcr_params[OUTPUT_PORT],
  1648. &inst->subcr_params[INPUT_PORT],
  1649. sizeof(inst->subcr_params[INPUT_PORT]));
  1650. rc = msm_vdec_subscribe_output_port_settings_change(inst, OUTPUT_PORT);
  1651. if (rc)
  1652. goto error;
  1653. inst->opsc_properties_set = true;
  1654. }
  1655. rc = msm_vdec_subscribe_property(inst, OUTPUT_PORT);
  1656. if (rc)
  1657. return rc;
  1658. rc = msm_vdec_subscribe_metadata(inst, OUTPUT_PORT);
  1659. if (rc)
  1660. goto error;
  1661. rc = msm_vdec_set_delivery_mode_metadata(inst, OUTPUT_PORT);
  1662. if (rc)
  1663. return rc;
  1664. rc = msm_vdec_get_output_internal_buffers(inst);
  1665. if (rc)
  1666. goto error;
  1667. rc = msm_vdec_create_output_internal_buffers(inst);
  1668. if (rc)
  1669. goto error;
  1670. rc = msm_vidc_session_streamon(inst, OUTPUT_PORT);
  1671. if (rc)
  1672. goto error;
  1673. rc = msm_vdec_queue_output_internal_buffers(inst);
  1674. if (rc)
  1675. goto error;
  1676. return 0;
  1677. error:
  1678. i_vpr_e(inst, "%s: failed\n", __func__);
  1679. msm_vdec_streamoff_output(inst);
  1680. return rc;
  1681. }
  1682. static inline enum msm_vidc_allow msm_vdec_allow_queue_deferred_buffers(
  1683. struct msm_vidc_inst *inst)
  1684. {
  1685. int count;
  1686. if (!inst) {
  1687. d_vpr_e("%s: invalid params\n", __func__);
  1688. return MSM_VIDC_DISALLOW;
  1689. }
  1690. /* do not defer buffers initially to avoid latency issues */
  1691. if (inst->power.buffer_counter <= SKIP_BATCH_WINDOW)
  1692. return MSM_VIDC_ALLOW;
  1693. /* do not defer, if client waiting for last flag FBD */
  1694. if (inst->state != MSM_VIDC_START)
  1695. return MSM_VIDC_ALLOW;
  1696. /* defer qbuf, if pending buffers count less than batch size */
  1697. count = msm_vidc_num_buffers(inst, MSM_VIDC_BUF_OUTPUT, MSM_VIDC_ATTR_DEFERRED);
  1698. if (count < inst->decode_batch.size)
  1699. return MSM_VIDC_DEFER;
  1700. return MSM_VIDC_ALLOW;
  1701. }
  1702. static int msm_vdec_qbuf_batch(struct msm_vidc_inst *inst,
  1703. struct vb2_buffer *vb2)
  1704. {
  1705. struct msm_vidc_buffer *buf;
  1706. enum msm_vidc_allow allow;
  1707. int rc;
  1708. if (!inst || !vb2 || !inst->decode_batch.size) {
  1709. d_vpr_e("%s: invalid params\n", __func__);
  1710. return -EINVAL;
  1711. }
  1712. buf = msm_vidc_get_driver_buf(inst, vb2);
  1713. if (!buf)
  1714. return -EINVAL;
  1715. allow = msm_vidc_allow_qbuf(inst, vb2->type);
  1716. if (allow == MSM_VIDC_DISALLOW) {
  1717. i_vpr_e(inst, "%s: qbuf not allowed\n", __func__);
  1718. return -EINVAL;
  1719. } else if (allow == MSM_VIDC_DEFER) {
  1720. print_vidc_buffer(VIDC_LOW, "low ", "qbuf deferred", inst, buf);
  1721. return 0;
  1722. }
  1723. allow = msm_vdec_allow_queue_deferred_buffers(inst);
  1724. if (allow == MSM_VIDC_DISALLOW) {
  1725. i_vpr_e(inst, "%s: queue deferred buffers not allowed\n", __func__);
  1726. return -EINVAL;
  1727. } else if (allow == MSM_VIDC_DEFER) {
  1728. print_vidc_buffer(VIDC_LOW, "low ", "batch-qbuf deferred", inst, buf);
  1729. schedule_batch_work(inst);
  1730. return 0;
  1731. }
  1732. cancel_batch_work(inst);
  1733. rc = msm_vidc_queue_deferred_buffers(inst, MSM_VIDC_BUF_OUTPUT);
  1734. if (rc)
  1735. return rc;
  1736. return rc;
  1737. }
  1738. static int msm_vdec_release_nonref_buffers(struct msm_vidc_inst *inst)
  1739. {
  1740. int rc = 0;
  1741. u32 fw_ro_count = 0, nonref_ro_count = 0;
  1742. struct msm_vidc_buffer *ro_buf, *rel_buf, *dummy;
  1743. int i = 0;
  1744. bool found = false;
  1745. if (!inst) {
  1746. d_vpr_e("%s: invalid params\n", __func__);
  1747. return -EINVAL;
  1748. }
  1749. /* count num buffers in read_only list */
  1750. list_for_each_entry(ro_buf, &inst->buffers.read_only.list, list)
  1751. fw_ro_count++;
  1752. if (fw_ro_count <= MAX_DPB_COUNT)
  1753. return 0;
  1754. /*
  1755. * Mark those buffers present in read_only list as non-reference
  1756. * if that buffer is not part of dpb_list_payload
  1757. * count such non-ref read only buffers as nonref_ro_count
  1758. * dpb_list_payload details:
  1759. * payload[0-1] : 64 bits base_address of DPB-1
  1760. * payload[2] : 32 bits addr_offset of DPB-1
  1761. * payload[3] : 32 bits data_offset of DPB-1
  1762. */
  1763. list_for_each_entry(ro_buf, &inst->buffers.read_only.list, list) {
  1764. found = false;
  1765. for (i = 0; (i + 3) < MAX_DPB_LIST_ARRAY_SIZE; i = i + 4) {
  1766. if (ro_buf->device_addr == inst->dpb_list_payload[i] &&
  1767. ro_buf->data_offset == inst->dpb_list_payload[i + 3]) {
  1768. found = true;
  1769. break;
  1770. }
  1771. }
  1772. if (!found) {
  1773. ro_buf->attr &= ~MSM_VIDC_ATTR_READ_ONLY;
  1774. nonref_ro_count++;
  1775. }
  1776. }
  1777. if (nonref_ro_count <= inst->buffers.output.min_count)
  1778. return 0;
  1779. i_vpr_l(inst, "%s: fw ro buf count %d, non-ref ro count %d\n",
  1780. __func__, fw_ro_count, nonref_ro_count);
  1781. /*
  1782. * move non-ref read only buffers from read_only list to
  1783. * release list
  1784. */
  1785. list_for_each_entry_safe(ro_buf, dummy, &inst->buffers.read_only.list, list) {
  1786. if (!(ro_buf->attr & MSM_VIDC_ATTR_READ_ONLY)) {
  1787. list_del(&ro_buf->list);
  1788. INIT_LIST_HEAD(&ro_buf->list);
  1789. list_add_tail(&ro_buf->list, &inst->buffers.release.list);
  1790. }
  1791. }
  1792. /* send release flag along with read only flag for release list bufs*/
  1793. list_for_each_entry(rel_buf, &inst->buffers.release.list, list) {
  1794. /* fw needs RO flag for FTB release buffer */
  1795. rel_buf->attr |= MSM_VIDC_ATTR_READ_ONLY;
  1796. print_vidc_buffer(VIDC_LOW, "low ", "release buf", inst, rel_buf);
  1797. rc = venus_hfi_release_buffer(inst, rel_buf);
  1798. if (rc)
  1799. return rc;
  1800. }
  1801. return rc;
  1802. }
  1803. int msm_vdec_handle_release_buffer(struct msm_vidc_inst *inst,
  1804. struct msm_vidc_buffer *buf)
  1805. {
  1806. int rc = 0;
  1807. if (!inst || !buf) {
  1808. d_vpr_e("%s: invalid params\n", __func__);
  1809. return -EINVAL;
  1810. }
  1811. /**
  1812. * RO & release list doesnot take dma ref_count using dma_buf_get().
  1813. * Dmabuf ptr willbe obsolete when its last ref was last.
  1814. * Use direct api to print logs instead of calling print_vidc_buffer()
  1815. * api, which will attempt to dereferrence dmabuf ptr.
  1816. */
  1817. i_vpr_l(inst,
  1818. "release done: %s: idx %2d fd %3d off %d daddr %#llx size %8d filled %8d flags %#x ts %8lld attr %#x counts(etb ebd ftb fbd) %4llu %4llu %4llu %4llu\n",
  1819. buf_name(buf->type),
  1820. buf->index, buf->fd, buf->data_offset,
  1821. buf->device_addr, buf->buffer_size, buf->data_size,
  1822. buf->flags, buf->timestamp, buf->attr, inst->debug_count.etb,
  1823. inst->debug_count.ebd, inst->debug_count.ftb, inst->debug_count.fbd);
  1824. /* delete the buffer from release list */
  1825. list_del(&buf->list);
  1826. msm_memory_free(inst, buf);
  1827. return rc;
  1828. }
  1829. static bool is_valid_removable_buffer(struct msm_vidc_inst *inst,
  1830. struct msm_vidc_map *map)
  1831. {
  1832. bool found = false;
  1833. struct msm_vidc_buffer *buf;
  1834. if (!inst || !map) {
  1835. d_vpr_e("%s: invalid params\n", __func__);
  1836. return -EINVAL;
  1837. }
  1838. if (map->refcount != 1)
  1839. return false;
  1840. list_for_each_entry(buf, &inst->buffers.read_only.list, list) {
  1841. if (map->device_addr == buf->device_addr) {
  1842. found = true;
  1843. break;
  1844. }
  1845. }
  1846. list_for_each_entry(buf, &inst->buffers.release.list, list) {
  1847. if (map->device_addr == buf->device_addr) {
  1848. found = true;
  1849. break;
  1850. }
  1851. }
  1852. if (!found)
  1853. return true;
  1854. return false;
  1855. }
  1856. static int msm_vidc_unmap_excessive_mappings(struct msm_vidc_inst *inst)
  1857. {
  1858. int rc = 0;
  1859. struct msm_vidc_map *map, *temp;
  1860. u32 refcount_one_bufs_count = 0;
  1861. if (!inst) {
  1862. d_vpr_e("%s: invalid params\n", __func__);
  1863. return -EINVAL;
  1864. }
  1865. /*
  1866. * count entries from map list which are not present in
  1867. * read_only buffers list, not present in release list
  1868. * and whose refcount is 1.these are excess mappings
  1869. * present due to delayed unmap feature.
  1870. */
  1871. list_for_each_entry(map, &inst->mappings.output.list, list) {
  1872. if (is_valid_removable_buffer(inst, map))
  1873. refcount_one_bufs_count++;
  1874. }
  1875. if (refcount_one_bufs_count <= inst->max_map_output_count)
  1876. return 0;
  1877. /* unmap these buffers as they are stale entries */
  1878. list_for_each_entry_safe(map, temp, &inst->mappings.output.list, list) {
  1879. if (is_valid_removable_buffer(inst, map)) {
  1880. i_vpr_l(inst,
  1881. "%s: type %11s, device_addr %#x, refcount %d, region %d\n",
  1882. __func__, buf_name(map->type), map->device_addr,
  1883. map->refcount, map->region);
  1884. rc = msm_vidc_put_delayed_unmap(inst, map);
  1885. if (rc)
  1886. return rc;
  1887. }
  1888. }
  1889. return rc;
  1890. }
  1891. int msm_vdec_qbuf(struct msm_vidc_inst *inst, struct vb2_buffer *vb2)
  1892. {
  1893. int rc = 0;
  1894. if (!inst || !vb2 || !inst->capabilities) {
  1895. d_vpr_e("%s: invalid params\n", __func__);
  1896. return -EINVAL;
  1897. }
  1898. if (vb2->type == OUTPUT_MPLANE) {
  1899. if (inst->capabilities->cap[DPB_LIST].value) {
  1900. rc = msm_vdec_release_nonref_buffers(inst);
  1901. if (rc)
  1902. return rc;
  1903. }
  1904. }
  1905. if (vb2->type == OUTPUT_META_PLANE) {
  1906. if (inst->capabilities->cap[META_DPB_TAG_LIST].value) {
  1907. /*
  1908. * vb2 is not allowing client to pass data in output meta plane.
  1909. * adjust the bytesused as client will send buffer tag metadata
  1910. * in output meta plane if DPB_TAG_LIST metadata enabled.
  1911. */
  1912. if (!vb2->planes[0].bytesused)
  1913. vb2->planes[0].bytesused = 1024;
  1914. }
  1915. }
  1916. if (inst->firmware_priority != (inst->priority_level +
  1917. inst->capabilities->cap[PRIORITY].value * 2))
  1918. msm_vidc_set_session_priority(inst, PRIORITY);
  1919. /* batch decoder output & meta buffer only */
  1920. if (inst->decode_batch.enable && vb2->type == OUTPUT_MPLANE)
  1921. rc = msm_vdec_qbuf_batch(inst, vb2);
  1922. else
  1923. rc = msm_vidc_queue_buffer_single(inst, vb2);
  1924. if (rc)
  1925. return rc;
  1926. if (vb2->type == OUTPUT_MPLANE) {
  1927. rc = msm_vidc_unmap_excessive_mappings(inst);
  1928. if (rc)
  1929. return rc;
  1930. }
  1931. return rc;
  1932. }
  1933. static msm_vdec_alloc_and_queue_additional_dpb_buffers(struct msm_vidc_inst *inst)
  1934. {
  1935. struct msm_vidc_buffers *buffers;
  1936. struct msm_vidc_buffer *buffer = NULL;
  1937. int i, cur_min_count = 0, rc = 0;
  1938. if (!inst) {
  1939. d_vpr_e("%s: invalid params\n", __func__);
  1940. return -EINVAL;
  1941. }
  1942. /* get latest min_count and size */
  1943. rc = msm_vidc_get_internal_buffers(inst, MSM_VIDC_BUF_DPB);
  1944. if (rc)
  1945. return rc;
  1946. buffers = msm_vidc_get_buffers(inst, MSM_VIDC_BUF_DPB, __func__);
  1947. if (!buffers)
  1948. return -EINVAL;
  1949. /* get current min_count */
  1950. list_for_each_entry(buffer, &buffers->list, list)
  1951. cur_min_count++;
  1952. /* skip alloc and queue */
  1953. if (cur_min_count >= buffers->min_count)
  1954. return 0;
  1955. i_vpr_h(inst, "%s: dpb buffer count increased from %u -> %u\n",
  1956. __func__, cur_min_count, buffers->min_count);
  1957. /* allocate additional DPB buffers */
  1958. for (i = cur_min_count; i < buffers->min_count; i++) {
  1959. rc = msm_vidc_create_internal_buffer(inst, MSM_VIDC_BUF_DPB, i);
  1960. if (rc)
  1961. return rc;
  1962. }
  1963. /* queue additional DPB buffers */
  1964. rc = msm_vidc_queue_internal_buffers(inst, MSM_VIDC_BUF_DPB);
  1965. if (rc)
  1966. return rc;
  1967. return 0;
  1968. }
  1969. int msm_vdec_process_cmd(struct msm_vidc_inst *inst, u32 cmd)
  1970. {
  1971. int rc = 0;
  1972. enum msm_vidc_allow allow = MSM_VIDC_DISALLOW;
  1973. enum msm_vidc_port_type port;
  1974. struct msm_vidc_inst_capability *capability;
  1975. if (!inst || !inst->core || !inst->capabilities) {
  1976. d_vpr_e("%s: invalid params\n", __func__);
  1977. return -EINVAL;
  1978. }
  1979. capability = inst->capabilities;
  1980. if (cmd == V4L2_DEC_CMD_STOP) {
  1981. i_vpr_h(inst, "received cmd: drain\n");
  1982. allow = msm_vidc_allow_stop(inst);
  1983. if (allow == MSM_VIDC_DISALLOW)
  1984. return -EBUSY;
  1985. else if (allow == MSM_VIDC_IGNORE)
  1986. return 0;
  1987. else if (allow != MSM_VIDC_ALLOW)
  1988. return -EINVAL;
  1989. rc = venus_hfi_session_command(inst,
  1990. HFI_CMD_DRAIN,
  1991. INPUT_PORT,
  1992. HFI_PAYLOAD_NONE,
  1993. NULL,
  1994. 0);
  1995. if (rc)
  1996. return rc;
  1997. rc = msm_vidc_state_change_stop(inst);
  1998. if (rc)
  1999. return rc;
  2000. } else if (cmd == V4L2_DEC_CMD_START) {
  2001. i_vpr_h(inst, "received cmd: resume\n");
  2002. vb2_clear_last_buffer_dequeued(inst->bufq[OUTPUT_META_PORT].vb2q);
  2003. vb2_clear_last_buffer_dequeued(inst->bufq[OUTPUT_PORT].vb2q);
  2004. if (capability->cap[CODED_FRAMES].value == CODED_FRAMES_INTERLACE &&
  2005. !is_ubwc_colorformat(capability->cap[PIX_FMTS].value)) {
  2006. i_vpr_e(inst,
  2007. "%s: interlace with non-ubwc color format is unsupported\n",
  2008. __func__);
  2009. return -EINVAL;
  2010. }
  2011. if (!msm_vidc_allow_start(inst))
  2012. return -EBUSY;
  2013. port = (inst->state == MSM_VIDC_DRAIN_LAST_FLAG) ? INPUT_PORT : OUTPUT_PORT;
  2014. rc = msm_vidc_state_change_start(inst);
  2015. if (rc)
  2016. return rc;
  2017. /* tune power features */
  2018. inst->decode_batch.enable = msm_vidc_allow_decode_batch(inst);
  2019. msm_vidc_allow_dcvs(inst);
  2020. msm_vidc_power_data_reset(inst);
  2021. /* allocate and queue extra dpb buffers */
  2022. rc = msm_vdec_alloc_and_queue_additional_dpb_buffers(inst);
  2023. if (rc)
  2024. return rc;
  2025. /* print final buffer counts & size details */
  2026. msm_vidc_print_buffer_info(inst);
  2027. rc = msm_vdec_session_resume(inst, port);
  2028. if (rc)
  2029. return rc;
  2030. } else {
  2031. i_vpr_e(inst, "%s: unknown cmd %d\n", __func__, cmd);
  2032. return -EINVAL;
  2033. }
  2034. return 0;
  2035. }
  2036. int msm_vdec_try_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  2037. {
  2038. int rc = 0;
  2039. struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
  2040. u32 pix_fmt;
  2041. if (!inst) {
  2042. d_vpr_e("%s: invalid params\n", __func__);
  2043. return -EINVAL;
  2044. }
  2045. memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
  2046. if (f->type == INPUT_MPLANE) {
  2047. pix_fmt = v4l2_codec_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  2048. if (!pix_fmt) {
  2049. i_vpr_h(inst, "%s: unsupported codec, set default params\n", __func__);
  2050. f->fmt.pix_mp.width = DEFAULT_WIDTH;
  2051. f->fmt.pix_mp.height = DEFAULT_HEIGHT;
  2052. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  2053. pix_fmt = v4l2_codec_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  2054. }
  2055. } else if (f->type == OUTPUT_MPLANE) {
  2056. if (inst->bufq[INPUT_PORT].vb2q->streaming) {
  2057. f->fmt.pix_mp.height = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  2058. f->fmt.pix_mp.width = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  2059. }
  2060. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  2061. if (!pix_fmt) {
  2062. i_vpr_h(inst, "%s: unsupported format, set default params\n", __func__);
  2063. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_VIDC_NV12C;
  2064. f->fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(f->fmt.pix_mp.pixelformat,
  2065. DEFAULT_WIDTH);
  2066. f->fmt.pix_mp.height = VIDEO_Y_SCANLINES(f->fmt.pix_mp.pixelformat,
  2067. DEFAULT_HEIGHT);
  2068. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  2069. }
  2070. } else {
  2071. i_vpr_e(inst, "%s: invalid type %d\n", __func__, f->type);
  2072. return -EINVAL;
  2073. }
  2074. if (pixmp->field == V4L2_FIELD_ANY)
  2075. pixmp->field = V4L2_FIELD_NONE;
  2076. pixmp->num_planes = 1;
  2077. return rc;
  2078. }
  2079. static bool msm_vidc_check_max_sessions_vp9d(struct msm_vidc_core *core)
  2080. {
  2081. u32 vp9d_instance_count = 0;
  2082. struct msm_vidc_inst *inst = NULL;
  2083. core_lock(core, __func__);
  2084. list_for_each_entry(inst, &core->instances, list) {
  2085. if (is_decode_session(inst) &&
  2086. inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat ==
  2087. V4L2_PIX_FMT_VP9)
  2088. vp9d_instance_count++;
  2089. }
  2090. core_unlock(core, __func__);
  2091. if (vp9d_instance_count > MAX_VP9D_INST_COUNT)
  2092. return true;
  2093. return false;
  2094. }
  2095. int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  2096. {
  2097. int rc = 0;
  2098. struct msm_vidc_core *core;
  2099. struct v4l2_format *fmt;
  2100. u32 codec_align, pix_fmt;
  2101. if (!inst || !inst->core) {
  2102. d_vpr_e("%s: invalid params\n", __func__);
  2103. return -EINVAL;
  2104. }
  2105. core = inst->core;
  2106. if (f->type == INPUT_MPLANE) {
  2107. if (inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat !=
  2108. f->fmt.pix_mp.pixelformat) {
  2109. rc = msm_vdec_codec_change(inst, f->fmt.pix_mp.pixelformat);
  2110. if (rc)
  2111. goto err_invalid_fmt;
  2112. }
  2113. if (f->fmt.pix_mp.pixelformat == V4L2_PIX_FMT_VP9) {
  2114. if (msm_vidc_check_max_sessions_vp9d(inst->core)) {
  2115. i_vpr_e(inst,
  2116. "%s: vp9d sessions exceeded max limit %d\n",
  2117. __func__, MAX_VP9D_INST_COUNT);
  2118. rc = -ENOMEM;
  2119. goto err_invalid_fmt;
  2120. }
  2121. }
  2122. fmt = &inst->fmts[INPUT_PORT];
  2123. fmt->type = INPUT_MPLANE;
  2124. codec_align = inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat ==
  2125. V4L2_PIX_FMT_HEVC ? 32 : 16;
  2126. fmt->fmt.pix_mp.width = ALIGN(f->fmt.pix_mp.width, codec_align);
  2127. fmt->fmt.pix_mp.height = ALIGN(f->fmt.pix_mp.height, codec_align);
  2128. fmt->fmt.pix_mp.num_planes = 1;
  2129. fmt->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
  2130. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  2131. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  2132. inst->buffers.input.min_count = call_session_op(core,
  2133. min_count, inst, MSM_VIDC_BUF_INPUT);
  2134. inst->buffers.input.extra_count = call_session_op(core,
  2135. extra_count, inst, MSM_VIDC_BUF_INPUT);
  2136. if (inst->buffers.input.actual_count <
  2137. inst->buffers.input.min_count +
  2138. inst->buffers.input.extra_count) {
  2139. inst->buffers.input.actual_count =
  2140. inst->buffers.input.min_count +
  2141. inst->buffers.input.extra_count;
  2142. }
  2143. inst->buffers.input.size =
  2144. fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
  2145. /* update crop dimensions */
  2146. inst->crop.left = inst->crop.top = 0;
  2147. inst->crop.width = f->fmt.pix_mp.width;
  2148. inst->crop.height = f->fmt.pix_mp.height;
  2149. i_vpr_h(inst,
  2150. "%s: type: INPUT, codec %s width %d height %d size %u min_count %d extra_count %d\n",
  2151. __func__, v4l2_pixelfmt_name(f->fmt.pix_mp.pixelformat),
  2152. f->fmt.pix_mp.width, f->fmt.pix_mp.height,
  2153. fmt->fmt.pix_mp.plane_fmt[0].sizeimage,
  2154. inst->buffers.input.min_count,
  2155. inst->buffers.input.extra_count);
  2156. } else if (f->type == INPUT_META_PLANE) {
  2157. fmt = &inst->fmts[INPUT_META_PORT];
  2158. fmt->type = INPUT_META_PLANE;
  2159. fmt->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  2160. if (is_input_meta_enabled(inst)) {
  2161. fmt->fmt.meta.buffersize = call_session_op(core,
  2162. buffer_size, inst, MSM_VIDC_BUF_INPUT_META);
  2163. inst->buffers.input_meta.min_count =
  2164. inst->buffers.input.min_count;
  2165. inst->buffers.input_meta.extra_count =
  2166. inst->buffers.input.extra_count;
  2167. inst->buffers.input_meta.actual_count =
  2168. inst->buffers.input.actual_count;
  2169. inst->buffers.input_meta.size = fmt->fmt.meta.buffersize;
  2170. } else {
  2171. fmt->fmt.meta.buffersize = 0;
  2172. inst->buffers.input_meta.min_count = 0;
  2173. inst->buffers.input_meta.extra_count = 0;
  2174. inst->buffers.input_meta.actual_count = 0;
  2175. inst->buffers.input_meta.size = 0;
  2176. }
  2177. i_vpr_h(inst,
  2178. "%s: type: INPUT_META, size %u min_count %d extra_count %d\n",
  2179. __func__, fmt->fmt.meta.buffersize,
  2180. inst->buffers.input_meta.min_count,
  2181. inst->buffers.input_meta.extra_count);
  2182. } else if (f->type == OUTPUT_MPLANE) {
  2183. fmt = &inst->fmts[OUTPUT_PORT];
  2184. fmt->type = OUTPUT_MPLANE;
  2185. if (inst->bufq[INPUT_PORT].vb2q->streaming) {
  2186. f->fmt.pix_mp.height = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  2187. f->fmt.pix_mp.width = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  2188. }
  2189. fmt->fmt.pix_mp.pixelformat = f->fmt.pix_mp.pixelformat;
  2190. fmt->fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(
  2191. fmt->fmt.pix_mp.pixelformat, f->fmt.pix_mp.width);
  2192. fmt->fmt.pix_mp.height = VIDEO_Y_SCANLINES(
  2193. fmt->fmt.pix_mp.pixelformat,
  2194. f->fmt.pix_mp.height);
  2195. fmt->fmt.pix_mp.num_planes = 1;
  2196. fmt->fmt.pix_mp.plane_fmt[0].bytesperline =
  2197. VIDEO_Y_STRIDE_BYTES(
  2198. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat,
  2199. f->fmt.pix_mp.width);
  2200. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  2201. buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  2202. if (!inst->bufq[INPUT_PORT].vb2q->streaming)
  2203. inst->buffers.output.min_count = call_session_op(core,
  2204. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  2205. inst->buffers.output.extra_count = call_session_op(core,
  2206. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  2207. if (inst->buffers.output.actual_count <
  2208. inst->buffers.output.min_count +
  2209. inst->buffers.output.extra_count) {
  2210. inst->buffers.output.actual_count =
  2211. inst->buffers.output.min_count +
  2212. inst->buffers.output.extra_count;
  2213. }
  2214. inst->buffers.output.size =
  2215. fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
  2216. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  2217. msm_vidc_update_cap_value(inst, PIX_FMTS, pix_fmt, __func__);
  2218. /* update crop while input port is not streaming */
  2219. if (!inst->bufq[INPUT_PORT].vb2q->streaming) {
  2220. inst->crop.top = 0;
  2221. inst->crop.left = 0;
  2222. inst->crop.width = f->fmt.pix_mp.width;
  2223. inst->crop.height = f->fmt.pix_mp.height;
  2224. }
  2225. i_vpr_h(inst,
  2226. "%s: type: OUTPUT, format %s width %d height %d size %u min_count %d extra_count %d\n",
  2227. __func__, v4l2_pixelfmt_name(fmt->fmt.pix_mp.pixelformat),
  2228. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height,
  2229. fmt->fmt.pix_mp.plane_fmt[0].sizeimage,
  2230. inst->buffers.output.min_count,
  2231. inst->buffers.output.extra_count);
  2232. } else if (f->type == OUTPUT_META_PLANE) {
  2233. fmt = &inst->fmts[OUTPUT_META_PORT];
  2234. fmt->type = OUTPUT_META_PLANE;
  2235. fmt->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  2236. if (is_output_meta_enabled(inst)) {
  2237. fmt->fmt.meta.buffersize = call_session_op(core,
  2238. buffer_size, inst, MSM_VIDC_BUF_OUTPUT_META);
  2239. inst->buffers.output_meta.min_count =
  2240. inst->buffers.output.min_count;
  2241. inst->buffers.output_meta.extra_count =
  2242. inst->buffers.output.extra_count;
  2243. inst->buffers.output_meta.actual_count =
  2244. inst->buffers.output.actual_count;
  2245. inst->buffers.output_meta.size = fmt->fmt.meta.buffersize;
  2246. } else {
  2247. fmt->fmt.meta.buffersize = 0;
  2248. inst->buffers.output_meta.min_count = 0;
  2249. inst->buffers.output_meta.extra_count = 0;
  2250. inst->buffers.output_meta.actual_count = 0;
  2251. inst->buffers.output_meta.size = 0;
  2252. }
  2253. i_vpr_h(inst,
  2254. "%s: type: OUTPUT_META, size %u min_count %d extra_count %d\n",
  2255. __func__, fmt->fmt.meta.buffersize,
  2256. inst->buffers.output_meta.min_count,
  2257. inst->buffers.output_meta.extra_count);
  2258. } else {
  2259. i_vpr_e(inst, "%s: invalid type %d\n", __func__, f->type);
  2260. goto err_invalid_fmt;
  2261. }
  2262. memcpy(f, fmt, sizeof(struct v4l2_format));
  2263. err_invalid_fmt:
  2264. return rc;
  2265. }
  2266. int msm_vdec_g_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  2267. {
  2268. int rc = 0;
  2269. int port;
  2270. if (!inst) {
  2271. d_vpr_e("%s: invalid params\n", __func__);
  2272. return -EINVAL;
  2273. }
  2274. port = v4l2_type_to_driver_port(inst, f->type, __func__);
  2275. if (port < 0)
  2276. return -EINVAL;
  2277. memcpy(f, &inst->fmts[port], sizeof(struct v4l2_format));
  2278. return rc;
  2279. }
  2280. int msm_vdec_s_selection(struct msm_vidc_inst* inst, struct v4l2_selection* s)
  2281. {
  2282. if (!inst || !s) {
  2283. d_vpr_e("%s: invalid params\n", __func__);
  2284. return -EINVAL;
  2285. }
  2286. i_vpr_e(inst, "%s: unsupported\n", __func__);
  2287. return -EINVAL;
  2288. }
  2289. int msm_vdec_g_selection(struct msm_vidc_inst* inst, struct v4l2_selection* s)
  2290. {
  2291. if (!inst || !s) {
  2292. d_vpr_e("%s: invalid params\n", __func__);
  2293. return -EINVAL;
  2294. }
  2295. if (s->type != OUTPUT_MPLANE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2296. i_vpr_e(inst, "%s: invalid type %d\n", __func__, s->type);
  2297. return -EINVAL;
  2298. }
  2299. switch (s->target) {
  2300. case V4L2_SEL_TGT_CROP_BOUNDS:
  2301. case V4L2_SEL_TGT_CROP_DEFAULT:
  2302. case V4L2_SEL_TGT_CROP:
  2303. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  2304. case V4L2_SEL_TGT_COMPOSE_PADDED:
  2305. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  2306. case V4L2_SEL_TGT_COMPOSE:
  2307. s->r.left = inst->crop.left;
  2308. s->r.top = inst->crop.top;
  2309. s->r.width = inst->crop.width;
  2310. s->r.height = inst->crop.height;
  2311. break;
  2312. default:
  2313. i_vpr_e(inst, "%s: invalid target %d\n",
  2314. __func__, s->target);
  2315. return -EINVAL;
  2316. }
  2317. i_vpr_h(inst, "%s: target %d, r [%d, %d, %d, %d]\n",
  2318. __func__, s->target, s->r.top, s->r.left,
  2319. s->r.width, s->r.height);
  2320. return 0;
  2321. }
  2322. int msm_vdec_s_param(struct msm_vidc_inst *inst,
  2323. struct v4l2_streamparm *s_parm)
  2324. {
  2325. int rc = 0;
  2326. struct msm_vidc_inst_capability *capability = NULL;
  2327. struct v4l2_fract *timeperframe = NULL;
  2328. u32 q16_rate, max_rate, default_rate;
  2329. u64 us_per_frame = 0, input_rate = 0;
  2330. bool is_frame_rate = false;
  2331. if (!inst || !s_parm) {
  2332. d_vpr_e("%s: invalid params\n", __func__);
  2333. return -EINVAL;
  2334. }
  2335. capability = inst->capabilities;
  2336. if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  2337. timeperframe = &s_parm->parm.output.timeperframe;
  2338. max_rate = capability->cap[FRAME_RATE].max >> 16;
  2339. default_rate = capability->cap[FRAME_RATE].value >> 16;
  2340. is_frame_rate = true;
  2341. } else {
  2342. timeperframe = &s_parm->parm.capture.timeperframe;
  2343. max_rate = capability->cap[OPERATING_RATE].max >> 16;
  2344. default_rate = capability->cap[OPERATING_RATE].value >> 16;
  2345. }
  2346. if (!timeperframe->denominator || !timeperframe->numerator) {
  2347. i_vpr_e(inst, "%s: type %s, invalid rate\n", __func__,
  2348. v4l2_type_name(s_parm->type));
  2349. input_rate = default_rate;
  2350. goto set_default;
  2351. }
  2352. us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
  2353. do_div(us_per_frame, timeperframe->denominator);
  2354. if (!us_per_frame) {
  2355. i_vpr_e(inst, "%s: us_per_frame is zero\n", __func__);
  2356. rc = -EINVAL;
  2357. goto exit;
  2358. }
  2359. input_rate = (u64)USEC_PER_SEC;
  2360. do_div(input_rate, us_per_frame);
  2361. set_default:
  2362. i_vpr_h(inst, "%s: type %s, %s value %llu\n",
  2363. __func__, v4l2_type_name(s_parm->type),
  2364. is_frame_rate ? "frame rate" : "operating rate", input_rate);
  2365. q16_rate = (u32)input_rate << 16;
  2366. msm_vidc_update_cap_value(inst, is_frame_rate ? FRAME_RATE : OPERATING_RATE,
  2367. q16_rate, __func__);
  2368. if (is_realtime_session(inst) &&
  2369. ((s_parm->type == INPUT_MPLANE && inst->bufq[INPUT_PORT].vb2q->streaming) ||
  2370. (s_parm->type == OUTPUT_MPLANE && inst->bufq[OUTPUT_PORT].vb2q->streaming))) {
  2371. rc = msm_vidc_check_core_mbps(inst);
  2372. if (rc) {
  2373. i_vpr_e(inst, "%s: unsupported load\n", __func__);
  2374. goto reset_rate;
  2375. }
  2376. rc = input_rate > max_rate;
  2377. if (rc) {
  2378. i_vpr_e(inst, "%s: unsupported rate %llu, max %u\n", __func__,
  2379. input_rate, max_rate);
  2380. rc = -ENOMEM;
  2381. goto reset_rate;
  2382. }
  2383. }
  2384. inst->priority_level = MSM_VIDC_PRIORITY_HIGH;
  2385. if (is_frame_rate)
  2386. capability->cap[FRAME_RATE].flags |= CAP_FLAG_CLIENT_SET;
  2387. else
  2388. capability->cap[OPERATING_RATE].flags |= CAP_FLAG_CLIENT_SET;
  2389. return 0;
  2390. reset_rate:
  2391. if (rc) {
  2392. i_vpr_e(inst, "%s: setting rate %llu failed, reset to %u\n", __func__,
  2393. input_rate, default_rate);
  2394. msm_vidc_update_cap_value(inst, is_frame_rate ? FRAME_RATE : OPERATING_RATE,
  2395. default_rate << 16, __func__);
  2396. }
  2397. exit:
  2398. return rc;
  2399. }
  2400. int msm_vdec_g_param(struct msm_vidc_inst *inst,
  2401. struct v4l2_streamparm *s_parm)
  2402. {
  2403. struct msm_vidc_inst_capability *capability = NULL;
  2404. struct v4l2_fract *timeperframe = NULL;
  2405. if (!inst || !s_parm) {
  2406. d_vpr_e("%s: invalid params\n", __func__);
  2407. return -EINVAL;
  2408. }
  2409. capability = inst->capabilities;
  2410. if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  2411. timeperframe = &s_parm->parm.output.timeperframe;
  2412. timeperframe->numerator = 1;
  2413. timeperframe->denominator =
  2414. capability->cap[FRAME_RATE].value >> 16;
  2415. } else {
  2416. timeperframe = &s_parm->parm.capture.timeperframe;
  2417. timeperframe->numerator = 1;
  2418. timeperframe->denominator =
  2419. capability->cap[OPERATING_RATE].value >> 16;
  2420. }
  2421. i_vpr_h(inst, "%s: type %u, num %u denom %u\n",
  2422. __func__, s_parm->type, timeperframe->numerator,
  2423. timeperframe->denominator);
  2424. return 0;
  2425. }
  2426. int msm_vdec_subscribe_event(struct msm_vidc_inst *inst,
  2427. const struct v4l2_event_subscription *sub)
  2428. {
  2429. int rc = 0;
  2430. if (!inst || !sub) {
  2431. d_vpr_e("%s: invalid params\n", __func__);
  2432. return -EINVAL;
  2433. }
  2434. switch (sub->type) {
  2435. case V4L2_EVENT_EOS:
  2436. rc = v4l2_event_subscribe(&inst->event_handler, sub, MAX_EVENTS, NULL);
  2437. break;
  2438. case V4L2_EVENT_SOURCE_CHANGE:
  2439. rc = v4l2_src_change_event_subscribe(&inst->event_handler, sub);
  2440. break;
  2441. case V4L2_EVENT_CTRL:
  2442. rc = v4l2_ctrl_subscribe_event(&inst->event_handler, sub);
  2443. break;
  2444. default:
  2445. i_vpr_e(inst, "%s: invalid type %d id %d\n", __func__, sub->type, sub->id);
  2446. return -EINVAL;
  2447. }
  2448. if (rc)
  2449. i_vpr_e(inst, "%s: failed, type %d id %d\n",
  2450. __func__, sub->type, sub->id);
  2451. return rc;
  2452. }
  2453. static int msm_vdec_check_colorformat_supported(struct msm_vidc_inst* inst,
  2454. enum msm_vidc_colorformat_type colorformat)
  2455. {
  2456. bool supported = true;
  2457. /* do not reject coloformats before streamon */
  2458. if (!inst->bufq[INPUT_PORT].vb2q->streaming)
  2459. return true;
  2460. /*
  2461. * bit_depth 8 bit supports 8 bit colorformats only
  2462. * bit_depth 10 bit supports 10 bit colorformats only
  2463. * interlace supports ubwc colorformats only
  2464. */
  2465. if (inst->capabilities->cap[BIT_DEPTH].value == BIT_DEPTH_8 &&
  2466. !is_8bit_colorformat(colorformat))
  2467. supported = false;
  2468. if (inst->capabilities->cap[BIT_DEPTH].value == BIT_DEPTH_10 &&
  2469. !is_10bit_colorformat(colorformat))
  2470. supported = false;
  2471. if (inst->capabilities->cap[CODED_FRAMES].value ==
  2472. CODED_FRAMES_INTERLACE &&
  2473. !is_ubwc_colorformat(colorformat))
  2474. supported = false;
  2475. return supported;
  2476. }
  2477. int msm_vdec_enum_fmt(struct msm_vidc_inst *inst, struct v4l2_fmtdesc *f)
  2478. {
  2479. int rc = 0;
  2480. struct msm_vidc_core *core;
  2481. u32 array[32] = {0};
  2482. u32 i = 0;
  2483. if (!inst || !inst->core || !inst->capabilities || !f ||
  2484. f->index >= ARRAY_SIZE(array)) {
  2485. d_vpr_e("%s: invalid params\n", __func__);
  2486. return -EINVAL;
  2487. }
  2488. core = inst->core;
  2489. if (f->type == INPUT_MPLANE) {
  2490. u32 codecs = core->capabilities[DEC_CODECS].value;
  2491. u32 idx = 0;
  2492. for (i = 0; i <= 31; i++) {
  2493. if (codecs & BIT(i)) {
  2494. if (idx >= ARRAY_SIZE(array))
  2495. break;
  2496. array[idx] = codecs & BIT(i);
  2497. idx++;
  2498. }
  2499. }
  2500. if (!array[f->index])
  2501. return -EINVAL;
  2502. f->pixelformat = v4l2_codec_from_driver(array[f->index],
  2503. __func__);
  2504. if (!f->pixelformat)
  2505. return -EINVAL;
  2506. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  2507. strlcpy(f->description, "codec", sizeof(f->description));
  2508. } else if (f->type == OUTPUT_MPLANE) {
  2509. u32 formats = inst->capabilities->cap[PIX_FMTS].step_or_mask;
  2510. u32 idx = 0;
  2511. for (i = 0; i <= 31; i++) {
  2512. if (formats & BIT(i)) {
  2513. if (idx >= ARRAY_SIZE(array))
  2514. break;
  2515. if (msm_vdec_check_colorformat_supported(inst,
  2516. formats & BIT(i))) {
  2517. array[idx] = formats & BIT(i);
  2518. idx++;
  2519. }
  2520. }
  2521. }
  2522. if (!array[f->index])
  2523. return -EINVAL;
  2524. f->pixelformat = v4l2_colorformat_from_driver(array[f->index],
  2525. __func__);
  2526. if (!f->pixelformat)
  2527. return -EINVAL;
  2528. strlcpy(f->description, "colorformat", sizeof(f->description));
  2529. } else if (f->type == INPUT_META_PLANE || f->type == OUTPUT_META_PLANE) {
  2530. if (!f->index) {
  2531. f->pixelformat = V4L2_META_FMT_VIDC;
  2532. strlcpy(f->description, "metadata", sizeof(f->description));
  2533. } else {
  2534. return -EINVAL;
  2535. }
  2536. }
  2537. memset(f->reserved, 0, sizeof(f->reserved));
  2538. i_vpr_h(inst, "%s: index %d, %s: %s, flags %#x\n",
  2539. __func__, f->index, f->description, v4l2_pixelfmt_name(f->pixelformat), f->flags);
  2540. return rc;
  2541. }
  2542. int msm_vdec_inst_init(struct msm_vidc_inst *inst)
  2543. {
  2544. int rc = 0;
  2545. struct msm_vidc_core *core;
  2546. struct v4l2_format *f;
  2547. if (!inst || !inst->core) {
  2548. d_vpr_e("%s: invalid params\n", __func__);
  2549. return -EINVAL;
  2550. }
  2551. core = inst->core;
  2552. INIT_DELAYED_WORK(&inst->decode_batch.work, msm_vidc_batch_handler);
  2553. if (core->capabilities[DECODE_BATCH].value) {
  2554. inst->decode_batch.enable = true;
  2555. inst->decode_batch.size = MAX_DEC_BATCH_SIZE;
  2556. }
  2557. if (core->capabilities[DCVS].value)
  2558. inst->power.dcvs_mode = true;
  2559. f = &inst->fmts[INPUT_PORT];
  2560. f->type = INPUT_MPLANE;
  2561. f->fmt.pix_mp.width = DEFAULT_WIDTH;
  2562. f->fmt.pix_mp.height = DEFAULT_HEIGHT;
  2563. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  2564. f->fmt.pix_mp.num_planes = 1;
  2565. f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
  2566. f->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  2567. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  2568. f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  2569. inst->buffers.input.min_count = call_session_op(core,
  2570. min_count, inst, MSM_VIDC_BUF_INPUT);
  2571. inst->buffers.input.extra_count = call_session_op(core,
  2572. extra_count, inst, MSM_VIDC_BUF_INPUT);
  2573. inst->buffers.input.actual_count =
  2574. inst->buffers.input.min_count +
  2575. inst->buffers.input.extra_count;
  2576. inst->buffers.input.size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
  2577. inst->crop.left = inst->crop.top = 0;
  2578. inst->crop.width = f->fmt.pix_mp.width;
  2579. inst->crop.height = f->fmt.pix_mp.height;
  2580. f = &inst->fmts[INPUT_META_PORT];
  2581. f->type = INPUT_META_PLANE;
  2582. f->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  2583. f->fmt.meta.buffersize = 0;
  2584. inst->buffers.input_meta.min_count = 0;
  2585. inst->buffers.input_meta.extra_count = 0;
  2586. inst->buffers.input_meta.actual_count = 0;
  2587. inst->buffers.input_meta.size = 0;
  2588. f = &inst->fmts[OUTPUT_PORT];
  2589. f->type = OUTPUT_MPLANE;
  2590. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_VIDC_NV12C;
  2591. f->fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(f->fmt.pix_mp.pixelformat,
  2592. DEFAULT_WIDTH);
  2593. f->fmt.pix_mp.height = VIDEO_Y_SCANLINES(f->fmt.pix_mp.pixelformat,
  2594. DEFAULT_HEIGHT);
  2595. f->fmt.pix_mp.num_planes = 1;
  2596. f->fmt.pix_mp.plane_fmt[0].bytesperline =
  2597. VIDEO_Y_STRIDE_BYTES(
  2598. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat,
  2599. DEFAULT_WIDTH);
  2600. f->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  2601. buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  2602. f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  2603. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  2604. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  2605. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  2606. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  2607. inst->buffers.output.min_count = call_session_op(core,
  2608. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  2609. inst->buffers.output.extra_count = call_session_op(core,
  2610. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  2611. inst->buffers.output.actual_count =
  2612. inst->buffers.output.min_count +
  2613. inst->buffers.output.extra_count;
  2614. inst->buffers.output.size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
  2615. inst->max_map_output_count = MAX_MAP_OUTPUT_COUNT;
  2616. f = &inst->fmts[OUTPUT_META_PORT];
  2617. f->type = OUTPUT_META_PLANE;
  2618. f->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  2619. f->fmt.meta.buffersize = 0;
  2620. inst->buffers.output_meta.min_count = 0;
  2621. inst->buffers.output_meta.extra_count = 0;
  2622. inst->buffers.output_meta.actual_count = 0;
  2623. inst->buffers.output_meta.size = 0;
  2624. inst->priority_level = MSM_VIDC_PRIORITY_LOW;
  2625. rc = msm_vdec_codec_change(inst,
  2626. inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat);
  2627. if (rc)
  2628. return rc;
  2629. return rc;
  2630. }
  2631. int msm_vdec_inst_deinit(struct msm_vidc_inst *inst)
  2632. {
  2633. int rc = 0;
  2634. if (!inst) {
  2635. d_vpr_e("%s: invalid params\n", __func__);
  2636. return -EINVAL;
  2637. }
  2638. /* cancel pending batch work */
  2639. cancel_batch_work(inst);
  2640. rc = msm_vidc_ctrl_deinit(inst);
  2641. if (rc)
  2642. return rc;
  2643. return rc;
  2644. }