xref: /linux/drivers/media/test-drivers/vicodec/codec-fwht.h (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /*
3  * Copyright 2016 Tom aan de Wiel
4  * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  */
6 
7 #ifndef CODEC_FWHT_H
8 #define CODEC_FWHT_H
9 
10 #include <linux/types.h>
11 #include <linux/bitops.h>
12 #include <asm/byteorder.h>
13 
14 /*
15  * The compressed format consists of a fwht_cframe_hdr struct followed by the
16  * compressed frame data. The header contains the size of that data.
17  * Each Y, Cb and Cr plane is compressed separately. If the compressed
18  * size of each plane becomes larger than the uncompressed size, then
19  * that plane is stored uncompressed and the corresponding bit is set
20  * in the flags field of the header.
21  *
22  * Each compressed plane consists of macroblocks and each macroblock
23  * is run-length-encoded. Each macroblock starts with a 16 bit value.
24  * Bit 15 indicates if this is a P-coded macroblock (1) or not (0).
25  * P-coded macroblocks contain a delta against the previous frame.
26  *
27  * Bits 1-12 contain a number. If non-zero, then this same macroblock
28  * repeats that number of times. This results in a high degree of
29  * compression for generated images like colorbars.
30  *
31  * Following this macroblock header the MB coefficients are run-length
32  * encoded: the top 12 bits contain the coefficient, the bottom 4 bits
33  * tell how many times this coefficient occurs. The value 0xf indicates
34  * that the remainder of the macroblock should be filled with zeroes.
35  *
36  * All 16 and 32 bit values are stored in big-endian (network) order.
37  *
38  * Each fwht_cframe_hdr starts with an 8 byte magic header that is
39  * guaranteed not to occur in the compressed frame data. This header
40  * can be used to sync to the next frame.
41  *
42  * This codec uses the Fast Walsh Hadamard Transform. Tom aan de Wiel
43  * developed this as part of a university project, specifically for use
44  * with this driver. His project report can be found here:
45  *
46  * https://hverkuil.home.xs4all.nl/fwht.pdf
47  */
48 
49 /*
50  * This is a sequence of 8 bytes with the low 4 bits set to 0xf.
51  *
52  * This sequence cannot occur in the encoded data
53  *
54  * Note that these two magic values are symmetrical so endian issues here.
55  */
56 #define FWHT_MAGIC1 0x4f4f4f4f
57 #define FWHT_MAGIC2 0xffffffff
58 
59 /*
60  * A macro to calculate the needed padding in order to make sure
61  * both luma and chroma components resolutions are rounded up to
62  * a multiple of 8
63  */
64 #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div))
65 
66 struct fwht_cframe_hdr {
67 	u32 magic1;
68 	u32 magic2;
69 	__be32 version;
70 	__be32 width, height;
71 	__be32 flags;
72 	__be32 colorspace;
73 	__be32 xfer_func;
74 	__be32 ycbcr_enc;
75 	__be32 quantization;
76 	__be32 size;
77 };
78 
79 struct fwht_cframe {
80 	u16 i_frame_qp;
81 	u16 p_frame_qp;
82 	__be16 *rlc_data;
83 	s16 coeffs[8 * 8];
84 	s16 de_coeffs[8 * 8];
85 	s16 de_fwht[8 * 8];
86 	u32 size;
87 };
88 
89 struct fwht_raw_frame {
90 	unsigned int width_div;
91 	unsigned int height_div;
92 	unsigned int luma_alpha_step;
93 	unsigned int chroma_step;
94 	unsigned int components_num;
95 	u8 *buf;
96 	u8 *luma, *cb, *cr, *alpha;
97 };
98 
99 #define FWHT_FRAME_PCODED	BIT(0)
100 #define FWHT_FRAME_UNENCODED	BIT(1)
101 #define FWHT_LUMA_UNENCODED	BIT(2)
102 #define FWHT_CB_UNENCODED	BIT(3)
103 #define FWHT_CR_UNENCODED	BIT(4)
104 #define FWHT_ALPHA_UNENCODED	BIT(5)
105 
106 u32 fwht_encode_frame(struct fwht_raw_frame *frm,
107 		      struct fwht_raw_frame *ref_frm,
108 		      struct fwht_cframe *cf,
109 		      bool is_intra, bool next_is_intra,
110 		      unsigned int width, unsigned int height,
111 		      unsigned int stride, unsigned int chroma_stride);
112 bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags,
113 		unsigned int components_num, unsigned int width,
114 		unsigned int height, const struct fwht_raw_frame *ref,
115 		unsigned int ref_stride, unsigned int ref_chroma_stride,
116 		struct fwht_raw_frame *dst, unsigned int dst_stride,
117 		unsigned int dst_chroma_stride);
118 #endif
119