162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Rockchip RK3288 VPU codec driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2014 Rockchip Electronics Co., Ltd.
662306a36Sopenharmony_ci *	Hertz Wong <hertz.wong@rock-chips.com>
762306a36Sopenharmony_ci *	Herman Chen <herman.chen@rock-chips.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (C) 2014 Google, Inc.
1062306a36Sopenharmony_ci *	Tomasz Figa <tfiga@chromium.org>
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/types.h>
1462306a36Sopenharmony_ci#include <media/v4l2-h264.h>
1562306a36Sopenharmony_ci#include <media/v4l2-mem2mem.h>
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "hantro.h"
1862306a36Sopenharmony_ci#include "hantro_hw.h"
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Size with u32 units. */
2162306a36Sopenharmony_ci#define CABAC_INIT_BUFFER_SIZE		(460 * 2)
2262306a36Sopenharmony_ci#define POC_BUFFER_SIZE			34
2362306a36Sopenharmony_ci#define SCALING_LIST_SIZE		(6 * 16 + 2 * 64)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * For valid and long term reference marking, index are reversed, so bit 31
2762306a36Sopenharmony_ci * indicates the status of the picture 0.
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#define REF_BIT(i)			BIT(32 - 1 - (i))
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* Data structure describing auxiliary buffer format. */
3262306a36Sopenharmony_cistruct hantro_h264_dec_priv_tbl {
3362306a36Sopenharmony_ci	u32 cabac_table[CABAC_INIT_BUFFER_SIZE];
3462306a36Sopenharmony_ci	u32 poc[POC_BUFFER_SIZE];
3562306a36Sopenharmony_ci	u8 scaling_list[SCALING_LIST_SIZE];
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/*
3962306a36Sopenharmony_ci * Constant CABAC table.
4062306a36Sopenharmony_ci * From drivers/media/platform/rk3288-vpu/rk3288_vpu_hw_h264d.c
4162306a36Sopenharmony_ci * in https://chromium.googlesource.com/chromiumos/third_party/kernel,
4262306a36Sopenharmony_ci * chromeos-3.14 branch.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_cistatic const u32 h264_cabac_table[] = {
4562306a36Sopenharmony_ci	0x14f10236, 0x034a14f1, 0x0236034a, 0xe47fe968, 0xfa35ff36, 0x07330000,
4662306a36Sopenharmony_ci	0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
4762306a36Sopenharmony_ci	0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
4862306a36Sopenharmony_ci	0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
4962306a36Sopenharmony_ci	0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
5062306a36Sopenharmony_ci	0x0029003f, 0x003f003f, 0xf7530456, 0x0061f948, 0x0d29033e, 0x000b0137,
5162306a36Sopenharmony_ci	0x0045ef7f, 0xf3660052, 0xf94aeb6b, 0xe57fe17f, 0xe87fee5f, 0xe57feb72,
5262306a36Sopenharmony_ci	0xe27fef7b, 0xf473f07a, 0xf573f43f, 0xfe44f154, 0xf368fd46, 0xf85df65a,
5362306a36Sopenharmony_ci	0xe27fff4a, 0xfa61f95b, 0xec7ffc38, 0xfb52f94c, 0xea7df95d, 0xf557fd4d,
5462306a36Sopenharmony_ci	0xfb47fc3f, 0xfc44f454, 0xf93ef941, 0x083d0538, 0xfe420140, 0x003dfe4e,
5562306a36Sopenharmony_ci	0x01320734, 0x0a23002c, 0x0b26012d, 0x002e052c, 0x1f110133, 0x07321c13,
5662306a36Sopenharmony_ci	0x10210e3e, 0xf36cf164, 0xf365f35b, 0xf45ef658, 0xf054f656, 0xf953f357,
5762306a36Sopenharmony_ci	0xed5e0146, 0x0048fb4a, 0x123bf866, 0xf164005f, 0xfc4b0248, 0xf54bfd47,
5862306a36Sopenharmony_ci	0x0f2ef345, 0x003e0041, 0x1525f148, 0x09391036, 0x003e0c48, 0x18000f09,
5962306a36Sopenharmony_ci	0x08190d12, 0x0f090d13, 0x0a250c12, 0x061d1421, 0x0f1e042d, 0x013a003e,
6062306a36Sopenharmony_ci	0x073d0c26, 0x0b2d0f27, 0x0b2a0d2c, 0x102d0c29, 0x0a311e22, 0x122a0a37,
6162306a36Sopenharmony_ci	0x1133112e, 0x00591aed, 0x16ef1aef, 0x1ee71cec, 0x21e925e5, 0x21e928e4,
6262306a36Sopenharmony_ci	0x26ef21f5, 0x28f129fa, 0x26012911, 0x1efa1b03, 0x1a1625f0, 0x23fc26f8,
6362306a36Sopenharmony_ci	0x26fd2503, 0x26052a00, 0x23102716, 0x0e301b25, 0x153c0c44, 0x0261fd47,
6462306a36Sopenharmony_ci	0xfa2afb32, 0xfd36fe3e, 0x003a013f, 0xfe48ff4a, 0xf75bfb43, 0xfb1bfd27,
6562306a36Sopenharmony_ci	0xfe2c002e, 0xf040f844, 0xf64efa4d, 0xf656f45c, 0xf137f63c, 0xfa3efc41,
6662306a36Sopenharmony_ci	0xf449f84c, 0xf950f758, 0xef6ef561, 0xec54f54f, 0xfa49fc4a, 0xf356f360,
6762306a36Sopenharmony_ci	0xf561ed75, 0xf84efb21, 0xfc30fe35, 0xfd3ef347, 0xf64ff456, 0xf35af261,
6862306a36Sopenharmony_ci	0x0000fa5d, 0xfa54f84f, 0x0042ff47, 0x003efe3c, 0xfe3bfb4b, 0xfd3efc3a,
6962306a36Sopenharmony_ci	0xf742ff4f, 0x00470344, 0x0a2cf93e, 0x0f240e28, 0x101b0c1d, 0x012c1424,
7062306a36Sopenharmony_ci	0x1220052a, 0x01300a3e, 0x112e0940, 0xf468f561, 0xf060f958, 0xf855f955,
7162306a36Sopenharmony_ci	0xf755f358, 0x0442fd4d, 0xfd4cfa4c, 0x0a3aff4c, 0xff53f963, 0xf25f025f,
7262306a36Sopenharmony_ci	0x004cfb4a, 0x0046f54b, 0x01440041, 0xf249033e, 0x043eff44, 0xf34b0b37,
7362306a36Sopenharmony_ci	0x05400c46, 0x0f060613, 0x07100c0e, 0x120d0d0b, 0x0d0f0f10, 0x0c170d17,
7462306a36Sopenharmony_ci	0x0f140e1a, 0x0e2c1128, 0x112f1811, 0x15151916, 0x1f1b161d, 0x13230e32,
7562306a36Sopenharmony_ci	0x0a39073f, 0xfe4dfc52, 0xfd5e0945, 0xf46d24dd, 0x24de20e6, 0x25e22ce0,
7662306a36Sopenharmony_ci	0x22ee22f1, 0x28f121f9, 0x23fb2100, 0x2602210d, 0x17230d3a, 0x1dfd1a00,
7762306a36Sopenharmony_ci	0x161e1ff9, 0x23f122fd, 0x220324ff, 0x2205200b, 0x2305220c, 0x270b1e1d,
7862306a36Sopenharmony_ci	0x221a1d27, 0x13421f15, 0x1f1f1932, 0xef78ec70, 0xee72f555, 0xf15cf259,
7962306a36Sopenharmony_ci	0xe647f151, 0xf2500044, 0xf246e838, 0xe944e832, 0xf54a17f3, 0x1af328f1,
8062306a36Sopenharmony_ci	0x31f22c03, 0x2d062c22, 0x21361352, 0xfd4bff17, 0x0122012b, 0x0036fe37,
8162306a36Sopenharmony_ci	0x003d0140, 0x0044f75c, 0xf26af361, 0xf15af45a, 0xee58f649, 0xf74ff256,
8262306a36Sopenharmony_ci	0xf649f646, 0xf645fb42, 0xf740fb3a, 0x023b15f6, 0x18f51cf8, 0x1cff1d03,
8362306a36Sopenharmony_ci	0x1d092314, 0x1d240e43, 0x14f10236, 0x034a14f1, 0x0236034a, 0xe47fe968,
8462306a36Sopenharmony_ci	0xfa35ff36, 0x07331721, 0x17021500, 0x01090031, 0xdb760539, 0xf34ef541,
8562306a36Sopenharmony_ci	0x013e0c31, 0xfc491132, 0x1240092b, 0x1d001a43, 0x105a0968, 0xd27fec68,
8662306a36Sopenharmony_ci	0x0143f34e, 0xf541013e, 0xfa56ef5f, 0xfa3d092d, 0xfd45fa51, 0xf5600637,
8762306a36Sopenharmony_ci	0x0743fb56, 0x0258003a, 0xfd4cf65e, 0x05360445, 0xfd510058, 0xf943fb4a,
8862306a36Sopenharmony_ci	0xfc4afb50, 0xf948013a, 0x0029003f, 0x003f003f, 0xf7530456, 0x0061f948,
8962306a36Sopenharmony_ci	0x0d29033e, 0x002dfc4e, 0xfd60e57e, 0xe462e765, 0xe943e452, 0xec5ef053,
9062306a36Sopenharmony_ci	0xea6eeb5b, 0xee66f35d, 0xe37ff95c, 0xfb59f960, 0xf36cfd2e, 0xff41ff39,
9162306a36Sopenharmony_ci	0xf75dfd4a, 0xf75cf857, 0xe97e0536, 0x063c063b, 0x0645ff30, 0x0044fc45,
9262306a36Sopenharmony_ci	0xf858fe55, 0xfa4eff4b, 0xf94d0236, 0x0532fd44, 0x0132062a, 0xfc51013f,
9362306a36Sopenharmony_ci	0xfc460043, 0x0239fe4c, 0x0b230440, 0x013d0b23, 0x12190c18, 0x0d1d0d24,
9462306a36Sopenharmony_ci	0xf65df949, 0xfe490d2e, 0x0931f964, 0x09350235, 0x0535fe3d, 0x00380038,
9562306a36Sopenharmony_ci	0xf33ffb3c, 0xff3e0439, 0xfa450439, 0x0e270433, 0x0d440340, 0x013d093f,
9662306a36Sopenharmony_ci	0x07321027, 0x052c0434, 0x0b30fb3c, 0xff3b003b, 0x1621052c, 0x0e2bff4e,
9762306a36Sopenharmony_ci	0x003c0945, 0x0b1c0228, 0x032c0031, 0x002e022c, 0x0233002f, 0x0427023e,
9862306a36Sopenharmony_ci	0x062e0036, 0x0336023a, 0x043f0633, 0x06390735, 0x06340637, 0x0b2d0e24,
9962306a36Sopenharmony_ci	0x0835ff52, 0x0737fd4e, 0x0f2e161f, 0xff541907, 0x1ef91c03, 0x1c042000,
10062306a36Sopenharmony_ci	0x22ff1e06, 0x1e062009, 0x1f131a1b, 0x1a1e2514, 0x1c221146, 0x0143053b,
10162306a36Sopenharmony_ci	0x0943101e, 0x12201223, 0x161d181f, 0x1726122b, 0x14290b3f, 0x093b0940,
10262306a36Sopenharmony_ci	0xff5efe59, 0xf76cfa4c, 0xfe2c002d, 0x0034fd40, 0xfe3bfc46, 0xfc4bf852,
10362306a36Sopenharmony_ci	0xef66f74d, 0x0318002a, 0x00300037, 0xfa3bf947, 0xf453f557, 0xe277013a,
10462306a36Sopenharmony_ci	0xfd1dff24, 0x0126022b, 0xfa37003a, 0x0040fd4a, 0xf65a0046, 0xfc1d051f,
10562306a36Sopenharmony_ci	0x072a013b, 0xfe3afd48, 0xfd51f561, 0x003a0805, 0x0a0e0e12, 0x0d1b0228,
10662306a36Sopenharmony_ci	0x003afd46, 0xfa4ff855, 0x0000f36a, 0xf06af657, 0xeb72ee6e, 0xf262ea6e,
10762306a36Sopenharmony_ci	0xeb6aee67, 0xeb6be96c, 0xe670f660, 0xf45ffb5b, 0xf75dea5e, 0xfb560943,
10862306a36Sopenharmony_ci	0xfc50f655, 0xff46073c, 0x093a053d, 0x0c320f32, 0x12311136, 0x0a29072e,
10962306a36Sopenharmony_ci	0xff330731, 0x08340929, 0x062f0237, 0x0d290a2c, 0x06320535, 0x0d31043f,
11062306a36Sopenharmony_ci	0x0640fe45, 0xfe3b0646, 0x0a2c091f, 0x0c2b0335, 0x0e220a26, 0xfd340d28,
11162306a36Sopenharmony_ci	0x1120072c, 0x07260d32, 0x0a391a2b, 0x0e0b0b0e, 0x090b120b, 0x150917fe,
11262306a36Sopenharmony_ci	0x20f120f1, 0x22eb27e9, 0x2adf29e1, 0x2ee426f4, 0x151d2de8, 0x35d330e6,
11362306a36Sopenharmony_ci	0x41d52bed, 0x27f61e09, 0x121a141b, 0x0039f252, 0xfb4bed61, 0xdd7d1b00,
11462306a36Sopenharmony_ci	0x1c001ffc, 0x1b062208, 0x1e0a1816, 0x21131620, 0x1a1f1529, 0x1a2c172f,
11562306a36Sopenharmony_ci	0x10410e47, 0x083c063f, 0x11411518, 0x17141a17, 0x1b201c17, 0x1c181728,
11662306a36Sopenharmony_ci	0x18201c1d, 0x172a1339, 0x1635163d, 0x0b560c28, 0x0b330e3b, 0xfc4ff947,
11762306a36Sopenharmony_ci	0xfb45f746, 0xf842f644, 0xed49f445, 0xf046f143, 0xec3eed46, 0xf042ea41,
11862306a36Sopenharmony_ci	0xec3f09fe, 0x1af721f7, 0x27f929fe, 0x2d033109, 0x2d1b243b, 0xfa42f923,
11962306a36Sopenharmony_ci	0xf92af82d, 0xfb30f438, 0xfa3cfb3e, 0xf842f84c, 0xfb55fa51, 0xf64df951,
12062306a36Sopenharmony_ci	0xef50ee49, 0xfc4af653, 0xf747f743, 0xff3df842, 0xf242003b, 0x023b15f3,
12162306a36Sopenharmony_ci	0x21f227f9, 0x2efe3302, 0x3c063d11, 0x37222a3e, 0x14f10236, 0x034a14f1,
12262306a36Sopenharmony_ci	0x0236034a, 0xe47fe968, 0xfa35ff36, 0x07331619, 0x22001000, 0xfe090429,
12362306a36Sopenharmony_ci	0xe3760241, 0xfa47f34f, 0x05340932, 0xfd460a36, 0x1a221316, 0x28003902,
12462306a36Sopenharmony_ci	0x29241a45, 0xd37ff165, 0xfc4cfa47, 0xf34f0534, 0x0645f35a, 0x0034082b,
12562306a36Sopenharmony_ci	0xfe45fb52, 0xf660023b, 0x024bfd57, 0xfd640138, 0xfd4afa55, 0x003bfd51,
12662306a36Sopenharmony_ci	0xf956fb5f, 0xff42ff4d, 0x0146fe56, 0xfb48003d, 0x0029003f, 0x003f003f,
12762306a36Sopenharmony_ci	0xf7530456, 0x0061f948, 0x0d29033e, 0x0d0f0733, 0x0250d97f, 0xee5bef60,
12862306a36Sopenharmony_ci	0xe651dd62, 0xe866e961, 0xe577e863, 0xeb6eee66, 0xdc7f0050, 0xfb59f95e,
12962306a36Sopenharmony_ci	0xfc5c0027, 0x0041f154, 0xdd7ffe49, 0xf468f75b, 0xe17f0337, 0x07380737,
13062306a36Sopenharmony_ci	0x083dfd35, 0x0044f94a, 0xf758f367, 0xf35bf759, 0xf25cf84c, 0xf457e96e,
13162306a36Sopenharmony_ci	0xe869f64e, 0xec70ef63, 0xb27fba7f, 0xce7fd27f, 0xfc42fb4e, 0xfc47f848,
13262306a36Sopenharmony_ci	0x023bff37, 0xf946fa4b, 0xf859de77, 0xfd4b2014, 0x1e16d47f, 0x0036fb3d,
13362306a36Sopenharmony_ci	0x003aff3c, 0xfd3df843, 0xe754f24a, 0xfb410534, 0x0239003d, 0xf745f546,
13462306a36Sopenharmony_ci	0x1237fc47, 0x003a073d, 0x09291219, 0x0920052b, 0x092f002c, 0x0033022e,
13562306a36Sopenharmony_ci	0x1326fc42, 0x0f260c2a, 0x09220059, 0x042d0a1c, 0x0a1f21f5, 0x34d5120f,
13662306a36Sopenharmony_ci	0x1c0023ea, 0x26e72200, 0x27ee20f4, 0x66a20000, 0x38f121fc, 0x1d0a25fb,
13762306a36Sopenharmony_ci	0x33e327f7, 0x34de45c6, 0x43c12cfb, 0x200737e3, 0x20010000, 0x1b2421e7,
13862306a36Sopenharmony_ci	0x22e224e4, 0x26e426e5, 0x22ee23f0, 0x22f220f8, 0x25fa2300, 0x1e0a1c12,
13962306a36Sopenharmony_ci	0x1a191d29, 0x004b0248, 0x084d0e23, 0x121f1123, 0x151e112d, 0x142a122d,
14062306a36Sopenharmony_ci	0x1b1a1036, 0x07421038, 0x0b490a43, 0xf674e970, 0xf147f93d, 0x0035fb42,
14162306a36Sopenharmony_ci	0xf54df750, 0xf754f657, 0xde7feb65, 0xfd27fb35, 0xf93df54b, 0xf14def5b,
14262306a36Sopenharmony_ci	0xe76be76f, 0xe47af54c, 0xf62cf634, 0xf639f73a, 0xf048f945, 0xfc45fb4a,
14362306a36Sopenharmony_ci	0xf7560242, 0xf7220120, 0x0b1f0534, 0xfe37fe43, 0x0049f859, 0x03340704,
14462306a36Sopenharmony_ci	0x0a081108, 0x10130325, 0xff3dfb49, 0xff46fc4e, 0x0000eb7e, 0xe97cec6e,
14562306a36Sopenharmony_ci	0xe67ee77c, 0xef69e579, 0xe575ef66, 0xe675e574, 0xdf7af65f, 0xf264f85f,
14662306a36Sopenharmony_ci	0xef6fe472, 0xfa59fe50, 0xfc52f755, 0xf851ff48, 0x05400143, 0x09380045,
14762306a36Sopenharmony_ci	0x01450745, 0xf945fa43, 0xf04dfe40, 0x023dfa43, 0xfd400239, 0xfd41fd42,
14862306a36Sopenharmony_ci	0x003e0933, 0xff42fe47, 0xfe4bff46, 0xf7480e3c, 0x1025002f, 0x12230b25,
14962306a36Sopenharmony_ci	0x0c290a29, 0x02300c29, 0x0d29003b, 0x03321328, 0x03421232, 0x13fa12fa,
15062306a36Sopenharmony_ci	0x0e001af4, 0x1ff021e7, 0x21ea25e4, 0x27e22ae2, 0x2fd62ddc, 0x31de29ef,
15162306a36Sopenharmony_ci	0x200945b9, 0x3fc142c0, 0x4db636d9, 0x34dd29f6, 0x240028ff, 0x1e0e1c1a,
15262306a36Sopenharmony_ci	0x17250c37, 0x0b4125df, 0x27dc28db, 0x26e22edf, 0x2ae228e8, 0x31e326f4,
15362306a36Sopenharmony_ci	0x28f626fd, 0x2efb1f14, 0x1d1e192c, 0x0c300b31, 0x1a2d1616, 0x17161b15,
15462306a36Sopenharmony_ci	0x21141a1c, 0x1e181b22, 0x122a1927, 0x12320c46, 0x15360e47, 0x0b531920,
15562306a36Sopenharmony_ci	0x15311536, 0xfb55fa51, 0xf64df951, 0xef50ee49, 0xfc4af653, 0xf747f743,
15662306a36Sopenharmony_ci	0xff3df842, 0xf242003b, 0x023b11f6, 0x20f32af7, 0x31fb3500, 0x4003440a,
15762306a36Sopenharmony_ci	0x421b2f39, 0xfb470018, 0xff24fe2a, 0xfe34f739, 0xfa3ffc41, 0xfc43f952,
15862306a36Sopenharmony_ci	0xfd51fd4c, 0xf948fa4e, 0xf448f244, 0xfd46fa4c, 0xfb42fb3e, 0x0039fc3d,
15962306a36Sopenharmony_ci	0xf73c0136, 0x023a11f6, 0x20f32af7, 0x31fb3500, 0x4003440a, 0x421b2f39,
16062306a36Sopenharmony_ci	0x14f10236, 0x034a14f1, 0x0236034a, 0xe47fe968, 0xfa35ff36, 0x07331d10,
16162306a36Sopenharmony_ci	0x19000e00, 0xf633fd3e, 0xe5631a10, 0xfc55e866, 0x05390639, 0xef490e39,
16262306a36Sopenharmony_ci	0x1428140a, 0x1d003600, 0x252a0c61, 0xe07fea75, 0xfe4afc55, 0xe8660539,
16362306a36Sopenharmony_ci	0xfa5df258, 0xfa2c0437, 0xf559f167, 0xeb741339, 0x143a0454, 0x0660013f,
16462306a36Sopenharmony_ci	0xfb55f36a, 0x053f064b, 0xfd5aff65, 0x0337fc4f, 0xfe4bf461, 0xf932013c,
16562306a36Sopenharmony_ci	0x0029003f, 0x003f003f, 0xf7530456, 0x0061f948, 0x0d29033e, 0x0722f758,
16662306a36Sopenharmony_ci	0xec7fdc7f, 0xef5bf25f, 0xe754e756, 0xf459ef5b, 0xe17ff24c, 0xee67f35a,
16762306a36Sopenharmony_ci	0xdb7f0b50, 0x054c0254, 0x054efa37, 0x043df253, 0xdb7ffb4f, 0xf568f55b,
16862306a36Sopenharmony_ci	0xe27f0041, 0xfe4f0048, 0xfc5cfa38, 0x0344f847, 0xf362fc56, 0xf458fb52,
16962306a36Sopenharmony_ci	0xfd48fc43, 0xf848f059, 0xf745ff3b, 0x05420439, 0xfc47fe47, 0x023aff4a,
17062306a36Sopenharmony_ci	0xfc2cff45, 0x003ef933, 0xfc2ffa2a, 0xfd29fa35, 0x084cf74e, 0xf5530934,
17162306a36Sopenharmony_ci	0x0043fb5a, 0x0143f148, 0xfb4bf850, 0xeb53eb40, 0xf31fe740, 0xe35e094b,
17262306a36Sopenharmony_ci	0x113ff84a, 0xfb23fe1b, 0x0d5b0341, 0xf945084d, 0xf642033e, 0xfd44ec51,
17362306a36Sopenharmony_ci	0x001e0107, 0xfd17eb4a, 0x1042e97c, 0x11252cee, 0x32deea7f, 0x0427002a,
17462306a36Sopenharmony_ci	0x07220b1d, 0x081f0625, 0x072a0328, 0x08210d2b, 0x0d24042f, 0x0337023a,
17562306a36Sopenharmony_ci	0x063c082c, 0x0b2c0e2a, 0x07300438, 0x04340d25, 0x0931133a, 0x0a300c2d,
17662306a36Sopenharmony_ci	0x00451421, 0x083f23ee, 0x21e71cfd, 0x180a1b00, 0x22f234d4, 0x27e81311,
17762306a36Sopenharmony_ci	0x1f19241d, 0x1821220f, 0x1e141649, 0x1422131f, 0x1b2c1310, 0x0f240f24,
17862306a36Sopenharmony_ci	0x151c1915, 0x1e141f0c, 0x1b10182a, 0x005d0e38, 0x0f391a26, 0xe87fe873,
17962306a36Sopenharmony_ci	0xea52f73e, 0x0035003b, 0xf255f359, 0xf35ef55c, 0xe37feb64, 0xf239f443,
18062306a36Sopenharmony_ci	0xf547f64d, 0xeb55f058, 0xe968f162, 0xdb7ff652, 0xf830f83d, 0xf842f946,
18162306a36Sopenharmony_ci	0xf24bf64f, 0xf753f45c, 0xee6cfc4f, 0xea45f04b, 0xfe3a013a, 0xf34ef753,
18262306a36Sopenharmony_ci	0xfc51f363, 0xf351fa26, 0xf33efa3a, 0xfe3bf049, 0xf64cf356, 0xf753f657,
18362306a36Sopenharmony_ci	0x0000ea7f, 0xe77fe778, 0xe57fed72, 0xe975e776, 0xe675e871, 0xe476e178,
18462306a36Sopenharmony_ci	0xdb7cf65e, 0xf166f663, 0xf36ace7f, 0xfb5c1139, 0xfb56f35e, 0xf45bfe4d,
18562306a36Sopenharmony_ci	0x0047ff49, 0x0440f951, 0x05400f39, 0x01430044, 0xf6430144, 0x004d0240,
18662306a36Sopenharmony_ci	0x0044fb4e, 0x0737053b, 0x02410e36, 0x0f2c053c, 0x0246fe4c, 0xee560c46,
18762306a36Sopenharmony_ci	0x0540f446, 0x0b370538, 0x00450241, 0xfa4a0536, 0x0736fa4c, 0xf552fe4d,
18862306a36Sopenharmony_ci	0xfe4d192a, 0x11f310f7, 0x11f41beb, 0x25e229d8, 0x2ad730d1, 0x27e02ed8,
18962306a36Sopenharmony_ci	0x34cd2ed7, 0x34d92bed, 0x200b3dc9, 0x38d23ece, 0x51bd2dec, 0x23fe1c0f,
19062306a36Sopenharmony_ci	0x22012701, 0x1e111426, 0x122d0f36, 0x004f24f0, 0x25f225ef, 0x2001220f,
19162306a36Sopenharmony_ci	0x1d0f1819, 0x22161f10, 0x23121f1c, 0x2129241c, 0x1b2f153e, 0x121f131a,
19262306a36Sopenharmony_ci	0x24181817, 0x1b10181e, 0x1f1d1629, 0x162a103c, 0x0f340e3c, 0x034ef07b,
19362306a36Sopenharmony_ci	0x15351638, 0x193d1521, 0x1332113d, 0xfd4ef84a, 0xf748f648, 0xee4bf447,
19462306a36Sopenharmony_ci	0xf53ffb46, 0xef4bf248, 0xf043f835, 0xf23bf734, 0xf54409fe, 0x1ef61ffc,
19562306a36Sopenharmony_ci	0x21ff2107, 0x1f0c2517, 0x1f261440, 0xf747f925, 0xf82cf531, 0xf638f43b,
19662306a36Sopenharmony_ci	0xf83ff743, 0xfa44f64f, 0xfd4ef84a, 0xf748f648, 0xee4bf447, 0xf53ffb46,
19762306a36Sopenharmony_ci	0xef4bf248, 0xf043f835, 0xf23bf734, 0xf54409fe, 0x1ef61ffc, 0x21ff2107,
19862306a36Sopenharmony_ci	0x1f0c2517, 0x1f261440
19962306a36Sopenharmony_ci};
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_cistatic void
20262306a36Sopenharmony_ciassemble_scaling_list(struct hantro_ctx *ctx)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	const struct hantro_h264_dec_ctrls *ctrls = &ctx->h264_dec.ctrls;
20562306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_scaling_matrix *scaling = ctrls->scaling;
20662306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_pps *pps = ctrls->pps;
20762306a36Sopenharmony_ci	const size_t num_list_4x4 = ARRAY_SIZE(scaling->scaling_list_4x4);
20862306a36Sopenharmony_ci	const size_t list_len_4x4 = ARRAY_SIZE(scaling->scaling_list_4x4[0]);
20962306a36Sopenharmony_ci	const size_t list_len_8x8 = ARRAY_SIZE(scaling->scaling_list_8x8[0]);
21062306a36Sopenharmony_ci	struct hantro_h264_dec_priv_tbl *tbl = ctx->h264_dec.priv.cpu;
21162306a36Sopenharmony_ci	u32 *dst = (u32 *)tbl->scaling_list;
21262306a36Sopenharmony_ci	const u32 *src;
21362306a36Sopenharmony_ci	int i, j;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	if (!(pps->flags & V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT))
21662306a36Sopenharmony_ci		return;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	for (i = 0; i < num_list_4x4; i++) {
21962306a36Sopenharmony_ci		src = (u32 *)&scaling->scaling_list_4x4[i];
22062306a36Sopenharmony_ci		for (j = 0; j < list_len_4x4 / 4; j++)
22162306a36Sopenharmony_ci			*dst++ = swab32(src[j]);
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	/* Only Intra/Inter Y lists */
22562306a36Sopenharmony_ci	for (i = 0; i < 2; i++) {
22662306a36Sopenharmony_ci		src = (u32 *)&scaling->scaling_list_8x8[i];
22762306a36Sopenharmony_ci		for (j = 0; j < list_len_8x8 / 4; j++)
22862306a36Sopenharmony_ci			*dst++ = swab32(src[j]);
22962306a36Sopenharmony_ci	}
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic void prepare_table(struct hantro_ctx *ctx)
23362306a36Sopenharmony_ci{
23462306a36Sopenharmony_ci	const struct hantro_h264_dec_ctrls *ctrls = &ctx->h264_dec.ctrls;
23562306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_decode_params *dec_param = ctrls->decode;
23662306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_sps *sps = ctrls->sps;
23762306a36Sopenharmony_ci	struct hantro_h264_dec_priv_tbl *tbl = ctx->h264_dec.priv.cpu;
23862306a36Sopenharmony_ci	const struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
23962306a36Sopenharmony_ci	u32 dpb_longterm = 0;
24062306a36Sopenharmony_ci	u32 dpb_valid = 0;
24162306a36Sopenharmony_ci	int i;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	for (i = 0; i < HANTRO_H264_DPB_SIZE; ++i) {
24462306a36Sopenharmony_ci		tbl->poc[i * 2] = dpb[i].top_field_order_cnt;
24562306a36Sopenharmony_ci		tbl->poc[i * 2 + 1] = dpb[i].bottom_field_order_cnt;
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci		if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_VALID))
24862306a36Sopenharmony_ci			continue;
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci		/*
25162306a36Sopenharmony_ci		 * Set up bit maps of valid and long term DPBs.
25262306a36Sopenharmony_ci		 * NOTE: The bits are reversed, i.e. MSb is DPB 0. For frame
25362306a36Sopenharmony_ci		 * decoding, bit 31 to 15 are used, while for field decoding,
25462306a36Sopenharmony_ci		 * all bits are used, with bit 31 being a top field, 30 a bottom
25562306a36Sopenharmony_ci		 * field and so on.
25662306a36Sopenharmony_ci		 */
25762306a36Sopenharmony_ci		if (dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC) {
25862306a36Sopenharmony_ci			if (dpb[i].fields & V4L2_H264_TOP_FIELD_REF)
25962306a36Sopenharmony_ci				dpb_valid |= REF_BIT(i * 2);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci			if (dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF)
26262306a36Sopenharmony_ci				dpb_valid |= REF_BIT(i * 2 + 1);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci			if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) {
26562306a36Sopenharmony_ci				dpb_longterm |= REF_BIT(i * 2);
26662306a36Sopenharmony_ci				dpb_longterm |= REF_BIT(i * 2 + 1);
26762306a36Sopenharmony_ci			}
26862306a36Sopenharmony_ci		} else {
26962306a36Sopenharmony_ci			dpb_valid |= REF_BIT(i);
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci			if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
27262306a36Sopenharmony_ci				dpb_longterm |= REF_BIT(i);
27362306a36Sopenharmony_ci		}
27462306a36Sopenharmony_ci	}
27562306a36Sopenharmony_ci	ctx->h264_dec.dpb_valid = dpb_valid;
27662306a36Sopenharmony_ci	ctx->h264_dec.dpb_longterm = dpb_longterm;
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	if ((dec_param->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC) ||
27962306a36Sopenharmony_ci	    !(sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD)) {
28062306a36Sopenharmony_ci		tbl->poc[32] = ctx->h264_dec.cur_poc;
28162306a36Sopenharmony_ci		tbl->poc[33] = 0;
28262306a36Sopenharmony_ci	} else {
28362306a36Sopenharmony_ci		tbl->poc[32] = dec_param->top_field_order_cnt;
28462306a36Sopenharmony_ci		tbl->poc[33] = dec_param->bottom_field_order_cnt;
28562306a36Sopenharmony_ci	}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	assemble_scaling_list(ctx);
28862306a36Sopenharmony_ci}
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_cistatic bool dpb_entry_match(const struct v4l2_h264_dpb_entry *a,
29162306a36Sopenharmony_ci			    const struct v4l2_h264_dpb_entry *b)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	return a->reference_ts == b->reference_ts;
29462306a36Sopenharmony_ci}
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_cistatic void update_dpb(struct hantro_ctx *ctx)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	const struct v4l2_ctrl_h264_decode_params *dec_param;
29962306a36Sopenharmony_ci	DECLARE_BITMAP(new, ARRAY_SIZE(dec_param->dpb)) = { 0, };
30062306a36Sopenharmony_ci	DECLARE_BITMAP(used, ARRAY_SIZE(dec_param->dpb)) = { 0, };
30162306a36Sopenharmony_ci	unsigned int i, j;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	dec_param = ctx->h264_dec.ctrls.decode;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	/* Disable all entries by default. */
30662306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ctx->h264_dec.dpb); i++)
30762306a36Sopenharmony_ci		ctx->h264_dec.dpb[i].flags = 0;
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	/* Try to match new DPB entries with existing ones by their POCs. */
31062306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(dec_param->dpb); i++) {
31162306a36Sopenharmony_ci		const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci		if (!(ndpb->flags & V4L2_H264_DPB_ENTRY_FLAG_VALID))
31462306a36Sopenharmony_ci			continue;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci		/*
31762306a36Sopenharmony_ci		 * To cut off some comparisons, iterate only on target DPB
31862306a36Sopenharmony_ci		 * entries which are not used yet.
31962306a36Sopenharmony_ci		 */
32062306a36Sopenharmony_ci		for_each_clear_bit(j, used, ARRAY_SIZE(ctx->h264_dec.dpb)) {
32162306a36Sopenharmony_ci			struct v4l2_h264_dpb_entry *cdpb;
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci			cdpb = &ctx->h264_dec.dpb[j];
32462306a36Sopenharmony_ci			if (!dpb_entry_match(cdpb, ndpb))
32562306a36Sopenharmony_ci				continue;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci			*cdpb = *ndpb;
32862306a36Sopenharmony_ci			set_bit(j, used);
32962306a36Sopenharmony_ci			break;
33062306a36Sopenharmony_ci		}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci		if (j == ARRAY_SIZE(ctx->h264_dec.dpb))
33362306a36Sopenharmony_ci			set_bit(i, new);
33462306a36Sopenharmony_ci	}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	/* For entries that could not be matched, use remaining free slots. */
33762306a36Sopenharmony_ci	for_each_set_bit(i, new, ARRAY_SIZE(dec_param->dpb)) {
33862306a36Sopenharmony_ci		const struct v4l2_h264_dpb_entry *ndpb = &dec_param->dpb[i];
33962306a36Sopenharmony_ci		struct v4l2_h264_dpb_entry *cdpb;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci		/*
34262306a36Sopenharmony_ci		 * Both arrays are of the same sizes, so there is no way
34362306a36Sopenharmony_ci		 * we can end up with no space in target array, unless
34462306a36Sopenharmony_ci		 * something is buggy.
34562306a36Sopenharmony_ci		 */
34662306a36Sopenharmony_ci		j = find_first_zero_bit(used, ARRAY_SIZE(ctx->h264_dec.dpb));
34762306a36Sopenharmony_ci		if (WARN_ON(j >= ARRAY_SIZE(ctx->h264_dec.dpb)))
34862306a36Sopenharmony_ci			return;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci		cdpb = &ctx->h264_dec.dpb[j];
35162306a36Sopenharmony_ci		*cdpb = *ndpb;
35262306a36Sopenharmony_ci		set_bit(j, used);
35362306a36Sopenharmony_ci	}
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cidma_addr_t hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
35762306a36Sopenharmony_ci				   unsigned int dpb_idx)
35862306a36Sopenharmony_ci{
35962306a36Sopenharmony_ci	struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
36062306a36Sopenharmony_ci	dma_addr_t dma_addr = 0;
36162306a36Sopenharmony_ci	s32 cur_poc = ctx->h264_dec.cur_poc;
36262306a36Sopenharmony_ci	u32 flags;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	if (dpb[dpb_idx].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
36562306a36Sopenharmony_ci		dma_addr = hantro_get_ref(ctx, dpb[dpb_idx].reference_ts);
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci	if (!dma_addr) {
36862306a36Sopenharmony_ci		struct vb2_v4l2_buffer *dst_buf;
36962306a36Sopenharmony_ci		struct vb2_buffer *buf;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci		/*
37262306a36Sopenharmony_ci		 * If a DPB entry is unused or invalid, address of current
37362306a36Sopenharmony_ci		 * destination buffer is returned.
37462306a36Sopenharmony_ci		 */
37562306a36Sopenharmony_ci		dst_buf = hantro_get_dst_buf(ctx);
37662306a36Sopenharmony_ci		buf = &dst_buf->vb2_buf;
37762306a36Sopenharmony_ci		dma_addr = hantro_get_dec_buf_addr(ctx, buf);
37862306a36Sopenharmony_ci	}
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	flags = dpb[dpb_idx].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD ? 0x2 : 0;
38162306a36Sopenharmony_ci	flags |= abs(dpb[dpb_idx].top_field_order_cnt - cur_poc) <
38262306a36Sopenharmony_ci		 abs(dpb[dpb_idx].bottom_field_order_cnt - cur_poc) ?
38362306a36Sopenharmony_ci		 0x1 : 0;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	return dma_addr | flags;
38662306a36Sopenharmony_ci}
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ciu16 hantro_h264_get_ref_nbr(struct hantro_ctx *ctx, unsigned int dpb_idx)
38962306a36Sopenharmony_ci{
39062306a36Sopenharmony_ci	const struct v4l2_h264_dpb_entry *dpb = &ctx->h264_dec.dpb[dpb_idx];
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	if (!(dpb->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
39362306a36Sopenharmony_ci		return 0;
39462306a36Sopenharmony_ci	return dpb->frame_num;
39562306a36Sopenharmony_ci}
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci/*
39862306a36Sopenharmony_ci * Removes all references with the same parity as the current picture from the
39962306a36Sopenharmony_ci * reference list. The remaining list will have references with the opposite
40062306a36Sopenharmony_ci * parity. This is effectively a deduplication of references since each buffer
40162306a36Sopenharmony_ci * stores two fields. For this reason, each buffer is found twice in the
40262306a36Sopenharmony_ci * reference list.
40362306a36Sopenharmony_ci *
40462306a36Sopenharmony_ci * This technique has been chosen through trial and error. This simple approach
40562306a36Sopenharmony_ci * resulted in the highest conformance score. Note that this method may suffer
40662306a36Sopenharmony_ci * worse quality in the case an opposite reference frame has been lost. If this
40762306a36Sopenharmony_ci * becomes a problem in the future, it should be possible to add a preprocessing
40862306a36Sopenharmony_ci * to identify un-paired fields and avoid removing them.
40962306a36Sopenharmony_ci */
41062306a36Sopenharmony_cistatic void deduplicate_reflist(struct v4l2_h264_reflist_builder *b,
41162306a36Sopenharmony_ci				struct v4l2_h264_reference *reflist)
41262306a36Sopenharmony_ci{
41362306a36Sopenharmony_ci	int write_idx = 0;
41462306a36Sopenharmony_ci	int i;
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	if (b->cur_pic_fields == V4L2_H264_FRAME_REF) {
41762306a36Sopenharmony_ci		write_idx = b->num_valid;
41862306a36Sopenharmony_ci		goto done;
41962306a36Sopenharmony_ci	}
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	for (i = 0; i < b->num_valid; i++) {
42262306a36Sopenharmony_ci		if (!(b->cur_pic_fields == reflist[i].fields)) {
42362306a36Sopenharmony_ci			reflist[write_idx++] = reflist[i];
42462306a36Sopenharmony_ci			continue;
42562306a36Sopenharmony_ci		}
42662306a36Sopenharmony_ci	}
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_cidone:
42962306a36Sopenharmony_ci	/* Should not happen unless we have a bug in the reflist builder. */
43062306a36Sopenharmony_ci	if (WARN_ON(write_idx > 16))
43162306a36Sopenharmony_ci		write_idx = 16;
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	/* Clear the remaining, some streams fails otherwise */
43462306a36Sopenharmony_ci	for (; write_idx < 16; write_idx++)
43562306a36Sopenharmony_ci		reflist[write_idx].index = 15;
43662306a36Sopenharmony_ci}
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ciint hantro_h264_dec_prepare_run(struct hantro_ctx *ctx)
43962306a36Sopenharmony_ci{
44062306a36Sopenharmony_ci	struct hantro_h264_dec_hw_ctx *h264_ctx = &ctx->h264_dec;
44162306a36Sopenharmony_ci	struct hantro_h264_dec_ctrls *ctrls = &h264_ctx->ctrls;
44262306a36Sopenharmony_ci	struct v4l2_h264_reflist_builder reflist_builder;
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_ci	hantro_start_prepare_run(ctx);
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	ctrls->scaling =
44762306a36Sopenharmony_ci		hantro_get_ctrl(ctx, V4L2_CID_STATELESS_H264_SCALING_MATRIX);
44862306a36Sopenharmony_ci	if (WARN_ON(!ctrls->scaling))
44962306a36Sopenharmony_ci		return -EINVAL;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	ctrls->decode =
45262306a36Sopenharmony_ci		hantro_get_ctrl(ctx, V4L2_CID_STATELESS_H264_DECODE_PARAMS);
45362306a36Sopenharmony_ci	if (WARN_ON(!ctrls->decode))
45462306a36Sopenharmony_ci		return -EINVAL;
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	ctrls->sps =
45762306a36Sopenharmony_ci		hantro_get_ctrl(ctx, V4L2_CID_STATELESS_H264_SPS);
45862306a36Sopenharmony_ci	if (WARN_ON(!ctrls->sps))
45962306a36Sopenharmony_ci		return -EINVAL;
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci	ctrls->pps =
46262306a36Sopenharmony_ci		hantro_get_ctrl(ctx, V4L2_CID_STATELESS_H264_PPS);
46362306a36Sopenharmony_ci	if (WARN_ON(!ctrls->pps))
46462306a36Sopenharmony_ci		return -EINVAL;
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	/* Update the DPB with new refs. */
46762306a36Sopenharmony_ci	update_dpb(ctx);
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	/* Build the P/B{0,1} ref lists. */
47062306a36Sopenharmony_ci	v4l2_h264_init_reflist_builder(&reflist_builder, ctrls->decode,
47162306a36Sopenharmony_ci				       ctrls->sps, ctx->h264_dec.dpb);
47262306a36Sopenharmony_ci	h264_ctx->cur_poc = reflist_builder.cur_pic_order_count;
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci	/* Prepare data in memory. */
47562306a36Sopenharmony_ci	prepare_table(ctx);
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	v4l2_h264_build_p_ref_list(&reflist_builder, h264_ctx->reflists.p);
47862306a36Sopenharmony_ci	v4l2_h264_build_b_ref_lists(&reflist_builder, h264_ctx->reflists.b0,
47962306a36Sopenharmony_ci				    h264_ctx->reflists.b1);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	/*
48262306a36Sopenharmony_ci	 * Reduce ref lists to at most 16 entries, Hantro hardware will deduce
48362306a36Sopenharmony_ci	 * the actual picture lists in field through the dpb_valid,
48462306a36Sopenharmony_ci	 * dpb_longterm bitmap along with the current frame parity.
48562306a36Sopenharmony_ci	 */
48662306a36Sopenharmony_ci	if (reflist_builder.cur_pic_fields != V4L2_H264_FRAME_REF) {
48762306a36Sopenharmony_ci		deduplicate_reflist(&reflist_builder, h264_ctx->reflists.p);
48862306a36Sopenharmony_ci		deduplicate_reflist(&reflist_builder, h264_ctx->reflists.b0);
48962306a36Sopenharmony_ci		deduplicate_reflist(&reflist_builder, h264_ctx->reflists.b1);
49062306a36Sopenharmony_ci	}
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	return 0;
49362306a36Sopenharmony_ci}
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_civoid hantro_h264_dec_exit(struct hantro_ctx *ctx)
49662306a36Sopenharmony_ci{
49762306a36Sopenharmony_ci	struct hantro_dev *vpu = ctx->dev;
49862306a36Sopenharmony_ci	struct hantro_h264_dec_hw_ctx *h264_dec = &ctx->h264_dec;
49962306a36Sopenharmony_ci	struct hantro_aux_buf *priv = &h264_dec->priv;
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_ci	dma_free_coherent(vpu->dev, priv->size, priv->cpu, priv->dma);
50262306a36Sopenharmony_ci}
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ciint hantro_h264_dec_init(struct hantro_ctx *ctx)
50562306a36Sopenharmony_ci{
50662306a36Sopenharmony_ci	struct hantro_dev *vpu = ctx->dev;
50762306a36Sopenharmony_ci	struct hantro_h264_dec_hw_ctx *h264_dec = &ctx->h264_dec;
50862306a36Sopenharmony_ci	struct hantro_aux_buf *priv = &h264_dec->priv;
50962306a36Sopenharmony_ci	struct hantro_h264_dec_priv_tbl *tbl;
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci	priv->cpu = dma_alloc_coherent(vpu->dev, sizeof(*tbl), &priv->dma,
51262306a36Sopenharmony_ci				       GFP_KERNEL);
51362306a36Sopenharmony_ci	if (!priv->cpu)
51462306a36Sopenharmony_ci		return -ENOMEM;
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	priv->size = sizeof(*tbl);
51762306a36Sopenharmony_ci	tbl = priv->cpu;
51862306a36Sopenharmony_ci	memcpy(tbl->cabac_table, h264_cabac_table, sizeof(tbl->cabac_table));
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	return 0;
52162306a36Sopenharmony_ci}
522