• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define FWHT_VERSION 3
60 
61 /* Set if this is an interlaced format */
62 #define FWHT_FL_IS_INTERLACED		BIT(0)
63 /* Set if this is a bottom-first (NTSC) interlaced format */
64 #define FWHT_FL_IS_BOTTOM_FIRST		BIT(1)
65 /* Set if each 'frame' contains just one field */
66 #define FWHT_FL_IS_ALTERNATE		BIT(2)
67 /*
68  * If FWHT_FL_IS_ALTERNATE was set, then this is set if this
69  * 'frame' is the bottom field, else it is the top field.
70  */
71 #define FWHT_FL_IS_BOTTOM_FIELD		BIT(3)
72 /* Set if this frame is uncompressed */
73 #define FWHT_FL_LUMA_IS_UNCOMPRESSED	BIT(4)
74 #define FWHT_FL_CB_IS_UNCOMPRESSED	BIT(5)
75 #define FWHT_FL_CR_IS_UNCOMPRESSED	BIT(6)
76 #define FWHT_FL_CHROMA_FULL_HEIGHT	BIT(7)
77 #define FWHT_FL_CHROMA_FULL_WIDTH	BIT(8)
78 #define FWHT_FL_ALPHA_IS_UNCOMPRESSED	BIT(9)
79 #define FWHT_FL_I_FRAME			BIT(10)
80 
81 /* A 4-values flag - the number of components - 1 */
82 #define FWHT_FL_COMPONENTS_NUM_MSK	GENMASK(18, 16)
83 #define FWHT_FL_COMPONENTS_NUM_OFFSET	16
84 
85 #define FWHT_FL_PIXENC_MSK	GENMASK(20, 19)
86 #define FWHT_FL_PIXENC_OFFSET	19
87 #define FWHT_FL_PIXENC_YUV	(1 << FWHT_FL_PIXENC_OFFSET)
88 #define FWHT_FL_PIXENC_RGB	(2 << FWHT_FL_PIXENC_OFFSET)
89 #define FWHT_FL_PIXENC_HSV	(3 << FWHT_FL_PIXENC_OFFSET)
90 
91 /*
92  * A macro to calculate the needed padding in order to make sure
93  * both luma and chroma components resolutions are rounded up to
94  * a multiple of 8
95  */
96 #define vic_round_dim(dim, div) (round_up((dim) / (div), 8) * (div))
97 
98 struct fwht_cframe_hdr {
99 	u32 magic1;
100 	u32 magic2;
101 	__be32 version;
102 	__be32 width, height;
103 	__be32 flags;
104 	__be32 colorspace;
105 	__be32 xfer_func;
106 	__be32 ycbcr_enc;
107 	__be32 quantization;
108 	__be32 size;
109 };
110 
111 struct fwht_cframe {
112 	u16 i_frame_qp;
113 	u16 p_frame_qp;
114 	__be16 *rlc_data;
115 	s16 coeffs[8 * 8];
116 	s16 de_coeffs[8 * 8];
117 	s16 de_fwht[8 * 8];
118 	u32 size;
119 };
120 
121 struct fwht_raw_frame {
122 	unsigned int width_div;
123 	unsigned int height_div;
124 	unsigned int luma_alpha_step;
125 	unsigned int chroma_step;
126 	unsigned int components_num;
127 	u8 *buf;
128 	u8 *luma, *cb, *cr, *alpha;
129 };
130 
131 #define FWHT_FRAME_PCODED	BIT(0)
132 #define FWHT_FRAME_UNENCODED	BIT(1)
133 #define FWHT_LUMA_UNENCODED	BIT(2)
134 #define FWHT_CB_UNENCODED	BIT(3)
135 #define FWHT_CR_UNENCODED	BIT(4)
136 #define FWHT_ALPHA_UNENCODED	BIT(5)
137 
138 u32 fwht_encode_frame(struct fwht_raw_frame *frm,
139 		      struct fwht_raw_frame *ref_frm,
140 		      struct fwht_cframe *cf,
141 		      bool is_intra, bool next_is_intra,
142 		      unsigned int width, unsigned int height,
143 		      unsigned int stride, unsigned int chroma_stride);
144 bool fwht_decode_frame(struct fwht_cframe *cf, u32 hdr_flags,
145 		unsigned int components_num, unsigned int width,
146 		unsigned int height, const struct fwht_raw_frame *ref,
147 		unsigned int ref_stride, unsigned int ref_chroma_stride,
148 		struct fwht_raw_frame *dst, unsigned int dst_stride,
149 		unsigned int dst_chroma_stride);
150 #endif
151