xmixahlx

linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec_vwork-in-progr

Feb 16th, 2020
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 187.97 KB | None | 0 0
  1. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/arch/arm64/boot/dts/rockchip/rk3399.dtsi linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/arch/arm64/boot/dts/rockchip/rk3399.dtsi
  2. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/arch/arm64/boot/dts/rockchip/rk3399.dtsi 2020-02-14 22:59:03.745510923 -0800
  3. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/arch/arm64/boot/dts/rockchip/rk3399.dtsi 2020-02-14 21:55:16.925830808 -0800
  4. @@ -1277,8 +1277,20 @@
  5. interrupt-names = "vdec_mmu";
  6. clocks = <&cru ACLK_VDU>, <&cru HCLK_VDU>;
  7. clock-names = "aclk", "iface";
  8. + power-domains = <&power RK3399_PD_VDU>;
  9. #iommu-cells = <0>;
  10. - status = "disabled";
  11. + };
  12. +
  13. + vdec: video-codec@ff660000 {
  14. + compatible = "rockchip,rk3399-vdec";
  15. + reg = <0x0 0xff660000 0x0 0x400>;
  16. + interrupts = <GIC_SPI 116 IRQ_TYPE_LEVEL_HIGH 0>;
  17. + interrupt-names = "vdpu";
  18. + clocks = <&cru ACLK_VDU>, <&cru HCLK_VDU>,
  19. + <&cru SCLK_VDU_CA>, <&cru SCLK_VDU_CORE>;
  20. + clock-names = "axi", "ahb", "cabac", "core";
  21. + power-domains = <&power RK3399_PD_VDU>;
  22. + iommus = <&vdec_mmu>;
  23. };
  24.  
  25. iep_mmu: iommu@ff670800 {
  26. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/Documentation/devicetree/bindings/media/rockchip,vdec.yaml linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/Documentation/devicetree/bindings/media/rockchip,vdec.yaml
  27. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/Documentation/devicetree/bindings/media/rockchip,vdec.yaml 1969-12-31 16:00:00.000000000 -0800
  28. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/Documentation/devicetree/bindings/media/rockchip,vdec.yaml 2020-02-14 21:55:16.133824945 -0800
  29. @@ -0,0 +1,71 @@
  30. +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
  31. +%YAML 1.2
  32. +---
  33. +$id: http://devicetree.org/schemas/media/rockchip,vdec.yaml#
  34. +$schema: http://devicetree.org/meta-schemas/core.yaml#
  35. +
  36. +title: Rockchip Video Decoder (VDec) Device Tree Bindings
  37. +
  38. +maintainers:
  39. + - Heiko Stuebner <[email protected]>
  40. +
  41. +description: |-
  42. + The Rockchip rk3399 has a stateless Video Decoder that can decodes H.264,
  43. + HEVC an VP9 streams.
  44. +
  45. +properties:
  46. + compatible:
  47. + const: rockchip,rk3399-vdec
  48. +
  49. + reg:
  50. + maxItems: 1
  51. +
  52. + interrupts:
  53. + maxItems: 1
  54. +
  55. + clocks:
  56. + items:
  57. + - description: The Video Decoder AXI interface clock
  58. + - description: The Video Decoder AHB interface clock
  59. + - description: The Video Decoded CABAC clock
  60. + - description: The Video Decoder core clock
  61. +
  62. + clock-names:
  63. + items:
  64. + - const: axi
  65. + - const: ahb
  66. + - const: cabac
  67. + - const: core
  68. +
  69. + power-domains:
  70. + maxItems: 1
  71. +
  72. + iommus:
  73. + maxItems: 1
  74. +
  75. +required:
  76. + - compatible
  77. + - reg
  78. + - interrupts
  79. + - clocks
  80. + - clock-names
  81. + - power-domains
  82. +
  83. +additionalProperties: false
  84. +
  85. +examples:
  86. + - |
  87. + #include <dt-bindings/interrupt-controller/arm-gic.h>
  88. +
  89. + vdec: video-codec@ff660000 {
  90. + compatible = "rockchip,rk3399-vdec";
  91. + reg = <0x0 0xff660000 0x0 0x400>;
  92. + interrupts = <GIC_SPI 116 IRQ_TYPE_LEVEL_HIGH 0>;
  93. + clocks = <&cru ACLK_VDU>, <&cru HCLK_VDU>,
  94. + <&cru SCLK_VDU_CA>, <&cru SCLK_VDU_CORE>;
  95. + clock-names = "aclk", "iface", "cabac", "core";
  96. + power-domains = <&power RK3399_PD_VDU>;
  97. + iommus = <&vdec_mmu>;
  98. + };
  99. +
  100. +...
  101. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/Documentation/media/uapi/v4l/ext-ctrls-codec.rst linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/Documentation/media/uapi/v4l/ext-ctrls-codec.rst
  102. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/Documentation/media/uapi/v4l/ext-ctrls-codec.rst 2020-02-14 22:59:03.205507039 -0800
  103. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/Documentation/media/uapi/v4l/ext-ctrls-codec.rst 2020-02-14 21:55:16.385826810 -0800
  104. @@ -2028,18 +2028,22 @@
  105. * - ``V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM``
  106. - 0x00000004
  107. - The DPB entry is a long term reference frame
  108. - * - ``V4L2_H264_DPB_ENTRY_FLAG_FIELD_PICTURE``
  109. + * - ``V4L2_H264_DPB_ENTRY_FLAG_FIELD``
  110. - 0x00000008
  111. - - The DPB entry is a field picture
  112. - * - ``V4L2_H264_DPB_ENTRY_FLAG_REF_TOP``
  113. + - The DPB entry is a field reference, which means only one of the field
  114. + will be used when decoding the new frame/field. When not set the DPB
  115. + entry is a frame reference (both fields will be used). Note that this
  116. + flag does not say anything about the number of fields contained in the
  117. + reference frame, it just describes the one used to decode the new
  118. + field/frame
  119. + * - ``V4L2_H264_DPB_ENTRY_FLAG_BOTTOM_FIELD``
  120. - 0x00000010
  121. - - The DPB entry is a top field reference
  122. - * - ``V4L2_H264_DPB_ENTRY_FLAG_REF_BOTTOM``
  123. - - 0x00000020
  124. - - The DPB entry is a bottom field reference
  125. - * - ``V4L2_H264_DPB_ENTRY_FLAG_REF_FRAME``
  126. - - 0x00000030
  127. - - The DPB entry is a reference frame
  128. + - The DPB entry is a bottom field reference (only the bottom field of the
  129. + reference frame is needed to decode the new frame/field). Only valid if
  130. + V4L2_H264_DPB_ENTRY_FLAG_FIELD is set. When
  131. + V4L2_H264_DPB_ENTRY_FLAG_FIELD is set but
  132. + V4L2_H264_DPB_ENTRY_FLAG_BOTTOM_FIELD is not, that means the
  133. + DPB entry is a top field reference
  134.  
  135. ``V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE (enum)``
  136. Specifies the decoding mode to use. Currently exposes slice-based and
  137. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/media/v4l2-core/Kconfig linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/media/v4l2-core/Kconfig
  138. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/media/v4l2-core/Kconfig 2020-02-14 22:59:06.637531721 -0800
  139. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/media/v4l2-core/Kconfig 2020-02-14 21:55:19.865852585 -0800
  140. @@ -45,6 +45,10 @@
  141. config VIDEO_TUNER
  142. tristate
  143.  
  144. +# Used by drivers that need v4l2-h264.ko
  145. +config V4L2_H264
  146. + tristate
  147. +
  148. # Used by drivers that need v4l2-mem2mem.ko
  149. config V4L2_MEM2MEM_DEV
  150. tristate
  151. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/media/v4l2-core/Makefile linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/media/v4l2-core/Makefile
  152. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/media/v4l2-core/Makefile 2020-02-14 22:59:06.637531721 -0800
  153. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/media/v4l2-core/Makefile 2020-02-14 21:55:19.865852585 -0800
  154. @@ -21,6 +21,7 @@
  155. obj-$(CONFIG_VIDEO_TUNER) += tuner.o
  156.  
  157. obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o
  158. +obj-$(CONFIG_V4L2_H264) += v4l2-h264.o
  159.  
  160. obj-$(CONFIG_V4L2_FLASH_LED_CLASS) += v4l2-flash-led-class.o
  161.  
  162. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/media/v4l2-core/v4l2-h264.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/media/v4l2-core/v4l2-h264.c
  163. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/media/v4l2-core/v4l2-h264.c 1969-12-31 16:00:00.000000000 -0800
  164. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/media/v4l2-core/v4l2-h264.c 2020-02-14 21:55:19.865852585 -0800
  165. @@ -0,0 +1,258 @@
  166. +// SPDX-License-Identifier: GPL-2.0
  167. +/*
  168. + * V4L2 H264 helpers.
  169. + *
  170. + * Copyright (C) 2019 Collabora, Ltd.
  171. + *
  172. + * Author: Boris Brezillon <[email protected]>
  173. + */
  174. +
  175. +#include <linux/module.h>
  176. +#include <linux/sort.h>
  177. +
  178. +#include <media/v4l2-h264.h>
  179. +
  180. +/**
  181. + * v4l2_h264_init_reflist_builder() - Initialize a P/B0/B1 reference list
  182. + * builder
  183. + *
  184. + * @b: the builder context to initialize
  185. + * @dec_params: decode parameters control
  186. + * @slice_params: first slice parameters control
  187. + * @sps: SPS control
  188. + * @dpb: DPB to use when creating the reference list
  189. + */
  190. +void
  191. +v4l2_h264_init_reflist_builder(struct v4l2_h264_reflist_builder *b,
  192. + const struct v4l2_ctrl_h264_decode_params *dec_params,
  193. + const struct v4l2_ctrl_h264_slice_params *slice_params,
  194. + const struct v4l2_ctrl_h264_sps *sps,
  195. + const struct v4l2_h264_dpb_entry *dpb)
  196. +{
  197. + int cur_frame_num, max_frame_num;
  198. + unsigned int i;
  199. +
  200. + max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
  201. + cur_frame_num = slice_params->frame_num;
  202. +
  203. + memset(b, 0, sizeof(*b));
  204. + if (!(slice_params->flags & V4L2_H264_SLICE_FLAG_FIELD_PIC))
  205. + b->cur_pic_order_count = min(dec_params->bottom_field_order_cnt,
  206. + dec_params->top_field_order_cnt);
  207. + else if (slice_params->flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  208. + b->cur_pic_order_count = dec_params->bottom_field_order_cnt;
  209. + else
  210. + b->cur_pic_order_count = dec_params->top_field_order_cnt;
  211. +
  212. + for (i = 0; i < 16; i++) {
  213. + u32 pic_order_count;
  214. +
  215. + if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
  216. + continue;
  217. +
  218. + b->refs[i].pic_num = dpb[i].pic_num;
  219. + if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  220. + b->refs[i].longterm = true;
  221. +
  222. + /*
  223. + * Handle frame_num wraparound as described in section
  224. + * '8.2.4.1 Decoding process for picture numbers' of the spec.
  225. + * TODO: This logic will have to be adjusted when we start
  226. + * supporting interlaced content.
  227. + */
  228. + if (dpb[i].frame_num > cur_frame_num)
  229. + b->refs[i].frame_num = (int)dpb[i].frame_num -
  230. + max_frame_num;
  231. + else
  232. + b->refs[i].frame_num = dpb[i].frame_num;
  233. +
  234. + if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD))
  235. + pic_order_count = min(dpb[i].top_field_order_cnt,
  236. + dpb[i].bottom_field_order_cnt);
  237. + else if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_BOTTOM_FIELD)
  238. + pic_order_count = dpb[i].bottom_field_order_cnt;
  239. + else
  240. + pic_order_count = dpb[i].top_field_order_cnt;
  241. +
  242. + b->refs[i].pic_order_count = pic_order_count;
  243. + b->unordered_reflist[b->num_valid] = i;
  244. + b->num_valid++;
  245. + }
  246. +
  247. + for (i = b->num_valid; i < ARRAY_SIZE(b->unordered_reflist); i++)
  248. + b->unordered_reflist[i] = i;
  249. +}
  250. +EXPORT_SYMBOL_GPL(v4l2_h264_init_reflist_builder);
  251. +
  252. +static int v4l2_h264_p_ref_list_cmp(const void *ptra, const void *ptrb,
  253. + const void *data)
  254. +{
  255. + const struct v4l2_h264_reflist_builder *builder = data;
  256. + u8 idxa, idxb;
  257. +
  258. + idxa = *((u8 *)ptra);
  259. + idxb = *((u8 *)ptrb);
  260. +
  261. + if (builder->refs[idxa].longterm != builder->refs[idxb].longterm) {
  262. + /* Short term pics first. */
  263. + if (!builder->refs[idxa].longterm)
  264. + return -1;
  265. + else
  266. + return 1;
  267. + }
  268. +
  269. + /*
  270. + * Short term pics in descending pic num order, long term ones in
  271. + * ascending order.
  272. + */
  273. + if (!builder->refs[idxa].longterm)
  274. + return builder->refs[idxb].frame_num <
  275. + builder->refs[idxa].frame_num ?
  276. + -1 : 1;
  277. +
  278. + return builder->refs[idxa].pic_num < builder->refs[idxb].pic_num ?
  279. + -1 : 1;
  280. +}
  281. +
  282. +static int v4l2_h264_b0_ref_list_cmp(const void *ptra, const void *ptrb,
  283. + const void *data)
  284. +{
  285. + const struct v4l2_h264_reflist_builder *builder = data;
  286. + s32 poca, pocb;
  287. + u8 idxa, idxb;
  288. +
  289. + idxa = *((u8 *)ptra);
  290. + idxb = *((u8 *)ptrb);
  291. +
  292. + if (builder->refs[idxa].longterm != builder->refs[idxb].longterm) {
  293. + /* Short term pics first. */
  294. + if (!builder->refs[idxa].longterm)
  295. + return -1;
  296. + else
  297. + return 1;
  298. + }
  299. +
  300. + /* Long term pics in ascending pic num order. */
  301. + if (builder->refs[idxa].longterm)
  302. + return builder->refs[idxa].pic_num <
  303. + builder->refs[idxb].pic_num ?
  304. + -1 : 1;
  305. +
  306. + poca = builder->refs[idxa].pic_order_count;
  307. + pocb = builder->refs[idxb].pic_order_count;
  308. +
  309. + /*
  310. + * Short term pics with POC < cur POC first in POC descending order
  311. + * followed by short term pics with POC > cur POC in POC ascending
  312. + * order.
  313. + */
  314. + if ((poca < builder->cur_pic_order_count) !=
  315. + (pocb < builder->cur_pic_order_count))
  316. + return poca < pocb ? -1 : 1;
  317. + else if (poca < builder->cur_pic_order_count)
  318. + return pocb < poca ? -1 : 1;
  319. +
  320. + return poca < pocb ? -1 : 1;
  321. +}
  322. +
  323. +static int v4l2_h264_b1_ref_list_cmp(const void *ptra, const void *ptrb,
  324. + const void *data)
  325. +{
  326. + const struct v4l2_h264_reflist_builder *builder = data;
  327. + s32 poca, pocb;
  328. + u8 idxa, idxb;
  329. +
  330. + idxa = *((u8 *)ptra);
  331. + idxb = *((u8 *)ptrb);
  332. +
  333. + if (builder->refs[idxa].longterm != builder->refs[idxb].longterm) {
  334. + /* Short term pics first. */
  335. + if (!builder->refs[idxa].longterm)
  336. + return -1;
  337. + else
  338. + return 1;
  339. + }
  340. +
  341. + /* Long term pics in ascending pic num order. */
  342. + if (builder->refs[idxa].longterm)
  343. + return builder->refs[idxa].pic_num <
  344. + builder->refs[idxb].pic_num ?
  345. + -1 : 1;
  346. +
  347. + poca = builder->refs[idxa].pic_order_count;
  348. + pocb = builder->refs[idxb].pic_order_count;
  349. +
  350. + /*
  351. + * Short term pics with POC > cur POC first in POC ascending order
  352. + * followed by short term pics with POC < cur POC in POC descending
  353. + * order.
  354. + */
  355. + if ((poca < builder->cur_pic_order_count) !=
  356. + (pocb < builder->cur_pic_order_count))
  357. + return pocb < poca ? -1 : 1;
  358. + else if (poca < builder->cur_pic_order_count)
  359. + return pocb < poca ? -1 : 1;
  360. +
  361. + return poca < pocb ? -1 : 1;
  362. +}
  363. +
  364. +/**
  365. + * v4l2_h264_build_p_ref_list() - Build the P reference list
  366. + *
  367. + * @builder: reference list builder context
  368. + * @p_reflist: 16-bytes array used to store the P reference list. Each entry
  369. + * is an index in the DPB
  370. + *
  371. + * This functions builds the P reference lists. This procedure is describe in
  372. + * section '8.2.4 Decoding process for reference picture lists construction'
  373. + * of the H264 spec. This function can be used by H264 decoder drivers that
  374. + * need to pass a P reference list to the hardware.
  375. + */
  376. +void
  377. +v4l2_h264_build_p_ref_list(const struct v4l2_h264_reflist_builder *builder,
  378. + u8 *reflist)
  379. +{
  380. + memcpy(reflist, builder->unordered_reflist,
  381. + sizeof(builder->unordered_reflist));
  382. + sort_r(reflist, builder->num_valid, sizeof(*reflist),
  383. + v4l2_h264_p_ref_list_cmp, NULL, builder);
  384. +}
  385. +EXPORT_SYMBOL_GPL(v4l2_h264_build_p_ref_list);
  386. +
  387. +/**
  388. + * v4l2_h264_build_b_ref_lists() - Build the B0/B1 reference lists
  389. + *
  390. + * @builder: reference list builder context
  391. + * @b0_reflist: 16-bytes array used to store the B0 reference list. Each entry
  392. + * is an index in the DPB
  393. + * @b1_reflist: 16-bytes array used to store the B1 reference list. Each entry
  394. + * is an index in the DPB
  395. + *
  396. + * This functions builds the B0/B1 reference lists. This procedure is described
  397. + * in section '8.2.4 Decoding process for reference picture lists construction'
  398. + * of the H264 spec. This function can be used by H264 decoder drivers that
  399. + * need to pass B0/B1 reference lists to the hardware.
  400. + */
  401. +void
  402. +v4l2_h264_build_b_ref_lists(const struct v4l2_h264_reflist_builder *builder,
  403. + u8 *b0_reflist, u8 *b1_reflist)
  404. +{
  405. + memcpy(b0_reflist, builder->unordered_reflist,
  406. + sizeof(builder->unordered_reflist));
  407. + sort_r(b0_reflist, builder->num_valid, sizeof(*b0_reflist),
  408. + v4l2_h264_b0_ref_list_cmp, NULL, builder);
  409. +
  410. + memcpy(b1_reflist, builder->unordered_reflist,
  411. + sizeof(builder->unordered_reflist));
  412. + sort_r(b1_reflist, builder->num_valid, sizeof(*b1_reflist),
  413. + v4l2_h264_b1_ref_list_cmp, NULL, builder);
  414. +
  415. + if (builder->num_valid > 1 &&
  416. + !memcmp(b1_reflist, b0_reflist, builder->num_valid))
  417. + swap(b1_reflist[0], b1_reflist[1]);
  418. +}
  419. +EXPORT_SYMBOL_GPL(v4l2_h264_build_b_ref_lists);
  420. +
  421. +MODULE_LICENSE("GPL");
  422. +MODULE_DESCRIPTION("V4L2 H264 Helpers");
  423. +MODULE_AUTHOR("Boris Brezillon <[email protected]>");
  424. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_g1_h264_dec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_g1_h264_dec.c
  425. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_g1_h264_dec.c 2020-02-14 22:59:08.117542364 -0800
  426. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_g1_h264_dec.c 2020-02-14 21:55:21.377863791 -0800
  427. @@ -1,6 +1,6 @@
  428. // SPDX-License-Identifier: GPL-2.0
  429. /*
  430. - * Hantro VPU codec driver
  431. + * Rockchip RK3288 VPU codec driver
  432. *
  433. * Copyright (c) 2014 Rockchip Electronics Co., Ltd.
  434. * Hertz Wong <[email protected]>
  435. @@ -15,430 +15,248 @@
  436.  
  437. #include <media/v4l2-mem2mem.h>
  438.  
  439. +#include "hantro_g1_regs.h"
  440. #include "hantro_hw.h"
  441. #include "hantro_v4l2.h"
  442.  
  443. -#define G1_SWREG(nr) ((nr) * 4)
  444. -
  445. -#define G1_REG_RLC_VLC_BASE G1_SWREG(12)
  446. -#define G1_REG_DEC_OUT_BASE G1_SWREG(13)
  447. -#define G1_REG_REFER0_BASE G1_SWREG(14)
  448. -#define G1_REG_REFER1_BASE G1_SWREG(15)
  449. -#define G1_REG_REFER2_BASE G1_SWREG(16)
  450. -#define G1_REG_REFER3_BASE G1_SWREG(17)
  451. -#define G1_REG_REFER4_BASE G1_SWREG(18)
  452. -#define G1_REG_REFER5_BASE G1_SWREG(19)
  453. -#define G1_REG_REFER6_BASE G1_SWREG(20)
  454. -#define G1_REG_REFER7_BASE G1_SWREG(21)
  455. -#define G1_REG_REFER8_BASE G1_SWREG(22)
  456. -#define G1_REG_REFER9_BASE G1_SWREG(23)
  457. -#define G1_REG_REFER10_BASE G1_SWREG(24)
  458. -#define G1_REG_REFER11_BASE G1_SWREG(25)
  459. -#define G1_REG_REFER12_BASE G1_SWREG(26)
  460. -#define G1_REG_REFER13_BASE G1_SWREG(27)
  461. -#define G1_REG_REFER14_BASE G1_SWREG(28)
  462. -#define G1_REG_REFER15_BASE G1_SWREG(29)
  463. -#define G1_REG_QTABLE_BASE G1_SWREG(40)
  464. -#define G1_REG_DIR_MV_BASE G1_SWREG(41)
  465. -#define G1_REG_DEC_E(v) ((v) ? BIT(0) : 0)
  466. -
  467. -#define G1_REG_DEC_AXI_RD_ID(v) (((v) << 24) & GENMASK(31, 24))
  468. -#define G1_REG_DEC_TIMEOUT_E(v) ((v) ? BIT(23) : 0)
  469. -#define G1_REG_DEC_STRSWAP32_E(v) ((v) ? BIT(22) : 0)
  470. -#define G1_REG_DEC_STRENDIAN_E(v) ((v) ? BIT(21) : 0)
  471. -#define G1_REG_DEC_INSWAP32_E(v) ((v) ? BIT(20) : 0)
  472. -#define G1_REG_DEC_OUTSWAP32_E(v) ((v) ? BIT(19) : 0)
  473. -#define G1_REG_DEC_DATA_DISC_E(v) ((v) ? BIT(18) : 0)
  474. -#define G1_REG_DEC_LATENCY(v) (((v) << 11) & GENMASK(16, 11))
  475. -#define G1_REG_DEC_CLK_GATE_E(v) ((v) ? BIT(10) : 0)
  476. -#define G1_REG_DEC_IN_ENDIAN(v) ((v) ? BIT(9) : 0)
  477. -#define G1_REG_DEC_OUT_ENDIAN(v) ((v) ? BIT(8) : 0)
  478. -#define G1_REG_DEC_ADV_PRE_DIS(v) ((v) ? BIT(6) : 0)
  479. -#define G1_REG_DEC_SCMD_DIS(v) ((v) ? BIT(5) : 0)
  480. -#define G1_REG_DEC_MAX_BURST(v) (((v) << 0) & GENMASK(4, 0))
  481. -
  482. -#define G1_REG_DEC_MODE(v) (((v) << 28) & GENMASK(31, 28))
  483. -#define G1_REG_RLC_MODE_E(v) ((v) ? BIT(27) : 0)
  484. -#define G1_REG_PIC_INTERLACE_E(v) ((v) ? BIT(23) : 0)
  485. -#define G1_REG_PIC_FIELDMODE_E(v) ((v) ? BIT(22) : 0)
  486. -#define G1_REG_PIC_TOPFIELD_E(v) ((v) ? BIT(19) : 0)
  487. -#define G1_REG_FILTERING_DIS(v) ((v) ? BIT(14) : 0)
  488. -#define G1_REG_PIC_FIXED_QUANT(v) ((v) ? BIT(13) : 0)
  489. -#define G1_REG_WRITE_MVS_E(v) ((v) ? BIT(12) : 0)
  490. -#define G1_REG_SEQ_MBAFF_E(v) ((v) ? BIT(10) : 0)
  491. -#define G1_REG_PICORD_COUNT_E(v) ((v) ? BIT(9) : 0)
  492. -#define G1_REG_DEC_AXI_WR_ID(v) (((v) << 0) & GENMASK(7, 0))
  493. -
  494. -#define G1_REG_PIC_MB_WIDTH(v) (((v) << 23) & GENMASK(31, 23))
  495. -#define G1_REG_PIC_MB_HEIGHT_P(v) (((v) << 11) & GENMASK(18, 11))
  496. -#define G1_REG_REF_FRAMES(v) (((v) << 0) & GENMASK(4, 0))
  497. -
  498. -#define G1_REG_STRM_START_BIT(v) (((v) << 26) & GENMASK(31, 26))
  499. -#define G1_REG_TYPE1_QUANT_E(v) ((v) ? BIT(24) : 0)
  500. -#define G1_REG_CH_QP_OFFSET(v) (((v) << 19) & GENMASK(23, 19))
  501. -#define G1_REG_CH_QP_OFFSET2(v) (((v) << 14) & GENMASK(18, 14))
  502. -#define G1_REG_FIELDPIC_FLAG_E(v) ((v) ? BIT(0) : 0)
  503. -
  504. -#define G1_REG_START_CODE_E(v) ((v) ? BIT(31) : 0)
  505. -#define G1_REG_INIT_QP(v) (((v) << 25) & GENMASK(30, 25))
  506. -#define G1_REG_CH_8PIX_ILEAV_E(v) ((v) ? BIT(24) : 0)
  507. -#define G1_REG_STREAM_LEN(v) (((v) << 0) & GENMASK(23, 0))
  508. -
  509. -#define G1_REG_CABAC_E(v) ((v) ? BIT(31) : 0)
  510. -#define G1_REG_BLACKWHITE_E(v) ((v) ? BIT(30) : 0)
  511. -#define G1_REG_DIR_8X8_INFER_E(v) ((v) ? BIT(29) : 0)
  512. -#define G1_REG_WEIGHT_PRED_E(v) ((v) ? BIT(28) : 0)
  513. -#define G1_REG_WEIGHT_BIPR_IDC(v) (((v) << 26) & GENMASK(27, 26))
  514. -#define G1_REG_FRAMENUM_LEN(v) (((v) << 16) & GENMASK(20, 16))
  515. -#define G1_REG_FRAMENUM(v) (((v) << 0) & GENMASK(15, 0))
  516. -
  517. -#define G1_REG_CONST_INTRA_E(v) ((v) ? BIT(31) : 0)
  518. -#define G1_REG_FILT_CTRL_PRES(v) ((v) ? BIT(30) : 0)
  519. -#define G1_REG_RDPIC_CNT_PRES(v) ((v) ? BIT(29) : 0)
  520. -#define G1_REG_8X8TRANS_FLAG_E(v) ((v) ? BIT(28) : 0)
  521. -#define G1_REG_REFPIC_MK_LEN(v) (((v) << 17) & GENMASK(27, 17))
  522. -#define G1_REG_IDR_PIC_E(v) ((v) ? BIT(16) : 0)
  523. -#define G1_REG_IDR_PIC_ID(v) (((v) << 0) & GENMASK(15, 0))
  524. -
  525. -#define G1_REG_PPS_ID(v) (((v) << 24) & GENMASK(31, 24))
  526. -#define G1_REG_REFIDX1_ACTIVE(v) (((v) << 19) & GENMASK(23, 19))
  527. -#define G1_REG_REFIDX0_ACTIVE(v) (((v) << 14) & GENMASK(18, 14))
  528. -#define G1_REG_POC_LENGTH(v) (((v) << 0) & GENMASK(7, 0))
  529. -
  530. -#define G1_REG_PINIT_RLIST_F9(v) (((v) << 25) & GENMASK(29, 25))
  531. -#define G1_REG_PINIT_RLIST_F8(v) (((v) << 20) & GENMASK(24, 20))
  532. -#define G1_REG_PINIT_RLIST_F7(v) (((v) << 15) & GENMASK(19, 15))
  533. -#define G1_REG_PINIT_RLIST_F6(v) (((v) << 10) & GENMASK(14, 10))
  534. -#define G1_REG_PINIT_RLIST_F5(v) (((v) << 5) & GENMASK(9, 5))
  535. -#define G1_REG_PINIT_RLIST_F4(v) (((v) << 0) & GENMASK(4, 0))
  536. -
  537. -#define G1_REG_PINIT_RLIST_F15(v) (((v) << 25) & GENMASK(29, 25))
  538. -#define G1_REG_PINIT_RLIST_F14(v) (((v) << 20) & GENMASK(24, 20))
  539. -#define G1_REG_PINIT_RLIST_F13(v) (((v) << 15) & GENMASK(19, 15))
  540. -#define G1_REG_PINIT_RLIST_F12(v) (((v) << 10) & GENMASK(14, 10))
  541. -#define G1_REG_PINIT_RLIST_F11(v) (((v) << 5) & GENMASK(9, 5))
  542. -#define G1_REG_PINIT_RLIST_F10(v) (((v) << 0) & GENMASK(4, 0))
  543. -
  544. -#define G1_REG_REFER1_NBR(v) (((v) << 16) & GENMASK(31, 16))
  545. -#define G1_REG_REFER0_NBR(v) (((v) << 0) & GENMASK(15, 0))
  546. -
  547. -#define G1_REG_REFER3_NBR(v) (((v) << 16) & GENMASK(31, 16))
  548. -#define G1_REG_REFER2_NBR(v) (((v) << 0) & GENMASK(15, 0))
  549. -
  550. -#define G1_REG_REFER5_NBR(v) (((v) << 16) & GENMASK(31, 16))
  551. -#define G1_REG_REFER4_NBR(v) (((v) << 0) & GENMASK(15, 0))
  552. -
  553. -#define G1_REG_REFER7_NBR(v) (((v) << 16) & GENMASK(31, 16))
  554. -#define G1_REG_REFER6_NBR(v) (((v) << 0) & GENMASK(15, 0))
  555. -
  556. -#define G1_REG_REFER9_NBR(v) (((v) << 16) & GENMASK(31, 16))
  557. -#define G1_REG_REFER8_NBR(v) (((v) << 0) & GENMASK(15, 0))
  558. -
  559. -#define G1_REG_REFER11_NBR(v) (((v) << 16) & GENMASK(31, 16))
  560. -#define G1_REG_REFER10_NBR(v) (((v) << 0) & GENMASK(15, 0))
  561. -
  562. -#define G1_REG_REFER13_NBR(v) (((v) << 16) & GENMASK(31, 16))
  563. -#define G1_REG_REFER12_NBR(v) (((v) << 0) & GENMASK(15, 0))
  564. -
  565. -#define G1_REG_REFER15_NBR(v) (((v) << 16) & GENMASK(31, 16))
  566. -#define G1_REG_REFER14_NBR(v) (((v) << 0) & GENMASK(15, 0))
  567. -
  568. -#define G1_REG_REFER_LTERM_E(v) (((v) << 0) & GENMASK(31, 0))
  569. -
  570. -#define G1_REG_REFER_VALID_E(v) (((v) << 0) & GENMASK(31, 0))
  571. -
  572. -#define G1_REG_BINIT_RLIST_B2(v) (((v) << 25) & GENMASK(29, 25))
  573. -#define G1_REG_BINIT_RLIST_F2(v) (((v) << 20) & GENMASK(24, 20))
  574. -#define G1_REG_BINIT_RLIST_B1(v) (((v) << 15) & GENMASK(19, 15))
  575. -#define G1_REG_BINIT_RLIST_F1(v) (((v) << 10) & GENMASK(14, 10))
  576. -#define G1_REG_BINIT_RLIST_B0(v) (((v) << 5) & GENMASK(9, 5))
  577. -#define G1_REG_BINIT_RLIST_F0(v) (((v) << 0) & GENMASK(4, 0))
  578. -
  579. -#define G1_REG_BINIT_RLIST_B5(v) (((v) << 25) & GENMASK(29, 25))
  580. -#define G1_REG_BINIT_RLIST_F5(v) (((v) << 20) & GENMASK(24, 20))
  581. -#define G1_REG_BINIT_RLIST_B4(v) (((v) << 15) & GENMASK(19, 15))
  582. -#define G1_REG_BINIT_RLIST_F4(v) (((v) << 10) & GENMASK(14, 10))
  583. -#define G1_REG_BINIT_RLIST_B3(v) (((v) << 5) & GENMASK(9, 5))
  584. -#define G1_REG_BINIT_RLIST_F3(v) (((v) << 0) & GENMASK(4, 0))
  585. -
  586. -#define G1_REG_BINIT_RLIST_B8(v) (((v) << 25) & GENMASK(29, 25))
  587. -#define G1_REG_BINIT_RLIST_F8(v) (((v) << 20) & GENMASK(24, 20))
  588. -#define G1_REG_BINIT_RLIST_B7(v) (((v) << 15) & GENMASK(19, 15))
  589. -#define G1_REG_BINIT_RLIST_F7(v) (((v) << 10) & GENMASK(14, 10))
  590. -#define G1_REG_BINIT_RLIST_B6(v) (((v) << 5) & GENMASK(9, 5))
  591. -#define G1_REG_BINIT_RLIST_F6(v) (((v) << 0) & GENMASK(4, 0))
  592. -
  593. -#define G1_REG_BINIT_RLIST_B11(v) (((v) << 25) & GENMASK(29, 25))
  594. -#define G1_REG_BINIT_RLIST_F11(v) (((v) << 20) & GENMASK(24, 20))
  595. -#define G1_REG_BINIT_RLIST_B10(v) (((v) << 15) & GENMASK(19, 15))
  596. -#define G1_REG_BINIT_RLIST_F10(v) (((v) << 10) & GENMASK(14, 10))
  597. -#define G1_REG_BINIT_RLIST_B9(v) (((v) << 5) & GENMASK(9, 5))
  598. -#define G1_REG_BINIT_RLIST_F9(v) (((v) << 0) & GENMASK(4, 0))
  599. -
  600. -#define G1_REG_BINIT_RLIST_B14(v) (((v) << 25) & GENMASK(29, 25))
  601. -#define G1_REG_BINIT_RLIST_F14(v) (((v) << 20) & GENMASK(24, 20))
  602. -#define G1_REG_BINIT_RLIST_B13(v) (((v) << 15) & GENMASK(19, 15))
  603. -#define G1_REG_BINIT_RLIST_F13(v) (((v) << 10) & GENMASK(14, 10))
  604. -#define G1_REG_BINIT_RLIST_B12(v) (((v) << 5) & GENMASK(9, 5))
  605. -#define G1_REG_BINIT_RLIST_F12(v) (((v) << 0) & GENMASK(4, 0))
  606. -
  607. -#define G1_REG_PINIT_RLIST_F3(v) (((v) << 25) & GENMASK(29, 25))
  608. -#define G1_REG_PINIT_RLIST_F2(v) (((v) << 20) & GENMASK(24, 20))
  609. -#define G1_REG_PINIT_RLIST_F1(v) (((v) << 15) & GENMASK(19, 15))
  610. -#define G1_REG_PINIT_RLIST_F0(v) (((v) << 10) & GENMASK(14, 10))
  611. -#define G1_REG_BINIT_RLIST_B15(v) (((v) << 5) & GENMASK(9, 5))
  612. -#define G1_REG_BINIT_RLIST_F15(v) (((v) << 0) & GENMASK(4, 0))
  613. -
  614. -#define G1_REG_STARTMB_X(v) (((v) << 23) & GENMASK(31, 23))
  615. -#define G1_REG_STARTMB_Y(v) (((v) << 15) & GENMASK(22, 15))
  616. -
  617. -#define G1_REG_PRED_BC_TAP_0_0(v) (((v) << 22) & GENMASK(31, 22))
  618. -#define G1_REG_PRED_BC_TAP_0_1(v) (((v) << 12) & GENMASK(21, 12))
  619. -#define G1_REG_PRED_BC_TAP_0_2(v) (((v) << 2) & GENMASK(11, 2))
  620. +static void set_params(struct hantro_ctx *ctx)
  621. +{
  622. + const struct hantro_h264_dec_ctrls *ctrls = &ctx->h264_dec.ctrls;
  623. + const struct v4l2_ctrl_h264_decode_params *dec_param = ctrls->decode;
  624. + const struct v4l2_ctrl_h264_slice_params *slices = ctrls->slices;
  625. + const struct v4l2_ctrl_h264_sps *sps = ctrls->sps;
  626. + const struct v4l2_ctrl_h264_pps *pps = ctrls->pps;
  627. + struct vb2_v4l2_buffer *src_buf = hantro_get_src_buf(ctx);
  628. + struct hantro_dev *vpu = ctx->dev;
  629. + u32 reg;
  630.  
  631. -#define G1_REG_REFBU_E(v) ((v) ? BIT(31) : 0)
  632. + /* Decoder control register 0. */
  633. + reg = G1_REG_DEC_CTRL0_DEC_AXI_WR_ID(0x0);
  634. + if (sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD)
  635. + reg |= G1_REG_DEC_CTRL0_SEQ_MBAFF_E;
  636. + if (sps->profile_idc > 66) {
  637. + reg |= G1_REG_DEC_CTRL0_PICORD_COUNT_E;
  638. + if (dec_param->nal_ref_idc)
  639. + reg |= G1_REG_DEC_CTRL0_WRITE_MVS_E;
  640. + }
  641.  
  642. -#define G1_REG_APF_THRESHOLD(v) (((v) << 0) & GENMASK(13, 0))
  643. + if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY) &&
  644. + (sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD ||
  645. + slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC))
  646. + reg |= G1_REG_DEC_CTRL0_PIC_INTERLACE_E;
  647. + if (slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC)
  648. + reg |= G1_REG_DEC_CTRL0_PIC_FIELDMODE_E;
  649. + if (!(slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD))
  650. + reg |= G1_REG_DEC_CTRL0_PIC_TOPFIELD_E;
  651. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL0);
  652. +
  653. + /* Decoder control register 1. */
  654. + reg = G1_REG_DEC_CTRL1_PIC_MB_WIDTH(MB_WIDTH(ctx->src_fmt.width)) |
  655. + G1_REG_DEC_CTRL1_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->src_fmt.height)) |
  656. + G1_REG_DEC_CTRL1_REF_FRAMES(sps->max_num_ref_frames);
  657. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL1);
  658. +
  659. + /* Decoder control register 2. */
  660. + reg = G1_REG_DEC_CTRL2_CH_QP_OFFSET(pps->chroma_qp_index_offset) |
  661. + G1_REG_DEC_CTRL2_CH_QP_OFFSET2(pps->second_chroma_qp_index_offset);
  662. +
  663. + /* always use the matrix sent from userspace */
  664. + reg |= G1_REG_DEC_CTRL2_TYPE1_QUANT_E;
  665. +
  666. + if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY))
  667. + reg |= G1_REG_DEC_CTRL2_FIELDPIC_FLAG_E;
  668. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL2);
  669. +
  670. + /* Decoder control register 3. */
  671. + reg = G1_REG_DEC_CTRL3_START_CODE_E |
  672. + G1_REG_DEC_CTRL3_INIT_QP(pps->pic_init_qp_minus26 + 26) |
  673. + G1_REG_DEC_CTRL3_STREAM_LEN(vb2_get_plane_payload(&src_buf->vb2_buf, 0));
  674. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL3);
  675. +
  676. + /* Decoder control register 4. */
  677. + reg = G1_REG_DEC_CTRL4_FRAMENUM_LEN(sps->log2_max_frame_num_minus4 + 4) |
  678. + G1_REG_DEC_CTRL4_FRAMENUM(slices[0].frame_num) |
  679. + G1_REG_DEC_CTRL4_WEIGHT_BIPR_IDC(pps->weighted_bipred_idc);
  680. + if (pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE)
  681. + reg |= G1_REG_DEC_CTRL4_CABAC_E;
  682. + if (sps->flags & V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE)
  683. + reg |= G1_REG_DEC_CTRL4_DIR_8X8_INFER_E;
  684. + if (sps->profile_idc >= 100 && sps->chroma_format_idc == 0)
  685. + reg |= G1_REG_DEC_CTRL4_BLACKWHITE_E;
  686. + if (pps->flags & V4L2_H264_PPS_FLAG_WEIGHTED_PRED)
  687. + reg |= G1_REG_DEC_CTRL4_WEIGHT_PRED_E;
  688. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL4);
  689. +
  690. + /* Decoder control register 5. */
  691. + reg = G1_REG_DEC_CTRL5_REFPIC_MK_LEN(slices[0].dec_ref_pic_marking_bit_size) |
  692. + G1_REG_DEC_CTRL5_IDR_PIC_ID(slices[0].idr_pic_id);
  693. + if (pps->flags & V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED)
  694. + reg |= G1_REG_DEC_CTRL5_CONST_INTRA_E;
  695. + if (pps->flags & V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT)
  696. + reg |= G1_REG_DEC_CTRL5_FILT_CTRL_PRES;
  697. + if (pps->flags & V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT)
  698. + reg |= G1_REG_DEC_CTRL5_RDPIC_CNT_PRES;
  699. + if (pps->flags & V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE)
  700. + reg |= G1_REG_DEC_CTRL5_8X8TRANS_FLAG_E;
  701. + if (dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC)
  702. + reg |= G1_REG_DEC_CTRL5_IDR_PIC_E;
  703. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL5);
  704. +
  705. + /* Decoder control register 6. */
  706. + reg = G1_REG_DEC_CTRL6_PPS_ID(slices[0].pic_parameter_set_id) |
  707. + G1_REG_DEC_CTRL6_REFIDX0_ACTIVE(pps->num_ref_idx_l0_default_active_minus1 + 1) |
  708. + G1_REG_DEC_CTRL6_REFIDX1_ACTIVE(pps->num_ref_idx_l1_default_active_minus1 + 1) |
  709. + G1_REG_DEC_CTRL6_POC_LENGTH(slices[0].pic_order_cnt_bit_size);
  710. + vdpu_write_relaxed(vpu, reg, G1_REG_DEC_CTRL6);
  711. +
  712. + /* Error concealment register. */
  713. + vdpu_write_relaxed(vpu, 0, G1_REG_ERR_CONC);
  714. +
  715. + /* Prediction filter tap register. */
  716. + vdpu_write_relaxed(vpu,
  717. + G1_REG_PRED_FLT_PRED_BC_TAP_0_0(1) |
  718. + G1_REG_PRED_FLT_PRED_BC_TAP_0_1(-5 & 0x3ff) |
  719. + G1_REG_PRED_FLT_PRED_BC_TAP_0_2(20),
  720. + G1_REG_PRED_FLT);
  721. +
  722. + /* Reference picture buffer control register. */
  723. + vdpu_write_relaxed(vpu, 0, G1_REG_REF_BUF_CTRL);
  724. +
  725. + /* Reference picture buffer control register 2. */
  726. + vdpu_write_relaxed(vpu, G1_REG_REF_BUF_CTRL2_APF_THRESHOLD(8),
  727. + G1_REG_REF_BUF_CTRL2);
  728. +}
  729.  
  730. -void hantro_g1_h264_dec_run(struct hantro_ctx *ctx)
  731. +static void set_ref(struct hantro_ctx *ctx)
  732. {
  733. - struct hantro_dev *vpu = ctx->dev;
  734. - struct vb2_v4l2_buffer *src_buf, *dst_buf;
  735. - const struct hantro_h264_dec_ctrls *ctrls;
  736. - const struct v4l2_ctrl_h264_decode_params *decode;
  737. - const struct v4l2_ctrl_h264_slice_params *slices;
  738. - const struct v4l2_ctrl_h264_sps *sps;
  739. - const struct v4l2_ctrl_h264_pps *pps;
  740. + struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
  741. const u8 *b0_reflist, *b1_reflist, *p_reflist;
  742. - dma_addr_t addr;
  743. - size_t offset = 0;
  744. + struct hantro_dev *vpu = ctx->dev;
  745. + u32 dpb_longterm = 0;
  746. + u32 dpb_valid = 0;
  747. + int reg_num;
  748. u32 reg;
  749. + int i;
  750.  
  751. - /* Prepare the H264 decoder context. */
  752. - if (hantro_h264_dec_prepare_run(ctx))
  753. - return;
  754. + /*
  755. + * Set up bit maps of valid and long term DPBs.
  756. + * NOTE: The bits are reversed, i.e. MSb is DPB 0.
  757. + */
  758. + for (i = 0; i < HANTRO_H264_DPB_SIZE; ++i) {
  759. + if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
  760. + dpb_valid |= BIT(HANTRO_H264_DPB_SIZE - 1 - i);
  761.  
  762. - src_buf = hantro_get_src_buf(ctx);
  763. - dst_buf = hantro_get_dst_buf(ctx);
  764. + if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  765. + dpb_longterm |= BIT(HANTRO_H264_DPB_SIZE - 1 - i);
  766. + }
  767. + vdpu_write_relaxed(vpu, dpb_valid << 16, G1_REG_VALID_REF);
  768. + vdpu_write_relaxed(vpu, dpb_longterm << 16, G1_REG_LT_REF);
  769.  
  770. - ctrls = &ctx->h264_dec.ctrls;
  771. - decode = ctrls->decode;
  772. - slices = ctrls->slices;
  773. - sps = ctrls->sps;
  774. - pps = ctrls->pps;
  775. + /*
  776. + * Set up reference frame picture numbers.
  777. + *
  778. + * Each G1_REG_REF_PIC(x) register contains numbers of two
  779. + * subsequential reference pictures.
  780. + */
  781. + for (i = 0; i < HANTRO_H264_DPB_SIZE; i += 2) {
  782. + reg = 0;
  783. + if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  784. + reg |= G1_REG_REF_PIC_REFER0_NBR(dpb[i].pic_num);
  785. + else
  786. + reg |= G1_REG_REF_PIC_REFER0_NBR(dpb[i].frame_num);
  787. +
  788. + if (dpb[i + 1].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  789. + reg |= G1_REG_REF_PIC_REFER1_NBR(dpb[i + 1].pic_num);
  790. + else
  791. + reg |= G1_REG_REF_PIC_REFER1_NBR(dpb[i + 1].frame_num);
  792. +
  793. + vdpu_write_relaxed(vpu, reg, G1_REG_REF_PIC(i / 2));
  794. + }
  795.  
  796. b0_reflist = ctx->h264_dec.reflists.b0;
  797. b1_reflist = ctx->h264_dec.reflists.b1;
  798. p_reflist = ctx->h264_dec.reflists.p;
  799.  
  800. - reg = G1_REG_DEC_AXI_RD_ID(0xff) |
  801. - G1_REG_DEC_TIMEOUT_E(1) |
  802. - G1_REG_DEC_STRSWAP32_E(1) |
  803. - G1_REG_DEC_STRENDIAN_E(1) |
  804. - G1_REG_DEC_INSWAP32_E(1) |
  805. - G1_REG_DEC_OUTSWAP32_E(1) |
  806. - G1_REG_DEC_DATA_DISC_E(0) |
  807. - G1_REG_DEC_LATENCY(0) |
  808. - G1_REG_DEC_CLK_GATE_E(1) |
  809. - G1_REG_DEC_IN_ENDIAN(0) |
  810. - G1_REG_DEC_OUT_ENDIAN(1) |
  811. - G1_REG_DEC_ADV_PRE_DIS(0) |
  812. - G1_REG_DEC_SCMD_DIS(0) |
  813. - G1_REG_DEC_MAX_BURST(16);
  814. - vdpu_write_relaxed(vpu, reg, G1_SWREG(2));
  815. -
  816. - reg = G1_REG_DEC_MODE(0) |
  817. - G1_REG_RLC_MODE_E(0) |
  818. - G1_REG_PIC_INTERLACE_E(!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY) && (sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD || slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC)) |
  819. - G1_REG_PIC_FIELDMODE_E(slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC) |
  820. - G1_REG_PIC_TOPFIELD_E(!(slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)) |
  821. - G1_REG_FILTERING_DIS(0) |
  822. - G1_REG_PIC_FIXED_QUANT(0) |
  823. - G1_REG_WRITE_MVS_E(sps->profile_idc > 66 && decode->nal_ref_idc) |
  824. - G1_REG_SEQ_MBAFF_E(sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD) |
  825. - G1_REG_PICORD_COUNT_E(sps->profile_idc > 66) |
  826. - G1_REG_DEC_AXI_WR_ID(0);
  827. - vdpu_write_relaxed(vpu, reg, G1_SWREG(3));
  828. -
  829. - reg = G1_REG_PIC_MB_WIDTH(MB_WIDTH(ctx->src_fmt.width)) |
  830. - G1_REG_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->src_fmt.height)) |
  831. - G1_REG_REF_FRAMES(sps->max_num_ref_frames);
  832. - vdpu_write_relaxed(vpu, reg, G1_SWREG(4));
  833. -
  834. - reg = G1_REG_STRM_START_BIT(0) |
  835. - G1_REG_TYPE1_QUANT_E(1) |
  836. - G1_REG_CH_QP_OFFSET(pps->chroma_qp_index_offset) |
  837. - G1_REG_CH_QP_OFFSET2(pps->second_chroma_qp_index_offset) |
  838. - G1_REG_FIELDPIC_FLAG_E(!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY));
  839. - vdpu_write_relaxed(vpu, reg, G1_SWREG(5));
  840. -
  841. - reg = G1_REG_START_CODE_E(1) |
  842. - G1_REG_INIT_QP(pps->pic_init_qp_minus26 + 26) |
  843. - G1_REG_CH_8PIX_ILEAV_E(0) |
  844. - G1_REG_STREAM_LEN(vb2_get_plane_payload(&src_buf->vb2_buf, 0));
  845. - vdpu_write_relaxed(vpu, reg, G1_SWREG(6));
  846. -
  847. - reg = G1_REG_CABAC_E(pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE) |
  848. - G1_REG_BLACKWHITE_E(sps->profile_idc >= 100 && sps->chroma_format_idc == 0) |
  849. - G1_REG_DIR_8X8_INFER_E(sps->flags & V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE) |
  850. - G1_REG_WEIGHT_PRED_E(pps->flags & V4L2_H264_PPS_FLAG_WEIGHTED_PRED) |
  851. - G1_REG_WEIGHT_BIPR_IDC(pps->weighted_bipred_idc) |
  852. - G1_REG_FRAMENUM_LEN(sps->log2_max_frame_num_minus4 + 4) |
  853. - G1_REG_FRAMENUM(slices[0].frame_num);
  854. - vdpu_write_relaxed(vpu, reg, G1_SWREG(7));
  855. -
  856. - reg = G1_REG_CONST_INTRA_E(pps->flags & V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED) |
  857. - G1_REG_FILT_CTRL_PRES(pps->flags & V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT) |
  858. - G1_REG_RDPIC_CNT_PRES(pps->flags & V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT) |
  859. - G1_REG_8X8TRANS_FLAG_E(pps->flags & V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE) |
  860. - G1_REG_REFPIC_MK_LEN(slices[0].dec_ref_pic_marking_bit_size) |
  861. - G1_REG_IDR_PIC_E(decode->flags & V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC) |
  862. - G1_REG_IDR_PIC_ID(slices[0].idr_pic_id);
  863. - vdpu_write_relaxed(vpu, reg, G1_SWREG(8));
  864. -
  865. - reg = G1_REG_PPS_ID(slices[0].pic_parameter_set_id) |
  866. - G1_REG_REFIDX1_ACTIVE(pps->num_ref_idx_l1_default_active_minus1 + 1) |
  867. - G1_REG_REFIDX0_ACTIVE(pps->num_ref_idx_l0_default_active_minus1 + 1) |
  868. - G1_REG_POC_LENGTH(slices[0].pic_order_cnt_bit_size);
  869. - vdpu_write_relaxed(vpu, reg, G1_SWREG(9));
  870. -
  871. - reg = G1_REG_PINIT_RLIST_F9(p_reflist[9]) |
  872. - G1_REG_PINIT_RLIST_F8(p_reflist[8]) |
  873. - G1_REG_PINIT_RLIST_F7(p_reflist[7]) |
  874. - G1_REG_PINIT_RLIST_F6(p_reflist[6]) |
  875. - G1_REG_PINIT_RLIST_F5(p_reflist[5]) |
  876. - G1_REG_PINIT_RLIST_F4(p_reflist[4]);
  877. - vdpu_write_relaxed(vpu, reg, G1_SWREG(10));
  878. -
  879. - reg = G1_REG_PINIT_RLIST_F15(p_reflist[15]) |
  880. - G1_REG_PINIT_RLIST_F14(p_reflist[14]) |
  881. - G1_REG_PINIT_RLIST_F13(p_reflist[13]) |
  882. - G1_REG_PINIT_RLIST_F12(p_reflist[12]) |
  883. - G1_REG_PINIT_RLIST_F11(p_reflist[11]) |
  884. - G1_REG_PINIT_RLIST_F10(p_reflist[10]);
  885. - vdpu_write_relaxed(vpu, reg, G1_SWREG(11));
  886. -
  887. - reg = G1_REG_REFER1_NBR(hantro_h264_get_ref_nbr(ctx, 1)) |
  888. - G1_REG_REFER0_NBR(hantro_h264_get_ref_nbr(ctx, 0));
  889. - vdpu_write_relaxed(vpu, reg, G1_SWREG(30));
  890. -
  891. - reg = G1_REG_REFER3_NBR(hantro_h264_get_ref_nbr(ctx, 3)) |
  892. - G1_REG_REFER2_NBR(hantro_h264_get_ref_nbr(ctx, 2));
  893. - vdpu_write_relaxed(vpu, reg, G1_SWREG(31));
  894. -
  895. - reg = G1_REG_REFER5_NBR(hantro_h264_get_ref_nbr(ctx, 5)) |
  896. - G1_REG_REFER4_NBR(hantro_h264_get_ref_nbr(ctx, 4));
  897. - vdpu_write_relaxed(vpu, reg, G1_SWREG(32));
  898. -
  899. - reg = G1_REG_REFER7_NBR(hantro_h264_get_ref_nbr(ctx, 7)) |
  900. - G1_REG_REFER6_NBR(hantro_h264_get_ref_nbr(ctx, 6));
  901. - vdpu_write_relaxed(vpu, reg, G1_SWREG(33));
  902. -
  903. - reg = G1_REG_REFER9_NBR(hantro_h264_get_ref_nbr(ctx, 9)) |
  904. - G1_REG_REFER8_NBR(hantro_h264_get_ref_nbr(ctx, 8));
  905. - vdpu_write_relaxed(vpu, reg, G1_SWREG(34));
  906. -
  907. - reg = G1_REG_REFER11_NBR(hantro_h264_get_ref_nbr(ctx, 11)) |
  908. - G1_REG_REFER10_NBR(hantro_h264_get_ref_nbr(ctx, 10));
  909. - vdpu_write_relaxed(vpu, reg, G1_SWREG(35));
  910. -
  911. - reg = G1_REG_REFER13_NBR(hantro_h264_get_ref_nbr(ctx, 13)) |
  912. - G1_REG_REFER12_NBR(hantro_h264_get_ref_nbr(ctx, 12));
  913. - vdpu_write_relaxed(vpu, reg, G1_SWREG(36));
  914. -
  915. - reg = G1_REG_REFER15_NBR(hantro_h264_get_ref_nbr(ctx, 15)) |
  916. - G1_REG_REFER14_NBR(hantro_h264_get_ref_nbr(ctx, 14));
  917. - vdpu_write_relaxed(vpu, reg, G1_SWREG(37));
  918. -
  919. - reg = G1_REG_REFER_LTERM_E(ctx->h264_dec.dpb_longterm);
  920. - vdpu_write_relaxed(vpu, reg, G1_SWREG(38));
  921. -
  922. - reg = G1_REG_REFER_VALID_E(ctx->h264_dec.dpb_valid);
  923. - vdpu_write_relaxed(vpu, reg, G1_SWREG(39));
  924. -
  925. - reg = G1_REG_BINIT_RLIST_B2(b1_reflist[2]) |
  926. - G1_REG_BINIT_RLIST_F2(b0_reflist[2]) |
  927. - G1_REG_BINIT_RLIST_B1(b1_reflist[1]) |
  928. - G1_REG_BINIT_RLIST_F1(b0_reflist[1]) |
  929. - G1_REG_BINIT_RLIST_B0(b1_reflist[0]) |
  930. - G1_REG_BINIT_RLIST_F0(b0_reflist[0]);
  931. - vdpu_write_relaxed(vpu, reg, G1_SWREG(42));
  932. -
  933. - reg = G1_REG_BINIT_RLIST_B5(b1_reflist[5]) |
  934. - G1_REG_BINIT_RLIST_F5(b0_reflist[5]) |
  935. - G1_REG_BINIT_RLIST_B4(b1_reflist[4]) |
  936. - G1_REG_BINIT_RLIST_F4(b0_reflist[4]) |
  937. - G1_REG_BINIT_RLIST_B3(b1_reflist[3]) |
  938. - G1_REG_BINIT_RLIST_F3(b0_reflist[3]);
  939. - vdpu_write_relaxed(vpu, reg, G1_SWREG(43));
  940. -
  941. - reg = G1_REG_BINIT_RLIST_B8(b1_reflist[8]) |
  942. - G1_REG_BINIT_RLIST_F8(b0_reflist[8]) |
  943. - G1_REG_BINIT_RLIST_B7(b1_reflist[7]) |
  944. - G1_REG_BINIT_RLIST_F7(b0_reflist[7]) |
  945. - G1_REG_BINIT_RLIST_B6(b1_reflist[6]) |
  946. - G1_REG_BINIT_RLIST_F6(b0_reflist[6]);
  947. - vdpu_write_relaxed(vpu, reg, G1_SWREG(44));
  948. -
  949. - reg = G1_REG_BINIT_RLIST_B11(b1_reflist[11]) |
  950. - G1_REG_BINIT_RLIST_F11(b0_reflist[11]) |
  951. - G1_REG_BINIT_RLIST_B10(b1_reflist[10]) |
  952. - G1_REG_BINIT_RLIST_F10(b0_reflist[10]) |
  953. - G1_REG_BINIT_RLIST_B9(b1_reflist[9]) |
  954. - G1_REG_BINIT_RLIST_F9(b0_reflist[9]);
  955. - vdpu_write_relaxed(vpu, reg, G1_SWREG(45));
  956. -
  957. - reg = G1_REG_BINIT_RLIST_B14(b1_reflist[14]) |
  958. - G1_REG_BINIT_RLIST_F14(b0_reflist[14]) |
  959. - G1_REG_BINIT_RLIST_B13(b1_reflist[13]) |
  960. - G1_REG_BINIT_RLIST_F13(b0_reflist[13]) |
  961. - G1_REG_BINIT_RLIST_B12(b1_reflist[12]) |
  962. - G1_REG_BINIT_RLIST_F12(b0_reflist[12]);
  963. - vdpu_write_relaxed(vpu, reg, G1_SWREG(46));
  964. -
  965. - reg = G1_REG_PINIT_RLIST_F3(p_reflist[3]) |
  966. - G1_REG_PINIT_RLIST_F2(p_reflist[2]) |
  967. - G1_REG_PINIT_RLIST_F1(p_reflist[1]) |
  968. - G1_REG_PINIT_RLIST_F0(p_reflist[0]) |
  969. - G1_REG_BINIT_RLIST_B15(b1_reflist[15]) |
  970. - G1_REG_BINIT_RLIST_F15(b0_reflist[15]);
  971. - vdpu_write_relaxed(vpu, reg, G1_SWREG(47));
  972. -
  973. - reg = G1_REG_STARTMB_X(0) |
  974. - G1_REG_STARTMB_Y(0);
  975. - vdpu_write_relaxed(vpu, reg, G1_SWREG(48));
  976. -
  977. - reg = G1_REG_PRED_BC_TAP_0_0(1) |
  978. - G1_REG_PRED_BC_TAP_0_1((u32)-5) |
  979. - G1_REG_PRED_BC_TAP_0_2(20);
  980. - vdpu_write_relaxed(vpu, reg, G1_SWREG(49));
  981. + /*
  982. + * Each G1_REG_BD_REF_PIC(x) register contains three entries
  983. + * of each forward and backward picture list.
  984. + */
  985. + reg_num = 0;
  986. + for (i = 0; i < 15; i += 3) {
  987. + reg = G1_REG_BD_REF_PIC_BINIT_RLIST_F0(b0_reflist[i]) |
  988. + G1_REG_BD_REF_PIC_BINIT_RLIST_F1(b0_reflist[i + 1]) |
  989. + G1_REG_BD_REF_PIC_BINIT_RLIST_F2(b0_reflist[i + 2]) |
  990. + G1_REG_BD_REF_PIC_BINIT_RLIST_B0(b1_reflist[i]) |
  991. + G1_REG_BD_REF_PIC_BINIT_RLIST_B1(b1_reflist[i + 1]) |
  992. + G1_REG_BD_REF_PIC_BINIT_RLIST_B2(b1_reflist[i + 2]);
  993. + vdpu_write_relaxed(vpu, reg, G1_REG_BD_REF_PIC(reg_num++));
  994. + }
  995.  
  996. - reg = G1_REG_REFBU_E(0);
  997. - vdpu_write_relaxed(vpu, reg, G1_SWREG(51));
  998. + /*
  999. + * G1_REG_BD_P_REF_PIC register contains last entries (index 15)
  1000. + * of forward and backward reference picture lists and first 4 entries
  1001. + * of P forward picture list.
  1002. + */
  1003. + reg = G1_REG_BD_P_REF_PIC_BINIT_RLIST_F15(b0_reflist[15]) |
  1004. + G1_REG_BD_P_REF_PIC_BINIT_RLIST_B15(b1_reflist[15]) |
  1005. + G1_REG_BD_P_REF_PIC_PINIT_RLIST_F0(p_reflist[0]) |
  1006. + G1_REG_BD_P_REF_PIC_PINIT_RLIST_F1(p_reflist[1]) |
  1007. + G1_REG_BD_P_REF_PIC_PINIT_RLIST_F2(p_reflist[2]) |
  1008. + G1_REG_BD_P_REF_PIC_PINIT_RLIST_F3(p_reflist[3]);
  1009. + vdpu_write_relaxed(vpu, reg, G1_REG_BD_P_REF_PIC);
  1010. +
  1011. + /*
  1012. + * Each G1_REG_FWD_PIC(x) register contains six consecutive
  1013. + * entries of P forward picture list, starting from index 4.
  1014. + */
  1015. + reg_num = 0;
  1016. + for (i = 4; i < HANTRO_H264_DPB_SIZE; i += 6) {
  1017. + reg = G1_REG_FWD_PIC_PINIT_RLIST_F0(p_reflist[i]) |
  1018. + G1_REG_FWD_PIC_PINIT_RLIST_F1(p_reflist[i + 1]) |
  1019. + G1_REG_FWD_PIC_PINIT_RLIST_F2(p_reflist[i + 2]) |
  1020. + G1_REG_FWD_PIC_PINIT_RLIST_F3(p_reflist[i + 3]) |
  1021. + G1_REG_FWD_PIC_PINIT_RLIST_F4(p_reflist[i + 4]) |
  1022. + G1_REG_FWD_PIC_PINIT_RLIST_F5(p_reflist[i + 5]);
  1023. + vdpu_write_relaxed(vpu, reg, G1_REG_FWD_PIC(reg_num++));
  1024. + }
  1025.  
  1026. - reg = G1_REG_APF_THRESHOLD(8);
  1027. - vdpu_write_relaxed(vpu, reg, G1_SWREG(55));
  1028. + /* Set up addresses of DPB buffers. */
  1029. + for (i = 0; i < HANTRO_H264_DPB_SIZE; i++) {
  1030. + dma_addr_t dma_addr = hantro_h264_get_ref_buf(ctx, i);
  1031.  
  1032. - /* Auxiliary buffer prepared in hantro_g1_h264_dec_prepare_table(). */
  1033. - vdpu_write_relaxed(vpu, ctx->h264_dec.priv.dma, G1_REG_QTABLE_BASE);
  1034. + vdpu_write_relaxed(vpu, dma_addr, G1_REG_ADDR_REF(i));
  1035. + }
  1036. +}
  1037. +
  1038. +static void set_buffers(struct hantro_ctx *ctx)
  1039. +{
  1040. + const struct hantro_h264_dec_ctrls *ctrls = &ctx->h264_dec.ctrls;
  1041. + struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1042. + struct hantro_dev *vpu = ctx->dev;
  1043. + dma_addr_t src_dma, dst_dma;
  1044. + size_t offset = 0;
  1045. +
  1046. + src_buf = hantro_get_src_buf(ctx);
  1047. + dst_buf = hantro_get_dst_buf(ctx);
  1048.  
  1049. /* Source (stream) buffer. */
  1050. - addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  1051. - vdpu_write_relaxed(vpu, addr, G1_REG_RLC_VLC_BASE);
  1052. + src_dma = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  1053. + vdpu_write_relaxed(vpu, src_dma, G1_REG_ADDR_STR);
  1054.  
  1055. /* Destination (decoded frame) buffer. */
  1056. - addr = hantro_get_dec_buf_addr(ctx, &dst_buf->vb2_buf);
  1057. + dst_dma = hantro_get_dec_buf_addr(ctx, &dst_buf->vb2_buf);
  1058. /* Adjust dma addr to start at second line for bottom field */
  1059. - if (slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  1060. + if (ctrls->slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  1061. offset = ALIGN(ctx->src_fmt.width, MB_DIM);
  1062. - vdpu_write_relaxed(vpu, addr + offset, G1_REG_DEC_OUT_BASE);
  1063. + vdpu_write_relaxed(vpu, dst_dma + offset, G1_REG_ADDR_DST);
  1064.  
  1065. /* Higher profiles require DMV buffer appended to reference frames. */
  1066. - if (sps->profile_idc > 66 && decode->nal_ref_idc) {
  1067. + if (ctrls->sps->profile_idc > 66 && ctrls->decode->nal_ref_idc) {
  1068. unsigned int bytes_per_mb = 384;
  1069.  
  1070. /* DMV buffer for monochrome start directly after Y-plane */
  1071. - if (sps->profile_idc >= 100 && sps->chroma_format_idc == 0)
  1072. + if (ctrls->sps->profile_idc >= 100 &&
  1073. + ctrls->sps->chroma_format_idc == 0)
  1074. bytes_per_mb = 256;
  1075. offset = bytes_per_mb * MB_WIDTH(ctx->src_fmt.width) *
  1076. MB_HEIGHT(ctx->src_fmt.height);
  1077. @@ -447,32 +265,42 @@
  1078. * DMV buffer is split in two for field encoded frames,
  1079. * adjust offset for bottom field
  1080. */
  1081. - if (slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  1082. + if (ctrls->slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  1083. offset += 32 * MB_WIDTH(ctx->src_fmt.width) *
  1084. MB_HEIGHT(ctx->src_fmt.height);
  1085. - vdpu_write_relaxed(vpu, addr + offset, G1_REG_DIR_MV_BASE);
  1086. + vdpu_write_relaxed(vpu, dst_dma + offset, G1_REG_ADDR_DIR_MV);
  1087. }
  1088.  
  1089. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 0), G1_REG_REFER0_BASE);
  1090. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 1), G1_REG_REFER1_BASE);
  1091. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 2), G1_REG_REFER2_BASE);
  1092. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 3), G1_REG_REFER3_BASE);
  1093. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 4), G1_REG_REFER4_BASE);
  1094. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 5), G1_REG_REFER5_BASE);
  1095. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 6), G1_REG_REFER6_BASE);
  1096. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 7), G1_REG_REFER7_BASE);
  1097. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 8), G1_REG_REFER8_BASE);
  1098. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 9), G1_REG_REFER9_BASE);
  1099. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 10), G1_REG_REFER10_BASE);
  1100. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 11), G1_REG_REFER11_BASE);
  1101. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 12), G1_REG_REFER12_BASE);
  1102. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 13), G1_REG_REFER13_BASE);
  1103. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 14), G1_REG_REFER14_BASE);
  1104. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 15), G1_REG_REFER15_BASE);
  1105. + /* Auxiliary buffer prepared in hantro_g1_h264_dec_prepare_table(). */
  1106. + vdpu_write_relaxed(vpu, ctx->h264_dec.priv.dma, G1_REG_ADDR_QTABLE);
  1107. +}
  1108. +
  1109. +void hantro_g1_h264_dec_run(struct hantro_ctx *ctx)
  1110. +{
  1111. + struct hantro_dev *vpu = ctx->dev;
  1112. +
  1113. + /* Prepare the H264 decoder context. */
  1114. + if (hantro_h264_dec_prepare_run(ctx))
  1115. + return;
  1116. +
  1117. + /* Configure hardware registers. */
  1118. + set_params(ctx);
  1119. + set_ref(ctx);
  1120. + set_buffers(ctx);
  1121.  
  1122. hantro_end_prepare_run(ctx);
  1123.  
  1124. /* Start decoding! */
  1125. - reg = G1_REG_DEC_E(1);
  1126. - vdpu_write(vpu, reg, G1_SWREG(1));
  1127. + vdpu_write_relaxed(vpu,
  1128. + G1_REG_CONFIG_DEC_AXI_RD_ID(0xffu) |
  1129. + G1_REG_CONFIG_DEC_TIMEOUT_E |
  1130. + G1_REG_CONFIG_DEC_OUT_ENDIAN |
  1131. + G1_REG_CONFIG_DEC_STRENDIAN_E |
  1132. + G1_REG_CONFIG_DEC_MAX_BURST(16) |
  1133. + G1_REG_CONFIG_DEC_OUTSWAP32_E |
  1134. + G1_REG_CONFIG_DEC_INSWAP32_E |
  1135. + G1_REG_CONFIG_DEC_STRSWAP32_E |
  1136. + G1_REG_CONFIG_DEC_CLK_GATE_E,
  1137. + G1_REG_CONFIG);
  1138. + vdpu_write(vpu, G1_REG_INTERRUPT_DEC_E, G1_REG_INTERRUPT);
  1139. }
  1140. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_g1_mpeg2_dec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_g1_mpeg2_dec.c
  1141. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_g1_mpeg2_dec.c 2020-02-14 22:59:08.117542364 -0800
  1142. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_g1_mpeg2_dec.c 2020-02-14 21:55:21.377863791 -0800
  1143. @@ -125,7 +125,7 @@
  1144. current_addr = addr;
  1145.  
  1146. if (picture->picture_structure == PICT_BOTTOM_FIELD)
  1147. - addr += ALIGN(ctx->src_fmt.width, MB_DIM);
  1148. + addr += ALIGN(ctx->dst_fmt.width, 16);
  1149. vdpu_write_relaxed(vpu, addr, G1_REG_DEC_OUT_BASE);
  1150.  
  1151. if (!forward_addr)
  1152. @@ -204,8 +204,8 @@
  1153. G1_REG_DEC_AXI_WR_ID(0);
  1154. vdpu_write_relaxed(vpu, reg, G1_SWREG(3));
  1155.  
  1156. - reg = G1_REG_PIC_MB_WIDTH(MB_WIDTH(ctx->src_fmt.width)) |
  1157. - G1_REG_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->src_fmt.height)) |
  1158. + reg = G1_REG_PIC_MB_WIDTH(MB_WIDTH(ctx->dst_fmt.width)) |
  1159. + G1_REG_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->dst_fmt.height)) |
  1160. G1_REG_ALT_SCAN_E(picture->alternate_scan) |
  1161. G1_REG_TOPFIELDFIRST_E(picture->top_field_first);
  1162. vdpu_write_relaxed(vpu, reg, G1_SWREG(4));
  1163. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_g1_vp8_dec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_g1_vp8_dec.c
  1164. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_g1_vp8_dec.c 2020-02-14 22:59:08.117542364 -0800
  1165. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_g1_vp8_dec.c 2020-02-14 21:55:21.377863791 -0800
  1166. @@ -430,8 +430,8 @@
  1167. {
  1168. const struct v4l2_ctrl_vp8_frame_header *hdr;
  1169. struct hantro_dev *vpu = ctx->dev;
  1170. - size_t height = ctx->src_fmt.height;
  1171. - size_t width = ctx->src_fmt.width;
  1172. + size_t height = ctx->dst_fmt.height;
  1173. + size_t width = ctx->dst_fmt.width;
  1174. u32 mb_width, mb_height;
  1175. u32 reg;
  1176.  
  1177. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_h264.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_h264.c
  1178. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_h264.c 2020-02-14 22:59:08.117542364 -0800
  1179. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_h264.c 2020-02-14 21:55:21.377863791 -0800
  1180. @@ -11,7 +11,7 @@
  1181. */
  1182.  
  1183. #include <linux/types.h>
  1184. -#include <linux/sort.h>
  1185. +#include <media/v4l2-h264.h>
  1186. #include <media/v4l2-mem2mem.h>
  1187.  
  1188. #include "hantro.h"
  1189. @@ -225,278 +225,26 @@
  1190. {
  1191. const struct hantro_h264_dec_ctrls *ctrls = &ctx->h264_dec.ctrls;
  1192. const struct v4l2_ctrl_h264_decode_params *dec_param = ctrls->decode;
  1193. - const struct v4l2_ctrl_h264_slice_params *slices = ctrls->slices;
  1194. struct hantro_h264_dec_priv_tbl *tbl = ctx->h264_dec.priv.cpu;
  1195. const struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
  1196. - u32 dpb_longterm = 0;
  1197. - u32 dpb_valid = 0;
  1198. int i;
  1199.  
  1200. - /*
  1201. - * Set up bit maps of valid and long term DPBs.
  1202. - * NOTE: The bits are reversed, i.e. MSb is DPB 0.
  1203. - */
  1204. - if ((slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC) || (slices[0].flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD)) {
  1205. - for (i = 0; i < HANTRO_H264_DPB_SIZE * 2; ++i) {
  1206. - // check for correct reference use
  1207. - u32 flag = (i & 0x1) ? V4L2_H264_DPB_ENTRY_FLAG_REF_BOTTOM : V4L2_H264_DPB_ENTRY_FLAG_REF_TOP;
  1208. - if (dpb[i / 2].flags & flag)
  1209. - dpb_valid |= BIT(HANTRO_H264_DPB_SIZE * 2 - 1 - i);
  1210. -
  1211. - if (dpb[i / 2].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  1212. - dpb_longterm |= BIT(HANTRO_H264_DPB_SIZE * 2 - 1 - i);
  1213. - }
  1214. -
  1215. - ctx->h264_dec.dpb_valid = dpb_valid;
  1216. - ctx->h264_dec.dpb_longterm = dpb_longterm;
  1217. - } else {
  1218. - for (i = 0; i < HANTRO_H264_DPB_SIZE; ++i) {
  1219. - if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
  1220. - dpb_valid |= BIT(HANTRO_H264_DPB_SIZE - 1 - i);
  1221. -
  1222. - if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  1223. - dpb_longterm |= BIT(HANTRO_H264_DPB_SIZE - 1 - i);
  1224. - }
  1225. -
  1226. - ctx->h264_dec.dpb_valid = dpb_valid << 16;
  1227. - ctx->h264_dec.dpb_longterm = dpb_longterm << 16;
  1228. - }
  1229. -
  1230. for (i = 0; i < HANTRO_H264_DPB_SIZE; ++i) {
  1231. - if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) {
  1232. - tbl->poc[i * 2] = dpb[i].top_field_order_cnt;
  1233. - tbl->poc[i * 2 + 1] = dpb[i].bottom_field_order_cnt;
  1234. - } else {
  1235. - tbl->poc[i * 2] = 0;
  1236. - tbl->poc[i * 2 + 1] = 0;
  1237. - }
  1238. + tbl->poc[i * 2] = dpb[i].top_field_order_cnt;
  1239. + tbl->poc[i * 2 + 1] = dpb[i].bottom_field_order_cnt;
  1240. }
  1241.  
  1242. - if ((slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC) || !(slices[0].flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD)) {
  1243. - if ((slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC))
  1244. - tbl->poc[32] = (slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD) ?
  1245. - dec_param->bottom_field_order_cnt :
  1246. - dec_param->top_field_order_cnt;
  1247. - else
  1248. - tbl->poc[32] = min(dec_param->top_field_order_cnt, dec_param->bottom_field_order_cnt);
  1249. - tbl->poc[33] = 0;
  1250. - } else {
  1251. - tbl->poc[32] = dec_param->top_field_order_cnt;
  1252. - tbl->poc[33] = dec_param->bottom_field_order_cnt;
  1253. - }
  1254. + tbl->poc[32] = dec_param->top_field_order_cnt;
  1255. + tbl->poc[33] = dec_param->bottom_field_order_cnt;
  1256.  
  1257. reorder_scaling_list(ctx);
  1258. }
  1259.  
  1260. -struct hantro_h264_reflist_builder {
  1261. - const struct v4l2_h264_dpb_entry *dpb;
  1262. - s32 pocs[HANTRO_H264_DPB_SIZE];
  1263. - u8 unordered_reflist[HANTRO_H264_DPB_SIZE];
  1264. - int frame_nums[HANTRO_H264_DPB_SIZE];
  1265. - s32 curpoc;
  1266. - u8 num_valid;
  1267. -};
  1268. -
  1269. -static void
  1270. -init_reflist_builder(struct hantro_ctx *ctx,
  1271. - struct hantro_h264_reflist_builder *b)
  1272. -{
  1273. - const struct v4l2_ctrl_h264_slice_params *slice_params;
  1274. - const struct v4l2_ctrl_h264_decode_params *dec_param;
  1275. - const struct v4l2_ctrl_h264_sps *sps;
  1276. - const struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
  1277. - int cur_frame_num, max_frame_num;
  1278. - unsigned int i;
  1279. -
  1280. - dec_param = ctx->h264_dec.ctrls.decode;
  1281. - slice_params = &ctx->h264_dec.ctrls.slices[0];
  1282. - sps = ctx->h264_dec.ctrls.sps;
  1283. - max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
  1284. - cur_frame_num = slice_params->frame_num;
  1285. -
  1286. - memset(b, 0, sizeof(*b));
  1287. - b->dpb = dpb;
  1288. - b->curpoc = (slice_params->flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD) ?
  1289. - dec_param->bottom_field_order_cnt :
  1290. - dec_param->top_field_order_cnt;
  1291. -
  1292. - for (i = 0; i < ARRAY_SIZE(ctx->h264_dec.dpb); i++) {
  1293. - u32 ref_flag = dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_REF_FRAME;
  1294. - if (!ref_flag)
  1295. - continue;
  1296. -
  1297. - /*
  1298. - * Handle frame_num wraparound as described in section
  1299. - * '8.2.4.1 Decoding process for picture numbers' of the spec.
  1300. - * TODO: This logic will have to be adjusted when we start
  1301. - * supporting interlaced content.
  1302. - */
  1303. - if (dpb[i].frame_num > cur_frame_num)
  1304. - b->frame_nums[i] = (int)dpb[i].frame_num - max_frame_num;
  1305. - else
  1306. - b->frame_nums[i] = dpb[i].frame_num;
  1307. -
  1308. - if (ref_flag == V4L2_H264_DPB_ENTRY_FLAG_REF_FRAME)
  1309. - b->pocs[i] = min(dpb[i].bottom_field_order_cnt, dpb[i].top_field_order_cnt);
  1310. - else if (ref_flag == V4L2_H264_DPB_ENTRY_FLAG_REF_BOTTOM)
  1311. - b->pocs[i] = dpb[i].bottom_field_order_cnt;
  1312. - else if (ref_flag == V4L2_H264_DPB_ENTRY_FLAG_REF_TOP)
  1313. - b->pocs[i] = dpb[i].top_field_order_cnt;
  1314. -
  1315. - b->unordered_reflist[b->num_valid] = i;
  1316. - b->num_valid++;
  1317. - }
  1318. -
  1319. - for (i = b->num_valid; i < ARRAY_SIZE(ctx->h264_dec.dpb); i++)
  1320. - b->unordered_reflist[i] = i;
  1321. -}
  1322. -
  1323. -static int p_ref_list_cmp(const void *ptra, const void *ptrb, const void *data)
  1324. -{
  1325. - const struct hantro_h264_reflist_builder *builder = data;
  1326. - const struct v4l2_h264_dpb_entry *a, *b;
  1327. - u8 idxa, idxb;
  1328. -
  1329. - idxa = *((u8 *)ptra);
  1330. - idxb = *((u8 *)ptrb);
  1331. - a = &builder->dpb[idxa];
  1332. - b = &builder->dpb[idxb];
  1333. -
  1334. - if ((a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) !=
  1335. - (b->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)) {
  1336. - /* Short term pics firt. */
  1337. - if (!(a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM))
  1338. - return -1;
  1339. - else
  1340. - return 1;
  1341. - }
  1342. -
  1343. - /*
  1344. - * Short term pics in descending pic num order, long term ones in
  1345. - * ascending order.
  1346. - */
  1347. - if (!(a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM))
  1348. - return HANTRO_CMP(builder->frame_nums[idxb],
  1349. - builder->frame_nums[idxa]);
  1350. -
  1351. - return HANTRO_CMP(a->pic_num, b->pic_num);
  1352. -}
  1353. -
  1354. -static int b0_ref_list_cmp(const void *ptra, const void *ptrb, const void *data)
  1355. -{
  1356. - const struct hantro_h264_reflist_builder *builder = data;
  1357. - const struct v4l2_h264_dpb_entry *a, *b;
  1358. - s32 poca, pocb;
  1359. - u8 idxa, idxb;
  1360. -
  1361. - idxa = *((u8 *)ptra);
  1362. - idxb = *((u8 *)ptrb);
  1363. - a = &builder->dpb[idxa];
  1364. - b = &builder->dpb[idxb];
  1365. -
  1366. - if ((a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) !=
  1367. - (b->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)) {
  1368. - /* Short term pics firt. */
  1369. - if (!(a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM))
  1370. - return -1;
  1371. - else
  1372. - return 1;
  1373. - }
  1374. -
  1375. - /* Long term pics in ascending pic num order. */
  1376. - if (a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  1377. - return HANTRO_CMP(a->pic_num, b->pic_num);
  1378. -
  1379. - poca = builder->pocs[idxa];
  1380. - pocb = builder->pocs[idxb];
  1381. -
  1382. - /*
  1383. - * Short term pics with POC < cur POC first in POC descending order
  1384. - * followed by short term pics with POC > cur POC in POC ascending
  1385. - * order.
  1386. - */
  1387. - if ((poca < builder->curpoc) != (pocb < builder->curpoc))
  1388. - return HANTRO_CMP(poca, pocb);
  1389. - else if (poca < builder->curpoc)
  1390. - return HANTRO_CMP(pocb, poca);
  1391. -
  1392. - return HANTRO_CMP(poca, pocb);
  1393. -}
  1394. -
  1395. -static int b1_ref_list_cmp(const void *ptra, const void *ptrb, const void *data)
  1396. -{
  1397. - const struct hantro_h264_reflist_builder *builder = data;
  1398. - const struct v4l2_h264_dpb_entry *a, *b;
  1399. - s32 poca, pocb;
  1400. - u8 idxa, idxb;
  1401. -
  1402. - idxa = *((u8 *)ptra);
  1403. - idxb = *((u8 *)ptrb);
  1404. - a = &builder->dpb[idxa];
  1405. - b = &builder->dpb[idxb];
  1406. -
  1407. - if ((a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) !=
  1408. - (b->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)) {
  1409. - /* Short term pics firt. */
  1410. - if (!(a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM))
  1411. - return -1;
  1412. - else
  1413. - return 1;
  1414. - }
  1415. -
  1416. - /* Long term pics in ascending pic num order. */
  1417. - if (a->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  1418. - return HANTRO_CMP(a->pic_num, b->pic_num);
  1419. -
  1420. - poca = builder->pocs[idxa];
  1421. - pocb = builder->pocs[idxb];
  1422. -
  1423. - /*
  1424. - * Short term pics with POC > cur POC first in POC ascending order
  1425. - * followed by short term pics with POC < cur POC in POC descending
  1426. - * order.
  1427. - */
  1428. - if ((poca < builder->curpoc) != (pocb < builder->curpoc))
  1429. - return HANTRO_CMP(pocb, poca);
  1430. - else if (poca < builder->curpoc)
  1431. - return HANTRO_CMP(pocb, poca);
  1432. -
  1433. - return HANTRO_CMP(poca, pocb);
  1434. -}
  1435. -
  1436. -static void
  1437. -build_p_ref_list(const struct hantro_h264_reflist_builder *builder,
  1438. - u8 *reflist)
  1439. -{
  1440. - memcpy(reflist, builder->unordered_reflist,
  1441. - sizeof(builder->unordered_reflist));
  1442. - sort_r(reflist, builder->num_valid, sizeof(*reflist),
  1443. - p_ref_list_cmp, NULL, builder);
  1444. -}
  1445. -
  1446. -static void
  1447. -build_b_ref_lists(const struct hantro_h264_reflist_builder *builder,
  1448. - u8 *b0_reflist, u8 *b1_reflist)
  1449. -{
  1450. - memcpy(b0_reflist, builder->unordered_reflist,
  1451. - sizeof(builder->unordered_reflist));
  1452. - sort_r(b0_reflist, builder->num_valid, sizeof(*b0_reflist),
  1453. - b0_ref_list_cmp, NULL, builder);
  1454. -
  1455. - memcpy(b1_reflist, builder->unordered_reflist,
  1456. - sizeof(builder->unordered_reflist));
  1457. - sort_r(b1_reflist, builder->num_valid, sizeof(*b1_reflist),
  1458. - b1_ref_list_cmp, NULL, builder);
  1459. -
  1460. - if (builder->num_valid > 1 &&
  1461. - !memcmp(b1_reflist, b0_reflist, builder->num_valid))
  1462. - swap(b1_reflist[0], b1_reflist[1]);
  1463. -}
  1464. -
  1465. static bool dpb_entry_match(const struct v4l2_h264_dpb_entry *a,
  1466. const struct v4l2_h264_dpb_entry *b)
  1467. {
  1468. - return a->reference_ts == b->reference_ts;
  1469. + return a->top_field_order_cnt == b->top_field_order_cnt &&
  1470. + a->bottom_field_order_cnt == b->bottom_field_order_cnt;
  1471. }
  1472.  
  1473. static void update_dpb(struct hantro_ctx *ctx)
  1474. @@ -510,13 +258,13 @@
  1475.  
  1476. /* Disable all entries by default. */
  1477. for (i = 0; i < ARRAY_SIZE(ctx->h264_dec.dpb); i++)
  1478. - ctx->h264_dec.dpb[i].flags = 0;
  1479. + ctx->h264_dec.dpb[i].flags &= ~V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
  1480.  
  1481. /* Try to match new DPB entries with existing ones by their POCs. */
  1482. for (i = 0; i < ARRAY_SIZE(dec_param->dpb); i++) {
  1483. const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
  1484.  
  1485. - if (!(ndpb->flags & V4L2_H264_DPB_ENTRY_FLAG_VALID))
  1486. + if (!(ndpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
  1487. continue;
  1488.  
  1489. /*
  1490. @@ -527,7 +275,8 @@
  1491. struct v4l2_h264_dpb_entry *cdpb;
  1492.  
  1493. cdpb = &ctx->h264_dec.dpb[j];
  1494. - if (!dpb_entry_match(cdpb, ndpb))
  1495. + if (cdpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE ||
  1496. + !dpb_entry_match(cdpb, ndpb))
  1497. continue;
  1498.  
  1499. *cdpb = *ndpb;
  1500. @@ -563,11 +312,7 @@
  1501. unsigned int dpb_idx)
  1502. {
  1503. struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
  1504. - const struct v4l2_ctrl_h264_decode_params *dec_param = ctx->h264_dec.ctrls.decode;
  1505. - const struct v4l2_ctrl_h264_slice_params *slices = ctx->h264_dec.ctrls.slices;
  1506. dma_addr_t dma_addr = 0;
  1507. - s32 cur_poc;
  1508. - u32 flags;
  1509.  
  1510. if (dpb[dpb_idx].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
  1511. dma_addr = hantro_get_ref(ctx, dpb[dpb_idx].reference_ts);
  1512. @@ -585,36 +330,14 @@
  1513. dma_addr = vb2_dma_contig_plane_dma_addr(buf, 0);
  1514. }
  1515.  
  1516. - cur_poc = slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD ?
  1517. - dec_param->bottom_field_order_cnt :
  1518. - dec_param->top_field_order_cnt;
  1519. - flags = dpb[dpb_idx].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD_PICTURE ? 0x2 : 0;
  1520. - flags |= abs(dpb[dpb_idx].top_field_order_cnt - cur_poc) <
  1521. - abs(dpb[dpb_idx].bottom_field_order_cnt - cur_poc) ?
  1522. - 0x1 : 0;
  1523. -
  1524. - return dma_addr | flags;
  1525. -}
  1526. -
  1527. -u16 hantro_h264_get_ref_nbr(struct hantro_ctx *ctx,
  1528. - unsigned int dpb_idx)
  1529. -{
  1530. - const struct v4l2_h264_dpb_entry *dpb = &ctx->h264_dec.dpb[dpb_idx];
  1531. -
  1532. - if (!(dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
  1533. - return 0;
  1534. -
  1535. - if (dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  1536. - return dpb->pic_num;
  1537. -
  1538. - return dpb->frame_num;
  1539. + return dma_addr;
  1540. }
  1541.  
  1542. int hantro_h264_dec_prepare_run(struct hantro_ctx *ctx)
  1543. {
  1544. struct hantro_h264_dec_hw_ctx *h264_ctx = &ctx->h264_dec;
  1545. struct hantro_h264_dec_ctrls *ctrls = &h264_ctx->ctrls;
  1546. - struct hantro_h264_reflist_builder reflist_builder;
  1547. + struct v4l2_h264_reflist_builder reflist_builder;
  1548.  
  1549. hantro_start_prepare_run(ctx);
  1550.  
  1551. @@ -650,10 +373,12 @@
  1552. prepare_table(ctx);
  1553.  
  1554. /* Build the P/B{0,1} ref lists. */
  1555. - init_reflist_builder(ctx, &reflist_builder);
  1556. - build_p_ref_list(&reflist_builder, h264_ctx->reflists.p);
  1557. - build_b_ref_lists(&reflist_builder, h264_ctx->reflists.b0,
  1558. - h264_ctx->reflists.b1);
  1559. + v4l2_h264_init_reflist_builder(&reflist_builder, ctrls->decode,
  1560. + &ctrls->slices[0], ctrls->sps,
  1561. + ctx->h264_dec.dpb);
  1562. + v4l2_h264_build_p_ref_list(&reflist_builder, h264_ctx->reflists.p);
  1563. + v4l2_h264_build_b_ref_lists(&reflist_builder, h264_ctx->reflists.b0,
  1564. + h264_ctx->reflists.b1);
  1565. return 0;
  1566. }
  1567.  
  1568. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_hw.h linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_hw.h
  1569. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_hw.h 2020-02-14 22:59:08.117542364 -0800
  1570. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_hw.h 2020-02-14 21:55:21.377863791 -0800
  1571. @@ -88,8 +88,6 @@
  1572. struct v4l2_h264_dpb_entry dpb[HANTRO_H264_DPB_SIZE];
  1573. struct hantro_h264_dec_reflists reflists;
  1574. struct hantro_h264_dec_ctrls ctrls;
  1575. - u32 dpb_longterm;
  1576. - u32 dpb_valid;
  1577. };
  1578.  
  1579. /**
  1580. @@ -172,11 +170,8 @@
  1581.  
  1582. dma_addr_t hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
  1583. unsigned int dpb_idx);
  1584. -u16 hantro_h264_get_ref_nbr(struct hantro_ctx *ctx,
  1585. - unsigned int dpb_idx);
  1586. int hantro_h264_dec_prepare_run(struct hantro_ctx *ctx);
  1587. void hantro_g1_h264_dec_run(struct hantro_ctx *ctx);
  1588. -void rk3399_vpu_h264_dec_run(struct hantro_ctx *ctx);
  1589. int hantro_h264_dec_init(struct hantro_ctx *ctx);
  1590. void hantro_h264_dec_exit(struct hantro_ctx *ctx);
  1591.  
  1592. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_vp8.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_vp8.c
  1593. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/hantro_vp8.c 2020-02-14 22:59:08.117542364 -0800
  1594. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/hantro_vp8.c 2020-02-14 21:55:21.377863791 -0800
  1595. @@ -151,8 +151,8 @@
  1596. int ret;
  1597.  
  1598. /* segment map table size calculation */
  1599. - mb_width = MB_WIDTH(ctx->src_fmt.width);
  1600. - mb_height = MB_HEIGHT(ctx->src_fmt.height);
  1601. + mb_width = DIV_ROUND_UP(ctx->dst_fmt.width, 16);
  1602. + mb_height = DIV_ROUND_UP(ctx->dst_fmt.height, 16);
  1603. segment_map_size = round_up(DIV_ROUND_UP(mb_width * mb_height, 4), 64);
  1604.  
  1605. /*
  1606. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/Kconfig linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/Kconfig
  1607. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/Kconfig 2020-02-14 22:59:08.117542364 -0800
  1608. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/Kconfig 2020-02-14 21:55:21.377863791 -0800
  1609. @@ -7,6 +7,7 @@
  1610. select VIDEOBUF2_DMA_CONTIG
  1611. select VIDEOBUF2_VMALLOC
  1612. select V4L2_MEM2MEM_DEV
  1613. + select V4L2_H264
  1614. help
  1615. Support for the Hantro IP based Video Processing Unit present on
  1616. Rockchip SoC, which accelerates video and image encoding and
  1617. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/Makefile linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/Makefile
  1618. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/Makefile 2020-02-14 22:59:08.117542364 -0800
  1619. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/Makefile 2020-02-14 21:55:21.377863791 -0800
  1620. @@ -9,7 +9,6 @@
  1621. hantro_g1_mpeg2_dec.o \
  1622. hantro_g1_vp8_dec.o \
  1623. rk3399_vpu_hw_jpeg_enc.o \
  1624. - rk3399_vpu_hw_h264_dec.o \
  1625. rk3399_vpu_hw_mpeg2_dec.o \
  1626. rk3399_vpu_hw_vp8_dec.o \
  1627. hantro_jpeg.o \
  1628. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw.c
  1629. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw.c 2020-02-14 22:59:08.117542364 -0800
  1630. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw.c 2020-02-14 21:55:21.377863791 -0800
  1631. @@ -61,19 +61,6 @@
  1632. .codec_mode = HANTRO_MODE_NONE,
  1633. },
  1634. {
  1635. - .fourcc = V4L2_PIX_FMT_H264_SLICE,
  1636. - .codec_mode = HANTRO_MODE_H264_DEC,
  1637. - .max_depth = 2,
  1638. - .frmsize = {
  1639. - .min_width = 48,
  1640. - .max_width = 1920,
  1641. - .step_width = MB_DIM,
  1642. - .min_height = 48,
  1643. - .max_height = 1088,
  1644. - .step_height = MB_DIM,
  1645. - },
  1646. - },
  1647. - {
  1648. .fourcc = V4L2_PIX_FMT_MPEG2_SLICE,
  1649. .codec_mode = HANTRO_MODE_MPEG2_DEC,
  1650. .max_depth = 2,
  1651. @@ -174,12 +161,6 @@
  1652. .init = hantro_jpeg_enc_init,
  1653. .exit = hantro_jpeg_enc_exit,
  1654. },
  1655. - [HANTRO_MODE_H264_DEC] = {
  1656. - .run = rk3399_vpu_h264_dec_run,
  1657. - .reset = rk3399_vpu_dec_reset,
  1658. - .init = hantro_h264_dec_init,
  1659. - .exit = hantro_h264_dec_exit,
  1660. - },
  1661. [HANTRO_MODE_MPEG2_DEC] = {
  1662. .run = rk3399_vpu_mpeg2_dec_run,
  1663. .reset = rk3399_vpu_dec_reset,
  1664. @@ -215,7 +196,7 @@
  1665. .dec_fmts = rk3399_vpu_dec_fmts,
  1666. .num_dec_fmts = ARRAY_SIZE(rk3399_vpu_dec_fmts),
  1667. .codec = HANTRO_JPEG_ENCODER | HANTRO_MPEG2_DECODER |
  1668. - HANTRO_VP8_DECODER | HANTRO_H264_DECODER,
  1669. + HANTRO_VP8_DECODER,
  1670. .codec_ops = rk3399_vpu_codec_ops,
  1671. .irqs = rk3399_irqs,
  1672. .num_irqs = ARRAY_SIZE(rk3399_irqs),
  1673. @@ -232,8 +213,7 @@
  1674. .dec_offset = 0x400,
  1675. .dec_fmts = rk3399_vpu_dec_fmts,
  1676. .num_dec_fmts = ARRAY_SIZE(rk3399_vpu_dec_fmts),
  1677. - .codec = HANTRO_MPEG2_DECODER | HANTRO_VP8_DECODER |
  1678. - HANTRO_H264_DECODER,
  1679. + .codec = HANTRO_MPEG2_DECODER | HANTRO_VP8_DECODER,
  1680. .codec_ops = rk3399_vpu_codec_ops,
  1681. .irqs = rk3328_irqs,
  1682. .num_irqs = ARRAY_SIZE(rk3328_irqs),
  1683. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw_h264_dec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw_h264_dec.c
  1684. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw_h264_dec.c 2020-02-14 22:59:08.117542364 -0800
  1685. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw_h264_dec.c 1969-12-31 16:00:00.000000000 -0800
  1686. @@ -1,493 +0,0 @@
  1687. -// SPDX-License-Identifier: GPL-2.0
  1688. -/*
  1689. - * Hantro VPU codec driver
  1690. - *
  1691. - * Copyright (c) 2014 Rockchip Electronics Co., Ltd.
  1692. - * Hertz Wong <[email protected]>
  1693. - * Herman Chen <[email protected]>
  1694. - *
  1695. - * Copyright (C) 2014 Google, Inc.
  1696. - * Tomasz Figa <[email protected]>
  1697. - */
  1698. -
  1699. -#include <linux/types.h>
  1700. -#include <linux/sort.h>
  1701. -
  1702. -#include <media/v4l2-mem2mem.h>
  1703. -
  1704. -#include "hantro_hw.h"
  1705. -#include "hantro_v4l2.h"
  1706. -
  1707. -#define VDPU_SWREG(nr) ((nr) * 4)
  1708. -
  1709. -#define VDPU_REG_DEC_OUT_BASE VDPU_SWREG(63)
  1710. -#define VDPU_REG_RLC_VLC_BASE VDPU_SWREG(64)
  1711. -#define VDPU_REG_QTABLE_BASE VDPU_SWREG(61)
  1712. -#define VDPU_REG_DIR_MV_BASE VDPU_SWREG(62)
  1713. -#define VDPU_REG_REFER0_BASE VDPU_SWREG(84)
  1714. -#define VDPU_REG_REFER1_BASE VDPU_SWREG(85)
  1715. -#define VDPU_REG_REFER2_BASE VDPU_SWREG(86)
  1716. -#define VDPU_REG_REFER3_BASE VDPU_SWREG(87)
  1717. -#define VDPU_REG_REFER4_BASE VDPU_SWREG(88)
  1718. -#define VDPU_REG_REFER5_BASE VDPU_SWREG(89)
  1719. -#define VDPU_REG_REFER6_BASE VDPU_SWREG(90)
  1720. -#define VDPU_REG_REFER7_BASE VDPU_SWREG(91)
  1721. -#define VDPU_REG_REFER8_BASE VDPU_SWREG(92)
  1722. -#define VDPU_REG_REFER9_BASE VDPU_SWREG(93)
  1723. -#define VDPU_REG_REFER10_BASE VDPU_SWREG(94)
  1724. -#define VDPU_REG_REFER11_BASE VDPU_SWREG(95)
  1725. -#define VDPU_REG_REFER12_BASE VDPU_SWREG(96)
  1726. -#define VDPU_REG_REFER13_BASE VDPU_SWREG(97)
  1727. -#define VDPU_REG_REFER14_BASE VDPU_SWREG(98)
  1728. -#define VDPU_REG_REFER15_BASE VDPU_SWREG(99)
  1729. -#define VDPU_REG_DEC_E(v) ((v) ? BIT(0) : 0)
  1730. -
  1731. -#define VDPU_REG_DEC_ADV_PRE_DIS(v) ((v) ? BIT(11) : 0)
  1732. -#define VDPU_REG_DEC_SCMD_DIS(v) ((v) ? BIT(10) : 0)
  1733. -#define VDPU_REG_FILTERING_DIS(v) ((v) ? BIT(8) : 0)
  1734. -#define VDPU_REG_PIC_FIXED_QUANT(v) ((v) ? BIT(7) : 0)
  1735. -#define VDPU_REG_DEC_LATENCY(v) (((v) << 1) & GENMASK(6, 1))
  1736. -
  1737. -#define VDPU_REG_INIT_QP(v) (((v) << 25) & GENMASK(30, 25))
  1738. -#define VDPU_REG_STREAM_LEN(v) (((v) << 0) & GENMASK(23, 0))
  1739. -
  1740. -#define VDPU_REG_APF_THRESHOLD(v) (((v) << 17) & GENMASK(30, 17))
  1741. -#define VDPU_REG_STARTMB_X(v) (((v) << 8) & GENMASK(16, 8))
  1742. -#define VDPU_REG_STARTMB_Y(v) (((v) << 0) & GENMASK(7, 0))
  1743. -
  1744. -#define VDPU_REG_DEC_MODE(v) (((v) << 0) & GENMASK(3, 0))
  1745. -
  1746. -#define VDPU_REG_DEC_STRENDIAN_E(v) ((v) ? BIT(5) : 0)
  1747. -#define VDPU_REG_DEC_STRSWAP32_E(v) ((v) ? BIT(4) : 0)
  1748. -#define VDPU_REG_DEC_OUTSWAP32_E(v) ((v) ? BIT(3) : 0)
  1749. -#define VDPU_REG_DEC_INSWAP32_E(v) ((v) ? BIT(2) : 0)
  1750. -#define VDPU_REG_DEC_OUT_ENDIAN(v) ((v) ? BIT(1) : 0)
  1751. -#define VDPU_REG_DEC_IN_ENDIAN(v) ((v) ? BIT(0) : 0)
  1752. -
  1753. -#define VDPU_REG_DEC_DATA_DISC_E(v) ((v) ? BIT(22) : 0)
  1754. -#define VDPU_REG_DEC_MAX_BURST(v) (((v) << 16) & GENMASK(20, 16))
  1755. -#define VDPU_REG_DEC_AXI_WR_ID(v) (((v) << 8) & GENMASK(15, 8))
  1756. -#define VDPU_REG_DEC_AXI_RD_ID(v) (((v) << 0) & GENMASK(7, 0))
  1757. -
  1758. -#define VDPU_REG_START_CODE_E(v) ((v) ? BIT(22) : 0)
  1759. -#define VDPU_REG_CH_8PIX_ILEAV_E(v) ((v) ? BIT(21) : 0)
  1760. -#define VDPU_REG_RLC_MODE_E(v) ((v) ? BIT(20) : 0)
  1761. -#define VDPU_REG_PIC_INTERLACE_E(v) ((v) ? BIT(17) : 0)
  1762. -#define VDPU_REG_PIC_FIELDMODE_E(v) ((v) ? BIT(16) : 0)
  1763. -#define VDPU_REG_PIC_TOPFIELD_E(v) ((v) ? BIT(13) : 0)
  1764. -#define VDPU_REG_WRITE_MVS_E(v) ((v) ? BIT(10) : 0)
  1765. -#define VDPU_REG_SEQ_MBAFF_E(v) ((v) ? BIT(7) : 0)
  1766. -#define VDPU_REG_PICORD_COUNT_E(v) ((v) ? BIT(6) : 0)
  1767. -#define VDPU_REG_DEC_TIMEOUT_E(v) ((v) ? BIT(5) : 0)
  1768. -#define VDPU_REG_DEC_CLK_GATE_E(v) ((v) ? BIT(4) : 0)
  1769. -
  1770. -#define VDPU_REG_PRED_BC_TAP_0_0(v) (((v) << 22) & GENMASK(31, 22))
  1771. -#define VDPU_REG_PRED_BC_TAP_0_1(v) (((v) << 12) & GENMASK(21, 12))
  1772. -#define VDPU_REG_PRED_BC_TAP_0_2(v) (((v) << 2) & GENMASK(11, 2))
  1773. -
  1774. -#define VDPU_REG_REFBU_E(v) ((v) ? BIT(31) : 0)
  1775. -
  1776. -#define VDPU_REG_PINIT_RLIST_F9(v) (((v) << 25) & GENMASK(29, 25))
  1777. -#define VDPU_REG_PINIT_RLIST_F8(v) (((v) << 20) & GENMASK(24, 20))
  1778. -#define VDPU_REG_PINIT_RLIST_F7(v) (((v) << 15) & GENMASK(19, 15))
  1779. -#define VDPU_REG_PINIT_RLIST_F6(v) (((v) << 10) & GENMASK(14, 10))
  1780. -#define VDPU_REG_PINIT_RLIST_F5(v) (((v) << 5) & GENMASK(9, 5))
  1781. -#define VDPU_REG_PINIT_RLIST_F4(v) (((v) << 0) & GENMASK(4, 0))
  1782. -
  1783. -#define VDPU_REG_PINIT_RLIST_F15(v) (((v) << 25) & GENMASK(29, 25))
  1784. -#define VDPU_REG_PINIT_RLIST_F14(v) (((v) << 20) & GENMASK(24, 20))
  1785. -#define VDPU_REG_PINIT_RLIST_F13(v) (((v) << 15) & GENMASK(19, 15))
  1786. -#define VDPU_REG_PINIT_RLIST_F12(v) (((v) << 10) & GENMASK(14, 10))
  1787. -#define VDPU_REG_PINIT_RLIST_F11(v) (((v) << 5) & GENMASK(9, 5))
  1788. -#define VDPU_REG_PINIT_RLIST_F10(v) (((v) << 0) & GENMASK(4, 0))
  1789. -
  1790. -#define VDPU_REG_REFER1_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1791. -#define VDPU_REG_REFER0_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1792. -
  1793. -#define VDPU_REG_REFER3_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1794. -#define VDPU_REG_REFER2_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1795. -
  1796. -#define VDPU_REG_REFER5_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1797. -#define VDPU_REG_REFER4_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1798. -
  1799. -#define VDPU_REG_REFER7_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1800. -#define VDPU_REG_REFER6_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1801. -
  1802. -#define VDPU_REG_REFER9_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1803. -#define VDPU_REG_REFER8_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1804. -
  1805. -#define VDPU_REG_REFER11_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1806. -#define VDPU_REG_REFER10_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1807. -
  1808. -#define VDPU_REG_REFER13_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1809. -#define VDPU_REG_REFER12_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1810. -
  1811. -#define VDPU_REG_REFER15_NBR(v) (((v) << 16) & GENMASK(31, 16))
  1812. -#define VDPU_REG_REFER14_NBR(v) (((v) << 0) & GENMASK(15, 0))
  1813. -
  1814. -#define VDPU_REG_BINIT_RLIST_F5(v) (((v) << 25) & GENMASK(29, 25))
  1815. -#define VDPU_REG_BINIT_RLIST_F4(v) (((v) << 20) & GENMASK(24, 20))
  1816. -#define VDPU_REG_BINIT_RLIST_F3(v) (((v) << 15) & GENMASK(19, 15))
  1817. -#define VDPU_REG_BINIT_RLIST_F2(v) (((v) << 10) & GENMASK(14, 10))
  1818. -#define VDPU_REG_BINIT_RLIST_F1(v) (((v) << 5) & GENMASK(9, 5))
  1819. -#define VDPU_REG_BINIT_RLIST_F0(v) (((v) << 0) & GENMASK(4, 0))
  1820. -
  1821. -#define VDPU_REG_BINIT_RLIST_F11(v) (((v) << 25) & GENMASK(29, 25))
  1822. -#define VDPU_REG_BINIT_RLIST_F10(v) (((v) << 20) & GENMASK(24, 20))
  1823. -#define VDPU_REG_BINIT_RLIST_F9(v) (((v) << 15) & GENMASK(19, 15))
  1824. -#define VDPU_REG_BINIT_RLIST_F8(v) (((v) << 10) & GENMASK(14, 10))
  1825. -#define VDPU_REG_BINIT_RLIST_F7(v) (((v) << 5) & GENMASK(9, 5))
  1826. -#define VDPU_REG_BINIT_RLIST_F6(v) (((v) << 0) & GENMASK(4, 0))
  1827. -
  1828. -#define VDPU_REG_BINIT_RLIST_F15(v) (((v) << 15) & GENMASK(19, 15))
  1829. -#define VDPU_REG_BINIT_RLIST_F14(v) (((v) << 10) & GENMASK(14, 10))
  1830. -#define VDPU_REG_BINIT_RLIST_F13(v) (((v) << 5) & GENMASK(9, 5))
  1831. -#define VDPU_REG_BINIT_RLIST_F12(v) (((v) << 0) & GENMASK(4, 0))
  1832. -
  1833. -#define VDPU_REG_BINIT_RLIST_B5(v) (((v) << 25) & GENMASK(29, 25))
  1834. -#define VDPU_REG_BINIT_RLIST_B4(v) (((v) << 20) & GENMASK(24, 20))
  1835. -#define VDPU_REG_BINIT_RLIST_B3(v) (((v) << 15) & GENMASK(19, 15))
  1836. -#define VDPU_REG_BINIT_RLIST_B2(v) (((v) << 10) & GENMASK(14, 10))
  1837. -#define VDPU_REG_BINIT_RLIST_B1(v) (((v) << 5) & GENMASK(9, 5))
  1838. -#define VDPU_REG_BINIT_RLIST_B0(v) (((v) << 0) & GENMASK(4, 0))
  1839. -
  1840. -#define VDPU_REG_BINIT_RLIST_B11(v) (((v) << 25) & GENMASK(29, 25))
  1841. -#define VDPU_REG_BINIT_RLIST_B10(v) (((v) << 20) & GENMASK(24, 20))
  1842. -#define VDPU_REG_BINIT_RLIST_B9(v) (((v) << 15) & GENMASK(19, 15))
  1843. -#define VDPU_REG_BINIT_RLIST_B8(v) (((v) << 10) & GENMASK(14, 10))
  1844. -#define VDPU_REG_BINIT_RLIST_B7(v) (((v) << 5) & GENMASK(9, 5))
  1845. -#define VDPU_REG_BINIT_RLIST_B6(v) (((v) << 0) & GENMASK(4, 0))
  1846. -
  1847. -#define VDPU_REG_BINIT_RLIST_B15(v) (((v) << 15) & GENMASK(19, 15))
  1848. -#define VDPU_REG_BINIT_RLIST_B14(v) (((v) << 10) & GENMASK(14, 10))
  1849. -#define VDPU_REG_BINIT_RLIST_B13(v) (((v) << 5) & GENMASK(9, 5))
  1850. -#define VDPU_REG_BINIT_RLIST_B12(v) (((v) << 0) & GENMASK(4, 0))
  1851. -
  1852. -#define VDPU_REG_PINIT_RLIST_F3(v) (((v) << 15) & GENMASK(19, 15))
  1853. -#define VDPU_REG_PINIT_RLIST_F2(v) (((v) << 10) & GENMASK(14, 10))
  1854. -#define VDPU_REG_PINIT_RLIST_F1(v) (((v) << 5) & GENMASK(9, 5))
  1855. -#define VDPU_REG_PINIT_RLIST_F0(v) (((v) << 0) & GENMASK(4, 0))
  1856. -
  1857. -#define VDPU_REG_REFER_LTERM_E(v) (((v) << 0) & GENMASK(31, 0))
  1858. -
  1859. -#define VDPU_REG_REFER_VALID_E(v) (((v) << 0) & GENMASK(31, 0))
  1860. -
  1861. -#define VDPU_REG_STRM_START_BIT(v) (((v) << 0) & GENMASK(5, 0))
  1862. -
  1863. -#define VDPU_REG_CH_QP_OFFSET2(v) (((v) << 22) & GENMASK(26, 22))
  1864. -#define VDPU_REG_CH_QP_OFFSET(v) (((v) << 17) & GENMASK(21, 17))
  1865. -#define VDPU_REG_PIC_MB_HEIGHT_P(v) (((v) << 9) & GENMASK(16, 9))
  1866. -#define VDPU_REG_PIC_MB_WIDTH(v) (((v) << 0) & GENMASK(8, 0))
  1867. -
  1868. -#define VDPU_REG_WEIGHT_BIPR_IDC(v) (((v) << 16) & GENMASK(17, 16))
  1869. -#define VDPU_REG_REF_FRAMES(v) (((v) << 0) & GENMASK(4, 0))
  1870. -
  1871. -#define VDPU_REG_FILT_CTRL_PRES(v) ((v) ? BIT(31) : 0)
  1872. -#define VDPU_REG_RDPIC_CNT_PRES(v) ((v) ? BIT(30) : 0)
  1873. -#define VDPU_REG_FRAMENUM_LEN(v) (((v) << 16) & GENMASK(20, 16))
  1874. -#define VDPU_REG_FRAMENUM(v) (((v) << 0) & GENMASK(15, 0))
  1875. -
  1876. -#define VDPU_REG_REFPIC_MK_LEN(v) (((v) << 16) & GENMASK(26, 16))
  1877. -#define VDPU_REG_IDR_PIC_ID(v) (((v) << 0) & GENMASK(15, 0))
  1878. -
  1879. -#define VDPU_REG_PPS_ID(v) (((v) << 24) & GENMASK(31, 24))
  1880. -#define VDPU_REG_REFIDX1_ACTIVE(v) (((v) << 19) & GENMASK(23, 19))
  1881. -#define VDPU_REG_REFIDX0_ACTIVE(v) (((v) << 14) & GENMASK(18, 14))
  1882. -#define VDPU_REG_POC_LENGTH(v) (((v) << 0) & GENMASK(7, 0))
  1883. -
  1884. -#define VDPU_REG_IDR_PIC_E(v) ((v) ? BIT(8) : 0)
  1885. -#define VDPU_REG_DIR_8X8_INFER_E(v) ((v) ? BIT(7) : 0)
  1886. -#define VDPU_REG_BLACKWHITE_E(v) ((v) ? BIT(6) : 0)
  1887. -#define VDPU_REG_CABAC_E(v) ((v) ? BIT(5) : 0)
  1888. -#define VDPU_REG_WEIGHT_PRED_E(v) ((v) ? BIT(4) : 0)
  1889. -#define VDPU_REG_CONST_INTRA_E(v) ((v) ? BIT(3) : 0)
  1890. -#define VDPU_REG_8X8TRANS_FLAG_E(v) ((v) ? BIT(2) : 0)
  1891. -#define VDPU_REG_TYPE1_QUANT_E(v) ((v) ? BIT(1) : 0)
  1892. -#define VDPU_REG_FIELDPIC_FLAG_E(v) ((v) ? BIT(0) : 0)
  1893. -
  1894. -void rk3399_vpu_h264_dec_run(struct hantro_ctx *ctx)
  1895. -{
  1896. - struct hantro_dev *vpu = ctx->dev;
  1897. - struct vb2_v4l2_buffer *src_buf, *dst_buf;
  1898. - const struct hantro_h264_dec_ctrls *ctrls;
  1899. - const struct v4l2_ctrl_h264_decode_params *decode;
  1900. - const struct v4l2_ctrl_h264_slice_params *slices;
  1901. - const struct v4l2_ctrl_h264_sps *sps;
  1902. - const struct v4l2_ctrl_h264_pps *pps;
  1903. - const u8 *b0_reflist, *b1_reflist, *p_reflist;
  1904. - dma_addr_t addr;
  1905. - size_t offset = 0;
  1906. - u32 reg;
  1907. -
  1908. - /* Prepare the H264 decoder context. */
  1909. - if (hantro_h264_dec_prepare_run(ctx))
  1910. - return;
  1911. -
  1912. - src_buf = hantro_get_src_buf(ctx);
  1913. - dst_buf = hantro_get_dst_buf(ctx);
  1914. -
  1915. - ctrls = &ctx->h264_dec.ctrls;
  1916. - decode = ctrls->decode;
  1917. - slices = ctrls->slices;
  1918. - sps = ctrls->sps;
  1919. - pps = ctrls->pps;
  1920. -
  1921. - b0_reflist = ctx->h264_dec.reflists.b0;
  1922. - b1_reflist = ctx->h264_dec.reflists.b1;
  1923. - p_reflist = ctx->h264_dec.reflists.p;
  1924. -
  1925. - reg = VDPU_REG_DEC_ADV_PRE_DIS(0) |
  1926. - VDPU_REG_DEC_SCMD_DIS(0) |
  1927. - VDPU_REG_FILTERING_DIS(0) |
  1928. - VDPU_REG_PIC_FIXED_QUANT(0) |
  1929. - VDPU_REG_DEC_LATENCY(0);
  1930. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(50));
  1931. -
  1932. - reg = VDPU_REG_INIT_QP(pps->pic_init_qp_minus26 + 26) |
  1933. - VDPU_REG_STREAM_LEN(vb2_get_plane_payload(&src_buf->vb2_buf, 0));
  1934. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(51));
  1935. -
  1936. - reg = VDPU_REG_APF_THRESHOLD(8) |
  1937. - VDPU_REG_STARTMB_X(0) |
  1938. - VDPU_REG_STARTMB_Y(0);
  1939. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(52));
  1940. -
  1941. - reg = VDPU_REG_DEC_MODE(0);
  1942. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(53));
  1943. -
  1944. - reg = VDPU_REG_DEC_STRENDIAN_E(1) |
  1945. - VDPU_REG_DEC_STRSWAP32_E(1) |
  1946. - VDPU_REG_DEC_OUTSWAP32_E(1) |
  1947. - VDPU_REG_DEC_INSWAP32_E(1) |
  1948. - VDPU_REG_DEC_OUT_ENDIAN(1) |
  1949. - VDPU_REG_DEC_IN_ENDIAN(0);
  1950. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(54));
  1951. -
  1952. - reg = VDPU_REG_DEC_DATA_DISC_E(0) |
  1953. - VDPU_REG_DEC_MAX_BURST(16) |
  1954. - VDPU_REG_DEC_AXI_WR_ID(0) |
  1955. - VDPU_REG_DEC_AXI_RD_ID(0xff);
  1956. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(56));
  1957. -
  1958. - reg = VDPU_REG_START_CODE_E(1) |
  1959. - VDPU_REG_CH_8PIX_ILEAV_E(0) |
  1960. - VDPU_REG_RLC_MODE_E(0) |
  1961. - VDPU_REG_PIC_INTERLACE_E(!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY) && (sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD || slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC)) |
  1962. - VDPU_REG_PIC_FIELDMODE_E(slices[0].flags & V4L2_H264_SLICE_FLAG_FIELD_PIC) |
  1963. - VDPU_REG_PIC_TOPFIELD_E(!(slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)) |
  1964. - VDPU_REG_WRITE_MVS_E(sps->profile_idc > 66 && decode->nal_ref_idc) |
  1965. - VDPU_REG_SEQ_MBAFF_E(sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD) |
  1966. - VDPU_REG_PICORD_COUNT_E(sps->profile_idc > 66) |
  1967. - VDPU_REG_DEC_TIMEOUT_E(1) |
  1968. - VDPU_REG_DEC_CLK_GATE_E(1);
  1969. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(57));
  1970. -
  1971. - reg = VDPU_REG_PRED_BC_TAP_0_0(1) |
  1972. - VDPU_REG_PRED_BC_TAP_0_1((u32)-5) |
  1973. - VDPU_REG_PRED_BC_TAP_0_2(20);
  1974. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(59));
  1975. -
  1976. - reg = VDPU_REG_REFBU_E(0);
  1977. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(65));
  1978. -
  1979. - reg = VDPU_REG_PINIT_RLIST_F9(p_reflist[9]) |
  1980. - VDPU_REG_PINIT_RLIST_F8(p_reflist[8]) |
  1981. - VDPU_REG_PINIT_RLIST_F7(p_reflist[7]) |
  1982. - VDPU_REG_PINIT_RLIST_F6(p_reflist[6]) |
  1983. - VDPU_REG_PINIT_RLIST_F5(p_reflist[5]) |
  1984. - VDPU_REG_PINIT_RLIST_F4(p_reflist[4]);
  1985. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(74));
  1986. -
  1987. - reg = VDPU_REG_PINIT_RLIST_F15(p_reflist[15]) |
  1988. - VDPU_REG_PINIT_RLIST_F14(p_reflist[14]) |
  1989. - VDPU_REG_PINIT_RLIST_F13(p_reflist[13]) |
  1990. - VDPU_REG_PINIT_RLIST_F12(p_reflist[12]) |
  1991. - VDPU_REG_PINIT_RLIST_F11(p_reflist[11]) |
  1992. - VDPU_REG_PINIT_RLIST_F10(p_reflist[10]);
  1993. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(75));
  1994. -
  1995. - reg = VDPU_REG_REFER1_NBR(hantro_h264_get_ref_nbr(ctx, 1)) |
  1996. - VDPU_REG_REFER0_NBR(hantro_h264_get_ref_nbr(ctx, 0));
  1997. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(76));
  1998. -
  1999. - reg = VDPU_REG_REFER3_NBR(hantro_h264_get_ref_nbr(ctx, 3)) |
  2000. - VDPU_REG_REFER2_NBR(hantro_h264_get_ref_nbr(ctx, 2));
  2001. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(77));
  2002. -
  2003. - reg = VDPU_REG_REFER5_NBR(hantro_h264_get_ref_nbr(ctx, 5)) |
  2004. - VDPU_REG_REFER4_NBR(hantro_h264_get_ref_nbr(ctx, 4));
  2005. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(78));
  2006. -
  2007. - reg = VDPU_REG_REFER7_NBR(hantro_h264_get_ref_nbr(ctx, 7)) |
  2008. - VDPU_REG_REFER6_NBR(hantro_h264_get_ref_nbr(ctx, 6));
  2009. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(79));
  2010. -
  2011. - reg = VDPU_REG_REFER9_NBR(hantro_h264_get_ref_nbr(ctx, 9)) |
  2012. - VDPU_REG_REFER8_NBR(hantro_h264_get_ref_nbr(ctx, 8));
  2013. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(80));
  2014. -
  2015. - reg = VDPU_REG_REFER11_NBR(hantro_h264_get_ref_nbr(ctx, 11)) |
  2016. - VDPU_REG_REFER10_NBR(hantro_h264_get_ref_nbr(ctx, 10));
  2017. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(81));
  2018. -
  2019. - reg = VDPU_REG_REFER13_NBR(hantro_h264_get_ref_nbr(ctx, 13)) |
  2020. - VDPU_REG_REFER12_NBR(hantro_h264_get_ref_nbr(ctx, 12));
  2021. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(82));
  2022. -
  2023. - reg = VDPU_REG_REFER15_NBR(hantro_h264_get_ref_nbr(ctx, 15)) |
  2024. - VDPU_REG_REFER14_NBR(hantro_h264_get_ref_nbr(ctx, 14));
  2025. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(83));
  2026. -
  2027. - reg = VDPU_REG_BINIT_RLIST_F5(b0_reflist[5]) |
  2028. - VDPU_REG_BINIT_RLIST_F4(b0_reflist[4]) |
  2029. - VDPU_REG_BINIT_RLIST_F3(b0_reflist[3]) |
  2030. - VDPU_REG_BINIT_RLIST_F2(b0_reflist[2]) |
  2031. - VDPU_REG_BINIT_RLIST_F1(b0_reflist[1]) |
  2032. - VDPU_REG_BINIT_RLIST_F0(b0_reflist[0]);
  2033. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(100));
  2034. -
  2035. - reg = VDPU_REG_BINIT_RLIST_F11(b0_reflist[11]) |
  2036. - VDPU_REG_BINIT_RLIST_F10(b0_reflist[10]) |
  2037. - VDPU_REG_BINIT_RLIST_F9(b0_reflist[9]) |
  2038. - VDPU_REG_BINIT_RLIST_F8(b0_reflist[8]) |
  2039. - VDPU_REG_BINIT_RLIST_F7(b0_reflist[7]) |
  2040. - VDPU_REG_BINIT_RLIST_F6(b0_reflist[6]);
  2041. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(101));
  2042. -
  2043. - reg = VDPU_REG_BINIT_RLIST_F15(b0_reflist[15]) |
  2044. - VDPU_REG_BINIT_RLIST_F14(b0_reflist[14]) |
  2045. - VDPU_REG_BINIT_RLIST_F13(b0_reflist[13]) |
  2046. - VDPU_REG_BINIT_RLIST_F12(b0_reflist[12]);
  2047. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(102));
  2048. -
  2049. - reg = VDPU_REG_BINIT_RLIST_B5(b1_reflist[5]) |
  2050. - VDPU_REG_BINIT_RLIST_B4(b1_reflist[4]) |
  2051. - VDPU_REG_BINIT_RLIST_B3(b1_reflist[3]) |
  2052. - VDPU_REG_BINIT_RLIST_B2(b1_reflist[2]) |
  2053. - VDPU_REG_BINIT_RLIST_B1(b1_reflist[1]) |
  2054. - VDPU_REG_BINIT_RLIST_B0(b1_reflist[0]);
  2055. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(103));
  2056. -
  2057. - reg = VDPU_REG_BINIT_RLIST_B11(b1_reflist[11]) |
  2058. - VDPU_REG_BINIT_RLIST_B10(b1_reflist[10]) |
  2059. - VDPU_REG_BINIT_RLIST_B9(b1_reflist[9]) |
  2060. - VDPU_REG_BINIT_RLIST_B8(b1_reflist[8]) |
  2061. - VDPU_REG_BINIT_RLIST_B7(b1_reflist[7]) |
  2062. - VDPU_REG_BINIT_RLIST_B6(b1_reflist[6]);
  2063. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(104));
  2064. -
  2065. - reg = VDPU_REG_BINIT_RLIST_B15(b1_reflist[15]) |
  2066. - VDPU_REG_BINIT_RLIST_B14(b1_reflist[14]) |
  2067. - VDPU_REG_BINIT_RLIST_B13(b1_reflist[13]) |
  2068. - VDPU_REG_BINIT_RLIST_B12(b1_reflist[12]);
  2069. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(105));
  2070. -
  2071. - reg = VDPU_REG_PINIT_RLIST_F3(p_reflist[3]) |
  2072. - VDPU_REG_PINIT_RLIST_F2(p_reflist[2]) |
  2073. - VDPU_REG_PINIT_RLIST_F1(p_reflist[1]) |
  2074. - VDPU_REG_PINIT_RLIST_F0(p_reflist[0]);
  2075. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(106));
  2076. -
  2077. - reg = VDPU_REG_REFER_LTERM_E(ctx->h264_dec.dpb_longterm);
  2078. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(107));
  2079. -
  2080. - reg = VDPU_REG_REFER_VALID_E(ctx->h264_dec.dpb_valid);
  2081. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(108));
  2082. -
  2083. - reg = VDPU_REG_STRM_START_BIT(0);
  2084. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(109));
  2085. -
  2086. - reg = VDPU_REG_CH_QP_OFFSET2(pps->second_chroma_qp_index_offset) |
  2087. - VDPU_REG_CH_QP_OFFSET(pps->chroma_qp_index_offset) |
  2088. - VDPU_REG_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->src_fmt.height)) |
  2089. - VDPU_REG_PIC_MB_WIDTH(MB_WIDTH(ctx->src_fmt.width));
  2090. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(110));
  2091. -
  2092. - reg = VDPU_REG_WEIGHT_BIPR_IDC(pps->weighted_bipred_idc) |
  2093. - VDPU_REG_REF_FRAMES(sps->max_num_ref_frames);
  2094. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(111));
  2095. -
  2096. - reg = VDPU_REG_FILT_CTRL_PRES(pps->flags & V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT) |
  2097. - VDPU_REG_RDPIC_CNT_PRES(pps->flags & V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT) |
  2098. - VDPU_REG_FRAMENUM_LEN(sps->log2_max_frame_num_minus4 + 4) |
  2099. - VDPU_REG_FRAMENUM(slices[0].frame_num);
  2100. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(112));
  2101. -
  2102. - reg = VDPU_REG_REFPIC_MK_LEN(slices[0].dec_ref_pic_marking_bit_size) |
  2103. - VDPU_REG_IDR_PIC_ID(slices[0].idr_pic_id);
  2104. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(113));
  2105. -
  2106. - reg = VDPU_REG_PPS_ID(slices[0].pic_parameter_set_id) |
  2107. - VDPU_REG_REFIDX1_ACTIVE(pps->num_ref_idx_l1_default_active_minus1 + 1) |
  2108. - VDPU_REG_REFIDX0_ACTIVE(pps->num_ref_idx_l0_default_active_minus1 + 1) |
  2109. - VDPU_REG_POC_LENGTH(slices[0].pic_order_cnt_bit_size);
  2110. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(114));
  2111. -
  2112. - reg = VDPU_REG_IDR_PIC_E(decode->flags & V4L2_H264_DECODE_PARAM_FLAG_IDR_PIC) |
  2113. - VDPU_REG_DIR_8X8_INFER_E(sps->flags & V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE) |
  2114. - VDPU_REG_BLACKWHITE_E(sps->profile_idc >= 100 && sps->chroma_format_idc == 0) |
  2115. - VDPU_REG_CABAC_E(pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE) |
  2116. - VDPU_REG_WEIGHT_PRED_E(pps->flags & V4L2_H264_PPS_FLAG_WEIGHTED_PRED) |
  2117. - VDPU_REG_CONST_INTRA_E(pps->flags & V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED) |
  2118. - VDPU_REG_8X8TRANS_FLAG_E(pps->flags & V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE) |
  2119. - VDPU_REG_TYPE1_QUANT_E(1) |
  2120. - VDPU_REG_FIELDPIC_FLAG_E(!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY));
  2121. - vdpu_write_relaxed(vpu, reg, VDPU_SWREG(115));
  2122. -
  2123. - /* Auxiliary buffer prepared in hantro_g1_h264_dec_prepare_table(). */
  2124. - vdpu_write_relaxed(vpu, ctx->h264_dec.priv.dma, VDPU_REG_QTABLE_BASE);
  2125. -
  2126. - /* Source (stream) buffer. */
  2127. - addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  2128. - vdpu_write_relaxed(vpu, addr, VDPU_REG_RLC_VLC_BASE);
  2129. -
  2130. - /* Destination (decoded frame) buffer. */
  2131. - addr = hantro_get_dec_buf_addr(ctx, &dst_buf->vb2_buf);
  2132. - /* Adjust dma addr to start at second line for bottom field */
  2133. - if (slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  2134. - offset = ALIGN(ctx->src_fmt.width, MB_DIM);
  2135. - vdpu_write_relaxed(vpu, addr + offset, VDPU_REG_DEC_OUT_BASE);
  2136. -
  2137. - /* Higher profiles require DMV buffer appended to reference frames. */
  2138. - if (sps->profile_idc > 66 && decode->nal_ref_idc) {
  2139. - unsigned int bytes_per_mb = 384;
  2140. -
  2141. - /* DMV buffer for monochrome start directly after Y-plane */
  2142. - if (sps->profile_idc >= 100 && sps->chroma_format_idc == 0)
  2143. - bytes_per_mb = 256;
  2144. - offset = bytes_per_mb * MB_WIDTH(ctx->src_fmt.width) *
  2145. - MB_HEIGHT(ctx->src_fmt.height);
  2146. -
  2147. - /*
  2148. - * DMV buffer is split in two for field encoded frames,
  2149. - * adjust offset for bottom field
  2150. - */
  2151. - if (slices[0].flags & V4L2_H264_SLICE_FLAG_BOTTOM_FIELD)
  2152. - offset += 32 * MB_WIDTH(ctx->src_fmt.width) *
  2153. - MB_HEIGHT(ctx->src_fmt.height);
  2154. - vdpu_write_relaxed(vpu, addr + offset, VDPU_REG_DIR_MV_BASE);
  2155. - }
  2156. -
  2157. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 0), VDPU_REG_REFER0_BASE);
  2158. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 1), VDPU_REG_REFER1_BASE);
  2159. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 2), VDPU_REG_REFER2_BASE);
  2160. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 3), VDPU_REG_REFER3_BASE);
  2161. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 4), VDPU_REG_REFER4_BASE);
  2162. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 5), VDPU_REG_REFER5_BASE);
  2163. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 6), VDPU_REG_REFER6_BASE);
  2164. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 7), VDPU_REG_REFER7_BASE);
  2165. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 8), VDPU_REG_REFER8_BASE);
  2166. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 9), VDPU_REG_REFER9_BASE);
  2167. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 10), VDPU_REG_REFER10_BASE);
  2168. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 11), VDPU_REG_REFER11_BASE);
  2169. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 12), VDPU_REG_REFER12_BASE);
  2170. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 13), VDPU_REG_REFER13_BASE);
  2171. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 14), VDPU_REG_REFER14_BASE);
  2172. - vdpu_write_relaxed(vpu, hantro_h264_get_ref_buf(ctx, 15), VDPU_REG_REFER15_BASE);
  2173. -
  2174. - hantro_end_prepare_run(ctx);
  2175. -
  2176. - /* Start decoding! */
  2177. - reg = vdpu_read(vpu, VDPU_SWREG(57)) | VDPU_REG_DEC_E(1);
  2178. - vdpu_write(vpu, reg, VDPU_SWREG(57));
  2179. -}
  2180. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw_mpeg2_dec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw_mpeg2_dec.c
  2181. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw_mpeg2_dec.c 2020-02-14 22:59:08.117542364 -0800
  2182. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw_mpeg2_dec.c 2020-02-14 21:55:21.377863791 -0800
  2183. @@ -127,7 +127,7 @@
  2184. current_addr = addr;
  2185.  
  2186. if (picture->picture_structure == PICT_BOTTOM_FIELD)
  2187. - addr += ALIGN(ctx->src_fmt.width, MB_DIM);
  2188. + addr += ALIGN(ctx->dst_fmt.width, 16);
  2189. vdpu_write_relaxed(vpu, addr, VDPU_REG_DEC_OUT_BASE);
  2190.  
  2191. if (!forward_addr)
  2192. @@ -220,8 +220,8 @@
  2193. VDPU_REG_DEC_CLK_GATE_E(1);
  2194. vdpu_write_relaxed(vpu, reg, VDPU_SWREG(57));
  2195.  
  2196. - reg = VDPU_REG_PIC_MB_WIDTH(MB_WIDTH(ctx->src_fmt.width)) |
  2197. - VDPU_REG_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->src_fmt.height)) |
  2198. + reg = VDPU_REG_PIC_MB_WIDTH(MB_WIDTH(ctx->dst_fmt.width)) |
  2199. + VDPU_REG_PIC_MB_HEIGHT_P(MB_HEIGHT(ctx->dst_fmt.height)) |
  2200. VDPU_REG_ALT_SCAN_E(picture->alternate_scan) |
  2201. VDPU_REG_TOPFIELDFIRST_E(picture->top_field_first);
  2202. vdpu_write_relaxed(vpu, reg, VDPU_SWREG(120));
  2203. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw_vp8_dec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw_vp8_dec.c
  2204. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/hantro/rk3399_vpu_hw_vp8_dec.c 2020-02-14 22:59:08.117542364 -0800
  2205. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/hantro/rk3399_vpu_hw_vp8_dec.c 2020-02-14 21:55:21.377863791 -0800
  2206. @@ -508,8 +508,8 @@
  2207. {
  2208. const struct v4l2_ctrl_vp8_frame_header *hdr;
  2209. struct hantro_dev *vpu = ctx->dev;
  2210. - size_t height = ctx->src_fmt.height;
  2211. - size_t width = ctx->src_fmt.width;
  2212. + size_t height = ctx->dst_fmt.height;
  2213. + size_t width = ctx->dst_fmt.width;
  2214. u32 mb_width, mb_height;
  2215. u32 reg;
  2216.  
  2217. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/Kconfig linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/Kconfig
  2218. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/Kconfig 2020-02-14 22:59:08.113542336 -0800
  2219. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/Kconfig 2020-02-14 21:55:21.373863761 -0800
  2220. @@ -30,6 +30,8 @@
  2221.  
  2222. source "drivers/staging/media/omap4iss/Kconfig"
  2223.  
  2224. +source "drivers/staging/media/rkvdec/Kconfig"
  2225. +
  2226. source "drivers/staging/media/sunxi/Kconfig"
  2227.  
  2228. source "drivers/staging/media/tegra-vde/Kconfig"
  2229. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/Makefile linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/Makefile
  2230. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/Makefile 2020-02-14 22:59:08.113542336 -0800
  2231. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/Makefile 2020-02-14 21:55:21.373863761 -0800
  2232. @@ -3,6 +3,7 @@
  2233. obj-$(CONFIG_VIDEO_IMX_MEDIA) += imx/
  2234. obj-$(CONFIG_VIDEO_MESON_VDEC) += meson/vdec/
  2235. obj-$(CONFIG_VIDEO_OMAP4) += omap4iss/
  2236. +obj-$(CONFIG_VIDEO_ROCKCHIP_VDEC) += rkvdec/
  2237. obj-$(CONFIG_VIDEO_SUNXI) += sunxi/
  2238. obj-$(CONFIG_TEGRA_VDE) += tegra-vde/
  2239. obj-$(CONFIG_VIDEO_HANTRO) += hantro/
  2240. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/Kconfig linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/Kconfig
  2241. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/Kconfig 1969-12-31 16:00:00.000000000 -0800
  2242. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/Kconfig 2020-02-14 21:55:21.389863881 -0800
  2243. @@ -0,0 +1,15 @@
  2244. +# SPDX-License-Identifier: GPL-2.0
  2245. +config VIDEO_ROCKCHIP_VDEC
  2246. + tristate "Rockchip Video Decoder driver"
  2247. + depends on ARCH_ROCKCHIP || COMPILE_TEST
  2248. + depends on VIDEO_DEV && VIDEO_V4L2 && MEDIA_CONTROLLER
  2249. + depends on MEDIA_CONTROLLER_REQUEST_API
  2250. + select VIDEOBUF2_DMA_CONTIG
  2251. + select VIDEOBUF2_VMALLOC
  2252. + select V4L2_MEM2MEM_DEV
  2253. + select V4L2_H264
  2254. + help
  2255. + Support for the Rockchip Video Decoder IP present on Rockchip SoCs,
  2256. + which accelerates video decoding.
  2257. + To compile this driver as a module, choose M here: the module
  2258. + will be called rockchip-vdec.
  2259. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/Makefile linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/Makefile
  2260. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/Makefile 1969-12-31 16:00:00.000000000 -0800
  2261. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/Makefile 2020-02-14 21:55:21.389863881 -0800
  2262. @@ -0,0 +1,3 @@
  2263. +obj-$(CONFIG_VIDEO_ROCKCHIP_VDEC) += rockchip-vdec.o
  2264. +
  2265. +rockchip-vdec-y += rkvdec.o rkvdec-h264.o
  2266. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec.c
  2267. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec.c 1969-12-31 16:00:00.000000000 -0800
  2268. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec.c 2020-02-14 21:55:21.389863881 -0800
  2269. @@ -0,0 +1,1134 @@
  2270. +// SPDX-License-Identifier: GPL-2.0
  2271. +/*
  2272. + * Rockchip Video Decoder driver
  2273. + *
  2274. + * Copyright (C) 2019 Collabora, Ltd.
  2275. + *
  2276. + * Based on rkvdec driver by Google LLC. (Tomasz Figa <[email protected]>)
  2277. + * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
  2278. + * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  2279. + */
  2280. +
  2281. +#include <linux/clk.h>
  2282. +#include <linux/interrupt.h>
  2283. +#include <linux/module.h>
  2284. +#include <linux/of.h>
  2285. +#include <linux/platform_device.h>
  2286. +#include <linux/pm.h>
  2287. +#include <linux/pm_runtime.h>
  2288. +#include <linux/slab.h>
  2289. +#include <linux/videodev2.h>
  2290. +#include <linux/workqueue.h>
  2291. +#include <media/v4l2-event.h>
  2292. +#include <media/v4l2-mem2mem.h>
  2293. +#include <media/videobuf2-core.h>
  2294. +#include <media/videobuf2-vmalloc.h>
  2295. +
  2296. +#include "rkvdec.h"
  2297. +#include "rkvdec-regs.h"
  2298. +
  2299. +static const struct rkvdec_ctrl_desc rkvdec_h264_ctrl_descs[] = {
  2300. + {
  2301. + .per_request = true,
  2302. + .mandatory = true,
  2303. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS,
  2304. + },
  2305. + {
  2306. + .per_request = true,
  2307. + .mandatory = true,
  2308. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAMS,
  2309. + },
  2310. + {
  2311. + .per_request = true,
  2312. + .mandatory = true,
  2313. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_SPS,
  2314. + },
  2315. + {
  2316. + .per_request = true,
  2317. + .mandatory = true,
  2318. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_PPS,
  2319. + },
  2320. + {
  2321. + .per_request = true,
  2322. + .mandatory = true,
  2323. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX,
  2324. + },
  2325. + {
  2326. + .mandatory = true,
  2327. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_DECODE_MODE,
  2328. + .cfg.min = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
  2329. + .cfg.max = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
  2330. + .cfg.def = V4L2_MPEG_VIDEO_H264_DECODE_MODE_FRAME_BASED,
  2331. + },
  2332. + {
  2333. + .mandatory = true,
  2334. + .cfg.id = V4L2_CID_MPEG_VIDEO_H264_START_CODE,
  2335. + .cfg.min = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
  2336. + .cfg.def = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
  2337. + .cfg.max = V4L2_MPEG_VIDEO_H264_START_CODE_ANNEX_B,
  2338. + },
  2339. +};
  2340. +
  2341. +static const struct rkvdec_ctrls rkvdec_h264_ctrls = {
  2342. + .ctrls = rkvdec_h264_ctrl_descs,
  2343. + .num_ctrls = ARRAY_SIZE(rkvdec_h264_ctrl_descs),
  2344. +};
  2345. +
  2346. +static const u32 rkvdec_h264_decoded_fmts[] = {
  2347. + V4L2_PIX_FMT_NV12,
  2348. +};
  2349. +
  2350. +static const struct rkvdec_coded_fmt_desc rkvdec_coded_fmts[] = {
  2351. + {
  2352. + .fourcc = V4L2_PIX_FMT_H264_SLICE,
  2353. + .frmsize = {
  2354. + .min_width = 48,
  2355. + .max_width = 4096,
  2356. + .step_width = 16,
  2357. + .min_height = 48,
  2358. + .max_height = 2304,
  2359. + .step_height = 16,
  2360. + },
  2361. + .ctrls = &rkvdec_h264_ctrls,
  2362. + .ops = &rkvdec_h264_fmt_ops,
  2363. + .num_decoded_fmts = ARRAY_SIZE(rkvdec_h264_decoded_fmts),
  2364. + .decoded_fmts = rkvdec_h264_decoded_fmts,
  2365. + }
  2366. +};
  2367. +
  2368. +static const struct rkvdec_coded_fmt_desc *
  2369. +rkvdec_find_coded_fmt_desc(u32 fourcc)
  2370. +{
  2371. + unsigned int i;
  2372. +
  2373. + for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
  2374. + if (rkvdec_coded_fmts[i].fourcc == fourcc)
  2375. + return &rkvdec_coded_fmts[i];
  2376. + }
  2377. +
  2378. + return NULL;
  2379. +}
  2380. +
  2381. +static void rkvdec_reset_fmt(struct rkvdec_ctx *ctx, struct v4l2_format *f,
  2382. + u32 fourcc)
  2383. +{
  2384. + memset(f, 0, sizeof(*f));
  2385. + f->fmt.pix_mp.pixelformat = fourcc;
  2386. + f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  2387. + f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709,
  2388. + f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  2389. + f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
  2390. + f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
  2391. +}
  2392. +
  2393. +static void rkvdec_reset_coded_fmt(struct rkvdec_ctx *ctx)
  2394. +{
  2395. + struct v4l2_format *f = &ctx->coded_fmt;
  2396. +
  2397. + ctx->coded_fmt_desc = &rkvdec_coded_fmts[0];
  2398. + rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->fourcc);
  2399. +
  2400. + f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  2401. + f->fmt.pix_mp.width = ctx->coded_fmt_desc->frmsize.min_width;
  2402. + f->fmt.pix_mp.height = ctx->coded_fmt_desc->frmsize.min_height;
  2403. +
  2404. + if (ctx->coded_fmt_desc->ops->adjust_fmt)
  2405. + ctx->coded_fmt_desc->ops->adjust_fmt(ctx, f);
  2406. +}
  2407. +
  2408. +static void rkvdec_reset_decoded_fmt(struct rkvdec_ctx *ctx)
  2409. +{
  2410. + struct v4l2_format *f = &ctx->decoded_fmt;
  2411. +
  2412. + if (!ctx->coded_fmt_desc)
  2413. + rkvdec_reset_coded_fmt(ctx);
  2414. +
  2415. + rkvdec_reset_fmt(ctx, f, ctx->coded_fmt_desc->decoded_fmts[0]);
  2416. + f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  2417. + v4l2_fill_pixfmt_mp(&f->fmt.pix_mp,
  2418. + ctx->coded_fmt_desc->decoded_fmts[0],
  2419. + ctx->coded_fmt_desc->frmsize.min_width,
  2420. + ctx->coded_fmt_desc->frmsize.min_height);
  2421. +}
  2422. +
  2423. +static int rkvdec_enum_framesizes(struct file *file, void *priv,
  2424. + struct v4l2_frmsizeenum *fsize)
  2425. +{
  2426. + const struct rkvdec_coded_fmt_desc *fmt;
  2427. +
  2428. + if (fsize->index != 0)
  2429. + return -EINVAL;
  2430. +
  2431. + fmt = rkvdec_find_coded_fmt_desc(fsize->pixel_format);
  2432. + if (!fmt)
  2433. + return -EINVAL;
  2434. +
  2435. + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  2436. + fsize->stepwise = fmt->frmsize;
  2437. + return 0;
  2438. +}
  2439. +
  2440. +static int rkvdec_querycap(struct file *file, void *priv,
  2441. + struct v4l2_capability *cap)
  2442. +{
  2443. + struct rkvdec_dev *rkvdec = video_drvdata(file);
  2444. + struct video_device *vdev = video_devdata(file);
  2445. +
  2446. + strscpy(cap->driver, rkvdec->dev->driver->name,
  2447. + sizeof(cap->driver));
  2448. + strscpy(cap->card, vdev->name, sizeof(cap->card));
  2449. + snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
  2450. + rkvdec->dev->driver->name);
  2451. + return 0;
  2452. +}
  2453. +
  2454. +static int rkvdec_try_capture_fmt(struct file *file, void *priv,
  2455. + struct v4l2_format *f)
  2456. +{
  2457. + struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
  2458. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2459. + const struct rkvdec_coded_fmt_desc *coded_desc;
  2460. + u32 fourcc, width, height;
  2461. + unsigned int i;
  2462. +
  2463. + /*
  2464. + * The codec context should point to a coded format desc, if the format
  2465. + * on the coded end has not been set yet, it should point to the
  2466. + * default value.
  2467. + */
  2468. + coded_desc = ctx->coded_fmt_desc;
  2469. + if (WARN_ON(!coded_desc))
  2470. + return -EINVAL;
  2471. +
  2472. + fourcc = pix_mp->pixelformat;
  2473. + for (i = 0; i < coded_desc->num_decoded_fmts; i++) {
  2474. + if (coded_desc->decoded_fmts[i] == fourcc)
  2475. + break;
  2476. + }
  2477. +
  2478. + if (i == coded_desc->num_decoded_fmts)
  2479. + return -EINVAL;
  2480. +
  2481. + /* Save the original width/height before aligning them. */
  2482. + width = pix_mp->width;
  2483. + height = pix_mp->height;
  2484. +
  2485. + /* Always apply the frmsize constraint of the coded end. */
  2486. + v4l2_apply_frmsize_constraints(&pix_mp->width,
  2487. + &pix_mp->height,
  2488. + &coded_desc->frmsize);
  2489. +
  2490. + v4l2_fill_pixfmt_mp(pix_mp, fourcc, pix_mp->width, pix_mp->height);
  2491. + pix_mp->plane_fmt[0].sizeimage +=
  2492. + 128 * DIV_ROUND_UP(width, 16) * DIV_ROUND_UP(height, 16);
  2493. + /*
  2494. + * Now that we have computed sizeimage and bytesperline we can restore
  2495. + * the original width/height (before macro block alignment).
  2496. + */
  2497. + pix_mp->width = width;
  2498. + pix_mp->height = height;
  2499. + pix_mp->field = V4L2_FIELD_NONE;
  2500. +
  2501. + return 0;
  2502. +}
  2503. +
  2504. +static int rkvdec_try_output_fmt(struct file *file, void *priv,
  2505. + struct v4l2_format *f)
  2506. +{
  2507. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2508. + const struct rkvdec_coded_fmt_desc *desc;
  2509. + u32 fourcc;
  2510. + int ret;
  2511. +
  2512. + fourcc = f->fmt.pix_mp.pixelformat;
  2513. + desc = rkvdec_find_coded_fmt_desc(fourcc);
  2514. + if (!desc)
  2515. + return -EINVAL;
  2516. +
  2517. + v4l2_apply_frmsize_constraints(&f->fmt.pix_mp.width,
  2518. + &f->fmt.pix_mp.height,
  2519. + &desc->frmsize);
  2520. +
  2521. + f->fmt.pix_mp.field = V4L2_FIELD_NONE;
  2522. + /* All coded formats are considered single planar for now. */
  2523. + f->fmt.pix_mp.num_planes = 1;
  2524. +
  2525. + if (desc->ops->adjust_fmt) {
  2526. + ret = desc->ops->adjust_fmt(ctx, f);
  2527. + if (ret)
  2528. + return ret;
  2529. + }
  2530. +
  2531. + return 0;
  2532. +}
  2533. +
  2534. +static int rkvdec_s_fmt(struct file *file, void *priv,
  2535. + struct v4l2_format *f,
  2536. + int (*try_fmt)(struct file *, void *,
  2537. + struct v4l2_format *))
  2538. +{
  2539. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2540. + struct vb2_queue *vq;
  2541. + int ret;
  2542. +
  2543. + if (!try_fmt)
  2544. + return -EINVAL;
  2545. +
  2546. + vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
  2547. + if (vb2_is_busy(vq))
  2548. + return -EBUSY;
  2549. +
  2550. + ret = try_fmt(file, priv, f);
  2551. + if (ret)
  2552. + return ret;
  2553. +
  2554. + return 0;
  2555. +}
  2556. +
  2557. +static int rkvdec_s_capture_fmt(struct file *file, void *priv,
  2558. + struct v4l2_format *f)
  2559. +{
  2560. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2561. + int ret;
  2562. +
  2563. + ret = rkvdec_s_fmt(file, priv, f, rkvdec_try_capture_fmt);
  2564. + if (ret)
  2565. + return ret;
  2566. +
  2567. + ctx->decoded_fmt = *f;
  2568. + return 0;
  2569. +}
  2570. +
  2571. +static int rkvdec_s_output_fmt(struct file *file, void *priv,
  2572. + struct v4l2_format *f)
  2573. +{
  2574. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2575. + struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
  2576. + const struct rkvdec_coded_fmt_desc *desc;
  2577. + struct v4l2_format *cap_fmt;
  2578. + struct vb2_queue *peer_vq;
  2579. + unsigned int i;
  2580. + int ret;
  2581. +
  2582. + /*
  2583. + * Since format change on the OUTPUT queue will reset the CAPTURE
  2584. + * queue, we can't allow doing so when the CAPTURE queue has buffers
  2585. + * allocated.
  2586. + */
  2587. + peer_vq = v4l2_m2m_get_vq(m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
  2588. + if (vb2_is_busy(peer_vq))
  2589. + return -EBUSY;
  2590. +
  2591. + ret = rkvdec_s_fmt(file, priv, f, rkvdec_try_output_fmt);
  2592. + if (ret)
  2593. + return ret;
  2594. +
  2595. + desc = rkvdec_find_coded_fmt_desc(f->fmt.pix_mp.pixelformat);
  2596. + if (!desc)
  2597. + return -EINVAL;
  2598. +
  2599. + /*
  2600. + * Make sure the capture format is supported by the codec, and if not
  2601. + * pick the default one.
  2602. + */
  2603. + cap_fmt = &ctx->decoded_fmt;
  2604. + for (i = 0; i < desc->num_decoded_fmts; i++) {
  2605. + if (cap_fmt->fmt.pix_mp.pixelformat == desc->decoded_fmts[i])
  2606. + break;
  2607. + }
  2608. +
  2609. + if (i == desc->num_decoded_fmts)
  2610. + rkvdec_reset_decoded_fmt(ctx);
  2611. +
  2612. + ctx->coded_fmt_desc = desc;
  2613. + ctx->coded_fmt = *f;
  2614. +
  2615. + /* Propagate colorspace information to capture. */
  2616. + cap_fmt->fmt.pix_mp.colorspace = f->fmt.pix_mp.colorspace;
  2617. + cap_fmt->fmt.pix_mp.xfer_func = f->fmt.pix_mp.xfer_func;
  2618. + cap_fmt->fmt.pix_mp.ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
  2619. + cap_fmt->fmt.pix_mp.quantization = f->fmt.pix_mp.quantization;
  2620. +
  2621. + return 0;
  2622. +}
  2623. +
  2624. +static int rkvdec_g_output_fmt(struct file *file, void *priv,
  2625. + struct v4l2_format *f)
  2626. +{
  2627. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2628. +
  2629. + *f = ctx->coded_fmt;
  2630. + return 0;
  2631. +}
  2632. +
  2633. +static int rkvdec_g_capture_fmt(struct file *file, void *priv,
  2634. + struct v4l2_format *f)
  2635. +{
  2636. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2637. +
  2638. + *f = ctx->decoded_fmt;
  2639. + return 0;
  2640. +}
  2641. +
  2642. +static int rkvdec_enum_output_fmt(struct file *file, void *priv,
  2643. + struct v4l2_fmtdesc *f)
  2644. +{
  2645. + if (f->index >= ARRAY_SIZE(rkvdec_coded_fmts))
  2646. + return -EINVAL;
  2647. +
  2648. + f->pixelformat = rkvdec_coded_fmts[f->index].fourcc;
  2649. + return 0;
  2650. +}
  2651. +
  2652. +static int rkvdec_enum_capture_fmt(struct file *file, void *priv,
  2653. + struct v4l2_fmtdesc *f)
  2654. +{
  2655. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(priv);
  2656. +
  2657. + if (WARN_ON(!ctx->coded_fmt_desc))
  2658. + return -EINVAL;
  2659. +
  2660. + if (f->index >= ctx->coded_fmt_desc->num_decoded_fmts)
  2661. + return -EINVAL;
  2662. +
  2663. + f->pixelformat = ctx->coded_fmt_desc->decoded_fmts[f->index];
  2664. + return 0;
  2665. +}
  2666. +
  2667. +static const struct v4l2_ioctl_ops rkvdec_ioctl_ops = {
  2668. + .vidioc_querycap = rkvdec_querycap,
  2669. + .vidioc_enum_framesizes = rkvdec_enum_framesizes,
  2670. +
  2671. + .vidioc_try_fmt_vid_cap_mplane = rkvdec_try_capture_fmt,
  2672. + .vidioc_try_fmt_vid_out_mplane = rkvdec_try_output_fmt,
  2673. + .vidioc_s_fmt_vid_out_mplane = rkvdec_s_output_fmt,
  2674. + .vidioc_s_fmt_vid_cap_mplane = rkvdec_s_capture_fmt,
  2675. + .vidioc_g_fmt_vid_out_mplane = rkvdec_g_output_fmt,
  2676. + .vidioc_g_fmt_vid_cap_mplane = rkvdec_g_capture_fmt,
  2677. + .vidioc_enum_fmt_vid_out = rkvdec_enum_output_fmt,
  2678. + .vidioc_enum_fmt_vid_cap = rkvdec_enum_capture_fmt,
  2679. +
  2680. + .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
  2681. + .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
  2682. + .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
  2683. + .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
  2684. + .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
  2685. + .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
  2686. + .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
  2687. +
  2688. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2689. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2690. +
  2691. + .vidioc_streamon = v4l2_m2m_ioctl_streamon,
  2692. + .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
  2693. +};
  2694. +
  2695. +static int rkvdec_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
  2696. + unsigned int *num_planes, unsigned int sizes[],
  2697. + struct device *alloc_devs[])
  2698. +{
  2699. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
  2700. + struct v4l2_format *f;
  2701. + unsigned int i;
  2702. +
  2703. + if (V4L2_TYPE_IS_OUTPUT(vq->type))
  2704. + f = &ctx->coded_fmt;
  2705. + else
  2706. + f = &ctx->decoded_fmt;
  2707. +
  2708. + if (*num_planes) {
  2709. + if (*num_planes != f->fmt.pix_mp.num_planes)
  2710. + return -EINVAL;
  2711. +
  2712. + for (i = 0; i < f->fmt.pix_mp.num_planes; i++) {
  2713. + if (sizes[i] < f->fmt.pix_mp.plane_fmt[i].sizeimage)
  2714. + return -EINVAL;
  2715. + }
  2716. + } else {
  2717. + *num_planes = f->fmt.pix_mp.num_planes;
  2718. + for (i = 0; i < f->fmt.pix_mp.num_planes; i++)
  2719. + sizes[i] = f->fmt.pix_mp.plane_fmt[i].sizeimage;
  2720. + }
  2721. +
  2722. + return 0;
  2723. +}
  2724. +
  2725. +static int rkvdec_buf_prepare(struct vb2_buffer *vb)
  2726. +{
  2727. + struct vb2_queue *vq = vb->vb2_queue;
  2728. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
  2729. + struct v4l2_format *f;
  2730. + unsigned int i;
  2731. +
  2732. + if (V4L2_TYPE_IS_OUTPUT(vq->type))
  2733. + f = &ctx->coded_fmt;
  2734. + else
  2735. + f = &ctx->decoded_fmt;
  2736. +
  2737. + for (i = 0; i < f->fmt.pix_mp.num_planes; ++i) {
  2738. + u32 sizeimage = f->fmt.pix_mp.plane_fmt[i].sizeimage;
  2739. +
  2740. + if (vb2_plane_size(vb, i) < sizeimage)
  2741. + return -EINVAL;
  2742. + }
  2743. +
  2744. + return 0;
  2745. +}
  2746. +
  2747. +static void rkvdec_buf_queue(struct vb2_buffer *vb)
  2748. +{
  2749. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  2750. + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  2751. +
  2752. + v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
  2753. +}
  2754. +
  2755. +static int rkvdec_buf_out_validate(struct vb2_buffer *vb)
  2756. +{
  2757. + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  2758. +
  2759. + vbuf->field = V4L2_FIELD_NONE;
  2760. + return 0;
  2761. +}
  2762. +
  2763. +static void rkvdec_buf_request_complete(struct vb2_buffer *vb)
  2764. +{
  2765. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  2766. +
  2767. + v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_hdl);
  2768. +}
  2769. +
  2770. +static int rkvdec_start_streaming(struct vb2_queue *q, unsigned int count)
  2771. +{
  2772. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
  2773. + const struct rkvdec_coded_fmt_desc *desc;
  2774. + int ret;
  2775. +
  2776. + if (!V4L2_TYPE_IS_OUTPUT(q->type))
  2777. + return 0;
  2778. +
  2779. + desc = ctx->coded_fmt_desc;
  2780. + if (WARN_ON(!desc))
  2781. + return -EINVAL;
  2782. +
  2783. + if (desc->ops->start) {
  2784. + ret = desc->ops->start(ctx);
  2785. + if (ret)
  2786. + return ret;
  2787. + }
  2788. +
  2789. + return 0;
  2790. +}
  2791. +
  2792. +static void rkvdec_queue_cleanup(struct vb2_queue *vq, u32 state)
  2793. +{
  2794. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(vq);
  2795. +
  2796. + while (true) {
  2797. + struct vb2_v4l2_buffer *vbuf;
  2798. +
  2799. + if (V4L2_TYPE_IS_OUTPUT(vq->type))
  2800. + vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
  2801. + else
  2802. + vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
  2803. +
  2804. + if (!vbuf)
  2805. + break;
  2806. +
  2807. + v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
  2808. + &ctx->ctrl_hdl);
  2809. + v4l2_m2m_buf_done(vbuf, state);
  2810. + }
  2811. +}
  2812. +
  2813. +static void rkvdec_stop_streaming(struct vb2_queue *q)
  2814. +{
  2815. + struct rkvdec_ctx *ctx = vb2_get_drv_priv(q);
  2816. +
  2817. + if (V4L2_TYPE_IS_OUTPUT(q->type)) {
  2818. + const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
  2819. +
  2820. + if (WARN_ON(!desc))
  2821. + return;
  2822. +
  2823. + if (desc->ops->stop)
  2824. + desc->ops->stop(ctx);
  2825. + }
  2826. +
  2827. + rkvdec_queue_cleanup(q, VB2_BUF_STATE_ERROR);
  2828. +}
  2829. +
  2830. +const struct vb2_ops rkvdec_queue_ops = {
  2831. + .queue_setup = rkvdec_queue_setup,
  2832. + .buf_prepare = rkvdec_buf_prepare,
  2833. + .buf_queue = rkvdec_buf_queue,
  2834. + .buf_out_validate = rkvdec_buf_out_validate,
  2835. + .buf_request_complete = rkvdec_buf_request_complete,
  2836. + .start_streaming = rkvdec_start_streaming,
  2837. + .stop_streaming = rkvdec_stop_streaming,
  2838. + .wait_prepare = vb2_ops_wait_prepare,
  2839. + .wait_finish = vb2_ops_wait_finish,
  2840. +};
  2841. +
  2842. +static int rkvdec_request_validate(struct media_request *req)
  2843. +{
  2844. + struct media_request_object *obj;
  2845. + const struct rkvdec_ctrls *ctrls;
  2846. + struct v4l2_ctrl_handler *hdl;
  2847. + struct rkvdec_ctx *ctx = NULL;
  2848. + unsigned int count, i;
  2849. + int ret;
  2850. +
  2851. + list_for_each_entry(obj, &req->objects, list) {
  2852. + struct vb2_buffer *vb;
  2853. +
  2854. + if (vb2_request_object_is_buffer(obj)) {
  2855. + vb = container_of(obj, struct vb2_buffer, req_obj);
  2856. + ctx = vb2_get_drv_priv(vb->vb2_queue);
  2857. + break;
  2858. + }
  2859. + }
  2860. +
  2861. + if (!ctx)
  2862. + return -EINVAL;
  2863. +
  2864. + count = vb2_request_buffer_cnt(req);
  2865. + if (!count)
  2866. + return -ENOENT;
  2867. + else if (count > 1)
  2868. + return -EINVAL;
  2869. +
  2870. + hdl = v4l2_ctrl_request_hdl_find(req, &ctx->ctrl_hdl);
  2871. + if (!hdl)
  2872. + return -ENOENT;
  2873. +
  2874. + ret = 0;
  2875. + ctrls = ctx->coded_fmt_desc->ctrls;
  2876. + for (i = 0; ctrls && i < ctrls->num_ctrls; i++) {
  2877. + u32 id = ctrls->ctrls[i].cfg.id;
  2878. + struct v4l2_ctrl *ctrl;
  2879. +
  2880. + if (!ctrls->ctrls[i].per_request || !ctrls->ctrls[i].mandatory)
  2881. + continue;
  2882. +
  2883. + ctrl = v4l2_ctrl_request_hdl_ctrl_find(hdl, id);
  2884. + if (!ctrl) {
  2885. + ret = -ENOENT;
  2886. + break;
  2887. + }
  2888. + }
  2889. +
  2890. + v4l2_ctrl_request_hdl_put(hdl);
  2891. +
  2892. + if (ret)
  2893. + return ret;
  2894. +
  2895. + return vb2_request_validate(req);
  2896. +}
  2897. +
  2898. +static const struct media_device_ops rkvdec_media_ops = {
  2899. + .req_validate = rkvdec_request_validate,
  2900. + .req_queue = v4l2_m2m_request_queue,
  2901. +};
  2902. +
  2903. +static void rkvdec_job_finish_no_pm(struct rkvdec_ctx *ctx,
  2904. + enum vb2_buffer_state result)
  2905. +{
  2906. + struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
  2907. + struct vb2_v4l2_buffer *src_buf = v4l2_m2m_src_buf_remove(m2m_ctx);
  2908. + struct vb2_v4l2_buffer *dst_buf = v4l2_m2m_dst_buf_remove(m2m_ctx);
  2909. + const struct v4l2_format *f;
  2910. +
  2911. + if (WARN_ON(!src_buf || !dst_buf))
  2912. + return;
  2913. +
  2914. + f = &ctx->decoded_fmt;
  2915. + if (result != VB2_BUF_STATE_ERROR)
  2916. + dst_buf->planes[0].bytesused =
  2917. + f->fmt.pix_mp.plane_fmt[0].sizeimage;
  2918. + else
  2919. + dst_buf->planes[0].bytesused = 0;
  2920. +
  2921. + if (ctx->coded_fmt_desc->ops->done)
  2922. + ctx->coded_fmt_desc->ops->done(ctx, src_buf, dst_buf, result);
  2923. +
  2924. + /*
  2925. + * Returning the OUTPUT (src) buffer will wake-up any process
  2926. + * waiting on the request file descriptor.
  2927. + * Therefore, let's return the CAPTURE (dst) buffer first.
  2928. + * This makes userspace slightly simpler.
  2929. + */
  2930. + v4l2_m2m_buf_done(dst_buf, result);
  2931. + v4l2_m2m_buf_done(src_buf, result);
  2932. + v4l2_m2m_job_finish(ctx->dev->m2m_dev, m2m_ctx);
  2933. +}
  2934. +
  2935. +static void rkvdec_job_finish(struct rkvdec_ctx *ctx,
  2936. + enum vb2_buffer_state result)
  2937. +{
  2938. + struct rkvdec_dev *rkvdec = ctx->dev;
  2939. +
  2940. + pm_runtime_mark_last_busy(rkvdec->dev);
  2941. + dev_dbg(rkvdec->dev, "%s:%i PM put\n", __func__, __LINE__);
  2942. + pm_runtime_put_autosuspend(rkvdec->dev);
  2943. + rkvdec_job_finish_no_pm(ctx, result);
  2944. +}
  2945. +
  2946. +void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
  2947. +{
  2948. + struct media_request *src_req;
  2949. +
  2950. + memset(run, 0, sizeof(*run));
  2951. +
  2952. + run->bufs.src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
  2953. + run->bufs.dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
  2954. +
  2955. + /* Apply request(s) controls if needed. */
  2956. + src_req = run->bufs.src->vb2_buf.req_obj.req;
  2957. + if (src_req)
  2958. + v4l2_ctrl_request_setup(src_req, &ctx->ctrl_hdl);
  2959. +
  2960. + v4l2_m2m_buf_copy_metadata(run->bufs.src, run->bufs.dst, true);
  2961. +}
  2962. +
  2963. +void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run)
  2964. +{
  2965. + struct media_request *src_req = run->bufs.src->vb2_buf.req_obj.req;
  2966. +
  2967. + if (src_req)
  2968. + v4l2_ctrl_request_complete(src_req, &ctx->ctrl_hdl);
  2969. +}
  2970. +
  2971. +static void rkvdec_device_run(void *priv)
  2972. +{
  2973. + struct rkvdec_ctx *ctx = priv;
  2974. + struct rkvdec_dev *rkvdec = ctx->dev;
  2975. + const struct rkvdec_coded_fmt_desc *desc = ctx->coded_fmt_desc;
  2976. + int ret;
  2977. +
  2978. + if (WARN_ON(!desc))
  2979. + return;
  2980. +
  2981. + dev_dbg(rkvdec->dev, "%s:%i PM get\n", __func__, __LINE__);
  2982. + ret = pm_runtime_get_sync(rkvdec->dev);
  2983. + if (ret < 0) {
  2984. + rkvdec_job_finish_no_pm(ctx, VB2_BUF_STATE_ERROR);
  2985. + return;
  2986. + }
  2987. +
  2988. + ret = desc->ops->run(ctx);
  2989. + if (ret)
  2990. + rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
  2991. +}
  2992. +
  2993. +static struct v4l2_m2m_ops rkvdec_m2m_ops = {
  2994. + .device_run = rkvdec_device_run,
  2995. +};
  2996. +
  2997. +static int rkvdec_queue_init(void *priv,
  2998. + struct vb2_queue *src_vq,
  2999. + struct vb2_queue *dst_vq)
  3000. +{
  3001. + struct rkvdec_ctx *ctx = priv;
  3002. + struct rkvdec_dev *rkvdec = ctx->dev;
  3003. + int ret;
  3004. +
  3005. + src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
  3006. + src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  3007. + src_vq->drv_priv = ctx;
  3008. + src_vq->ops = &rkvdec_queue_ops;
  3009. + src_vq->mem_ops = &vb2_dma_contig_memops;
  3010. +
  3011. + /*
  3012. + * Driver does mostly sequential access, so sacrifice TLB efficiency
  3013. + * for faster allocation. Also, no CPU access on the source queue,
  3014. + * so no kernel mapping needed.
  3015. + */
  3016. + src_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
  3017. + DMA_ATTR_NO_KERNEL_MAPPING;
  3018. + src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  3019. + src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  3020. + src_vq->lock = &rkvdec->vdev_lock;
  3021. + src_vq->dev = rkvdec->v4l2_dev.dev;
  3022. + src_vq->supports_requests = true;
  3023. + src_vq->requires_requests = true;
  3024. +
  3025. + ret = vb2_queue_init(src_vq);
  3026. + if (ret)
  3027. + return ret;
  3028. +
  3029. + dst_vq->bidirectional = true;
  3030. + dst_vq->mem_ops = &vb2_dma_contig_memops;
  3031. + dst_vq->dma_attrs = DMA_ATTR_ALLOC_SINGLE_PAGES |
  3032. + DMA_ATTR_NO_KERNEL_MAPPING;
  3033. + dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
  3034. + dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
  3035. + dst_vq->drv_priv = ctx;
  3036. + dst_vq->ops = &rkvdec_queue_ops;
  3037. + dst_vq->buf_struct_size = sizeof(struct rkvdec_decoded_buffer);
  3038. + dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
  3039. + dst_vq->lock = &rkvdec->vdev_lock;
  3040. + dst_vq->dev = rkvdec->v4l2_dev.dev;
  3041. +
  3042. + return vb2_queue_init(dst_vq);
  3043. +}
  3044. +
  3045. +static int rkvdec_add_ctrls(struct rkvdec_ctx *ctx,
  3046. + const struct rkvdec_ctrls *ctrls)
  3047. +{
  3048. + unsigned int i;
  3049. +
  3050. + for (i = 0; i < ctrls->num_ctrls; i++) {
  3051. + const struct v4l2_ctrl_config *cfg = &ctrls->ctrls[i].cfg;
  3052. +
  3053. + v4l2_ctrl_new_custom(&ctx->ctrl_hdl, cfg, ctx);
  3054. + if (ctx->ctrl_hdl.error)
  3055. + return ctx->ctrl_hdl.error;
  3056. + }
  3057. +
  3058. + return 0;
  3059. +}
  3060. +
  3061. +static int rkvdec_init_ctrls(struct rkvdec_ctx *ctx)
  3062. +{
  3063. + unsigned int i, nctrls = 0;
  3064. + int ret;
  3065. +
  3066. + for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++)
  3067. + nctrls += rkvdec_coded_fmts[i].ctrls->num_ctrls;
  3068. +
  3069. + v4l2_ctrl_handler_init(&ctx->ctrl_hdl, nctrls);
  3070. +
  3071. + for (i = 0; i < ARRAY_SIZE(rkvdec_coded_fmts); i++) {
  3072. + ret = rkvdec_add_ctrls(ctx, rkvdec_coded_fmts[i].ctrls);
  3073. + if (ret)
  3074. + goto err_free_handler;
  3075. + }
  3076. +
  3077. + ret = v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
  3078. + if (ret)
  3079. + goto err_free_handler;
  3080. +
  3081. + ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
  3082. + return 0;
  3083. +
  3084. +err_free_handler:
  3085. + v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  3086. + return ret;
  3087. +}
  3088. +
  3089. +static int rkvdec_open(struct file *filp)
  3090. +{
  3091. + struct rkvdec_dev *rkvdec = video_drvdata(filp);
  3092. + struct rkvdec_ctx *ctx;
  3093. + int ret;
  3094. +
  3095. + ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  3096. + if (!ctx)
  3097. + return -ENOMEM;
  3098. +
  3099. + ctx->dev = rkvdec;
  3100. + rkvdec_reset_coded_fmt(ctx);
  3101. + rkvdec_reset_decoded_fmt(ctx);
  3102. + v4l2_fh_init(&ctx->fh, video_devdata(filp));
  3103. +
  3104. + ret = rkvdec_init_ctrls(ctx);
  3105. + if (ret)
  3106. + goto err_free_ctx;
  3107. +
  3108. + ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(rkvdec->m2m_dev, ctx,
  3109. + rkvdec_queue_init);
  3110. + if (IS_ERR(ctx->fh.m2m_ctx)) {
  3111. + ret = PTR_ERR(ctx->fh.m2m_ctx);
  3112. + goto err_cleanup_ctrls;
  3113. + }
  3114. +
  3115. + filp->private_data = &ctx->fh;
  3116. + v4l2_fh_add(&ctx->fh);
  3117. +
  3118. + return 0;
  3119. +
  3120. +err_cleanup_ctrls:
  3121. + v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  3122. +
  3123. +err_free_ctx:
  3124. + kfree(ctx);
  3125. + return ret;
  3126. +}
  3127. +
  3128. +static int rkvdec_release(struct file *filp)
  3129. +{
  3130. + struct rkvdec_ctx *ctx = fh_to_rkvdec_ctx(filp->private_data);
  3131. +
  3132. + v4l2_fh_del(&ctx->fh);
  3133. + v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
  3134. + v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  3135. + v4l2_fh_exit(&ctx->fh);
  3136. + kfree(ctx);
  3137. +
  3138. + return 0;
  3139. +}
  3140. +
  3141. +static const struct v4l2_file_operations rkvdec_fops = {
  3142. + .owner = THIS_MODULE,
  3143. + .open = rkvdec_open,
  3144. + .release = rkvdec_release,
  3145. + .poll = v4l2_m2m_fop_poll,
  3146. + .unlocked_ioctl = video_ioctl2,
  3147. + .mmap = v4l2_m2m_fop_mmap,
  3148. +};
  3149. +
  3150. +static int rkvdec_v4l2_init(struct rkvdec_dev *rkvdec)
  3151. +{
  3152. + int ret;
  3153. +
  3154. + ret = v4l2_device_register(rkvdec->dev, &rkvdec->v4l2_dev);
  3155. + if (ret) {
  3156. + dev_err(rkvdec->dev, "Failed to register V4L2 device\n");
  3157. + return ret;
  3158. + }
  3159. +
  3160. + rkvdec->m2m_dev = v4l2_m2m_init(&rkvdec_m2m_ops);
  3161. + if (IS_ERR(rkvdec->m2m_dev)) {
  3162. + v4l2_err(&rkvdec->v4l2_dev, "Failed to init mem2mem device\n");
  3163. + ret = PTR_ERR(rkvdec->m2m_dev);
  3164. + goto err_unregister_v4l2;
  3165. + }
  3166. +
  3167. + rkvdec->mdev.dev = rkvdec->dev;
  3168. + strscpy(rkvdec->mdev.model, "rkvdec", sizeof(rkvdec->mdev.model));
  3169. + strscpy(rkvdec->mdev.bus_info, "platform:rkvdec",
  3170. + sizeof(rkvdec->mdev.bus_info));
  3171. + media_device_init(&rkvdec->mdev);
  3172. + rkvdec->mdev.ops = &rkvdec_media_ops;
  3173. + rkvdec->v4l2_dev.mdev = &rkvdec->mdev;
  3174. +
  3175. + rkvdec->vdev.lock = &rkvdec->vdev_lock;
  3176. + rkvdec->vdev.v4l2_dev = &rkvdec->v4l2_dev;
  3177. + rkvdec->vdev.fops = &rkvdec_fops;
  3178. + rkvdec->vdev.release = video_device_release_empty;
  3179. + rkvdec->vdev.vfl_dir = VFL_DIR_M2M;
  3180. + rkvdec->vdev.device_caps = V4L2_CAP_STREAMING |
  3181. + V4L2_CAP_VIDEO_M2M_MPLANE;
  3182. + rkvdec->vdev.ioctl_ops = &rkvdec_ioctl_ops;
  3183. + video_set_drvdata(&rkvdec->vdev, rkvdec);
  3184. + strscpy(rkvdec->vdev.name, "rkvdec", sizeof(rkvdec->vdev.name));
  3185. +
  3186. + ret = video_register_device(&rkvdec->vdev, VFL_TYPE_GRABBER, -1);
  3187. + if (ret) {
  3188. + v4l2_err(&rkvdec->v4l2_dev, "Failed to register video device\n");
  3189. + goto err_cleanup_mc;
  3190. + }
  3191. +
  3192. + ret = v4l2_m2m_register_media_controller(rkvdec->m2m_dev, &rkvdec->vdev,
  3193. + MEDIA_ENT_F_PROC_VIDEO_DECODER);
  3194. + if (ret) {
  3195. + v4l2_err(&rkvdec->v4l2_dev,
  3196. + "Failed to initialize V4L2 M2M media controller\n");
  3197. + goto err_unregister_vdev;
  3198. + }
  3199. +
  3200. + ret = media_device_register(&rkvdec->mdev);
  3201. + if (ret) {
  3202. + v4l2_err(&rkvdec->v4l2_dev, "Failed to register media device\n");
  3203. + goto err_unregister_mc;
  3204. + }
  3205. +
  3206. + return 0;
  3207. +
  3208. +err_unregister_mc:
  3209. + v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
  3210. +
  3211. +err_unregister_vdev:
  3212. + video_unregister_device(&rkvdec->vdev);
  3213. +
  3214. +err_cleanup_mc:
  3215. + media_device_cleanup(&rkvdec->mdev);
  3216. + v4l2_m2m_release(rkvdec->m2m_dev);
  3217. +
  3218. +err_unregister_v4l2:
  3219. + v4l2_device_unregister(&rkvdec->v4l2_dev);
  3220. + return ret;
  3221. +}
  3222. +
  3223. +static void rkvdec_v4l2_cleanup(struct rkvdec_dev *rkvdec)
  3224. +{
  3225. + media_device_unregister(&rkvdec->mdev);
  3226. + v4l2_m2m_unregister_media_controller(rkvdec->m2m_dev);
  3227. + video_unregister_device(&rkvdec->vdev);
  3228. + media_device_cleanup(&rkvdec->mdev);
  3229. + v4l2_m2m_release(rkvdec->m2m_dev);
  3230. + v4l2_device_unregister(&rkvdec->v4l2_dev);
  3231. +}
  3232. +
  3233. +static irqreturn_t rkvdec_irq_handler(int irq, void *priv)
  3234. +{
  3235. + struct rkvdec_dev *rkvdec = priv;
  3236. + u32 status = readl(rkvdec->regs + RKVDEC_REG_INTERRUPT);
  3237. +
  3238. + dev_dbg(rkvdec->dev, "dec status %x\n", status);
  3239. + writel(0, rkvdec->regs + RKVDEC_REG_INTERRUPT);
  3240. +
  3241. + if (cancel_delayed_work(&rkvdec->watchdog_work)) {
  3242. + struct rkvdec_ctx *ctx;
  3243. +
  3244. + ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
  3245. + rkvdec_job_finish(ctx, VB2_BUF_STATE_DONE);
  3246. + }
  3247. +
  3248. + return IRQ_HANDLED;
  3249. +}
  3250. +
  3251. +static void rkvdec_watchdog_func(struct work_struct *work)
  3252. +{
  3253. + struct rkvdec_dev *rkvdec;
  3254. + struct rkvdec_ctx *ctx;
  3255. +
  3256. + rkvdec = container_of(to_delayed_work(work), struct rkvdec_dev,
  3257. + watchdog_work);
  3258. + ctx = v4l2_m2m_get_curr_priv(rkvdec->m2m_dev);
  3259. + if (ctx) {
  3260. + dev_err(rkvdec->dev, "Frame processing timed out!\n");
  3261. + writel(RKVDEC_IRQ_DIS, rkvdec->regs + RKVDEC_REG_INTERRUPT);
  3262. + writel(0, rkvdec->regs + RKVDEC_REG_SYSCTRL);
  3263. + rkvdec_job_finish(ctx, VB2_BUF_STATE_ERROR);
  3264. + }
  3265. +}
  3266. +
  3267. +static const struct of_device_id of_rkvdec_match[] = {
  3268. + { .compatible = "rockchip,rk3399-vdec" },
  3269. + { /* sentinel */ }
  3270. +};
  3271. +MODULE_DEVICE_TABLE(of, of_rkvdec_match);
  3272. +
  3273. +static const char * const rkvdec_clk_names[] = {
  3274. + "axi", "ahb", "cabac", "core"
  3275. +};
  3276. +
  3277. +static int rkvdec_probe(struct platform_device *pdev)
  3278. +{
  3279. + struct rkvdec_dev *rkvdec;
  3280. + struct resource *res;
  3281. + unsigned int i;
  3282. + int ret, irq;
  3283. +
  3284. + rkvdec = devm_kzalloc(&pdev->dev, sizeof(*rkvdec), GFP_KERNEL);
  3285. + if (!rkvdec)
  3286. + return -ENOMEM;
  3287. +
  3288. + platform_set_drvdata(pdev, rkvdec);
  3289. + rkvdec->dev = &pdev->dev;
  3290. + mutex_init(&rkvdec->vdev_lock);
  3291. + INIT_DELAYED_WORK(&rkvdec->watchdog_work, rkvdec_watchdog_func);
  3292. +
  3293. + rkvdec->clocks = devm_kcalloc(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
  3294. + sizeof(*rkvdec->clocks), GFP_KERNEL);
  3295. + if (!rkvdec->clocks)
  3296. + return -ENOMEM;
  3297. +
  3298. + for (i = 0; i < ARRAY_SIZE(rkvdec_clk_names); i++)
  3299. + rkvdec->clocks[i].id = rkvdec_clk_names[i];
  3300. +
  3301. + ret = devm_clk_bulk_get(&pdev->dev, ARRAY_SIZE(rkvdec_clk_names),
  3302. + rkvdec->clocks);
  3303. + if (ret)
  3304. + return ret;
  3305. +
  3306. + /*
  3307. + * Bump ACLK to max. possible freq. (500 MHz) to improve performance
  3308. + * When 4k video playback.
  3309. + */
  3310. + clk_set_rate(rkvdec->clocks[0].clk, 500 * 1000 * 1000);
  3311. +
  3312. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  3313. + rkvdec->regs = devm_ioremap_resource(&pdev->dev, res);
  3314. + if (IS_ERR(rkvdec->regs))
  3315. + return PTR_ERR(rkvdec->regs);
  3316. +
  3317. + ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  3318. + if (ret) {
  3319. + dev_err(&pdev->dev, "Could not set DMA coherent mask.\n");
  3320. + return ret;
  3321. + }
  3322. +
  3323. + vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
  3324. +
  3325. + irq = platform_get_irq(pdev, 0);
  3326. + if (irq <= 0) {
  3327. + dev_err(&pdev->dev, "Could not get vdec IRQ\n");
  3328. + return -ENXIO;
  3329. + }
  3330. +
  3331. + ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  3332. + rkvdec_irq_handler, IRQF_ONESHOT,
  3333. + dev_name(&pdev->dev), rkvdec);
  3334. + if (ret) {
  3335. + dev_err(&pdev->dev, "Could not request vdec IRQ\n");
  3336. + return ret;
  3337. + }
  3338. +
  3339. + pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
  3340. + pm_runtime_use_autosuspend(&pdev->dev);
  3341. + pm_runtime_enable(&pdev->dev);
  3342. +
  3343. + ret = rkvdec_v4l2_init(rkvdec);
  3344. + if (ret)
  3345. + goto err_disable_runtime_pm;
  3346. +
  3347. + return 0;
  3348. +
  3349. +err_disable_runtime_pm:
  3350. + pm_runtime_dont_use_autosuspend(&pdev->dev);
  3351. + pm_runtime_disable(&pdev->dev);
  3352. + return ret;
  3353. +}
  3354. +
  3355. +static int rkvdec_remove(struct platform_device *pdev)
  3356. +{
  3357. + struct rkvdec_dev *rkvdec = platform_get_drvdata(pdev);
  3358. +
  3359. + rkvdec_v4l2_cleanup(rkvdec);
  3360. + pm_runtime_disable(&pdev->dev);
  3361. + pm_runtime_dont_use_autosuspend(&pdev->dev);
  3362. + return 0;
  3363. +}
  3364. +
  3365. +#ifdef CONFIG_PM
  3366. +static int rkvdec_runtime_resume(struct device *dev)
  3367. +{
  3368. + struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
  3369. +
  3370. + return clk_bulk_prepare_enable(ARRAY_SIZE(rkvdec_clk_names),
  3371. + rkvdec->clocks);
  3372. +}
  3373. +
  3374. +static int rkvdec_runtime_suspend(struct device *dev)
  3375. +{
  3376. + struct rkvdec_dev *rkvdec = dev_get_drvdata(dev);
  3377. +
  3378. + clk_bulk_disable_unprepare(ARRAY_SIZE(rkvdec_clk_names),
  3379. + rkvdec->clocks);
  3380. + return 0;
  3381. +}
  3382. +#endif
  3383. +
  3384. +static const struct dev_pm_ops rkvdec_pm_ops = {
  3385. + SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  3386. + pm_runtime_force_resume)
  3387. + SET_RUNTIME_PM_OPS(rkvdec_runtime_suspend, rkvdec_runtime_resume, NULL)
  3388. +};
  3389. +
  3390. +static struct platform_driver rkvdec_driver = {
  3391. + .probe = rkvdec_probe,
  3392. + .remove = rkvdec_remove,
  3393. + .driver = {
  3394. + .name = "rkvdec",
  3395. + .of_match_table = of_match_ptr(of_rkvdec_match),
  3396. + .pm = &rkvdec_pm_ops,
  3397. + },
  3398. +};
  3399. +module_platform_driver(rkvdec_driver);
  3400. +
  3401. +MODULE_AUTHOR("Boris Brezillon <[email protected]>");
  3402. +MODULE_DESCRIPTION("Rockchip Video Decoder driver");
  3403. +MODULE_LICENSE("GPL v2");
  3404. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec.h linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec.h
  3405. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec.h 1969-12-31 16:00:00.000000000 -0800
  3406. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec.h 2020-02-14 21:55:21.389863881 -0800
  3407. @@ -0,0 +1,124 @@
  3408. +/* SPDX-License-Identifier: GPL-2.0 */
  3409. +/*
  3410. + * Hantro VPU codec driver
  3411. + *
  3412. + * Copyright 2018 Google LLC.
  3413. + * Tomasz Figa <[email protected]>
  3414. + *
  3415. + * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
  3416. + * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  3417. + */
  3418. +
  3419. +#ifndef RKVDEC_H_
  3420. +#define RKVDEC_H_
  3421. +
  3422. +#include <linux/platform_device.h>
  3423. +#include <linux/videodev2.h>
  3424. +#include <linux/wait.h>
  3425. +#include <linux/clk.h>
  3426. +
  3427. +#include <media/v4l2-ctrls.h>
  3428. +#include <media/v4l2-device.h>
  3429. +#include <media/v4l2-ioctl.h>
  3430. +#include <media/videobuf2-core.h>
  3431. +#include <media/videobuf2-dma-contig.h>
  3432. +
  3433. +struct rkvdec_ctx;
  3434. +
  3435. +struct rkvdec_ctrl_desc {
  3436. + u32 per_request : 1;
  3437. + u32 mandatory : 1;
  3438. + struct v4l2_ctrl_config cfg;
  3439. +};
  3440. +
  3441. +struct rkvdec_ctrls {
  3442. + const struct rkvdec_ctrl_desc *ctrls;
  3443. + unsigned int num_ctrls;
  3444. +};
  3445. +
  3446. +struct rkvdec_run {
  3447. + struct {
  3448. + struct vb2_v4l2_buffer *src;
  3449. + struct vb2_v4l2_buffer *dst;
  3450. + } bufs;
  3451. +};
  3452. +
  3453. +struct rkvdec_vp9_decoded_buffer_info {
  3454. + /* Info needed when the decoded frame serves as a reference frame. */
  3455. + u16 width;
  3456. + u16 height;
  3457. + u32 bit_depth : 4;
  3458. +};
  3459. +
  3460. +struct rkvdec_decoded_buffer {
  3461. + /* Must be the first field in this struct. */
  3462. + struct v4l2_m2m_buffer base;
  3463. +};
  3464. +
  3465. +static inline struct rkvdec_decoded_buffer *
  3466. +vb2_to_rkvdec_decoded_buf(struct vb2_buffer *buf)
  3467. +{
  3468. + return container_of(buf, struct rkvdec_decoded_buffer,
  3469. + base.vb.vb2_buf);
  3470. +}
  3471. +
  3472. +struct rkvdec_ctx;
  3473. +
  3474. +struct rkvdec_coded_fmt_ops {
  3475. + int (*adjust_fmt)(struct rkvdec_ctx *ctx,
  3476. + struct v4l2_format *f);
  3477. + int (*start)(struct rkvdec_ctx *ctx);
  3478. + void (*stop)(struct rkvdec_ctx *ctx);
  3479. + int (*run)(struct rkvdec_ctx *ctx);
  3480. + void (*done)(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *src_buf,
  3481. + struct vb2_v4l2_buffer *dst_buf,
  3482. + enum vb2_buffer_state result);
  3483. +};
  3484. +
  3485. +struct rkvdec_coded_fmt_desc {
  3486. + u32 fourcc;
  3487. + struct v4l2_frmsize_stepwise frmsize;
  3488. + const struct rkvdec_ctrls *ctrls;
  3489. + const struct rkvdec_coded_fmt_ops *ops;
  3490. + unsigned int num_decoded_fmts;
  3491. + const u32 *decoded_fmts;
  3492. +};
  3493. +
  3494. +struct rkvdec_dev {
  3495. + struct v4l2_device v4l2_dev;
  3496. + struct media_device mdev;
  3497. + struct video_device vdev;
  3498. + struct v4l2_m2m_dev *m2m_dev;
  3499. + struct device *dev;
  3500. + struct clk_bulk_data *clocks;
  3501. + void __iomem *regs;
  3502. + struct mutex vdev_lock; /* serializes ioctls */
  3503. + struct delayed_work watchdog_work;
  3504. +};
  3505. +
  3506. +struct rkvdec_ctx {
  3507. + struct v4l2_fh fh;
  3508. + struct v4l2_format coded_fmt;
  3509. + struct v4l2_format decoded_fmt;
  3510. + const struct rkvdec_coded_fmt_desc *coded_fmt_desc;
  3511. + struct v4l2_ctrl_handler ctrl_hdl;
  3512. + struct rkvdec_dev *dev;
  3513. + void *priv;
  3514. +};
  3515. +
  3516. +static inline struct rkvdec_ctx *fh_to_rkvdec_ctx(struct v4l2_fh *fh)
  3517. +{
  3518. + return container_of(fh, struct rkvdec_ctx, fh);
  3519. +}
  3520. +
  3521. +struct rkvdec_aux_buf {
  3522. + void *cpu;
  3523. + dma_addr_t dma;
  3524. + size_t size;
  3525. +};
  3526. +
  3527. +void rkvdec_run_preamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run);
  3528. +void rkvdec_run_postamble(struct rkvdec_ctx *ctx, struct rkvdec_run *run);
  3529. +
  3530. +extern const struct rkvdec_coded_fmt_ops rkvdec_h264_fmt_ops;
  3531. +#endif /* RKVDEC_H_ */
  3532. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec-h264.c linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec-h264.c
  3533. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec-h264.c 1969-12-31 16:00:00.000000000 -0800
  3534. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec-h264.c 2020-02-14 21:55:21.389863881 -0800
  3535. @@ -0,0 +1,1154 @@
  3536. +// SPDX-License-Identifier: GPL-2.0
  3537. +/*
  3538. + * Rockchip Video Decoder H264 backend
  3539. + *
  3540. + * Copyright (C) 2019 Collabora, Ltd.
  3541. + * Boris Brezillon <[email protected]>
  3542. + *
  3543. + * Copyright (C) 2016 Rockchip Electronics Co., Ltd.
  3544. + * Jeffy Chen <[email protected]>
  3545. + */
  3546. +
  3547. +#include <media/v4l2-h264.h>
  3548. +#include <media/v4l2-mem2mem.h>
  3549. +
  3550. +#include "rkvdec.h"
  3551. +#include "rkvdec-regs.h"
  3552. +
  3553. +/* Size with u32 units. */
  3554. +#define RKV_CABAC_INIT_BUFFER_SIZE (3680 + 128)
  3555. +#define RKV_RPS_SIZE ((128 + 128) / 4)
  3556. +#define RKV_SCALING_LIST_SIZE (6 * 16 + 6 * 64 + 128)
  3557. +#define RKV_ERROR_INFO_SIZE (256 * 144 * 4)
  3558. +
  3559. +struct rkvdec_sps_pps_packet {
  3560. + u32 info[8];
  3561. +};
  3562. +
  3563. +struct rkvdec_ps_field {
  3564. + u16 offset;
  3565. + u8 len;
  3566. +};
  3567. +
  3568. +#define PS_FIELD(_offset, _len) \
  3569. + ((struct rkvdec_ps_field){ _offset, _len })
  3570. +
  3571. +#define SEQ_PARAMETER_SET_ID PS_FIELD(0, 4)
  3572. +#define PROFILE_IDC PS_FIELD(4, 8)
  3573. +#define CONSTRAINT_SET3_FLAG PS_FIELD(12, 1)
  3574. +#define CHROMA_FORMAT_IDC PS_FIELD(13, 2)
  3575. +#define BIT_DEPTH_LUMA PS_FIELD(15, 3)
  3576. +#define BIT_DEPTH_CHROMA PS_FIELD(18, 3)
  3577. +#define QPPRIME_Y_ZERO_TRANSFORM_BYPASS_FLAG PS_FIELD(21, 1)
  3578. +#define LOG2_MAX_FRAME_NUM_MINUS4 PS_FIELD(22, 4)
  3579. +#define MAX_NUM_REF_FRAMES PS_FIELD(26, 5)
  3580. +#define PIC_ORDER_CNT_TYPE PS_FIELD(31, 2)
  3581. +#define LOG2_MAX_PIC_ORDER_CNT_LSB_MINUS4 PS_FIELD(33, 4)
  3582. +#define DELTA_PIC_ORDER_ALWAYS_ZERO_FLAG PS_FIELD(37, 1)
  3583. +#define PIC_WIDTH_IN_MBS PS_FIELD(38, 9)
  3584. +#define PIC_HEIGHT_IN_MBS PS_FIELD(47, 9)
  3585. +#define FRAME_MBS_ONLY_FLAG PS_FIELD(56, 1)
  3586. +#define MB_ADAPTIVE_FRAME_FIELD_FLAG PS_FIELD(57, 1)
  3587. +#define DIRECT_8X8_INFERENCE_FLAG PS_FIELD(58, 1)
  3588. +#define MVC_EXTENSION_ENABLE PS_FIELD(59, 1)
  3589. +#define NUM_VIEWS PS_FIELD(60, 2)
  3590. +#define VIEW_ID(i) PS_FIELD(62 + ((i) * 10), 10)
  3591. +#define NUM_ANCHOR_REFS_L(i) PS_FIELD(82 + ((i) * 11), 1)
  3592. +#define ANCHOR_REF_L(i) PS_FIELD(83 + ((i) * 11), 10)
  3593. +#define NUM_NON_ANCHOR_REFS_L(i) PS_FIELD(104 + ((i) * 11), 1)
  3594. +#define NON_ANCHOR_REFS_L(i) PS_FIELD(105 + ((i) * 11), 10)
  3595. +#define PIC_PARAMETER_SET_ID PS_FIELD(128, 8)
  3596. +#define PPS_SEQ_PARAMETER_SET_ID PS_FIELD(136, 5)
  3597. +#define ENTROPY_CODING_MODE_FLAG PS_FIELD(141, 1)
  3598. +#define BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT_FLAG PS_FIELD(142, 1)
  3599. +#define NUM_REF_IDX_L_DEFAULT_ACTIVE_MINUS1(i) PS_FIELD(143 + ((i) * 5), 5)
  3600. +#define WEIGHTED_PRED_FLAG PS_FIELD(153, 1)
  3601. +#define WEIGHTED_BIPRED_IDC PS_FIELD(154, 2)
  3602. +#define PIC_INIT_QP_MINUS26 PS_FIELD(156, 7)
  3603. +#define PIC_INIT_QS_MINUS26 PS_FIELD(163, 6)
  3604. +#define CHROMA_QP_INDEX_OFFSET PS_FIELD(169, 5)
  3605. +#define DEBLOCKING_FILTER_CONTROL_PRESENT_FLAG PS_FIELD(174, 1)
  3606. +#define CONSTRAINED_INTRA_PRED_FLAG PS_FIELD(175, 1)
  3607. +#define REDUNDANT_PIC_CNT_PRESENT PS_FIELD(176, 1)
  3608. +#define TRANSFORM_8X8_MODE_FLAG PS_FIELD(177, 1)
  3609. +#define SECOND_CHROMA_QP_INDEX_OFFSET PS_FIELD(178, 5)
  3610. +#define SCALING_LIST_ENABLE_FLAG PS_FIELD(183, 1)
  3611. +#define SCALING_LIST_ADDRESS PS_FIELD(184, 32)
  3612. +#define IS_LONG_TERM(i) PS_FIELD(216 + (i), 1)
  3613. +
  3614. +#define DPB_OFFS(i, j) (288 + ((j) * 32 * 7) + ((i) * 7))
  3615. +#define DPB_INFO(i, j) PS_FIELD(DPB_OFFS(i, j), 5)
  3616. +#define BOTTOM_FLAG(i, j) PS_FIELD(DPB_OFFS(i, j) + 5, 1)
  3617. +#define VIEW_INDEX_OFF(i, j) PS_FIELD(DPB_OFFS(i, j) + 6, 1)
  3618. +
  3619. +/* Data structure describing auxiliary buffer format. */
  3620. +struct rkvdec_h264_priv_tbl {
  3621. + s8 cabac_table[4][464][2];
  3622. + u8 scaling_list[RKV_SCALING_LIST_SIZE];
  3623. + u32 rps[RKV_RPS_SIZE];
  3624. + struct rkvdec_sps_pps_packet param_set[256];
  3625. + u8 err_info[RKV_ERROR_INFO_SIZE];
  3626. +};
  3627. +
  3628. +#define RKVDEC_H264_DPB_SIZE 16
  3629. +
  3630. +struct rkvdec_h264_reflists {
  3631. + u8 p[RKVDEC_H264_DPB_SIZE];
  3632. + u8 b0[RKVDEC_H264_DPB_SIZE];
  3633. + u8 b1[RKVDEC_H264_DPB_SIZE];
  3634. + u8 num_valid;
  3635. +};
  3636. +
  3637. +struct rkvdec_h264_run {
  3638. + struct rkvdec_run base;
  3639. + const struct v4l2_ctrl_h264_decode_params *decode_params;
  3640. + const struct v4l2_ctrl_h264_slice_params *slices_params;
  3641. + const struct v4l2_ctrl_h264_sps *sps;
  3642. + const struct v4l2_ctrl_h264_pps *pps;
  3643. + const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
  3644. +};
  3645. +
  3646. +struct rkvdec_h264_ctx {
  3647. + struct rkvdec_aux_buf priv_tbl;
  3648. + struct rkvdec_h264_reflists reflists;
  3649. +};
  3650. +
  3651. +#define M_N(ctxidx, idc0_m, idc0_n, idc1_m, idc1_n, \
  3652. + idc2_m, idc2_n, intra_m, intra_n) \
  3653. + [0][(ctxidx)] = {idc0_m, idc0_n}, \
  3654. + [1][(ctxidx)] = {idc1_m, idc1_n}, \
  3655. + [2][(ctxidx)] = {idc2_m, idc2_n}, \
  3656. + [3][(ctxidx)] = {intra_m, intra_n}
  3657. +
  3658. +/*
  3659. + * Constant CABAC table.
  3660. + * Built from the tables described in section '9.3.1.1 Initialisation process
  3661. + * for context variables' of the H264 spec.
  3662. + */
  3663. +static const s8 rkvdec_h264_cabac_table[4][464][2] = {
  3664. + /* Table 9-12 – Values of variables m and n for ctxIdx from 0 to 10 */
  3665. + M_N(0, 20, -15, 20, -15, 20, -15, 20, -15),
  3666. + M_N(1, 2, 54, 2, 54, 2, 54, 2, 54),
  3667. + M_N(2, 3, 74, 3, 74, 3, 74, 3, 74),
  3668. + M_N(3, 20, -15, 20, -15, 20, -15, 20, -15),
  3669. + M_N(4, 2, 54, 2, 54, 2, 54, 2, 54),
  3670. + M_N(5, 3, 74, 3, 74, 3, 74, 3, 74),
  3671. + M_N(6, -28, 127, -28, 127, -28, 127, -28, 127),
  3672. + M_N(7, -23, 104, -23, 104, -23, 104, -23, 104),
  3673. + M_N(8, -6, 53, -6, 53, -6, 53, -6, 53),
  3674. + M_N(9, -1, 54, -1, 54, -1, 54, -1, 54),
  3675. + M_N(10, 7, 51, 7, 51, 7, 51, 7, 51),
  3676. +
  3677. + /* Table 9-13 – Values of variables m and n for ctxIdx from 11 to 23 */
  3678. + M_N(11, 23, 33, 22, 25, 29, 16, 0, 0),
  3679. + M_N(12, 23, 2, 34, 0, 25, 0, 0, 0),
  3680. + M_N(13, 21, 0, 16, 0, 14, 0, 0, 0),
  3681. + M_N(14, 1, 9, -2, 9, -10, 51, 0, 0),
  3682. + M_N(15, 0, 49, 4, 41, -3, 62, 0, 0),
  3683. + M_N(16, -37, 118, -29, 118, -27, 99, 0, 0),
  3684. + M_N(17, 5, 57, 2, 65, 26, 16, 0, 0),
  3685. + M_N(18, -13, 78, -6, 71, -4, 85, 0, 0),
  3686. + M_N(19, -11, 65, -13, 79, -24, 102, 0, 0),
  3687. + M_N(20, 1, 62, 5, 52, 5, 57, 0, 0),
  3688. + M_N(21, 12, 49, 9, 50, 6, 57, 0, 0),
  3689. + M_N(22, -4, 73, -3, 70, -17, 73, 0, 0),
  3690. + M_N(23, 17, 50, 10, 54, 14, 57, 0, 0),
  3691. +
  3692. + /* Table 9-14 – Values of variables m and n for ctxIdx from 24 to 39 */
  3693. + M_N(24, 18, 64, 26, 34, 20, 40, 0, 0),
  3694. + M_N(25, 9, 43, 19, 22, 20, 10, 0, 0),
  3695. + M_N(26, 29, 0, 40, 0, 29, 0, 0, 0),
  3696. + M_N(27, 26, 67, 57, 2, 54, 0, 0, 0),
  3697. + M_N(28, 16, 90, 41, 36, 37, 42, 0, 0),
  3698. + M_N(29, 9, 104, 26, 69, 12, 97, 0, 0),
  3699. + M_N(30, -46, 127, -45, 127, -32, 127, 0, 0),
  3700. + M_N(31, -20, 104, -15, 101, -22, 117, 0, 0),
  3701. + M_N(32, 1, 67, -4, 76, -2, 74, 0, 0),
  3702. + M_N(33, -13, 78, -6, 71, -4, 85, 0, 0),
  3703. + M_N(34, -11, 65, -13, 79, -24, 102, 0, 0),
  3704. + M_N(35, 1, 62, 5, 52, 5, 57, 0, 0),
  3705. + M_N(36, -6, 86, 6, 69, -6, 93, 0, 0),
  3706. + M_N(37, -17, 95, -13, 90, -14, 88, 0, 0),
  3707. + M_N(38, -6, 61, 0, 52, -6, 44, 0, 0),
  3708. + M_N(39, 9, 45, 8, 43, 4, 55, 0, 0),
  3709. +
  3710. + /* Table 9-15 – Values of variables m and n for ctxIdx from 40 to 53 */
  3711. + M_N(40, -3, 69, -2, 69, -11, 89, 0, 0),
  3712. + M_N(41, -6, 81, -5, 82, -15, 103, 0, 0),
  3713. + M_N(42, -11, 96, -10, 96, -21, 116, 0, 0),
  3714. + M_N(43, 6, 55, 2, 59, 19, 57, 0, 0),
  3715. + M_N(44, 7, 67, 2, 75, 20, 58, 0, 0),
  3716. + M_N(45, -5, 86, -3, 87, 4, 84, 0, 0),
  3717. + M_N(46, 2, 88, -3, 100, 6, 96, 0, 0),
  3718. + M_N(47, 0, 58, 1, 56, 1, 63, 0, 0),
  3719. + M_N(48, -3, 76, -3, 74, -5, 85, 0, 0),
  3720. + M_N(49, -10, 94, -6, 85, -13, 106, 0, 0),
  3721. + M_N(50, 5, 54, 0, 59, 5, 63, 0, 0),
  3722. + M_N(51, 4, 69, -3, 81, 6, 75, 0, 0),
  3723. + M_N(52, -3, 81, -7, 86, -3, 90, 0, 0),
  3724. + M_N(53, 0, 88, -5, 95, -1, 101, 0, 0),
  3725. +
  3726. + /* Table 9-16 – Values of variables m and n for ctxIdx from 54 to 59 */
  3727. + M_N(54, -7, 67, -1, 66, 3, 55, 0, 0),
  3728. + M_N(55, -5, 74, -1, 77, -4, 79, 0, 0),
  3729. + M_N(56, -4, 74, 1, 70, -2, 75, 0, 0),
  3730. + M_N(57, -5, 80, -2, 86, -12, 97, 0, 0),
  3731. + M_N(58, -7, 72, -5, 72, -7, 50, 0, 0),
  3732. + M_N(59, 1, 58, 0, 61, 1, 60, 0, 0),
  3733. +
  3734. + /* Table 9-17 – Values of variables m and n for ctxIdx from 60 to 69 */
  3735. + M_N(60, 0, 41, 0, 41, 0, 41, 0, 41),
  3736. + M_N(61, 0, 63, 0, 63, 0, 63, 0, 63),
  3737. + M_N(62, 0, 63, 0, 63, 0, 63, 0, 63),
  3738. + M_N(63, 0, 63, 0, 63, 0, 63, 0, 63),
  3739. + M_N(64, -9, 83, -9, 83, -9, 83, -9, 83),
  3740. + M_N(65, 4, 86, 4, 86, 4, 86, 4, 86),
  3741. + M_N(66, 0, 97, 0, 97, 0, 97, 0, 97),
  3742. + M_N(67, -7, 72, -7, 72, -7, 72, -7, 72),
  3743. + M_N(68, 13, 41, 13, 41, 13, 41, 13, 41),
  3744. + M_N(69, 3, 62, 3, 62, 3, 62, 3, 62),
  3745. +
  3746. + /* Table 9-18 – Values of variables m and n for ctxIdx from 70 to 104 */
  3747. + M_N(70, 0, 45, 13, 15, 7, 34, 0, 11),
  3748. + M_N(71, -4, 78, 7, 51, -9, 88, 1, 55),
  3749. + M_N(72, -3, 96, 2, 80, -20, 127, 0, 69),
  3750. + M_N(73, -27, 126, -39, 127, -36, 127, -17, 127),
  3751. + M_N(74, -28, 98, -18, 91, -17, 91, -13, 102),
  3752. + M_N(75, -25, 101, -17, 96, -14, 95, 0, 82),
  3753. + M_N(76, -23, 67, -26, 81, -25, 84, -7, 74),
  3754. + M_N(77, -28, 82, -35, 98, -25, 86, -21, 107),
  3755. + M_N(78, -20, 94, -24, 102, -12, 89, -27, 127),
  3756. + M_N(79, -16, 83, -23, 97, -17, 91, -31, 127),
  3757. + M_N(80, -22, 110, -27, 119, -31, 127, -24, 127),
  3758. + M_N(81, -21, 91, -24, 99, -14, 76, -18, 95),
  3759. + M_N(82, -18, 102, -21, 110, -18, 103, -27, 127),
  3760. + M_N(83, -13, 93, -18, 102, -13, 90, -21, 114),
  3761. + M_N(84, -29, 127, -36, 127, -37, 127, -30, 127),
  3762. + M_N(85, -7, 92, 0, 80, 11, 80, -17, 123),
  3763. + M_N(86, -5, 89, -5, 89, 5, 76, -12, 115),
  3764. + M_N(87, -7, 96, -7, 94, 2, 84, -16, 122),
  3765. + M_N(88, -13, 108, -4, 92, 5, 78, -11, 115),
  3766. + M_N(89, -3, 46, 0, 39, -6, 55, -12, 63),
  3767. + M_N(90, -1, 65, 0, 65, 4, 61, -2, 68),
  3768. + M_N(91, -1, 57, -15, 84, -14, 83, -15, 84),
  3769. + M_N(92, -9, 93, -35, 127, -37, 127, -13, 104),
  3770. + M_N(93, -3, 74, -2, 73, -5, 79, -3, 70),
  3771. + M_N(94, -9, 92, -12, 104, -11, 104, -8, 93),
  3772. + M_N(95, -8, 87, -9, 91, -11, 91, -10, 90),
  3773. + M_N(96, -23, 126, -31, 127, -30, 127, -30, 127),
  3774. + M_N(97, 5, 54, 3, 55, 0, 65, -1, 74),
  3775. + M_N(98, 6, 60, 7, 56, -2, 79, -6, 97),
  3776. + M_N(99, 6, 59, 7, 55, 0, 72, -7, 91),
  3777. + M_N(100, 6, 69, 8, 61, -4, 92, -20, 127),
  3778. + M_N(101, -1, 48, -3, 53, -6, 56, -4, 56),
  3779. + M_N(102, 0, 68, 0, 68, 3, 68, -5, 82),
  3780. + M_N(103, -4, 69, -7, 74, -8, 71, -7, 76),
  3781. + M_N(104, -8, 88, -9, 88, -13, 98, -22, 125),
  3782. +
  3783. + /* Table 9-19 – Values of variables m and n for ctxIdx from 105 to 165 */
  3784. + M_N(105, -2, 85, -13, 103, -4, 86, -7, 93),
  3785. + M_N(106, -6, 78, -13, 91, -12, 88, -11, 87),
  3786. + M_N(107, -1, 75, -9, 89, -5, 82, -3, 77),
  3787. + M_N(108, -7, 77, -14, 92, -3, 72, -5, 71),
  3788. + M_N(109, 2, 54, -8, 76, -4, 67, -4, 63),
  3789. + M_N(110, 5, 50, -12, 87, -8, 72, -4, 68),
  3790. + M_N(111, -3, 68, -23, 110, -16, 89, -12, 84),
  3791. + M_N(112, 1, 50, -24, 105, -9, 69, -7, 62),
  3792. + M_N(113, 6, 42, -10, 78, -1, 59, -7, 65),
  3793. + M_N(114, -4, 81, -20, 112, 5, 66, 8, 61),
  3794. + M_N(115, 1, 63, -17, 99, 4, 57, 5, 56),
  3795. + M_N(116, -4, 70, -78, 127, -4, 71, -2, 66),
  3796. + M_N(117, 0, 67, -70, 127, -2, 71, 1, 64),
  3797. + M_N(118, 2, 57, -50, 127, 2, 58, 0, 61),
  3798. + M_N(119, -2, 76, -46, 127, -1, 74, -2, 78),
  3799. + M_N(120, 11, 35, -4, 66, -4, 44, 1, 50),
  3800. + M_N(121, 4, 64, -5, 78, -1, 69, 7, 52),
  3801. + M_N(122, 1, 61, -4, 71, 0, 62, 10, 35),
  3802. + M_N(123, 11, 35, -8, 72, -7, 51, 0, 44),
  3803. + M_N(124, 18, 25, 2, 59, -4, 47, 11, 38),
  3804. + M_N(125, 12, 24, -1, 55, -6, 42, 1, 45),
  3805. + M_N(126, 13, 29, -7, 70, -3, 41, 0, 46),
  3806. + M_N(127, 13, 36, -6, 75, -6, 53, 5, 44),
  3807. + M_N(128, -10, 93, -8, 89, 8, 76, 31, 17),
  3808. + M_N(129, -7, 73, -34, 119, -9, 78, 1, 51),
  3809. + M_N(130, -2, 73, -3, 75, -11, 83, 7, 50),
  3810. + M_N(131, 13, 46, 32, 20, 9, 52, 28, 19),
  3811. + M_N(132, 9, 49, 30, 22, 0, 67, 16, 33),
  3812. + M_N(133, -7, 100, -44, 127, -5, 90, 14, 62),
  3813. + M_N(134, 9, 53, 0, 54, 1, 67, -13, 108),
  3814. + M_N(135, 2, 53, -5, 61, -15, 72, -15, 100),
  3815. + M_N(136, 5, 53, 0, 58, -5, 75, -13, 101),
  3816. + M_N(137, -2, 61, -1, 60, -8, 80, -13, 91),
  3817. + M_N(138, 0, 56, -3, 61, -21, 83, -12, 94),
  3818. + M_N(139, 0, 56, -8, 67, -21, 64, -10, 88),
  3819. + M_N(140, -13, 63, -25, 84, -13, 31, -16, 84),
  3820. + M_N(141, -5, 60, -14, 74, -25, 64, -10, 86),
  3821. + M_N(142, -1, 62, -5, 65, -29, 94, -7, 83),
  3822. + M_N(143, 4, 57, 5, 52, 9, 75, -13, 87),
  3823. + M_N(144, -6, 69, 2, 57, 17, 63, -19, 94),
  3824. + M_N(145, 4, 57, 0, 61, -8, 74, 1, 70),
  3825. + M_N(146, 14, 39, -9, 69, -5, 35, 0, 72),
  3826. + M_N(147, 4, 51, -11, 70, -2, 27, -5, 74),
  3827. + M_N(148, 13, 68, 18, 55, 13, 91, 18, 59),
  3828. + M_N(149, 3, 64, -4, 71, 3, 65, -8, 102),
  3829. + M_N(150, 1, 61, 0, 58, -7, 69, -15, 100),
  3830. + M_N(151, 9, 63, 7, 61, 8, 77, 0, 95),
  3831. + M_N(152, 7, 50, 9, 41, -10, 66, -4, 75),
  3832. + M_N(153, 16, 39, 18, 25, 3, 62, 2, 72),
  3833. + M_N(154, 5, 44, 9, 32, -3, 68, -11, 75),
  3834. + M_N(155, 4, 52, 5, 43, -20, 81, -3, 71),
  3835. + M_N(156, 11, 48, 9, 47, 0, 30, 15, 46),
  3836. + M_N(157, -5, 60, 0, 44, 1, 7, -13, 69),
  3837. + M_N(158, -1, 59, 0, 51, -3, 23, 0, 62),
  3838. + M_N(159, 0, 59, 2, 46, -21, 74, 0, 65),
  3839. + M_N(160, 22, 33, 19, 38, 16, 66, 21, 37),
  3840. + M_N(161, 5, 44, -4, 66, -23, 124, -15, 72),
  3841. + M_N(162, 14, 43, 15, 38, 17, 37, 9, 57),
  3842. + M_N(163, -1, 78, 12, 42, 44, -18, 16, 54),
  3843. + M_N(164, 0, 60, 9, 34, 50, -34, 0, 62),
  3844. + M_N(165, 9, 69, 0, 89, -22, 127, 12, 72),
  3845. +
  3846. + /* Table 9-20 – Values of variables m and n for ctxIdx from 166 to 226 */
  3847. + M_N(166, 11, 28, 4, 45, 4, 39, 24, 0),
  3848. + M_N(167, 2, 40, 10, 28, 0, 42, 15, 9),
  3849. + M_N(168, 3, 44, 10, 31, 7, 34, 8, 25),
  3850. + M_N(169, 0, 49, 33, -11, 11, 29, 13, 18),
  3851. + M_N(170, 0, 46, 52, -43, 8, 31, 15, 9),
  3852. + M_N(171, 2, 44, 18, 15, 6, 37, 13, 19),
  3853. + M_N(172, 2, 51, 28, 0, 7, 42, 10, 37),
  3854. + M_N(173, 0, 47, 35, -22, 3, 40, 12, 18),
  3855. + M_N(174, 4, 39, 38, -25, 8, 33, 6, 29),
  3856. + M_N(175, 2, 62, 34, 0, 13, 43, 20, 33),
  3857. + M_N(176, 6, 46, 39, -18, 13, 36, 15, 30),
  3858. + M_N(177, 0, 54, 32, -12, 4, 47, 4, 45),
  3859. + M_N(178, 3, 54, 102, -94, 3, 55, 1, 58),
  3860. + M_N(179, 2, 58, 0, 0, 2, 58, 0, 62),
  3861. + M_N(180, 4, 63, 56, -15, 6, 60, 7, 61),
  3862. + M_N(181, 6, 51, 33, -4, 8, 44, 12, 38),
  3863. + M_N(182, 6, 57, 29, 10, 11, 44, 11, 45),
  3864. + M_N(183, 7, 53, 37, -5, 14, 42, 15, 39),
  3865. + M_N(184, 6, 52, 51, -29, 7, 48, 11, 42),
  3866. + M_N(185, 6, 55, 39, -9, 4, 56, 13, 44),
  3867. + M_N(186, 11, 45, 52, -34, 4, 52, 16, 45),
  3868. + M_N(187, 14, 36, 69, -58, 13, 37, 12, 41),
  3869. + M_N(188, 8, 53, 67, -63, 9, 49, 10, 49),
  3870. + M_N(189, -1, 82, 44, -5, 19, 58, 30, 34),
  3871. + M_N(190, 7, 55, 32, 7, 10, 48, 18, 42),
  3872. + M_N(191, -3, 78, 55, -29, 12, 45, 10, 55),
  3873. + M_N(192, 15, 46, 32, 1, 0, 69, 17, 51),
  3874. + M_N(193, 22, 31, 0, 0, 20, 33, 17, 46),
  3875. + M_N(194, -1, 84, 27, 36, 8, 63, 0, 89),
  3876. + M_N(195, 25, 7, 33, -25, 35, -18, 26, -19),
  3877. + M_N(196, 30, -7, 34, -30, 33, -25, 22, -17),
  3878. + M_N(197, 28, 3, 36, -28, 28, -3, 26, -17),
  3879. + M_N(198, 28, 4, 38, -28, 24, 10, 30, -25),
  3880. + M_N(199, 32, 0, 38, -27, 27, 0, 28, -20),
  3881. + M_N(200, 34, -1, 34, -18, 34, -14, 33, -23),
  3882. + M_N(201, 30, 6, 35, -16, 52, -44, 37, -27),
  3883. + M_N(202, 30, 6, 34, -14, 39, -24, 33, -23),
  3884. + M_N(203, 32, 9, 32, -8, 19, 17, 40, -28),
  3885. + M_N(204, 31, 19, 37, -6, 31, 25, 38, -17),
  3886. + M_N(205, 26, 27, 35, 0, 36, 29, 33, -11),
  3887. + M_N(206, 26, 30, 30, 10, 24, 33, 40, -15),
  3888. + M_N(207, 37, 20, 28, 18, 34, 15, 41, -6),
  3889. + M_N(208, 28, 34, 26, 25, 30, 20, 38, 1),
  3890. + M_N(209, 17, 70, 29, 41, 22, 73, 41, 17),
  3891. + M_N(210, 1, 67, 0, 75, 20, 34, 30, -6),
  3892. + M_N(211, 5, 59, 2, 72, 19, 31, 27, 3),
  3893. + M_N(212, 9, 67, 8, 77, 27, 44, 26, 22),
  3894. + M_N(213, 16, 30, 14, 35, 19, 16, 37, -16),
  3895. + M_N(214, 18, 32, 18, 31, 15, 36, 35, -4),
  3896. + M_N(215, 18, 35, 17, 35, 15, 36, 38, -8),
  3897. + M_N(216, 22, 29, 21, 30, 21, 28, 38, -3),
  3898. + M_N(217, 24, 31, 17, 45, 25, 21, 37, 3),
  3899. + M_N(218, 23, 38, 20, 42, 30, 20, 38, 5),
  3900. + M_N(219, 18, 43, 18, 45, 31, 12, 42, 0),
  3901. + M_N(220, 20, 41, 27, 26, 27, 16, 35, 16),
  3902. + M_N(221, 11, 63, 16, 54, 24, 42, 39, 22),
  3903. + M_N(222, 9, 59, 7, 66, 0, 93, 14, 48),
  3904. + M_N(223, 9, 64, 16, 56, 14, 56, 27, 37),
  3905. + M_N(224, -1, 94, 11, 73, 15, 57, 21, 60),
  3906. + M_N(225, -2, 89, 10, 67, 26, 38, 12, 68),
  3907. + M_N(226, -9, 108, -10, 116, -24, 127, 2, 97),
  3908. +
  3909. + /* Table 9-21 – Values of variables m and n for ctxIdx from 227 to 275 */
  3910. + M_N(227, -6, 76, -23, 112, -24, 115, -3, 71),
  3911. + M_N(228, -2, 44, -15, 71, -22, 82, -6, 42),
  3912. + M_N(229, 0, 45, -7, 61, -9, 62, -5, 50),
  3913. + M_N(230, 0, 52, 0, 53, 0, 53, -3, 54),
  3914. + M_N(231, -3, 64, -5, 66, 0, 59, -2, 62),
  3915. + M_N(232, -2, 59, -11, 77, -14, 85, 0, 58),
  3916. + M_N(233, -4, 70, -9, 80, -13, 89, 1, 63),
  3917. + M_N(234, -4, 75, -9, 84, -13, 94, -2, 72),
  3918. + M_N(235, -8, 82, -10, 87, -11, 92, -1, 74),
  3919. + M_N(236, -17, 102, -34, 127, -29, 127, -9, 91),
  3920. + M_N(237, -9, 77, -21, 101, -21, 100, -5, 67),
  3921. + M_N(238, 3, 24, -3, 39, -14, 57, -5, 27),
  3922. + M_N(239, 0, 42, -5, 53, -12, 67, -3, 39),
  3923. + M_N(240, 0, 48, -7, 61, -11, 71, -2, 44),
  3924. + M_N(241, 0, 55, -11, 75, -10, 77, 0, 46),
  3925. + M_N(242, -6, 59, -15, 77, -21, 85, -16, 64),
  3926. + M_N(243, -7, 71, -17, 91, -16, 88, -8, 68),
  3927. + M_N(244, -12, 83, -25, 107, -23, 104, -10, 78),
  3928. + M_N(245, -11, 87, -25, 111, -15, 98, -6, 77),
  3929. + M_N(246, -30, 119, -28, 122, -37, 127, -10, 86),
  3930. + M_N(247, 1, 58, -11, 76, -10, 82, -12, 92),
  3931. + M_N(248, -3, 29, -10, 44, -8, 48, -15, 55),
  3932. + M_N(249, -1, 36, -10, 52, -8, 61, -10, 60),
  3933. + M_N(250, 1, 38, -10, 57, -8, 66, -6, 62),
  3934. + M_N(251, 2, 43, -9, 58, -7, 70, -4, 65),
  3935. + M_N(252, -6, 55, -16, 72, -14, 75, -12, 73),
  3936. + M_N(253, 0, 58, -7, 69, -10, 79, -8, 76),
  3937. + M_N(254, 0, 64, -4, 69, -9, 83, -7, 80),
  3938. + M_N(255, -3, 74, -5, 74, -12, 92, -9, 88),
  3939. + M_N(256, -10, 90, -9, 86, -18, 108, -17, 110),
  3940. + M_N(257, 0, 70, 2, 66, -4, 79, -11, 97),
  3941. + M_N(258, -4, 29, -9, 34, -22, 69, -20, 84),
  3942. + M_N(259, 5, 31, 1, 32, -16, 75, -11, 79),
  3943. + M_N(260, 7, 42, 11, 31, -2, 58, -6, 73),
  3944. + M_N(261, 1, 59, 5, 52, 1, 58, -4, 74),
  3945. + M_N(262, -2, 58, -2, 55, -13, 78, -13, 86),
  3946. + M_N(263, -3, 72, -2, 67, -9, 83, -13, 96),
  3947. + M_N(264, -3, 81, 0, 73, -4, 81, -11, 97),
  3948. + M_N(265, -11, 97, -8, 89, -13, 99, -19, 117),
  3949. + M_N(266, 0, 58, 3, 52, -13, 81, -8, 78),
  3950. + M_N(267, 8, 5, 7, 4, -6, 38, -5, 33),
  3951. + M_N(268, 10, 14, 10, 8, -13, 62, -4, 48),
  3952. + M_N(269, 14, 18, 17, 8, -6, 58, -2, 53),
  3953. + M_N(270, 13, 27, 16, 19, -2, 59, -3, 62),
  3954. + M_N(271, 2, 40, 3, 37, -16, 73, -13, 71),
  3955. + M_N(272, 0, 58, -1, 61, -10, 76, -10, 79),
  3956. + M_N(273, -3, 70, -5, 73, -13, 86, -12, 86),
  3957. + M_N(274, -6, 79, -1, 70, -9, 83, -13, 90),
  3958. + M_N(275, -8, 85, -4, 78, -10, 87, -14, 97),
  3959. +
  3960. + /* Table 9-22 – Values of variables m and n for ctxIdx from 277 to 337 */
  3961. + M_N(277, -13, 106, -21, 126, -22, 127, -6, 93),
  3962. + M_N(278, -16, 106, -23, 124, -25, 127, -6, 84),
  3963. + M_N(279, -10, 87, -20, 110, -25, 120, -8, 79),
  3964. + M_N(280, -21, 114, -26, 126, -27, 127, 0, 66),
  3965. + M_N(281, -18, 110, -25, 124, -19, 114, -1, 71),
  3966. + M_N(282, -14, 98, -17, 105, -23, 117, 0, 62),
  3967. + M_N(283, -22, 110, -27, 121, -25, 118, -2, 60),
  3968. + M_N(284, -21, 106, -27, 117, -26, 117, -2, 59),
  3969. + M_N(285, -18, 103, -17, 102, -24, 113, -5, 75),
  3970. + M_N(286, -21, 107, -26, 117, -28, 118, -3, 62),
  3971. + M_N(287, -23, 108, -27, 116, -31, 120, -4, 58),
  3972. + M_N(288, -26, 112, -33, 122, -37, 124, -9, 66),
  3973. + M_N(289, -10, 96, -10, 95, -10, 94, -1, 79),
  3974. + M_N(290, -12, 95, -14, 100, -15, 102, 0, 71),
  3975. + M_N(291, -5, 91, -8, 95, -10, 99, 3, 68),
  3976. + M_N(292, -9, 93, -17, 111, -13, 106, 10, 44),
  3977. + M_N(293, -22, 94, -28, 114, -50, 127, -7, 62),
  3978. + M_N(294, -5, 86, -6, 89, -5, 92, 15, 36),
  3979. + M_N(295, 9, 67, -2, 80, 17, 57, 14, 40),
  3980. + M_N(296, -4, 80, -4, 82, -5, 86, 16, 27),
  3981. + M_N(297, -10, 85, -9, 85, -13, 94, 12, 29),
  3982. + M_N(298, -1, 70, -8, 81, -12, 91, 1, 44),
  3983. + M_N(299, 7, 60, -1, 72, -2, 77, 20, 36),
  3984. + M_N(300, 9, 58, 5, 64, 0, 71, 18, 32),
  3985. + M_N(301, 5, 61, 1, 67, -1, 73, 5, 42),
  3986. + M_N(302, 12, 50, 9, 56, 4, 64, 1, 48),
  3987. + M_N(303, 15, 50, 0, 69, -7, 81, 10, 62),
  3988. + M_N(304, 18, 49, 1, 69, 5, 64, 17, 46),
  3989. + M_N(305, 17, 54, 7, 69, 15, 57, 9, 64),
  3990. + M_N(306, 10, 41, -7, 69, 1, 67, -12, 104),
  3991. + M_N(307, 7, 46, -6, 67, 0, 68, -11, 97),
  3992. + M_N(308, -1, 51, -16, 77, -10, 67, -16, 96),
  3993. + M_N(309, 7, 49, -2, 64, 1, 68, -7, 88),
  3994. + M_N(310, 8, 52, 2, 61, 0, 77, -8, 85),
  3995. + M_N(311, 9, 41, -6, 67, 2, 64, -7, 85),
  3996. + M_N(312, 6, 47, -3, 64, 0, 68, -9, 85),
  3997. + M_N(313, 2, 55, 2, 57, -5, 78, -13, 88),
  3998. + M_N(314, 13, 41, -3, 65, 7, 55, 4, 66),
  3999. + M_N(315, 10, 44, -3, 66, 5, 59, -3, 77),
  4000. + M_N(316, 6, 50, 0, 62, 2, 65, -3, 76),
  4001. + M_N(317, 5, 53, 9, 51, 14, 54, -6, 76),
  4002. + M_N(318, 13, 49, -1, 66, 15, 44, 10, 58),
  4003. + M_N(319, 4, 63, -2, 71, 5, 60, -1, 76),
  4004. + M_N(320, 6, 64, -2, 75, 2, 70, -1, 83),
  4005. + M_N(321, -2, 69, -1, 70, -2, 76, -7, 99),
  4006. + M_N(322, -2, 59, -9, 72, -18, 86, -14, 95),
  4007. + M_N(323, 6, 70, 14, 60, 12, 70, 2, 95),
  4008. + M_N(324, 10, 44, 16, 37, 5, 64, 0, 76),
  4009. + M_N(325, 9, 31, 0, 47, -12, 70, -5, 74),
  4010. + M_N(326, 12, 43, 18, 35, 11, 55, 0, 70),
  4011. + M_N(327, 3, 53, 11, 37, 5, 56, -11, 75),
  4012. + M_N(328, 14, 34, 12, 41, 0, 69, 1, 68),
  4013. + M_N(329, 10, 38, 10, 41, 2, 65, 0, 65),
  4014. + M_N(330, -3, 52, 2, 48, -6, 74, -14, 73),
  4015. + M_N(331, 13, 40, 12, 41, 5, 54, 3, 62),
  4016. + M_N(332, 17, 32, 13, 41, 7, 54, 4, 62),
  4017. + M_N(333, 7, 44, 0, 59, -6, 76, -1, 68),
  4018. + M_N(334, 7, 38, 3, 50, -11, 82, -13, 75),
  4019. + M_N(335, 13, 50, 19, 40, -2, 77, 11, 55),
  4020. + M_N(336, 10, 57, 3, 66, -2, 77, 5, 64),
  4021. + M_N(337, 26, 43, 18, 50, 25, 42, 12, 70),
  4022. +
  4023. + /* Table 9-23 – Values of variables m and n for ctxIdx from 338 to 398 */
  4024. + M_N(338, 14, 11, 19, -6, 17, -13, 15, 6),
  4025. + M_N(339, 11, 14, 18, -6, 16, -9, 6, 19),
  4026. + M_N(340, 9, 11, 14, 0, 17, -12, 7, 16),
  4027. + M_N(341, 18, 11, 26, -12, 27, -21, 12, 14),
  4028. + M_N(342, 21, 9, 31, -16, 37, -30, 18, 13),
  4029. + M_N(343, 23, -2, 33, -25, 41, -40, 13, 11),
  4030. + M_N(344, 32, -15, 33, -22, 42, -41, 13, 15),
  4031. + M_N(345, 32, -15, 37, -28, 48, -47, 15, 16),
  4032. + M_N(346, 34, -21, 39, -30, 39, -32, 12, 23),
  4033. + M_N(347, 39, -23, 42, -30, 46, -40, 13, 23),
  4034. + M_N(348, 42, -33, 47, -42, 52, -51, 15, 20),
  4035. + M_N(349, 41, -31, 45, -36, 46, -41, 14, 26),
  4036. + M_N(350, 46, -28, 49, -34, 52, -39, 14, 44),
  4037. + M_N(351, 38, -12, 41, -17, 43, -19, 17, 40),
  4038. + M_N(352, 21, 29, 32, 9, 32, 11, 17, 47),
  4039. + M_N(353, 45, -24, 69, -71, 61, -55, 24, 17),
  4040. + M_N(354, 53, -45, 63, -63, 56, -46, 21, 21),
  4041. + M_N(355, 48, -26, 66, -64, 62, -50, 25, 22),
  4042. + M_N(356, 65, -43, 77, -74, 81, -67, 31, 27),
  4043. + M_N(357, 43, -19, 54, -39, 45, -20, 22, 29),
  4044. + M_N(358, 39, -10, 52, -35, 35, -2, 19, 35),
  4045. + M_N(359, 30, 9, 41, -10, 28, 15, 14, 50),
  4046. + M_N(360, 18, 26, 36, 0, 34, 1, 10, 57),
  4047. + M_N(361, 20, 27, 40, -1, 39, 1, 7, 63),
  4048. + M_N(362, 0, 57, 30, 14, 30, 17, -2, 77),
  4049. + M_N(363, -14, 82, 28, 26, 20, 38, -4, 82),
  4050. + M_N(364, -5, 75, 23, 37, 18, 45, -3, 94),
  4051. + M_N(365, -19, 97, 12, 55, 15, 54, 9, 69),
  4052. + M_N(366, -35, 125, 11, 65, 0, 79, -12, 109),
  4053. + M_N(367, 27, 0, 37, -33, 36, -16, 36, -35),
  4054. + M_N(368, 28, 0, 39, -36, 37, -14, 36, -34),
  4055. + M_N(369, 31, -4, 40, -37, 37, -17, 32, -26),
  4056. + M_N(370, 27, 6, 38, -30, 32, 1, 37, -30),
  4057. + M_N(371, 34, 8, 46, -33, 34, 15, 44, -32),
  4058. + M_N(372, 30, 10, 42, -30, 29, 15, 34, -18),
  4059. + M_N(373, 24, 22, 40, -24, 24, 25, 34, -15),
  4060. + M_N(374, 33, 19, 49, -29, 34, 22, 40, -15),
  4061. + M_N(375, 22, 32, 38, -12, 31, 16, 33, -7),
  4062. + M_N(376, 26, 31, 40, -10, 35, 18, 35, -5),
  4063. + M_N(377, 21, 41, 38, -3, 31, 28, 33, 0),
  4064. + M_N(378, 26, 44, 46, -5, 33, 41, 38, 2),
  4065. + M_N(379, 23, 47, 31, 20, 36, 28, 33, 13),
  4066. + M_N(380, 16, 65, 29, 30, 27, 47, 23, 35),
  4067. + M_N(381, 14, 71, 25, 44, 21, 62, 13, 58),
  4068. + M_N(382, 8, 60, 12, 48, 18, 31, 29, -3),
  4069. + M_N(383, 6, 63, 11, 49, 19, 26, 26, 0),
  4070. + M_N(384, 17, 65, 26, 45, 36, 24, 22, 30),
  4071. + M_N(385, 21, 24, 22, 22, 24, 23, 31, -7),
  4072. + M_N(386, 23, 20, 23, 22, 27, 16, 35, -15),
  4073. + M_N(387, 26, 23, 27, 21, 24, 30, 34, -3),
  4074. + M_N(388, 27, 32, 33, 20, 31, 29, 34, 3),
  4075. + M_N(389, 28, 23, 26, 28, 22, 41, 36, -1),
  4076. + M_N(390, 28, 24, 30, 24, 22, 42, 34, 5),
  4077. + M_N(391, 23, 40, 27, 34, 16, 60, 32, 11),
  4078. + M_N(392, 24, 32, 18, 42, 15, 52, 35, 5),
  4079. + M_N(393, 28, 29, 25, 39, 14, 60, 34, 12),
  4080. + M_N(394, 23, 42, 18, 50, 3, 78, 39, 11),
  4081. + M_N(395, 19, 57, 12, 70, -16, 123, 30, 29),
  4082. + M_N(396, 22, 53, 21, 54, 21, 53, 34, 26),
  4083. + M_N(397, 22, 61, 14, 71, 22, 56, 29, 39),
  4084. + M_N(398, 11, 86, 11, 83, 25, 61, 19, 66),
  4085. +
  4086. + /* Values of variables m and n for ctxIdx from 399 to 463 (not documented) */
  4087. + M_N(399, 12, 40, 25, 32, 21, 33, 31, 21),
  4088. + M_N(400, 11, 51, 21, 49, 19, 50, 31, 31),
  4089. + M_N(401, 14, 59, 21, 54, 17, 61, 25, 50),
  4090. + M_N(402, -4, 79, -5, 85, -3, 78, -17, 120),
  4091. + M_N(403, -7, 71, -6, 81, -8, 74, -20, 112),
  4092. + M_N(404, -5, 69, -10, 77, -9, 72, -18, 114),
  4093. + M_N(405, -9, 70, -7, 81, -10, 72, -11, 85),
  4094. + M_N(406, -8, 66, -17, 80, -18, 75, -15, 92),
  4095. + M_N(407, -10, 68, -18, 73, -12, 71, -14, 89),
  4096. + M_N(408, -19, 73, -4, 74, -11, 63, -26, 71),
  4097. + M_N(409, -12, 69, -10, 83, -5, 70, -15, 81),
  4098. + M_N(410, -16, 70, -9, 71, -17, 75, -14, 80),
  4099. + M_N(411, -15, 67, -9, 67, -14, 72, 0, 68),
  4100. + M_N(412, -20, 62, -1, 61, -16, 67, -14, 70),
  4101. + M_N(413, -19, 70, -8, 66, -8, 53, -24, 56),
  4102. + M_N(414, -16, 66, -14, 66, -14, 59, -23, 68),
  4103. + M_N(415, -22, 65, 0, 59, -9, 52, -24, 50),
  4104. + M_N(416, -20, 63, 2, 59, -11, 68, -11, 74),
  4105. + M_N(417, 9, -2, 17, -10, 9, -2, 23, -13),
  4106. + M_N(418, 26, -9, 32, -13, 30, -10, 26, -13),
  4107. + M_N(419, 33, -9, 42, -9, 31, -4, 40, -15),
  4108. + M_N(420, 39, -7, 49, -5, 33, -1, 49, -14),
  4109. + M_N(421, 41, -2, 53, 0, 33, 7, 44, 3),
  4110. + M_N(422, 45, 3, 64, 3, 31, 12, 45, 6),
  4111. + M_N(423, 49, 9, 68, 10, 37, 23, 44, 34),
  4112. + M_N(424, 45, 27, 66, 27, 31, 38, 33, 54),
  4113. + M_N(425, 36, 59, 47, 57, 20, 64, 19, 82),
  4114. + M_N(426, -6, 66, -5, 71, -9, 71, -3, 75),
  4115. + M_N(427, -7, 35, 0, 24, -7, 37, -1, 23),
  4116. + M_N(428, -7, 42, -1, 36, -8, 44, 1, 34),
  4117. + M_N(429, -8, 45, -2, 42, -11, 49, 1, 43),
  4118. + M_N(430, -5, 48, -2, 52, -10, 56, 0, 54),
  4119. + M_N(431, -12, 56, -9, 57, -12, 59, -2, 55),
  4120. + M_N(432, -6, 60, -6, 63, -8, 63, 0, 61),
  4121. + M_N(433, -5, 62, -4, 65, -9, 67, 1, 64),
  4122. + M_N(434, -8, 66, -4, 67, -6, 68, 0, 68),
  4123. + M_N(435, -8, 76, -7, 82, -10, 79, -9, 92),
  4124. + M_N(436, -5, 85, -3, 81, -3, 78, -14, 106),
  4125. + M_N(437, -6, 81, -3, 76, -8, 74, -13, 97),
  4126. + M_N(438, -10, 77, -7, 72, -9, 72, -15, 90),
  4127. + M_N(439, -7, 81, -6, 78, -10, 72, -12, 90),
  4128. + M_N(440, -17, 80, -12, 72, -18, 75, -18, 88),
  4129. + M_N(441, -18, 73, -14, 68, -12, 71, -10, 73),
  4130. + M_N(442, -4, 74, -3, 70, -11, 63, -9, 79),
  4131. + M_N(443, -10, 83, -6, 76, -5, 70, -14, 86),
  4132. + M_N(444, -9, 71, -5, 66, -17, 75, -10, 73),
  4133. + M_N(445, -9, 67, -5, 62, -14, 72, -10, 70),
  4134. + M_N(446, -1, 61, 0, 57, -16, 67, -10, 69),
  4135. + M_N(447, -8, 66, -4, 61, -8, 53, -5, 66),
  4136. + M_N(448, -14, 66, -9, 60, -14, 59, -9, 64),
  4137. + M_N(449, 0, 59, 1, 54, -9, 52, -5, 58),
  4138. + M_N(450, 2, 59, 2, 58, -11, 68, 2, 59),
  4139. + M_N(451, 21, -13, 17, -10, 9, -2, 21, -10),
  4140. + M_N(452, 33, -14, 32, -13, 30, -10, 24, -11),
  4141. + M_N(453, 39, -7, 42, -9, 31, -4, 28, -8),
  4142. + M_N(454, 46, -2, 49, -5, 33, -1, 28, -1),
  4143. + M_N(455, 51, 2, 53, 0, 33, 7, 29, 3),
  4144. + M_N(456, 60, 6, 64, 3, 31, 12, 29, 9),
  4145. + M_N(457, 61, 17, 68, 10, 37, 23, 35, 20),
  4146. + M_N(458, 55, 34, 66, 27, 31, 38, 29, 36),
  4147. + M_N(459, 42, 62, 47, 57, 20, 64, 14, 67),
  4148. +};
  4149. +
  4150. +static void set_ps_field(u32 *buf, struct rkvdec_ps_field field, u32 value)
  4151. +{
  4152. + u8 bit = field.offset % 32, word = field.offset / 32;
  4153. + u64 mask = GENMASK_ULL(bit + field.len - 1, bit);
  4154. + u64 val = ((u64)value << bit) & mask;
  4155. +
  4156. + buf[word] &= ~mask;
  4157. + buf[word] |= val;
  4158. + if (bit + field.len > 32) {
  4159. + buf[word + 1] &= ~(mask >> 32);
  4160. + buf[word + 1] |= val >> 32;
  4161. + }
  4162. +}
  4163. +
  4164. +static void assemble_hw_pps(struct rkvdec_ctx *ctx,
  4165. + struct rkvdec_h264_run *run)
  4166. +{
  4167. + struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
  4168. + const struct v4l2_ctrl_h264_sps *sps = run->sps;
  4169. + const struct v4l2_ctrl_h264_pps *pps = run->pps;
  4170. + const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
  4171. + struct rkvdec_h264_priv_tbl *priv_tbl = h264_ctx->priv_tbl.cpu;
  4172. + struct rkvdec_sps_pps_packet *hw_ps;
  4173. + dma_addr_t scaling_list_address;
  4174. + u32 scaling_distance;
  4175. + u32 i;
  4176. +
  4177. + /*
  4178. + * HW read the SPS/PPS informantion from PPS packet index by PPS id.
  4179. + * offset from the base can be calculated by PPS_id * 32 (size per PPS
  4180. + * packet unit). so the driver copy SPS/PPS information to the exact PPS
  4181. + * packet unit for HW accessing.
  4182. + */
  4183. + hw_ps = &priv_tbl->param_set[pps->pic_parameter_set_id];
  4184. + memset(hw_ps, 0, sizeof(*hw_ps));
  4185. +
  4186. +#define WRITE_PPS(value, field) set_ps_field(hw_ps->info, field, value)
  4187. + /* write sps */
  4188. + WRITE_PPS(0xf, SEQ_PARAMETER_SET_ID);
  4189. + WRITE_PPS(0xff, PROFILE_IDC);
  4190. + WRITE_PPS(1, CONSTRAINT_SET3_FLAG);
  4191. + WRITE_PPS(sps->chroma_format_idc, CHROMA_FORMAT_IDC);
  4192. + WRITE_PPS(sps->bit_depth_luma_minus8 + 8, BIT_DEPTH_LUMA);
  4193. + WRITE_PPS(sps->bit_depth_chroma_minus8 + 8, BIT_DEPTH_CHROMA);
  4194. + WRITE_PPS(0, QPPRIME_Y_ZERO_TRANSFORM_BYPASS_FLAG);
  4195. + WRITE_PPS(sps->log2_max_frame_num_minus4, LOG2_MAX_FRAME_NUM_MINUS4);
  4196. + WRITE_PPS(sps->max_num_ref_frames, MAX_NUM_REF_FRAMES);
  4197. + WRITE_PPS(sps->pic_order_cnt_type, PIC_ORDER_CNT_TYPE);
  4198. + WRITE_PPS(sps->log2_max_pic_order_cnt_lsb_minus4,
  4199. + LOG2_MAX_PIC_ORDER_CNT_LSB_MINUS4);
  4200. + WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO),
  4201. + DELTA_PIC_ORDER_ALWAYS_ZERO_FLAG);
  4202. + WRITE_PPS(sps->pic_width_in_mbs_minus1 + 1, PIC_WIDTH_IN_MBS);
  4203. + WRITE_PPS(sps->pic_height_in_map_units_minus1 + 1, PIC_HEIGHT_IN_MBS);
  4204. + WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY),
  4205. + FRAME_MBS_ONLY_FLAG);
  4206. + WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD),
  4207. + MB_ADAPTIVE_FRAME_FIELD_FLAG);
  4208. + WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE),
  4209. + DIRECT_8X8_INFERENCE_FLAG);
  4210. +
  4211. + /* write pps */
  4212. + WRITE_PPS(0xff, PIC_PARAMETER_SET_ID);
  4213. + WRITE_PPS(0x1f, PPS_SEQ_PARAMETER_SET_ID);
  4214. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE),
  4215. + ENTROPY_CODING_MODE_FLAG);
  4216. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT),
  4217. + BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT_FLAG);
  4218. + WRITE_PPS(pps->num_ref_idx_l0_default_active_minus1,
  4219. + NUM_REF_IDX_L_DEFAULT_ACTIVE_MINUS1(0));
  4220. + WRITE_PPS(pps->num_ref_idx_l1_default_active_minus1,
  4221. + NUM_REF_IDX_L_DEFAULT_ACTIVE_MINUS1(1));
  4222. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_WEIGHTED_PRED),
  4223. + WEIGHTED_PRED_FLAG);
  4224. + WRITE_PPS(pps->weighted_bipred_idc, WEIGHTED_BIPRED_IDC);
  4225. + WRITE_PPS(pps->pic_init_qp_minus26, PIC_INIT_QP_MINUS26);
  4226. + WRITE_PPS(pps->pic_init_qs_minus26, PIC_INIT_QS_MINUS26);
  4227. + WRITE_PPS(pps->chroma_qp_index_offset, CHROMA_QP_INDEX_OFFSET);
  4228. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT),
  4229. + DEBLOCKING_FILTER_CONTROL_PRESENT_FLAG);
  4230. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED),
  4231. + CONSTRAINED_INTRA_PRED_FLAG);
  4232. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT),
  4233. + REDUNDANT_PIC_CNT_PRESENT);
  4234. + WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE),
  4235. + TRANSFORM_8X8_MODE_FLAG);
  4236. + WRITE_PPS(pps->second_chroma_qp_index_offset,
  4237. + SECOND_CHROMA_QP_INDEX_OFFSET);
  4238. +
  4239. + /* always use the matrix sent from userspace */
  4240. + WRITE_PPS(1, SCALING_LIST_ENABLE_FLAG);
  4241. +
  4242. + scaling_distance = offsetof(struct rkvdec_h264_priv_tbl, scaling_list);
  4243. + scaling_list_address = h264_ctx->priv_tbl.dma + scaling_distance;
  4244. + WRITE_PPS(scaling_list_address, SCALING_LIST_ADDRESS);
  4245. +
  4246. + for (i = 0; i < 16; i++) {
  4247. + u32 is_longterm = 0;
  4248. +
  4249. + if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
  4250. + is_longterm = 1;
  4251. +
  4252. + WRITE_PPS(is_longterm, IS_LONG_TERM(i));
  4253. + }
  4254. +}
  4255. +
  4256. +static void assemble_hw_rps(struct rkvdec_ctx *ctx,
  4257. + struct rkvdec_h264_run *run)
  4258. +{
  4259. + const struct v4l2_ctrl_h264_decode_params *dec_params = run->decode_params;
  4260. + const struct v4l2_ctrl_h264_slice_params *sl_params = &run->slices_params[0];
  4261. + const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
  4262. + struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
  4263. + const struct v4l2_ctrl_h264_sps *sps = run->sps;
  4264. + struct rkvdec_h264_priv_tbl *priv_tbl = h264_ctx->priv_tbl.cpu;
  4265. + u32 max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
  4266. +
  4267. + u32 *hw_rps = priv_tbl->rps;
  4268. + u32 i, j;
  4269. + u16 *p = (u16 *)hw_rps;
  4270. +
  4271. + memset(hw_rps, 0, sizeof(priv_tbl->rps));
  4272. +
  4273. + /*
  4274. + * Assign an invalid pic_num if DPB entry at that position is inactive.
  4275. + * If we assign 0 in that position hardware will treat that as a real
  4276. + * reference picture with pic_num 0, triggering output picture
  4277. + * corruption.
  4278. + */
  4279. + for (i = 0; i < 16; i++) {
  4280. + if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
  4281. + continue;
  4282. +
  4283. + if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM ||
  4284. + dpb[i].frame_num < sl_params->frame_num) {
  4285. + p[i] = dpb[i].frame_num;
  4286. + continue;
  4287. + }
  4288. +
  4289. + p[i] = dpb[i].frame_num - max_frame_num;
  4290. + }
  4291. +
  4292. + for (j = 0; j < 3; j++) {
  4293. + for (i = 0; i < h264_ctx->reflists.num_valid; i++) {
  4294. + u8 dpb_valid = 0;
  4295. + u8 idx = 0;
  4296. +
  4297. + switch (j) {
  4298. + case 0:
  4299. + idx = h264_ctx->reflists.p[i];
  4300. + break;
  4301. + case 1:
  4302. + idx = h264_ctx->reflists.b0[i];
  4303. + break;
  4304. + case 2:
  4305. + idx = h264_ctx->reflists.b1[i];
  4306. + break;
  4307. + }
  4308. +
  4309. + if (idx >= ARRAY_SIZE(dec_params->dpb))
  4310. + continue;
  4311. + dpb_valid = !!(dpb[idx].flags &
  4312. + V4L2_H264_DPB_ENTRY_FLAG_ACTIVE);
  4313. +
  4314. + set_ps_field(hw_rps, DPB_INFO(i, j),
  4315. + idx | dpb_valid << 4);
  4316. + }
  4317. + }
  4318. +}
  4319. +
  4320. +/*
  4321. + * NOTE: The values in a scaling list are in zig-zag order, apply inverse
  4322. + * scanning process to get the values in matrix order.
  4323. + */
  4324. +static const u32 zig_zag_4x4[16] = {
  4325. + 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
  4326. +};
  4327. +
  4328. +static const u32 zig_zag_8x8[64] = {
  4329. + 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5,
  4330. + 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
  4331. + 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
  4332. + 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
  4333. +};
  4334. +
  4335. +static void reorder_scaling_list(struct rkvdec_ctx *ctx,
  4336. + struct rkvdec_h264_run *run)
  4337. +{
  4338. + const struct v4l2_ctrl_h264_scaling_matrix *scaling = run->scaling_matrix;
  4339. + const size_t num_list_4x4 = ARRAY_SIZE(scaling->scaling_list_4x4);
  4340. + const size_t list_len_4x4 = ARRAY_SIZE(scaling->scaling_list_4x4[0]);
  4341. + const size_t num_list_8x8 = ARRAY_SIZE(scaling->scaling_list_8x8);
  4342. + const size_t list_len_8x8 = ARRAY_SIZE(scaling->scaling_list_8x8[0]);
  4343. + struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
  4344. + struct rkvdec_h264_priv_tbl *tbl = h264_ctx->priv_tbl.cpu;
  4345. + u8 *dst = tbl->scaling_list;
  4346. + const u8 *src;
  4347. + int i, j;
  4348. +
  4349. + BUILD_BUG_ON(ARRAY_SIZE(zig_zag_4x4) != list_len_4x4);
  4350. + BUILD_BUG_ON(ARRAY_SIZE(zig_zag_8x8) != list_len_8x8);
  4351. + BUILD_BUG_ON(ARRAY_SIZE(tbl->scaling_list) <
  4352. + num_list_4x4 * list_len_4x4 +
  4353. + num_list_8x8 * list_len_8x8);
  4354. +
  4355. + src = &scaling->scaling_list_4x4[0][0];
  4356. + for (i = 0; i < num_list_4x4; ++i) {
  4357. + for (j = 0; j < list_len_4x4; ++j)
  4358. + dst[zig_zag_4x4[j]] = src[j];
  4359. + src += list_len_4x4;
  4360. + dst += list_len_4x4;
  4361. + }
  4362. +
  4363. + src = &scaling->scaling_list_8x8[0][0];
  4364. + for (i = 0; i < num_list_8x8; ++i) {
  4365. + for (j = 0; j < list_len_8x8; ++j)
  4366. + dst[zig_zag_8x8[j]] = src[j];
  4367. + src += list_len_8x8;
  4368. + dst += list_len_8x8;
  4369. + }
  4370. +}
  4371. +
  4372. +/*
  4373. + * dpb poc related registers table
  4374. + */
  4375. +static const u32 poc_reg_tbl_top_field[16] = {
  4376. + RKVDEC_REG_H264_POC_REFER0(0),
  4377. + RKVDEC_REG_H264_POC_REFER0(2),
  4378. + RKVDEC_REG_H264_POC_REFER0(4),
  4379. + RKVDEC_REG_H264_POC_REFER0(6),
  4380. + RKVDEC_REG_H264_POC_REFER0(8),
  4381. + RKVDEC_REG_H264_POC_REFER0(10),
  4382. + RKVDEC_REG_H264_POC_REFER0(12),
  4383. + RKVDEC_REG_H264_POC_REFER0(14),
  4384. + RKVDEC_REG_H264_POC_REFER1(1),
  4385. + RKVDEC_REG_H264_POC_REFER1(3),
  4386. + RKVDEC_REG_H264_POC_REFER1(5),
  4387. + RKVDEC_REG_H264_POC_REFER1(7),
  4388. + RKVDEC_REG_H264_POC_REFER1(9),
  4389. + RKVDEC_REG_H264_POC_REFER1(11),
  4390. + RKVDEC_REG_H264_POC_REFER1(13),
  4391. + RKVDEC_REG_H264_POC_REFER2(0)
  4392. +};
  4393. +
  4394. +static const u32 poc_reg_tbl_bottom_field[16] = {
  4395. + RKVDEC_REG_H264_POC_REFER0(1),
  4396. + RKVDEC_REG_H264_POC_REFER0(3),
  4397. + RKVDEC_REG_H264_POC_REFER0(5),
  4398. + RKVDEC_REG_H264_POC_REFER0(7),
  4399. + RKVDEC_REG_H264_POC_REFER0(9),
  4400. + RKVDEC_REG_H264_POC_REFER0(11),
  4401. + RKVDEC_REG_H264_POC_REFER0(13),
  4402. + RKVDEC_REG_H264_POC_REFER1(0),
  4403. + RKVDEC_REG_H264_POC_REFER1(2),
  4404. + RKVDEC_REG_H264_POC_REFER1(4),
  4405. + RKVDEC_REG_H264_POC_REFER1(6),
  4406. + RKVDEC_REG_H264_POC_REFER1(8),
  4407. + RKVDEC_REG_H264_POC_REFER1(10),
  4408. + RKVDEC_REG_H264_POC_REFER1(12),
  4409. + RKVDEC_REG_H264_POC_REFER1(14),
  4410. + RKVDEC_REG_H264_POC_REFER2(1)
  4411. +};
  4412. +
  4413. +static struct vb2_buffer *
  4414. +get_ref_buf(struct rkvdec_ctx *ctx, struct rkvdec_h264_run *run,
  4415. + unsigned int dpb_idx)
  4416. +{
  4417. + struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
  4418. + const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
  4419. + struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
  4420. + int buf_idx = -1;
  4421. +
  4422. + if (dpb[dpb_idx].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
  4423. + buf_idx = vb2_find_timestamp(cap_q,
  4424. + dpb[dpb_idx].reference_ts, 0);
  4425. +
  4426. + /*
  4427. + * If a DPB entry is unused or invalid, address of current destination
  4428. + * buffer is returned.
  4429. + */
  4430. + if (buf_idx < 0)
  4431. + return &run->base.bufs.dst->vb2_buf;
  4432. +
  4433. + return vb2_get_buffer(cap_q, buf_idx);
  4434. +}
  4435. +
  4436. +static void config_registers(struct rkvdec_ctx *ctx,
  4437. + struct rkvdec_h264_run *run)
  4438. +{
  4439. + struct rkvdec_dev *rkvdec = ctx->dev;
  4440. + const struct v4l2_ctrl_h264_decode_params *dec_params = run->decode_params;
  4441. + const struct v4l2_ctrl_h264_sps *sps = run->sps;
  4442. + const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
  4443. + struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
  4444. + dma_addr_t priv_start_addr = h264_ctx->priv_tbl.dma;
  4445. + const struct v4l2_pix_format_mplane *dst_fmt;
  4446. + struct vb2_v4l2_buffer *src_buf = run->base.bufs.src;
  4447. + struct vb2_v4l2_buffer *dst_buf = run->base.bufs.dst;
  4448. + const struct v4l2_format *f;
  4449. + dma_addr_t rlc_addr;
  4450. + dma_addr_t refer_addr;
  4451. + u32 rlc_len;
  4452. + u32 hor_virstride = 0;
  4453. + u32 ver_virstride = 0;
  4454. + u32 y_virstride = 0;
  4455. + u32 yuv_virstride = 0;
  4456. + u32 offset;
  4457. + dma_addr_t dst_addr;
  4458. + u32 reg, i;
  4459. +
  4460. + reg = RKVDEC_MODE(RKVDEC_MODE_H264);
  4461. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_SYSCTRL);
  4462. +
  4463. + f = &ctx->decoded_fmt;
  4464. + dst_fmt = &f->fmt.pix_mp;
  4465. + hor_virstride = (sps->bit_depth_luma_minus8 + 8) * dst_fmt->width / 8;
  4466. + ver_virstride = round_up(dst_fmt->height, 16);
  4467. + y_virstride = hor_virstride * ver_virstride;
  4468. +
  4469. + if (sps->chroma_format_idc == 0)
  4470. + yuv_virstride = y_virstride;
  4471. + else if (sps->chroma_format_idc == 1)
  4472. + yuv_virstride += y_virstride + y_virstride / 2;
  4473. + else if (sps->chroma_format_idc == 2)
  4474. + yuv_virstride += 2 * y_virstride;
  4475. +
  4476. + reg = RKVDEC_Y_HOR_VIRSTRIDE(hor_virstride / 16) |
  4477. + RKVDEC_UV_HOR_VIRSTRIDE(hor_virstride / 16) |
  4478. + RKVDEC_SLICE_NUM_HIGHBIT |
  4479. + RKVDEC_SLICE_NUM_LOWBITS(0x7ff);
  4480. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_PICPAR);
  4481. +
  4482. + /* config rlc base address */
  4483. + rlc_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
  4484. + writel_relaxed(rlc_addr, rkvdec->regs + RKVDEC_REG_STRM_RLC_BASE);
  4485. + writel_relaxed(rlc_addr, rkvdec->regs + RKVDEC_REG_RLCWRITE_BASE);
  4486. +
  4487. + rlc_len = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
  4488. + reg = RKVDEC_STRM_LEN(rlc_len);
  4489. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_STRM_LEN);
  4490. +
  4491. + /* config cabac table */
  4492. + offset = offsetof(struct rkvdec_h264_priv_tbl, cabac_table);
  4493. + writel_relaxed(priv_start_addr + offset,
  4494. + rkvdec->regs + RKVDEC_REG_CABACTBL_PROB_BASE);
  4495. +
  4496. + /* config output base address */
  4497. + dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
  4498. + writel_relaxed(dst_addr, rkvdec->regs + RKVDEC_REG_DECOUT_BASE);
  4499. +
  4500. + reg = RKVDEC_Y_VIRSTRIDE(y_virstride / 16);
  4501. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_Y_VIRSTRIDE);
  4502. +
  4503. + reg = RKVDEC_YUV_VIRSTRIDE(yuv_virstride / 16);
  4504. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_YUV_VIRSTRIDE);
  4505. +
  4506. + /* config ref pic address & poc */
  4507. + for (i = 0; i < 16; i++) {
  4508. + struct vb2_buffer *vb_buf = get_ref_buf(ctx, run, i);
  4509. +
  4510. + refer_addr = vb2_dma_contig_plane_dma_addr(vb_buf, 0) |
  4511. + RKVDEC_COLMV_USED_FLAG_REF;
  4512. +
  4513. + if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD))
  4514. + refer_addr |= RKVDEC_TOPFIELD_USED_REF |
  4515. + RKVDEC_BOTFIELD_USED_REF;
  4516. + else if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_BOTTOM_FIELD)
  4517. + refer_addr |= RKVDEC_BOTFIELD_USED_REF;
  4518. + else
  4519. + refer_addr |= RKVDEC_TOPFIELD_USED_REF;
  4520. +
  4521. + writel_relaxed(dpb[i].top_field_order_cnt,
  4522. + rkvdec->regs + poc_reg_tbl_top_field[i]);
  4523. + writel_relaxed(dpb[i].bottom_field_order_cnt,
  4524. + rkvdec->regs + poc_reg_tbl_bottom_field[i]);
  4525. +
  4526. + if (i < 15)
  4527. + writel_relaxed(refer_addr,
  4528. + rkvdec->regs + RKVDEC_REG_H264_BASE_REFER(i));
  4529. + else
  4530. + writel_relaxed(refer_addr,
  4531. + rkvdec->regs + RKVDEC_REG_H264_BASE_REFER15);
  4532. + }
  4533. +
  4534. + /*
  4535. + * Since support frame mode only
  4536. + * top_field_order_cnt is the same as bottom_field_order_cnt
  4537. + */
  4538. + reg = RKVDEC_CUR_POC(dec_params->top_field_order_cnt);
  4539. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_CUR_POC0);
  4540. +
  4541. + reg = RKVDEC_CUR_POC(dec_params->bottom_field_order_cnt);
  4542. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_CUR_POC1);
  4543. +
  4544. + /* config hw pps address */
  4545. + offset = offsetof(struct rkvdec_h264_priv_tbl, param_set);
  4546. + writel_relaxed(priv_start_addr + offset,
  4547. + rkvdec->regs + RKVDEC_REG_PPS_BASE);
  4548. +
  4549. + /* config hw rps address */
  4550. + offset = offsetof(struct rkvdec_h264_priv_tbl, rps);
  4551. + writel_relaxed(priv_start_addr + offset,
  4552. + rkvdec->regs + RKVDEC_REG_RPS_BASE);
  4553. +
  4554. + reg = RKVDEC_AXI_DDR_RDATA(0);
  4555. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_AXI_DDR_RDATA);
  4556. +
  4557. + reg = RKVDEC_AXI_DDR_WDATA(0);
  4558. + writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_AXI_DDR_WDATA);
  4559. +
  4560. + offset = offsetof(struct rkvdec_h264_priv_tbl, err_info);
  4561. + writel_relaxed(priv_start_addr + offset,
  4562. + rkvdec->regs + RKVDEC_REG_H264_ERRINFO_BASE);
  4563. +}
  4564. +
  4565. +#define RKVDEC_H264_MAX_DEPTH_IN_BYTES 2
  4566. +
  4567. +static int rkvdec_h264_adjust_fmt(struct rkvdec_ctx *ctx,
  4568. + struct v4l2_format *f)
  4569. +{
  4570. + struct v4l2_pix_format_mplane *fmt = &f->fmt.pix_mp;
  4571. +
  4572. + fmt->num_planes = 1;
  4573. + fmt->plane_fmt[0].sizeimage = fmt->width * fmt->height *
  4574. + RKVDEC_H264_MAX_DEPTH_IN_BYTES;
  4575. + return 0;
  4576. +}
  4577. +
  4578. +static int rkvdec_h264_start(struct rkvdec_ctx *ctx)
  4579. +{
  4580. + struct rkvdec_dev *rkvdec = ctx->dev;
  4581. + struct rkvdec_h264_priv_tbl *priv_tbl;
  4582. + struct rkvdec_h264_ctx *h264_ctx;
  4583. + int ret;
  4584. +
  4585. + h264_ctx = kzalloc(sizeof(*h264_ctx), GFP_KERNEL);
  4586. + if (!h264_ctx)
  4587. + return -ENOMEM;
  4588. +
  4589. + priv_tbl = dma_alloc_coherent(rkvdec->dev, sizeof(*priv_tbl),
  4590. + &h264_ctx->priv_tbl.dma, GFP_KERNEL);
  4591. + if (!priv_tbl) {
  4592. + ret = -ENOMEM;
  4593. + goto err_free_ctx;
  4594. + }
  4595. +
  4596. + h264_ctx->priv_tbl.size = sizeof(*priv_tbl);
  4597. + h264_ctx->priv_tbl.cpu = priv_tbl;
  4598. + memcpy(priv_tbl->cabac_table, rkvdec_h264_cabac_table,
  4599. + sizeof(rkvdec_h264_cabac_table));
  4600. +
  4601. + ctx->priv = h264_ctx;
  4602. + return 0;
  4603. +
  4604. +err_free_ctx:
  4605. + kfree(ctx);
  4606. + return ret;
  4607. +}
  4608. +
  4609. +static void rkvdec_h264_stop(struct rkvdec_ctx *ctx)
  4610. +{
  4611. + struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
  4612. + struct rkvdec_dev *rkvdec = ctx->dev;
  4613. +
  4614. + dma_free_coherent(rkvdec->dev, h264_ctx->priv_tbl.size,
  4615. + h264_ctx->priv_tbl.cpu, h264_ctx->priv_tbl.dma);
  4616. + kfree(h264_ctx);
  4617. +}
  4618. +
  4619. +static void rkvdec_h264_run_preamble(struct rkvdec_ctx *ctx,
  4620. + struct rkvdec_h264_run *run)
  4621. +{
  4622. + struct v4l2_ctrl *ctrl;
  4623. +
  4624. + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
  4625. + V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAMS);
  4626. + run->decode_params = ctrl ? ctrl->p_cur.p : NULL;
  4627. + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
  4628. + V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAMS);
  4629. + run->slices_params = ctrl ? ctrl->p_cur.p : NULL;
  4630. + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
  4631. + V4L2_CID_MPEG_VIDEO_H264_SPS);
  4632. + run->sps = ctrl ? ctrl->p_cur.p : NULL;
  4633. + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
  4634. + V4L2_CID_MPEG_VIDEO_H264_PPS);
  4635. + run->pps = ctrl ? ctrl->p_cur.p : NULL;
  4636. + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
  4637. + V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX);
  4638. + run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL;
  4639. +
  4640. + rkvdec_run_preamble(ctx, &run->base);
  4641. +}
  4642. +
  4643. +static int rkvdec_h264_run(struct rkvdec_ctx *ctx)
  4644. +{
  4645. + struct v4l2_h264_reflist_builder reflist_builder;
  4646. + struct rkvdec_dev *rkvdec = ctx->dev;
  4647. + struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
  4648. + struct rkvdec_h264_run run;
  4649. +
  4650. + rkvdec_h264_run_preamble(ctx, &run);
  4651. +
  4652. + /* Build the P/B{0,1} ref lists. */
  4653. + v4l2_h264_init_reflist_builder(&reflist_builder, run.decode_params,
  4654. + &run.slices_params[0], run.sps,
  4655. + run.decode_params->dpb);
  4656. + h264_ctx->reflists.num_valid = reflist_builder.num_valid;
  4657. + v4l2_h264_build_p_ref_list(&reflist_builder, h264_ctx->reflists.p);
  4658. + v4l2_h264_build_b_ref_lists(&reflist_builder, h264_ctx->reflists.b0,
  4659. + h264_ctx->reflists.b1);
  4660. +
  4661. + reorder_scaling_list(ctx, &run);
  4662. + assemble_hw_pps(ctx, &run);
  4663. + assemble_hw_rps(ctx, &run);
  4664. + config_registers(ctx, &run);
  4665. +
  4666. + rkvdec_run_postamble(ctx, &run.base);
  4667. +
  4668. + schedule_delayed_work(&rkvdec->watchdog_work, msecs_to_jiffies(2000));
  4669. +
  4670. + writel(0xffffffff, rkvdec->regs + RKVDEC_REG_STRMD_ERR_EN);
  4671. + writel(0xffffffff, rkvdec->regs + RKVDEC_REG_H264_ERR_E);
  4672. + writel(1, rkvdec->regs + RKVDEC_REG_PREF_LUMA_CACHE_COMMAND);
  4673. + writel(1, rkvdec->regs + RKVDEC_REG_PREF_CHR_CACHE_COMMAND);
  4674. +
  4675. + /* Start decoding! */
  4676. + writel(RKVDEC_INTERRUPT_DEC_E | RKVDEC_CONFIG_DEC_CLK_GATE_E |
  4677. + RKVDEC_TIMEOUT_E | RKVDEC_BUF_EMPTY_E,
  4678. + rkvdec->regs + RKVDEC_REG_INTERRUPT);
  4679. +
  4680. + return 0;
  4681. +}
  4682. +
  4683. +const struct rkvdec_coded_fmt_ops rkvdec_h264_fmt_ops = {
  4684. + .adjust_fmt = rkvdec_h264_adjust_fmt,
  4685. + .start = rkvdec_h264_start,
  4686. + .stop = rkvdec_h264_stop,
  4687. + .run = rkvdec_h264_run,
  4688. +};
  4689. +
  4690. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec-regs.h linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec-regs.h
  4691. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/drivers/staging/media/rkvdec/rkvdec-regs.h 1969-12-31 16:00:00.000000000 -0800
  4692. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/drivers/staging/media/rkvdec/rkvdec-regs.h 2020-02-14 21:55:21.389863881 -0800
  4693. @@ -0,0 +1,239 @@
  4694. +/* SPDX-License-Identifier: GPL-2.0 */
  4695. +/*
  4696. + * Rockchip VPU codec driver
  4697. + *
  4698. + * Copyright (C) 2015 Rockchip Electronics Co., Ltd.
  4699. + * Jung Zhao <[email protected]>
  4700. + * Alpha Lin <[email protected]>
  4701. + *
  4702. + * This software is licensed under the terms of the GNU General Public
  4703. + * License version 2, as published by the Free Software Foundation, and
  4704. + * may be copied, distributed, and modified under those terms.
  4705. + *
  4706. + * This program is distributed in the hope that it will be useful,
  4707. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4708. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4709. + * GNU General Public License for more details.
  4710. + */
  4711. +
  4712. +#ifndef RKVDEC_REGS_H_
  4713. +#define RKVDEC_REGS_H_
  4714. +
  4715. +/* rkvcodec registers */
  4716. +#define RKVDEC_REG_INTERRUPT 0x004
  4717. +#define RKVDEC_INTERRUPT_DEC_E BIT(0)
  4718. +#define RKVDEC_CONFIG_DEC_CLK_GATE_E BIT(1)
  4719. +#define RKVDEC_E_STRMD_CLKGATE_DIS BIT(2)
  4720. +#define RKVDEC_TIMEOUT_MODE BIT(3)
  4721. +#define RKVDEC_IRQ_DIS BIT(4)
  4722. +#define RKVDEC_TIMEOUT_E BIT(5)
  4723. +#define RKVDEC_BUF_EMPTY_E BIT(6)
  4724. +#define RKVDEC_STRM_E_WAITDECFIFO_EMPTY BIT(7)
  4725. +#define RKVDEC_IRQ BIT(8)
  4726. +#define RKVDEC_IRQ_RAW BIT(9)
  4727. +#define RKVDEC_E_REWRITE_VALID BIT(10)
  4728. +#define RKVDEC_COMMONIRQ_MODE BIT(11)
  4729. +#define RKVDEC_RDY_STA BIT(12)
  4730. +#define RKVDEC_BUS_STA BIT(13)
  4731. +#define RKVDEC_ERR_STA BIT(14)
  4732. +#define RKVDEC_TIMEOUT_STA BIT(15)
  4733. +#define RKVDEC_BUF_EMPTY_STA BIT(16)
  4734. +#define RKVDEC_COLMV_REF_ERR_STA BIT(17)
  4735. +#define RKVDEC_CABU_END_STA BIT(18)
  4736. +#define RKVDEC_H264ORVP9_ERR_MODE BIT(19)
  4737. +#define RKVDEC_SOFTRST_EN_P BIT(20)
  4738. +#define RKVDEC_FORCE_SOFTRESET_VALID BIT(21)
  4739. +#define RKVDEC_SOFTRESET_RDY BIT(22)
  4740. +
  4741. +#define RKVDEC_REG_SYSCTRL 0x008
  4742. +#define RKVDEC_IN_ENDIAN BIT(0)
  4743. +#define RKVDEC_IN_SWAP32_E BIT(1)
  4744. +#define RKVDEC_IN_SWAP64_E BIT(2)
  4745. +#define RKVDEC_STR_ENDIAN BIT(3)
  4746. +#define RKVDEC_STR_SWAP32_E BIT(4)
  4747. +#define RKVDEC_STR_SWAP64_E BIT(5)
  4748. +#define RKVDEC_OUT_ENDIAN BIT(6)
  4749. +#define RKVDEC_OUT_SWAP32_E BIT(7)
  4750. +#define RKVDEC_OUT_CBCR_SWAP BIT(8)
  4751. +#define RKVDEC_RLC_MODE_DIRECT_WRITE BIT(10)
  4752. +#define RKVDEC_RLC_MODE BIT(11)
  4753. +#define RKVDEC_STRM_START_BIT(x) (((x) & 0x7f) << 12)
  4754. +#define RKVDEC_MODE(x) (((x) & 0x03) << 20)
  4755. +#define RKVDEC_MODE_H264 1
  4756. +#define RKVDEC_MODE_VP9 2
  4757. +#define RKVDEC_RPS_MODE BIT(24)
  4758. +#define RKVDEC_STRM_MODE BIT(25)
  4759. +#define RKVDEC_H264_STRM_LASTPKT BIT(26)
  4760. +#define RKVDEC_H264_FIRSTSLICE_FLAG BIT(27)
  4761. +#define RKVDEC_H264_FRAME_ORSLICE BIT(28)
  4762. +#define RKVDEC_BUSPR_SLOT_DIS BIT(29)
  4763. +
  4764. +#define RKVDEC_REG_PICPAR 0x00C
  4765. +#define RKVDEC_Y_HOR_VIRSTRIDE(x) ((x) & 0x1ff)
  4766. +#define RKVDEC_SLICE_NUM_HIGHBIT BIT(11)
  4767. +#define RKVDEC_UV_HOR_VIRSTRIDE(x) (((x) & 0x1ff) << 12)
  4768. +#define RKVDEC_SLICE_NUM_LOWBITS(x) (((x) & 0x7ff) << 21)
  4769. +
  4770. +#define RKVDEC_REG_STRM_RLC_BASE 0x010
  4771. +
  4772. +#define RKVDEC_REG_STRM_LEN 0x014
  4773. +#define RKVDEC_STRM_LEN(x) ((x) & 0x7ffffff)
  4774. +
  4775. +#define RKVDEC_REG_CABACTBL_PROB_BASE 0x018
  4776. +#define RKVDEC_REG_DECOUT_BASE 0x01C
  4777. +
  4778. +#define RKVDEC_REG_Y_VIRSTRIDE 0x020
  4779. +#define RKVDEC_Y_VIRSTRIDE(x) ((x) & 0xfffff)
  4780. +
  4781. +#define RKVDEC_REG_YUV_VIRSTRIDE 0x024
  4782. +#define RKVDEC_YUV_VIRSTRIDE(x) ((x) & 0x1fffff)
  4783. +#define RKVDEC_REG_H264_BASE_REFER(i) (((i) * 0x04) + 0x028)
  4784. +
  4785. +#define RKVDEC_REG_H264_BASE_REFER15 0x0C0
  4786. +#define RKVDEC_FIELD_REF BIT(0)
  4787. +#define RKVDEC_TOPFIELD_USED_REF BIT(1)
  4788. +#define RKVDEC_BOTFIELD_USED_REF BIT(2)
  4789. +#define RKVDEC_COLMV_USED_FLAG_REF BIT(3)
  4790. +
  4791. +#define RKVDEC_REG_VP9_LAST_FRAME_BASE 0x02c
  4792. +#define RKVDEC_REG_VP9_GOLDEN_FRAME_BASE 0x030
  4793. +#define RKVDEC_REG_VP9_ALTREF_FRAME_BASE 0x034
  4794. +
  4795. +#define RKVDEC_REG_VP9_CPRHEADER_OFFSET 0x028
  4796. +#define RKVDEC_VP9_CPRHEADER_OFFSET(x) ((x) & 0xffff)
  4797. +
  4798. +#define RKVDEC_REG_VP9_REFERLAST_BASE 0x02C
  4799. +#define RKVDEC_REG_VP9_REFERGOLDEN_BASE 0x030
  4800. +#define RKVDEC_REG_VP9_REFERALFTER_BASE 0x034
  4801. +
  4802. +#define RKVDEC_REG_VP9COUNT_BASE 0x038
  4803. +#define RKVDEC_VP9COUNT_UPDATE_EN BIT(0)
  4804. +
  4805. +#define RKVDEC_REG_VP9_SEGIDLAST_BASE 0x03C
  4806. +#define RKVDEC_REG_VP9_SEGIDCUR_BASE 0x040
  4807. +#define RKVDEC_REG_VP9_FRAME_SIZE(i) ((i) * 0x04 + 0x044)
  4808. +#define RKVDEC_VP9_FRAMEWIDTH(x) (((x) & 0xffff) << 0)
  4809. +#define RKVDEC_VP9_FRAMEHEIGHT(x) (((x) & 0xffff) << 16)
  4810. +
  4811. +#define RKVDEC_VP9_SEGID_GRP(i) ((i) * 0x04 + 0x050)
  4812. +#define RKVDEC_SEGID_ABS_DELTA(x) ((x) & 0x1)
  4813. +#define RKVDEC_SEGID_FRAME_QP_DELTA_EN(x) (((x) & 0x1) << 1)
  4814. +#define RKVDEC_SEGID_FRAME_QP_DELTA(x) (((x) & 0x1ff) << 2)
  4815. +#define RKVDEC_SEGID_FRAME_LOOPFILTER_VALUE_EN(x) (((x) & 0x1) << 11)
  4816. +#define RKVDEC_SEGID_FRAME_LOOPFILTER_VALUE(x) (((x) & 0x7f) << 12)
  4817. +#define RKVDEC_SEGID_REFERINFO_EN(x) (((x) & 0x1) << 19)
  4818. +#define RKVDEC_SEGID_REFERINFO(x) (((x) & 0x03) << 20)
  4819. +#define RKVDEC_SEGID_FRAME_SKIP_EN(x) (((x) & 0x1) << 22)
  4820. +
  4821. +#define RKVDEC_VP9_CPRHEADER_CONFIG 0x070
  4822. +#define RKVDEC_VP9_TX_MODE(x) ((x) & 0x07)
  4823. +#define RKVDEC_VP9_FRAME_REF_MODE(x) (((x) & 0x03) << 3)
  4824. +
  4825. +#define RKVDEC_VP9_REF_SCALE(i) ((i) * 0x04 + 0x074)
  4826. +#define RKVDEC_VP9_REF_HOR_SCALE(x) ((x) & 0xffff)
  4827. +#define RKVDEC_VP9_REF_VER_SCALE(x) (((x) & 0xffff) << 16)
  4828. +
  4829. +#define RKVDEC_VP9_REF_DELTAS_LASTFRAME 0x080
  4830. +#define RKVDEC_REF_DELTAS_LASTFRAME(pos, val) (((val) & 0x7f) << ((pos) * 7))
  4831. +
  4832. +#define RKVDEC_VP9_INFO_LASTFRAME 0x084
  4833. +#define RKVDEC_MODE_DELTAS_LASTFRAME(pos, val) (((val) & 0x7f) << ((pos) * 7))
  4834. +#define RKVDEC_SEG_EN_LASTFRAME BIT(16)
  4835. +#define RKVDEC_LAST_SHOW_FRAME BIT(17)
  4836. +#define RKVDEC_LAST_INTRA_ONLY BIT(18)
  4837. +#define RKVDEC_LAST_WIDHHEIGHT_EQCUR BIT(19)
  4838. +#define RKVDEC_COLOR_SPACE_LASTKEYFRAME(x) (((x) & 0x07) << 20)
  4839. +
  4840. +#define RKVDEC_VP9_INTERCMD_BASE 0x088
  4841. +
  4842. +#define RKVDEC_VP9_INTERCMD_NUM 0x08C
  4843. +#define RKVDEC_INTERCMD_NUM(x) ((x) & 0xffffff)
  4844. +
  4845. +#define RKVDEC_VP9_LASTTILE_SIZE 0x090
  4846. +#define RKVDEC_LASTTILE_SIZE(x) ((x) & 0xffffff)
  4847. +
  4848. +#define RKVDEC_VP9_HOR_VIRSTRIDE(i) ((i) * 0x04 + 0x094)
  4849. +#define RKVDEC_HOR_Y_VIRSTRIDE(x) ((x) & 0x1ff)
  4850. +#define RKVDEC_HOR_UV_VIRSTRIDE(x) (((x) & 0x1ff) << 16)
  4851. +
  4852. +#define RKVDEC_REG_H264_POC_REFER0(i) (((i) * 0x04) + 0x064)
  4853. +#define RKVDEC_REG_H264_POC_REFER1(i) (((i) * 0x04) + 0x0C4)
  4854. +#define RKVDEC_REG_H264_POC_REFER2(i) (((i) * 0x04) + 0x120)
  4855. +#define RKVDEC_POC_REFER(x) ((x) & 0xffffffff)
  4856. +
  4857. +#define RKVDEC_REG_CUR_POC0 0x0A0
  4858. +#define RKVDEC_REG_CUR_POC1 0x128
  4859. +#define RKVDEC_CUR_POC(x) ((x) & 0xffffffff)
  4860. +
  4861. +#define RKVDEC_REG_RLCWRITE_BASE 0x0A4
  4862. +#define RKVDEC_REG_PPS_BASE 0x0A8
  4863. +#define RKVDEC_REG_RPS_BASE 0x0AC
  4864. +
  4865. +#define RKVDEC_REG_STRMD_ERR_EN 0x0B0
  4866. +#define RKVDEC_STRMD_ERR_EN(x) ((x) & 0xffffffff)
  4867. +
  4868. +#define RKVDEC_REG_STRMD_ERR_STA 0x0B4
  4869. +#define RKVDEC_STRMD_ERR_STA(x) ((x) & 0xfffffff)
  4870. +#define RKVDEC_COLMV_ERR_REF_PICIDX(x) (((x) & 0x0f) << 28)
  4871. +
  4872. +#define RKVDEC_REG_STRMD_ERR_CTU 0x0B8
  4873. +#define RKVDEC_STRMD_ERR_CTU(x) ((x) & 0xff)
  4874. +#define RKVDEC_STRMD_ERR_CTU_YOFFSET(x) (((x) & 0xff) << 8)
  4875. +#define RKVDEC_STRMFIFO_SPACE2FULL(x) (((x) & 0x7f) << 16)
  4876. +#define RKVDEC_VP9_ERR_EN_CTU0 BIT(24)
  4877. +
  4878. +#define RKVDEC_REG_SAO_CTU_POS 0x0BC
  4879. +#define RKVDEC_SAOWR_XOFFSET(x) ((x) & 0x1ff)
  4880. +#define RKVDEC_SAOWR_YOFFSET(x) (((x) & 0x3ff) << 16)
  4881. +
  4882. +#define RKVDEC_VP9_LAST_FRAME_YSTRIDE 0x0C0
  4883. +#define RKVDEC_VP9_GOLDEN_FRAME_YSTRIDE 0x0C4
  4884. +#define RKVDEC_VP9_ALTREF_FRAME_YSTRIDE 0x0C8
  4885. +#define RKVDEC_VP9_REF_YSTRIDE(x) (((x) & 0xfffff) << 0)
  4886. +
  4887. +#define RKVDEC_VP9_LAST_FRAME_YUVSTRIDE 0x0CC
  4888. +#define RKVDEC_VP9_REF_YUVSTRIDE(x) (((x) & 0x1fffff) << 0)
  4889. +
  4890. +#define RKVDEC_VP9_REF_COLMV_BASE 0x0D0
  4891. +
  4892. +#define RKVDEC_REG_PERFORMANCE_CYCLE 0x100
  4893. +#define RKVDEC_PERFORMANCE_CYCLE(x) ((x) & 0xffffffff)
  4894. +
  4895. +#define RKVDEC_REG_AXI_DDR_RDATA 0x104
  4896. +#define RKVDEC_AXI_DDR_RDATA(x) ((x) & 0xffffffff)
  4897. +
  4898. +#define RKVDEC_REG_AXI_DDR_WDATA 0x108
  4899. +#define RKVDEC_AXI_DDR_WDATA(x) ((x) & 0xffffffff)
  4900. +
  4901. +#define RKVDEC_REG_FPGADEBUG_RESET 0x10C
  4902. +#define RKVDEC_BUSIFD_RESETN BIT(0)
  4903. +#define RKVDEC_CABAC_RESETN BIT(1)
  4904. +#define RKVDEC_DEC_CTRL_RESETN BIT(2)
  4905. +#define RKVDEC_TRANSD_RESETN BIT(3)
  4906. +#define RKVDEC_INTRA_RESETN BIT(4)
  4907. +#define RKVDEC_INTER_RESETN BIT(5)
  4908. +#define RKVDEC_RECON_RESETN BIT(6)
  4909. +#define RKVDEC_FILER_RESETN BIT(7)
  4910. +
  4911. +#define RKVDEC_REG_PERFORMANCE_SEL 0x110
  4912. +#define RKVDEC_PERF_SEL_CNT0(x) ((x) & 0x3f)
  4913. +#define RKVDEC_PERF_SEL_CNT1(x) (((x) & 0x3f) << 8)
  4914. +#define RKVDEC_PERF_SEL_CNT2(x) (((x) & 0x3f) << 16)
  4915. +
  4916. +#define RKVDEC_REG_PERFORMANCE_CNT(i) ((i) * 0x04 + 0x114)
  4917. +#define RKVDEC_PERF_CNT(x) ((x) & 0xffffffff)
  4918. +
  4919. +#define RKVDEC_REG_H264_ERRINFO_BASE 0x12C
  4920. +
  4921. +#define RKVDEC_REG_H264_ERRINFO_NUM 0x130
  4922. +#define RKVDEC_SLICEDEC_NUM(x) ((x) & 0x3fff)
  4923. +#define RKVDEC_STRMD_DECT_ERR_FLAG BIT(15)
  4924. +#define RKVDEC_ERR_PKT_NUM(x) (((x) & 0x3fff) << 16)
  4925. +
  4926. +#define RKVDEC_REG_H264_ERR_E 0x134
  4927. +#define RKVDEC_H264_ERR_EN_HIGHBITS(x) ((x) & 0x3fffffff)
  4928. +
  4929. +#define RKVDEC_REG_PREF_LUMA_CACHE_COMMAND 0x410
  4930. +#define RKVDEC_REG_PREF_CHR_CACHE_COMMAND 0x450
  4931. +
  4932. +#endif /* RKVDEC_REGS_H_ */
  4933. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/include/media/h264-ctrls.h linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/include/media/h264-ctrls.h
  4934. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/include/media/h264-ctrls.h 2020-02-14 22:59:09.069549210 -0800
  4935. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/include/media/h264-ctrls.h 2020-02-14 21:55:22.369871146 -0800
  4936. @@ -185,10 +185,8 @@
  4937. #define V4L2_H264_DPB_ENTRY_FLAG_VALID 0x01
  4938. #define V4L2_H264_DPB_ENTRY_FLAG_ACTIVE 0x02
  4939. #define V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM 0x04
  4940. -#define V4L2_H264_DPB_ENTRY_FLAG_FIELD_PICTURE 0x08
  4941. -#define V4L2_H264_DPB_ENTRY_FLAG_REF_TOP 0x10
  4942. -#define V4L2_H264_DPB_ENTRY_FLAG_REF_BOTTOM 0x20
  4943. -#define V4L2_H264_DPB_ENTRY_FLAG_REF_FRAME 0x30
  4944. +#define V4L2_H264_DPB_ENTRY_FLAG_FIELD 0x08
  4945. +#define V4L2_H264_DPB_ENTRY_FLAG_BOTTOM_FIELD 0x10
  4946.  
  4947. struct v4l2_h264_dpb_entry {
  4948. __u64 reference_ts;
  4949. diff -uNr '--exclude=.git' linux-rockchip-v4l2-5.5-work-in-progress-v5.5/include/media/v4l2-h264.h linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/include/media/v4l2-h264.h
  4950. --- linux-rockchip-v4l2-5.5-work-in-progress-v5.5/include/media/v4l2-h264.h 1969-12-31 16:00:00.000000000 -0800
  4951. +++ linux-rockchip-v4l2-5.5-from-list-v5.5-rkvdec/include/media/v4l2-h264.h 2020-02-14 21:55:22.377871205 -0800
  4952. @@ -0,0 +1,86 @@
  4953. +/* SPDX-License-Identifier: GPL-2.0-or-later */
  4954. +/*
  4955. + * Helper functions for H264 codecs.
  4956. + *
  4957. + * Copyright (c) 2019 Collabora, Ltd.
  4958. + *
  4959. + * Author: Boris Brezillon <[email protected]>
  4960. + */
  4961. +
  4962. +#ifndef _MEDIA_V4L2_H264_H
  4963. +#define _MEDIA_V4L2_H264_H
  4964. +
  4965. +#include <media/h264-ctrls.h>
  4966. +
  4967. +/**
  4968. + * struct v4l2_h264_reflist_builder - Reference list builder object
  4969. + *
  4970. + * @refs.pic_order_count: reference picture order count
  4971. + * @refs.frame_num: reference frame number
  4972. + * @refs.pic_num: reference picture number
  4973. + * @refs.longterm: set to true for a long term reference
  4974. + * @refs: array of references
  4975. + * @cur_pic_order_count: picture order count of the frame being decoded
  4976. + * @unordered_reflist: unordered list of references. Will be used to generate
  4977. + * ordered P/B0/B1 lists
  4978. + * @num_valid: number of valid references in the refs array
  4979. + *
  4980. + * This object stores the context of the P/B0/B1 reference list builder.
  4981. + * This procedure is described in section '8.2.4 Decoding process for reference
  4982. + * picture lists construction' of the H264 spec.
  4983. + */
  4984. +struct v4l2_h264_reflist_builder {
  4985. + struct {
  4986. + s32 pic_order_count;
  4987. + int frame_num;
  4988. + u16 pic_num;
  4989. + u16 longterm : 1;
  4990. + } refs[16];
  4991. + s32 cur_pic_order_count;
  4992. + u8 unordered_reflist[16];
  4993. + u8 num_valid;
  4994. +};
  4995. +
  4996. +void
  4997. +v4l2_h264_init_reflist_builder(struct v4l2_h264_reflist_builder *b,
  4998. + const struct v4l2_ctrl_h264_decode_params *dec_params,
  4999. + const struct v4l2_ctrl_h264_slice_params *slice_params,
  5000. + const struct v4l2_ctrl_h264_sps *sps,
  5001. + const struct v4l2_h264_dpb_entry *dpb);
  5002. +
  5003. +/**
  5004. + * v4l2_h264_build_b_ref_lists() - Build the B0/B1 reference lists
  5005. + *
  5006. + * @builder: reference list builder context
  5007. + * @b0_reflist: 16-bytes array used to store the B0 reference list. Each entry
  5008. + * is an index in the DPB
  5009. + * @b1_reflist: 16-bytes array used to store the B1 reference list. Each entry
  5010. + * is an index in the DPB
  5011. + *
  5012. + * This functions builds the B0/B1 reference lists. This procedure is described
  5013. + * in section '8.2.4 Decoding process for reference picture lists construction'
  5014. + * of the H264 spec. This function can be used by H264 decoder drivers that
  5015. + * need to pass B0/B1 reference lists to the hardware.
  5016. + */
  5017. +void
  5018. +v4l2_h264_build_b_ref_lists(const struct v4l2_h264_reflist_builder *builder,
  5019. + u8 *b0_reflist, u8 *b1_reflist);
  5020. +
  5021. +/**
  5022. + * v4l2_h264_build_b_ref_lists() - Build the P reference list
  5023. + *
  5024. + * @builder: reference list builder context
  5025. + * @p_reflist: 16-bytes array used to store the P reference list. Each entry
  5026. + * is an index in the DPB
  5027. + *
  5028. + * This functions builds the P reference lists. This procedure is describe in
  5029. + * section '8.2.4 Decoding process for reference picture lists construction'
  5030. + * of the H264 spec. This function can be used by H264 decoder drivers that
  5031. + * need to pass a P reference list to the hardware.
  5032. + */
  5033. +void
  5034. +v4l2_h264_build_p_ref_list(const struct v4l2_h264_reflist_builder *builder,
  5035. + u8 *reflist);
  5036. +
  5037. +#endif /* _MEDIA_V4L2_H264_H */
  5038. +
Advertisement
Add Comment
Please, Sign In to add comment