162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) Collabora, Ltd.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Based on GSPCA and CODA drivers:
662306a36Sopenharmony_ci * Copyright (C) Jean-Francois Moine (http://moinejf.free.fr)
762306a36Sopenharmony_ci * Copyright (C) 2014 Philipp Zabel, Pengutronix
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/align.h>
1162306a36Sopenharmony_ci#include <linux/build_bug.h>
1262306a36Sopenharmony_ci#include <linux/kernel.h>
1362306a36Sopenharmony_ci#include <linux/string.h>
1462306a36Sopenharmony_ci#include "hantro_jpeg.h"
1562306a36Sopenharmony_ci#include "hantro.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define LUMA_QUANT_OFF		25
1862306a36Sopenharmony_ci#define CHROMA_QUANT_OFF	90
1962306a36Sopenharmony_ci#define HEIGHT_OFF		159
2062306a36Sopenharmony_ci#define WIDTH_OFF		161
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define HUFF_LUMA_DC_OFF	178
2362306a36Sopenharmony_ci#define HUFF_LUMA_AC_OFF	211
2462306a36Sopenharmony_ci#define HUFF_CHROMA_DC_OFF	394
2562306a36Sopenharmony_ci#define HUFF_CHROMA_AC_OFF	427
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* Default tables from JPEG ITU-T.81
2862306a36Sopenharmony_ci * (ISO/IEC 10918-1) Annex K, tables K.1 and K.2
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_cistatic const unsigned char luma_q_table[] = {
3162306a36Sopenharmony_ci	0x10, 0x0b, 0x0a, 0x10, 0x18, 0x28, 0x33, 0x3d,
3262306a36Sopenharmony_ci	0x0c, 0x0c, 0x0e, 0x13, 0x1a, 0x3a, 0x3c, 0x37,
3362306a36Sopenharmony_ci	0x0e, 0x0d, 0x10, 0x18, 0x28, 0x39, 0x45, 0x38,
3462306a36Sopenharmony_ci	0x0e, 0x11, 0x16, 0x1d, 0x33, 0x57, 0x50, 0x3e,
3562306a36Sopenharmony_ci	0x12, 0x16, 0x25, 0x38, 0x44, 0x6d, 0x67, 0x4d,
3662306a36Sopenharmony_ci	0x18, 0x23, 0x37, 0x40, 0x51, 0x68, 0x71, 0x5c,
3762306a36Sopenharmony_ci	0x31, 0x40, 0x4e, 0x57, 0x67, 0x79, 0x78, 0x65,
3862306a36Sopenharmony_ci	0x48, 0x5c, 0x5f, 0x62, 0x70, 0x64, 0x67, 0x63
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistatic const unsigned char chroma_q_table[] = {
4262306a36Sopenharmony_ci	0x11, 0x12, 0x18, 0x2f, 0x63, 0x63, 0x63, 0x63,
4362306a36Sopenharmony_ci	0x12, 0x15, 0x1a, 0x42, 0x63, 0x63, 0x63, 0x63,
4462306a36Sopenharmony_ci	0x18, 0x1a, 0x38, 0x63, 0x63, 0x63, 0x63, 0x63,
4562306a36Sopenharmony_ci	0x2f, 0x42, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
4662306a36Sopenharmony_ci	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
4762306a36Sopenharmony_ci	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
4862306a36Sopenharmony_ci	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
4962306a36Sopenharmony_ci	0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
5062306a36Sopenharmony_ci};
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic const unsigned char zigzag[] = {
5362306a36Sopenharmony_ci	 0,  1,  8, 16,  9,  2,  3, 10,
5462306a36Sopenharmony_ci	17, 24, 32, 25, 18, 11,  4,  5,
5562306a36Sopenharmony_ci	12, 19, 26, 33, 40, 48, 41, 34,
5662306a36Sopenharmony_ci	27, 20, 13,  6,  7, 14, 21, 28,
5762306a36Sopenharmony_ci	35, 42, 49, 56, 57, 50, 43, 36,
5862306a36Sopenharmony_ci	29, 22, 15, 23, 30, 37, 44, 51,
5962306a36Sopenharmony_ci	58, 59, 52, 45, 38, 31, 39, 46,
6062306a36Sopenharmony_ci	53, 60, 61, 54, 47, 55, 62, 63
6162306a36Sopenharmony_ci};
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_cistatic const u32 hw_reorder[] = {
6462306a36Sopenharmony_ci	 0,  8, 16, 24,  1,  9, 17, 25,
6562306a36Sopenharmony_ci	32, 40, 48, 56, 33, 41, 49, 57,
6662306a36Sopenharmony_ci	 2, 10, 18, 26,  3, 11, 19, 27,
6762306a36Sopenharmony_ci	34, 42, 50, 58, 35, 43, 51, 59,
6862306a36Sopenharmony_ci	 4, 12, 20, 28,  5, 13, 21, 29,
6962306a36Sopenharmony_ci	36, 44, 52, 60, 37, 45, 53, 61,
7062306a36Sopenharmony_ci	 6, 14, 22, 30,  7, 15, 23, 31,
7162306a36Sopenharmony_ci	38, 46, 54, 62, 39, 47, 55, 63
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/* Huffman tables are shared with CODA */
7562306a36Sopenharmony_cistatic const unsigned char luma_dc_table[] = {
7662306a36Sopenharmony_ci	0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01,
7762306a36Sopenharmony_ci	0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7862306a36Sopenharmony_ci	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
7962306a36Sopenharmony_ci	0x08, 0x09, 0x0a, 0x0b,
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic const unsigned char chroma_dc_table[] = {
8362306a36Sopenharmony_ci	0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
8462306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
8562306a36Sopenharmony_ci	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
8662306a36Sopenharmony_ci	0x08, 0x09, 0x0a, 0x0b,
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic const unsigned char luma_ac_table[] = {
9062306a36Sopenharmony_ci	0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
9162306a36Sopenharmony_ci	0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
9262306a36Sopenharmony_ci	0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
9362306a36Sopenharmony_ci	0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
9462306a36Sopenharmony_ci	0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
9562306a36Sopenharmony_ci	0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
9662306a36Sopenharmony_ci	0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
9762306a36Sopenharmony_ci	0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
9862306a36Sopenharmony_ci	0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
9962306a36Sopenharmony_ci	0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
10062306a36Sopenharmony_ci	0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
10162306a36Sopenharmony_ci	0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
10262306a36Sopenharmony_ci	0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
10362306a36Sopenharmony_ci	0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
10462306a36Sopenharmony_ci	0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
10562306a36Sopenharmony_ci	0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
10662306a36Sopenharmony_ci	0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
10762306a36Sopenharmony_ci	0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
10862306a36Sopenharmony_ci	0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
10962306a36Sopenharmony_ci	0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
11062306a36Sopenharmony_ci	0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
11162306a36Sopenharmony_ci	0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
11262306a36Sopenharmony_ci	0xf9, 0xfa,
11362306a36Sopenharmony_ci};
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic const unsigned char chroma_ac_table[] = {
11662306a36Sopenharmony_ci	0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04,
11762306a36Sopenharmony_ci	0x07, 0x05, 0x04, 0x04, 0x00, 0x01, 0x02, 0x77,
11862306a36Sopenharmony_ci	0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
11962306a36Sopenharmony_ci	0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
12062306a36Sopenharmony_ci	0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
12162306a36Sopenharmony_ci	0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
12262306a36Sopenharmony_ci	0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
12362306a36Sopenharmony_ci	0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
12462306a36Sopenharmony_ci	0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
12562306a36Sopenharmony_ci	0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
12662306a36Sopenharmony_ci	0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
12762306a36Sopenharmony_ci	0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
12862306a36Sopenharmony_ci	0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
12962306a36Sopenharmony_ci	0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
13062306a36Sopenharmony_ci	0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
13162306a36Sopenharmony_ci	0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
13262306a36Sopenharmony_ci	0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
13362306a36Sopenharmony_ci	0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
13462306a36Sopenharmony_ci	0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
13562306a36Sopenharmony_ci	0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
13662306a36Sopenharmony_ci	0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
13762306a36Sopenharmony_ci	0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
13862306a36Sopenharmony_ci	0xf9, 0xfa,
13962306a36Sopenharmony_ci};
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci/* For simplicity, we keep a pre-formatted JPEG header,
14262306a36Sopenharmony_ci * and we'll use fixed offsets to change the width, height
14362306a36Sopenharmony_ci * quantization tables, etc.
14462306a36Sopenharmony_ci */
14562306a36Sopenharmony_cistatic const unsigned char hantro_jpeg_header[] = {
14662306a36Sopenharmony_ci	/* SOI */
14762306a36Sopenharmony_ci	0xff, 0xd8,
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	/* JFIF-APP0 */
15062306a36Sopenharmony_ci	0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46,
15162306a36Sopenharmony_ci	0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01,
15262306a36Sopenharmony_ci	0x00, 0x00,
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	/* DQT */
15562306a36Sopenharmony_ci	0xff, 0xdb, 0x00, 0x84,
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	0x00,
15862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
15962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	0x01,
16862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	/* SOF */
17862306a36Sopenharmony_ci	0xff, 0xc0, 0x00, 0x11, 0x08, 0x00, 0xf0, 0x01,
17962306a36Sopenharmony_ci	0x40, 0x03, 0x01, 0x22, 0x00, 0x02, 0x11, 0x01,
18062306a36Sopenharmony_ci	0x03, 0x11, 0x01,
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	/* DHT */
18362306a36Sopenharmony_ci	0xff, 0xc4, 0x00, 0x1f, 0x00,
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18662306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18762306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00,
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	/* DHT */
19162306a36Sopenharmony_ci	0xff, 0xc4, 0x00, 0xb5, 0x10,
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	0x00, 0x00,
19462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19662306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19762306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20662306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20762306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	/* DHT */
21862306a36Sopenharmony_ci	0xff, 0xc4, 0x00, 0x1f, 0x01,
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
22362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00,
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	/* DHT */
22662306a36Sopenharmony_ci	0xff, 0xc4, 0x00, 0xb5, 0x11,
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	0x00, 0x00,
22962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23662306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23762306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
23962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24262306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24462306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24662306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24762306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24862306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25062306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	/* COM */
25362306a36Sopenharmony_ci	0xff, 0xfe, 0x00, 0x03, 0x00,
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	/* SOS */
25662306a36Sopenharmony_ci	0xff, 0xda, 0x00, 0x0c, 0x03, 0x01, 0x00, 0x02,
25762306a36Sopenharmony_ci	0x11, 0x03, 0x11, 0x00, 0x3f, 0x00,
25862306a36Sopenharmony_ci};
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci/*
26162306a36Sopenharmony_ci * JPEG_HEADER_SIZE is used in other parts of the driver in lieu of
26262306a36Sopenharmony_ci * "sizeof(hantro_jpeg_header)". The two must be equal.
26362306a36Sopenharmony_ci */
26462306a36Sopenharmony_cistatic_assert(sizeof(hantro_jpeg_header) == JPEG_HEADER_SIZE);
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci/*
26762306a36Sopenharmony_ci * hantro_jpeg_header is padded with a COM segment, so that the payload
26862306a36Sopenharmony_ci * of the SOS segment (the entropy-encoded image scan), which should
26962306a36Sopenharmony_ci * trail the whole header, is 8-byte aligned for the hardware to write
27062306a36Sopenharmony_ci * to directly.
27162306a36Sopenharmony_ci */
27262306a36Sopenharmony_cistatic_assert(IS_ALIGNED(sizeof(hantro_jpeg_header), 8),
27362306a36Sopenharmony_ci	      "Hantro JPEG header size needs to be 8-byte aligned.");
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_cistatic unsigned char jpeg_scale_qp(const unsigned char qp, int scale)
27662306a36Sopenharmony_ci{
27762306a36Sopenharmony_ci	unsigned int temp;
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci	temp = DIV_ROUND_CLOSEST((unsigned int)qp * scale, 100);
28062306a36Sopenharmony_ci	if (temp <= 0)
28162306a36Sopenharmony_ci		temp = 1;
28262306a36Sopenharmony_ci	if (temp > 255)
28362306a36Sopenharmony_ci		temp = 255;
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	return (unsigned char)temp;
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_cistatic void
28962306a36Sopenharmony_cijpeg_scale_quant_table(unsigned char *file_q_tab,
29062306a36Sopenharmony_ci		       unsigned char *reordered_q_tab,
29162306a36Sopenharmony_ci		       const unsigned char *tab, int scale)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	int i;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(zigzag) != JPEG_QUANT_SIZE);
29662306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(hw_reorder) != JPEG_QUANT_SIZE);
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ci	for (i = 0; i < JPEG_QUANT_SIZE; i++) {
29962306a36Sopenharmony_ci		file_q_tab[i] = jpeg_scale_qp(tab[zigzag[i]], scale);
30062306a36Sopenharmony_ci		reordered_q_tab[i] = jpeg_scale_qp(tab[hw_reorder[i]], scale);
30162306a36Sopenharmony_ci	}
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_cistatic void jpeg_set_quality(struct hantro_jpeg_ctx *ctx)
30562306a36Sopenharmony_ci{
30662306a36Sopenharmony_ci	int scale;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	/*
30962306a36Sopenharmony_ci	 * Non-linear scaling factor:
31062306a36Sopenharmony_ci	 * [5,50] -> [1000..100], [51,100] -> [98..0]
31162306a36Sopenharmony_ci	 */
31262306a36Sopenharmony_ci	if (ctx->quality < 50)
31362306a36Sopenharmony_ci		scale = 5000 / ctx->quality;
31462306a36Sopenharmony_ci	else
31562306a36Sopenharmony_ci		scale = 200 - 2 * ctx->quality;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(luma_q_table) != JPEG_QUANT_SIZE);
31862306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(chroma_q_table) != JPEG_QUANT_SIZE);
31962306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(ctx->hw_luma_qtable) != JPEG_QUANT_SIZE);
32062306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(ctx->hw_chroma_qtable) != JPEG_QUANT_SIZE);
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	jpeg_scale_quant_table(ctx->buffer + LUMA_QUANT_OFF,
32362306a36Sopenharmony_ci			       ctx->hw_luma_qtable, luma_q_table, scale);
32462306a36Sopenharmony_ci	jpeg_scale_quant_table(ctx->buffer + CHROMA_QUANT_OFF,
32562306a36Sopenharmony_ci			       ctx->hw_chroma_qtable, chroma_q_table, scale);
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_civoid hantro_jpeg_header_assemble(struct hantro_jpeg_ctx *ctx)
32962306a36Sopenharmony_ci{
33062306a36Sopenharmony_ci	char *buf = ctx->buffer;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	memcpy(buf, hantro_jpeg_header,
33362306a36Sopenharmony_ci	       sizeof(hantro_jpeg_header));
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci	buf[HEIGHT_OFF + 0] = ctx->height >> 8;
33662306a36Sopenharmony_ci	buf[HEIGHT_OFF + 1] = ctx->height;
33762306a36Sopenharmony_ci	buf[WIDTH_OFF + 0] = ctx->width >> 8;
33862306a36Sopenharmony_ci	buf[WIDTH_OFF + 1] = ctx->width;
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci	memcpy(buf + HUFF_LUMA_DC_OFF, luma_dc_table, sizeof(luma_dc_table));
34162306a36Sopenharmony_ci	memcpy(buf + HUFF_LUMA_AC_OFF, luma_ac_table, sizeof(luma_ac_table));
34262306a36Sopenharmony_ci	memcpy(buf + HUFF_CHROMA_DC_OFF, chroma_dc_table,
34362306a36Sopenharmony_ci	       sizeof(chroma_dc_table));
34462306a36Sopenharmony_ci	memcpy(buf + HUFF_CHROMA_AC_OFF, chroma_ac_table,
34562306a36Sopenharmony_ci	       sizeof(chroma_ac_table));
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci	jpeg_set_quality(ctx);
34862306a36Sopenharmony_ci}
349