msm_vdec.c 69 KB

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