msm_vdec.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  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);
  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 = msm_vidc_set_seq_change_at_sync_frame(inst);
  1740. if (rc)
  1741. return rc;
  1742. rc = venus_hfi_session_command(inst,
  1743. HFI_CMD_RESUME,
  1744. port,
  1745. HFI_PAYLOAD_NONE,
  1746. NULL,
  1747. 0);
  1748. if (rc)
  1749. return rc;
  1750. } else {
  1751. i_vpr_e(inst, "%s: unknown cmd %d\n", __func__, cmd);
  1752. return -EINVAL;
  1753. }
  1754. return 0;
  1755. }
  1756. int msm_vdec_s_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  1757. {
  1758. int rc = 0;
  1759. struct msm_vidc_core *core;
  1760. struct v4l2_format *fmt;
  1761. u32 codec_align, pix_fmt;
  1762. if (!inst || !inst->core) {
  1763. d_vpr_e("%s: invalid params\n", __func__);
  1764. return -EINVAL;
  1765. }
  1766. core = inst->core;
  1767. if (f->type == INPUT_MPLANE) {
  1768. if (inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat !=
  1769. f->fmt.pix_mp.pixelformat) {
  1770. rc = msm_vdec_codec_change(inst, f->fmt.pix_mp.pixelformat);
  1771. if (rc)
  1772. goto err_invalid_fmt;
  1773. }
  1774. fmt = &inst->fmts[INPUT_PORT];
  1775. fmt->type = INPUT_MPLANE;
  1776. codec_align = inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat ==
  1777. V4L2_PIX_FMT_HEVC ? 32 : 16;
  1778. fmt->fmt.pix_mp.width = ALIGN(f->fmt.pix_mp.width, codec_align);
  1779. fmt->fmt.pix_mp.height = ALIGN(f->fmt.pix_mp.height, codec_align);
  1780. fmt->fmt.pix_mp.pixelformat = f->fmt.pix_mp.pixelformat;
  1781. fmt->fmt.pix_mp.num_planes = 1;
  1782. fmt->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
  1783. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  1784. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  1785. inst->buffers.input.min_count = call_session_op(core,
  1786. min_count, inst, MSM_VIDC_BUF_INPUT);
  1787. inst->buffers.input.extra_count = call_session_op(core,
  1788. extra_count, inst, MSM_VIDC_BUF_INPUT);
  1789. if (inst->buffers.input.actual_count <
  1790. inst->buffers.input.min_count +
  1791. inst->buffers.input.extra_count) {
  1792. inst->buffers.input.actual_count =
  1793. inst->buffers.input.min_count +
  1794. inst->buffers.input.extra_count;
  1795. }
  1796. inst->buffers.input.size =
  1797. fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
  1798. /* update crop dimensions */
  1799. inst->crop.left = inst->crop.top = 0;
  1800. inst->crop.width = f->fmt.pix_mp.width;
  1801. inst->crop.height = f->fmt.pix_mp.height;
  1802. i_vpr_h(inst,
  1803. "%s: type: INPUT, codec %s width %d height %d size %u min_count %d extra_count %d\n",
  1804. __func__, v4l2_pixelfmt_name(f->fmt.pix_mp.pixelformat),
  1805. f->fmt.pix_mp.width, f->fmt.pix_mp.height,
  1806. fmt->fmt.pix_mp.plane_fmt[0].sizeimage,
  1807. inst->buffers.input.min_count,
  1808. inst->buffers.input.extra_count);
  1809. } else if (f->type == INPUT_META_PLANE) {
  1810. fmt = &inst->fmts[INPUT_META_PORT];
  1811. fmt->type = INPUT_META_PLANE;
  1812. fmt->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  1813. if (is_input_meta_enabled(inst)) {
  1814. fmt->fmt.meta.buffersize = call_session_op(core,
  1815. buffer_size, inst, MSM_VIDC_BUF_INPUT_META);
  1816. inst->buffers.input_meta.min_count =
  1817. inst->buffers.input.min_count;
  1818. inst->buffers.input_meta.extra_count =
  1819. inst->buffers.input.extra_count;
  1820. inst->buffers.input_meta.actual_count =
  1821. inst->buffers.input.actual_count;
  1822. inst->buffers.input_meta.size = fmt->fmt.meta.buffersize;
  1823. } else {
  1824. fmt->fmt.meta.buffersize = 0;
  1825. inst->buffers.input_meta.min_count = 0;
  1826. inst->buffers.input_meta.extra_count = 0;
  1827. inst->buffers.input_meta.actual_count = 0;
  1828. inst->buffers.input_meta.size = 0;
  1829. }
  1830. i_vpr_h(inst,
  1831. "%s: type: INPUT_META, size %u min_count %d extra_count %d\n",
  1832. __func__, fmt->fmt.meta.buffersize,
  1833. inst->buffers.input_meta.min_count,
  1834. inst->buffers.input_meta.extra_count);
  1835. } else if (f->type == OUTPUT_MPLANE) {
  1836. fmt = &inst->fmts[OUTPUT_PORT];
  1837. fmt->type = OUTPUT_MPLANE;
  1838. if (inst->vb2q[INPUT_PORT].streaming) {
  1839. f->fmt.pix_mp.height = inst->fmts[INPUT_PORT].fmt.pix_mp.height;
  1840. f->fmt.pix_mp.width = inst->fmts[INPUT_PORT].fmt.pix_mp.width;
  1841. }
  1842. fmt->fmt.pix_mp.pixelformat = f->fmt.pix_mp.pixelformat;
  1843. fmt->fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(
  1844. fmt->fmt.pix_mp.pixelformat, f->fmt.pix_mp.width);
  1845. fmt->fmt.pix_mp.height = VIDEO_Y_SCANLINES(
  1846. fmt->fmt.pix_mp.pixelformat,
  1847. f->fmt.pix_mp.height);
  1848. fmt->fmt.pix_mp.num_planes = 1;
  1849. fmt->fmt.pix_mp.plane_fmt[0].bytesperline =
  1850. VIDEO_Y_STRIDE_BYTES(
  1851. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat,
  1852. f->fmt.pix_mp.width);
  1853. fmt->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  1854. buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  1855. if (!inst->vb2q[INPUT_PORT].streaming)
  1856. inst->buffers.output.min_count = call_session_op(core,
  1857. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  1858. inst->buffers.output.extra_count = call_session_op(core,
  1859. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  1860. if (inst->buffers.output.actual_count <
  1861. inst->buffers.output.min_count +
  1862. inst->buffers.output.extra_count) {
  1863. inst->buffers.output.actual_count =
  1864. inst->buffers.output.min_count +
  1865. inst->buffers.output.extra_count;
  1866. }
  1867. inst->buffers.output.size =
  1868. fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
  1869. pix_fmt = v4l2_colorformat_to_driver(f->fmt.pix_mp.pixelformat, __func__);
  1870. msm_vidc_update_cap_value(inst, PIX_FMTS, pix_fmt, __func__);
  1871. i_vpr_h(inst,
  1872. "%s: type: OUTPUT, format %s width %d height %d size %u min_count %d extra_count %d\n",
  1873. __func__, v4l2_pixelfmt_name(fmt->fmt.pix_mp.pixelformat),
  1874. fmt->fmt.pix_mp.width, fmt->fmt.pix_mp.height,
  1875. fmt->fmt.pix_mp.plane_fmt[0].sizeimage,
  1876. inst->buffers.output.min_count,
  1877. inst->buffers.output.extra_count);
  1878. } else if (f->type == OUTPUT_META_PLANE) {
  1879. fmt = &inst->fmts[OUTPUT_META_PORT];
  1880. fmt->type = OUTPUT_META_PLANE;
  1881. fmt->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  1882. if (is_output_meta_enabled(inst)) {
  1883. fmt->fmt.meta.buffersize = call_session_op(core,
  1884. buffer_size, inst, MSM_VIDC_BUF_OUTPUT_META);
  1885. inst->buffers.output_meta.min_count =
  1886. inst->buffers.output.min_count;
  1887. inst->buffers.output_meta.extra_count =
  1888. inst->buffers.output.extra_count;
  1889. inst->buffers.output_meta.actual_count =
  1890. inst->buffers.output.actual_count;
  1891. inst->buffers.output_meta.size = fmt->fmt.meta.buffersize;
  1892. } else {
  1893. fmt->fmt.meta.buffersize = 0;
  1894. inst->buffers.output_meta.min_count = 0;
  1895. inst->buffers.output_meta.extra_count = 0;
  1896. inst->buffers.output_meta.actual_count = 0;
  1897. inst->buffers.output_meta.size = 0;
  1898. }
  1899. i_vpr_h(inst,
  1900. "%s: type: OUTPUT_META, size %u min_count %d extra_count %d\n",
  1901. __func__, fmt->fmt.meta.buffersize,
  1902. inst->buffers.output_meta.min_count,
  1903. inst->buffers.output_meta.extra_count);
  1904. } else {
  1905. i_vpr_e(inst, "%s: invalid type %d\n", __func__, f->type);
  1906. goto err_invalid_fmt;
  1907. }
  1908. memcpy(f, fmt, sizeof(struct v4l2_format));
  1909. err_invalid_fmt:
  1910. return rc;
  1911. }
  1912. int msm_vdec_g_fmt(struct msm_vidc_inst *inst, struct v4l2_format *f)
  1913. {
  1914. int rc = 0;
  1915. int port;
  1916. if (!inst) {
  1917. d_vpr_e("%s: invalid params\n", __func__);
  1918. return -EINVAL;
  1919. }
  1920. port = v4l2_type_to_driver_port(inst, f->type, __func__);
  1921. if (port < 0)
  1922. return -EINVAL;
  1923. memcpy(f, &inst->fmts[port], sizeof(struct v4l2_format));
  1924. return rc;
  1925. }
  1926. int msm_vdec_s_selection(struct msm_vidc_inst* inst, struct v4l2_selection* s)
  1927. {
  1928. if (!inst || !s) {
  1929. d_vpr_e("%s: invalid params\n", __func__);
  1930. return -EINVAL;
  1931. }
  1932. i_vpr_e(inst, "%s: unsupported\n", __func__);
  1933. return -EINVAL;
  1934. }
  1935. int msm_vdec_g_selection(struct msm_vidc_inst* inst, struct v4l2_selection* s)
  1936. {
  1937. if (!inst || !s) {
  1938. d_vpr_e("%s: invalid params\n", __func__);
  1939. return -EINVAL;
  1940. }
  1941. switch (s->target) {
  1942. case V4L2_SEL_TGT_CROP_BOUNDS:
  1943. case V4L2_SEL_TGT_CROP_DEFAULT:
  1944. case V4L2_SEL_TGT_CROP:
  1945. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1946. case V4L2_SEL_TGT_COMPOSE_PADDED:
  1947. case V4L2_SEL_TGT_COMPOSE_DEFAULT:
  1948. case V4L2_SEL_TGT_COMPOSE:
  1949. default:
  1950. s->r.left = inst->crop.left;
  1951. s->r.top = inst->crop.top;
  1952. s->r.width = inst->crop.width;
  1953. s->r.height = inst->crop.height;
  1954. break;
  1955. }
  1956. i_vpr_h(inst, "%s: target %d, r [%d, %d, %d, %d]\n",
  1957. __func__, s->target, s->r.top, s->r.left,
  1958. s->r.width, s->r.height);
  1959. return 0;
  1960. }
  1961. int msm_vdec_s_param(struct msm_vidc_inst *inst,
  1962. struct v4l2_streamparm *s_parm)
  1963. {
  1964. int rc = 0;
  1965. struct msm_vidc_inst_capability *capability = NULL;
  1966. struct v4l2_fract *timeperframe = NULL;
  1967. u32 q16_rate, max_rate, default_rate;
  1968. u64 us_per_frame = 0, input_rate = 0;
  1969. bool is_frame_rate = false;
  1970. if (!inst || !s_parm) {
  1971. d_vpr_e("%s: invalid params\n", __func__);
  1972. return -EINVAL;
  1973. }
  1974. capability = inst->capabilities;
  1975. if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  1976. timeperframe = &s_parm->parm.output.timeperframe;
  1977. max_rate = capability->cap[FRAME_RATE].max >> 16;
  1978. default_rate = capability->cap[FRAME_RATE].value >> 16;
  1979. is_frame_rate = true;
  1980. } else {
  1981. timeperframe = &s_parm->parm.capture.timeperframe;
  1982. max_rate = capability->cap[OPERATING_RATE].max >> 16;
  1983. default_rate = capability->cap[OPERATING_RATE].value >> 16;
  1984. }
  1985. if (!timeperframe->denominator || !timeperframe->numerator) {
  1986. i_vpr_e(inst, "%s: type %s, invalid rate\n", __func__,
  1987. v4l2_type_name(s_parm->type));
  1988. input_rate = default_rate;
  1989. goto set_default;
  1990. }
  1991. us_per_frame = timeperframe->numerator * (u64)USEC_PER_SEC;
  1992. do_div(us_per_frame, timeperframe->denominator);
  1993. if (!us_per_frame) {
  1994. i_vpr_e(inst, "%s: us_per_frame is zero\n", __func__);
  1995. rc = -EINVAL;
  1996. goto exit;
  1997. }
  1998. input_rate = (u64)USEC_PER_SEC;
  1999. do_div(input_rate, us_per_frame);
  2000. set_default:
  2001. i_vpr_h(inst, "%s: type %s, %s value %d\n",
  2002. __func__, v4l2_type_name(s_parm->type),
  2003. is_frame_rate ? "frame rate" : "operating rate", input_rate);
  2004. q16_rate = (u32)input_rate << 16;
  2005. msm_vidc_update_cap_value(inst, is_frame_rate ? FRAME_RATE : OPERATING_RATE,
  2006. q16_rate, __func__);
  2007. if (is_realtime_session(inst) &&
  2008. ((s_parm->type == INPUT_MPLANE && inst->vb2q[INPUT_PORT].streaming) ||
  2009. (s_parm->type == OUTPUT_MPLANE && inst->vb2q[OUTPUT_PORT].streaming))) {
  2010. rc = msm_vidc_check_core_mbps(inst);
  2011. if (rc) {
  2012. i_vpr_e(inst, "%s: unsupported load\n", __func__);
  2013. goto reset_rate;
  2014. }
  2015. rc = input_rate > max_rate;
  2016. if (rc) {
  2017. i_vpr_e(inst, "%s: unsupported rate %u, max %u\n", __func__,
  2018. input_rate, max_rate);
  2019. rc = -ENOMEM;
  2020. goto reset_rate;
  2021. }
  2022. }
  2023. inst->priority_level = MSM_VIDC_PRIORITY_HIGH;
  2024. if (is_frame_rate)
  2025. capability->cap[FRAME_RATE].flags |= CAP_FLAG_CLIENT_SET;
  2026. else
  2027. capability->cap[OPERATING_RATE].flags |= CAP_FLAG_CLIENT_SET;
  2028. return 0;
  2029. reset_rate:
  2030. if (rc) {
  2031. i_vpr_e(inst, "%s: setting rate %u failed, reset to %u\n", __func__,
  2032. input_rate, default_rate);
  2033. msm_vidc_update_cap_value(inst, is_frame_rate ? FRAME_RATE : OPERATING_RATE,
  2034. default_rate << 16, __func__);
  2035. }
  2036. exit:
  2037. return rc;
  2038. }
  2039. int msm_vdec_g_param(struct msm_vidc_inst *inst,
  2040. struct v4l2_streamparm *s_parm)
  2041. {
  2042. struct msm_vidc_inst_capability *capability = NULL;
  2043. struct v4l2_fract *timeperframe = NULL;
  2044. if (!inst || !s_parm) {
  2045. d_vpr_e("%s: invalid params\n", __func__);
  2046. return -EINVAL;
  2047. }
  2048. capability = inst->capabilities;
  2049. if (s_parm->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
  2050. timeperframe = &s_parm->parm.output.timeperframe;
  2051. timeperframe->numerator = 1;
  2052. timeperframe->denominator =
  2053. capability->cap[FRAME_RATE].value >> 16;
  2054. } else {
  2055. timeperframe = &s_parm->parm.capture.timeperframe;
  2056. timeperframe->numerator = 1;
  2057. timeperframe->denominator =
  2058. capability->cap[OPERATING_RATE].value >> 16;
  2059. }
  2060. i_vpr_h(inst, "%s: type %u, num %u denom %u\n",
  2061. __func__, s_parm->type, timeperframe->numerator,
  2062. timeperframe->denominator);
  2063. return 0;
  2064. }
  2065. static int msm_vdec_check_colorformat_supported(struct msm_vidc_inst* inst,
  2066. enum msm_vidc_colorformat_type colorformat)
  2067. {
  2068. bool supported = true;
  2069. /* do not reject coloformats before streamon */
  2070. if (!inst->vb2q[INPUT_PORT].streaming)
  2071. return true;
  2072. /*
  2073. * bit_depth 8 bit supports 8 bit colorformats only
  2074. * bit_depth 10 bit supports 10 bit colorformats only
  2075. * interlace supports ubwc colorformats only
  2076. */
  2077. if (inst->capabilities->cap[BIT_DEPTH].value == BIT_DEPTH_8 &&
  2078. !is_8bit_colorformat(colorformat))
  2079. supported = false;
  2080. if (inst->capabilities->cap[BIT_DEPTH].value == BIT_DEPTH_10 &&
  2081. !is_10bit_colorformat(colorformat))
  2082. supported = false;
  2083. if (inst->capabilities->cap[CODED_FRAMES].value ==
  2084. CODED_FRAMES_INTERLACE &&
  2085. !is_ubwc_colorformat(colorformat))
  2086. supported = false;
  2087. return supported;
  2088. }
  2089. int msm_vdec_enum_fmt(struct msm_vidc_inst *inst, struct v4l2_fmtdesc *f)
  2090. {
  2091. int rc = 0;
  2092. struct msm_vidc_core *core;
  2093. u32 array[32] = {0};
  2094. u32 i = 0;
  2095. if (!inst || !inst->core || !inst->capabilities || !f ||
  2096. f->index >= ARRAY_SIZE(array)) {
  2097. d_vpr_e("%s: invalid params\n", __func__);
  2098. return -EINVAL;
  2099. }
  2100. core = inst->core;
  2101. if (f->type == INPUT_MPLANE) {
  2102. u32 codecs = core->capabilities[DEC_CODECS].value;
  2103. u32 idx = 0;
  2104. for (i = 0; i <= 31; i++) {
  2105. if (codecs & BIT(i)) {
  2106. if (idx >= ARRAY_SIZE(array))
  2107. break;
  2108. array[idx] = codecs & BIT(i);
  2109. idx++;
  2110. }
  2111. }
  2112. if (!array[f->index])
  2113. return -EINVAL;
  2114. f->pixelformat = v4l2_codec_from_driver(array[f->index],
  2115. __func__);
  2116. if (!f->pixelformat)
  2117. return -EINVAL;
  2118. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  2119. strlcpy(f->description, "codec", sizeof(f->description));
  2120. } else if (f->type == OUTPUT_MPLANE) {
  2121. u32 formats = inst->capabilities->cap[PIX_FMTS].step_or_mask;
  2122. u32 idx = 0;
  2123. for (i = 0; i <= 31; i++) {
  2124. if (formats & BIT(i)) {
  2125. if (idx >= ARRAY_SIZE(array))
  2126. break;
  2127. if (msm_vdec_check_colorformat_supported(inst,
  2128. formats & BIT(i))) {
  2129. array[idx] = formats & BIT(i);
  2130. idx++;
  2131. }
  2132. }
  2133. }
  2134. if (!array[f->index])
  2135. return -EINVAL;
  2136. f->pixelformat = v4l2_colorformat_from_driver(array[f->index],
  2137. __func__);
  2138. if (!f->pixelformat)
  2139. return -EINVAL;
  2140. strlcpy(f->description, "colorformat", sizeof(f->description));
  2141. } else if (f->type == INPUT_META_PLANE || f->type == OUTPUT_META_PLANE) {
  2142. if (!f->index) {
  2143. f->pixelformat = V4L2_META_FMT_VIDC;
  2144. strlcpy(f->description, "metadata", sizeof(f->description));
  2145. } else {
  2146. return -EINVAL;
  2147. }
  2148. }
  2149. memset(f->reserved, 0, sizeof(f->reserved));
  2150. i_vpr_h(inst, "%s: index %d, %s: %s, flags %#x\n",
  2151. __func__, f->index, f->description, v4l2_pixelfmt_name(f->pixelformat), f->flags);
  2152. return rc;
  2153. }
  2154. int msm_vdec_inst_init(struct msm_vidc_inst *inst)
  2155. {
  2156. int rc = 0;
  2157. struct msm_vidc_core *core;
  2158. struct v4l2_format *f;
  2159. if (!inst || !inst->core) {
  2160. d_vpr_e("%s: invalid params\n", __func__);
  2161. return -EINVAL;
  2162. }
  2163. core = inst->core;
  2164. INIT_DELAYED_WORK(&inst->decode_batch.work, msm_vidc_batch_handler);
  2165. if (core->capabilities[DECODE_BATCH].value) {
  2166. inst->decode_batch.enable = true;
  2167. inst->decode_batch.size = MAX_DEC_BATCH_SIZE;
  2168. }
  2169. if (core->capabilities[DCVS].value)
  2170. inst->power.dcvs_mode = true;
  2171. f = &inst->fmts[INPUT_PORT];
  2172. f->type = INPUT_MPLANE;
  2173. f->fmt.pix_mp.width = DEFAULT_WIDTH;
  2174. f->fmt.pix_mp.height = DEFAULT_HEIGHT;
  2175. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
  2176. f->fmt.pix_mp.num_planes = 1;
  2177. f->fmt.pix_mp.plane_fmt[0].bytesperline = 0;
  2178. f->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  2179. buffer_size, inst, MSM_VIDC_BUF_INPUT);
  2180. inst->buffers.input.min_count = call_session_op(core,
  2181. min_count, inst, MSM_VIDC_BUF_INPUT);
  2182. inst->buffers.input.extra_count = call_session_op(core,
  2183. extra_count, inst, MSM_VIDC_BUF_INPUT);
  2184. inst->buffers.input.actual_count =
  2185. inst->buffers.input.min_count +
  2186. inst->buffers.input.extra_count;
  2187. inst->buffers.input.size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
  2188. inst->crop.left = inst->crop.top = 0;
  2189. inst->crop.width = f->fmt.pix_mp.width;
  2190. inst->crop.height = f->fmt.pix_mp.height;
  2191. f = &inst->fmts[INPUT_META_PORT];
  2192. f->type = INPUT_META_PLANE;
  2193. f->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  2194. f->fmt.meta.buffersize = 0;
  2195. inst->buffers.input_meta.min_count = 0;
  2196. inst->buffers.input_meta.extra_count = 0;
  2197. inst->buffers.input_meta.actual_count = 0;
  2198. inst->buffers.input_meta.size = 0;
  2199. f = &inst->fmts[OUTPUT_PORT];
  2200. f->type = OUTPUT_MPLANE;
  2201. f->fmt.pix_mp.pixelformat = V4L2_PIX_FMT_VIDC_NV12C;
  2202. f->fmt.pix_mp.width = VIDEO_Y_STRIDE_PIX(f->fmt.pix_mp.pixelformat,
  2203. DEFAULT_WIDTH);
  2204. f->fmt.pix_mp.height = VIDEO_Y_SCANLINES(f->fmt.pix_mp.pixelformat,
  2205. DEFAULT_HEIGHT);
  2206. f->fmt.pix_mp.num_planes = 1;
  2207. f->fmt.pix_mp.plane_fmt[0].bytesperline =
  2208. VIDEO_Y_STRIDE_BYTES(
  2209. inst->fmts[OUTPUT_PORT].fmt.pix_mp.pixelformat,
  2210. DEFAULT_WIDTH);
  2211. f->fmt.pix_mp.plane_fmt[0].sizeimage = call_session_op(core,
  2212. buffer_size, inst, MSM_VIDC_BUF_OUTPUT);
  2213. f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
  2214. f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  2215. f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  2216. f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  2217. inst->buffers.output.min_count = call_session_op(core,
  2218. min_count, inst, MSM_VIDC_BUF_OUTPUT);
  2219. inst->buffers.output.extra_count = call_session_op(core,
  2220. extra_count, inst, MSM_VIDC_BUF_OUTPUT);
  2221. inst->buffers.output.actual_count =
  2222. inst->buffers.output.min_count +
  2223. inst->buffers.output.extra_count;
  2224. inst->buffers.output.size = f->fmt.pix_mp.plane_fmt[0].sizeimage;
  2225. inst->max_map_output_count = MAX_MAP_OUTPUT_COUNT;
  2226. f = &inst->fmts[OUTPUT_META_PORT];
  2227. f->type = OUTPUT_META_PLANE;
  2228. f->fmt.meta.dataformat = V4L2_META_FMT_VIDC;
  2229. f->fmt.meta.buffersize = 0;
  2230. inst->buffers.output_meta.min_count = 0;
  2231. inst->buffers.output_meta.extra_count = 0;
  2232. inst->buffers.output_meta.actual_count = 0;
  2233. inst->buffers.output_meta.size = 0;
  2234. inst->priority_level = MSM_VIDC_PRIORITY_LOW;
  2235. rc = msm_vdec_codec_change(inst,
  2236. inst->fmts[INPUT_PORT].fmt.pix_mp.pixelformat);
  2237. if (rc)
  2238. return rc;
  2239. return rc;
  2240. }
  2241. int msm_vdec_inst_deinit(struct msm_vidc_inst *inst)
  2242. {
  2243. int rc = 0;
  2244. if (!inst) {
  2245. d_vpr_e("%s: invalid params\n", __func__);
  2246. return -EINVAL;
  2247. }
  2248. /* cancel pending batch work */
  2249. cancel_batch_work(inst);
  2250. rc = msm_vidc_ctrl_deinit(inst);
  2251. if (rc)
  2252. return rc;
  2253. return rc;
  2254. }