msm_vdec.c 68 KB

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